redis 1.3.0

Redis driver for Rust.
Documentation
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! Defines types to use with Bloom filter commands.

use crate::errors::invalid_type_error;
use crate::{
    Commands, Connection, FromRedisValue, ParsingError, RedisResult, RedisWrite, ToRedisArgs,
    ToSingleRedisArg, Value,
};
use std::ops::Deref;

/// Specific type of information to query for Bloom filters
#[derive(Debug)]
#[non_exhaustive]
pub enum BloomFilterInfoType {
    /// Number of unique items the Bloom filter can hold before scaling would be required
    Capacity,
    /// Number of bytes allocated for the Bloom filter
    Size,
    /// Number of sub-filters of the Bloom filter
    Filters,
    /// Number of detected unique items added to the Bloom filter
    Items,
    /// Expansion rate of the Bloom filter
    Expansion,
}

impl ToRedisArgs for BloomFilterInfoType {
    fn write_redis_args<W>(&self, out: &mut W)
    where
        W: ?Sized + RedisWrite,
    {
        match *self {
            BloomFilterInfoType::Capacity => {
                out.write_arg(b"CAPACITY");
            }
            BloomFilterInfoType::Expansion => {
                out.write_arg(b"EXPANSION");
            }
            BloomFilterInfoType::Filters => {
                out.write_arg(b"FILTERS");
            }
            BloomFilterInfoType::Items => {
                out.write_arg(b"ITEMS");
            }
            BloomFilterInfoType::Size => {
                out.write_arg(b"SIZE");
            }
        }
    }
}

impl ToSingleRedisArg for BloomFilterInfoType {}

/// Response of a Bloom filter info type query
///
/// `RESP2` and `RESP3` respond with different structures, so we need to abstract that difference
/// away to make the info type query usage simple. Note that it derefs to `f64` for ease of use.
#[derive(Debug, PartialEq)]
pub struct BloomFilterInfoTypeResponse {
    value: f64,
}

impl Deref for BloomFilterInfoTypeResponse {
    type Target = f64;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl PartialEq<f64> for BloomFilterInfoTypeResponse {
    fn eq(&self, other: &f64) -> bool {
        self.value == *other
    }
}
impl FromRedisValue for BloomFilterInfoTypeResponse {
    fn from_redis_value(v: Value) -> Result<Self, ParsingError> {
        match v {
            // type-less RESP2 query gives an array of exactly one `Int`
            Value::Array(items) => {
                let mut item_iter = items.into_iter();
                let Some(value) = item_iter.next() else {
                    invalid_type_error!("array has to hold exactly one element, but was empty");
                };

                if item_iter.next().is_some() {
                    invalid_type_error!(
                        "array has to hold exactly one element, but contained more"
                    );
                }

                Ok(Self {
                    value: f64::from_redis_value(value)?,
                })
            }

            // type-less RESP3 query gives a map of exactly one pair of (`SimpleString`, `Int`)
            Value::Map(items) => {
                let mut item_iter = items.into_iter();
                let Some((_, value)) = item_iter.next() else {
                    invalid_type_error!("map has to hold exactly one element, but was empty");
                };

                if item_iter.next().is_some() {
                    invalid_type_error!("map holds more than a single value");
                }

                Ok(Self {
                    value: f64::from_redis_value(value)?,
                })
            }

            // typed query gives an `Int`
            Value::Int(_) => Ok(Self {
                value: f64::from_redis_value(v)?,
            }),

            _ => invalid_type_error!(
                v,
                "expected array of an int, map to an int, or int response"
            ),
        }
    }
}

/// Strategies for how a Bloom filter can scale to hold more items
#[derive(Debug)]
#[non_exhaustive]
pub enum BloomFilterScalingOptions {
    /// Specifies the desired expansion rate for the filter to be created.
    ExpansionRate(usize),
    /// Prevents the filter from creating additional sub-filters if initial capacity is reached.
    NonScaling,
}

impl ToRedisArgs for BloomFilterScalingOptions {
    fn write_redis_args<W>(&self, out: &mut W)
    where
        W: ?Sized + RedisWrite,
    {
        match *self {
            BloomFilterScalingOptions::ExpansionRate(expansion) => {
                out.write_arg(b"EXPANSION");
                expansion.write_redis_args(out);
            }
            BloomFilterScalingOptions::NonScaling => {
                out.write_arg(b"NONSCALING");
            }
        }
    }
}

/// Options for inserting items to a Bloom filter
#[derive(Default, Debug)]
pub struct BloomFilterInsertOptions {
    create: Option<bool>,
    capacity: Option<usize>,
    error_rate: Option<f64>,
    expansion: Option<BloomFilterScalingOptions>,
}

impl BloomFilterInsertOptions {
    ///Indicates that the filter should not be created if it does not already exist.
    pub fn nocreate(mut self) -> Self {
        self.create = Some(false);
        self
    }

    /// Specifies the desired capacity for the filter to be created.
    pub fn expansion(mut self, scale_option: BloomFilterScalingOptions) -> Self {
        self.expansion = Some(scale_option);
        self
    }

    /// Specifies the error ratio of the newly created filter if it does not yet exist.
    pub fn error_rate(mut self, error_rate: f64) -> Self {
        self.error_rate = Some(error_rate);
        self
    }

    /// Specifies the desired capacity for the filter to be created.
    pub fn capacity(mut self, capacity: usize) -> Self {
        self.capacity = Some(capacity);
        self
    }
}
impl ToRedisArgs for BloomFilterInsertOptions {
    fn write_redis_args<W>(&self, out: &mut W)
    where
        W: ?Sized + RedisWrite,
    {
        if Some(false) == self.create {
            out.write_arg(b"NOCREATE");
        }

        if let Some(ref error) = self.error_rate {
            out.write_arg(b"ERROR");
            error.write_redis_args(out);
        }

        if let Some(ref capacity) = self.capacity {
            out.write_arg(b"CAPACITY");
            capacity.write_redis_args(out);
        }

        if let Some(ref scaling) = self.expansion {
            scaling.write_redis_args(out);
        }
    }
}

/// Single chunk of a Bloom filter scan dump
#[derive(Debug, Clone, PartialEq)]
pub struct BloomFilterDumpChunk {
    /// The iterator associated to the [`data`](Self::data)
    pub iterator: i64,
    /// The chunked data
    pub data: Vec<u8>,
}

impl FromRedisValue for BloomFilterDumpChunk {
    fn from_redis_value(v: Value) -> Result<Self, ParsingError> {
        // `v` should be an array holding an Int (iterator) followed by BulkString (data).

        let Value::Array(items) = v else {
            invalid_type_error!(v, "expected array response");
        };

        let mut items_iter = items.into_iter();

        // Extract the iterator
        let item = items_iter.next();
        let Some(Value::Int(iterator)) = item else {
            invalid_type_error!(item, "expected first element to be integer");
        };

        // Extract the data
        let item = items_iter.next();
        let Some(Value::BulkString(data)) = item else {
            invalid_type_error!(item, "expected second element to be a Bulk string");
        };

        // `items_iter` should be empty now, so we guard against upstream additions
        if items_iter.next().is_some() {
            invalid_type_error!("expected only two elements");
        }

        // Yield the chunk
        Ok(BloomFilterDumpChunk { iterator, data })
    }
}

/// An iterator performing a Bloom filter scan dump
///
/// # Examples
///
/// ```rust,no_run
/// # fn dump() -> redis::RedisResult<()> {
/// let client = redis::Client::open("redis://127.0.0.1/")?;
/// let mut con = client.get_connection()?;
///
/// // Fully dump the bloom filter at key `foo`
/// let full_dump = redis::bloom::BloomFilterDumpIterator::new(&mut con, "foo")
///     .map(|r| r.expect("dump should succeed"))
///     .collect::<Vec<_>>();
/// # Ok(())
/// # }
/// ```
pub struct BloomFilterDumpIterator<'a> {
    con: &'a mut Connection,
    key: &'a str,
    iterator: i64,
    /// `true`, iff the iterator cannot produce more elements.
    dry: bool,
}

impl<'a> BloomFilterDumpIterator<'a> {
    /// Create a new iterator for the given key
    pub fn new(con: &'a mut Connection, key: &'a str) -> Self {
        BloomFilterDumpIterator {
            con,
            key,
            iterator: 0,
            dry: false,
        }
    }
}

