From 2a436e07ad96a68ccb1ca654bc2626ac2b451890 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Mon, 16 Jan 2023 17:28:14 +0800 Subject: [PATCH] fix: snapshot encoding err --- crates/loro-core/src/container/list/list_container.rs | 10 +++++++++- crates/loro-core/src/container/text/text_container.rs | 10 +++++++++- .../src/log_store/encoding/encode_snapshot.rs | 9 +++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/crates/loro-core/src/container/list/list_container.rs b/crates/loro-core/src/container/list/list_container.rs index a75adbbe..d8652144 100644 --- a/crates/loro-core/src/container/list/list_container.rs +++ b/crates/loro-core/src/container/list/list_container.rs @@ -517,9 +517,17 @@ impl Container for ListContainer { .as_mut() .unwrap() .convert_ops_slice(slice.0.clone(), old_pool); + let mut offset = 0; new_slice .into_iter() - .map(|slice| InnerContent::List(InnerListOp::Insert { slice, pos: *pos })) + .map(|slice| { + let ans = InnerContent::List(InnerListOp::Insert { + slice, + pos: *pos + offset, + }); + offset += ans.atom_len(); + ans + }) .collect() } InnerListOp::Delete(span) => { diff --git a/crates/loro-core/src/container/text/text_container.rs b/crates/loro-core/src/container/text/text_container.rs index 8e345167..0945ca44 100644 --- a/crates/loro-core/src/container/text/text_container.rs +++ b/crates/loro-core/src/container/text/text_container.rs @@ -435,9 +435,17 @@ impl Container for TextContainer { .as_mut() .unwrap() .convert_ops_slice(slice.0.clone(), old_pool); + let mut offset = 0; new_slice .into_iter() - .map(|slice| InnerContent::List(InnerListOp::Insert { slice, pos: *pos })) + .map(|slice| { + let ans = InnerContent::List(InnerListOp::Insert { + slice, + pos: *pos + offset, + }); + offset += ans.atom_len(); + ans + }) .collect() } InnerListOp::Delete(span) => { diff --git a/crates/loro-core/src/log_store/encoding/encode_snapshot.rs b/crates/loro-core/src/log_store/encoding/encode_snapshot.rs index 6d704efc..df44f22f 100644 --- a/crates/loro-core/src/log_store/encoding/encode_snapshot.rs +++ b/crates/loro-core/src/log_store/encoding/encode_snapshot.rs @@ -187,6 +187,8 @@ fn convert_inner_content( } pub(super) fn encode_snapshot(store: &LogStore, gc: bool) -> Result, LoroError> { + debug_log::debug_dbg!(&store.vv); + debug_log::debug_dbg!(&store.changes); let mut client_id_to_idx: FxHashMap = FxHashMap::default(); let mut clients = Vec::with_capacity(store.changes.len()); let mut change_num = 0; @@ -214,13 +216,13 @@ pub(super) fn encode_snapshot(store: &LogStore, gc: bool) -> Result, Lor let mut deps = Vec::with_capacity(change_num); for (client_idx, (_, change_vec)) in store.changes.iter().enumerate() { for change in change_vec.iter() { + let mut op_len = 0; for dep in change.deps.iter() { deps.push(DepsEncoding::new( *client_id_to_idx.get(&dep.client_id).unwrap(), dep.counter, )); } - let op_len = change.ops.len() as u32; for op in change.ops.iter() { let container_idx = op.container; let container_id = store.reg.get_id(container_idx).unwrap(); @@ -231,6 +233,7 @@ pub(super) fn encode_snapshot(store: &LogStore, gc: bool) -> Result, Lor .try_lock() .unwrap() .to_export_snapshot(&op.content, gc); + op_len += new_ops.len(); for op_content in new_ops { let (prop, value, value2) = convert_inner_content(&op_content, &mut key_to_idx, &mut keys); @@ -249,7 +252,7 @@ pub(super) fn encode_snapshot(store: &LogStore, gc: bool) -> Result, Lor lamport: change.lamport, timestamp: change.timestamp, deps_len: change.deps.len() as u32, - op_len, + op_len: op_len as u32, }); } } @@ -407,6 +410,8 @@ pub(super) fn decode_snapshot( .map(|changes| changes.last().unwrap().id_last()) .collect(); + debug_log::debug_dbg!(&vv, &changes); + let can_load = match vv.partial_cmp(&store.vv) { Some(ord) => match ord { std::cmp::Ordering::Less => false,