y-crdt / yrs-persistence

Native persistence providers for Yrs CRDT
10 stars 4 forks source link

How do I incrementally update doc_state and state_vector #4

Open huang12zheng opened 1 year ago

huang12zheng commented 1 year ago

It seems to update doc_state and state_vector in full every time, how can it be incremental?

https://github.com/y-crdt/yrs-persistence/blob/a94b086b987a066d98b7d36a13bd540fca1722ee/yrs-kvstore/src/lib.rs

/// Inserts or updates a document given it's read transaction and name. lib0 v1 encoding is
    /// used for storing the document.
    ///
    /// This feature requires a write capabilities from the database transaction.
    fn insert_doc<K: AsRef<[u8]> + ?Sized, T: ReadTxn>(
        &self,
        name: &K,
        txn: &T,
    ) -> Result<(), Error> {
        let doc_state = txn.encode_diff_v1(&StateVector::default());
        let state_vector = txn.state_vector().encode_v1();
        self.insert_doc_raw_v1(name.as_ref(), &doc_state, &state_vector)
    }
huang12zheng commented 1 year ago

In other words, whether doc_state and state_vector can be split into multiple KVs, then only the updated kv needs to be updated.

let mut diff = Self::diff_state_vectors(&local_sv, sv);

for (client, clock) in diff {
    let blocks = self.blocks.get(&client).unwrap();

    let start = {
        let clock = clock.max(blocks.first().id().clock); // make sure the first id exists
        blocks.find_pivot(clock).unwrap()
    }
    for i in (start + 1)..blocks.len() {
        let v = blocks.get(i);
        db.set("$name-$client-$i","%v")
    }
}