mirror of
https://github.com/loro-dev/loro.git
synced 2024-11-24 12:20:06 +00:00
parent
4f2bbee2a7
commit
35e7ea5f54
6 changed files with 47 additions and 2 deletions
5
.changeset/two-jokes-happen.md
Normal file
5
.changeset/two-jokes-happen.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"loro-crdt": patch
|
||||
---
|
||||
|
||||
Add changeCount and opCount methods
|
|
@ -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,
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue