-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added keys scan API. * Added comment on taken the underline RedisModuleKey. * Apply suggestions from code review Co-authored-by: Guy Korland <[email protected]> Co-authored-by: Guy Korland <[email protected]>
- Loading branch information
1 parent
2587a7a
commit dbc7883
Showing
7 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#[macro_use] | ||
extern crate redis_module; | ||
|
||
use redis_module::{Context, KeysCursor, RedisResult, RedisString, RedisValue}; | ||
|
||
fn scan_keys(ctx: &Context, _args: Vec<RedisString>) -> RedisResult { | ||
let cursor = KeysCursor::new(); | ||
let mut res = Vec::new(); | ||
while cursor.scan(ctx, &|_ctx, key_name, _key| { | ||
res.push(RedisValue::BulkRedisString(key_name)); | ||
}) {} | ||
Ok(RedisValue::Array(res)) | ||
} | ||
|
||
////////////////////////////////////////////////////// | ||
|
||
redis_module! { | ||
name: "scan", | ||
version: 1, | ||
data_types: [], | ||
commands: [ | ||
["scan_keys", scan_keys, "readonly", 0, 0, 0], | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::context::Context; | ||
use crate::key::RedisKey; | ||
use crate::raw; | ||
use crate::redismodule::RedisString; | ||
use std::ffi::c_void; | ||
|
||
pub struct KeysCursor { | ||
inner_cursor: *mut raw::RedisModuleScanCursor, | ||
} | ||
|
||
extern "C" fn scan_callback<C: FnMut(&Context, RedisString, Option<&RedisKey>)>( | ||
ctx: *mut raw::RedisModuleCtx, | ||
key_name: *mut raw::RedisModuleString, | ||
key: *mut raw::RedisModuleKey, | ||
private_data: *mut ::std::os::raw::c_void, | ||
) { | ||
let context = Context::new(ctx); | ||
let key_name = RedisString::new(ctx, key_name); | ||
let redis_key = if key.is_null() { | ||
None | ||
} else { | ||
Some(RedisKey::from_raw_parts(ctx, key)) | ||
}; | ||
let callback = unsafe { &mut *(private_data.cast::<C>()) }; | ||
callback(&context, key_name, redis_key.as_ref()); | ||
|
||
// we are not the owner of the key, so we must take the underline *mut raw::RedisModuleKey so it will not be freed. | ||
redis_key.map(|v| v.take()); | ||
} | ||
|
||
impl KeysCursor { | ||
pub fn new() -> KeysCursor { | ||
let inner_cursor = unsafe { raw::RedisModule_ScanCursorCreate.unwrap()() }; | ||
KeysCursor { inner_cursor } | ||
} | ||
|
||
pub fn scan<F: FnMut(&Context, RedisString, Option<&RedisKey>)>( | ||
&self, | ||
ctx: &Context, | ||
callback: &F, | ||
) -> bool { | ||
let res = unsafe { | ||
raw::RedisModule_Scan.unwrap()( | ||
ctx.ctx, | ||
self.inner_cursor, | ||
Some(scan_callback::<F>), | ||
callback as *const F as *mut c_void, | ||
) | ||
}; | ||
res != 0 | ||
} | ||
|
||
pub fn restart(&self) { | ||
unsafe { raw::RedisModule_ScanCursorRestart.unwrap()(self.inner_cursor) }; | ||
} | ||
} | ||
|
||
impl Default for KeysCursor { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Drop for KeysCursor { | ||
fn drop(&mut self) { | ||
unsafe { raw::RedisModule_ScanCursorDestroy.unwrap()(self.inner_cursor) }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters