MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / RM_ZsetRangeNext

Function RM_ZsetRangeNext

src/module.cpp:3009–3068  ·  view source on GitHub ↗

Go to the next element of the sorted set iterator. Returns 1 if there was * a next element, 0 if we are already at the latest element or the range * does not include any item at all. */

Source from the content-addressed store, hash-verified

3007 * a next element, 0 if we are already at the latest element or the range
3008 * does not include any item at all. */
3009int RM_ZsetRangeNext(RedisModuleKey *key) {
3010 if (!key->value || key->value->type != OBJ_ZSET) return 0;
3011 if (!key->u.zset.type || !key->u.zset.current) return 0; /* No active iterator. */
3012
3013 if (key->value->encoding == OBJ_ENCODING_ZIPLIST) {
3014 unsigned char *zl = (unsigned char*)ptrFromObj(key->value);
3015 unsigned char *eptr = (unsigned char*)key->u.zset.current;
3016 unsigned char *next;
3017 next = ziplistNext(zl,eptr); /* Skip element. */
3018 if (next) next = ziplistNext(zl,next); /* Skip score. */
3019 if (next == NULL) {
3020 key->u.zset.er = 1;
3021 return 0;
3022 } else {
3023 /* Are we still within the range? */
3024 if (key->u.zset.type == REDISMODULE_ZSET_RANGE_SCORE) {
3025 /* Fetch the next element score for the
3026 * range check. */
3027 unsigned char *saved_next = next;
3028 next = ziplistNext(zl,next); /* Skip next element. */
3029 double score = zzlGetScore(next); /* Obtain the next score. */
3030 if (!zslValueLteMax(score,&key->u.zset.rs)) {
3031 key->u.zset.er = 1;
3032 return 0;
3033 }
3034 next = saved_next;
3035 } else if (key->u.zset.type == REDISMODULE_ZSET_RANGE_LEX) {
3036 if (!zzlLexValueLteMax(next,&key->u.zset.lrs)) {
3037 key->u.zset.er = 1;
3038 return 0;
3039 }
3040 }
3041 key->u.zset.current = next;
3042 return 1;
3043 }
3044 } else if (key->value->encoding == OBJ_ENCODING_SKIPLIST) {
3045 zskiplistNode *ln = (zskiplistNode*)key->u.zset.current, *next = (zskiplistNode*)ln->level(0)->forward;
3046 if (next == NULL) {
3047 key->u.zset.er = 1;
3048 return 0;
3049 } else {
3050 /* Are we still within the range? */
3051 if (key->u.zset.type == REDISMODULE_ZSET_RANGE_SCORE &&
3052 !zslValueLteMax(next->score,&key->u.zset.rs))
3053 {
3054 key->u.zset.er = 1;
3055 return 0;
3056 } else if (key->u.zset.type == REDISMODULE_ZSET_RANGE_LEX) {
3057 if (!zslLexValueLteMax(next->ele,&key->u.zset.lrs)) {
3058 key->u.zset.er = 1;
3059 return 0;
3060 }
3061 }
3062 key->u.zset.current = next;
3063 return 1;
3064 }
3065 } else {
3066 serverPanic("Unsupported zset encoding");

Callers

nothing calls this directly

Calls 7

ptrFromObjFunction · 0.85
ziplistNextFunction · 0.85
zzlGetScoreFunction · 0.85
zslValueLteMaxFunction · 0.85
zzlLexValueLteMaxFunction · 0.85
zslLexValueLteMaxFunction · 0.85
levelMethod · 0.80

Tested by

no test coverage detected