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
use super::non_standard_slice::{NonStandard, NonStandardSlice};
use super::skiplist::SkipList;
use agilulf_protocol::Slice;
use std::ops::RangeBounds;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;

#[derive(Clone)]
struct Item<T: Default + Clone> {
    pub key: NonStandardSlice,
    pub value: T,
    pub serial_number: u64,
}

impl<T: Default + Clone> PartialOrd for Item<T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        if self.key < other.key {
            Some(std::cmp::Ordering::Less)
        } else if self.key > other.key {
            Some(std::cmp::Ordering::Greater)
        } else if self.serial_number < other.serial_number {
            Some(std::cmp::Ordering::Greater)
        } else if self.serial_number > other.serial_number {
            Some(std::cmp::Ordering::Less)
        } else {
            Some(std::cmp::Ordering::Equal)
        }
    }
}

impl<T: Default + Clone> NonStandard for Item<T> {
    fn min() -> Self {
        Item {
            key: NonStandardSlice::MIN,
            value: T::default(),
            serial_number: std::u64::MAX,
        }
    }

    fn max() -> Self {
        Item {
            key: NonStandardSlice::MAX,
            value: T::default(),
            serial_number: std::u64::MIN,
        }
    }
}

impl<T: Default + Clone> PartialEq for Item<T> {
    fn eq(&self, other: &Self) -> bool {
        self.partial_cmp(other) == Some(std::cmp::Ordering::Equal)
    }
}

/// A map contains a skiplist and a serial_number.
///
/// ```ignore
/// pub struct SkipMap<T: Default + Clone> {
///     skiplist: SkipList<Item<T>>,
///     serial_number: AtomicU64,
/// }
/// ```
///
/// `serial_number` is automatically increased. It's used to keep the order of insert: The later it
/// is inserted, the smaller it is. (Actually the `serial_number` is bigger but the item is smaller
/// according to the strategy of comparing item.
///
/// Generic parameter `T` should be `Default + Clone`. However the limitation can be relaxed to only
/// `Default`. The `Clone` here is to avoid lifetime parameter and keep this crate simple. (And
/// what we need to use for T is actually `Clone`). If we remove the `Clone` limitation, the `insert`
/// method may need to receive a `T` but not `&T`
pub struct SkipMap<T: Default + Clone> {
    skiplist: SkipList<Item<T>>,
    serial_number: AtomicU64,
}

impl<T: Default + Clone> Default for SkipMap<T> {
    fn default() -> Self {
        Self::new(0)
    }
}

impl<T: Default + Clone> SkipMap<T> {
    fn new(serial_number: u64) -> SkipMap<T> {
        SkipMap {
            skiplist: SkipList::default(),
            serial_number: AtomicU64::new(serial_number),
        }
    }

    pub fn len(&self) -> u64 {
        self.serial_number.load(Ordering::SeqCst)
    }

    ///```
    /// # use agilulf_skiplist::SkipMap;
    /// # use agilulf_protocol::Slice;
    /// let map: SkipMap<Slice> = SkipMap::default();
    /// map.insert(&Slice(b"key1".to_vec()), &Slice(b"value1".to_vec()));
    /// map.insert(&Slice(b"key2".to_vec()), &Slice(b"value2".to_vec()));
    /// map.insert(&Slice(b"key3".to_vec()), &Slice(b"value3".to_vec()));
    ///
    /// assert_eq!(
    ///     map.find(&Slice(b"key1".to_vec())).unwrap(),
    ///     Slice(b"value1".to_vec())
    /// );
    ///
    /// map.insert(
    ///     &Slice(b"key1".to_vec()),
    ///     &Slice(b"modified_value1".to_vec()),
    /// );
    /// assert_eq!(
    ///     map.find(&Slice(b"key1".to_vec())).unwrap(),
    ///     Slice(b"modified_value1".to_vec())
    /// );
    ///```
    pub fn insert(&self, key: &Slice, value: &T) {
        let new_item = Item {
            key: NonStandardSlice::Slice(key.clone()),
            value: value.clone(),
            serial_number: self.serial_number.fetch_add(1, Ordering::SeqCst),
        };

        self.skiplist.insert(&new_item);
    }

