loro_internal/
lib.rs

1//! loro-internal is a CRDT framework.
2//!
3//!
4//!
5//!
6#![deny(clippy::undocumented_unsafe_blocks)]
7#![warn(rustdoc::broken_intra_doc_links)]
8#![warn(missing_debug_implementations)]
9
10pub mod arena;
11mod change_meta;
12pub mod diff;
13pub mod diff_calc;
14pub mod handler;
15use std::sync::atomic::AtomicBool;
16use std::sync::{Arc, Mutex};
17
18use arena::SharedArena;
19use configure::Configure;
20use diff_calc::DiffCalculator;
21
22pub use change_meta::ChangeMeta;
23pub use event::{ContainerDiff, DiffEvent, DocDiff, ListDiff, ListDiffInsertItem, ListDiffItem};
24pub use fxhash::FxHashMap;
25pub use handler::{
26    BasicHandler, HandlerTrait, ListHandler, MapHandler, MovableListHandler, TextHandler,
27    TreeHandler, UnknownHandler,
28};
29pub use loro_common;
30pub use oplog::OpLog;
31pub use state::DocState;
32pub use state::{TreeNode, TreeNodeWithChildren, TreeParentId};
33use subscription::{LocalUpdateCallback, Observer, PeerIdUpdateCallback};
34use txn::Transaction;
35pub use undo::UndoManager;
36use utils::subscription::SubscriberSetWithQueue;
37pub use utils::subscription::Subscription;
38pub mod allocation;
39pub mod awareness;
40pub mod change;
41pub mod configure;
42pub mod container;
43pub mod cursor;
44pub mod dag;
45pub mod encoding;
46pub(crate) mod fork;
47pub mod id;
48#[cfg(feature = "jsonpath")]
49pub mod jsonpath;
50pub mod kv_store;
51pub mod loro;
52pub mod op;
53pub mod oplog;
54pub mod subscription;
55pub mod txn;
56pub mod version;
57
58mod error;
59#[cfg(feature = "test_utils")]
60pub mod fuzz;
61mod parent;
62mod span;
63#[cfg(test)]
64pub mod tests;
65mod utils;
66pub use utils::string_slice::StringSlice;
67
68pub mod delta;
69pub use loro_delta;
70pub mod event;
71
72pub mod estimated_size;
73pub(crate) mod history_cache;
74pub(crate) mod macros;
75pub(crate) mod state;
76pub mod undo;
77pub(crate) mod value;
78
79// TODO: rename as Key?
80pub(crate) use loro_common::InternalString;
81
82pub use container::ContainerType;
83pub use encoding::json_schema::json;
84pub use fractional_index::FractionalIndex;
85pub use loro_common::{loro_value, to_value};
86pub use loro_common::{
87    Counter, CounterSpan, IdLp, IdSpan, IdSpanVector, Lamport, LoroEncodeError, LoroError,
88    LoroResult, LoroTreeError, PeerID, TreeID, ID,
89};
90pub use loro_common::{LoroBinaryValue, LoroListValue, LoroMapValue, LoroStringValue};
91#[cfg(feature = "wasm")]
92pub use value::wasm;
93pub use value::{ApplyDiff, LoroValue, ToJson};
94pub use version::VersionVector;
95
96/// `LoroApp` serves as the library's primary entry point.
97/// It's constituted by an [OpLog] and an [AppState].
98///
99/// - [OpLog] encompasses all operations, signifying the document history.
100/// - [AppState] signifies the current document state.
101///
102/// They will share a [super::arena::SharedArena]
103///
104/// # Detached Mode
105///
106/// This mode enables separate usage of [OpLog] and [AppState].
107/// It facilitates temporal navigation. [AppState] can be reverted to
108/// any version contained within the [OpLog].
109///
110/// `LoroApp::detach()` separates [AppState] from [OpLog]. In this mode,
111/// updates to [OpLog] won't affect [AppState], while updates to [AppState]
112/// will continue to affect [OpLog].
113#[derive(Debug, Clone)]
114pub struct LoroDoc {
115    inner: Arc<LoroDocInner>,
116}
117
118impl LoroDoc {
119    pub(crate) fn from_inner(inner: Arc<LoroDocInner>) -> Self {
120        Self { inner }
121    }
122}
123
124impl std::ops::Deref for LoroDoc {
125    type Target = LoroDocInner;
126
127    fn deref(&self) -> &Self::Target {
128        &self.inner
129    }
130}
131
132pub struct LoroDocInner {
133    oplog: Arc<Mutex<OpLog>>,
134    state: Arc<Mutex<DocState>>,
135    arena: SharedArena,
136    config: Configure,
137    observer: Arc<Observer>,
138    diff_calculator: Arc<Mutex<DiffCalculator>>,
139    // when dropping the doc, the txn will be committed
140    txn: Arc<Mutex<Option<Transaction>>>,
141    auto_commit: AtomicBool,
142    detached: AtomicBool,
143    local_update_subs: SubscriberSetWithQueue<(), LocalUpdateCallback, Vec<u8>>,
144    peer_id_change_subs: SubscriberSetWithQueue<(), PeerIdUpdateCallback, ID>,
145}
146
147/// The version of the loro crate
148pub const LORO_VERSION: &str = include_str!("../VERSION");