diff --git a/crates/fuzz/src/container/tree.rs b/crates/fuzz/src/container/tree.rs index 702b94db..790b10d5 100644 --- a/crates/fuzz/src/container/tree.rs +++ b/crates/fuzz/src/container/tree.rs @@ -421,8 +421,8 @@ impl ApplyDiff for TreeTracker { } fn apply_diff(&mut self, diff: Diff) { - trace!("current tree: {:#?}", &self.tree); - trace!("applying diff: {:#?}", &diff); + // trace!("current tree: {:#?}", &self.tree); + // trace!("applying diff: {:#?}", &diff); let diff = diff.as_tree().unwrap(); for diff in &diff.diff { let target = diff.target; diff --git a/crates/loro-internal/src/diff_calc/tree.rs b/crates/loro-internal/src/diff_calc/tree.rs index 015aa245..e74b5ed6 100644 --- a/crates/loro-internal/src/diff_calc/tree.rs +++ b/crates/loro-internal/src/diff_calc/tree.rs @@ -189,22 +189,15 @@ impl TreeDiffCalculator { } tracing::info!(msg="retreat ops", retreat_ops=?retreat_ops); for op in retreat_ops { - tree_cache - .tree - .get_mut(&op.op.target()) - .unwrap() - .remove(&op); - tree_cache.current_vv.shrink_to_exclude(IdSpan::new( - op.id.peer, - op.id.counter, - op.id.counter + 1, - )); + tree_cache.retreat_op(&op); } + // forward and apply let current_frontiers = tree_cache.current_vv.to_frontiers(&oplog.dag); let forward_min_lamport = self .get_min_lamport_by_frontiers(¤t_frontiers, oplog) .min(min_lamport); + let max_lamport = self.get_max_lamport_by_frontiers(&to_frontiers, oplog); let mut forward_ops = vec![]; let group = h @@ -292,16 +285,7 @@ impl TreeDiffCalculator { // tracing::info!("retreat ops {:?}", retreat_ops); for op in retreat_ops.into_iter().sorted().rev() { - tree_cache - .tree - .get_mut(&op.op.target()) - .unwrap() - .remove(&op); - tree_cache.current_vv.shrink_to_exclude(IdSpan::new( - op.id.peer, - op.id.counter, - op.id.counter + 1, - )); + tree_cache.retreat_op(&op); let (old_parent, position, last_effective_move_op_id) = tree_cache.get_parent_with_id(op.op.target()); if op.effected { @@ -485,6 +469,23 @@ impl std::fmt::Debug for TreeCacheForDiff { } impl TreeCacheForDiff { + fn retreat_op(&mut self, op: &MoveLamportAndID) { + self.tree.get_mut(&op.op.target()).unwrap().remove(&op); + self.current_vv.shrink_to_exclude(IdSpan::new( + op.id.peer, + op.id.counter, + op.id.counter + 1, + )); + // Only shrink cannot get the correct empty vv, + if self.is_empty() { + self.current_vv = VersionVector::default(); + } + } + + fn is_empty(&self) -> bool { + self.tree.iter().all(|(_, v)| v.is_empty()) + } + fn is_ancestor_of(&self, maybe_ancestor: &TreeID, node_id: &TreeParentId) -> bool { if !self.tree.contains_key(maybe_ancestor) { return false; diff --git a/crates/loro-internal/src/state/tree_state.rs b/crates/loro-internal/src/state/tree_state.rs index 3b405609..51aee666 100644 --- a/crates/loro-internal/src/state/tree_state.rs +++ b/crates/loro-internal/src/state/tree_state.rs @@ -1083,8 +1083,6 @@ impl ContainerState for TreeState { TreeInternalDiff::UnCreate => { // maybe the node created and moved to the parent deleted if !self.is_node_deleted(&target).unwrap() { - trace!("tree {:#?}", &self.trees); - trace!("DEL from b {:?}", target); ans.push(TreeDiffItem { target, action: TreeExternalDiff::Delete {