Commit 6eeeda39 authored by antirez's avatar antirez
Browse files

Modules: sorted set iterators WIP #2.

parent eac5a13c
...@@ -1075,6 +1075,17 @@ RedisModuleString *RM_ZsetRangeCurrentElement(RedisModuleKey *key, double *score ...@@ -1075,6 +1075,17 @@ RedisModuleString *RM_ZsetRangeCurrentElement(RedisModuleKey *key, double *score
* does not include any item at all. */ * does not include any item at all. */
int RM_ZsetRangeNext(RedisModuleKey *key) { int RM_ZsetRangeNext(RedisModuleKey *key) {
if (!key->zr || !key->zcurrent) return 0; /* No active iterator. */ if (!key->zr || !key->zcurrent) return 0; /* No active iterator. */
zrangespec zrs;
/* Convert to core range structure. */
RedisModuleZsetRange *zr = key->zr;
if (zr->type == REDISMODULE_ZSET_RANGE_SCORE) {
zrs.min = zr->score_start;
zrs.max = zr->score_end;
zrs.minex = (zr->flags & REDISMODULE_ZSET_RANGE_START_EX) != 0;
zrs.maxex = (zr->flags & REDISMODULE_ZSET_RANGE_END_EX) != 0;
}
if (key->value->encoding == OBJ_ENCODING_ZIPLIST) { if (key->value->encoding == OBJ_ENCODING_ZIPLIST) {
unsigned char *zl = key->value->ptr; unsigned char *zl = key->value->ptr;
unsigned char *eptr = key->zcurrent; unsigned char *eptr = key->zcurrent;
...@@ -1085,8 +1096,19 @@ int RM_ZsetRangeNext(RedisModuleKey *key) { ...@@ -1085,8 +1096,19 @@ int RM_ZsetRangeNext(RedisModuleKey *key) {
key->zer = 1; key->zer = 1;
return 0; return 0;
} else { } else {
/* TODO: check if we are in range. */ /* Fetch the next element score for the
key->zcurrent = next; * range check. */
unsigned char *saved_next = next;
next = ziplistNext(zl,next); /* Skip next element. */
double score = zzlGetScore(next); /* Obtain the next score. */
/* Are we still within the range? */
if (zr->type == REDISMODULE_ZSET_RANGE_SCORE &&
!zslValueLteMax(score,&zrs))
{
key->zer = 1;
return 0;
}
key->zcurrent = saved_next;
return 1; return 1;
} }
} else if (key->value->encoding == OBJ_ENCODING_SKIPLIST) { } else if (key->value->encoding == OBJ_ENCODING_SKIPLIST) {
...@@ -1095,7 +1117,13 @@ int RM_ZsetRangeNext(RedisModuleKey *key) { ...@@ -1095,7 +1117,13 @@ int RM_ZsetRangeNext(RedisModuleKey *key) {
key->zer = 1; key->zer = 1;
return 0; return 0;
} else { } else {
/* TODO: check if we are in range. */ /* Are we still within the range? */
if (zr->type == REDISMODULE_ZSET_RANGE_SCORE &&
!zslValueLteMax(ln->score,&zrs))
{
key->zer = 1;
return 0;
}
key->zcurrent = next; key->zcurrent = next;
return 1; return 1;
} }
......
...@@ -1344,6 +1344,8 @@ int zsetAdd(robj *zobj, double score, sds ele, int *flags, double *newscore); ...@@ -1344,6 +1344,8 @@ int zsetAdd(robj *zobj, double score, sds ele, int *flags, double *newscore);
long zsetRank(robj *zobj, sds ele, int reverse); long zsetRank(robj *zobj, sds ele, int reverse);
int zsetDel(robj *zobj, sds ele); int zsetDel(robj *zobj, sds ele);
sds ziplistGetObject(unsigned char *sptr); sds ziplistGetObject(unsigned char *sptr);
int zslValueGteMin(double value, zrangespec *spec);
int zslValueLteMax(double value, zrangespec *spec);
/* Core functions */ /* Core functions */
int freeMemoryIfNeeded(void); int freeMemoryIfNeeded(void);
......
...@@ -244,7 +244,7 @@ int zslDelete(zskiplist *zsl, double score, sds ele, zskiplistNode **node) { ...@@ -244,7 +244,7 @@ int zslDelete(zskiplist *zsl, double score, sds ele, zskiplistNode **node) {
return 0; /* not found */ return 0; /* not found */
} }
static int zslValueGteMin(double value, zrangespec *spec) { int zslValueGteMin(double value, zrangespec *spec) {
return spec->minex ? (value > spec->min) : (value >= spec->min); return spec->minex ? (value > spec->min) : (value >= spec->min);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment