Skip to main content

ScanKeyCursor

Struct ScanKeyCursor 

Source
pub struct ScanKeyCursor { /* private fields */ }
Expand description

A cursor to scan field/value pairs of a (hash) key.

It provides access via a closure given to ScanKeyCursor::for_each or if you need more control, you can use ScanKeyCursor::scan and implement your own loop, e.g. to allow an early stop.

§Example usage

Here we show how to extract values to communicate them back to the Redis client. We assume that the following hash key is setup before:

HSET user:123 name Alice age 29 location Austin

The following example command implementation scans all fields and values in the hash key and returns them as an array of RedisString.

fn example_scan_key_for_each(ctx: &Context) -> RedisResult {
   let key = ctx.open_key_with_flags("user:123", KeyFlags::NOEFFECTS | KeyFlags::NOEXPIRE | KeyFlags::ACCESS_EXPIRED );
   let cursor  = ScanKeyCursor::new(key);

   let res = RefCell::new(Vec::new());
   cursor.for_each(|_key, field, value| {
       let mut res = res.borrow_mut();
       res.push(RedisValue::BulkRedisString(field.clone()));
       res.push(RedisValue::BulkRedisString(value.clone()));
   });

   Ok(RedisValue::Array(res.take()))
}

The method will produce the following output:

1) "name"
2) "Alice"
3) "age"
4) "29"
5) "location"
6) "Austin"

Implementations§

Source§

impl ScanKeyCursor

Source

pub fn new(key: RedisKey) -> Self

Creates a new scan cursor for the given key.

Source

pub fn restart(&self)

Restarts the cursor from the beginning.

Source

pub fn scan<F: FnMut(&RedisKey, &RedisString, &RedisString)>( &self, f: F, ) -> bool

Implements a call to RedisModule_ScanKey and calls the given closure for each callback invocation by ScanKey. Returns true if there are more fields to scan, false otherwise.

The callback may be called multiple times per RedisModule_ScanKey invocation.

§Example
while cursor.scan(|_key, field, value| {
   // do something with field and value
}) {
  // do something between scans if needed, like an early stop
}
Source

pub fn for_each<F: FnMut(&RedisKey, &RedisString, &RedisString)>(&self, f: F)

Implements a callback based for_each loop over all fields and values in the hash key. If you need more control, e.g. stopping after a scan invocation, then use ScanKeyCursor::scan directly.

Trait Implementations§

Source§

impl Drop for ScanKeyCursor

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.