    pub fn find(&self, key: &Slice) -> Option<T> {
        let new_item = Item {
            key: NonStandardSlice::Slice(key.clone()),
            value: T::default(),
            serial_number: std::u64::MAX,
        };

        let item = self.skiplist.read_key(&new_item);
        match &item.key {
            NonStandardSlice::Slice(slice) => {
                if slice == key {
                    Some(item.value.clone())
                } else {
                    None
                }
            }
            _ => None,
        }
    }

    pub fn scan<R>(&self, range: R) -> Vec<(Slice, T)>
    where
        R: RangeBounds<Slice>,
    {
        use std::ops::Bound;

        let start_item = match range.start_bound() {
            Bound::Included(bound) => Item {
                key: NonStandardSlice::Slice(bound.clone()),
                value: T::default(),
                serial_number: std::u64::MAX,
            },
            Bound::Excluded(bound) => Item {
                key: NonStandardSlice::Slice(bound.clone()),
                value: T::default(),
                serial_number: std::u64::MIN,
            },
            Bound::Unbounded => Item::min(),
        };
        let end_item = match range.end_bound() {
            Bound::Included(bound) => Item {
                key: NonStandardSlice::Slice(bound.clone()),
                value: T::default(),
                serial_number: std::u64::MIN,
            },
            Bound::Excluded(bound) => Item {
                key: NonStandardSlice::Slice(bound.clone()),
                value: T::default(),
                serial_number: std::u64::MAX,
            },
            Bound::Unbounded => Item::max(),
        };
        let data = self
            .skiplist
            .scan(&start_item, &end_item)
            .into_iter()
            .map(|item| (&item.key, &item.value, &item.serial_number));

        let mut ret = Vec::new();
        let mut last_key = &NonStandardSlice::MIN;
        for (key, value, _) in data {
            if last_key != key {
                ret.push((key.clone().unwrap(), value.clone()));
                last_key = key;
            } else {
                continue;
            }
        }

        ret
    }
}

#[cfg(test)]
mod tests {
    use super::SkipMap;
    use agilulf_protocol::Slice;
    use rand::distributions::Standard;
    use rand::{thread_rng, Rng};
    use std::collections::BTreeMap;
    use std::thread;

    fn generate_keys(num: usize) -> Vec<Vec<u8>> {
        (0..num)
            .map(|_| thread_rng().sample_iter(&Standard).take(8).collect())
            .collect()
    }

    fn generate_values(num: usize) -> Vec<Vec<u8>> {
        (0..num)
            .map(|_| thread_rng().sample_iter(&Standard).take(256).collect())
            .collect()
    }

    #[test]
    fn simple_put_get_test() {
        let map: SkipMap<Slice> = SkipMap::new(0);
        map.insert(&Slice(b"key1".to_vec()), &Slice(b"value1".to_vec()));
        map.insert(&Slice(b"key2".to_vec()), &Slice(b"value2".to_vec()));
        map.insert(&Slice(b"key3".to_vec()), &Slice(b"value3".to_vec()));

        assert_eq!(
            map.find(&Slice(b"key1".to_vec())).unwrap(),
            Slice(b"value1".to_vec())
        );

        map.insert(
            &Slice(b"key1".to_vec()),
            &Slice(b"modified_value1".to_vec()),
        );
        assert_eq!(
            map.find(&Slice(b"key1".to_vec())).unwrap(),
            Slice(b"modified_value1".to_vec())
        );
    }

