diff --git a/crates/loro-internal/src/refactor/event.rs b/crates/loro-internal/src/refactor/event.rs index 03019fb1..082ff3e5 100644 --- a/crates/loro-internal/src/refactor/event.rs +++ b/crates/loro-internal/src/refactor/event.rs @@ -26,7 +26,7 @@ pub struct DiffEvent<'a> { /// It's the exposed event type. /// It's exposed to the user. The user can use this to apply the diff to their local state. /// -/// DocDiff may include the diff that calculated from several transactions and imports. +/// [DocDiff] may include the diff that calculated from several transactions and imports. /// They all should have the same origin and local flag. #[derive(Debug, Clone)] pub struct DocDiff { @@ -35,7 +35,6 @@ pub struct DocDiff { pub origin: InternalString, pub local: bool, pub diff: Vec, - // state: Weak>, } #[derive(Debug, Clone)] diff --git a/crates/loro-internal/src/refactor/state.rs b/crates/loro-internal/src/refactor/state.rs index 73bff6dc..fb54c033 100644 --- a/crates/loro-internal/src/refactor/state.rs +++ b/crates/loro-internal/src/refactor/state.rs @@ -104,22 +104,7 @@ impl DocState { #[inline] pub fn new(arena: SharedArena) -> Self { let peer = SystemRandom::new().next_u64(); - // TODO: maybe we should switch to certain version in oplog - Self { - peer, - arena, - frontiers: Frontiers::default(), - states: FxHashMap::default(), - in_txn: false, - changed_idx_in_txn: FxHashSet::default(), - event_recorder: Default::default(), - } - } - - #[inline] - pub fn new_from_arena(arena: SharedArena) -> Self { - let peer = SystemRandom::new().next_u64(); - // TODO: maybe we should switch to certain version in oplog + // TODO: maybe we should switch to certain version in oplog? Self { peer, arena, @@ -161,11 +146,12 @@ impl DocState { } /// Record the next diff. + /// Caller should call [pre_txn] before calling this. /// /// # Panic /// - /// If the diff cannot be merged with the previous diff. - /// Caller should call [pre_txn] before calling this. + /// Panic when the diff cannot be merged with the previous diff. + /// Caller should call [pre_txn] before calling this to avoid panic. fn record_diff(&mut self, diff: InternalDocDiff) { if !self.event_recorder.recording_diff { return; @@ -319,7 +305,7 @@ impl DocState { /// /// # Panic /// - /// If the state already exists. + /// If the state is not empty. pub(super) fn init_with_states_and_version( &mut self, states: FxHashMap, @@ -461,7 +447,7 @@ impl DocState { } // Because we need to calculate path based on [DocState], so we cannot extract - // [EventRecorder] to a separate module. + // the event recorder to a separate module. fn diffs_to_event(&self, diffs: Vec>, from: Frontiers) -> DocDiff { if diffs.is_empty() { panic!("diffs is empty"); @@ -505,6 +491,8 @@ impl DocState { }) .collect(); + // Sort by path length, so caller can apply the diff from the root to the leaf. + // Otherwise, the caller may use a wrong path to apply the diff. diff.sort_by_key(|x| x.path.len()); DocDiff { from, @@ -522,7 +510,6 @@ impl DocState { loop { let id = self.arena.idx_to_id(idx).unwrap(); if let Some(parent_idx) = self.arena.get_parent(idx) { - let parent_id = self.arena.get_container_id(parent_idx); let parent_state = self.states.get(&parent_idx).unwrap(); let prop = parent_state.get_child_index(&id)?; ans.push((id, prop)); diff --git a/crates/loro-internal/src/refactor/state/map_state.rs b/crates/loro-internal/src/refactor/state/map_state.rs index 9533b3a0..db4d12f4 100644 --- a/crates/loro-internal/src/refactor/state/map_state.rs +++ b/crates/loro-internal/src/refactor/state/map_state.rs @@ -1,6 +1,5 @@ use std::{mem, sync::Arc}; -use debug_log::debug_dbg; use fxhash::FxHashMap; use loro_common::ContainerID; @@ -42,7 +41,7 @@ impl ContainerState for MapState { match op.content { RawOpContent::Map(map) => { if map.value.is_container() { - let idx = arena.register_container(&map.value.as_container().unwrap()); + let idx = arena.register_container(map.value.as_container().unwrap()); arena.set_parent(idx, Some(self.idx)); } diff --git a/crates/loro-internal/src/refactor/txn.rs b/crates/loro-internal/src/refactor/txn.rs index 66c9b84e..7ab386ee 100644 --- a/crates/loro-internal/src/refactor/txn.rs +++ b/crates/loro-internal/src/refactor/txn.rs @@ -33,6 +33,7 @@ use super::{ }; pub type OnCommitFn = Box>)>; + pub struct Transaction { peer: PeerID, origin: InternalString,