crdt-richtext 0.1.0

Richtext CRDT, Rust implementation of Peritext and Fugue
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
use super::*;
pub use range_map::tree_impl::TreeRangeMap;
pub use range_map::RangeMap;
mod range_map;
#[cfg(feature = "test")]
pub mod test_utils;
use range_map::{AnnPosRelativeToInsert, Span};

#[derive(Debug)]
pub struct CrdtRange<R> {
    pub(crate) range_map: R,
}

impl<R: RangeMap + Debug> CrdtRange<R> {
    pub fn new() -> Self {
        let mut r = R::init();
        r.insert_directly(0, 2);
        CrdtRange { range_map: r }
    }

    /// Insert a new span of text into the range. It's used to sync
    /// List Crdt insert ops.  
    ///
    /// It will only generate new RangeOp(Patches) when inserting new
    /// text locally and there are annotations attached to the tombstones
    /// at `pos`.
    ///
    /// - `cmp(target)` returns whether the target is in right side or
    ///   left side of the new inserted op. `target` may be any op id
    ///   from the List CRDT because it's used to test both sides of an
    ///   annotation
    pub fn insert_text<Cmp>(
        &mut self,
        pos: usize,
        len: usize,
        is_local: bool,
        left_id: Option<OpID>,
        right_id: Option<OpID>,
        next_lamport: Lamport,
        next_op_id: OpID,
        mut cmp: Cmp,
    ) -> Vec<RangeOp>
    where
        Cmp: FnMut(OpID) -> Ordering,
    {
        let mut ans = vec![];
        // Maybe add the zero-len filter rule as a requirement for the range_map?
        let spans = self.get_trimmed_spans_around(pos);
        assert!(spans.len() <= 3, "{}", spans.len());
        assert!(spans.iter().map(|x| x.len).sum::<usize>() == 2);
        let non_empty_span_count = spans.iter().filter(|x| x.len != 0).count();
        if is_local && non_empty_span_count > 1 {
            self.gen_patch(
                non_empty_span_count,
                spans,
                left_id,
                right_id,
                next_lamport,
                next_op_id,
                &mut ans,
            );
        }

        self.range_map.insert(pos * 3 + 1, len * 3, |ann| {
            // dbg!(&tombstones, first_new_op_id, ann, relative);
            let start_before_insert = match ann.range.start.id {
                Some(id) => cmp(id) == Ordering::Less,
                None => true,
            };
            let end_after_insert = match ann.range.end.id {
                Some(id) => cmp(id) == Ordering::Greater,
                None => true,
            };
            match (start_before_insert, end_after_insert) {
                (true, true) => AnnPosRelativeToInsert::IncludeInsert,
                (true, false) => AnnPosRelativeToInsert::Before,
                (false, true) => AnnPosRelativeToInsert::After,
                (false, false) => unreachable!(),
            }
        });

        ans
    }

    fn get_trimmed_spans_around(&mut self, pos: usize) -> Vec<Span> {
        let mut spans: Vec<Span> = self
            .range_map
            .get_annotations(pos * 3, 2)
            .into_iter()
            .skip_while(|x| x.len == 0)
            .collect();
        for i in (0..spans.len()).rev() {
            if spans[i].len != 0 {
                spans.drain(i + 1..);
                break;
            }
        }
        spans
    }

