3 releases (breaking)

0.3.0 Jul 9, 2024
0.2.0 Nov 25, 2023
0.1.0 Mar 1, 2023

#1844 in Database interfaces

Download history 93/week @ 2024-07-22 58/week @ 2024-08-05 8/week @ 2024-09-02 1/week @ 2024-09-09 8/week @ 2024-09-16 17/week @ 2024-09-23 13/week @ 2024-09-30

139 downloads per month

MIT license

50KB
940 lines

yrs-lmdb

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

Read the documentation for further examples.


lib.rs:

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

Example

use std::sync::Arc;
use lmdb_rs::core::DbCreate;
use lmdb_rs::Environment;
use yrs::{Doc, Text, Transact};
use yrs_kvstore::DocOps;
use yrs_lmdb::LmdbStore;

let env = Arc::new(Environment::new()
    .autocreate_dir(true)
    .max_dbs(4)
    .open("my-lmdb-dir", 0o777)
    .unwrap());
let h = Arc::new(env.create_db("yrs", DbCreate).unwrap());

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

// restore document state from DB
{
  let db_txn = env.get_reader().unwrap();
  let db = LmdbStore::from(db_txn.bind(&h));
  db.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 = env.new_transaction().unwrap();
  let db = LmdbStore::from(db_txn.bind(&h));
  let doc = db.flush_doc_with("my-doc-name", yrs::Options::default()).unwrap();
  db_txn.commit().unwrap(); // flush may modify internal store state
}

// configure document to persist every update and
// occassionaly compact them into document state
let sub = {
  let env = env.clone();
  let h = h.clone();
  let options = doc.options().clone();
  doc.observe_update_v1(move |_,e| {
      let db_txn = env.new_transaction().unwrap();
      let db = LmdbStore::from(db_txn.bind(&h));
      let seq_nr = db.push_update("my-doc-name", &e.update).unwrap();
      if seq_nr % 64 == 0 {
          // occassinally merge updates into the document state
          db.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

~3–4.5MB
~92K SLoC