    #[test]
    fn simple_map_test() {
        let map: SkipMap<Slice> = SkipMap::new(0);

        let keys: Vec<Slice> = generate_keys(1000)
            .into_iter()
            .map(|key| Slice(key))
            .collect();
        let values: Vec<Slice> = generate_values(1000)
            .into_iter()
            .map(|key| Slice(key))
            .collect();

        for i in 0..1000 {
            map.insert(&keys[i], &values[i])
        }

        for i in 0..1000 {
            let value = map.find(&keys[i]).unwrap();
            assert_eq!(value, values[i]);
        }
    }

    #[test]
    fn multi_thread_test() {
        use std::sync::Arc;

        let map: Arc<SkipMap<Slice>> = Arc::new(SkipMap::new(0));

        let map_ref = &map;
        (0..4)
            .map(move |_| {
                let map = map_ref.clone();
                thread::spawn(move || {
                    let keys: Vec<Slice> = generate_keys(1000)
                        .into_iter()
                        .map(|key| Slice(key))
                        .collect();
                    let values: Vec<Slice> = generate_values(1000)
                        .into_iter()
                        .map(|key| Slice(key))
                        .collect();

                    for i in 0..1000 {
                        map.insert(&keys[i], &values[i])
                    }

                    for i in 0..1000 {
                        let value = map.find(&keys[i]).unwrap();
                        assert_eq!(value, values[i]);
                    }
                })
            })
            .for_each(|thread| thread.join().unwrap());
    }

    #[test]
    fn scan() {
        let map: SkipMap<Slice> = SkipMap::new(0);
        let mut btree_map = BTreeMap::new();

        let keys: Vec<Slice> = generate_keys(1000)
            .into_iter()
            .map(|key| Slice(key))
            .collect();
        let values: Vec<Slice> = generate_values(1000)
            .into_iter()
            .map(|key| Slice(key))
            .collect();

        for i in 0..1000 {
            map.insert(&keys[i], &values[i]);
            btree_map.insert(keys[i].clone(), values[i].clone());
        }

        for _ in 0..5 {
            let start = rand::thread_rng().gen_range(0, 1000);
            let end = rand::thread_rng().gen_range(start, 1000);

            let range_start = std::cmp::min(keys[start].clone(), keys[end].clone());
            let range_end = std::cmp::max(keys[start].clone(), keys[end].clone());

            let kv_pair: Vec<(Slice, Slice)> = map.scan(range_start.clone()..range_end.clone());
            let kv_pair_exp: Vec<(Slice, Slice)> = btree_map
                .range(range_start.clone()..range_end.clone())
                .into_iter()
                .map(|(key, value)| (key.clone(), value.clone()))
                .collect();

            assert_eq!(kv_pair.len(), kv_pair_exp.len());
            for i in 0..kv_pair.len() {
                assert_eq!(kv_pair[i], kv_pair_exp[i]);
            }
        }
    }

    #[test]
    fn update_test() {
        let map: SkipMap<Slice> = SkipMap::default();
        map.insert(&Slice(b"key1".to_vec()), &Slice(b"value1".to_vec()));
        map.insert(&Slice(b"key2".to_vec()), &Slice(b"value2".to_vec()));
        map.insert(&Slice(b"key3".to_vec()), &Slice(b"value3".to_vec()));

        let scan_result = map.scan(Slice(b"key1".to_vec())..Slice(b"key3".to_vec()));
        assert_eq!(
            scan_result,
            vec![
                (Slice(b"key1".to_vec()), Slice(b"value1".to_vec())),
                (Slice(b"key2".to_vec()), Slice(b"value2".to_vec()))
            ]
        );

        map.insert(
            &Slice(b"key1".to_vec()),
            &Slice(b"modified_value1".to_vec()),
        );
        let scan_result = map.scan(Slice(b"key1".to_vec())..Slice(b"key3".to_vec()));
        assert_eq!(
            scan_result,
            vec![
                (Slice(b"key1".to_vec()), Slice(b"modified_value1".to_vec())),
                (Slice(b"key2".to_vec()), Slice(b"value2".to_vec()))
            ]
        );
    }
}