From 1979c2312585e5d6ccb78c71403b54c46c9f4c16 Mon Sep 17 00:00:00 2001 From: leeeon233 Date: Fri, 3 Mar 2023 09:14:24 +0800 Subject: [PATCH] fix: transaction op apply --- .../src/container/list/list_container.rs | 1 - .../src/container/text/text_container.rs | 1 - crates/loro-internal/src/fuzz/recursive.rs | 278 ++++++------------ crates/loro-internal/src/hierarchy.rs | 9 +- crates/loro-internal/tests/test.rs | 6 +- 5 files changed, 96 insertions(+), 199 deletions(-) diff --git a/crates/loro-internal/src/container/list/list_container.rs b/crates/loro-internal/src/container/list/list_container.rs index 60107b38..7a1fd7f1 100644 --- a/crates/loro-internal/src/container/list/list_container.rs +++ b/crates/loro-internal/src/container/list/list_container.rs @@ -110,7 +110,6 @@ impl ListContainer { ops.push(op); } DeltaItem::Delete(len) => { - index -= len; let op = self.apply_delete(index, len, store); ops.push(op); } diff --git a/crates/loro-internal/src/container/text/text_container.rs b/crates/loro-internal/src/container/text/text_container.rs index 87351818..c7b61b5e 100644 --- a/crates/loro-internal/src/container/text/text_container.rs +++ b/crates/loro-internal/src/container/text/text_container.rs @@ -98,7 +98,6 @@ impl TextContainer { ops.push(op); } DeltaItem::Delete(len) => { - index -= len; let op = self.apply_delete(index, *len, store); ops.push(op); } diff --git a/crates/loro-internal/src/fuzz/recursive.rs b/crates/loro-internal/src/fuzz/recursive.rs index 32d96355..7fdf32ba 100644 --- a/crates/loro-internal/src/fuzz/recursive.rs +++ b/crates/loro-internal/src/fuzz/recursive.rs @@ -115,7 +115,6 @@ impl Actor { Box::new(move |event| { let mut map = map.lock().unwrap(); for diff in event.diff.iter() { - println!("diff {:?}", diff); match diff { Diff::Map(map_diff) => { for (key, value) in map_diff.added.iter() { @@ -1306,13 +1305,21 @@ mod failed_tests { #[test] fn list_slice_err() { test_multi_sites( - 3, - &mut [Map { - site: 1, - container_idx: 0, - key: 21, - value: Null, - }], + 5, + &mut [ + List { + site: 4, + container_idx: 0, + key: 0, + value: Container(C::List), + }, + List { + site: 4, + container_idx: 0, + key: 0, + value: Null, + }, + ], ) } @@ -1331,88 +1338,54 @@ mod failed_tests { SyncAll, SyncAll, SyncAll, - Text { - site: 149, - container_idx: 149, - pos: 149, - value: 38293, - is_del: true, - }, - Text { - site: 149, - container_idx: 149, - pos: 149, - value: 38293, - is_del: true, - }, - Text { - site: 149, - container_idx: 149, - pos: 149, - value: 38293, - is_del: true, - }, - Text { - site: 149, - container_idx: 149, - pos: 149, - value: 38293, - is_del: true, - }, - Text { - site: 149, - container_idx: 149, - pos: 149, - value: 38293, - is_del: true, - }, - Text { - site: 149, - container_idx: 149, - pos: 149, - value: 38293, - is_del: true, - }, - Map { - site: 21, - container_idx: 21, - key: 21, - value: Container(C::List), - }, SyncAll, SyncAll, SyncAll, SyncAll, SyncAll, - Map { - site: 21, - container_idx: 21, - key: 21, - value: Null, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + Text { + site: 124, + container_idx: 124, + pos: 124, + value: 31868, + is_del: false, }, - Map { - site: 21, - container_idx: 21, - key: 21, - value: Null, + Text { + site: 124, + container_idx: 124, + pos: 124, + value: 31868, + is_del: false, }, - Map { - site: 21, - container_idx: 21, - key: 21, - value: Null, + Text { + site: 124, + container_idx: 124, + pos: 124, + value: 31868, + is_del: false, }, - Map { - site: 21, - container_idx: 21, - key: 21, - value: Null, + Text { + site: 124, + container_idx: 124, + pos: 124, + value: 31868, + is_del: false, }, - Map { - site: 21, + Text { + site: 124, container_idx: 21, - key: 21, - value: Null, + pos: 21, + value: 5397, + is_del: true, }, Map { site: 21, @@ -1457,16 +1430,10 @@ mod failed_tests { site: 64, container_idx: 64, key: 64, - value: Container(C::Text), + value: Null, }, - Map { - site: 21, - container_idx: 21, - key: 235, - value: Container(C::Text), - }, - Map { - site: 21, + List { + site: 64, container_idx: 21, key: 21, value: Null, @@ -1478,26 +1445,39 @@ mod failed_tests { value: Null, }, Map { - site: 21, - container_idx: 21, - key: 21, + site: 0, + container_idx: 0, + key: 0, value: Null, }, - Map { - site: 21, - container_idx: 21, - key: 21, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + SyncAll, + Sync { from: 191, to: 191 }, + Sync { from: 197, to: 64 }, + List { + site: 64, + container_idx: 64, + key: 64, value: Null, }, - Map { - site: 21, - container_idx: 21, - key: 21, + List { + site: 242, + container_idx: 64, + key: 64, value: Null, }, - Map { - site: 21, - container_idx: 21, + List { + site: 64, + container_idx: 64, key: 64, value: Null, }, @@ -1513,94 +1493,10 @@ mod failed_tests { key: 64, value: Null, }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, - value: Null, - }, - Map { - site: 0, - container_idx: 0, - key: 0, + List { + site: 64, + container_idx: 64, + key: 64, value: Null, }, Map { diff --git a/crates/loro-internal/src/hierarchy.rs b/crates/loro-internal/src/hierarchy.rs index 99729464..cdd7857a 100644 --- a/crates/loro-internal/src/hierarchy.rs +++ b/crates/loro-internal/src/hierarchy.rs @@ -185,9 +185,12 @@ impl Hierarchy { .unwrap() .try_lock() .unwrap() - .index_of_child(node_id) - .unwrap(); - path.push(index); + .index_of_child(node_id); + if let Some(index) = index { + path.push(index); + } else { + return None; + } } else { match node_id { ContainerID::Root { diff --git a/crates/loro-internal/tests/test.rs b/crates/loro-internal/tests/test.rs index efb012d0..f6157149 100644 --- a/crates/loro-internal/tests/test.rs +++ b/crates/loro-internal/tests/test.rs @@ -158,7 +158,7 @@ fn map() { .insert(&loro, "map", loro_internal::ContainerType::Map) .unwrap() .unwrap() - .idx(); + .idx; drop(root); let sub_map = loro.get_container_by_idx(&map_id).unwrap(); let mut sub_map = Map::from_instance(sub_map, loro.client_id()); @@ -284,14 +284,14 @@ fn encode_hierarchy() { let mut c1 = LoroCore::default(); let mut map = c1.get_map("map"); let list_id = map.insert(&c1, "a", ContainerType::List).unwrap(); - let list = c1.get_container_by_idx(&list_id.unwrap().idx()).unwrap(); + let list = c1.get_container_by_idx(&list_id.unwrap().idx).unwrap(); let list = List::from_instance(list, c1.client_id()); let txn = c1.transact(); let idx = list .insert(&txn, 0, ContainerType::Text) .unwrap() .unwrap() - .idx(); + .idx; let text = c1.get_container_by_idx(&idx).unwrap(); let text = Text::from_instance(text, c1.client_id()); text.insert(&c1, 0, "text_text");