From 7cf54e8aa3396635960ecfe170c00f0d70611e0e Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Wed, 28 Aug 2024 00:06:24 +0800 Subject: [PATCH] fix: should not use snapshot importing when it's inside a batch importing (#436) * fix: should not use snapshot importing when it's inside a batch importing * chore: bk --- .changeset/strong-walls-vanish.md | 6 ++++++ 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 5 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 .changeset/strong-walls-vanish.md create mode 100644 crates/loro/tests/issue.rs create mode 100644 crates/loro/tests/issue_0.bin diff --git a/.changeset/strong-walls-vanish.md b/.changeset/strong-walls-vanish.md new file mode 100644 index 00000000..a85fd163 --- /dev/null +++ b/.changeset/strong-walls-vanish.md @@ -0,0 +1,6 @@ +--- +"loro-wasm": patch +"loro-crdt": patch +--- + +Fix batch importing with snapshot diff --git a/crates/loro-internal/src/loro.rs b/crates/loro-internal/src/loro.rs index a4ec8df9..3bd21ba1 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 12326224..a985426b 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 00000000..60caa920 --- /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