mdcs-db 0.1.2

Database layer for the Carnelia MDCS - Document API with collaborative features
Documentation
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! RGA Text - Collaborative text CRDT based on Replicated Growable Array.
//!
//! Provides character-level collaborative text editing with:
//! - Insert at any position
//! - Delete ranges
//! - Stable position anchors for cursor sync
//!
//! Based on the RGA algorithm but optimized for text.

use mdcs_core::lattice::Lattice;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

/// Unique identifier for a character in the text.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TextId {
    /// The replica that created this character.
    pub replica: String,
    /// Sequence number within that replica.
    pub seq: u64,
}

impl TextId {
    pub fn new(replica: impl Into<String>, seq: u64) -> Self {
        Self {
            replica: replica.into(),
            seq,
        }
    }

    /// Create a genesis ID (for the virtual start of text).
    pub fn genesis() -> Self {
        Self {
            replica: "".to_string(),
            seq: 0,
        }
    }

    /// Create an end marker ID.
    pub fn end() -> Self {
        Self {
            replica: "".to_string(),
            seq: u64::MAX,
        }
    }
}

impl PartialOrd for TextId {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for TextId {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Higher sequence = later in causal order
        // Tie-break on replica ID for determinism
        self.seq
            .cmp(&other.seq)
            .then_with(|| self.replica.cmp(&other.replica))
    }
}

/// A character node in the RGA text.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
struct TextNode {
    /// The unique ID of this character.
    id: TextId,
    /// The character (or None if deleted).
    char: Option<char>,
    /// The ID of the character this was inserted after.
    origin: TextId,
    /// Whether this node is deleted (tombstone).
    deleted: bool,
}

impl TextNode {
    fn new(id: TextId, ch: char, origin: TextId) -> Self {
        Self {
            id,
            char: Some(ch),
            origin,
            deleted: false,
        }
    }
}

/// Delta for text operations.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RGATextDelta {
    /// Characters to insert.
    pub inserts: Vec<(TextId, char, TextId)>, // (id, char, origin)
    /// IDs of characters to delete.
    pub deletes: Vec<TextId>,
}

impl RGATextDelta {
    pub fn new() -> Self {
        Self {
            inserts: Vec::new(),
            deletes: Vec::new(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.inserts.is_empty() && self.deletes.is_empty()
    }
}

impl Default for RGATextDelta {
    fn default() -> Self {
        Self::new()
    }
}

/// Collaborative text CRDT using RGA algorithm.
///
/// Supports character-level insert and delete with
/// deterministic conflict resolution.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RGAText {
    /// All nodes indexed by their ID.
    nodes: HashMap<TextId, TextNode>,
    /// Children of each node (characters inserted after it).
    /// Maps origin -> list of children sorted by ID (descending for RGA).
    children: HashMap<TextId, Vec<TextId>>,
    /// The replica ID for this instance.
    replica_id: String,
    /// Sequence counter for generating IDs.
    seq: u64,
    /// Pending delta for replication.
    #[serde(skip)]
    pending_delta: Option<RGATextDelta>,
}

impl RGAText {
    /// Create a new empty text.
    pub fn new(replica_id: impl Into<String>) -> Self {
        let replica_id = replica_id.into();
        let mut text = Self {
            nodes: HashMap::new(),
            children: HashMap::new(),
            replica_id,
            seq: 0,
            pending_delta: None,
        };

        // Initialize with genesis node's children list
        text.children.insert(TextId::genesis(), Vec::new());

        text
    }

    /// Get the replica ID.
    pub fn replica_id(&self) -> &str {
        &self.replica_id
    }

    /// Generate a new unique ID.
    fn next_id(&mut self) -> TextId {
        self.seq += 1;
        TextId::new(&self.replica_id, self.seq)
    }

    /// Insert a string at the given position.
    pub fn insert(&mut self, position: usize, text: &str) {
        let mut origin = self
            .id_at_index(position.saturating_sub(1))
            .unwrap_or(TextId::genesis());

        for ch in text.chars() {
            let id = self.next_id();
            let node = TextNode::new(id.clone(), ch, origin.clone());

            self.integrate_node(node.clone());

            // Record in delta
            let delta = self.pending_delta.get_or_insert_with(RGATextDelta::new);
            delta.inserts.push((id.clone(), ch, origin));

            origin = id;
        }
    }

    /// Delete characters from start to start+length.
    pub fn delete(&mut self, start: usize, length: usize) {
        let ids: Vec<_> = self
            .visible_ids()
            .skip(start)
            .take(length)
            .cloned()
            .collect();

        for id in ids {
            self.delete_by_id(&id);
        }
    }

    /// Delete a character by its ID.
    fn delete_by_id(&mut self, id: &TextId) -> Option<char> {
        if let Some(node) = self.nodes.get_mut(id) {
            if !node.deleted {
                node.deleted = true;
                let ch = node.char.take();

                // Record delta
                let delta = self.pending_delta.get_or_insert_with(RGATextDelta::new);
                delta.deletes.push(id.clone());

                return ch;
            }
        }
        None
    }

    /// Replace a range with new text.
    pub fn replace(&mut self, start: usize, end: usize, text: &str) {
        self.delete(start, end - start);
        self.insert(start, text);
    }

    /// Splice: delete some characters and insert new ones.
    pub fn splice(&mut self, position: usize, delete_count: usize, insert: &str) {
        self.delete(position, delete_count);
        self.insert(position, insert);
    }

    /// Get the length (number of visible characters).
    pub fn len(&self) -> usize {
        self.nodes.values().filter(|n| !n.deleted).count()
    }

    /// Check if empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get character at position.
    pub fn char_at(&self, position: usize) -> Option<char> {
        self.iter().nth(position)
    }

    /// Get a substring.
    pub fn slice(&self, start: usize, end: usize) -> String {
        self.iter().skip(start).take(end - start).collect()
    }

    /// Iterate over visible characters.
    pub fn iter(&self) -> impl Iterator<Item = char> + '_ {
        self.iter_nodes()
            .filter(|n| !n.deleted)
            .filter_map(|n| n.char)
    }

    /// Get the ID at a visible index.
    fn id_at_index(&self, index: usize) -> Option<TextId> {
        self.visible_ids().nth(index).cloned()
    }

    /// Iterate over visible IDs.
    fn visible_ids(&self) -> impl Iterator<Item = &TextId> + '_ {
        self.iter_nodes().filter(|n| !n.deleted).map(|n| &n.id)
    }

    /// Convert a TextId to a visible position.
    pub fn id_to_position(&self, id: &TextId) -> Option<usize> {
        self.visible_ids().position(|i| i == id)
    }

    /// Convert a visible position to a TextId.
    pub fn position_to_id(&self, position: usize) -> Option<TextId> {
        self.id_at_index(position)
    }

    /// Iterate over all nodes in order.
    fn iter_nodes(&self) -> impl Iterator<Item = &TextNode> + '_ {
        TextIterator {
            text: self,
            stack: vec![TextId::genesis()],
            visited: HashSet::new(),
        }
    }

    /// Integrate a node into the text.
    fn integrate_node(&mut self, node: TextNode) {
        let id = node.id.clone();
        let origin = node.origin.clone();

        // Add to nodes map
        self.nodes.insert(id.clone(), node);

        // Add to children of origin, maintaining sort order (descending by ID for RGA)
        let children = self.children.entry(origin).or_default();
        let pos = children
            .iter()
            .position(|c| c < &id)
            .unwrap_or(children.len());
        children.insert(pos, id.clone());

        // Ensure this node has a children entry
        self.children.entry(id).or_default();
    }

    /// Take the pending delta.
    pub fn take_delta(&mut self) -> Option<RGATextDelta> {
        self.pending_delta.take()
    }

    /// Apply a delta from another replica.
    pub fn apply_delta(&mut self, delta: &RGATextDelta) {
        // Apply inserts
        for (id, ch, origin) in &delta.inserts {
            if !self.nodes.contains_key(id) {
                let node = TextNode::new(id.clone(), *ch, origin.clone());
                self.integrate_node(node);
            }
        }

        // Apply deletes
        for id in &delta.deletes {
            if let Some(node) = self.nodes.get_mut(id) {
                node.deleted = true;
                node.char = None;
            }
        }
    }
}

/// Iterator for traversing text nodes in order.
struct TextIterator<'a> {
    text: &'a RGAText,
    stack: Vec<TextId>,
    visited: HashSet<TextId>,
}

impl<'a> Iterator for TextIterator<'a> {
    type Item = &'a TextNode;

    fn next(&mut self) -> Option<Self::Item> {
        while let Some(id) = self.stack.pop() {
            if self.visited.contains(&id) {
                continue;
            }
            self.visited.insert(id.clone());

            // Push children in reverse order
            if let Some(children) = self.text.children.get(&id) {
                for child in children.iter().rev() {
                    if !self.visited.contains(child) {
                        self.stack.push(child.clone());
                    }
                }
            }

            // Return the node (skip genesis)
            if id != TextId::genesis() {
                if let Some(node) = self.text.nodes.get(&id) {
                    return Some(node);
                }
            }
        }
        None
    }
}

impl std::fmt::Display for RGAText {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for ch in self.iter() {
            write!(f, "{}", ch)?;
        }
        Ok(())
    }
}

