Skip to content

Commit e9aec23

Browse files
committed
fix: from_snapshot with shallow snapshot err
1 parent 998a737 commit e9aec23

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

crates/loro-internal/src/encoding/fast_snapshot.rs

+8
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ pub(crate) fn decode_snapshot(doc: &LoroDoc, bytes: Bytes) -> LoroResult<()> {
9494
}
9595

9696
pub(crate) fn decode_snapshot_inner(snapshot: Snapshot, doc: &LoroDoc) -> Result<(), LoroError> {
97+
let (options, guard) = doc.commit_then_stop();
98+
_decode_snapshot_inner(snapshot, doc)?;
99+
drop(guard);
100+
doc.renew_txn_if_auto_commit(options);
101+
Ok(())
102+
}
103+
104+
fn _decode_snapshot_inner(snapshot: Snapshot, doc: &LoroDoc) -> Result<(), LoroError> {
97105
let Snapshot {
98106
oplog_bytes,
99107
state_bytes,

crates/loro-wasm/tests/basic.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1622,3 +1622,15 @@ it("hideEmptyRootContainers", () => {
16221622
doc.setHideEmptyRootContainers(true);
16231623
expect(doc.toJSON()).toStrictEqual({});
16241624
});
1625+
1626+
it("fromShallowSnapshot", () => {
1627+
const doc = new LoroDoc();
1628+
doc.setPeerId("1");
1629+
doc.getText("text").insert(0, "Hello");
1630+
doc.commit();
1631+
const snapshot = doc.export({ mode: "shallow-snapshot", frontiers: doc.frontiers() });
1632+
const newDoc = LoroDoc.fromSnapshot(snapshot);
1633+
expect(newDoc.toJSON()).toStrictEqual({
1634+
text: "Hello"
1635+
});
1636+
})

crates/loro/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,12 @@ impl LoroDoc {
472472
self.doc.is_detached()
473473
}
474474

475+
/// Create a new `LoroDoc` from a snapshot.
476+
pub fn from_snapshot(bytes: &[u8]) -> LoroResult<Self> {
477+
let inner = InnerLoroDoc::from_snapshot(bytes)?;
478+
Ok(Self::_new(inner))
479+
}
480+
475481
/// Import updates/snapshot exported by [`LoroDoc::export_snapshot`] or [`LoroDoc::export_from`].
476482
#[inline]
477483
pub fn import(&self, bytes: &[u8]) -> Result<ImportStatus, LoroError> {

crates/loro/tests/loro_rust_test.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use loro::{
1717
event::{Diff, DiffBatch, ListDiffItem},
1818
loro_value, CommitOptions, ContainerID, ContainerTrait, ContainerType, ExportMode, Frontiers,
1919
FrontiersNotIncluded, IdSpan, Index, LoroDoc, LoroError, LoroList, LoroMap, LoroMapValue,
20-
LoroStringValue, LoroText, LoroValue, ToJson, TreeParentId, UpdateOptions,
20+
LoroStringValue, LoroText, LoroValue, ToJson, TreeParentId,
2121
};
2222
use loro_internal::{
2323
encoding::EncodedBlobMode, fx_map, handler::TextDelta, id::ID, version_range, vv, LoroResult,
@@ -3341,3 +3341,20 @@ fn test_hide_empty_root_containers() {
33413341
LoroValue::Map(LoroMapValue::default())
33423342
);
33433343
}
3344+
3345+
#[test]
3346+
fn test_from_shallow_snapshot() {
3347+
let doc = LoroDoc::new();
3348+
doc.set_peer_id(1).unwrap();
3349+
doc.get_text("text").insert(0, "Hello").unwrap();
3350+
doc.commit();
3351+
let snapshot = doc
3352+
.export(ExportMode::shallow_snapshot_owned(doc.state_frontiers()))
3353+
.unwrap();
3354+
let new_doc = LoroDoc::from_snapshot(&snapshot).unwrap();
3355+
let mut expected = LoroMapValue::default();
3356+
expected
3357+
.make_mut()
3358+
.insert("text".into(), LoroValue::String("Hello".into()));
3359+
assert_eq!(new_doc.get_deep_value(), LoroValue::Map(expected));
3360+
}

0 commit comments

Comments
 (0)