impl Iterator for BloomFilterDumpIterator<'_> {
    type Item = RedisResult<BloomFilterDumpChunk>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.dry {
            return None;
        }
        let dump_result: Self::Item = self.con.bf_scandump(self.key, self.iterator);
        match dump_result {
            Ok(ref dump) => {
                self.iterator = dump.iterator;
                if dump.iterator == 0 {
                    self.dry = true;
                    return None;
                }
                Some(dump_result)
            }

            Err(e) => {
                self.dry = true;
                Some(Err(e))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{BloomFilterDumpChunk, BloomFilterInfoTypeResponse};
    use crate::FromRedisValue;
    use crate::types::Value;

    /// Tries to assure that [`BloomFilterInfoTypeResponse`] conversion from wrong type gives a useful error
    #[test]
    fn info_type_response_from_value_wrong_type() {
        // The value to try to parse from.
        // An `Array` or `Map` is expected, but it is text, so conversion should fail.
        let value = Value::SimpleString("foo".to_string());

        // Actual parsing
        let err = BloomFilterInfoTypeResponse::from_redis_value(value)
            .expect_err("conversion should fail");

        // Checking the error message
        assert!(err.to_string().contains("expected array"));
    }

    /// Tries to assure that [`BloomFilterInfoTypeResponse`] conversion from a too short RESP2 response gives a useful error
    #[test]
    fn info_type_response_from_value_array_missing_item() {
        // The value to try to parse from.
        // An `Array` response should have exactly 1 entry. Here we have 0, so the conversion should fail.
        let value = Value::Array(vec![]);

        // Actual parsing
        let err = BloomFilterInfoTypeResponse::from_redis_value(value)
            .expect_err("conversion should fail");

        // Checking the error message
        assert!(err.to_string().contains("empty"));
    }

    /// Tries to assure that [`BloomFilterInfoTypeResponse`] conversion from a too long RESP2 response gives a useful error
    #[test]
    fn info_type_response_from_value_array_extra_item() {
        // The value to try to parse from.
        // An `Array` response should have exactly 1 entry. Here we have 2, so the conversion should fail.
        let value = Value::Array(vec![Value::Int(42), Value::Int(23)]);

        // Actual parsing
        let err = BloomFilterInfoTypeResponse::from_redis_value(value)
            .expect_err("conversion should fail");

        // Checking the error message
        assert!(err.to_string().contains("more"));
    }

    /// Tries to assure that [`BloomFilterInfoTypeResponse`] conversion from a faulty RESP2 response gives a useful error
    #[test]
    fn info_type_response_from_value_array_wrong_type() {
        // The value to try to parse from.
        // An `Array` response should have exactly 1 `Int`. Here we have an `Okay` instead, so the conversion should fail.
        let value = Value::Array(vec![Value::Okay]);

        // Actual parsing
        let err = BloomFilterInfoTypeResponse::from_redis_value(value)
            .expect_err("conversion should fail");

        // Checking the error message
        assert!(err.to_string().contains("not convertible"));
    }

    /// Tries to assure that [`BloomFilterInfoTypeResponse`] conversion from a too short RESP3 response gives a useful error
    #[test]
    fn info_type_response_from_value_map_missing_item() {
        // The value to try to parse from.
        // A `Map` response should have exactly 1 entry. Here we have 0, so the conversion should fail.
        let value = Value::Map(vec![]);

        // Actual parsing
        let err = BloomFilterInfoTypeResponse::from_redis_value(value)
            .expect_err("conversion should fail");

        // Checking the error message
        assert!(err.to_string().contains("empty"));
    }

    /// Tries to assure that [`BloomFilterInfoTypeResponse`] conversion from a too long RESP3 response gives a useful error
    #[test]
    fn info_type_response_from_value_map_extra_item() {
        // The value to try to parse from.
        // A `Map` response should have exactly 1 entry. Here we have 2, so the conversion should fail.
        let value = Value::Map(vec![
            (Value::SimpleString("foo".to_string()), Value::Int(42)),
            (Value::SimpleString("bar".to_string()), Value::Int(23)),
        ]);

        // Actual parsing
        let err = BloomFilterInfoTypeResponse::from_redis_value(value)
            .expect_err("conversion should fail");

        // Checking the error message
        assert!(err.to_string().contains("more"));
    }

    /// Tries to assure that [`BloomFilterInfoTypeResponse`] conversion from a faulty RESP3 response gives a useful error
    #[test]
    fn info_type_response_from_value_map_wrong_type() {
        // The value to try to parse from.
        // A `Map` response should have exactly entry mapping to `Int`. Here we have an `Okay`
        // instead, so the conversion should fail.
        let value = Value::Map(vec![(Value::SimpleString("foo".to_string()), Value::Okay)]);

        // Actual parsing
        let err = BloomFilterInfoTypeResponse::from_redis_value(value)
            .expect_err("conversion should fail");

        // Checking the error message
        assert!(err.to_string().contains("not convertible"));
    }

    /// Tries to assure that [`BloomFilterInfoTypeResponse`] conversion from a good RESP2 response works
    #[test]
    fn info_type_response_from_value_ok_resp2() {
        // The value to try to parse from.
        // A good RESP2 Response is an `Array` of a single `Int`.
        let value = Value::Array(vec![Value::Int(42)]);

        // Actual parsing
        let response = BloomFilterInfoTypeResponse::from_redis_value(value)
            .expect("conversion should succeed");

        // Checking the response
        assert_eq!(*response, 42.);
    }

    /// Tries to assure that [`BloomFilterInfoTypeResponse`] conversion from a good RESP3 response works
    #[test]
    fn info_type_response_from_value_ok_resp3() {
        // The value to try to parse from.
        // A good RESP3 Response is an `Map` of a single `SimpleString`, `Int` pair.
        let value = Value::Map(vec![(
            Value::SimpleString("foo".to_string()),
            Value::Int(42),
        )]);

        // Actual parsing
        let response = BloomFilterInfoTypeResponse::from_redis_value(value)
            .expect("conversion should succeed");

        // Checking the response
        assert_eq!(*response, 42.);
    }

    /// Tries to assure that [`BloomFilterInfoTypeResponse`] conversion from an integer succeeds
    #[test]
    fn info_type_response_from_value_ok_int() {
        // The value to try to parse from.
        let value = Value::Int(42);

        // Actual parsing
        let response = BloomFilterInfoTypeResponse::from_redis_value(value)
            .expect("conversion should succeed");

        // Checking the response
        assert_eq!(*response, 42.);
    }

    /// Tries to assure that [`BloomFilterDumpChunk`] conversion from non-array gives a useful error
    #[test]
    fn dump_chunk_from_value_no_array() {
        // The value to try to parse from.
        // An `Array` is expected, but it is an `Int`, so conversion should fail.
        let value = Value::Int(42);

        // Actual parsing
        let err =
            BloomFilterDumpChunk::from_redis_value(value).expect_err("conversion should fail");

        // Checking the error message
        assert!(err.to_string().contains("expected array"));
    }

    /// Tries to assure that [`BloomFilterDumpChunk`] conversion from mistyped first array element gives a useful error
    #[test]
    fn dump_chunk_from_value_first_item_wrong_type() {
        // The value to try to parse from.
        // The first element should be an `Int`, but is an `Ok`, so conversion should fail.
        let value = Value::Array(vec![
            Value::Okay,
            Value::BulkString("bar".as_bytes().to_vec()),
        ]);

        // Actual parsing
        let err =
            BloomFilterDumpChunk::from_redis_value(value).expect_err("conversion should fail");

        // Checking the error message
        assert!(err.to_string().contains("expected first"));
    }

    /// Tries to assure that [`BloomFilterDumpChunk`] conversion from mistyped second array element gives a useful error
    #[test]
    fn dump_chunk_from_value_second_item_wrong_type() {
        // The value to try to parse from.
        // The second element should be a `BulkString`, but is an `Ok`, so conversion should fail.
        let value = Value::Array(vec![Value::Int(42), Value::Okay]);

        // Actual parsing
        let err =
            BloomFilterDumpChunk::from_redis_value(value).expect_err("conversion should fail");

        // Checking the error message
        assert!(err.to_string().contains("expected second"));
    }

    /// Tries to assure that [`BloomFilterDumpChunk`] conversion from too short array gives a useful error
    #[test]
    fn dump_chunk_from_value_missing_item() {
        // The value to try to parse from.
        // The array should have two elements, but has only one, so conversion should fail.
        let value = Value::Array(vec![Value::Int(42)]);

        // Actual parsing
        let err =
            BloomFilterDumpChunk::from_redis_value(value).expect_err("conversion should fail");

        // Checking the error message
        assert!(err.to_string().contains("expected second"));
    }

    /// Tries to assure that [`BloomFilterDumpChunk`] conversion from too long array gives a useful error
    #[test]
    fn dump_chunk_from_value_extra_item() {
        // The value to try to parse from.
        // The array should have two elements, but has only one, so conversion should fail.
        let value = Value::Array(vec![
            Value::Int(42),
            Value::BulkString("foo".as_bytes().to_vec()),
            Value::Okay,
        ]);

        // Actual parsing
        let err =
            BloomFilterDumpChunk::from_redis_value(value).expect_err("conversion should fail");

        // Checking the error message
        assert!(err.to_string().contains("expected only"));
    }

    /// Tries to assure that [`BloomFilterDumpChunk`] conversion can succeed
    #[test]
    fn dump_chunk_from_value_ok() {
        // The value to try to parse from.
        let value = Value::Array(vec![
            Value::Int(42),
            Value::BulkString("foo".as_bytes().to_vec()),
        ]);

        // Actual parsing
        let chunk =
            BloomFilterDumpChunk::from_redis_value(value).expect("conversion should succeed");

        // Checking the returned chunk
        let expected = BloomFilterDumpChunk {
            iterator: 42,
            data: "foo".as_bytes().to_vec(),
        };
        assert_eq!(chunk, expected);
    }
}