crdt-richtext 0.1.1

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
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
use crate::{Counter, OpID};
use append_only_bytes::BytesSlice;
use core::fmt;

use generic_btree::rle::{HasLength, Mergeable, Sliceable};
use smallvec::SmallVec;
use std::{
    ops::{Deref, DerefMut},
    str::Chars,
};

use self::{rich_tree_btree_impl::RichTreeTrait, utf16::get_utf16_len};

use super::ann::{AnchorSetDiff, CacheAnchorSet, ElemAnchorSet};

pub(crate) mod query;
pub(crate) mod rich_tree_btree_impl;
pub mod utf16;

type AnnIdx = i32;
#[derive(Clone)]
pub struct Elem {
    inner: Box<ElemInner>,
}

#[derive(Clone)]
pub struct ElemInner {
    pub id: OpID,
    pub left: Option<OpID>,
    pub right: Option<OpID>,
    pub string: BytesSlice,
    pub utf16_len: u32,
    pub status: Status,
    pub anchor_set: ElemAnchorSet,
}

impl Deref for Elem {
    type Target = ElemInner;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for Elem {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

#[test]
fn size() {
    assert_eq!(std::mem::size_of::<Elem>(), 96);
}

impl std::fmt::Debug for Elem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Elem")
            .field("id", &self.id)
            .field("left", &self.left)
            .field("right", &self.right)
            .field("string", &std::str::from_utf8(&self.string))
            .field("utf16_len", &self.utf16_len)
            .field("status", &self.status)
            .field("anchor_set", &self.anchor_set)
            .finish()
    }
}

impl Elem {
    pub fn new(id: OpID, left: Option<OpID>, right: Option<OpID>, string: BytesSlice) -> Self {
        Elem {
            inner: Box::new(ElemInner {
                id,
                left,
                right,
                utf16_len: get_utf16_len(&string),
                string,
                status: Status::ALIVE,
                anchor_set: Default::default(),
            }),
        }
    }

    pub fn id_last(&self) -> OpID {
        OpID {
            client: self.id.client,
            counter: self.id.counter + self.atom_len() as Counter - 1,
        }
    }

    #[inline(always)]
    pub fn content_len(&self) -> usize {
        if self.status.is_dead() {
            0
        } else {
            self.string.len()
        }
    }

    #[inline(always)]
    pub fn atom_len(&self) -> usize {
        self.string.len()
    }

    #[inline(always)]
    pub fn is_dead(&self) -> bool {
        self.status.is_dead()
    }

    pub fn split(&mut self, offset: usize) -> Self {
        assert!(offset != 0);
        let start = offset;
        let s = self.string.slice_clone(offset..);
        let utf16_len = get_utf16_len(&s);
        let right = Self {
            inner: Box::new(ElemInner {
                anchor_set: self.anchor_set.split(),
                id: self.id.inc(start as Counter),
                left: Some(self.id.inc(start as Counter - 1)),
                right: self.right,
                string: s,
                utf16_len,
                status: self.status,
            }),
        };
        self.utf16_len -= utf16_len;
        self.string = self.string.slice_clone(..offset);
        right
    }

    #[inline(always)]
    pub fn local_delete(&mut self) -> bool {
        if !self.is_dead() {
            self.status.deleted_times += 1;
            true
        } else {
            false
        }
    }

    #[inline(always)]
    pub fn apply_remote_delete(&mut self) {
        self.status.deleted_times += 1;
    }

    #[must_use]
    pub fn update<R>(
        &mut self,
        start: usize,
        end: usize,
        f: &mut dyn FnMut(&mut Elem) -> R,
    ) -> (SmallVec<[Elem; 2]>, Option<R>) {
        let mut ans = SmallVec::new();
        debug_assert!(start <= end && end <= self.rle_len());
        if start == end {
            return (ans, None);
        }

        assert!(end > start);
        if start == 0 && end == self.atom_len() {
            let r = f(self);
            return (ans, Some(r));
        }
        if start == 0 {
            let right = self.split(end);
            let r = f(self);
            ans.push(right);
            return (ans, Some(r));
        }
        if end == self.atom_len() {
            let mut right = self.split(start);
            let r = f(&mut right);
            ans.push(right);
            return (ans, Some(r));
        }

        let mut middle = self.split(start);
        let right = middle.split(end - start);
        let r = f(&mut middle);
        ans.push(middle);
        ans.push(right);
        (ans, Some(r))
    }

