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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use std::{
borrow::{Borrow, BorrowMut},
io::{Read, Write},
};
use crate::{
associated_data::AssociatedData,
config::{CuckooConfiguration, LruConfig, TtlConfig},
data_block::{DataBlock, DataBlockFieldConfiguration, Fingerprint},
};
/// A single bucket in the filter, holding all the fingerprints and their associated data
/// Everything is stored as a single [`Vec<u8>`], where each fingerprint together with its
/// associated data is aligned to [`u8`].
pub(crate) struct Bucket {
data: Vec<u8>,
}
impl Bucket {
/// Creates a new bucket based on [`CuckooConfiguration`].
///
/// ## Panics
///
/// Panics on OOM errors (if the requested bucket byte size is too high).
pub(crate) fn new(configuration: &CuckooConfiguration) -> Self {
Self {
data: vec![0; configuration.bucket_byte_size],
}
}
/// Creates a new bucket based on exported state found in the provided reader.
///
/// ## Panics
///
/// Panics on OOM errors (if the requested bucket byte size is too high).
pub(crate) fn take_from(
mut reader: impl Read,
configuration: &CuckooConfiguration,
) -> std::io::Result<Self> {
let mut data = vec![0; configuration.bucket_byte_size];
reader.read_exact(&mut data)?;
Ok(Self { data })
}
/// Inserts a new fingerprint into this bucket.
///
/// Sets TTL to the default value (if enabled), increments both LRU and generic counters by 1
/// (if enabled). This is done when the same fingerprint is inserted again, restarting TTL and
/// increasing counters.
///
/// Returns false if the insertion has failed (if the bucket is fully occupied). In that case,
/// alternate bucket should be tried and if that fails too, kicking process should be started.
pub(crate) fn insert<T: Borrow<[u8]>>(
&mut self,
data_block: &DataBlock<T>,
configuration: &CuckooConfiguration,
) -> bool {
let fingerprint = data_block.get_fingerprint(configuration);
for i in 0..configuration.bucket_size {
let mut data = self.get_data_block(i, configuration);
let stored = data.get_fingerprint(configuration);
let reinsert = stored == fingerprint;
if !reinsert {
if stored.is_empty() {
data.copy_from(data_block);
} else {
continue;
}
} else {
data.merge_associated_from(data_block, configuration);
}
return true;
}
false
}
/// Kicks a random item from this bucket, by exchaging that [`DataBlock`] with the one
/// provided.
///
/// This doesn't return. It always succeeds and the kicked item can be found in the provided
/// [`DataBlock`].
pub(crate) fn kick_random<T: BorrowMut<[u8]>>(
&mut self,
data_block: &mut DataBlock<T>,
configuration: &CuckooConfiguration,
) {
let index = rand::random_range(0..configuration.bucket_size);
self.get_data_block(index, configuration).swap(data_block);
}
/// Kicks an item from this bucket, based on LRU - kicks out the lowest LRU counter item from
/// this bucket, that has lower LRU counter than the new item. If the new item has the lowest
/// LRU counter, kick fails and false is returned. For completely new items insertion is guaranteed.
///
/// Returns true if any item was kicked. Returns false if no item was kicked and the new item
/// was not moved out of [`DataBlock`].
pub(crate) fn kick_lru<T: BorrowMut<[u8]>>(
&mut self,
data_block: &mut DataBlock<T>,
configuration: &CuckooConfiguration,
lru_config: &(LruConfig, DataBlockFieldConfiguration),
new_item: bool,
) -> bool {
let mut min = data_block.get_lru_counter(lru_config);
if new_item {
min = u32::MAX;
}
let mut pos = configuration.bucket_size;
for i in 0..configuration.bucket_size {
let data = self.get_data_block(i, configuration);
let counter = data.get_lru_counter(lru_config);
if counter < min {
min = counter;
pos = i;
}
}
if pos < configuration.bucket_size {
self.get_data_block(pos, configuration).swap(data_block);
true
} else {
false
}
}
/// Looks for the fingerprint in this bucket.
///
/// If fingerprint is found, its LRU and generic counters are also incremented (if enabled).
///
/// Returns true if the fingeprint is stored in this bucket.
pub(crate) fn contains(
&mut self,
fingerprint: &Fingerprint,
configuration: &CuckooConfiguration,
update: &LookupValues,
) -> bool {
for i in 0..configuration.bucket_size {
let mut data = self.get_data_block(i, configuration);
let stored = data.get_fingerprint(configuration);
if stored == *fingerprint {
if let Some(counter_config) = configuration.counter_field_config.as_ref() {
data.update_counter(
counter_config,
update
.counter_diff
.unwrap_or(counter_config.0.change_on_lookup),
);
}
if let Some(ttl_config) = configuration.ttl_field_config.as_ref()
&& let Some(ttl) = update.ttl
{
data.set_ttl(ttl_config, ttl);
}
if let Some(lru_config) = configuration.lru_field_config.as_ref() {
data.inc_lru_counter(lru_config);
}
return true;
}
}
false
}
/// Looks for the fingerprint in this bucket and returns its associated data.
///
/// Returns the data associated with the fingerprint, if found.
pub(crate) fn get_associated_data(
&mut self,
fingerprint: &Fingerprint,
configuration: &CuckooConfiguration,
update: &LookupValues,
) -> Option<AssociatedData> {
for i in 0..configuration.bucket_size {
let mut data = self.get_data_block(i, configuration);
let stored = data.get_fingerprint(configuration);
if stored == *fingerprint {
if let Some(counter_config) = configuration.counter_field_config.as_ref() {
data.update_counter(
counter_config,
update
.counter_diff
.unwrap_or(counter_config.0.change_on_lookup),
);
}
if let Some(ttl_config) = configuration.ttl_field_config.as_ref()
&& let Some(ttl) = update.ttl
{
data.set_ttl(ttl_config, ttl);
}
if let Some(lru_config) = configuration.lru_field_config.as_ref() {
data.inc_lru_counter(lru_config);
}
return Some(AssociatedData::new(
self.get_data_block(i, configuration),
configuration.clone(),
));
}
}
None
}
/// Removes the fingerprint from this bucket, by clearing out its slot.
///
/// Returns true if fingerprint was found and removed, false if it was not found.
pub(crate) fn remove(
&mut self,
fingerprint: &Fingerprint,
configuration: &CuckooConfiguration,
) -> bool {
for i in 0..configuration.bucket_size {
let mut data = self.get_data_block(i, configuration);
let stored = data.get_fingerprint(configuration);
if stored == *fingerprint {
data.reset();
return true;
}
}
false
}
/// Ages all LRU counters in this bucket.
pub(crate) fn age_lru_counters(
&mut self,
configuration: &CuckooConfiguration,
lru_config: &(LruConfig, DataBlockFieldConfiguration),
) -> usize {
let mut removed = 0;
for i in 0..configuration.bucket_size {
let db = self.get_data_block(i, configuration);
if db.occupied(configuration) {
removed += if self
.get_data_block(i, configuration)
.age_lru_counter(lru_config)
{
1
} else {
0
}
}
}
removed
}
/// Ages all TTL counters in this bucket.
///
/// Returns the number of removed items after aging.
pub(crate) fn age_ttl_counters(
&mut self,
configuration: &CuckooConfiguration,
ttl_config: &(TtlConfig, DataBlockFieldConfiguration),
) -> usize {
let mut removed = 0;
for i in 0..configuration.bucket_size {
let db = self.get_data_block(i, configuration);
if db.occupied(configuration) {
removed += if self
.get_data_block(i, configuration)
.age_ttl_counter(ttl_config)
{
1
} else {
0
}
}
}
removed
}
pub(crate) fn occupied_count(&self, configuration: &CuckooConfiguration) -> usize {
(0..configuration.bucket_size)
.map(|i| {
let size = configuration.data_block_size;
DataBlock::from(&self.data[(i * size)..((i + 1) * size)])
})
.filter(|db| db.occupied(configuration))
.count()
}
pub(crate) fn export(&self, mut writer: impl Write) -> std::io::Result<()> {
writer.write_all(&self.data)
}
fn get_data_block(
&mut self,
index: usize,
configuration: &CuckooConfiguration,
) -> DataBlock<&mut [u8]> {
let size = configuration.data_block_size;
(&mut self.data[(index * size)..((index + 1) * size)]).into()
}
}
/// Values to store with the fingerprint on insertion.
#[derive(Default)]
pub struct InsertValues {
/// TTL to set for the fingerprint on insertion.
/// This is ignored if the TTL configuration is not enabled.
pub ttl: Option<u32>,
/// Counter to set to the fingerprint on insertion.
/// This is ignored if the counter configuration is not enabled.
pub counter: Option<i32>,
}
/// Values to store with the fingerprint on lookups.
#[derive(Default)]
pub struct LookupValues {
/// TTL to set for the fingerprint on lookup.
/// This is ignored if the TTL configuration is not enabled.
pub ttl: Option<u32>,
/// Counter diff to apply (add/remove) to the fingerprint on lookups.
/// This is ignored if the counter configuration is not enabled.
pub counter_diff: Option<i32>,
}