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
pub(crate) mod bloom_filter;
pub(crate) mod cache;
pub(crate) mod matecito_internal;
use std::sync::Arc;
pub struct Matecito<K, T>(Arc<cache::Cache<K, T>>);
impl<K: Clone + Ord + std::hash::Hash, T: std::fmt::Debug + Clone> Matecito<K, T> {
pub fn new(num_elements: usize) -> Self {
let put_threshold = 1000;
Self(Arc::new(cache::Cache::new(num_elements, put_threshold)))
}
pub fn put(&self, key: K, value: T) {
self.0.put(key, value)
}
pub fn get(&self, key: K) -> Option<T> {
self.0.get(key)
}
}
impl<K, T> Clone for Matecito<K, T> {
fn clone(&self) -> Self {
let cache = self.0.clone();
Self(cache)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple() {
let m = Matecito::<u64, String>::new(2usize.pow(10));
let m1 = m.clone();
std::thread::spawn(move || {
m1.put(123, "asd".to_string());
m1.put(01010101, "321".to_string());
});
std::thread::sleep(std::time::Duration::from_millis(1));
let m2 = m.clone();
let result = std::thread::spawn(move || m2.get(123)).join();
assert_eq!(Some("asd".to_string()), result.unwrap());
let m3 = m.clone();
let result = std::thread::spawn(move || m3.get(01010101)).join();
assert_eq!(Some("321".to_string()), result.unwrap());
let m4 = m.clone();
let result = std::thread::spawn(move || m4.get(0xf00)).join();
assert_eq!(None, result.unwrap());
}
}