From 7e7fe782b448fb3fb269f867b0c642ee1fd1c19d Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Tue, 6 Aug 2024 15:34:55 +0800 Subject: [PATCH] refactor: refine movable list internal event --- Cargo.lock | 8 ++++---- crates/loro-internal/src/delta/movable_list.rs | 12 ++++++------ crates/loro-internal/src/diff_calc.rs | 10 ++++------ .../loro-internal/src/state/movable_list_state.rs | 15 ++++++++++----- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 68df4b15..62adc83d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -201,9 +201,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "bytes" -version = "1.6.0" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" +checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" [[package]] name = "cast" @@ -1873,9 +1873,9 @@ dependencies = [ [[package]] name = "serde_columnar" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06a86f5f6dc16d8308c37e145dd4c7e60fba1486d84982519388d31ea0ad6703" +checksum = "7947238638e841d935a1dadff54b74575ae60d51b977be75dab16e7638ea6e7d" dependencies = [ "itertools 0.11.0", "postcard", diff --git a/crates/loro-internal/src/delta/movable_list.rs b/crates/loro-internal/src/delta/movable_list.rs index 51b1b826..40459e01 100644 --- a/crates/loro-internal/src/delta/movable_list.rs +++ b/crates/loro-internal/src/delta/movable_list.rs @@ -36,21 +36,21 @@ impl MovableListInnerDelta { #[derive(Clone, Debug)] pub struct ElementDelta { - pub pos: IdLp, - pub pos_updated: bool, + /// This must be Some if it's in checkout mode (not fast-forward mode) + pub pos: Option, pub value: LoroValue, pub value_updated: bool, - pub value_id: IdLp, + /// This must be Some if it's in checkout mode (not fast-forward mode) + pub value_id: Option, } impl ElementDelta { pub fn placeholder() -> Self { Self { - pos: IdLp::NONE_ID, - pos_updated: false, + pos: None, value: LoroValue::Null, value_updated: false, - value_id: IdLp::NONE_ID, + value_id: None, } } } diff --git a/crates/loro-internal/src/diff_calc.rs b/crates/loro-internal/src/diff_calc.rs index b5d369a8..95f569ab 100644 --- a/crates/loro-internal/src/diff_calc.rs +++ b/crates/loro-internal/src/diff_calc.rs @@ -1048,20 +1048,18 @@ impl DiffCalculatorTrait for MovableListDiffCalculator { on_new_container(c); } *change = ElementDelta { - pos: pos.idlp(), + pos: Some(pos.idlp()), value: value.value.clone(), - value_id: IdLp::new(value.peer, value.lamport), - pos_updated: true, + value_id: Some(IdLp::new(value.peer, value.lamport)), value_updated: true, }; } else { // TODO: PERF: can be filtered based on the list_diff and whether the pos/value are updated *change = ElementDelta { - pos: pos.idlp(), - pos_updated: old_pos.unwrap().idlp() != pos.idlp(), + pos: Some(pos.idlp()), value: value.value.clone(), value_updated: old_value.unwrap().value != value.value, - value_id: IdLp::new(value.peer, value.lamport), + value_id: Some(IdLp::new(value.peer, value.lamport)), }; } diff --git a/crates/loro-internal/src/state/movable_list_state.rs b/crates/loro-internal/src/state/movable_list_state.rs index 5c1405d0..e4e224ab 100644 --- a/crates/loro-internal/src/state/movable_list_state.rs +++ b/crates/loro-internal/src/state/movable_list_state.rs @@ -1059,7 +1059,6 @@ impl ContainerState for MovableListState { for (elem_id, delta_item) in diff.elements.into_iter() { let crate::delta::ElementDelta { pos, - pos_updated: _, value, value_updated, value_id, @@ -1072,7 +1071,8 @@ impl ContainerState for MovableListState { // Update value if needed if elem.value != value { maybe_moved.remove(&elem_id); - self.inner.update_value(elem_id, value.clone(), value_id); + self.inner + .update_value(elem_id, value.clone(), value_id.unwrap()); let index = self.get_index_of_elem(elem_id); if let Some(index) = index { event.compose( @@ -1091,9 +1091,9 @@ impl ContainerState for MovableListState { } // Update pos if needed - if elem.pos != pos { + if elem.pos != pos.unwrap() { // don't need to update old list item, because it's handled by list diff already - let result = self.inner.update_pos(elem_id, pos, false); + let result = self.inner.update_pos(elem_id, pos.unwrap(), false); let result = self.inner.convert_update_to_event_pos(result); if let Some(new_index) = result.insert { let new_value = @@ -1135,7 +1135,12 @@ impl ContainerState for MovableListState { } None => { // Need to create new element - let result = self.create_new_elem(elem_id, pos, value.clone(), value_id); + let result = self.create_new_elem( + elem_id, + pos.unwrap(), + value.clone(), + value_id.unwrap(), + ); // Composing events let result = self.inner.convert_update_to_event_pos(result); // Create event for pos change and value change