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 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 000000000..ec9093493 Binary files /dev/null and b/crates/loro/tests/issue_0.bin differ