fix: calc change count (#556)
Some checks failed
Release WASM / Release (push) Has been cancelled
Test All / build (push) Has been cancelled

* fix: calc change count

* fix: len ops
This commit is contained in:
Zixuan Chen 2024-11-13 16:52:41 +08:00 committed by GitHub
parent 4f2bbee2a7
commit 35e7ea5f54
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 47 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
"loro-crdt": patch
---
Add changeCount and opCount methods

View file

@ -1757,7 +1757,6 @@ impl RichtextState {
self.get_index_from_cursor(right, PosType::Entity).unwrap()
}
#[tracing::instrument(level = "trace", skip(self))]
pub fn get_index_from_cursor(
&self,
cursor: generic_btree::Cursor,

View file

@ -1212,7 +1212,17 @@ impl LoroDoc {
#[inline]
pub fn len_ops(&self) -> usize {
let oplog = self.oplog.try_lock().unwrap();
oplog.vv().iter().map(|(_, ops)| *ops).sum::<i32>() as usize
let ans = oplog.vv().iter().map(|(_, ops)| *ops).sum::<i32>() as usize;
if oplog.is_shallow() {
let sub = oplog
.shallow_since_vv()
.iter()
.map(|(_, ops)| *ops)
.sum::<i32>() as usize;
ans - sub
} else {
ans
}
}
#[inline]

View file

@ -433,6 +433,7 @@ impl ChangeStore {
}
pub fn change_num(&self) -> usize {
self.ensure_block_loaded_in_range(Bound::Unbounded, Bound::Unbounded);
let mut inner = self.inner.try_lock().unwrap();
inner
.mem_parsed_kv

View file

@ -1412,6 +1412,18 @@ impl LoroDoc {
console_log!("{:#?}", oplog.diagnose_size());
}
/// Get the number of changes in the oplog.
pub fn changeCount(&self) -> usize {
let borrow_mut = &self.0;
let oplog = borrow_mut.oplog().try_lock().unwrap();
oplog.len_changes()
}
/// Get the number of ops in the oplog.
pub fn opCount(&self) -> usize {
self.0.len_ops()
}
/// Get all of changes in the oplog.
///
/// Note: this method is expensive when the oplog is large. O(n)

View file

@ -2238,3 +2238,21 @@ fn is_deleted() {
let container_after = doc.get_map(&container_before.id());
assert!(container_after.is_deleted());
}
#[test]
fn change_count() {
let doc = LoroDoc::new();
let n = 1024 * 5;
for i in 0..n {
doc.get_text("text").insert(0, "H").unwrap();
doc.set_next_commit_message(&format!("{}", i));
doc.commit();
}
doc.compact_change_store();
assert_eq!(doc.len_changes(), n);
let bytes = doc.export(loro::ExportMode::Snapshot);
let new_doc = LoroDoc::new();
new_doc.import(&bytes.unwrap()).unwrap();
assert_eq!(new_doc.len_changes(), n);
}