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
#![warn(missing_docs)]
use std::mem;
use std::cmp;
use std::hash::Hash;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::ops::{Deref, DerefMut};
use std::time::{SystemTime, UNIX_EPOCH};
type LifetimeSec = u32;
pub trait Timer {
fn get_time(&self) -> i64;
}
#[derive(Default)]
pub struct StandardTimer;
impl Timer for StandardTimer {
fn get_time(&self) -> i64 {
match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(d) => d.as_secs() as i64,
Err(err) => -(err.duration().as_secs() as i64)
}
}
}
pub struct TransientHashMap<K, V, T = StandardTimer> where T: Timer {
backing: HashMap<K, V>,
timestamps: HashMap<K, i64>,
lifetime: LifetimeSec,
timer: T
}
impl<K, V> TransientHashMap<K, V, StandardTimer> where K: Eq + Hash + Clone {
pub fn new(lifetime: LifetimeSec) -> Self {
TransientHashMap::new_with_timer(lifetime, Default::default())
}
}
impl<K, V, T> TransientHashMap<K, V, T> where K: Eq + Hash + Clone, T: Timer {
pub fn new_with_timer(lifetime: LifetimeSec, t: T) -> Self {
TransientHashMap {
backing: HashMap::new(),
timestamps: HashMap::new(),
lifetime: lifetime,
timer: t
}
}
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
self.note_used_if(true, &key);
self.backing.insert(key, value)
}
pub fn entry(&mut self, key: K) -> Entry<K, V> {
self.note_used_if(true, &key);
self.backing.entry(key)
}
pub fn get(&mut self, key: &K) -> Option<&V> {
let has_key = self.backing.contains_key(key);
self.note_used_if(has_key, key);
self.backing.get(key)
}
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
self.contains_key(key);
self.backing.get_mut(key)
}
pub fn contains_key(&mut self, key: &K) -> bool {
let contains = self.backing.contains_key(key);
self.note_used_if(contains, key);
contains
}
pub fn remove(&mut self, k: &K) -> Option<V> {
self.timestamps.remove(k);
self.backing.remove(k)
}
pub fn remaining_lifetime(&mut self, key: &K) -> Option<LifetimeSec> {
self.timestamps.get(key).map(|time| {
let time = self.timer.get_time() - time;
cmp::max(0, self.lifetime as i64 - time) as LifetimeSec
})
}
#[inline]
fn note_used_if(&mut self, condition: bool, key: &K) {
if condition {
self.timestamps.insert(key.clone(), self.timer.get_time());
}
}
pub fn prune(&mut self) -> Vec<K> {
let now = self.timer.get_time();
let timestamps = mem::replace(&mut self.timestamps, HashMap::new());
let (ok, removed) = timestamps.into_iter()
.partition(|entry| now - entry.1 < self.lifetime as i64);
self.timestamps = ok;
removed
.into_iter()
.map(|entry| {
self.backing.remove(&entry.0);
entry.0
})
.collect()
}
pub fn direct(&self) -> &HashMap<K, V> {
&self.backing
}
pub fn direct_mut(&mut self) -> &mut HashMap<K, V> {
&mut self.backing
}
}
impl<K, V, T> Deref for TransientHashMap<K, V, T> where T: Timer {
type Target = HashMap<K, V>;
fn deref(&self) -> &Self::Target {
&self.backing
}
}
impl<K, V, T> DerefMut for TransientHashMap<K, V, T> where T: Timer {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.backing
}
}
#[cfg(test)]
mod test {
use std::cell::Cell;
use super::{TransientHashMap, Timer};
struct TestTimer<'a> {
time: &'a Cell<i64>
}
impl<'a> Timer for TestTimer<'a> {
fn get_time(&self) -> i64 {
self.time.get()
}
}
#[test]
fn should_remove_lifetime_when_calling_remove() {
let time = Cell::new(0);
let timer = TestTimer {
time: &time
};
let mut t_map: TransientHashMap<u64, (), _> = TransientHashMap::new_with_timer(2, timer);
t_map.insert(2, ());
assert_eq!(t_map.remaining_lifetime(&2), Some(2));
t_map.remove(&2);
assert_eq!(t_map.remaining_lifetime(&2), None);
}
#[test]
fn should_not_track_lifetime_if_key_is_not_present() {
let time = Cell::new(0);
let timer = TestTimer {
time: &time
};
let mut t_map: TransientHashMap<u64, (), _> = TransientHashMap::new_with_timer(2, timer);
t_map.contains_key(&2);
assert_eq!(t_map.remaining_lifetime(&2), None);
}
#[test]
fn should_return_correct_lifetime_when_negative() {
let time = Cell::new(0);
let timer = TestTimer {
time: &time
};
let mut t_map = TransientHashMap::new_with_timer(2, timer);
t_map.insert(1, 0);
time.set(10);
assert_eq!(t_map.remaining_lifetime(&1), Some(0));
}
#[test]
fn should_return_pruned_keys() {
let time = Cell::new(0);
let timer = TestTimer {
time: &time
};
let mut t_map = TransientHashMap::new_with_timer(2, timer);
t_map.insert(1, 0);
t_map.insert(2, 0);
t_map.insert(3, 0);
time.set(1);
t_map.insert(4, 0);
assert_eq!(t_map.direct().len(), 4);
time.set(2);
let keys = t_map.prune();
assert_eq!(t_map.direct().len(), 1);
assert_eq!(keys.len(), 3);
assert!(keys.contains(&1));
assert!(keys.contains(&2));
assert!(keys.contains(&3));
}
#[test]
fn it_works() {
let time = Cell::new(0);
let timer = TestTimer {
time: &time
};
let mut t_map = TransientHashMap::new_with_timer(2, timer);
assert_eq!(t_map.remaining_lifetime(&1), None);
t_map.insert(1, 1);
assert_eq!(t_map.remaining_lifetime(&1), Some(2));
time.set(1);
assert_eq!(t_map.remaining_lifetime(&1), Some(1));
time.set(2);
assert_eq!(t_map.remaining_lifetime(&1), Some(0));
time.set(1);
assert_eq!(t_map.remaining_lifetime(&1), Some(1));
t_map.prune();
assert_eq!(t_map.remaining_lifetime(&1), Some(1));
time.set(2);
assert_eq!(t_map.remaining_lifetime(&1), Some(0));
t_map.prune();
assert_eq!(t_map.remaining_lifetime(&1), None);
time.set(1);
assert_eq!(t_map.remaining_lifetime(&1), None);
}
}