-
Notifications
You must be signed in to change notification settings - Fork 77
MOD-10712 - Port RM_ScanKey #419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2de7ecc
42976c7
5f09b81
1c91208
05fe1fa
c9f20c2
72e612c
1a98bb5
dd503b3
2ae50c8
03682b7
10d1ced
b0746d3
d23e1ac
ac22308
aff0e3b
3fc969b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,131 @@ | ||||||
| use std::{ | ||||||
| ffi::c_void, | ||||||
| ptr::{self}, | ||||||
| }; | ||||||
|
|
||||||
| use crate::{key::RedisKey, raw, RedisString}; | ||||||
|
|
||||||
| /// 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: | ||||||
| /// | ||||||
| /// ```text | ||||||
| /// 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. | ||||||
| /// | ||||||
| /// ```ignore | ||||||
| /// 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: | ||||||
| /// | ||||||
| /// ```text | ||||||
| /// 1) "name" | ||||||
| /// 2) "Alice" | ||||||
| /// 3) "age" | ||||||
| /// 4) "29" | ||||||
| /// 5) "location" | ||||||
| /// 6) "Austin" | ||||||
| /// ``` | ||||||
| pub struct ScanKeyCursor { | ||||||
| key: RedisKey, | ||||||
| inner_cursor: *mut raw::RedisModuleScanCursor, | ||||||
| } | ||||||
|
|
||||||
| impl ScanKeyCursor { | ||||||
| /// Creates a new scan cursor for the given key. | ||||||
| pub fn new(key: RedisKey) -> Self { | ||||||
| let inner_cursor = unsafe { raw::RedisModule_ScanCursorCreate.unwrap()() }; | ||||||
| Self { key, inner_cursor } | ||||||
| } | ||||||
|
|
||||||
| /// Restarts the cursor from the beginning. | ||||||
| pub fn restart(&self) { | ||||||
| unsafe { raw::RedisModule_ScanCursorRestart.unwrap()(self.inner_cursor) }; | ||||||
| } | ||||||
|
|
||||||
| /// 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 | ||||||
| /// | ||||||
| /// ```ignore | ||||||
| /// while cursor.scan(|_key, field, value| { | ||||||
| /// // do something with field and value | ||||||
| /// }) { | ||||||
| /// // do something between scans if needed, like an early stop | ||||||
| /// } | ||||||
| pub fn scan<F: FnMut(&RedisKey, &RedisString, &RedisString)>(&self, f: F) -> bool { | ||||||
| unsafe extern "C" fn scan_callback<F: FnMut(&RedisKey, &RedisString, &RedisString)>( | ||||||
| key: *mut raw::RedisModuleKey, | ||||||
| field: *mut raw::RedisModuleString, | ||||||
| value: *mut raw::RedisModuleString, | ||||||
| data: *mut c_void, | ||||||
| ) { | ||||||
| let ctx = ptr::null_mut(); | ||||||
| let key = RedisKey::from_raw_parts(ctx, key); | ||||||
|
|
||||||
| let field = RedisString::from_redis_module_string(ctx, field); | ||||||
| let value = RedisString::from_redis_module_string(ctx, value); | ||||||
|
|
||||||
| let callback = unsafe { &mut *(data.cast::<F>()) }; | ||||||
| callback(&key, &field, &value); | ||||||
|
|
||||||
| // we're not the owner of field and value strings | ||||||
| field.take(); | ||||||
| value.take(); | ||||||
|
|
||||||
| key.take(); // we're not the owner of the key either | ||||||
| } | ||||||
|
|
||||||
| // Safety: The c-side initialized the function ptr and it is is never changed, | ||||||
| // i.e. after module initialization the function pointers stay valid till the end of the program. | ||||||
| let scan_key = unsafe { raw::RedisModule_ScanKey.unwrap() }; | ||||||
|
|
||||||
| let res = unsafe { | ||||||
| scan_key( | ||||||
| self.key.key_inner, | ||||||
| self.inner_cursor, | ||||||
| Some(scan_callback::<F>), | ||||||
| &f as *const F as *mut c_void, | ||||||
|
||||||
| &f as *const F as *mut c_void, | |
| &mut f as *mut F as *mut c_void, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f is not mutable, we're not allowed to change the function pointer, but we need to cast to *mut void to fit the function signature of the C callback.
FnMut allows changing the captured variables. Such that it can take a let mut array = Vec::new(); and add entries.
Uh oh!
There was an error while loading. Please reload this page.