ditto 0.2.0

CRDTs for common data structures like maps, sets, vecs, strings, and JSON
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
//! A sequence Uid is an unique identifier that maintains
//! a total order of sequential elements in a distributed
//! environment.
//!
//! ## Implementation Notes
//!
//! New Uids are generated with a variant of the LSEQ
//! algorithm. It diverges from the original paper by
//! choosing allocation strategy deterministically rather
//! than according to a random coin flip. Randomized
//! allocation strategies cannot converge in a distributed
//! environment because replicas cannot reliably coordinate
//! strategies, and so Uid bit size increases quickly with
//! the number of sequence elements. However, a
//! deterministic allocation strategy known by all replicas
//! ahead of time is inherently coordinated, and Uid bit
//! size increases more slowly with the number of elements.

use base64;
use Error;
use num::bigint::{BigUint, ToBigUint};
use num::cast::ToPrimitive;
use rand::distributions::{IndependentSample, Range};
use rand;
use dot::{Dot, SiteId, Counter};
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use serde::de::{self, Visitor, SeqAccess};
use std::cmp::{self, Ordering};
use std::fmt::{self, Debug};
use std::str::FromStr;
use vlq;

const BASE_LEVEL: usize = 20;
const MAX_LEVEL:  usize = 64;
const BOUNDARY:   usize = 40;

#[derive(Clone,PartialEq,Eq)]
pub struct Uid {
    pub position: BigUint,
    pub site_id:  SiteId,
    pub counter:  Counter,
}

lazy_static! {
    pub static ref MIN: Uid = Uid::min();
    pub static ref MAX: Uid = Uid::max();
}

impl Uid {
    fn new(position: BigUint, site_id: u32, counter: u32) -> Self {
        Uid{position, site_id, counter}
    }

    pub fn dot(&self) -> Dot {
        Dot{site_id: self.site_id, counter: self.counter}
    }

    pub fn min() -> Self {
        Uid::new(big(1 << BASE_LEVEL), 0, 0)
    }

    pub fn max() -> Self {
        let position = big((1 << (BASE_LEVEL+1)) - 1);
        Uid::new(position, u32::max_value(), u32::max_value())
    }

    pub fn between(uid1: &Uid, uid2: &Uid, dot: Dot) -> Self {
        let position1            = &uid1.position;
        let position2            = &uid2.position;
        let mut position         = big(1);
        let mut significant_bits = 1;

        for level in BASE_LEVEL..(MAX_LEVEL+1) {
            significant_bits += level;
            let pos1 = Uid::get_pos(position1, level, significant_bits).unwrap_or(0);
            let pos2 = Uid::get_pos(position2, level, significant_bits).unwrap_or((1 << level) - 1);

            if pos1 + 1 < pos2 {
                let pos = Uid::generate_pos(pos1, pos2, level);
                position = (position << level) + big(pos);
                return Uid::new(position, dot.site_id, dot.counter);
            } else {
                position = (position << level) + big(pos1);
            }
        }
        panic!(format!("Uid cannot have more than ({}) levels", MAX_LEVEL));
    }

    /// Gets the value for a particular level in a position
    /// if the position has a value for that level. A position's
    /// most significant bit is not part of any level (it
    /// is a placeholder to prevent the highest level from being
    /// truncated). The next 3 most significant bits form level 3,
    /// the next 4 most significant bits form level 4; and so on.
    fn get_pos(position: &BigUint, level: usize, significant_bits: usize) -> Option<usize> {
        let bits = position.bits();
        if bits >= significant_bits {
            let insignificant_bits = bits - significant_bits;
            let level_mask = big((1 << level) - 1);
            let pos = (position >> insignificant_bits) & level_mask;
            Some(pos.to_usize().unwrap())
        } else {
            None
        }
    }

    /// Generates a number that falls between pos1 and pos2.
    /// It requires pos1 + 1 < pos2. Uses boundary+ strategy
    /// on odd levels and boundary+ strategy on even levels.
    ///
    /// boundary+ returns an integer in the interval
    /// [pos1+1, min(pos1+BOUNDARY, pos2-1)]
    ///
    /// boundary- returns an integer in the interval
    /// [max(pos1+1, pos2-BOUNDARY), pos2-1]
    ///
    fn generate_pos(pos1: usize, pos2: usize, level: usize) -> usize {
        let range =
            if Uid::use_boundary_plus_strategy(level) {
                let lo_bound = pos1+1;
                let hi_bound = cmp::min(pos1+BOUNDARY, pos2);
                Range::new(lo_bound, hi_bound)
            } else if pos2 <= BOUNDARY {
                let lo_bound = pos1+1;
                let hi_bound = pos2;
                Range::new(lo_bound, hi_bound)
            } else {
                let lo_bound = cmp::max(pos1+1, pos2-BOUNDARY);
                let hi_bound = pos2;
                Range::new(lo_bound, hi_bound)
            };
        let mut rng = rand::thread_rng();
        range.ind_sample(&mut rng)
    }

