From 5bcdc28968dfdb775c2266d79df63711fad7fac3 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Wed, 28 Aug 2024 00:01:15 +0800 Subject: [PATCH 1/2] fix: should not use snapshot importing when it's inside a batch importing --- crates/loro-internal/src/loro.rs | 7 ++++++- crates/loro/src/lib.rs | 2 +- crates/loro/tests/issue.rs | 14 ++++++++++++++ crates/loro/tests/issue_0.bin | Bin 0 -> 223 bytes 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 crates/loro/tests/issue.rs create mode 100644 crates/loro/tests/issue_0.bin diff --git a/crates/loro-internal/src/loro.rs b/crates/loro-internal/src/loro.rs index a4ec8df90..3bd21ba17 100644 --- a/crates/loro-internal/src/loro.rs +++ b/crates/loro-internal/src/loro.rs @@ -216,7 +216,12 @@ impl LoroDoc { /// Is the document empty? (no ops) #[inline(always)] pub fn can_reset_with_snapshot(&self) -> bool { - self.oplog.lock().unwrap().is_empty() && self.state.lock().unwrap().is_empty() + let oplog = self.oplog.lock().unwrap(); + if oplog.batch_importing { + return false; + } + + oplog.is_empty() && self.state.lock().unwrap().is_empty() } /// Whether [OpLog] and [DocState] are detached. diff --git a/crates/loro/src/lib.rs b/crates/loro/src/lib.rs index 123262240..a985426b8 100644 --- a/crates/loro/src/lib.rs +++ b/crates/loro/src/lib.rs @@ -213,7 +213,7 @@ impl LoroDoc { /// Import a batch of updates/snapshot. /// /// The data can be in arbitrary order. The import result will be the same. - pub fn import_batch(&mut self, bytes: &[Vec]) -> LoroResult<()> { + pub fn import_batch(&self, bytes: &[Vec]) -> LoroResult<()> { self.doc.import_batch(bytes) } diff --git a/crates/loro/tests/issue.rs b/crates/loro/tests/issue.rs new file mode 100644 index 000000000..60caa9206 --- /dev/null +++ b/crates/loro/tests/issue.rs @@ -0,0 +1,14 @@ +use loro::LoroDoc; + +#[ctor::ctor] +fn init() { + dev_utils::setup_test_log(); +} + +#[test] +fn issue_0() { + let bytes = include_bytes!("./issue_0.bin"); + let doc = LoroDoc::new(); + doc.import_batch(&[bytes.into()]).unwrap(); + doc.export_snapshot(); +} diff --git a/crates/loro/tests/issue_0.bin b/crates/loro/tests/issue_0.bin new file mode 100644 index 0000000000000000000000000000000000000000..ec9093493d063ad65749f8fd42650385d8c5d8c2 GIT binary patch literal 223 zcmW-aJ8l9&5JancdWIQ<2pu3ILYk4)Y+f_YqD})AG1JzgY)DtzFj(2RH55|6X;M1& literal 0 HcmV?d00001 From 061782eac3af6dba000c513f6f2d16dad05f0199 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Wed, 28 Aug 2024 00:02:33 +0800 Subject: [PATCH 2/2] chore: bk --- .changeset/strong-walls-vanish.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/strong-walls-vanish.md diff --git a/.changeset/strong-walls-vanish.md b/.changeset/strong-walls-vanish.md new file mode 100644 index 000000000..a85fd1634 --- /dev/null +++ b/.changeset/strong-walls-vanish.md @@ -0,0 +1,6 @@ +--- +"loro-wasm": patch +"loro-crdt": patch +--- + +Fix batch importing with snapshot