MCPcopy Create free account
hub / github.com/F-Stack/f-stack / RM_ZsetRangePrev

Function RM_ZsetRangePrev

app/redis-6.2.6/src/module.c:2985–3044  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

2983 * a previous element, 0 if we are already at the first element or the range
2984 * does not include any item at all. */
2985int RM_ZsetRangePrev(RedisModuleKey *key) {
2986 if (!key->value || key->value->type != OBJ_ZSET) return 0;
2987 if (!key->u.zset.type || !key->u.zset.current) return 0; /* No active iterator. */
2988
2989 if (key->value->encoding == OBJ_ENCODING_ZIPLIST) {
2990 unsigned char *zl = key->value->ptr;
2991 unsigned char *eptr = key->u.zset.current;
2992 unsigned char *prev;
2993 prev = ziplistPrev(zl,eptr); /* Go back to previous score. */
2994 if (prev) prev = ziplistPrev(zl,prev); /* Back to previous ele. */
2995 if (prev == NULL) {
2996 key->u.zset.er = 1;
2997 return 0;
2998 } else {
2999 /* Are we still within the range? */
3000 if (key->u.zset.type == REDISMODULE_ZSET_RANGE_SCORE) {
3001 /* Fetch the previous element score for the
3002 * range check. */
3003 unsigned char *saved_prev = prev;
3004 prev = ziplistNext(zl,prev); /* Skip element to get the score.*/
3005 double score = zzlGetScore(prev); /* Obtain the prev score. */
3006 if (!zslValueGteMin(score,&key->u.zset.rs)) {
3007 key->u.zset.er = 1;
3008 return 0;
3009 }
3010 prev = saved_prev;
3011 } else if (key->u.zset.type == REDISMODULE_ZSET_RANGE_LEX) {
3012 if (!zzlLexValueGteMin(prev,&key->u.zset.lrs)) {
3013 key->u.zset.er = 1;
3014 return 0;
3015 }
3016 }
3017 key->u.zset.current = prev;
3018 return 1;
3019 }
3020 } else if (key->value->encoding == OBJ_ENCODING_SKIPLIST) {
3021 zskiplistNode *ln = key->u.zset.current, *prev = ln->backward;
3022 if (prev == NULL) {
3023 key->u.zset.er = 1;
3024 return 0;
3025 } else {
3026 /* Are we still within the range? */
3027 if (key->u.zset.type == REDISMODULE_ZSET_RANGE_SCORE &&
3028 !zslValueGteMin(prev->score,&key->u.zset.rs))
3029 {
3030 key->u.zset.er = 1;
3031 return 0;
3032 } else if (key->u.zset.type == REDISMODULE_ZSET_RANGE_LEX) {
3033 if (!zslLexValueGteMin(prev->ele,&key->u.zset.lrs)) {
3034 key->u.zset.er = 1;
3035 return 0;
3036 }
3037 }
3038 key->u.zset.current = prev;
3039 return 1;
3040 }
3041 } else {
3042 serverPanic("Unsupported zset encoding");

Callers

nothing calls this directly

Calls 6

ziplistPrevFunction · 0.85
ziplistNextFunction · 0.85
zzlGetScoreFunction · 0.85
zslValueGteMinFunction · 0.85
zzlLexValueGteMinFunction · 0.85
zslLexValueGteMinFunction · 0.85

Tested by

no test coverage detected