impl PartialEq for RGAText {
    fn eq(&self, other: &Self) -> bool {
        self.to_string() == other.to_string()
    }
}

impl Eq for RGAText {}

impl Lattice for RGAText {
    fn bottom() -> Self {
        Self::new("")
    }

    fn join(&self, other: &Self) -> Self {
        let mut result = self.clone();

        // Merge all nodes from other
        for (id, node) in &other.nodes {
            if let Some(existing) = result.nodes.get_mut(id) {
                if node.deleted {
                    existing.deleted = true;
                    existing.char = None;
                }
            } else {
                result.integrate_node(node.clone());
            }
        }

        result
    }
}

impl Default for RGAText {
    fn default() -> Self {
        Self::new("")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_insert() {
        let mut text = RGAText::new("r1");
        text.insert(0, "Hello");
        assert_eq!(text.to_string(), "Hello");
        assert_eq!(text.len(), 5);
    }

    #[test]
    fn test_insert_at_position() {
        let mut text = RGAText::new("r1");
        text.insert(0, "Hello");
        text.insert(5, " World");
        assert_eq!(text.to_string(), "Hello World");
    }

    #[test]
    fn test_insert_in_middle() {
        let mut text = RGAText::new("r1");
        text.insert(0, "Helo");
        text.insert(2, "l");
        assert_eq!(text.to_string(), "Hello");
    }

    #[test]
    fn test_delete() {
        let mut text = RGAText::new("r1");
        text.insert(0, "Hello World");
        text.delete(5, 6); // Delete " World"
        assert_eq!(text.to_string(), "Hello");
    }

    #[test]
    fn test_replace() {
        let mut text = RGAText::new("r1");
        text.insert(0, "Hello World");
        text.replace(6, 11, "Rust");
        assert_eq!(text.to_string(), "Hello Rust");
    }

    #[test]
    fn test_concurrent_inserts() {
        let mut text1 = RGAText::new("r1");
        let mut text2 = RGAText::new("r2");

        // Both start with "Hello"
        text1.insert(0, "Hello");
        text2.apply_delta(&text1.take_delta().unwrap());

        // Concurrent inserts at the end
        text1.insert(5, " World");
        text2.insert(5, " Rust");

        // Exchange deltas
        let delta1 = text1.take_delta().unwrap();
        let delta2 = text2.take_delta().unwrap();

        text1.apply_delta(&delta2);
        text2.apply_delta(&delta1);

        // Should converge to same text
        assert_eq!(text1.to_string(), text2.to_string());
        // Both additions should be present
        assert!(text1.to_string().contains("World") || text1.to_string().contains("Rust"));
    }

    #[test]
    fn test_concurrent_insert_delete() {
        let mut text1 = RGAText::new("r1");
        let mut text2 = RGAText::new("r2");

        // Both start with "Hello"
        text1.insert(0, "Hello");
        text2.apply_delta(&text1.take_delta().unwrap());

        // r1 deletes "llo", r2 inserts "x" after "He"
        text1.delete(2, 3);
        text2.insert(2, "x");

        // Exchange deltas
        let delta1 = text1.take_delta().unwrap();
        let delta2 = text2.take_delta().unwrap();

        text1.apply_delta(&delta2);
        text2.apply_delta(&delta1);

        // Should converge
        assert_eq!(text1.to_string(), text2.to_string());
        // x should survive, llo should be deleted
        assert!(text1.to_string().contains("x"));
        assert!(!text1.to_string().contains("llo"));
    }

    #[test]
    fn test_char_at() {
        let mut text = RGAText::new("r1");
        text.insert(0, "Hello");

        assert_eq!(text.char_at(0), Some('H'));
        assert_eq!(text.char_at(4), Some('o'));
        assert_eq!(text.char_at(5), None);
    }

    #[test]
    fn test_slice() {
        let mut text = RGAText::new("r1");
        text.insert(0, "Hello World");

        assert_eq!(text.slice(0, 5), "Hello");
        assert_eq!(text.slice(6, 11), "World");
    }

    #[test]
    fn test_position_id_conversion() {
        let mut text = RGAText::new("r1");
        text.insert(0, "Hello");

        let id = text.position_to_id(2).unwrap();
        let pos = text.id_to_position(&id).unwrap();

        assert_eq!(pos, 2);
    }

    #[test]
    fn test_lattice_join() {
        let mut text1 = RGAText::new("r1");
        let mut text2 = RGAText::new("r2");

        text1.insert(0, "Hello");
        text2.insert(0, "World");

        let merged = text1.join(&text2);

        // Both texts should be somehow combined
        assert!(merged.len() >= 5);
    }
}