causal-length 0.2.0

CRDT's based on causal length sets
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
use super::*;
use crate::register::Register;
use std::borrow::Borrow;
use std::cmp::max;
use std::collections::HashMap;

/// Causal Length Map
///
/// A CRDT map based on an adaptation of the causal length set.
///
/// `Map` uses the tag for garbage collection of old removed members, and to
/// resolve conflicting values for the same key and causal length.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Map<K, V, Tag, CL>
where
    K: Key + Ord,
    V: Value + Hash + Eq + Ord,
    Tag: TagT,
    CL: CausalLength,
{
    map: HashMap<K, Register<V, Tag, CL>>,
}

impl<K, V, Tag, CL> Map<K, V, Tag, CL>
where
    K: Key + Ord,
    V: Value + Hash + Eq + Ord,
    Tag: TagT,
    CL: CausalLength,
{
    /// Create an empty `Map`
    pub fn new() -> Map<K, V, Tag, CL> {
        Map {
            map: HashMap::new(),
        }
    }

    /// Returns a reference to the value and tag corresponding to the key.
    pub fn get<Q>(&self, key: Q) -> Option<(&V, Tag)>
    where
        Q: Borrow<K>,
    {
        if let Some(e) = self.map.get(key.borrow()) {
            if e.length.is_odd() {
                return Some((&e.item, e.tag));
            }
        }
        None
    }

    /// Returns true if the map contains a value for the specified key.
    pub fn contains<Q>(&self, key: Q) -> bool
    where
        Q: Borrow<K>,
    {
        self.get(key).is_some()
    }

    /// Inserts a key, value, and tag into the map.
    ///
    /// If the map did not have this key present, [`None`] is returned.
    ///
    /// If the map did have this key present, the value is updated, and the old
    /// value is returned, along with the old tag.
    pub fn insert(&mut self, key: K, value: V, tag: Tag) -> Option<(V, Tag)> {
        let one: CL = CL::one();
        let e = self.map.entry(key);
        match e {
            std::collections::hash_map::Entry::Occupied(mut oe) => {
                let oe = oe.get_mut();
                // s{e |-> s(e)+1} if even
                //s if odd s(e)
                if oe.length.is_even() {
                    oe.length = oe.length + one;
                } else if oe.item != value {
                    // Special adaptation for a map: we add two to the causal length
                    // in cases where the key exists, but the value is not the same.
                    // This is equivalent to removing and re-adding the key.
                    oe.length = oe.length + one + one;
                }
                // always use the max value of tag
                oe.tag = max(oe.tag, tag);
                let r = oe.item.clone();
                oe.item = value;
                Some((r, oe.tag))
            }
            _ => {
                e.or_insert_with(|| Register::make(value, tag, one));
                None
            }
        }
    }

    /// Remove a key from the map, returning the stored value and tag if
    /// the key was in the map.
    pub fn remove(&mut self, key: K, tag: Tag) -> Option<(V, Tag)> {
        let e = self.map.entry(key);
        match e {
            std::collections::hash_map::Entry::Occupied(mut oe) => {
                let oe = oe.get_mut();
                oe.tag = max(oe.tag, tag);

                // {} if even(s(e))
                // { e |-> s(e) + 1 } if odd(s(e))
                if oe.length.is_odd() {
                    oe.length = oe.length + CL::one();
                    Some((oe.item.clone(), oe.tag))
                } else {
                    None
                }
            }
            _ => None,
        }
        // ignore attempts to remove items that aren't present...
    }

    /// An iterator visiting all key, value, tag tuples in arbitrary order.
    pub fn iter(&self) -> impl Iterator<Item = (K, V, Tag)> + '_ {
        self.map
            .iter()
            .filter(|(_k, v)| v.length.is_odd())
            .map(|(k, v)| (k.clone(), v.item.clone(), v.tag))
    }

    /// An iterator visiting all delta registers in arbitrary order.
    pub fn register_iter(&self) -> impl Iterator<Item = Register<(K, V), Tag, CL>> + '_ {
        self.map
            .iter()
            .map(|(k, v)| Register::make((k.clone(), v.item.clone()), v.tag, v.length))
    }

    /// Merge a delta [Register] into a map.
    ///
    /// Remove deltas with a tag value less than `min_tag` will be ignored.
    pub fn merge_register(&mut self, delta: Register<(K, V), Tag, CL>, min_tag: Tag) {
        if delta.length.is_even() && delta.tag < min_tag {
            // ignore excessively old remove records
            return;
        }

        match self.map.entry(delta.item.0.clone()) {
            Entry::Occupied(mut e) => {
                let e = e.get_mut();

                let reg = Register::make(delta.item.1.clone(), delta.tag, delta.length);
                e.merge(&reg);
            }
            Entry::Vacant(e) => {
                e.insert(Register::make(delta.item.1, delta.tag, delta.length));
            }
        }
    }

    /// Merge two maps.
    ///
    /// Remove deltas with a tag value less than `min_tag` will be ignored.
    pub fn merge(&mut self, other: &Self, min_tag: Tag) {
        for delta in other.register_iter() {
            self.merge_register(delta, min_tag);
        }
    }

    /// Filter out old remove tombstone deltas from the map.
    ///
    /// Remove deltas with a tag value less than `min_tag` will be removed.
    pub fn retain(&mut self, min_tag: Tag) {
        self.map
            .retain(|_k, v| v.length.is_odd() || min_tag < v.tag);
    }
}

