From ccee20164132ea4c3e6fe07e00f05438df61a8ed Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Sat, 5 Aug 2023 12:44:31 +0800 Subject: [PATCH] refactor: better interface --- crates/loro-internal/src/change.rs | 28 ++++++++++++--------- crates/loro-internal/src/handler.rs | 38 +++++++++++++++++++++++++---- crates/loro-internal/src/oplog.rs | 4 +++ 3 files changed, 53 insertions(+), 17 deletions(-) diff --git a/crates/loro-internal/src/change.rs b/crates/loro-internal/src/change.rs index af7df3a8..c822a309 100644 --- a/crates/loro-internal/src/change.rs +++ b/crates/loro-internal/src/change.rs @@ -50,6 +50,22 @@ impl Change { timestamp, } } + + pub fn ops(&self) -> &RleVec<[O; 1]> { + &self.ops + } + + pub fn lamport(&self) -> Lamport { + self.lamport + } + + pub fn timestamp(&self) -> Timestamp { + self.timestamp + } + + pub fn id(&self) -> ID { + self.id + } } impl HasIndex for Change { @@ -119,18 +135,6 @@ impl DagNode for Change { } impl Change { - pub fn lamport(&self) -> Lamport { - self.lamport - } - - pub fn timestamp(&self) -> Timestamp { - self.timestamp - } - - pub fn id(&self) -> ID { - self.id - } - pub fn can_merge_right(&self, other: &Self) -> bool { other.id.peer == self.id.peer && other.id.counter == self.id.counter + self.content_len() as Counter diff --git a/crates/loro-internal/src/handler.rs b/crates/loro-internal/src/handler.rs index 95a6a423..f97c6907 100644 --- a/crates/loro-internal/src/handler.rs +++ b/crates/loro-internal/src/handler.rs @@ -21,19 +21,37 @@ pub struct TextHandler { state: Weak>, } +impl std::fmt::Debug for TextHandler { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("TextHandler") + } +} + #[derive(Clone)] pub struct MapHandler { container_idx: ContainerIdx, state: Weak>, } +impl std::fmt::Debug for MapHandler { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("MapHandler") + } +} + #[derive(Clone)] pub struct ListHandler { container_idx: ContainerIdx, state: Weak>, } -#[derive(Clone, EnumAsInner)] +impl std::fmt::Debug for ListHandler { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("ListHandler") + } +} + +#[derive(Clone, EnumAsInner, Debug)] pub enum Handler { Text(TextHandler), Map(MapHandler), @@ -322,6 +340,11 @@ impl ListHandler { ) } + pub fn push(&self, txn: &mut Transaction, v: LoroValue) -> LoroResult<()> { + let pos = self.len(); + self.insert(txn, pos, v) + } + pub fn insert_container( &self, txn: &mut Transaction, @@ -435,9 +458,9 @@ impl ListHandler { }) } - pub fn for_each(&self, f: I) + pub fn for_each(&self, mut f: I) where - I: Fn(&LoroValue), + I: FnMut(&LoroValue), { self.state .upgrade() @@ -468,6 +491,11 @@ impl MapHandler { return Ok(()); } + if self.get(key).map(|x| x == value).unwrap_or(false) { + // skip if the value is already set + return Ok(()); + } + txn.apply_local_op( self.container_idx, crate::op::RawOpContent::Map(crate::container::map::MapSet { @@ -514,9 +542,9 @@ impl MapHandler { ) } - pub fn for_each(&self, f: I) + pub fn for_each(&self, mut f: I) where - I: Fn(&str, &MapValue), + I: FnMut(&str, &MapValue), { self.state .upgrade() diff --git a/crates/loro-internal/src/oplog.rs b/crates/loro-internal/src/oplog.rs index 99cdd163..fe8186ed 100644 --- a/crates/loro-internal/src/oplog.rs +++ b/crates/loro-internal/src/oplog.rs @@ -228,6 +228,10 @@ impl OpLog { ID::new(peer, cnt) } + pub fn get_peer_changes(&self, peer: PeerID) -> Option<&RleVec<[Change; 0]>> { + self.changes.get(&peer) + } + pub(crate) fn vv(&self) -> &VersionVector { &self.dag.vv }