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

Function genericZrangebyrankCommand

app/redis-6.2.6/src/t_zset.c:3030–3118  ·  view source on GitHub ↗

This command implements ZRANGE, ZREVRANGE. */

Source from the content-addressed store, hash-verified

3028
3029/* This command implements ZRANGE, ZREVRANGE. */
3030void genericZrangebyrankCommand(zrange_result_handler *handler,
3031 robj *zobj, long start, long end, int withscores, int reverse) {
3032
3033 client *c = handler->client;
3034 long llen;
3035 long rangelen;
3036 size_t result_cardinality;
3037
3038 /* Sanitize indexes. */
3039 llen = zsetLength(zobj);
3040 if (start < 0) start = llen+start;
3041 if (end < 0) end = llen+end;
3042 if (start < 0) start = 0;
3043
3044 handler->beginResultEmission(handler);
3045
3046 /* Invariant: start >= 0, so this test will be true when end < 0.
3047 * The range is empty when start > end or start >= length. */
3048 if (start > end || start >= llen) {
3049 handler->finalizeResultEmission(handler, 0);
3050 return;
3051 }
3052 if (end >= llen) end = llen-1;
3053 rangelen = (end-start)+1;
3054 result_cardinality = rangelen;
3055
3056 if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {
3057 unsigned char *zl = zobj->ptr;
3058 unsigned char *eptr, *sptr;
3059 unsigned char *vstr;
3060 unsigned int vlen;
3061 long long vlong;
3062 double score = 0.0;
3063
3064 if (reverse)
3065 eptr = ziplistIndex(zl,-2-(2*start));
3066 else
3067 eptr = ziplistIndex(zl,2*start);
3068
3069 serverAssertWithInfo(c,zobj,eptr != NULL);
3070 sptr = ziplistNext(zl,eptr);
3071
3072 while (rangelen--) {
3073 serverAssertWithInfo(c,zobj,eptr != NULL && sptr != NULL);
3074 serverAssertWithInfo(c,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));
3075
3076 if (withscores) /* don't bother to extract the score if it's gonna be ignored. */
3077 score = zzlGetScore(sptr);
3078
3079 if (vstr == NULL) {
3080 handler->emitResultFromLongLong(handler, vlong, score);
3081 } else {
3082 handler->emitResultFromCBuffer(handler, vstr, vlen, score);
3083 }
3084
3085 if (reverse)
3086 zzlPrev(zl,&eptr,&sptr);
3087 else

Callers 1

zrangeGenericCommandFunction · 0.85

Calls 9

zsetLengthFunction · 0.85
ziplistIndexFunction · 0.85
ziplistNextFunction · 0.85
ziplistGetFunction · 0.85
zzlGetScoreFunction · 0.85
zzlPrevFunction · 0.85
zzlNextFunction · 0.85
zslGetElementByRankFunction · 0.85
sdslenFunction · 0.85

Tested by

no test coverage detected