-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcuckoo_commands.rs
477 lines (454 loc) · 17.9 KB
/
cuckoo_commands.rs
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use crate::{
client::{prepare_command, PreparedCommand},
resp::{
cmd, deserialize_byte_buf, CollectionResponse, CommandArgs, SingleArg, SingleArgCollection,
ToArgs, Value,
},
};
use serde::Deserialize;
use std::collections::HashMap;
/// A group of Redis commands related to [`Cuckoo filters`](https://redis.io/docs/stack/bloom/)
///
/// # See Also
/// [Cuckoo Filter Commands](https://redis.io/commands/?group=cf)
pub trait CuckooCommands<'a> {
/// Adds an item to the cuckoo filter, creating the filter if it does not exist.
///
/// Cuckoo filters can contain the same item multiple times, and consider each insert as separate.
/// You can use [`cf_addnx`](CuckooCommands::cf_addnx) to only add the item if it does not exist yet.
/// Keep in mind that deleting an element inserted using [`cf_addnx`](CuckooCommands::cf_addnx) may cause false-negative errors.
///
/// # Arguments
/// * `key` - The name of the filter
/// * `item` - The item to add
///
/// # See Also
/// * [<https://redis.io/commands/cf.add/>](https://redis.io/commands/cf.add/)
#[must_use]
fn cf_add(self, key: impl SingleArg, item: impl SingleArg) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(self, cmd("CF.ADD").arg(key).arg(item))
}
/// Adds an item to a cuckoo filter if the item did not exist previously.
///
/// See documentation on [`cf_add`](CuckooCommands::cf_add) for more information on this command.
///
/// This command is equivalent to a [`cf_exists`](CuckooCommands::cf_exists) + [`cf_add`](CuckooCommands::cf_add) command.
/// It does not insert an element into the filter if its fingerprint already exists in order to use the available capacity more efficiently.
/// However, deleting elements can introduce `false negative` error rate!
///
/// Note that this command is slower than [`cf_add`](CuckooCommands::cf_add) because it first checks whether the item exists.
///
/// # Arguments
/// * `key` - The name of the filter
/// * `item` - The item to add
///
/// # Return
/// * `true` - if the item did not exist in the filter.
/// * `false` - if the item already existed.
///
/// # See Also
/// * [<https://redis.io/commands/cf.addnx/>](https://redis.io/commands/cf.addnx/)
#[must_use]
fn cf_addnx(self, key: impl SingleArg, item: impl SingleArg) -> PreparedCommand<'a, Self, bool>
where
Self: Sized,
{
prepare_command(self, cmd("CF.ADDNX").arg(key).arg(item))
}
/// Returns the number of times an item may be in the filter.
///
/// Because this is a probabilistic data structure, this may not necessarily be accurate.
///
/// If you just want to know if an item exists in the filter,
/// use [`cf_exists`](CuckooCommands::cf_exists) because it is more efficient for that purpose.
///
/// # Arguments
/// * `key` - The name of the filter
/// * `item` - The item to count
///
/// # Return
/// the count of possible matching copies of the item in the filter.
///
/// # See Also
/// * [<https://redis.io/commands/cf.count/>](https://redis.io/commands/cf.count/)
#[must_use]
fn cf_count(self, key: impl SingleArg, item: impl SingleArg) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
{
prepare_command(self, cmd("CF.COUNT").arg(key).arg(item))
}
/// Deletes an item once from the filter.
///
/// If the item exists only once, it will be removed from the filter.
/// If the item was added multiple times, it will still be present.
///
/// # Danger !
/// Deleting elements that are not in the filter may delete a different item, resulting in false negatives!
///
/// # Arguments
/// * `key` - The name of the filter
/// * `item` - The item to delete from the filter
///
/// # Complexity
/// O(n), where n is the number of `sub-filters`. Both alternative locations are checked on all `sub-filters`.
///
/// # Return
/// * `true` - the item has been deleted from the filter.
/// * `false` - if the item was not found.
///
/// # See Also
/// * [<https://redis.io/commands/cf.del/>](https://redis.io/commands/cf.del/)
#[must_use]
fn cf_del(self, key: impl SingleArg, item: impl SingleArg) -> PreparedCommand<'a, Self, bool>
where
Self: Sized,
{
prepare_command(self, cmd("CF.DEL").arg(key).arg(item))
}
/// Check if an `item` exists in a Cuckoo Filter `key`
///
/// # Arguments
/// * `key` - The name of the filter
/// * `item` - The item to check for
///
/// # Return
/// * `true` - the item may exist in the filter
/// * `false` - if the item does not exist in the filter.
///
/// # See Also
/// * [<https://redis.io/commands/cf.exists/>](https://redis.io/commands/cf.exists/)
#[must_use]
fn cf_exists(self, key: impl SingleArg, item: impl SingleArg) -> PreparedCommand<'a, Self, bool>
where
Self: Sized,
{
prepare_command(self, cmd("CF.EXISTS").arg(key).arg(item))
}
/// Return information about `key`
///
/// # Arguments
/// * `key` - Name of the key to get info about
///
/// # Return
/// An instance of [`CfInfoResult`](CfInfoResult)
///
/// # See Also
/// * [<https://redis.io/commands/cf.info/>](https://redis.io/commands/cf.info/)
#[must_use]
fn cf_info(self, key: impl SingleArg) -> PreparedCommand<'a, Self, CfInfoResult>
where
Self: Sized,
{
prepare_command(self, cmd("CF.INFO").arg(key))
}
/// Adds one or more items to a cuckoo filter, allowing the filter to be created with a custom capacity if it does not exist yet.
///
/// These commands offers more flexibility over the [`cf_add`](CuckooCommands::cf_add) command, at the cost of more verbosity.
///
/// # Arguments
/// * `key` - The name of the filter
/// * `options` - see [`CfInsertOptions`](CfInsertOptions)
/// * `items` - One or more items to add.
///
/// # See Also
/// * [<https://redis.io/commands/cf.insert/>](https://redis.io/commands/cf.insert/)
#[must_use]
fn cf_insert<I: SingleArg>(
self,
key: impl SingleArg,
options: CfInsertOptions,
item: impl SingleArgCollection<I>,
) -> PreparedCommand<'a, Self, Vec<usize>>
where
Self: Sized,
{
prepare_command(
self,
cmd("CF.INSERT")
.arg(key)
.arg(options)
.arg("ITEMS")
.arg(item),
)
}
/// Adds one or more items to a cuckoo filter, allowing the filter to be created with a custom capacity if it does not exist yet.
///
/// This command is equivalent to a [`cf_exists`](CuckooCommands::cf_exists) + [`cf_add`](CuckooCommands::cf_add) command.
/// It does not insert an element into the filter if its fingerprint already exists and therefore better utilizes the available capacity.
/// However, if you delete elements it might introduce `false negative` error rate!
///
/// These commands offers more flexibility over the [`cf_add`](CuckooCommands::cf_add) and [`cf_addnx`](CuckooCommands::cf_addnx) commands,
/// at the cost of more verbosity.
///
/// # Complexity
/// `O(n + i)`, where n is the number of `sub-filters` and `i` is `maxIterations`.
/// Adding items requires up to 2 memory accesses per `sub-filter`.
/// But as the filter fills up, both locations for an item might be full.
/// The filter attempts to `Cuckoo` swap items up to maxIterations times.
///
/// # Arguments
/// * `key` - The name of the filter
/// * `options` - see [`CfInsertOptions`](CfInsertOptions)
/// * `items` - One or more items to add.
///
/// # Return
/// A collection of integers corresponding to the items specified. Possible values for each element are:
/// * `>0` - if the item was successfully inserted
/// * `0` - if the item already existed and [`cf_insertnx`](CuckooCommands::cf_insertnx) is used.
/// * `<0` - if an error occurred
///
/// # See Also
/// * [<https://redis.io/commands/cf.insert/>](https://redis.io/commands/cf.insert/)
#[must_use]
fn cf_insertnx<I: SingleArg, R: CollectionResponse<i64>>(
self,
key: impl SingleArg,
options: CfInsertOptions,
item: impl SingleArgCollection<I>,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
{
prepare_command(
self,
cmd("CF.INSERTNX")
.arg(key)
.arg(options)
.arg("ITEMS")
.arg(item),
)
}
/// Restores a filter previously saved using [`cf_scandump`](CuckooCommands::cf_scandump).
///
/// See the [`cf_scandump`](CuckooCommands::cf_scandump) command for example usage.
///
/// This command overwrites any bloom filter stored under `key`.
/// Make sure that the bloom filter is not be changed between invocations.
///
/// # Arguments
/// * `key` - Name of the key to restore
/// * `iterator` - Iterator value associated with `data` (returned by [`cf_scandump`](CuckooCommands::cf_scandump))
/// * `data` - Current data chunk (returned by [`cf_scandump`](CuckooCommands::cf_scandump))
///
/// # See Also
/// [<https://redis.io/commands/cf.loadchunk/>](https://redis.io/commands/cf.loadchunk/)
#[must_use]
fn cf_loadchunk(
self,
key: impl SingleArg,
iterator: i64,
data: impl SingleArg,
) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(self, cmd("CF.LOADCHUNK").arg(key).arg(iterator).arg(data))
}
/// Check if one or more `items` exists in a Cuckoo Filter `key`
///
/// # Arguments
/// * `key` - The name of the filter
/// * `items` - One or more items to check for
///
/// # Return
/// Collection reply of boolean - for each item where `true` value means the corresponding item
/// may exist in the filter, and a `false` value means it does not exist in the filter.
///
/// # See Also
/// [<https://redis.io/commands/cf.mexists/>](https://redis.io/commands/cf.mexists/)
#[must_use]
fn cf_mexists<I: SingleArg, R: CollectionResponse<bool>>(
self,
key: impl SingleArg,
items: impl SingleArgCollection<I>,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
{
prepare_command(self, cmd("CF.MEXISTS").arg(key).arg(items))
}
/// Create a Cuckoo Filter as `key` with a single sub-filter for the initial amount of `capacity` for items.
/// Because of how Cuckoo Filters work, the filter is likely to declare itself full before `capacity` is reached
/// and therefore fill rate will likely never reach 100%.
/// The fill rate can be improved by using a larger `bucketsize` at the cost of a higher error rate.
/// When the filter self-declare itself `full`, it will auto-expand by generating additional sub-filters at the cost of reduced performance and increased error rate.
/// The new sub-filter is created with size of the previous sub-filter multiplied by `expansion`.
/// Like bucket size, additional sub-filters grow the error rate linearly.
/// The size of the new sub-filter is the size of the last sub-filter multiplied by `expansion`.
///
/// The minimal false positive error rate is 2/255 ≈ 0.78% when bucket size of 1 is used.
/// Larger buckets increase the error rate linearly (for example, a bucket size of 3 yields a 2.35% error rate) but improve the fill rate of the filter.
///
/// `maxiterations` dictates the number of attempts to find a slot for the incoming fingerprint.
/// Once the filter gets full, high `maxIterations` value will slow down insertions.
///
/// Unused capacity in prior sub-filters is automatically used when possible. The filter can grow up to 32 times.
///
/// # Arguments
/// * `key` - The key under which the filter is found
/// * `capacity` - Estimated capacity for the filter.
/// Capacity is rounded to the next 2^n number.
/// The filter will likely not fill up to 100% of it's capacity.
/// Make sure to reserve extra capacity if you want to avoid expansions.
/// * `options` - See [`CfReserveOptions`](CfReserveOptions)
///
/// # See Also
/// [<https://redis.io/commands/cf.reserve/>](https://redis.io/commands/cf.reserve/)
#[must_use]
fn cf_reserve(
self,
key: impl SingleArg,
capacity: usize,
options: CfReserveOptions,
) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(self, cmd("CF.RESERVE").arg(key).arg(capacity).arg(options))
}
/// Begins an incremental save of the cuckoo filter.
/// This is useful for large cuckoo filters which cannot fit into the normal [`dump`](crate::commands::GenericCommands::dump)
/// and [`restore`](crate::commands::GenericCommands::restore) model.
///
/// # Arguments
/// * `key` - Name of the filter
/// * `iterator` - Iterator value; either 0 or the iterator from a previous invocation of this command.\
/// The first time this command is called, the value of `iterator` should be 0.
///
/// # Return
/// This command returns successive `(iterator, data)` pairs until `(0, vec![])` to indicate completion.
///
/// # See Also
/// [<https://redis.io/commands/cf.scandump/>](https://redis.io/commands/cf.scandump/)
#[must_use]
fn cf_scandump(
self,
key: impl SingleArg,
iterator: i64,
) -> PreparedCommand<'a, Self, CfScanDumpResult>
where
Self: Sized,
{
prepare_command(self, cmd("CF.SCANDUMP").arg(key).arg(iterator))
}
}
/// Result for the [`cf_info`](CuckooCommands::cf_info) command.
#[derive(Debug, Deserialize)]
pub struct CfInfoResult {
/// Size
#[serde(rename = "Size")]
pub size: usize,
/// Number of buckets
#[serde(rename = "Number of buckets")]
pub num_buckets: usize,
/// Number of filters
#[serde(rename = "Number of filters")]
pub num_filters: usize,
/// Number of items inserted
#[serde(rename = "Number of items inserted")]
pub num_items_inserted: usize,
/// Number of items deleted
#[serde(rename = "Number of items deleted")]
pub num_items_deleted: usize,
/// Bucket size
#[serde(rename = "Bucket size")]
pub bucket_size: usize,
/// Expansion rate
#[serde(rename = "Expansion rate")]
pub expansion_rate: usize,
/// Max iteration
#[serde(rename = "Max iterations")]
pub max_iteration: usize,
/// Additional information
#[serde(flatten)]
pub additional_info: HashMap<String, Value>,
}
/// Options for the [`cf_insert`](CuckooCommands::cf_insert) command.
#[derive(Default)]
pub struct CfInsertOptions {
command_args: CommandArgs,
}
impl CfInsertOptions {
/// Specifies the desired capacity of the new filter, if this filter does not exist yet.
///
/// If the filter already exists, then this parameter is ignored.
/// If the filter does not exist yet and this parameter is not specified,
/// then the filter is created with the module-level default capacity which is `1024`.
/// See [`cf_reserve`](CuckooCommands::cf_reserve) for more information on cuckoo filter capacities.
#[must_use]
pub fn capacity(mut self, capacity: usize) -> Self {
Self {
command_args: self.command_args.arg("CAPACITY").arg(capacity).build(),
}
}
/// If specified, prevents automatic filter creation if the filter does not exist.
///
/// Instead, an error is returned if the filter does not already exist.
/// This option is mutually exclusive with [`capacity`](CfInsertOptions::capacity).
#[must_use]
pub fn nocreate(mut self) -> Self {
Self {
command_args: self.command_args.arg("NOCREATE").build(),
}
}
}
impl ToArgs for CfInsertOptions {
fn write_args(&self, args: &mut CommandArgs) {
args.arg(&self.command_args);
}
}
/// Options for the [`cf_reserve`](CuckooCommands::cf_reserve) command.
#[derive(Default)]
pub struct CfReserveOptions {
command_args: CommandArgs,
}
impl CfReserveOptions {
/// Number of items in each bucket.
///
/// A higher bucket size value improves the fill rate but also causes a higher error rate and slightly slower performance.
/// The default value is 2.
#[must_use]
pub fn bucketsize(mut self, bucketsize: usize) -> Self {
Self {
command_args: self.command_args.arg("BUCKETSIZE").arg(bucketsize).build(),
}
}
/// Number of attempts to swap items between buckets before declaring filter as full and creating an additional filter.
///
/// A low value is better for performance and a higher number is better for filter fill rate.
/// The default value is 20.
pub fn maxiterations(mut self, maxiterations: usize) -> Self {
Self {
command_args: self
.command_args
.arg("MAXITERATIONS")
.arg(maxiterations)
.build(),
}
}
/// When a new filter is created, its size is the size of the current filter multiplied by `expansion`.
/// Expansion is rounded to the next `2^n` number.
/// The default value is 1.
#[must_use]
pub fn expansion(mut self, expansion: usize) -> Self {
Self {
command_args: self.command_args.arg("EXPANSION").arg(expansion).build(),
}
}
}
impl ToArgs for CfReserveOptions {
fn write_args(&self, args: &mut CommandArgs) {
args.arg(&self.command_args);
}
}
/// Result for the [`cf_scandump`](CuckooCommands::cf_scandump) command.
#[derive(Debug, Deserialize)]
pub struct CfScanDumpResult {
pub iterator: i64,
#[serde(deserialize_with = "deserialize_byte_buf")]
pub data: Vec<u8>,
}