forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

fork.rs64 lines, 2.5 KiBHistoryRawDownload
1//! The fork module provides functionality to create a new LoroDoc instance at a specified version
2//! (Frontiers) with minimal overhead.
3//!
4//! # Implementation Overview
5//!
6//! The `fork_at` function in this module allows for the creation of a new document that reflects
7//! the state of the original document at a given version. The function achieves this by:
8//!
9//! ## Exporting Necessary Data:
10//!
11//! - **Change Store Data**: Collects all changes up to the specified version from the change
12//! store's key-value (kv) data store. It includes the version vector and frontiers for accurate
13//! identification of the version.
14//!
15//! - **Container Store Data**: Exports the container store's kv data representing the document's
16//! state at the specified version. This involves checking out to the desired version, exporting
17//! the state, and efficiently checking back to the latest version.
18//!
19//! - **GC Store Data**: If applicable, exports the gc store's kv data, ensuring that version
20//! identifiers are included.
21//!
22//! ## Reconstructing the New Document:
23//!
24//! Imports the exported data into a new LoroDoc instance using optimized import mechanisms
25//! similar to those used in fast snapshot imports.
26//!
27//! By focusing on exporting only the necessary data and optimizing state transitions during
28//! version checkout, the `fork_at` function minimizes overhead and efficiently creates new
29//! document instances representing past versions.
30//!
31use std::borrow::Cow;
32
33use crate::{version::Frontiers, LoroDoc};
34use loro_common::{LoroError, LoroResult};
35
36impl LoroDoc {
37 /// Creates a new LoroDoc at a specified version (Frontiers)
38 pub fn fork_at(&self, frontiers: &Frontiers) -> LoroResult<Self> {
39 self.fork_at_inner(frontiers, true)
40 }
41
42 /// See [`LoroDoc::export_inner`] for `reject_partial_history`.
43 pub(crate) fn fork_at_inner(
44 &self,
45 frontiers: &Frontiers,
46 reject_partial_history: bool,
47 ) -> LoroResult<Self> {
48 let bytes = self
49 .export_inner(
50 crate::loro::ExportMode::SnapshotAt {
51 version: Cow::Borrowed(frontiers),
52 },
53 reject_partial_history,
54 )
55 .map_err(LoroError::from)?;
56 let doc = LoroDoc::new();
57 doc.set_config(&self.config);
58 if self.auto_commit.load(std::sync::atomic::Ordering::Relaxed) {
59 doc.start_auto_commit();
60 }
61 doc.import(&bytes)?;
62 Ok(doc)
63 }
64}