diff options
author | meir@redislabs.com <meir@redislabs.com> | 2019-10-17 15:37:01 +0300 |
---|---|---|
committer | meir@redislabs.com <meir@redislabs.com> | 2019-11-11 16:01:41 +0200 |
commit | 11c6ce812aa32cf6a6011697cbfe8881ff9450fa (patch) | |
tree | b1da92611952a4c70c9ba2aeafdf5267d0f27ff5 /tests | |
parent | 0f026af185e918a9773148f6ceaa1b084662be88 (diff) | |
download | redis-11c6ce812aa32cf6a6011697cbfe8881ff9450fa.tar.gz |
Added scan implementation to module api.
The implementation expose the following new functions:
1. RedisModule_CursorCreate - allow to create a new cursor object for
keys scanning
2. RedisModule_CursorRestart - restart an existing cursor to restart the
scan
3. RedisModule_CursorDestroy - destroy an existing cursor
4. RedisModule_Scan - scan keys
The RedisModule_Scan function gets a cursor object, a callback and void*
(used as user private data).
The callback will be called for each key in the database proving the key
name and the value as RedisModuleKey.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/modules/Makefile | 3 | ||||
-rw-r--r-- | tests/modules/scan.c | 62 | ||||
-rw-r--r-- | tests/unit/moduleapi/scan.tcl | 18 |
3 files changed, 82 insertions, 1 deletions
diff --git a/tests/modules/Makefile b/tests/modules/Makefile index 9e27758a2..07c3cb829 100644 --- a/tests/modules/Makefile +++ b/tests/modules/Makefile @@ -19,7 +19,8 @@ TEST_MODULES = \ propagate.so \ misc.so \ hooks.so \ - blockonkeys.so + blockonkeys.so \ + scan.so .PHONY: all diff --git a/tests/modules/scan.c b/tests/modules/scan.c new file mode 100644 index 000000000..21071720a --- /dev/null +++ b/tests/modules/scan.c @@ -0,0 +1,62 @@ +#define REDISMODULE_EXPERIMENTAL_API +#include "redismodule.h" + +#include <string.h> +#include <assert.h> +#include <unistd.h> + +#define UNUSED(V) ((void) V) + +typedef struct scan_pd{ + size_t nkeys; + RedisModuleCtx *ctx; +} scan_pd; + +void scan_callback(void *privdata, RedisModuleString* keyname, RedisModuleKey* key){ + scan_pd* pd = privdata; + RedisModule_ReplyWithArray(pd->ctx, 2); + + RedisModule_ReplyWithString(pd->ctx, keyname); + if(key && RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_STRING){ + size_t len; + char * data = RedisModule_StringDMA(key, &len, REDISMODULE_READ); + RedisModule_ReplyWithStringBuffer(pd->ctx, data, len); + }else{ + RedisModule_ReplyWithNull(pd->ctx); + } + pd->nkeys++; +} + +int scan_keys_values(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) +{ + scan_pd pd = { + .nkeys = 0, + .ctx = ctx, + }; + + RedisModule_ReplyWithArray(ctx, REDISMODULE_POSTPONED_ARRAY_LEN); + + RedisModuleCursor* cursor = RedisModule_CursorCreate(); + while(RedisModule_Scan(ctx, cursor, scan_callback, &pd)); + RedisModule_CursorDestroy(cursor); + + RedisModule_ReplySetArrayLength(ctx, pd.nkeys); + return 0; +} + +int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { + UNUSED(argv); + UNUSED(argc); + if (RedisModule_Init(ctx, "scan", 1, REDISMODULE_APIVER_1)== REDISMODULE_ERR) + return REDISMODULE_ERR; + + if (RedisModule_CreateCommand(ctx, "scan.scankeysvalues", scan_keys_values, "", 0, 0, 0) == REDISMODULE_ERR) + return REDISMODULE_ERR; + + return REDISMODULE_OK; +} + + + + + diff --git a/tests/unit/moduleapi/scan.tcl b/tests/unit/moduleapi/scan.tcl new file mode 100644 index 000000000..5a77e8195 --- /dev/null +++ b/tests/unit/moduleapi/scan.tcl @@ -0,0 +1,18 @@ +set testmodule [file normalize tests/modules/scan.so] + +proc count_log_message {pattern} { + set result [exec grep -c $pattern < [srv 0 stdout]] +} + +start_server {tags {"modules"}} { + r module load $testmodule + + test {Module scan} { + # the module create a scan command which also return values + r set x 1 + r set y 2 + r set z 3 + lsort [r scan.scankeysvalues] + } {{x 1} {y 2} {z 3}} + +}
\ No newline at end of file |