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
#![doc(html_logo_url = "http://maidsafe.net/img/Resources/branding/maidsafe_logo.fab2.png",
html_favicon_url = "http://maidsafe.net/img/favicon.ico",
html_root_url = "http://dirvine.github.io/accumulator")]
#![deny(missing_docs)]
extern crate lru_time_cache;
use lru_time_cache::LruCache;
#[derive(Clone)]
pub struct Entry<V> {
pub received_response: Vec<V>,
}
pub struct Accumulator<K, V> where K: PartialOrd + Ord + Clone, V: Clone {
quorum: usize,
storage: LruCache<K, Entry<V>>
}
impl<K: PartialOrd + Ord + Clone, V: Clone> Accumulator<K, V> {
pub fn new(quorum: usize) -> Accumulator<K, V> {
Accumulator { quorum: quorum, storage: LruCache::<K, Entry<V>>::with_capacity(1000) }
}
pub fn have_name(&mut self, name: &K) -> bool {
self.storage.get(name).is_some()
}
pub fn is_quorum_reached(&mut self, name: &K) -> bool {
let entry = self.storage.get(name);
if entry.is_none() {
false
} else {
entry.unwrap().received_response.len() >= self.quorum
}
}
pub fn add(&mut self, name: K, value: V)-> Option<(K, Vec<V>)> {
let entry = self.storage.remove(&name);
if entry.is_none() {
let entry_in = Entry { received_response : vec![value]};
self.storage.add(name.clone(), entry_in.clone());
if self.quorum == 1 {
let result = (name, entry_in.received_response);
return Some(result);
}
} else {
let mut tmp = entry.unwrap();
tmp.received_response.push(value);
self.storage.add(name.clone(), tmp.clone());
if tmp.received_response.len() >= self.quorum {
return Some((name, tmp.received_response));
}
}
None
}
pub fn get(&mut self, name: &K) -> Option<(K, Vec<V>)>{
let entry = self.storage.get(name);
if entry.is_none() {
None
} else {
Some((name.clone(), entry.unwrap().received_response.clone()))
}
}
pub fn delete(&mut self, name: &K) {
self.storage.remove(name);
}
pub fn cache_size(&mut self) -> usize {
self.storage.len()
}
}
#[cfg(test)]
mod test {
extern crate rand;
use super::*;
#[test]
fn add() {
let mut accumulator : Accumulator<i32, u32> = Accumulator::new(1);
assert!(accumulator.add(2, 3).is_some());
assert_eq!(accumulator.have_name(&1), false);
assert_eq!(accumulator.have_name(&2), true);
assert_eq!(accumulator.is_quorum_reached(&1), false);
assert_eq!(accumulator.is_quorum_reached(&2), true);
assert!(accumulator.add(1, 3).is_some());
assert_eq!(accumulator.have_name(&1), true);
assert_eq!(accumulator.is_quorum_reached(&1), true);
assert!(accumulator.add(1, 3).is_some());
assert_eq!(accumulator.have_name(&1), true);
assert_eq!(accumulator.is_quorum_reached(&1), true);
let (key, responses) = accumulator.get(&1).unwrap();
assert_eq!(key, 1);
assert_eq!(responses.len(), 2);
assert_eq!(responses[0], 3);
assert_eq!(responses[1], 3);
let (key, responses) = accumulator.get(&2).unwrap();
assert_eq!(key, 2);
assert_eq!(responses.len(), 1);
assert_eq!(responses[0], 3);
}
#[test]
fn add_single_value_quorum() {
let quorum_size : usize = 19;
let mut accumulator : Accumulator<i32, u32> = Accumulator::new(quorum_size);
let key = rand::random::<i32>();
let value = rand::random::<u32>();
for i in 0..quorum_size-1 {
assert!(accumulator.add(key, value).is_none());
let key_value = accumulator.get(&key).unwrap();
assert_eq!(key_value.0, key);
assert_eq!(key_value.1.len(), i + 1);
for response in key_value.1 { assert_eq!(response, value); };
assert_eq!(accumulator.is_quorum_reached(&key), false);
}
assert!(accumulator.add(key, value).is_some());
assert_eq!(accumulator.is_quorum_reached(&key), true);
let key_value = accumulator.get(&key).unwrap();
assert_eq!(key_value.0, key);
assert_eq!(key_value.1.len(), quorum_size);
for response in key_value.1 { assert_eq!(response, value); };
}
#[test]
fn add_multiple_values_quorum() {
let quorum_size : usize = 19;
let mut accumulator : Accumulator<i32, u32> = Accumulator::new(quorum_size);
let key = rand::random::<i32>();
for _ in 0..quorum_size-1 {
assert!(accumulator.add(key, rand::random::<u32>()).is_none());
assert_eq!(accumulator.is_quorum_reached(&key), false);
}
assert!(accumulator.add(key, rand::random::<u32>()).is_some());
assert_eq!(accumulator.is_quorum_reached(&key), true);
}
#[test]
fn add_multiple_keys_quorum() {
let quorum_size : usize = 19;
let mut accumulator : Accumulator<i32, u32> = Accumulator::new(quorum_size);
let key = rand::random::<i32>();
let mut noise_keys : Vec<i32> = Vec::with_capacity(5);
while noise_keys.len() < 5 {
let noise_key = rand::random::<i32>();
if noise_key != key { noise_keys.push(noise_key); }; };
for _ in 0..quorum_size-1 {
for noise_key in noise_keys.iter() {
accumulator.add(noise_key.clone(), rand::random::<u32>());
}
assert!(accumulator.add(key, rand::random::<u32>()).is_none());
assert_eq!(accumulator.is_quorum_reached(&key), false);
}
assert!(accumulator.add(key, rand::random::<u32>()).is_some());
assert_eq!(accumulator.is_quorum_reached(&key), true);
}
#[test]
fn delete() {
let mut accumulator : Accumulator<i32, u32> = Accumulator::new(2);
assert!(accumulator.add(1, 1).is_none());
assert_eq!(accumulator.have_name(&1), true);
assert_eq!(accumulator.is_quorum_reached(&1), false);
let (key, responses) = accumulator.get(&1).unwrap();
assert_eq!(key, 1);
assert_eq!(responses.len(), 1);
assert_eq!(responses[0], 1);
accumulator.delete(&1);
let option = accumulator.get(&1);
assert!(option.is_none());
assert!(accumulator.add(1, 1).is_none());
assert_eq!(accumulator.have_name(&1), true);
assert_eq!(accumulator.is_quorum_reached(&1), false);
assert!(accumulator.add(1, 1).is_some());
assert_eq!(accumulator.have_name(&1), true);
assert_eq!(accumulator.is_quorum_reached(&1), true);
let (key, responses) = accumulator.get(&1).unwrap();
assert_eq!(key, 1);
assert_eq!(responses.len(), 2);
assert_eq!(responses[0], 1);
assert_eq!(responses[1], 1);
accumulator.delete(&1);
let option = accumulator.get(&1);
assert!(option.is_none());
}
#[test]
fn fill() {
let mut accumulator : Accumulator<i32, u32> = Accumulator::new(1);
for count in 0..1000 {
assert!(accumulator.add(count, 1).is_some());
assert_eq!(accumulator.have_name(&count), true);
assert_eq!(accumulator.is_quorum_reached(&count), true);
}
for count in 0..1000 {
let (key, responses) = accumulator.get(&count).unwrap();
assert_eq!(key, count);
assert_eq!(responses.len(), 1);
assert_eq!(responses[0], 1);
}
}
#[test]
fn cache_removals() {
let mut accumulator : Accumulator<i32, u32> = Accumulator::new(2);
for count in 0..1000 {
assert!(accumulator.add(count, 1).is_none());
assert_eq!(accumulator.have_name(&count), true);
assert_eq!(accumulator.is_quorum_reached(&count), false);
let (key, responses) = accumulator.get(&count).unwrap();
assert_eq!(key, count);
assert_eq!(responses.len(), 1);
assert_eq!(responses[0], 1);
assert_eq!(accumulator.cache_size(), count as usize + 1);
}
assert!(accumulator.add(1000, 1).is_none());
assert_eq!(accumulator.have_name(&1000), true);
assert_eq!(accumulator.is_quorum_reached(&1000), false);
assert_eq!(accumulator.cache_size(), 1000);
for count in 0..1000 {
let option = accumulator.get(&count);
assert!(option.is_none());
assert!(accumulator.add(count + 1001, 1).is_none());
assert_eq!(accumulator.have_name(&(count + 1001)), true);
assert_eq!(accumulator.is_quorum_reached(&(count + 1001)), false);
assert_eq!(accumulator.cache_size(), 1000);
}
}
}