#[cfg(feature = "serialization")]
mod serialization {
    use super::*;
    use serde::de::{SeqAccess, Visitor};
    use serde::ser::SerializeSeq;
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::fmt::Formatter;
    use std::marker::PhantomData;

    impl<K, V, Tag, CL> Serialize for Map<K, V, Tag, CL>
    where
        K: Key + Ord + Serialize,
        V: Value + Hash + Ord + Serialize,
        Tag: TagT + Serialize,
        CL: CausalLength + Serialize,
    {
        fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            let mut seq = serializer.serialize_seq(Some(self.map.len()))?;
            for member in self.register_iter() {
                seq.serialize_element(&(member.item.0, member.item.1, member.tag, member.length))?;
            }
            seq.end()
        }
    }

    struct DeltaVisitor<K, V, Tag, CL>(
        PhantomData<K>,
        PhantomData<V>,
        PhantomData<Tag>,
        PhantomData<CL>,
    );

    impl<'de, K, V, Tag, CL> Visitor<'de> for DeltaVisitor<K, V, Tag, CL>
    where
        K: Key + Ord + Deserialize<'de>,
        V: Value + Hash + Ord + Deserialize<'de>,
        Tag: TagT + Deserialize<'de>,
        CL: CausalLength + Deserialize<'de>,
    {
        type Value = HashMap<K, Register<V, Tag, CL>>;

        fn expecting(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
            formatter.write_str("a tuple of key, value, tag, and causal length")
        }

        fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Self::Value, A::Error>
        where
            A: SeqAccess<'de>,
        {
            let mut map: HashMap<K, Register<V, Tag, CL>> =
                HashMap::with_capacity(seq.size_hint().unwrap_or(0));
            while let Some(d) = seq.next_element::<(K, V, Tag, CL)>()? {
                map.insert(d.0, Register::make(d.1, d.2, d.3));
            }
            Ok(map)
        }
    }

    impl<'de, K, V, Tag, CL> Deserialize<'de> for Map<K, V, Tag, CL>
    where
        K: Key + Ord + Deserialize<'de>,
        V: Value + Hash + Ord + Deserialize<'de>,
        Tag: TagT + Deserialize<'de>,
        CL: CausalLength + Deserialize<'de>,
    {
        fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            let visitor =
                DeltaVisitor::<K, V, Tag, CL>(PhantomData, PhantomData, PhantomData, PhantomData);
            let map = deserializer.deserialize_seq(visitor)?;

            Ok(Map { map })
        }
    }
}

impl<K, V, Tag, CL> From<Set<(K, V), Tag, CL>> for Map<K, V, Tag, CL>
where
    K: Key + Ord,
    V: Value + Hash + Eq + Ord,
    Tag: TagT,
    CL: CausalLength,
{
    fn from(s: Set<(K, V), Tag, CL>) -> Self {
        let mut m = Self::new();
        for item in s.register_iter() {
            m.merge_register(item, Tag::default());
        }
        m
    }
}

impl<K, V, Tag, CL> From<Map<K, V, Tag, CL>> for Set<(K, V), Tag, CL>
where
    K: Key + Ord,
    V: Value + Hash + Eq + Ord,
    Tag: TagT,
    CL: CausalLength,
{
    fn from(m: Map<K, V, Tag, CL>) -> Self {
        let mut s = Self::new();
        for item in m.register_iter() {
            s.merge_register(item, Tag::default());
        }
        s
    }
}

impl<K, V, Tag, CL> From<Map<K, V, Tag, CL>> for HashMap<K, (V, Tag)>
where
    K: Key + Ord,
    V: Value + Hash + Eq + Ord,
    Tag: TagT,
    CL: CausalLength,
{
    fn from(m: Map<K, V, Tag, CL>) -> Self {
        let mut h = Self::new();
        for item in m.register_iter() {
            if let Some(((k, v), tag)) = item.get() {
                h.insert(k.clone(), (v.clone(), tag));
            }
        }
        h
    }
}