    #[must_use]
    pub fn update_twice(
        &mut self,
        f_start: usize,
        f_end_g_start: usize,
        g_end: usize,
        f: &mut dyn FnMut(&mut Elem),
        g: &mut dyn FnMut(&mut Elem),
    ) -> SmallVec<[Elem; 4]> {
        let mut ans = SmallVec::new();
        debug_assert!(f_start < f_end_g_start && f_end_g_start < g_end);
        debug_assert!(g_end <= self.rle_len());
        if f_start == 0 && g_end == self.atom_len() {
            let new = self.split(f_end_g_start);
            ans.push(new);
            f(self);
            g(&mut ans[0]);
            return ans;
        }

        if f_start == 0 {
            let mut middle = self.split(f_end_g_start);
            let mut new_elems = middle.update(0, g_end - f_end_g_start, g);
            ans.push(middle);
            ans.append(&mut new_elems.0);
            f(self);
            return ans;
        }

        if g_end == self.atom_len() {
            let mut middle = self.split(f_start);
            let mut new_elems = middle.update(0, f_end_g_start - f_start, f);
            ans.push(middle);
            ans.append(&mut new_elems.0);
            g(ans.last_mut().unwrap());
            return ans;
        }

        let len = self.atom_len();
        let mut left = self.split(f_start);
        let mut middle0 = left.split(f_end_g_start - f_start);
        let mut middle1 = middle0.split(g_end - f_end_g_start);
        let right = middle1.split(len - g_end);
        f(&mut middle0);
        g(&mut middle1);
        ans.push(left);
        ans.push(middle0);
        ans.push(middle1);
        ans.push(right);
        ans
    }

    pub fn merge_slice(&mut self, s: &BytesSlice) {
        self.string.try_merge(s).unwrap();
        self.utf16_len += get_utf16_len(s);
    }

    pub fn contains_id(&self, id: OpID) -> bool {
        id.client == self.id.client
            && self.id.counter <= id.counter
            && self.id.counter + self.rle_len() as Counter > id.counter
    }

    pub fn overlap(&self, id: OpID, len: usize) -> bool {
        id.client == self.id.client
            && self.id.counter < id.counter + len as Counter
            && self.id.counter + self.rle_len() as Counter > id.counter as Counter
    }

    pub fn try_merge_arr(arr: &mut Vec<Self>, mut from: usize, mut len: usize) -> bool {
        len = len.min(arr.len() - from);
        while len > 0 {
            let mut j = from + 1;
            while j < arr.len() {
                let (left, right) = arr.split_at_mut(j);
                if left[from].can_merge(&right[0]) {
                    left[from].merge_right(&right[0]);
                    j += 1;
                } else {
                    break;
                }
            }
            if j > from + 1 {
                arr.drain(from + 1..j);
                // may continue?
                len = len.saturating_sub(j - from);
                from += 1;
            } else {
                len -= 1;
                from += 1;
            }
        }

        false
    }

    #[inline]
    pub fn has_after_anchor(&self) -> bool {
        self.anchor_set.has_after_anchor()
    }

    #[inline]
    #[allow(unused)]
    pub fn has_before_anchor(&self) -> bool {
        self.anchor_set.has_before_anchor()
    }
}

impl Mergeable for Elem {
    fn can_merge(&self, rhs: &Self) -> bool {
        self.id.client == rhs.id.client
            && self.id.counter + self.atom_len() as Counter == rhs.id.counter
            && rhs.left == Some(self.id_last())
            && self.right == rhs.right
            && self.status == rhs.status
            && self.string.can_merge(&rhs.string)
            && self.anchor_set.can_merge(&rhs.anchor_set)
    }

    fn merge_right(&mut self, rhs: &Self) {
        self.string.try_merge(&rhs.string).unwrap();
        self.utf16_len += rhs.utf16_len;
        self.anchor_set.merge_right(&rhs.anchor_set);
    }

    fn merge_left(&mut self, lhs: &Self) {
        self.id = lhs.id;
        self.left = lhs.left;
        let mut string = lhs.string.clone();
        string.try_merge(&self.string).unwrap();
        self.string = string;
        self.utf16_len += lhs.utf16_len;
        self.anchor_set.merge_left(&lhs.anchor_set);
    }
}

impl HasLength for Elem {
    fn rle_len(&self) -> usize {
        self.atom_len()
    }
}

impl Sliceable for Elem {
    fn slice(&self, range: impl std::ops::RangeBounds<usize>) -> Self {
        let start = match range.start_bound() {
            std::ops::Bound::Included(x) => *x,
            std::ops::Bound::Excluded(x) => *x + 1,
            std::ops::Bound::Unbounded => 0,
        };
        let end = match range.end_bound() {
            std::ops::Bound::Included(x) => *x + 1,
            std::ops::Bound::Excluded(x) => *x,
            std::ops::Bound::Unbounded => self.atom_len(),
        };
        let s = self.string.slice_clone(range);
        let utf16_len = get_utf16_len(&s);
        Self {
            inner: Box::new(ElemInner {
                anchor_set: self.anchor_set.trim(start != 0, end != self.rle_len()),
                id: self.id.inc(start as Counter),
                left: if start == 0 {
                    self.left
                } else {
                    Some(self.id.inc(start as Counter - 1))
                },
                right: self.right,
                string: s,
                utf16_len,
                status: self.status,
            }),
        }
    }

    fn slice_(&mut self, range: impl std::ops::RangeBounds<usize>)
    where
        Self: Sized,
    {
        let start = match range.start_bound() {
            std::ops::Bound::Included(x) => *x,
            std::ops::Bound::Excluded(x) => *x + 1,
            std::ops::Bound::Unbounded => 0,
        };
        let end = match range.end_bound() {
            std::ops::Bound::Included(x) => *x + 1,
            std::ops::Bound::Excluded(x) => *x,
            std::ops::Bound::Unbounded => self.atom_len(),
        };
        if start == 0 && end == self.atom_len() {
            return;
        }

        self.inner
            .anchor_set
            .trim_(start != 0, end != self.atom_len());
        self.id = self.id.inc(start as Counter);
        self.left = if start == 0 {
            self.left
        } else {
            Some(self.id.inc(start as Counter - 1))
        };
        self.string = self.string.slice_clone(range);
        self.utf16_len = get_utf16_len(&self.string);
    }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Status {
    pub future: bool,
    pub deleted_times: u16,
}

impl Status {
    pub const ALIVE: Status = Status {
        future: false,
        deleted_times: 0,
    };

    #[allow(unused)]
    pub fn new() -> Self {
        Status {
            future: false,
            deleted_times: 0,
        }
    }

    #[inline(always)]
    fn is_dead(&self) -> bool {
        self.future || self.deleted_times > 0
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) struct Cache {
    pub len: u32,
    pub utf16_len: u32,
    pub anchor_set: CacheAnchorSet,
}

#[derive(Default, Debug)]
pub(crate) struct CacheDiff {
    pub(super) anchor_diff: AnchorSetDiff,
    pub(super) len_diff: isize,
    pub(super) utf16_len_diff: isize,
}

impl Cache {
    fn apply_diff(&mut self, diff: &CacheDiff) {
        self.len = (self.len as isize + diff.len_diff) as u32;
        self.utf16_len = (self.utf16_len as isize + diff.utf16_len_diff) as u32;
        self.anchor_set.apply_diff(&diff.anchor_diff);
    }
}

impl CacheDiff {
    pub fn new_len_diff(diff: isize, utf16_len_diff: isize) -> CacheDiff {
        CacheDiff {
            len_diff: diff,
            utf16_len_diff,
            anchor_diff: Default::default(),
        }
    }
}