    /// NOTE: This is error-prone, need more attention
    fn gen_patch(
        &mut self,
        non_empty_span_count: usize,
        spans: Vec<Span>,
        left_id: Option<OpID>,
        right_id: Option<OpID>,
        mut next_lamport: Lamport,
        mut next_op_id: OpID,
        ans: &mut Vec<RangeOp>,
    ) {
        assert!(non_empty_span_count <= 2);
        let mut visited_left = false;
        let mut pure_left = BTreeSet::new();
        let mut pure_middle = BTreeSet::new();
        let mut left_annotations = BTreeSet::new();
        let mut right_annotations = BTreeSet::new();
        for span in spans {
            if !visited_left {
                // left
                assert_eq!(span.len, 1);
                visited_left = true;
                pure_left = span.annotations.clone();
                left_annotations = span.annotations;
            } else if span.len == 0 {
                // middle
                pure_middle = span.annotations;
            } else {
                // right
                assert_eq!(span.len, 1);
                for ann in span.annotations.iter() {
                    right_annotations.insert(ann.clone());
                    left_annotations.remove(ann);
                    pure_middle.remove(ann);
                }
            }
        }

        for ann in pure_left {
            right_annotations.remove(&ann);
            pure_middle.remove(&ann);
        }

        for annotation in left_annotations {
            let end_id = annotation.range.end.id;
            if end_id != left_id && end_id != right_id {
                // TODO: simplify
                if AnchorType::Before == annotation.range.end.type_ {
                    ans.push(RangeOp::Patch(Patch {
                        id: next_op_id,
                        lamport: next_lamport,
                        target_range_id: annotation.id,
                        move_start_to: annotation.range.start.id,
                        move_end_to: right_id,
                    }));
                    self.range_map.adjust_annotation(
                        annotation.id,
                        next_lamport,
                        next_op_id,
                        None,
                        Some((1, right_id)),
                    );
                    next_op_id.counter += 1;
                    next_lamport += 1;
                } else if !pure_middle.contains(&annotation) {
                    ans.push(RangeOp::Patch(Patch {
                        id: next_op_id,
                        lamport: next_lamport,
                        target_range_id: annotation.id,
                        move_start_to: annotation.range.start.id,
                        move_end_to: left_id,
                    }));
                    self.range_map.adjust_annotation(
                        annotation.id,
                        next_lamport,
                        next_op_id,
                        None,
                        Some((-1, left_id)),
                    );
                    next_op_id.counter += 1;
                    next_lamport += 1;
                }
            }
        }

        for annotation in right_annotations {
            let start_id = annotation.range.start.id;
            if start_id != left_id && start_id != right_id {
                match annotation.range.start.type_ {
                    AnchorType::Before => {
                        if !pure_middle.contains(&annotation) {
                            ans.push(RangeOp::Patch(Patch {
                                id: next_op_id,
                                lamport: next_lamport,
                                target_range_id: annotation.id,
                                move_start_to: right_id,
                                move_end_to: annotation.range.end.id,
                            }));
                            self.range_map.adjust_annotation(
                                annotation.id,
                                next_lamport,
                                next_op_id,
                                Some((1, right_id)),
                                None,
                            );
                            next_op_id.counter += 1;
                            next_lamport += 1;
                        }
                    }
                    AnchorType::After => {
                        ans.push(RangeOp::Patch(Patch {
                            id: next_op_id,
                            lamport: next_lamport,
                            target_range_id: annotation.id,
                            move_start_to: right_id,
                            move_end_to: annotation.range.end.id,
                        }));
                        self.range_map.adjust_annotation(
                            annotation.id,
                            next_lamport,
                            next_op_id,
                            Some((-1, left_id)),
                            None,
                        );
                        next_op_id.counter += 1;
                        next_lamport += 1;
                    }
                }
            }
        }
    }

    /// NOTE: This is error-prone, need more attention
    fn apply_remote_patch<Index>(&mut self, patch: Patch, index: &Index)
    where
        Index: Fn(OpID) -> Result<usize, usize>,
    {
        let Some((ann, pos)) = self.range_map.get_annotation_pos(patch.target_range_id) else { return };
        let new_start = index_start(
            Anchor {
                id: patch.move_start_to,
                type_: ann.range.start.type_,
            },
            index,
        );
        let new_end = index_end(
            Anchor {
                id: patch.move_end_to,
                type_: ann.range.end.type_,
            },
            index,
        )
        .unwrap_or(self.range_map.len());

        self.range_map.adjust_annotation(
            patch.target_range_id,
            patch.lamport,
            patch.id,
            Some((new_start as isize - pos.start as isize, patch.move_start_to)),
            Some((new_end as isize - pos.end as isize, patch.move_end_to)),
        );
    }

    pub fn delete_text(&mut self, pos: usize, len: usize) {
        self.range_map.delete(pos * 3 + 1, len * 3);
    }

