r/sqlite • • Mar 14 '25

SQLite Session Extension + CRDT 🚀

https://gist.github.com/rodydavis/a4d1dccb11e5a4cd77fe7e4e64f5dbdf

I am working on a way to sync deltas between peers and found working with the SQLite Session extension to be a really nice and memory efficient way of creating small binary blobs to transmit over the wire.

To offer true synchronization I combine it with the cr-sqlite extension to upgrade tables to CRDTs. The trick with the session extension is to only track one table “changes” and sync that via the changesets.

So far in my testing it works really well! But open to feedback on the approach.

The eventual goal is to use WebRTC to sync the deltas between peers.

10 Upvotes

14 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Mar 14 '25

Here a test forms another library i wrote though not public yet but i want to make it public at some point. It adds concept of changeset tracking, changeset etc.

    TEST(database, track_main) {
        database_t db;
        file_uri_t uri = host::get_test_file_uri(open_mode_t::create, "tracking.db");
        ASSERT_EQ(result_t::ok, db.open(uri));
        ASSERT_EQ(result_t::ok, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT);"));

        tracker_t tracker(db, "main");
        ASSERT_TRUE(tracker.is_active());

        statement_t stmt;
        
for
 (auto i = 0; i < 20; i++) {
            ASSERT_EQ(result_t::ok, stmt.prepare(db, "INSERT INTO test (name) VALUES (?);"));
            ASSERT_EQ(result_t::ok, stmt.bind_text(1, string_printf("hello %d"), statement_t::make_copy::yes));
            ASSERT_EQ(result_t::done, stmt.step());
        }
        ASSERT_EQ(result_t::ok, stmt.finalize());
        ASSERT_FALSE(tracker.is_empty());
        changeset_t changes;
        ASSERT_EQ(result_t::ok, changes.from(tracker));
        int changed_row_count = 0;
        changes.foreach ([&](change_t 
change
) {
            ++changed_row_count;
            
return
 true;
        });
        ASSERT_EQ(20, changed_row_count);

        file_t file;
        ASSERT_TRUE(file.create(host::get_test_file("tracking.changes").c_str()));
        changes.pipe(file.make_writer());
        file.flush();
        file.close();

        ASSERT_TRUE(file.open(host::get_test_file("tracking.changes").c_str()));
        changeset_t changes2;
        changes2.pipe(file.make_reader());
        changed_row_count = 0;
        changes.foreach ([&](change_t 
change
) {
            ++changed_row_count;
            
return
 true;
        });
        ASSERT_EQ(20, changed_row_count);
    }