diff options
author | antirez <antirez@gmail.com> | 2018-09-25 16:49:46 +0200 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2018-09-25 16:49:46 +0200 |
commit | 3ff82790e11bac25db7d0e75d8a27382526161c5 (patch) | |
tree | 493b0bae125f93ebcb6f4c1a9941a5e21c196390 | |
parent | fb1d5717dedf731ed2678b0340b34ea6bcbc64f7 (diff) | |
download | redis-3ff82790e11bac25db7d0e75d8a27382526161c5.tar.gz |
Modules: dictionary API WIP #9: iterator returning string object.
-rw-r--r-- | src/module.c | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/src/module.c b/src/module.c index 6d0a003d9..3f6a25aa4 100644 --- a/src/module.c +++ b/src/module.c @@ -4544,12 +4544,29 @@ void *RM_DictPrevC(RedisModuleDictIter *di, size_t *keylen, void **dataptr) { return di->ri.key; } -/* TODO - RM_DictNextC(); - RM_DictPrevC(); - RM_DictNext(); - RM_DictPrev(); -*/ +/* Like RedisModuleNextC(), but instead of returning an internally allocated + * buffer and key length, it returns directly a module string object allocated + * in the specified context 'ctx' (that may be NULL exactly like for the main + * API RedisModule_CreateString). + * + * The returned string object should be deallocated after use, either manually + * or by using a context that has automatic memory management active. */ +RedisModuleString *RM_DictNext(RedisModuleCtx *ctx, RedisModuleDictIter *di, void **dataptr) { + size_t keylen; + void *key = RM_DictNextC(di,&keylen,dataptr); + if (key == NULL) return NULL; + return RM_CreateString(ctx,key,keylen); +} + +/* Like RedisModule_DictNext() but after returning the currently selected + * element in the iterator, it selects the previous element (laxicographically + * smaller) instead of the next one. */ +RedisModuleString *RM_DictPrev(RedisModuleCtx *ctx, RedisModuleDictIter *di, void **dataptr) { + size_t keylen; + void *key = RM_DictPrevC(di,&keylen,dataptr); + if (key == NULL) return NULL; + return RM_CreateString(ctx,key,keylen); +} /* -------------------------------------------------------------------------- * Modules utility APIs |