    pub fn annotate(&mut self, annotation: Annotation, range: impl RangeBounds<usize>) -> RangeOp {
        let start = match range.start_bound() {
            Bound::Included(x) => *x * 3 + 2,
            Bound::Excluded(x) => *x * 3 + 3,
            Bound::Unbounded => 0,
        };
        assert!(annotation.range.start.type_ != AnchorType::After);
        assert!(annotation.range.start.id.is_some());
        let end = match range.end_bound() {
            Bound::Included(x) => *x * 3 + 3,
            Bound::Excluded(x) => *x * 3 + 2,
            Bound::Unbounded => self.range_map.len(),
        };
        self.range_map
            .annotate(start, end - start, annotation.clone());
        RangeOp::Annotate(annotation)
    }

    pub fn delete_annotation(&mut self, lamport: Lamport, op_id: OpID, target_id: OpID) -> RangeOp {
        self.range_map.delete_annotation(target_id);
        RangeOp::Patch(Patch {
            id: op_id,
            target_range_id: target_id,
            move_start_to: None,
            move_end_to: None,
            lamport,
        })
    }

    pub fn apply_remote_op<Index>(&mut self, op: RangeOp, index: &Index)
    where
        Index: Fn(OpID) -> Result<usize, usize>,
    {
        match op {
            RangeOp::Patch(patch) => {
                self.apply_remote_patch(patch, index);
            }
            RangeOp::Annotate(a) => {
                let start = index_start(a.range.start, index);
                let end = index_end(a.range.end, index).unwrap_or(self.range_map.len());
                self.range_map.annotate(start, end - start, a)
            }
        }
    }

    pub fn get_annotation_range(&mut self, id: OpID) -> Option<Range<usize>> {
        let (_, range) = self.range_map.get_annotation_pos(id)?;
        Some((range.start / 3)..(range.end / 3))
    }

    pub fn get_annotations(&mut self, range: impl RangeBounds<usize>) -> Vec<Span> {
        let start = match range.start_bound() {
            std::ops::Bound::Included(x) => x * 3 + 2,
            std::ops::Bound::Excluded(_) => unreachable!(),
            std::ops::Bound::Unbounded => 2,
        };
        let end = match range.end_bound() {
            std::ops::Bound::Included(x) => x * 3 + 3,
            std::ops::Bound::Excluded(x) => x * 3,
            std::ops::Bound::Unbounded => self.range_map.len(),
        };

        let mut last_index = 0;
        let mut ans = vec![];
        for mut span in self
            .range_map
            .get_annotations(start, end - start)
            .into_iter()
        {
            let next_index = last_index + span.len;
            let len = (next_index + 2) / 3 - (last_index + 2) / 3;
            span.len = len;

            type Key = (Lamport, OpID);
            let mut annotations: HashMap<InternalString, (Key, Vec<Arc<Annotation>>)> =
                HashMap::new();
            for a in std::mem::take(&mut span.annotations) {
                if let Some(x) = annotations.get_mut(&a.type_) {
                    if a.behavior == Behavior::Inclusive {
                        x.1.push(a);
                    } else if a.range_lamport > x.0 {
                        *x = (a.range_lamport, vec![a]);
                    }
                } else {
                    annotations.insert(a.type_.clone(), (a.range_lamport, vec![a]));
                }
            }
            span.annotations = annotations.into_values().flat_map(|x| x.1).collect();
            ans.push(span);
            last_index = next_index;
        }

        ans
    }

    pub fn len(&self) -> usize {
        self.range_map.len() / 3
    }

    pub fn is_empty(&self) -> bool {
        self.range_map.len() == 2
    }
}

fn index_start<Index>(start: Anchor, index: &Index) -> usize
where
    Index: Fn(OpID) -> Result<usize, usize>,
{
    start
        .id
        .map(|x| match index(x) {
            Ok(x) => {
                if start.type_ == AnchorType::Before {
                    x * 3 + 2
                } else {
                    x * 3 + 3
                }
            }
            Err(x) => x * 3 + 1,
        })
        .unwrap_or(0)
}

fn index_end<Index>(end: Anchor, index: &Index) -> Option<usize>
where
    Index: Fn(OpID) -> Result<usize, usize>,
{
    end.id.map(|x| match index(x) {
        Ok(x) => {
            if end.type_ == AnchorType::Before {
                x * 3 + 2
            } else {
                x * 3 + 3
            }
        }

        Err(x) => x * 3 + 1,
    })
}

impl<R: RangeMap + Debug> Default for CrdtRange<R> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(all(test, feature = "test"))]
pub mod test;