#[cfg(feature = "serialization")]
pub use serialization::*;
use std::collections::hash_map::Entry;

#[cfg(test)]
mod tests {
    use super::*;
    use quickcheck_macros::quickcheck;
    use rand::seq::SliceRandom;

    #[test]
    fn test_add() {
        let later_time = 1;
        let mut cls: Map<&str, bool, u16, u16> = Map::new();

        cls.insert("foo", true, later_time);
        cls.insert("foo", true, later_time);
        cls.insert("foo", true, later_time);
        assert_eq!(cls.map.len(), 1);
        assert_eq!(
            cls.map.get("foo"),
            Some(&Register {
                item: true,
                tag: later_time,
                length: 1
            })
        );
        assert_eq!(cls.contains("foo"), true);
        assert_eq!(cls.get("bar"), None);
    }

    #[test]
    fn test_remove() {
        let time_1 = 1;
        let time_2 = 2;
        let time_3 = 3;
        let mut cls: Map<&str, bool, u32, u16> = Map::new();

        cls.insert("foo", true, time_1);
        cls.insert("bar", false, time_1);
        cls.remove("foo", time_2);
        cls.remove("bar", time_2);
        cls.insert("bar", true, time_3);
        // check map
        assert_eq!(cls.map.len(), 2);
        assert_eq!(
            cls.map.get("bar"),
            Some(&Register {
                item: true,
                tag: time_3,
                length: 3
            })
        );
        assert_eq!(
            cls.map.get("foo"),
            Some(&Register {
                item: true,
                tag: time_2,
                length: 2
            })
        );
        // check edges
        let values: Vec<(&str, bool, u32)> = cls.iter().collect();
        assert_eq!(values.len(), 1);
        assert_eq!(values[0], ("bar", true, time_3));
    }

    #[test]
    fn test_merge() {
        let time_0 = 0;
        let time_1 = 1;
        let time_2 = 2;
        let time_3 = 3;
        let mut cls1: Map<&str, u32, u32, u16> = Map::new();
        let mut cls2: Map<&str, u32, u32, u16> = Map::new();

        cls1.insert("foo", 128, time_1);
        cls1.insert("bar", 256, time_1);
        cls2.merge(&cls1, time_0);
        cls2.insert("foo", 128, time_2);
        cls1.remove("foo", time_2);
        cls1.remove("bar", time_2);
        cls2.merge(&cls1, time_0);
        cls2.insert("bar", 256, time_3);

        assert_eq!(cls2.map.len(), 2);
        assert_eq!(
            cls2.map.get(&"bar"),
            Some(&Register {
                item: 256,
                tag: time_3,
                length: 3
            })
        );
        assert_eq!(
            cls2.map.get(&"foo"),
            Some(&Register {
                item: 128,
                tag: time_2,
                length: 2
            })
        );

        let values: Vec<(&str, u32, u32)> = cls2.iter().collect();
        assert_eq!(values.len(), 1);
        assert_eq!(values[0], ("bar", 256, time_3));
    }

    #[test]
    fn test_retain() {
        let time_0 = 0;
        let time_1 = 1;
        let time_2 = 2;
        let time_3 = 3;
        let mut cls: Map<&str, u32, u32, u16> = Map::new();

        cls.insert("foo", 128, time_0);
        cls.insert("bar", 256, time_0);
        cls.remove("foo", time_1);
        cls.remove("bar", time_1);
        cls.insert("bar", 256, time_2);
        // check map
        assert_eq!(cls.map.len(), 2);
        assert_eq!(
            cls.map.get(&"bar"),
            Some(&Register {
                item: 256,
                tag: time_2,
                length: 3
            })
        );
        assert_eq!(
            cls.map.get(&"foo"),
            Some(&Register {
                item: 128,
                tag: time_1,
                length: 2
            })
        );
        // check edges
        let values: Vec<(&str, u32, u32)> = cls.iter().collect();
        assert_eq!(values.len(), 1);
        assert_eq!(values[0], ("bar", 256, time_2));
        // now clear old removes
        cls.retain(time_3);
        assert_eq!(cls.map.len(), 1);
        assert_eq!(
            cls.map.get(&"bar"),
            Some(&Register {
                item: 256,
                tag: time_2,
                length: 3
            })
        );
        // attempt to merge an out of date remove
        cls.merge_register(
            Register {
                item: ("bar", 512),
                tag: time_2,
                length: 2,
            },
            time_0,
        );
        assert_eq!(cls.map.len(), 1);
        assert_eq!(
            cls.map.get(&"bar"),
            Some(&Register {
                item: 256,
                tag: time_2,
                length: 3
            })
        );
    }

