Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve flexibility of replication hook #89

Merged
merged 16 commits into from
May 8, 2013
Merged

Improve flexibility of replication hook #89

merged 16 commits into from
May 8, 2013

Conversation

kspangsege
Copy link
Contributor

Improve the flexibility of the replication "hook" in the core library. The purpose is to make it able to support different kinds of replication implementations, and in particular, the following simplified scheme:

Here is an example of how it could look when an app starts for the first time:

* Client create SharedGroup with URL
    C: opens socket to server
    S: sends entire db file
    C: listens to changes on socket
* Other clients change the db
    S: Sends changesets to clients (change sets are sequentially numbered)
    C: Receive changesets and apply them to db
* Client wants to change db
    C: Start write transaction
        C: Sends write request to server (including last changeset id)
        S: Start write transaction (will wait for any other writer to finish, and then blocks so only current client can write)
            S: If changeset id is less that current, sends missing changes
            C: Return control to application within write transaction
            C: App does it's changes and commits
            C: Sends changeset to server
            S: Apply changeset and commits write transaction
        S: Send OK to client
        C: Commit changes locally and close write transaction

The following example illustrates precisely what the improved replication hook consists of:

#include <algorithm>

#include <tightdb/string_buffer.hpp>
#include <tightdb/group_shared.hpp>


using namespace std;
using namespace tightdb;


namespace {

class ReplicationImpl: public Replication {
public:
    class Provider: public Replication::Provider {
    public:
        ReplicationImpl* new_instance() TIGHTDB_OVERRIDE
        {
            return new ReplicationImpl();
        }
    };

protected:
    string do_get_database_path() TIGHTDB_OVERRIDE
    {
        return "repl.tightdb";
    }

    void do_begin_write_transact() TIGHTDB_OVERRIDE
    {
        m_transact_log_free_begin = m_transact_log_free_end = m_buffer.data();
    }

    void do_commit_write_transact() TIGHTDB_OVERRIDE
    {
        cout << "---------------------------------\n";
        cout << string(m_buffer.data(), m_transact_log_free_begin) << '\n';
    }

    void do_rollback_write_transact() TIGHTDB_OVERRIDE
    {
        cout << "---------------------------------\n";
        cout << "rollback\n";
    }

    void do_interrupt() TIGHTDB_OVERRIDE
    {
        cout << "---------------------------------\n";
        cout << "interrupt\n";
    }

    void do_clear_interrupt() TIGHTDB_OVERRIDE
    {
        cout << "---------------------------------\n";
        cout << "clear interrupt\n";
    }

    void do_transact_log_reserve(size_t n) TIGHTDB_OVERRIDE
    {
        size_t used = m_transact_log_free_begin - m_buffer.data();
        size_t free = m_buffer.size() - used;
        if (free < n) {
            m_buffer.resize(used + n);
        }
        m_transact_log_free_begin = m_buffer.data() + used;
        m_transact_log_free_end   = m_buffer.data() + m_buffer.size();
    }

    void do_transact_log_append(const char* data, size_t size) TIGHTDB_OVERRIDE
    {
        do_transact_log_reserve(size);
        m_transact_log_free_begin = copy(data, data+size, m_transact_log_free_begin);
    }

private:
    StringBuffer m_buffer;
};


TIGHTDB_TABLE_2(Test,
                hilbert, Int,
                banach,  Int)

}


int main()
{
    ReplicationImpl::Provider rp;
    SharedGroup sg(rp);
    WriteTransaction wt(sg);
    Test::Ref t = wt.get_table<Test>("test");
    t->add(1, 2);
    wt.commit();
}

/cc @rrrlasse

@ghost ghost assigned astigsen Apr 30, 2013
@bmunkholm
Copy link
Contributor

+1

bmunkholm pushed a commit that referenced this pull request May 8, 2013
Improve flexibility of replication hook
@bmunkholm bmunkholm merged commit f11d716 into realm:master May 8, 2013
tgoyne pushed a commit that referenced this pull request Jul 11, 2018
update comments to reflect that Rows are now Objs
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants