diff options
author | antirez <antirez@gmail.com> | 2018-09-24 11:44:45 +0200 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2018-09-24 11:44:49 +0200 |
commit | 14b2f7b033ab7445146ba44940297cb97473aebe (patch) | |
tree | a3def217930f00ad4e43f0626a1da4ab64bada67 /src | |
parent | bb64c7d8b25b6253172d9af41a600a0640a725d3 (diff) | |
download | redis-14b2f7b033ab7445146ba44940297cb97473aebe.tar.gz |
Modules: dictionary API work in progress #3: Iterator creation.
Diffstat (limited to 'src')
-rw-r--r-- | src/module.c | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/src/module.c b/src/module.c index 3c697d421..456f83eaa 100644 --- a/src/module.c +++ b/src/module.c @@ -254,6 +254,11 @@ typedef struct RedisModuleDict { rax *rax; /* The radix tree. */ } RedisModuleDict; +typedef struct RedisModuleDictIter { + RedisModuleDict *dict; + raxIterator ri; +} RedisModuleDictIter; + /* -------------------------------------------------------------------------- * Prototypes * -------------------------------------------------------------------------- */ @@ -4404,6 +4409,40 @@ int RM_DictDelStr(RedisModuleDict *d, RedisModuleString *key, void *oldval) { return RM_DictDel(d,key->ptr,sdslen(key->ptr),oldval); } +/* Return an interator, setup in order to start iterating from the specified + * key by applying the operator 'op', which is just a string specifying the + * comparison operator to use in order to seek the first element. The + * operators avalable are: + * + * "^" -- Seek the first (lexicographically smaller) key. + * "$" -- Seek the last (lexicographically biffer) key. + * ">" -- Seek the first element greter than the specified key. + * ">=" -- Seek the first element greater or equal than the specified key. + * "<" -- Seek the first element smaller than the specified key. + * "<=" -- Seek the first element smaller or equal than the specified key. + * "==" -- Seek the first element matching exactly the specified key. + * + * Note that for "^" and "$" the passed key is not used, and the user may + * just pass NULL with a length of 0. + * + * If the element to start the iteration cannot be seeked based on the + * key and operator passed, RedisModule_DictNext() / Prev() will just return + * REDISMODULE_ERR at the first call, otherwise they'll produce elements. + */ +RedisModuleDictIter *RM_DictIteratorStart(RedisModuleDict *d, const char *op, void *key, size_t keylen) { + RedisModuleDictIter *di = zmalloc(sizeof(*di)); + di->dict = d; + raxStart(&di->ri,d->rax); + raxSeek(&di->ri,op,key,keylen); + return di; +} + +/* Exactly like RedisModule_DictIteratorStart, but the key is passed as a + * RedisModuleString. */ +RedisModuleDictIter *RM_DictIteratorStartStr(RedisModuleDict *d, const char *op, RedisModuleString *key) { + return RM_DictIteratorStart(d,op,key->ptr,sdslen(key->ptr)); +} + /* TODO RM_DictIteratorStart(); @@ -4412,8 +4451,9 @@ int RM_DictDelStr(RedisModuleDict *d, RedisModuleString *key, void *oldval) { RM_DictIteratorReseekStr(); RM_DictNext(); RM_DictPrev(); + RM_DictNextStr(); + RM_DictPrevStr(); RM_DictIteratorStop(); - */ /* -------------------------------------------------------------------------- |