    #[test]
    fn test_overwrite() {
        let time_0 = 0;
        let time_1 = 1;
        let time_2 = 2;
        let mut cls: Map<&str, u32, u32, u16> = Map::new();

        cls.insert("bar", 256, time_0);
        cls.insert("bar", 256, time_1);
        // now try an overwrite
        cls.insert("bar", 512, time_2);
        assert_eq!(cls.map.len(), 1);
        assert_eq!(
            cls.map.get(&"bar"),
            Some(&Register {
                item: 512,
                tag: time_2,
                length: 3
            })
        );
    }

    #[cfg(feature = "serialization")]
    #[test]
    fn test_serialization() {
        let time_1 = 1;
        let time_2 = 2;
        let time_3 = 3;
        let mut m: Map<&str, bool, u32, u16> = Map::new();

        m.insert("foo", true, time_1);
        m.insert("bar", false, time_1);
        m.remove("foo", time_2);
        m.remove("bar", time_2);
        m.insert("bar", true, time_3);

        let data = serde_json::to_string(&m).unwrap_or("".to_owned());
        let cls2: Map<&str, bool, u32, u16> = serde_json::from_str(&data).unwrap();
        assert_eq!(m.map, cls2.map);
    }

    #[test]
    fn test_order_independence() {
        let mut m1: Map<&str, usize, u32, u16> = Map::new();
        let mut m2: Map<&str, usize, u32, u16> = Map::new();
        let mut v: Vec<Register<(&str, usize), u32, u16>> = vec![];

        for i in 0..1000 {
            v.push(Register {
                item: ("foo", i as usize),
                tag: i as u32,
                length: i as u16,
            });
        }

        // now randomize the updates
        v.shuffle(&mut rand::thread_rng());

        for r in v {
            m1.merge_register(r, 0);
        }
        assert_eq!(
            m1.map.get("foo"),
            Some(&Register {
                item: 999,
                tag: 999,
                length: 999
            })
        );

        let mut v: Vec<Register<(&str, usize), u32, u16>> = vec![];
        for i in 0..1000 {
            v.push(Register {
                item: ("foo", i as usize),
                tag: i as u32,
                length: i as u16,
            });
        }
        v.shuffle(&mut rand::thread_rng());
        for r in v {
            m2.merge_register(r, 0);
        }
        assert_eq!(m1, m2);
    }

    fn merge(mut acc: Map<u8, u8, u8, u8>, el: &Register<(u8, u8), u8, u8>) -> Map<u8, u8, u8, u8> {
        acc.merge_register(el.clone(), 0);
        acc
    }

    #[quickcheck]
    fn test_merge_commutative(xs: Vec<Register<(u8, u8), u8, u8>>) -> bool {
        let left: HashMap<u8, (u8, u8)> = xs.iter().fold(Map::default(), merge).into();
        let right: HashMap<u8, (u8, u8)> = xs.iter().rfold(Map::default(), merge).into();
        left == right
    }

    #[quickcheck]
    fn is_merge_order_independent(xs: Vec<Register<(u8, u8), u8, u8>>) -> bool {
        let mut copy = xs.clone();
        copy.shuffle(&mut rand::thread_rng());
        let left: HashMap<u8, (u8, u8)> = xs.iter().fold(Map::default(), merge).into();
        let right: HashMap<u8, (u8, u8)> = copy.iter().rfold(Map::default(), merge).into();
        left == right
    }

    mod simple_model {
        use super::*;
        use quickcheck::{Arbitrary, Gen};
        #[derive(Clone, Debug)]
        enum Op {
            Insert(u8, u8),
            Get(u8),
            Delete(u8),
        }

        const KEY_SPACE: u8 = 20;

        impl Arbitrary for Op {
            fn arbitrary(g: &mut Gen) -> Op {
                let k: u8 = u8::arbitrary(g) % KEY_SPACE;
                let v: u8 = u8::arbitrary(g);
                let n: u8 = u8::arbitrary(g) % 4;

                match n {
                    0 => Op::Insert(k, v),
                    1 => Op::Delete(k),
                    2 | 3 => Op::Get(k),
                    _ => Op::Get(k),
                }
            }
        }

        #[quickcheck]
        fn implementation_matches_model(ops: Vec<Op>) -> bool {
            let mut implementation: Map<u8, u8, u8, u8> = Map::new();
            let mut model = std::collections::HashMap::new();

            for op in ops {
                match op {
                    Op::Insert(k, v) => {
                        implementation.insert(k, v, 0);
                        model.insert(k, v);
                    }
                    Op::Get(k) => {
                        if implementation.get(&k).map(|i| i.0) != model.get(&k) {
                            return false;
                        }
                    }
                    Op::Delete(k) => {
                        implementation.remove(k, 0);
                        model.remove(&k);
                    }
                }
            }

            true
        }
    }
}