summaryrefslogtreecommitdiff
path: root/src/modules/helloworld.c
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2016-04-21 11:45:52 +0200
committerantirez <antirez@gmail.com>2016-05-10 06:40:09 +0200
commit00109e111339221c414e340f191bcd47037d0e4c (patch)
treec1483f85643449f769af0fce4f203b5806d2ae46 /src/modules/helloworld.c
parentdb3ade22eb553d743bb4667c1b864201a574bb9d (diff)
downloadredis-00109e111339221c414e340f191bcd47037d0e4c.tar.gz
Modules: zset lex iterator #3.
Diffstat (limited to 'src/modules/helloworld.c')
-rw-r--r--src/modules/helloworld.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/modules/helloworld.c b/src/modules/helloworld.c
index 738983250..785d401b2 100644
--- a/src/modules/helloworld.c
+++ b/src/modules/helloworld.c
@@ -334,6 +334,7 @@ int HelloMoreExpire_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv,
* should match.*/
int HelloZsumRange_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
double score_start, score_end;
+ if (argc != 4) return RedisModule_WrongArity(ctx);
if (RedisModule_StringToDouble(argv[2],&score_start) != REDISMODULE_OK ||
RedisModule_StringToDouble(argv[3],&score_end) != REDISMODULE_OK)
@@ -379,6 +380,41 @@ int HelloZsumRange_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, i
return REDISMODULE_OK;
}
+/* HELLO.LEXRANGE key min_lex max_lex min_age max_age
+ * This command expects a sorted set stored at key in the following form:
+ * - All the elements have score 0.
+ * - Elements are pairs of "<name>:<age>", for example "Anna:52".
+ * The command will return all the sorted set items that are lexicographically
+ * between the specified range (using the same format as ZRANGEBYLEX)
+ * and having an age between min_age and max_age. */
+int HelloZsumRange_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
+ RedisModule_AutoMemory(ctx); /* Use automatic memory management. */
+
+ if (argc != 6) return RedisModule_WrongArity(ctx);
+
+ RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1],
+ REDISMODULE_READ|REDISMODULE_WRITE);
+ if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_ZSET) {
+ return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);
+ }
+
+ RedisModule_ReplyWithArray(ctx,REDISMODULE_POSTPONED_ARRAY_LEN);
+ RedisModule_ZsetFirstInLexRange(key,argv[2],argv[3]);
+ int arraylen = 0;
+ while(!RedisModule_ZsetRangeEndReached(key)) {
+ double score;
+ RedisModuleString *ele = RedisModule_ZsetRangeCurrentElement(key,&score);
+ RedisModule_ReplyWithString(ctx,ele);
+ RedisModule_FreeString(ctx,ele);
+ RedisModule_ZsetRangeNext(key);
+ arraylen++;
+ }
+ RedisModule_ZsetRangeStop(key);
+ RedisModule_SetArrayLength(ctx,arraylen);
+ RedisModule_CloseKey(key);
+ return REDISMODULE_OK;
+}
+
/* This function must be present on each Redis module. It is used in order to
* register the commands into the Redis server. */
int RedisModule_OnLoad(RedisModuleCtx *ctx) {