]> git.saurik.com Git - redis.git/blame - src/t_zset.c
Support dual encoding for more commands
[redis.git] / src / t_zset.c
CommitLineData
e2641e09 1#include "redis.h"
2
3#include <math.h>
4
5/*-----------------------------------------------------------------------------
6 * Sorted set API
7 *----------------------------------------------------------------------------*/
8
9/* ZSETs are ordered sets using two data structures to hold the same elements
10 * in order to get O(log(N)) INSERT and REMOVE operations into a sorted
11 * data structure.
12 *
13 * The elements are added to an hash table mapping Redis objects to scores.
14 * At the same time the elements are added to a skip list mapping scores
15 * to Redis objects (so objects are sorted by scores in this "view"). */
16
17/* This skiplist implementation is almost a C translation of the original
18 * algorithm described by William Pugh in "Skip Lists: A Probabilistic
19 * Alternative to Balanced Trees", modified in three ways:
20 * a) this implementation allows for repeated values.
21 * b) the comparison is not just by key (our 'score') but by satellite data.
22 * c) there is a back pointer, so it's a doubly linked list with the back
23 * pointers being only at "level 1". This allows to traverse the list
24 * from tail to head, useful for ZREVRANGE. */
25
26zskiplistNode *zslCreateNode(int level, double score, robj *obj) {
2159782b 27 zskiplistNode *zn = zmalloc(sizeof(*zn)+level*sizeof(struct zskiplistLevel));
e2641e09 28 zn->score = score;
29 zn->obj = obj;
30 return zn;
31}
32
33zskiplist *zslCreate(void) {
34 int j;
35 zskiplist *zsl;
36
37 zsl = zmalloc(sizeof(*zsl));
38 zsl->level = 1;
39 zsl->length = 0;
40 zsl->header = zslCreateNode(ZSKIPLIST_MAXLEVEL,0,NULL);
41 for (j = 0; j < ZSKIPLIST_MAXLEVEL; j++) {
2159782b
PN
42 zsl->header->level[j].forward = NULL;
43 zsl->header->level[j].span = 0;
e2641e09 44 }
45 zsl->header->backward = NULL;
46 zsl->tail = NULL;
47 return zsl;
48}
49
50void zslFreeNode(zskiplistNode *node) {
51 decrRefCount(node->obj);
e2641e09 52 zfree(node);
53}
54
55void zslFree(zskiplist *zsl) {
2159782b 56 zskiplistNode *node = zsl->header->level[0].forward, *next;
e2641e09 57
e2641e09 58 zfree(zsl->header);
59 while(node) {
2159782b 60 next = node->level[0].forward;
e2641e09 61 zslFreeNode(node);
62 node = next;
63 }
64 zfree(zsl);
65}
66
67int zslRandomLevel(void) {
68 int level = 1;
69 while ((random()&0xFFFF) < (ZSKIPLIST_P * 0xFFFF))
70 level += 1;
71 return (level<ZSKIPLIST_MAXLEVEL) ? level : ZSKIPLIST_MAXLEVEL;
72}
73
69ef89f2 74zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj) {
e2641e09 75 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
76 unsigned int rank[ZSKIPLIST_MAXLEVEL];
77 int i, level;
78
79 x = zsl->header;
80 for (i = zsl->level-1; i >= 0; i--) {
81 /* store rank that is crossed to reach the insert position */
82 rank[i] = i == (zsl->level-1) ? 0 : rank[i+1];
2159782b
PN
83 while (x->level[i].forward &&
84 (x->level[i].forward->score < score ||
85 (x->level[i].forward->score == score &&
86 compareStringObjects(x->level[i].forward->obj,obj) < 0))) {
87 rank[i] += x->level[i].span;
88 x = x->level[i].forward;
e2641e09 89 }
90 update[i] = x;
91 }
92 /* we assume the key is not already inside, since we allow duplicated
93 * scores, and the re-insertion of score and redis object should never
94 * happpen since the caller of zslInsert() should test in the hash table
95 * if the element is already inside or not. */
96 level = zslRandomLevel();
97 if (level > zsl->level) {
98 for (i = zsl->level; i < level; i++) {
99 rank[i] = 0;
100 update[i] = zsl->header;
2159782b 101 update[i]->level[i].span = zsl->length;
e2641e09 102 }
103 zsl->level = level;
104 }
105 x = zslCreateNode(level,score,obj);
106 for (i = 0; i < level; i++) {
2159782b
PN
107 x->level[i].forward = update[i]->level[i].forward;
108 update[i]->level[i].forward = x;
e2641e09 109
110 /* update span covered by update[i] as x is inserted here */
2159782b
PN
111 x->level[i].span = update[i]->level[i].span - (rank[0] - rank[i]);
112 update[i]->level[i].span = (rank[0] - rank[i]) + 1;
e2641e09 113 }
114
115 /* increment span for untouched levels */
116 for (i = level; i < zsl->level; i++) {
2159782b 117 update[i]->level[i].span++;
e2641e09 118 }
119
120 x->backward = (update[0] == zsl->header) ? NULL : update[0];
2159782b
PN
121 if (x->level[0].forward)
122 x->level[0].forward->backward = x;
e2641e09 123 else
124 zsl->tail = x;
125 zsl->length++;
69ef89f2 126 return x;
e2641e09 127}
128
129/* Internal function used by zslDelete, zslDeleteByScore and zslDeleteByRank */
130void zslDeleteNode(zskiplist *zsl, zskiplistNode *x, zskiplistNode **update) {
131 int i;
132 for (i = 0; i < zsl->level; i++) {
2159782b
PN
133 if (update[i]->level[i].forward == x) {
134 update[i]->level[i].span += x->level[i].span - 1;
135 update[i]->level[i].forward = x->level[i].forward;
e2641e09 136 } else {
2159782b 137 update[i]->level[i].span -= 1;
e2641e09 138 }
139 }
2159782b
PN
140 if (x->level[0].forward) {
141 x->level[0].forward->backward = x->backward;
e2641e09 142 } else {
143 zsl->tail = x->backward;
144 }
2159782b 145 while(zsl->level > 1 && zsl->header->level[zsl->level-1].forward == NULL)
e2641e09 146 zsl->level--;
147 zsl->length--;
148}
149
150/* Delete an element with matching score/object from the skiplist. */
151int zslDelete(zskiplist *zsl, double score, robj *obj) {
152 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
153 int i;
154
155 x = zsl->header;
156 for (i = zsl->level-1; i >= 0; i--) {
2159782b
PN
157 while (x->level[i].forward &&
158 (x->level[i].forward->score < score ||
159 (x->level[i].forward->score == score &&
160 compareStringObjects(x->level[i].forward->obj,obj) < 0)))
161 x = x->level[i].forward;
e2641e09 162 update[i] = x;
163 }
164 /* We may have multiple elements with the same score, what we need
165 * is to find the element with both the right score and object. */
2159782b 166 x = x->level[0].forward;
e2641e09 167 if (x && score == x->score && equalStringObjects(x->obj,obj)) {
168 zslDeleteNode(zsl, x, update);
169 zslFreeNode(x);
170 return 1;
171 } else {
172 return 0; /* not found */
173 }
174 return 0; /* not found */
175}
176
91504b6c
PN
177/* Struct to hold a inclusive/exclusive range spec. */
178typedef struct {
179 double min, max;
180 int minex, maxex; /* are min or max exclusive? */
181} zrangespec;
182
45290ad9 183static int zslValueGteMin(double value, zrangespec *spec) {
22b9bf15
PN
184 return spec->minex ? (value > spec->min) : (value >= spec->min);
185}
186
45290ad9 187static int zslValueLteMax(double value, zrangespec *spec) {
22b9bf15
PN
188 return spec->maxex ? (value < spec->max) : (value <= spec->max);
189}
190
df278b8b 191static int zslValueInRange(double value, zrangespec *spec) {
45290ad9 192 return zslValueGteMin(value,spec) && zslValueLteMax(value,spec);
22b9bf15
PN
193}
194
195/* Returns if there is a part of the zset is in range. */
196int zslIsInRange(zskiplist *zsl, zrangespec *range) {
197 zskiplistNode *x;
198
8e1b3277
PN
199 /* Test for ranges that will always be empty. */
200 if (range->min > range->max ||
201 (range->min == range->max && (range->minex || range->maxex)))
202 return 0;
22b9bf15 203 x = zsl->tail;
45290ad9 204 if (x == NULL || !zslValueGteMin(x->score,range))
22b9bf15
PN
205 return 0;
206 x = zsl->header->level[0].forward;
45290ad9 207 if (x == NULL || !zslValueLteMax(x->score,range))
22b9bf15
PN
208 return 0;
209 return 1;
210}
211
212/* Find the first node that is contained in the specified range.
213 * Returns NULL when no element is contained in the range. */
214zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec range) {
215 zskiplistNode *x;
216 int i;
217
218 /* If everything is out of range, return early. */
219 if (!zslIsInRange(zsl,&range)) return NULL;
220
221 x = zsl->header;
222 for (i = zsl->level-1; i >= 0; i--) {
223 /* Go forward while *OUT* of range. */
224 while (x->level[i].forward &&
45290ad9 225 !zslValueGteMin(x->level[i].forward->score,&range))
22b9bf15
PN
226 x = x->level[i].forward;
227 }
228
229 /* The tail is in range, so the previous block should always return a
230 * node that is non-NULL and the last one to be out of range. */
231 x = x->level[0].forward;
232 redisAssert(x != NULL && zslValueInRange(x->score,&range));
233 return x;
234}
235
236/* Find the last node that is contained in the specified range.
237 * Returns NULL when no element is contained in the range. */
238zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec range) {
239 zskiplistNode *x;
240 int i;
241
242 /* If everything is out of range, return early. */
243 if (!zslIsInRange(zsl,&range)) return NULL;
244
245 x = zsl->header;
246 for (i = zsl->level-1; i >= 0; i--) {
247 /* Go forward while *IN* range. */
248 while (x->level[i].forward &&
45290ad9 249 zslValueLteMax(x->level[i].forward->score,&range))
22b9bf15
PN
250 x = x->level[i].forward;
251 }
252
253 /* The header is in range, so the previous block should always return a
254 * node that is non-NULL and in range. */
255 redisAssert(x != NULL && zslValueInRange(x->score,&range));
256 return x;
257}
258
e2641e09 259/* Delete all the elements with score between min and max from the skiplist.
260 * Min and mx are inclusive, so a score >= min || score <= max is deleted.
261 * Note that this function takes the reference to the hash table view of the
262 * sorted set, in order to remove the elements from the hash table too. */
91504b6c 263unsigned long zslDeleteRangeByScore(zskiplist *zsl, zrangespec range, dict *dict) {
e2641e09 264 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
265 unsigned long removed = 0;
266 int i;
267
268 x = zsl->header;
269 for (i = zsl->level-1; i >= 0; i--) {
91504b6c
PN
270 while (x->level[i].forward && (range.minex ?
271 x->level[i].forward->score <= range.min :
272 x->level[i].forward->score < range.min))
273 x = x->level[i].forward;
e2641e09 274 update[i] = x;
275 }
91504b6c
PN
276
277 /* Current node is the last with score < or <= min. */
2159782b 278 x = x->level[0].forward;
91504b6c
PN
279
280 /* Delete nodes while in range. */
281 while (x && (range.maxex ? x->score < range.max : x->score <= range.max)) {
2159782b 282 zskiplistNode *next = x->level[0].forward;
69ef89f2 283 zslDeleteNode(zsl,x,update);
e2641e09 284 dictDelete(dict,x->obj);
285 zslFreeNode(x);
286 removed++;
287 x = next;
288 }
91504b6c 289 return removed;
e2641e09 290}
291
292/* Delete all the elements with rank between start and end from the skiplist.
293 * Start and end are inclusive. Note that start and end need to be 1-based */
294unsigned long zslDeleteRangeByRank(zskiplist *zsl, unsigned int start, unsigned int end, dict *dict) {
295 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
296 unsigned long traversed = 0, removed = 0;
297 int i;
298
299 x = zsl->header;
300 for (i = zsl->level-1; i >= 0; i--) {
2159782b
PN
301 while (x->level[i].forward && (traversed + x->level[i].span) < start) {
302 traversed += x->level[i].span;
303 x = x->level[i].forward;
e2641e09 304 }
305 update[i] = x;
306 }
307
308 traversed++;
2159782b 309 x = x->level[0].forward;
e2641e09 310 while (x && traversed <= end) {
2159782b 311 zskiplistNode *next = x->level[0].forward;
69ef89f2 312 zslDeleteNode(zsl,x,update);
e2641e09 313 dictDelete(dict,x->obj);
314 zslFreeNode(x);
315 removed++;
316 traversed++;
317 x = next;
318 }
319 return removed;
320}
321
e2641e09 322/* Find the rank for an element by both score and key.
323 * Returns 0 when the element cannot be found, rank otherwise.
324 * Note that the rank is 1-based due to the span of zsl->header to the
325 * first element. */
a3004773 326unsigned long zslGetRank(zskiplist *zsl, double score, robj *o) {
e2641e09 327 zskiplistNode *x;
328 unsigned long rank = 0;
329 int i;
330
331 x = zsl->header;
332 for (i = zsl->level-1; i >= 0; i--) {
2159782b
PN
333 while (x->level[i].forward &&
334 (x->level[i].forward->score < score ||
335 (x->level[i].forward->score == score &&
336 compareStringObjects(x->level[i].forward->obj,o) <= 0))) {
337 rank += x->level[i].span;
338 x = x->level[i].forward;
e2641e09 339 }
340
341 /* x might be equal to zsl->header, so test if obj is non-NULL */
342 if (x->obj && equalStringObjects(x->obj,o)) {
343 return rank;
344 }
345 }
346 return 0;
347}
348
349/* Finds an element by its rank. The rank argument needs to be 1-based. */
a3004773 350zskiplistNode* zslGetElementByRank(zskiplist *zsl, unsigned long rank) {
e2641e09 351 zskiplistNode *x;
352 unsigned long traversed = 0;
353 int i;
354
355 x = zsl->header;
356 for (i = zsl->level-1; i >= 0; i--) {
2159782b 357 while (x->level[i].forward && (traversed + x->level[i].span) <= rank)
e2641e09 358 {
2159782b
PN
359 traversed += x->level[i].span;
360 x = x->level[i].forward;
e2641e09 361 }
362 if (traversed == rank) {
363 return x;
364 }
365 }
366 return NULL;
367}
368
25bb8a44 369/* Populate the rangespec according to the objects min and max. */
7236fdb2
PN
370static int zslParseRange(robj *min, robj *max, zrangespec *spec) {
371 char *eptr;
25bb8a44
PN
372 spec->minex = spec->maxex = 0;
373
374 /* Parse the min-max interval. If one of the values is prefixed
375 * by the "(" character, it's considered "open". For instance
376 * ZRANGEBYSCORE zset (1.5 (2.5 will match min < x < max
377 * ZRANGEBYSCORE zset 1.5 2.5 will instead match min <= x <= max */
378 if (min->encoding == REDIS_ENCODING_INT) {
379 spec->min = (long)min->ptr;
380 } else {
381 if (((char*)min->ptr)[0] == '(') {
7236fdb2
PN
382 spec->min = strtod((char*)min->ptr+1,&eptr);
383 if (eptr[0] != '\0' || isnan(spec->min)) return REDIS_ERR;
25bb8a44
PN
384 spec->minex = 1;
385 } else {
7236fdb2
PN
386 spec->min = strtod((char*)min->ptr,&eptr);
387 if (eptr[0] != '\0' || isnan(spec->min)) return REDIS_ERR;
25bb8a44
PN
388 }
389 }
390 if (max->encoding == REDIS_ENCODING_INT) {
391 spec->max = (long)max->ptr;
392 } else {
393 if (((char*)max->ptr)[0] == '(') {
7236fdb2
PN
394 spec->max = strtod((char*)max->ptr+1,&eptr);
395 if (eptr[0] != '\0' || isnan(spec->max)) return REDIS_ERR;
25bb8a44
PN
396 spec->maxex = 1;
397 } else {
7236fdb2
PN
398 spec->max = strtod((char*)max->ptr,&eptr);
399 if (eptr[0] != '\0' || isnan(spec->max)) return REDIS_ERR;
25bb8a44
PN
400 }
401 }
402
403 return REDIS_OK;
404}
405
21c5b508
PN
406/*-----------------------------------------------------------------------------
407 * Ziplist-backed sorted set API
408 *----------------------------------------------------------------------------*/
409
410double zzlGetScore(unsigned char *sptr) {
411 unsigned char *vstr;
412 unsigned int vlen;
413 long long vlong;
414 char buf[128];
415 double score;
416
417 redisAssert(sptr != NULL);
418 redisAssert(ziplistGet(sptr,&vstr,&vlen,&vlong));
419
420 if (vstr) {
421 memcpy(buf,vstr,vlen);
422 buf[vlen] = '\0';
423 score = strtod(buf,NULL);
424 } else {
425 score = vlong;
426 }
427
428 return score;
429}
430
431/* Compare element in sorted set with given element. */
432int zzlCompareElements(unsigned char *eptr, unsigned char *cstr, unsigned int clen) {
433 unsigned char *vstr;
434 unsigned int vlen;
435 long long vlong;
436 unsigned char vbuf[32];
437 int minlen, cmp;
438
439 redisAssert(ziplistGet(eptr,&vstr,&vlen,&vlong));
440 if (vstr == NULL) {
441 /* Store string representation of long long in buf. */
442 vlen = ll2string((char*)vbuf,sizeof(vbuf),vlong);
443 vstr = vbuf;
444 }
445
446 minlen = (vlen < clen) ? vlen : clen;
447 cmp = memcmp(vstr,cstr,minlen);
448 if (cmp == 0) return vlen-clen;
449 return cmp;
450}
451
9f9b60f9 452unsigned int zzlLength(robj *zobj) {
0b10e104
PN
453 unsigned char *zl = zobj->ptr;
454 return ziplistLen(zl)/2;
455}
456
4c5f0966
PN
457/* Move to next entry based on the values in eptr and sptr. Both are set to
458 * NULL when there is no next entry. */
459void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr) {
460 unsigned char *_eptr, *_sptr;
461 redisAssert(*eptr != NULL && *sptr != NULL);
462
463 _eptr = ziplistNext(zl,*sptr);
464 if (_eptr != NULL) {
465 _sptr = ziplistNext(zl,_eptr);
466 redisAssert(_sptr != NULL);
467 } else {
468 /* No next entry. */
469 _sptr = NULL;
470 }
471
472 *eptr = _eptr;
473 *sptr = _sptr;
474}
475
476/* Move to the previous entry based on the values in eptr and sptr. Both are
477 * set to NULL when there is no next entry. */
478void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr) {
479 unsigned char *_eptr, *_sptr;
480 redisAssert(*eptr != NULL && *sptr != NULL);
481
482 _sptr = ziplistPrev(zl,*eptr);
483 if (_sptr != NULL) {
484 _eptr = ziplistPrev(zl,_sptr);
485 redisAssert(_eptr != NULL);
486 } else {
487 /* No previous entry. */
488 _eptr = NULL;
489 }
490
491 *eptr = _eptr;
492 *sptr = _sptr;
493}
494
4a14dbba
PN
495/* Returns if there is a part of the zset is in range. Should only be used
496 * internally by zzlFirstInRange and zzlLastInRange. */
497int zzlIsInRange(unsigned char *zl, zrangespec *range) {
498 unsigned char *p;
499 double score;
500
501 /* Test for ranges that will always be empty. */
502 if (range->min > range->max ||
503 (range->min == range->max && (range->minex || range->maxex)))
504 return 0;
505
506 p = ziplistIndex(zl,-1); /* Last score. */
507 redisAssert(p != NULL);
508 score = zzlGetScore(p);
509 if (!zslValueGteMin(score,range))
510 return 0;
511
512 p = ziplistIndex(zl,1); /* First score. */
513 redisAssert(p != NULL);
514 score = zzlGetScore(p);
515 if (!zslValueLteMax(score,range))
516 return 0;
517
518 return 1;
519}
520
521/* Find pointer to the first element contained in the specified range.
522 * Returns NULL when no element is contained in the range. */
523unsigned char *zzlFirstInRange(robj *zobj, zrangespec range) {
524 unsigned char *zl = zobj->ptr;
525 unsigned char *eptr = ziplistIndex(zl,0), *sptr;
526 double score;
527
528 /* If everything is out of range, return early. */
529 if (!zzlIsInRange(zl,&range)) return NULL;
530
531 while (eptr != NULL) {
532 sptr = ziplistNext(zl,eptr);
533 redisAssert(sptr != NULL);
534
535 score = zzlGetScore(sptr);
536 if (zslValueGteMin(score,&range))
537 return eptr;
538
539 /* Move to next element. */
540 eptr = ziplistNext(zl,sptr);
541 }
542
543 return NULL;
544}
545
546/* Find pointer to the last element contained in the specified range.
547 * Returns NULL when no element is contained in the range. */
548unsigned char *zzlLastInRange(robj *zobj, zrangespec range) {
549 unsigned char *zl = zobj->ptr;
550 unsigned char *eptr = ziplistIndex(zl,-2), *sptr;
551 double score;
552
553 /* If everything is out of range, return early. */
554 if (!zzlIsInRange(zl,&range)) return NULL;
555
556 while (eptr != NULL) {
557 sptr = ziplistNext(zl,eptr);
558 redisAssert(sptr != NULL);
559
560 score = zzlGetScore(sptr);
561 if (zslValueLteMax(score,&range))
562 return eptr;
563
564 /* Move to previous element by moving to the score of previous element.
565 * When this returns NULL, we know there also is no element. */
566 sptr = ziplistPrev(zl,eptr);
567 if (sptr != NULL)
568 redisAssert((eptr = ziplistPrev(zl,sptr)) != NULL);
569 else
570 eptr = NULL;
571 }
572
573 return NULL;
574}
575
21c5b508
PN
576unsigned char *zzlFind(robj *zobj, robj *ele, double *score) {
577 unsigned char *zl = zobj->ptr;
578 unsigned char *eptr = ziplistIndex(zl,0), *sptr;
579
580 ele = getDecodedObject(ele);
581 while (eptr != NULL) {
582 sptr = ziplistNext(zl,eptr);
583 redisAssert(sptr != NULL);
584
585 if (ziplistCompare(eptr,ele->ptr,sdslen(ele->ptr))) {
586 /* Matching element, pull out score. */
0b10e104 587 if (score != NULL) *score = zzlGetScore(sptr);
21c5b508
PN
588 decrRefCount(ele);
589 return eptr;
590 }
591
592 /* Move to next element. */
593 eptr = ziplistNext(zl,sptr);
594 }
595
596 decrRefCount(ele);
597 return NULL;
598}
599
600/* Delete (element,score) pair from ziplist. Use local copy of eptr because we
601 * don't want to modify the one given as argument. */
602int zzlDelete(robj *zobj, unsigned char *eptr) {
603 unsigned char *zl = zobj->ptr;
604 unsigned char *p = eptr;
605
606 /* TODO: add function to ziplist API to delete N elements from offset. */
607 zl = ziplistDelete(zl,&p);
608 zl = ziplistDelete(zl,&p);
609 zobj->ptr = zl;
610 return REDIS_OK;
611}
612
613int zzlInsertAt(robj *zobj, robj *ele, double score, unsigned char *eptr) {
614 unsigned char *zl = zobj->ptr;
615 unsigned char *sptr;
616 char scorebuf[128];
617 int scorelen;
618 int offset;
619
620 redisAssert(ele->encoding == REDIS_ENCODING_RAW);
621 scorelen = d2string(scorebuf,sizeof(scorebuf),score);
622 if (eptr == NULL) {
623 zl = ziplistPush(zl,ele->ptr,sdslen(ele->ptr),ZIPLIST_TAIL);
624 zl = ziplistPush(zl,(unsigned char*)scorebuf,scorelen,ZIPLIST_TAIL);
625 } else {
626 /* Keep offset relative to zl, as it might be re-allocated. */
627 offset = eptr-zl;
628 zl = ziplistInsert(zl,eptr,ele->ptr,sdslen(ele->ptr));
629 eptr = zl+offset;
630
631 /* Insert score after the element. */
632 redisAssert((sptr = ziplistNext(zl,eptr)) != NULL);
633 zl = ziplistInsert(zl,sptr,(unsigned char*)scorebuf,scorelen);
634 }
635
636 zobj->ptr = zl;
637 return REDIS_OK;
638}
639
640/* Insert (element,score) pair in ziplist. This function assumes the element is
641 * not yet present in the list. */
642int zzlInsert(robj *zobj, robj *ele, double score) {
643 unsigned char *zl = zobj->ptr;
644 unsigned char *eptr = ziplistIndex(zl,0), *sptr;
645 double s;
21c5b508
PN
646
647 ele = getDecodedObject(ele);
648 while (eptr != NULL) {
649 sptr = ziplistNext(zl,eptr);
650 redisAssert(sptr != NULL);
651 s = zzlGetScore(sptr);
652
653 if (s > score) {
654 /* First element with score larger than score for element to be
655 * inserted. This means we should take its spot in the list to
656 * maintain ordering. */
21c5b508
PN
657 zzlInsertAt(zobj,ele,score,eptr);
658 break;
8218db3d
PN
659 } else if (s == score) {
660 /* Ensure lexicographical ordering for elements. */
d1c920c5 661 if (zzlCompareElements(eptr,ele->ptr,sdslen(ele->ptr)) > 0) {
8218db3d
PN
662 zzlInsertAt(zobj,ele,score,eptr);
663 break;
664 }
21c5b508
PN
665 }
666
667 /* Move to next element. */
668 eptr = ziplistNext(zl,sptr);
669 }
670
671 /* Push on tail of list when it was not yet inserted. */
8218db3d
PN
672 if (eptr == NULL)
673 zzlInsertAt(zobj,ele,score,NULL);
21c5b508
PN
674
675 decrRefCount(ele);
676 return REDIS_OK;
677}
25bb8a44 678
4a14dbba
PN
679unsigned long zzlDeleteRangeByScore(robj *zobj, zrangespec range) {
680 unsigned char *zl = zobj->ptr;
681 unsigned char *eptr, *sptr;
682 double score;
683 unsigned long deleted = 0;
684
685 eptr = zzlFirstInRange(zobj,range);
686 if (eptr == NULL) return deleted;
687
688
689 /* When the tail of the ziplist is deleted, eptr will point to the sentinel
690 * byte and ziplistNext will return NULL. */
691 while ((sptr = ziplistNext(zl,eptr)) != NULL) {
692 score = zzlGetScore(sptr);
693 if (zslValueLteMax(score,&range)) {
694 /* Delete both the element and the score. */
695 zl = ziplistDelete(zl,&eptr);
696 zl = ziplistDelete(zl,&eptr);
697 deleted++;
698 } else {
699 /* No longer in range. */
700 break;
701 }
702 }
703
704 return deleted;
705}
706
63b7b7fb
PN
707/* Delete all the elements with rank between start and end from the skiplist.
708 * Start and end are inclusive. Note that start and end need to be 1-based */
709unsigned long zzlDeleteRangeByRank(robj *zobj, unsigned int start, unsigned int end) {
710 unsigned int num = (end-start)+1;
711 zobj->ptr = ziplistDeleteRange(zobj->ptr,2*(start-1),2*num);
712 return num;
713}
714
5d1b4fb6
PN
715/*-----------------------------------------------------------------------------
716 * Common sorted set API
717 *----------------------------------------------------------------------------*/
718
719int zsLength(robj *zobj) {
720 int length = -1;
721 if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
722 length = zzlLength(zobj);
723 } else if (zobj->encoding == REDIS_ENCODING_RAW) {
724 length = ((zset*)zobj->ptr)->zsl->length;
725 } else {
726 redisPanic("Unknown sorted set encoding");
727 }
728 return length;
729}
730
e2641e09 731/*-----------------------------------------------------------------------------
732 * Sorted set commands
733 *----------------------------------------------------------------------------*/
734
69ef89f2 735/* This generic command implements both ZADD and ZINCRBY. */
3ca7532a 736void zaddGenericCommand(redisClient *c, int incr) {
21c5b508 737 static char *nanerr = "resulting score is not a number (NaN)";
3ca7532a
PN
738 robj *key = c->argv[1];
739 robj *ele;
21c5b508
PN
740 robj *zobj;
741 robj *curobj;
3ca7532a
PN
742 double score, curscore = 0.0;
743
744 if (getDoubleFromObjectOrReply(c,c->argv[2],&score,NULL) != REDIS_OK)
745 return;
21c5b508
PN
746
747 zobj = lookupKeyWrite(c->db,key);
748 if (zobj == NULL) {
749 zobj = createZsetZiplistObject();
750 dbAdd(c->db,key,zobj);
e2641e09 751 } else {
21c5b508 752 if (zobj->type != REDIS_ZSET) {
e2641e09 753 addReply(c,shared.wrongtypeerr);
754 return;
755 }
756 }
e2641e09 757
21c5b508
PN
758 if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
759 unsigned char *eptr;
760
3ca7532a
PN
761 /* Prefer non-encoded element when dealing with ziplists. */
762 ele = c->argv[3];
21c5b508
PN
763 if ((eptr = zzlFind(zobj,ele,&curscore)) != NULL) {
764 if (incr) {
765 score += curscore;
766 if (isnan(score)) {
767 addReplyError(c,nanerr);
768 /* Don't need to check if the sorted set is empty, because
769 * we know it has at least one element. */
770 return;
771 }
772 }
773
774 /* Remove and re-insert when score changed. */
775 if (score != curscore) {
776 redisAssert(zzlDelete(zobj,eptr) == REDIS_OK);
777 redisAssert(zzlInsert(zobj,ele,score) == REDIS_OK);
778
779 signalModifiedKey(c->db,key);
780 server.dirty++;
781 }
782
783 if (incr) /* ZINCRBY */
784 addReplyDouble(c,score);
785 else /* ZADD */
786 addReply(c,shared.czero);
787 } else {
788 redisAssert(zzlInsert(zobj,ele,score) == REDIS_OK);
69ef89f2 789
21c5b508
PN
790 signalModifiedKey(c->db,key);
791 server.dirty++;
69ef89f2 792
21c5b508
PN
793 if (incr) /* ZINCRBY */
794 addReplyDouble(c,score);
795 else /* ZADD */
796 addReply(c,shared.cone);
797 }
798 } else if (zobj->encoding == REDIS_ENCODING_RAW) {
799 zset *zs = zobj->ptr;
800 zskiplistNode *znode;
e2641e09 801 dictEntry *de;
e2641e09 802
3ca7532a 803 ele = c->argv[3] = tryObjectEncoding(c->argv[3]);
e2641e09 804 de = dictFind(zs->dict,ele);
21c5b508
PN
805 if (de != NULL) {
806 curobj = dictGetEntryKey(de);
807 curscore = *(double*)dictGetEntryVal(de);
808
809 if (incr) {
810 score += curscore;
811 if (isnan(score)) {
812 addReplyError(c,nanerr);
813 /* Don't need to check if the sorted set is empty, because
814 * we know it has at least one element. */
815 return;
816 }
817 }
818
819 /* Remove and re-insert when score changed. We can safely delete
820 * the key object from the skiplist, since the dictionary still has
821 * a reference to it. */
822 if (score != curscore) {
823 redisAssert(zslDelete(zs->zsl,curscore,curobj));
824 znode = zslInsert(zs->zsl,score,curobj);
825 incrRefCount(curobj); /* Re-inserted in skiplist. */
826 dictGetEntryVal(de) = &znode->score; /* Update score ptr. */
827
828 signalModifiedKey(c->db,key);
829 server.dirty++;
830 }
831
832 if (incr) /* ZINCRBY */
833 addReplyDouble(c,score);
834 else /* ZADD */
835 addReply(c,shared.czero);
836 } else {
837 znode = zslInsert(zs->zsl,score,ele);
838 incrRefCount(ele); /* Inserted in skiplist. */
839 redisAssert(dictAdd(zs->dict,ele,&znode->score) == DICT_OK);
840 incrRefCount(ele); /* Added to dictionary. */
841
842 signalModifiedKey(c->db,key);
e2641e09 843 server.dirty++;
21c5b508
PN
844
845 if (incr) /* ZINCRBY */
846 addReplyDouble(c,score);
847 else /* ZADD */
848 addReply(c,shared.cone);
e2641e09 849 }
21c5b508
PN
850 } else {
851 redisPanic("Unknown sorted set encoding");
e2641e09 852 }
853}
854
855void zaddCommand(redisClient *c) {
3ca7532a 856 zaddGenericCommand(c,0);
e2641e09 857}
858
859void zincrbyCommand(redisClient *c) {
3ca7532a 860 zaddGenericCommand(c,1);
e2641e09 861}
862
863void zremCommand(redisClient *c) {
0b10e104
PN
864 robj *key = c->argv[1];
865 robj *ele = c->argv[2];
866 robj *zobj;
867
868 if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL ||
869 checkType(c,zobj,REDIS_ZSET)) return;
870
871 if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
872 unsigned char *eptr;
873
874 if ((eptr = zzlFind(zobj,ele,NULL)) != NULL) {
875 redisAssert(zzlDelete(zobj,eptr) == REDIS_OK);
876 if (zzlLength(zobj) == 0) dbDelete(c->db,key);
877 } else {
878 addReply(c,shared.czero);
879 return;
880 }
881 } else if (zobj->encoding == REDIS_ENCODING_RAW) {
882 zset *zs = zobj->ptr;
883 dictEntry *de;
884 double score;
885
886 de = dictFind(zs->dict,ele);
887 if (de != NULL) {
888 /* Delete from the skiplist */
889 score = *(double*)dictGetEntryVal(de);
890 redisAssert(zslDelete(zs->zsl,score,ele));
891
892 /* Delete from the hash table */
893 dictDelete(zs->dict,ele);
894 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
895 if (dictSize(zs->dict) == 0) dbDelete(c->db,key);
896 } else {
897 addReply(c,shared.czero);
898 return;
899 }
900 } else {
901 redisPanic("Unknown sorted set encoding");
e2641e09 902 }
e2641e09 903
0b10e104 904 signalModifiedKey(c->db,key);
e2641e09 905 server.dirty++;
906 addReply(c,shared.cone);
907}
908
909void zremrangebyscoreCommand(redisClient *c) {
4a14dbba
PN
910 robj *key = c->argv[1];
911 robj *zobj;
91504b6c 912 zrangespec range;
4a14dbba 913 unsigned long deleted;
e2641e09 914
91504b6c 915 /* Parse the range arguments. */
7236fdb2
PN
916 if (zslParseRange(c->argv[2],c->argv[3],&range) != REDIS_OK) {
917 addReplyError(c,"min or max is not a double");
918 return;
919 }
e2641e09 920
4a14dbba
PN
921 if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL ||
922 checkType(c,zobj,REDIS_ZSET)) return;
923
924 if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
925 deleted = zzlDeleteRangeByScore(zobj,range);
926 } else if (zobj->encoding == REDIS_ENCODING_RAW) {
927 zset *zs = zobj->ptr;
928 deleted = zslDeleteRangeByScore(zs->zsl,range,zs->dict);
929 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
930 if (dictSize(zs->dict) == 0) dbDelete(c->db,key);
931 } else {
932 redisPanic("Unknown sorted set encoding");
933 }
e2641e09 934
4a14dbba 935 if (deleted) signalModifiedKey(c->db,key);
e2641e09 936 server.dirty += deleted;
937 addReplyLongLong(c,deleted);
938}
939
940void zremrangebyrankCommand(redisClient *c) {
63b7b7fb
PN
941 robj *key = c->argv[1];
942 robj *zobj;
e2641e09 943 long start;
944 long end;
945 int llen;
63b7b7fb 946 unsigned long deleted;
e2641e09 947
948 if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
949 (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;
950
63b7b7fb
PN
951 if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL ||
952 checkType(c,zobj,REDIS_ZSET)) return;
e2641e09 953
63b7b7fb
PN
954 /* Sanitize indexes. */
955 llen = zsLength(zobj);
e2641e09 956 if (start < 0) start = llen+start;
957 if (end < 0) end = llen+end;
958 if (start < 0) start = 0;
e2641e09 959
d0a4e24e
PN
960 /* Invariant: start >= 0, so this test will be true when end < 0.
961 * The range is empty when start > end or start >= length. */
e2641e09 962 if (start > end || start >= llen) {
963 addReply(c,shared.czero);
964 return;
965 }
966 if (end >= llen) end = llen-1;
967
63b7b7fb
PN
968 if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
969 /* Correct for 1-based rank. */
970 deleted = zzlDeleteRangeByRank(zobj,start+1,end+1);
971 } else if (zobj->encoding == REDIS_ENCODING_RAW) {
972 zset *zs = zobj->ptr;
973
974 /* Correct for 1-based rank. */
975 deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict);
976 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
977 if (dictSize(zs->dict) == 0) dbDelete(c->db,key);
978 } else {
979 redisPanic("Unknown sorted set encoding");
980 }
981
982 if (deleted) signalModifiedKey(c->db,key);
e2641e09 983 server.dirty += deleted;
63b7b7fb 984 addReplyLongLong(c,deleted);
e2641e09 985}
986
987typedef struct {
988 dict *dict;
989 double weight;
990} zsetopsrc;
991
992int qsortCompareZsetopsrcByCardinality(const void *s1, const void *s2) {
993 zsetopsrc *d1 = (void*) s1, *d2 = (void*) s2;
994 unsigned long size1, size2;
995 size1 = d1->dict ? dictSize(d1->dict) : 0;
996 size2 = d2->dict ? dictSize(d2->dict) : 0;
997 return size1 - size2;
998}
999
1000#define REDIS_AGGR_SUM 1
1001#define REDIS_AGGR_MIN 2
1002#define REDIS_AGGR_MAX 3
1003#define zunionInterDictValue(_e) (dictGetEntryVal(_e) == NULL ? 1.0 : *(double*)dictGetEntryVal(_e))
1004
1005inline static void zunionInterAggregate(double *target, double val, int aggregate) {
1006 if (aggregate == REDIS_AGGR_SUM) {
1007 *target = *target + val;
d9e28bcf
PN
1008 /* The result of adding two doubles is NaN when one variable
1009 * is +inf and the other is -inf. When these numbers are added,
1010 * we maintain the convention of the result being 0.0. */
1011 if (isnan(*target)) *target = 0.0;
e2641e09 1012 } else if (aggregate == REDIS_AGGR_MIN) {
1013 *target = val < *target ? val : *target;
1014 } else if (aggregate == REDIS_AGGR_MAX) {
1015 *target = val > *target ? val : *target;
1016 } else {
1017 /* safety net */
1018 redisPanic("Unknown ZUNION/INTER aggregate type");
1019 }
1020}
1021
1022void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) {
1023 int i, j, setnum;
1024 int aggregate = REDIS_AGGR_SUM;
1025 zsetopsrc *src;
1026 robj *dstobj;
1027 zset *dstzset;
69ef89f2 1028 zskiplistNode *znode;
e2641e09 1029 dictIterator *di;
1030 dictEntry *de;
8c1420ff 1031 int touched = 0;
e2641e09 1032
1033 /* expect setnum input keys to be given */
1034 setnum = atoi(c->argv[2]->ptr);
1035 if (setnum < 1) {
3ab20376
PN
1036 addReplyError(c,
1037 "at least 1 input key is needed for ZUNIONSTORE/ZINTERSTORE");
e2641e09 1038 return;
1039 }
1040
1041 /* test if the expected number of keys would overflow */
1042 if (3+setnum > c->argc) {
1043 addReply(c,shared.syntaxerr);
1044 return;
1045 }
1046
1047 /* read keys to be used for input */
1048 src = zmalloc(sizeof(zsetopsrc) * setnum);
1049 for (i = 0, j = 3; i < setnum; i++, j++) {
1050 robj *obj = lookupKeyWrite(c->db,c->argv[j]);
1051 if (!obj) {
1052 src[i].dict = NULL;
1053 } else {
1054 if (obj->type == REDIS_ZSET) {
1055 src[i].dict = ((zset*)obj->ptr)->dict;
1056 } else if (obj->type == REDIS_SET) {
1057 src[i].dict = (obj->ptr);
1058 } else {
1059 zfree(src);
1060 addReply(c,shared.wrongtypeerr);
1061 return;
1062 }
1063 }
1064
1065 /* default all weights to 1 */
1066 src[i].weight = 1.0;
1067 }
1068
1069 /* parse optional extra arguments */
1070 if (j < c->argc) {
1071 int remaining = c->argc - j;
1072
1073 while (remaining) {
1074 if (remaining >= (setnum + 1) && !strcasecmp(c->argv[j]->ptr,"weights")) {
1075 j++; remaining--;
1076 for (i = 0; i < setnum; i++, j++, remaining--) {
673e1fb7
PN
1077 if (getDoubleFromObjectOrReply(c,c->argv[j],&src[i].weight,
1078 "weight value is not a double") != REDIS_OK)
1079 {
1080 zfree(src);
e2641e09 1081 return;
673e1fb7 1082 }
e2641e09 1083 }
1084 } else if (remaining >= 2 && !strcasecmp(c->argv[j]->ptr,"aggregate")) {
1085 j++; remaining--;
1086 if (!strcasecmp(c->argv[j]->ptr,"sum")) {
1087 aggregate = REDIS_AGGR_SUM;
1088 } else if (!strcasecmp(c->argv[j]->ptr,"min")) {
1089 aggregate = REDIS_AGGR_MIN;
1090 } else if (!strcasecmp(c->argv[j]->ptr,"max")) {
1091 aggregate = REDIS_AGGR_MAX;
1092 } else {
1093 zfree(src);
1094 addReply(c,shared.syntaxerr);
1095 return;
1096 }
1097 j++; remaining--;
1098 } else {
1099 zfree(src);
1100 addReply(c,shared.syntaxerr);
1101 return;
1102 }
1103 }
1104 }
1105
1106 /* sort sets from the smallest to largest, this will improve our
1107 * algorithm's performance */
1108 qsort(src,setnum,sizeof(zsetopsrc),qsortCompareZsetopsrcByCardinality);
1109
1110 dstobj = createZsetObject();
1111 dstzset = dstobj->ptr;
1112
1113 if (op == REDIS_OP_INTER) {
1114 /* skip going over all entries if the smallest zset is NULL or empty */
1115 if (src[0].dict && dictSize(src[0].dict) > 0) {
1116 /* precondition: as src[0].dict is non-empty and the zsets are ordered
1117 * from small to large, all src[i > 0].dict are non-empty too */
1118 di = dictGetIterator(src[0].dict);
1119 while((de = dictNext(di)) != NULL) {
d433ebc6 1120 double score, value;
e2641e09 1121
50a9fad5 1122 score = src[0].weight * zunionInterDictValue(de);
e2641e09 1123 for (j = 1; j < setnum; j++) {
1124 dictEntry *other = dictFind(src[j].dict,dictGetEntryKey(de));
1125 if (other) {
1126 value = src[j].weight * zunionInterDictValue(other);
d433ebc6 1127 zunionInterAggregate(&score,value,aggregate);
e2641e09 1128 } else {
1129 break;
1130 }
1131 }
1132
d433ebc6
PN
1133 /* Only continue when present in every source dict. */
1134 if (j == setnum) {
e2641e09 1135 robj *o = dictGetEntryKey(de);
d433ebc6 1136 znode = zslInsert(dstzset->zsl,score,o);
e2641e09 1137 incrRefCount(o); /* added to skiplist */
69ef89f2
PN
1138 dictAdd(dstzset->dict,o,&znode->score);
1139 incrRefCount(o); /* added to dictionary */
e2641e09 1140 }
1141 }
1142 dictReleaseIterator(di);
1143 }
1144 } else if (op == REDIS_OP_UNION) {
1145 for (i = 0; i < setnum; i++) {
1146 if (!src[i].dict) continue;
1147
1148 di = dictGetIterator(src[i].dict);
1149 while((de = dictNext(di)) != NULL) {
d433ebc6
PN
1150 double score, value;
1151
e2641e09 1152 /* skip key when already processed */
d433ebc6
PN
1153 if (dictFind(dstzset->dict,dictGetEntryKey(de)) != NULL)
1154 continue;
e2641e09 1155
d433ebc6
PN
1156 /* initialize score */
1157 score = src[i].weight * zunionInterDictValue(de);
e2641e09 1158
1159 /* because the zsets are sorted by size, its only possible
1160 * for sets at larger indices to hold this entry */
1161 for (j = (i+1); j < setnum; j++) {
1162 dictEntry *other = dictFind(src[j].dict,dictGetEntryKey(de));
1163 if (other) {
1164 value = src[j].weight * zunionInterDictValue(other);
d433ebc6 1165 zunionInterAggregate(&score,value,aggregate);
e2641e09 1166 }
1167 }
1168
1169 robj *o = dictGetEntryKey(de);
d433ebc6 1170 znode = zslInsert(dstzset->zsl,score,o);
e2641e09 1171 incrRefCount(o); /* added to skiplist */
69ef89f2
PN
1172 dictAdd(dstzset->dict,o,&znode->score);
1173 incrRefCount(o); /* added to dictionary */
e2641e09 1174 }
1175 dictReleaseIterator(di);
1176 }
1177 } else {
1178 /* unknown operator */
1179 redisAssert(op == REDIS_OP_INTER || op == REDIS_OP_UNION);
1180 }
1181
8c1420ff 1182 if (dbDelete(c->db,dstkey)) {
cea8c5cd 1183 signalModifiedKey(c->db,dstkey);
8c1420ff 1184 touched = 1;
1185 server.dirty++;
1186 }
e2641e09 1187 if (dstzset->zsl->length) {
1188 dbAdd(c->db,dstkey,dstobj);
1189 addReplyLongLong(c, dstzset->zsl->length);
cea8c5cd 1190 if (!touched) signalModifiedKey(c->db,dstkey);
cbf7e107 1191 server.dirty++;
e2641e09 1192 } else {
1193 decrRefCount(dstobj);
1194 addReply(c, shared.czero);
1195 }
1196 zfree(src);
1197}
1198
1199void zunionstoreCommand(redisClient *c) {
1200 zunionInterGenericCommand(c,c->argv[1], REDIS_OP_UNION);
1201}
1202
1203void zinterstoreCommand(redisClient *c) {
1204 zunionInterGenericCommand(c,c->argv[1], REDIS_OP_INTER);
1205}
1206
1207void zrangeGenericCommand(redisClient *c, int reverse) {
5d1b4fb6
PN
1208 robj *key = c->argv[1];
1209 robj *zobj;
1210 int withscores = 0;
e2641e09 1211 long start;
1212 long end;
e2641e09 1213 int llen;
5d1b4fb6 1214 int rangelen;
e2641e09 1215
1216 if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
1217 (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;
1218
1219 if (c->argc == 5 && !strcasecmp(c->argv[4]->ptr,"withscores")) {
1220 withscores = 1;
1221 } else if (c->argc >= 5) {
1222 addReply(c,shared.syntaxerr);
1223 return;
1224 }
1225
5d1b4fb6
PN
1226 if ((zobj = lookupKeyReadOrReply(c,key,shared.emptymultibulk)) == NULL
1227 || checkType(c,zobj,REDIS_ZSET)) return;
e2641e09 1228
5d1b4fb6
PN
1229 /* Sanitize indexes. */
1230 llen = zsLength(zobj);
e2641e09 1231 if (start < 0) start = llen+start;
1232 if (end < 0) end = llen+end;
1233 if (start < 0) start = 0;
e2641e09 1234
d0a4e24e
PN
1235 /* Invariant: start >= 0, so this test will be true when end < 0.
1236 * The range is empty when start > end or start >= length. */
e2641e09 1237 if (start > end || start >= llen) {
e2641e09 1238 addReply(c,shared.emptymultibulk);
1239 return;
1240 }
1241 if (end >= llen) end = llen-1;
1242 rangelen = (end-start)+1;
1243
e2641e09 1244 /* Return the result in form of a multi-bulk reply */
5d1b4fb6
PN
1245 addReplyMultiBulkLen(c, withscores ? (rangelen*2) : rangelen);
1246
1247 if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
1248 unsigned char *zl = zobj->ptr;
1249 unsigned char *eptr, *sptr;
1250 unsigned char *vstr;
1251 unsigned int vlen;
1252 long long vlong;
1253
1254 if (reverse)
1255 eptr = ziplistIndex(zl,-2-(2*start));
1256 else
1257 eptr = ziplistIndex(zl,2*start);
1258
4c5f0966
PN
1259 redisAssert(eptr != NULL);
1260 sptr = ziplistNext(zl,eptr);
1261
5d1b4fb6 1262 while (rangelen--) {
4c5f0966 1263 redisAssert(eptr != NULL && sptr != NULL);
5d1b4fb6
PN
1264 redisAssert(ziplistGet(eptr,&vstr,&vlen,&vlong));
1265 if (vstr == NULL)
1266 addReplyBulkLongLong(c,vlong);
1267 else
1268 addReplyBulkCBuffer(c,vstr,vlen);
1269
4c5f0966 1270 if (withscores)
5d1b4fb6 1271 addReplyDouble(c,zzlGetScore(sptr));
5d1b4fb6 1272
4c5f0966
PN
1273 if (reverse)
1274 zzlPrev(zl,&eptr,&sptr);
1275 else
1276 zzlNext(zl,&eptr,&sptr);
5d1b4fb6
PN
1277 }
1278
1279 } else if (zobj->encoding == REDIS_ENCODING_RAW) {
1280 zset *zs = zobj->ptr;
1281 zskiplist *zsl = zs->zsl;
1282 zskiplistNode *ln;
1283 robj *ele;
1284
1285 /* Check if starting point is trivial, before doing log(N) lookup. */
1286 if (reverse) {
1287 ln = zsl->tail;
1288 if (start > 0)
1289 ln = zslGetElementByRank(zsl,llen-start);
1290 } else {
1291 ln = zsl->header->level[0].forward;
1292 if (start > 0)
1293 ln = zslGetElementByRank(zsl,start+1);
1294 }
1295
1296 while(rangelen--) {
1297 redisAssert(ln != NULL);
1298 ele = ln->obj;
1299 addReplyBulk(c,ele);
1300 if (withscores)
1301 addReplyDouble(c,ln->score);
1302 ln = reverse ? ln->backward : ln->level[0].forward;
1303 }
1304 } else {
1305 redisPanic("Unknown sorted set encoding");
e2641e09 1306 }
1307}
1308
1309void zrangeCommand(redisClient *c) {
1310 zrangeGenericCommand(c,0);
1311}
1312
1313void zrevrangeCommand(redisClient *c) {
1314 zrangeGenericCommand(c,1);
1315}
1316
25bb8a44
PN
1317/* This command implements ZRANGEBYSCORE, ZREVRANGEBYSCORE and ZCOUNT.
1318 * If "justcount", only the number of elements in the range is returned. */
1319void genericZrangebyscoreCommand(redisClient *c, int reverse, int justcount) {
1320 zrangespec range;
aff255c8
PN
1321 robj *key = c->argv[1];
1322 robj *emptyreply, *zobj;
e2641e09 1323 int offset = 0, limit = -1;
1324 int withscores = 0;
25bb8a44
PN
1325 unsigned long rangelen = 0;
1326 void *replylen = NULL;
22b9bf15 1327 int minidx, maxidx;
e2641e09 1328
25bb8a44 1329 /* Parse the range arguments. */
22b9bf15
PN
1330 if (reverse) {
1331 /* Range is given as [max,min] */
1332 maxidx = 2; minidx = 3;
1333 } else {
1334 /* Range is given as [min,max] */
1335 minidx = 2; maxidx = 3;
1336 }
1337
1338 if (zslParseRange(c->argv[minidx],c->argv[maxidx],&range) != REDIS_OK) {
7236fdb2
PN
1339 addReplyError(c,"min or max is not a double");
1340 return;
1341 }
25bb8a44
PN
1342
1343 /* Parse optional extra arguments. Note that ZCOUNT will exactly have
1344 * 4 arguments, so we'll never enter the following code path. */
1345 if (c->argc > 4) {
1346 int remaining = c->argc - 4;
1347 int pos = 4;
1348
1349 while (remaining) {
1350 if (remaining >= 1 && !strcasecmp(c->argv[pos]->ptr,"withscores")) {
1351 pos++; remaining--;
1352 withscores = 1;
1353 } else if (remaining >= 3 && !strcasecmp(c->argv[pos]->ptr,"limit")) {
1354 offset = atoi(c->argv[pos+1]->ptr);
1355 limit = atoi(c->argv[pos+2]->ptr);
1356 pos += 3; remaining -= 3;
1357 } else {
1358 addReply(c,shared.syntaxerr);
1359 return;
1360 }
1361 }
e2641e09 1362 }
25bb8a44
PN
1363
1364 /* Ok, lookup the key and get the range */
1365 emptyreply = justcount ? shared.czero : shared.emptymultibulk;
aff255c8
PN
1366 if ((zobj = lookupKeyReadOrReply(c,key,emptyreply)) == NULL ||
1367 checkType(c,zobj,REDIS_ZSET)) return;
25bb8a44 1368
aff255c8
PN
1369 if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
1370 unsigned char *zl = zobj->ptr;
1371 unsigned char *eptr, *sptr;
1372 unsigned char *vstr;
1373 unsigned int vlen;
1374 long long vlong;
1375 double score;
e2641e09 1376
aff255c8
PN
1377 /* If reversed, get the last node in range as starting point. */
1378 if (reverse)
1379 eptr = zzlLastInRange(zobj,range);
1380 else
1381 eptr = zzlFirstInRange(zobj,range);
e2641e09 1382
aff255c8
PN
1383 /* No "first" element in the specified interval. */
1384 if (eptr == NULL) {
1385 addReply(c,emptyreply);
1386 return;
1387 }
e2641e09 1388
aff255c8
PN
1389 /* Get score pointer for the first element. */
1390 redisAssert(eptr != NULL);
1391 sptr = ziplistNext(zl,eptr);
e2641e09 1392
aff255c8
PN
1393 /* We don't know in advance how many matching elements there are in the
1394 * list, so we push this object that will represent the multi-bulk
1395 * length in the output buffer, and will "fix" it later */
1396 if (!justcount)
1397 replylen = addDeferredMultiBulkLength(c);
1398
1399 /* If there is an offset, just traverse the number of elements without
1400 * checking the score because that is done in the next loop. */
1401 while (eptr && offset--)
1402 if (reverse)
1403 zzlPrev(zl,&eptr,&sptr);
1404 else
1405 zzlNext(zl,&eptr,&sptr);
1406
1407 while (eptr && limit--) {
1408 score = zzlGetScore(sptr);
1409
1410 /* Abort when the node is no longer in range. */
1411 if (reverse) {
1412 if (!zslValueGteMin(score,&range)) break;
1413 } else {
1414 if (!zslValueLteMax(score,&range)) break;
1415 }
1416
1417 /* Do our magic */
1418 rangelen++;
1419 if (!justcount) {
1420 redisAssert(ziplistGet(eptr,&vstr,&vlen,&vlong));
1421 if (vstr == NULL)
1422 addReplyBulkLongLong(c,vlong);
1423 else
1424 addReplyBulkCBuffer(c,vstr,vlen);
1425
1426 if (withscores)
1427 addReplyDouble(c,score);
1428 }
1429
1430 /* Move to next node */
1431 if (reverse)
1432 zzlPrev(zl,&eptr,&sptr);
1433 else
1434 zzlNext(zl,&eptr,&sptr);
e2641e09 1435 }
aff255c8
PN
1436 } else if (zobj->encoding == REDIS_ENCODING_RAW) {
1437 zset *zs = zobj->ptr;
1438 zskiplist *zsl = zs->zsl;
1439 zskiplistNode *ln;
25bb8a44 1440
aff255c8
PN
1441 /* If reversed, get the last node in range as starting point. */
1442 if (reverse)
1443 ln = zslLastInRange(zsl,range);
1444 else
1445 ln = zslFirstInRange(zsl,range);
1446
1447 /* No "first" element in the specified interval. */
1448 if (ln == NULL) {
1449 addReply(c,emptyreply);
1450 return;
25bb8a44
PN
1451 }
1452
aff255c8
PN
1453 /* We don't know in advance how many matching elements there are in the
1454 * list, so we push this object that will represent the multi-bulk
1455 * length in the output buffer, and will "fix" it later */
1456 if (!justcount)
1457 replylen = addDeferredMultiBulkLength(c);
1458
1459 /* If there is an offset, just traverse the number of elements without
1460 * checking the score because that is done in the next loop. */
1461 while (ln && offset--)
1462 ln = reverse ? ln->backward : ln->level[0].forward;
1463
1464 while (ln && limit--) {
1465 /* Abort when the node is no longer in range. */
1466 if (reverse) {
1467 if (!zslValueGteMin(ln->score,&range)) break;
1468 } else {
1469 if (!zslValueLteMax(ln->score,&range)) break;
1470 }
1471
1472 /* Do our magic */
1473 rangelen++;
1474 if (!justcount) {
1475 addReplyBulk(c,ln->obj);
1476 if (withscores)
1477 addReplyDouble(c,ln->score);
1478 }
1479
1480 /* Move to next node */
1481 ln = reverse ? ln->backward : ln->level[0].forward;
1482 }
1483 } else {
1484 redisPanic("Unknown sorted set encoding");
25bb8a44
PN
1485 }
1486
1487 if (justcount) {
1488 addReplyLongLong(c,(long)rangelen);
1489 } else {
aff255c8
PN
1490 if (withscores) rangelen *= 2;
1491 setDeferredMultiBulkLength(c,replylen,rangelen);
e2641e09 1492 }
1493}
1494
1495void zrangebyscoreCommand(redisClient *c) {
25bb8a44
PN
1496 genericZrangebyscoreCommand(c,0,0);
1497}
1498
1499void zrevrangebyscoreCommand(redisClient *c) {
1500 genericZrangebyscoreCommand(c,1,0);
e2641e09 1501}
1502
1503void zcountCommand(redisClient *c) {
25bb8a44 1504 genericZrangebyscoreCommand(c,0,1);
e2641e09 1505}
1506
1507void zcardCommand(redisClient *c) {
d1c920c5
PN
1508 robj *key = c->argv[1];
1509 robj *zobj;
e2641e09 1510
d1c920c5
PN
1511 if ((zobj = lookupKeyReadOrReply(c,key,shared.czero)) == NULL ||
1512 checkType(c,zobj,REDIS_ZSET)) return;
e2641e09 1513
d1c920c5 1514 addReplyLongLong(c,zzlLength(zobj));
e2641e09 1515}
1516
1517void zscoreCommand(redisClient *c) {
d1c920c5
PN
1518 robj *key = c->argv[1];
1519 robj *zobj;
1520 double score;
e2641e09 1521
d1c920c5
PN
1522 if ((zobj = lookupKeyReadOrReply(c,key,shared.nullbulk)) == NULL ||
1523 checkType(c,zobj,REDIS_ZSET)) return;
e2641e09 1524
d1c920c5
PN
1525 if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
1526 if (zzlFind(zobj,c->argv[2],&score) != NULL)
1527 addReplyDouble(c,score);
1528 else
1529 addReply(c,shared.nullbulk);
1530 } else if (zobj->encoding == REDIS_ENCODING_RAW) {
1531 zset *zs = zobj->ptr;
1532 dictEntry *de;
e2641e09 1533
d1c920c5
PN
1534 c->argv[2] = tryObjectEncoding(c->argv[2]);
1535 de = dictFind(zs->dict,c->argv[2]);
1536 if (de != NULL) {
1537 score = *(double*)dictGetEntryVal(de);
1538 addReplyDouble(c,score);
1539 } else {
1540 addReply(c,shared.nullbulk);
1541 }
1542 } else {
1543 redisPanic("Unknown sorted set encoding");
e2641e09 1544 }
1545}
1546
1547void zrankGenericCommand(redisClient *c, int reverse) {
d1c920c5
PN
1548 robj *key = c->argv[1];
1549 robj *ele = c->argv[2];
1550 robj *zobj;
1551 unsigned long llen;
e2641e09 1552 unsigned long rank;
e2641e09 1553
d1c920c5
PN
1554 if ((zobj = lookupKeyReadOrReply(c,key,shared.nullbulk)) == NULL ||
1555 checkType(c,zobj,REDIS_ZSET)) return;
1556 llen = zsLength(zobj);
e2641e09 1557
d1c920c5
PN
1558 redisAssert(ele->encoding == REDIS_ENCODING_RAW);
1559 if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
1560 unsigned char *zl = zobj->ptr;
1561 unsigned char *eptr, *sptr;
e2641e09 1562
d1c920c5
PN
1563 eptr = ziplistIndex(zl,0);
1564 redisAssert(eptr != NULL);
1565 sptr = ziplistNext(zl,eptr);
1566 redisAssert(sptr != NULL);
1567
1568 rank = 1;
1569 while(eptr != NULL) {
1570 if (ziplistCompare(eptr,ele->ptr,sdslen(ele->ptr)))
1571 break;
1572 rank++;
1573 zzlNext(zl,&eptr,&sptr);
1574 }
1575
1576 if (eptr != NULL) {
1577 if (reverse)
1578 addReplyLongLong(c,llen-rank);
1579 else
1580 addReplyLongLong(c,rank-1);
e2641e09 1581 } else {
d1c920c5
PN
1582 addReply(c,shared.nullbulk);
1583 }
1584 } else if (zobj->encoding == REDIS_ENCODING_RAW) {
1585 zset *zs = zobj->ptr;
1586 zskiplist *zsl = zs->zsl;
1587 dictEntry *de;
1588 double score;
1589
1590 ele = c->argv[2] = tryObjectEncoding(c->argv[2]);
1591 de = dictFind(zs->dict,ele);
1592 if (de != NULL) {
1593 score = *(double*)dictGetEntryVal(de);
1594 rank = zslGetRank(zsl,score,ele);
1595 redisAssert(rank); /* Existing elements always have a rank. */
1596 if (reverse)
1597 addReplyLongLong(c,llen-rank);
1598 else
1599 addReplyLongLong(c,rank-1);
1600 } else {
1601 addReply(c,shared.nullbulk);
e2641e09 1602 }
1603 } else {
d1c920c5 1604 redisPanic("Unknown sorted set encoding");
e2641e09 1605 }
1606}
1607
1608void zrankCommand(redisClient *c) {
1609 zrankGenericCommand(c, 0);
1610}
1611
1612void zrevrankCommand(redisClient *c) {
1613 zrankGenericCommand(c, 1);
1614}