1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! CRDT state management backed by loro.
//!
//! Each `CrdtState` wraps a `LoroDoc` representing one tenant/namespace's
//! state. Collections within the doc are `LoroMap` instances keyed by row ID,
//! where each row is itself a `LoroMap` of field→value.
use loro::{LoroDoc, LoroMap, LoroValue, ValueOrContainer};
use crate::error::{CrdtError, Result};
/// A CRDT state for a single tenant/namespace.
pub struct CrdtState {
doc: LoroDoc,
peer_id: u64,
}
impl CrdtState {
/// Create a new empty state for the given peer.
pub fn new(peer_id: u64) -> Result<Self> {
let doc = LoroDoc::new();
doc.set_peer_id(peer_id)
.map_err(|e| CrdtError::Loro(format!("failed to set peer_id {peer_id}: {e}")))?;
Ok(Self { doc, peer_id })
}
/// Insert or update a row in a collection.
pub fn upsert(
&self,
collection: &str,
row_id: &str,
fields: &[(&str, LoroValue)],
) -> Result<()> {
let coll = self.doc.get_map(collection);
let row_container = coll
.insert_container(row_id, LoroMap::new())
.map_err(|e| CrdtError::Loro(e.to_string()))?;
for (field, value) in fields {
row_container
.insert(field, value.clone())
.map_err(|e| CrdtError::Loro(e.to_string()))?;
}
Ok(())
}
/// Delete a row from a collection.
pub fn delete(&self, collection: &str, row_id: &str) -> Result<()> {
let coll = self.doc.get_map(collection);
coll.delete(row_id)
.map_err(|e| CrdtError::Loro(e.to_string()))?;
Ok(())
}
/// Delete all rows in a collection. Returns the number of rows deleted.
pub fn clear_collection(&self, collection: &str) -> Result<usize> {
let coll = self.doc.get_map(collection);
let keys: Vec<String> = coll.keys().map(|k| k.to_string()).collect();
let count = keys.len();
for key in &keys {
coll.delete(key)
.map_err(|e| CrdtError::Loro(e.to_string()))?;
}
Ok(count)
}
/// Read a single row's fields as a `LoroValue::Map`.
///
/// Returns the deep value of the row (all nested containers resolved),
/// or `None` if the row does not exist.
pub fn read_row(&self, collection: &str, row_id: &str) -> Option<LoroValue> {
let path = format!("{collection}/{row_id}");
Some(self.doc.get_by_str_path(&path)?.get_deep_value())
}
/// Check if a row exists in a collection.
pub fn row_exists(&self, collection: &str, row_id: &str) -> bool {
let coll = self.doc.get_map(collection);
coll.get(row_id).is_some()
}
/// List all collection names (top-level map keys in the Loro doc).
pub fn collection_names(&self) -> Vec<String> {
let root = self.doc.get_deep_value();
match root {
LoroValue::Map(map) => map.keys().map(|k| k.to_string()).collect(),
_ => Vec::new(),
}
}
/// Get all row IDs in a collection.
pub fn row_ids(&self, collection: &str) -> Vec<String> {
let coll = self.doc.get_map(collection);
coll.keys().map(|k| k.to_string()).collect()
}
/// Check if a value exists for the given field across all rows in a collection.
/// Used for UNIQUE constraint checking.
pub fn field_value_exists(&self, collection: &str, field: &str, value: &LoroValue) -> bool {
let coll = self.doc.get_map(collection);
for key in coll.keys() {
let path = format!("{collection}/{key}/{field}");
if let Some(voc) = self.doc.get_by_str_path(&path) {
let field_val = match voc {
ValueOrContainer::Value(v) => v,
ValueOrContainer::Container(_) => {
continue;
}
};
if &field_val == value {
return true;
}
}
}
false
}
/// Export the current state as bytes for sync.
pub fn export_snapshot(&self) -> Result<Vec<u8>> {
self.doc
.export(loro::ExportMode::Snapshot)
.map_err(|e| CrdtError::Loro(format!("snapshot export failed: {e}")))
}
/// Import remote updates.
pub fn import(&self, data: &[u8]) -> Result<()> {
self.doc
.import(data)
.map_err(|e| CrdtError::DeltaApplyFailed(e.to_string()))?;
Ok(())
}
/// Get the underlying LoroDoc for advanced operations.
pub fn doc(&self) -> &LoroDoc {
&self.doc
}
/// Peer ID of this state.
pub fn peer_id(&self) -> u64 {
self.peer_id
}
/// Compact the CRDT history by replacing the internal LoroDoc with a
/// shallow snapshot.
///
/// A shallow snapshot contains the current state but discards the
/// full operation history. This is the CRDT equivalent of WAL
/// truncation after checkpoint.
///
/// After compaction:
/// - All current state is preserved (reads return same values).
/// - New deltas can still be applied and merged.
/// - Historical operations before the snapshot point are gone.
/// - Peers that sync after compaction receive a full snapshot
/// instead of incremental deltas (acceptable for long-offline peers).
///
/// Call this periodically (e.g., every 30 minutes or when memory
/// pressure exceeds threshold) to prevent unbounded history growth.
pub fn compact_history(&mut self) -> Result<()> {
// Export a shallow snapshot at the current frontiers.
let frontiers = self.doc.oplog_frontiers();
let snapshot = self
.doc
.export(loro::ExportMode::shallow_snapshot(&frontiers))
.map_err(|e| CrdtError::Loro(format!("shallow snapshot export: {e}")))?;
// Replace the doc with a fresh one loaded from the snapshot.
let new_doc = LoroDoc::new();
new_doc
.set_peer_id(self.peer_id)
.map_err(|e| CrdtError::Loro(format!("failed to set peer_id on compacted doc: {e}")))?;
new_doc
.import(&snapshot)
.map_err(|e| CrdtError::Loro(format!("shallow snapshot import: {e}")))?;
self.doc = new_doc;
Ok(())
}
/// Estimated memory usage of the CRDT state (bytes).
///
/// Includes operation history, current state, and internal caches.
/// Use this to decide when to trigger `compact_history()`.
pub fn estimated_memory_bytes(&self) -> usize {
// Loro doesn't expose a direct memory metric.
// Use snapshot size as a proxy — it's proportional to state size.
// This is not precise but good enough for pressure monitoring.
self.doc
.export(loro::ExportMode::Snapshot)
.map(|s| s.len())
.unwrap_or(0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn upsert_and_check_existence() {
let state = CrdtState::new(1).unwrap();
state
.upsert(
"users",
"user-1",
&[
("name", LoroValue::String("Alice".into())),
("email", LoroValue::String("alice@example.com".into())),
],
)
.unwrap();
assert!(state.row_exists("users", "user-1"));
assert!(!state.row_exists("users", "user-2"));
}
#[test]
fn delete_row() {
let state = CrdtState::new(1).unwrap();
state
.upsert(
"users",
"user-1",
&[("name", LoroValue::String("Alice".into()))],
)
.unwrap();
assert!(state.row_exists("users", "user-1"));
state.delete("users", "user-1").unwrap();
assert!(!state.row_exists("users", "user-1"));
}
#[test]
fn row_ids_listing() {
let state = CrdtState::new(1).unwrap();
state
.upsert("users", "a", &[("x", LoroValue::I64(1))])
.unwrap();
state
.upsert("users", "b", &[("x", LoroValue::I64(2))])
.unwrap();
let mut ids = state.row_ids("users");
ids.sort();
assert_eq!(ids, vec!["a", "b"]);
}
#[test]
fn field_value_uniqueness_check() {
let state = CrdtState::new(1).unwrap();
state
.upsert(
"users",
"u1",
&[("email", LoroValue::String("alice@example.com".into()))],
)
.unwrap();
assert!(state.field_value_exists(
"users",
"email",
&LoroValue::String("alice@example.com".into())
));
assert!(!state.field_value_exists(
"users",
"email",
&LoroValue::String("bob@example.com".into())
));
}
#[test]
fn compact_history_preserves_state() {
let mut state = CrdtState::new(1).unwrap();
// Create some state with history.
state
.upsert(
"users",
"u1",
&[("name", LoroValue::String("Alice".into()))],
)
.unwrap();
state
.upsert("users", "u2", &[("name", LoroValue::String("Bob".into()))])
.unwrap();
// Update to create more history.
state
.upsert(
"users",
"u1",
&[("name", LoroValue::String("Alice Updated".into()))],
)
.unwrap();
// Compact.
state.compact_history().unwrap();
// State should be preserved after compaction.
assert!(state.row_exists("users", "u1"));
assert!(state.row_exists("users", "u2"));
// New operations should still work.
state
.upsert(
"users",
"u3",
&[("name", LoroValue::String("Carol".into()))],
)
.unwrap();
assert!(state.row_exists("users", "u3"));
}
#[test]
fn estimated_memory_grows_with_data() {
let state = CrdtState::new(1).unwrap();
let before = state.estimated_memory_bytes();
for i in 0..100 {
state
.upsert(
"items",
&format!("item-{i}"),
&[("value", LoroValue::I64(i))],
)
.unwrap();
}
let after = state.estimated_memory_bytes();
assert!(
after > before,
"memory should grow: before={before}, after={after}"
);
}
#[test]
fn snapshot_roundtrip() {
let state1 = CrdtState::new(1).unwrap();
state1
.upsert("users", "u1", &[("name", LoroValue::String("Bob".into()))])
.unwrap();
let snapshot = state1.export_snapshot().unwrap();
let state2 = CrdtState::new(2).unwrap();
state2.import(&snapshot).unwrap();
assert!(state2.row_exists("users", "u1"));
}
}