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
//! HashMap with entries living for limited period of time.

extern crate time;

use std::mem;
use std::cmp;
use std::hash::Hash;
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::ops::{Deref, DerefMut};

/// Time provider.
pub trait Timer {
	/// Returns current timestamp in seconds.
	fn get_time(&self) -> i64;
}

#[derive(Default)]
pub struct StandardTimer;

impl Timer for StandardTimer {
	fn get_time(&self) -> i64 {
		time::get_time().sec
	}
}

pub struct TransientHashMap<K, V, T = StandardTimer> where T: Timer {
	backing: HashMap<K, V>,
	timestamps: RefCell<HashMap<K, i64>>,
	lifetime: u64,
	timer: T
}

impl<K, V> TransientHashMap<K, V, StandardTimer> where K: Eq + Hash {
	pub fn new(lifetime: u64) -> Self {
		TransientHashMap::new_with_timer(lifetime, Default::default())
	}
}

impl<K, V, T> TransientHashMap<K, V, T> where K: Eq + Hash, T: Timer {
	pub fn new_with_timer(lifetime: u64, t: T) -> Self {
		TransientHashMap {
			backing: HashMap::new(),
			timestamps: RefCell::new(HashMap::new()),
			lifetime: lifetime,
			timer: t
		}
	}

	pub fn insert(&mut self, key: K, value: V) -> Option<V> where K: Clone {
		self.note_used(key.clone());
		self.backing.insert(key, value)
	}

	pub fn entry(&mut self, key: K) -> Entry<K, V> where K: Clone {
		self.note_used(key.clone());
		self.backing.entry(key)
	}

	pub fn get(&self, key: &K) -> Option<&V> where K: Clone {
		self.note_used(key.clone());
		self.backing.get(key)
	}

	pub fn get_mut(&mut self, key: &K) -> Option<&mut V> where K: Clone {
		self.note_used(key.clone());
		self.backing.get_mut(key)
	}

	pub fn contains_key(&self, key: &K) -> bool where K: Clone {
		self.note_used(key.clone());
		self.backing.contains_key(key)
	}

	pub fn remaining_lifetime(&self, key: &K) -> u64 {
		let timestamps = self.timestamps.borrow();
		match timestamps.get(key) {
			None => 0,
			Some(time) => cmp::max(0, self.lifetime - (self.timer.get_time() - time) as u64)
		}
	}

	fn note_used(&self, key: K) {
		self.timestamps.borrow_mut().insert(key, self.timer.get_time());
	}

	pub fn prune(&mut self) {
		let now = self.timer.get_time();
		let timestamps = mem::replace(&mut self.timestamps, RefCell::new(HashMap::new()));
		*self.timestamps.borrow_mut() = timestamps.into_inner().into_iter()
			.filter(|entry| ((now - entry.1) as u64) < self.lifetime)
			.collect();

		let backing = mem::replace(&mut self.backing, HashMap::new());
		let timestamps = self.timestamps.borrow();
		self.backing = backing.into_iter()
			.filter(|entry| timestamps.contains_key(&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::RefCell;
	use super::{TransientHashMap, Timer};

	struct TestTimer<'a> {
		time: &'a RefCell<i64>
	}

	impl<'a> Timer for TestTimer<'a> {
		fn get_time(&self) -> i64 {
			*self.time.borrow()
		}
	}

    #[test]
    fn it_works() {
		let time = RefCell::new(0);
		let timer = TestTimer {
			time: &time
		};
	
		let mut t_map = TransientHashMap::new_with_timer(2, timer);
		assert_eq!(t_map.remaining_lifetime(&1), 0);

		t_map.insert(1, 1);
		assert_eq!(t_map.remaining_lifetime(&1), 2);

		*time.borrow_mut() = 1;
		assert_eq!(t_map.remaining_lifetime(&1), 1);

		*time.borrow_mut() = 2;
		assert_eq!(t_map.remaining_lifetime(&1), 0);

		*time.borrow_mut() = 1;
		assert_eq!(t_map.remaining_lifetime(&1), 1);

		t_map.prune();
		assert_eq!(t_map.remaining_lifetime(&1), 1);

		*time.borrow_mut() = 2;
		assert_eq!(t_map.remaining_lifetime(&1), 0);

		t_map.prune();
		assert_eq!(t_map.remaining_lifetime(&1), 0);

		*time.borrow_mut() = 1;
		assert_eq!(t_map.remaining_lifetime(&1), 0);
    }
}