plane_controller/ttl_store/
ttl_multistore.rs

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
use super::{ttl_list::TtlList, ttl_map::TtlMap};
use std::{
    hash::Hash,
    time::{Duration, SystemTime},
};

pub struct TtlMultistore<K: Hash + Eq + Clone, V> {
    inner: TtlMap<K, TtlList<V>>,
    ttl: Duration,
}

impl<K: Hash + Eq + Clone, V> TtlMultistore<K, V> {
    pub fn new(ttl: Duration) -> Self {
        TtlMultistore {
            ttl,
            inner: TtlMap::new(ttl),
        }
    }

    pub fn insert(&mut self, key: K, value: V, time: SystemTime) {
        let list = self
            .inner
            .get_or_insert_with(key, || TtlList::new(self.ttl));
        list.push(value, time);
    }

    pub fn iter(&mut self, key: &K, time: SystemTime) -> Option<impl Iterator<Item = &V>> {
        if let Some(v) = self.inner.get_mut(key, time) {
            Some(v.iter(time))
        } else {
            None
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::ttl_store::test::ts;

    #[test]
    fn test_multistore() {
        let mut store: TtlMultistore<u32, u32> = TtlMultistore::new(Duration::from_secs(10));

        store.insert(4, 10, ts(200));
        store.insert(4, 11, ts(201));
        store.insert(4, 12, ts(202));

        store.insert(5, 100, ts(205));
        store.insert(5, 110, ts(206));
        store.insert(5, 120, ts(207));

        let vals: Vec<u32> = store.iter(&4, ts(208)).unwrap().cloned().collect();
        assert_eq!(vec![10, 11, 12], vals);

        let vals: Vec<u32> = store.iter(&4, ts(211)).unwrap().cloned().collect();
        assert_eq!(vec![12], vals);

        let vals: Vec<u32> = store.iter(&5, ts(211)).unwrap().cloned().collect();
        assert_eq!(vec![100, 110, 120], vals);

        let vals: Vec<u32> = store.iter(&4, ts(212)).unwrap().cloned().collect();
        assert!(vals.is_empty());

        let vals: Vec<u32> = store.iter(&5, ts(215)).unwrap().cloned().collect();
        assert_eq!(vec![110, 120], vals);

        let vals: Vec<u32> = store.iter(&5, ts(216)).unwrap().cloned().collect();
        assert_eq!(vec![120], vals);

        let vals: Vec<u32> = store.iter(&5, ts(217)).unwrap().cloned().collect();
        assert!(vals.is_empty());
    }
}