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

Function RM_ZsetRangeNext

app/redis-6.2.6/src/module.c:2921–2980  ·  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

2919 * a next element, 0 if we are already at the latest element or the range
2920 * does not include any item at all. */
2921int RM_ZsetRangeNext(RedisModuleKey *key) {
2922 if (!key->value || key->value->type != OBJ_ZSET) return 0;
2923 if (!key->u.zset.type || !key->u.zset.current) return 0; /* No active iterator. */
2924
2925 if (key->value->encoding == OBJ_ENCODING_ZIPLIST) {
2926 unsigned char *zl = key->value->ptr;
2927 unsigned char *eptr = key->u.zset.current;
2928 unsigned char *next;
2929 next = ziplistNext(zl,eptr); /* Skip element. */
2930 if (next) next = ziplistNext(zl,next); /* Skip score. */
2931 if (next == NULL) {
2932 key->u.zset.er = 1;
2933 return 0;
2934 } else {
2935 /* Are we still within the range? */
2936 if (key->u.zset.type == REDISMODULE_ZSET_RANGE_SCORE) {
2937 /* Fetch the next element score for the
2938 * range check. */
2939 unsigned char *saved_next = next;
2940 next = ziplistNext(zl,next); /* Skip next element. */
2941 double score = zzlGetScore(next); /* Obtain the next score. */
2942 if (!zslValueLteMax(score,&key->u.zset.rs)) {
2943 key->u.zset.er = 1;
2944 return 0;
2945 }
2946 next = saved_next;
2947 } else if (key->u.zset.type == REDISMODULE_ZSET_RANGE_LEX) {
2948 if (!zzlLexValueLteMax(next,&key->u.zset.lrs)) {
2949 key->u.zset.er = 1;
2950 return 0;
2951 }
2952 }
2953 key->u.zset.current = next;
2954 return 1;
2955 }
2956 } else if (key->value->encoding == OBJ_ENCODING_SKIPLIST) {
2957 zskiplistNode *ln = key->u.zset.current, *next = ln->level[0].forward;
2958 if (next == NULL) {
2959 key->u.zset.er = 1;
2960 return 0;
2961 } else {
2962 /* Are we still within the range? */
2963 if (key->u.zset.type == REDISMODULE_ZSET_RANGE_SCORE &&
2964 !zslValueLteMax(next->score,&key->u.zset.rs))
2965 {
2966 key->u.zset.er = 1;
2967 return 0;
2968 } else if (key->u.zset.type == REDISMODULE_ZSET_RANGE_LEX) {
2969 if (!zslLexValueLteMax(next->ele,&key->u.zset.lrs)) {
2970 key->u.zset.er = 1;
2971 return 0;
2972 }
2973 }
2974 key->u.zset.current = next;
2975 return 1;
2976 }
2977 } else {
2978 serverPanic("Unsupported zset encoding");

Callers

nothing calls this directly

Calls 5

ziplistNextFunction · 0.85
zzlGetScoreFunction · 0.85
zslValueLteMaxFunction · 0.85
zzlLexValueLteMaxFunction · 0.85
zslLexValueLteMaxFunction · 0.85

Tested by

no test coverage detected