#yrs #crdt #persistence #rocksdb #back-end

yrs-rocksdb

Persistence layer over Yrs documents for RocksDB backend

2 unstable releases

0.2.0 Nov 25, 2023
0.1.0 Mar 1, 2023

#1913 in Database interfaces

32 downloads per month

MIT license

48KB
888 lines

yrs-rocksdb

yrs-rocksdb is a persistence layer allowing to store Yrs documents and providing convenient utility functions to work with them, using RocksDB for persistent backed.

Read the documentation for further examples.


lib.rs:

yrs-rocksdb is a persistence layer allowing to store Yrs documents and providing convenient utility functions to work with them, using RocksDB for persistent backed.

Example

use std::sync::Arc;
use rocksdb::TransactionDB;
use yrs::{Doc, Text, Transact};
use yrs_kvstore::DocOps;
use yrs_rocksdb::RocksDBStore;

let db: Arc<TransactionDB> = Arc::new(TransactionDB::open_default("my-db-path").unwrap());

let doc = Doc::new();
let text = doc.get_or_insert_text("text");

// restore document state from DB
{
  let db_txn = RocksDBStore::from(db.transaction());
  db_txn.load_doc("my-doc-name", &mut doc.transact_mut()).unwrap();
}

// another options is to flush document state right away, but
// this requires a read-write transaction
{
  let db_txn = RocksDBStore::from(db.transaction());
  let doc = db_txn.flush_doc_with("my-doc-name", yrs::Options::default()).unwrap();
  db_txn.commit().unwrap(); // flush may change store state
}

// configure document to persist every update and
// occassionaly compact them into document state
let sub = {
  let db = db.clone();
  let options = doc.options().clone();
  doc.observe_update_v1(move |_,e| {
      let db_txn = RocksDBStore::from(db.transaction());
      let seq_nr = db_txn.push_update("my-doc-name", &e.update).unwrap();
      if seq_nr % 64 == 0 {
          // occassinally merge updates into the document state
          db_txn.flush_doc_with("my-doc-name", options.clone()).unwrap();
      }
      db_txn.commit().unwrap();
  })
};

text.insert(&mut doc.transact_mut(), 0, "a");
text.insert(&mut doc.transact_mut(), 1, "b");
text.insert(&mut doc.transact_mut(), 2, "c");

Dependencies

~29MB
~592K SLoC