    // TODO: Use Boundary- for arrays on odd levels.
    // In a Text crdt, Boundary- doesn't make any sense
    // because no one inserts text backwards. On an array,
    // it might make more sense because array ops are more
    // likely to be truly random access.
    fn use_boundary_plus_strategy(_: usize) -> bool {
        true
    }

    fn to_vlq(&self) -> Vec<u8> {
        let mut vlq = vlq::encode_biguint(&self.position);
        vlq.append(&mut vlq::encode_u32(self.site_id));
        vlq.append(&mut vlq::encode_u32(self.counter));
        vlq

    }

    fn from_vlq(vlq: &[u8]) -> Result<Self, Error> {
        let (position, vlq_rest1) = vlq::decode_biguint(vlq)?;
        let (site_id, vlq_rest2) = vlq::decode_u32(vlq_rest1)?;
        let (counter, _) = vlq::decode_u32(vlq_rest2)?;
        Ok(Uid{position, site_id, counter})
    }
}

fn big(num: usize) -> BigUint {
    num.to_biguint().unwrap()
}

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

impl Ord for Uid {
    fn cmp(&self, other: &Uid) -> Ordering {
        let mut self_position = self.position.clone();
        let mut other_position = other.position.clone();

        let self_bits = self_position.bits();
        let other_bits = other_position.bits();

        // truncate
        if self_bits > other_bits {
            self_position >>= self_bits - other_bits;
        } else {
            other_position >>= other_bits - self_bits;
        }

        // compare
        if self_position < other_position {
            Ordering::Less
        } else if self_position > other_position {
            Ordering::Greater
        } else if self_bits < other_bits {
            Ordering::Less
        } else if self_bits > other_bits {
            Ordering::Greater
        } else if self.site_id < other.site_id {
            Ordering::Less
        } else if self.site_id > other.site_id {
            Ordering::Greater
        } else if self.counter < other.counter {
            Ordering::Less
        } else if self.counter > other.counter {
            Ordering::Greater
        } else {
            Ordering::Equal
        }
    }
}

impl Debug for Uid {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let uid = self.position.to_str_radix(2);
        write!(f, "Uid{{position: {}, site_id: {}, counter: {}}}", uid, self.site_id, self.counter)
    }
}

impl ToString for Uid {
    fn to_string(&self) -> String {
        base64::encode_config(&self.to_vlq(), base64::URL_SAFE_NO_PAD)
    }
}

impl FromStr for Uid {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let vlq = base64::decode_config(s, base64::URL_SAFE_NO_PAD).map_err(|_| Error::DeserializeSequenceUid)?;
        Uid::from_vlq(&vlq)
    }
}

impl Serialize for Uid {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
        serializer.serialize_bytes(&self.to_vlq())
    }
}

impl<'de> Deserialize<'de> for Uid {
    fn deserialize<D>(deserializer: D) -> Result<Uid, D::Error> where D: Deserializer<'de> {
        struct UidVisitor;

        impl<'de> Visitor<'de> for UidVisitor {
            type Value = Uid;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a byte buffer")
            }

            fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> where V: SeqAccess<'de> {
                let mut vec = Vec::with_capacity(visitor.size_hint().unwrap_or(0));
                while let Some(byte) = visitor.next_element()? { vec.push(byte); }
                Ok(Uid::from_vlq(&vec).map_err(|_| de::Error::missing_field("invalid VLQ value"))?)
            }

            fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> where E: de::Error {
                Ok(Uid::from_vlq(v).map_err(|_| de::Error::missing_field("invalid VLQ value"))?)
            }
        }

        deserializer.deserialize_any(UidVisitor)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use num::bigint::{BigUint, ToBigUint};
    use std::str::FromStr;
    use serde_json;
    use rmp_serde;

    const DOT: Dot = Dot{site_id: 3, counter: 2};

    #[test]
    fn test_min() {
        let uid = Uid::min();
        assert!(uid.position == big(0b1_00000000000000000000));
        assert!(uid.site_id == 0);
        assert!(uid.counter == 0);
    }

    #[test]
    fn test_max() {
        let uid = Uid::max();
        assert!(uid.position == big(0b1_11111111111111111111));
        assert!(uid.site_id == 4294967295);
        assert!(uid.counter == 4294967295);
    }

    #[test]
    fn test_ord() {
        let uid0 = Uid::min();
        let uid1 = Uid{position: big(0b1_00000000000000101001), site_id: 8, counter: 382};
        let uid2 = Uid{position: big(0b1_00000000000101010010), site_id: 1, counter: 5};
        let uid3 = Uid{position: big(0b1_00000000000101010010), site_id: 1, counter: 5};
        let uid4 = Uid{position: big(0b1_00000000001011110010), site_id: 4, counter: 4};
        let uid5 = Uid{position: big(0b1_00000000001011111101), site_id: 4, counter: 4};
        let uid6 = Uid::max();

        let mut uids: Vec<&Uid> = vec![&uid4, &uid1, &uid5, &uid6, &uid0, &uid2, &uid3];
        uids.sort();

        assert!(uids[0] == &uid0);
        assert!(uids[1] == &uid1);
        assert!(uids[2] == &uid2);
        assert!(uids[3] == &uid2);
        assert!(uids[4] == &uid4);
        assert!(uids[5] == &uid5);
        assert!(uids[6] == &uid6);
    }

    #[test]
    fn test_between_trivial() {
        let uid1 = Uid::min();
        let uid2 = Uid::max();
        let uid  = Uid::between(&uid1, &uid2, DOT);

        assert!(big(0b1_00000000000000000000) < uid.position);
        assert!(big(0b1_11111111111111111111) > uid.position);
        assert!(uid.site_id == 3);
        assert!(uid.counter == 2);
    }

    #[test]
    fn test_between_basic() {
        let uid1 = Uid{position: big(0b1_01111111111111111110), site_id: 1, counter: 1};
        let uid2 = Uid{position: big(0b1_10000000000000000000), site_id: 1, counter: 1};
        let uid  = Uid::between(&uid1, &uid2, DOT);
        assert!(uid.position == big(0b1_01111111111111111111));
    }

    #[test]
    fn test_between_multi_level() {
        let uid1 = Uid{position: big(0b1_11111000000000000000), site_id: 1, counter: 1};
        let uid2 = Uid{position: big(0b1_11111000000000000001), site_id: 1, counter: 1};
        let uid  = Uid::between(&uid1, &uid2, DOT);
        assert!(uid.position > big(0b1_11111000000000000000_000000000000000000000));
        assert!(uid.position < big(0b1_11111000000000000000_000000000000000101001));
    }

    #[test]
    fn test_between_squeeze() {
        let uid1 = Uid{position: big(0b1_11111000000000000000_001101010010101010101_1010101010101010101010), site_id: 1, counter: 1};
        let uid2 = Uid{position: big(0b1_11111000000000000000_001101010010101010111_1010101010101010101010), site_id: 1, counter: 1};
        let uid  = Uid::between(&uid1, &uid2, DOT);
        assert!(uid.position == big(0b1_11111000000000000000_001101010010101010110));
    }

    #[test]
    fn test_between_equals() {
        let uid1 = Uid{position: big(0b1_00110011100000000010), site_id: 1, counter: 1};
        let uid2 = Uid{position: big(0b1_00110011100000000010), site_id: 2, counter: 1};
        let uid  = Uid::between(&uid1, &uid2, DOT);
        assert!(uid.position > big(0b1_00110011100000000010_000000000000000000000));
        assert!(uid.position < big(0b1_00110011100000000010_000000000000000101001));
    }

    #[test]
    fn test_between_first_is_shorter() {
        let uid1 = Uid{position: big(0b1_11111000000000000000), site_id: 1, counter: 1};
        let uid2 = Uid{position: big(0b1_11111000000000000000_001101010010101010101), site_id: 2, counter: 1};
        let uid  = Uid::between(&uid1, &uid2, DOT);
        assert!(uid.position > big(0b1_11111000000000000000_000000000000000000000));
        assert!(uid.position < big(0b1_11111000000000000000_000000000000000101001));
    }

    #[test]
    fn test_between_first_is_longer() {
        let uid1 = Uid{position: big(0b1_11111000000000000000_001101010010101010110), site_id: 1, counter: 1};
        let uid2 = Uid{position: big(0b1_11111000000000000000), site_id: 2, counter: 1};
        let uid  = Uid::between(&uid1, &uid2, DOT);
        assert!(uid.position > big(0b1_11111000000000000000_001101010010101010110));
        assert!(uid.position < big(0b1_11111000000000000000_001101010010101111111));
    }

    #[test]
    fn test_to_from_string() {
        let uid = Uid{position: big(0b1_010_1010), site_id: 4, counter: 83};
        let serialized = uid.to_string();
        let deserialized = Uid::from_str(&serialized).unwrap();
        assert!(serialized == "gSoEUw");
        assert!(deserialized == uid);
    }

    #[test]
    fn test_serialize() {
        let uid1 = Uid{position: big(0b1_010_1010_01101_100110_1011011_10111011_011001101), site_id: 491, counter: 82035};
        let s_json = serde_json::to_string(&uid1).unwrap();
        let s_msgpack = rmp_serde::to_vec(&uid1).unwrap();
        let uid2: Uid = serde_json::from_str(&s_json).unwrap();
        let uid3: Uid = rmp_serde::from_slice(&s_msgpack).unwrap();
        assert!(uid1 == uid2);
        assert!(uid1 == uid3);
    }

    #[test]
    fn test_serialize_deserialize_invalid() {
        let serialized = "bjad%%";
        let deserialized = Uid::from_str(&serialized);
        assert!(deserialized.is_err());
    }

    fn big(num: usize) -> BigUint {
        num.to_biguint().unwrap()
    }
}