]>
git.saurik.com Git - redis.git/blob - src/sort.c
2 #include "pqsort.h" /* Partial qsort for SORT+LIMIT */
3 #include <math.h> /* isnan() */
5 zskiplistNode
* zslGetElementByRank(zskiplist
*zsl
, unsigned long rank
);
7 redisSortOperation
*createSortOperation(int type
, robj
*pattern
) {
8 redisSortOperation
*so
= zmalloc(sizeof(*so
));
10 so
->pattern
= pattern
;
14 /* Return the value associated to the key with a name obtained using
15 * the following rules:
17 * 1) The first occurence of '*' in 'pattern' is substituted with 'subst'.
19 * 2) If 'pattern' matches the "->" string, everything on the left of
20 * the arrow is treated as the name of an hash field, and the part on the
21 * left as the key name containing an hash. The value of the specified
24 * 3) If 'pattern' equals "#", the function simply returns 'subst' itself so
25 * that the SORT command can be used like: SORT key GET # to retrieve
26 * the Set/List elements directly.
28 * The returned object will always have its refcount increased by 1
29 * when it is non-NULL. */
30 robj
*lookupKeyByPattern(redisDb
*db
, robj
*pattern
, robj
*subst
) {
33 robj
*keyobj
, *fieldobj
= NULL
, *o
;
34 int prefixlen
, sublen
, postfixlen
, fieldlen
;
36 /* If the pattern is "#" return the substitution object itself in order
37 * to implement the "SORT ... GET #" feature. */
39 if (spat
[0] == '#' && spat
[1] == '\0') {
44 /* The substitution object may be specially encoded. If so we create
45 * a decoded object on the fly. Otherwise getDecodedObject will just
46 * increment the ref count, that we'll decrement later. */
47 subst
= getDecodedObject(subst
);
50 /* If we can't find '*' in the pattern we return NULL as to GET a
51 * fixed key does not make sense. */
58 /* Find out if we're dealing with a hash dereference. */
59 if ((f
= strstr(p
+1, "->")) != NULL
&& *(f
+2) != '\0') {
60 fieldlen
= sdslen(spat
)-(f
-spat
)-2;
61 fieldobj
= createStringObject(f
+2,fieldlen
);
66 /* Perform the '*' substitution. */
68 sublen
= sdslen(ssub
);
69 postfixlen
= sdslen(spat
)-(prefixlen
+1)-(fieldlen
? fieldlen
+2 : 0);
70 keyobj
= createStringObject(NULL
,prefixlen
+sublen
+postfixlen
);
72 memcpy(k
,spat
,prefixlen
);
73 memcpy(k
+prefixlen
,ssub
,sublen
);
74 memcpy(k
+prefixlen
+sublen
,p
+1,postfixlen
);
75 decrRefCount(subst
); /* Incremented by decodeObject() */
77 /* Lookup substituted key */
78 o
= lookupKeyRead(db
,keyobj
);
79 if (o
== NULL
) goto noobj
;
82 if (o
->type
!= REDIS_HASH
) goto noobj
;
84 /* Retrieve value from hash by the field name. This operation
85 * already increases the refcount of the returned object. */
86 o
= hashTypeGetObject(o
, fieldobj
);
88 if (o
->type
!= REDIS_STRING
) goto noobj
;
90 /* Every object that this function returns needs to have its refcount
91 * increased. sortCommand decreases it again. */
95 if (fieldobj
) decrRefCount(fieldobj
);
100 if (fieldlen
) decrRefCount(fieldobj
);
104 /* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
105 * the additional parameter is not standard but a BSD-specific we have to
106 * pass sorting parameters via the global 'server' structure */
107 int sortCompare(const void *s1
, const void *s2
) {
108 const redisSortObject
*so1
= s1
, *so2
= s2
;
111 if (!server
.sort_alpha
) {
112 /* Numeric sorting. Here it's trivial as we precomputed scores */
113 if (so1
->u
.score
> so2
->u
.score
) {
115 } else if (so1
->u
.score
< so2
->u
.score
) {
118 /* Objects have the same score, but we don't want the comparison
119 * to be undefined, so we compare objects lexicographycally.
120 * This way the result of SORT is deterministic. */
121 cmp
= compareStringObjects(so1
->obj
,so2
->obj
);
124 /* Alphanumeric sorting */
125 if (server
.sort_bypattern
) {
126 if (!so1
->u
.cmpobj
|| !so2
->u
.cmpobj
) {
127 /* At least one compare object is NULL */
128 if (so1
->u
.cmpobj
== so2
->u
.cmpobj
)
130 else if (so1
->u
.cmpobj
== NULL
)
135 /* We have both the objects, use strcoll */
136 cmp
= strcoll(so1
->u
.cmpobj
->ptr
,so2
->u
.cmpobj
->ptr
);
139 /* Compare elements directly. */
140 cmp
= compareStringObjects(so1
->obj
,so2
->obj
);
143 return server
.sort_desc
? -cmp
: cmp
;
146 /* The SORT command is the most complex command in Redis. Warning: this code
147 * is optimized for speed and a bit less for readability */
148 void sortCommand(redisClient
*c
) {
150 unsigned int outputlen
= 0;
151 int desc
= 0, alpha
= 0;
152 long limit_start
= 0, limit_count
= -1, start
, end
;
153 int j
, dontsort
= 0, vectorlen
;
154 int getop
= 0; /* GET operation counter */
155 int int_convertion_error
= 0;
156 robj
*sortval
, *sortby
= NULL
, *storekey
= NULL
;
157 redisSortObject
*vector
; /* Resulting vector to sort */
159 /* Lookup the key to sort. It must be of the right types */
160 sortval
= lookupKeyRead(c
->db
,c
->argv
[1]);
161 if (sortval
&& sortval
->type
!= REDIS_SET
&&
162 sortval
->type
!= REDIS_LIST
&&
163 sortval
->type
!= REDIS_ZSET
)
165 addReply(c
,shared
.wrongtypeerr
);
169 /* Create a list of operations to perform for every sorted element.
170 * Operations can be GET/DEL/INCR/DECR */
171 operations
= listCreate();
172 listSetFreeMethod(operations
,zfree
);
173 j
= 2; /* options start at argv[2] */
175 /* Now we need to protect sortval incrementing its count, in the future
176 * SORT may have options able to overwrite/delete keys during the sorting
177 * and the sorted key itself may get destroied */
179 incrRefCount(sortval
);
181 sortval
= createListObject();
183 /* The SORT command has an SQL-alike syntax, parse it */
185 int leftargs
= c
->argc
-j
-1;
186 if (!strcasecmp(c
->argv
[j
]->ptr
,"asc")) {
188 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"desc")) {
190 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"alpha")) {
192 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"limit") && leftargs
>= 2) {
193 if ((getLongFromObjectOrReply(c
, c
->argv
[j
+1], &limit_start
, NULL
) != REDIS_OK
) ||
194 (getLongFromObjectOrReply(c
, c
->argv
[j
+2], &limit_count
, NULL
) != REDIS_OK
)) return;
196 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"store") && leftargs
>= 1) {
197 storekey
= c
->argv
[j
+1];
199 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"by") && leftargs
>= 1) {
200 sortby
= c
->argv
[j
+1];
201 /* If the BY pattern does not contain '*', i.e. it is constant,
202 * we don't need to sort nor to lookup the weight keys. */
203 if (strchr(c
->argv
[j
+1]->ptr
,'*') == NULL
) dontsort
= 1;
205 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"get") && leftargs
>= 1) {
206 listAddNodeTail(operations
,createSortOperation(
207 REDIS_SORT_GET
,c
->argv
[j
+1]));
211 decrRefCount(sortval
);
212 listRelease(operations
);
213 addReply(c
,shared
.syntaxerr
);
219 /* For the STORE option, or when SORT is called from a Lua script,
220 * we want to force a specific ordering even when no explicit ordering
221 * was asked (SORT BY nosort). This guarantees that replication / AOF
224 * However in the case 'dontsort' is true, but the type to sort is a
225 * sorted set, we don't need to do anything as ordering is guaranteed
226 * in this special case. */
227 if ((storekey
|| c
->flags
& REDIS_LUA_CLIENT
) &&
228 (dontsort
&& sortval
->type
!= REDIS_ZSET
))
230 /* Force ALPHA sorting */
236 /* Destructively convert encoded sorted sets for SORT. */
237 if (sortval
->type
== REDIS_ZSET
)
238 zsetConvert(sortval
, REDIS_ENCODING_SKIPLIST
);
240 /* Objtain the length of the object to sort. */
241 switch(sortval
->type
) {
242 case REDIS_LIST
: vectorlen
= listTypeLength(sortval
); break;
243 case REDIS_SET
: vectorlen
= setTypeSize(sortval
); break;
244 case REDIS_ZSET
: vectorlen
= dictSize(((zset
*)sortval
->ptr
)->dict
); break;
245 default: vectorlen
= 0; redisPanic("Bad SORT type"); /* Avoid GCC warning */
248 /* Perform LIMIT start,count sanity checking. */
249 start
= (limit_start
< 0) ? 0 : limit_start
;
250 end
= (limit_count
< 0) ? vectorlen
-1 : start
+limit_count
-1;
251 if (start
>= vectorlen
) {
255 if (end
>= vectorlen
) end
= vectorlen
-1;
259 * 1) if the object to sort is a sorted set.
260 * 2) There is nothing to sort as dontsort is true (BY <constant string>).
261 * 3) We have a LIMIT option that actually reduces the number of elements
264 * In this case to load all the objects in the vector is a huge waste of
265 * resources. We just allocate a vector that is big enough for the selected
266 * range length, and make sure to load just this part in the vector. */
267 if (sortval
->type
== REDIS_ZSET
&&
269 (start
!= 0 || end
!= vectorlen
-1))
271 vectorlen
= end
-start
+1;
274 /* Load the sorting vector with all the objects to sort */
275 vector
= zmalloc(sizeof(redisSortObject
)*vectorlen
);
278 if (sortval
->type
== REDIS_LIST
) {
279 listTypeIterator
*li
= listTypeInitIterator(sortval
,0,REDIS_TAIL
);
281 while(listTypeNext(li
,&entry
)) {
282 vector
[j
].obj
= listTypeGet(&entry
);
283 vector
[j
].u
.score
= 0;
284 vector
[j
].u
.cmpobj
= NULL
;
287 listTypeReleaseIterator(li
);
288 } else if (sortval
->type
== REDIS_SET
) {
289 setTypeIterator
*si
= setTypeInitIterator(sortval
);
291 while((ele
= setTypeNextObject(si
)) != NULL
) {
293 vector
[j
].u
.score
= 0;
294 vector
[j
].u
.cmpobj
= NULL
;
297 setTypeReleaseIterator(si
);
298 } else if (sortval
->type
== REDIS_ZSET
&& dontsort
) {
299 /* Special handling for a sorted set, if 'dontsort' is true.
300 * This makes sure we return elements in the sorted set original
301 * ordering, accordingly to DESC / ASC options.
303 * Note that in this case we also handle LIMIT here in a direct
304 * way, just getting the required range, as an optimization. */
306 zset
*zs
= sortval
->ptr
;
307 zskiplist
*zsl
= zs
->zsl
;
310 int rangelen
= vectorlen
;
312 /* Check if starting point is trivial, before doing log(N) lookup. */
314 long zsetlen
= dictSize(((zset
*)sortval
->ptr
)->dict
);
318 ln
= zslGetElementByRank(zsl
,zsetlen
-start
);
320 ln
= zsl
->header
->level
[0].forward
;
322 ln
= zslGetElementByRank(zsl
,start
+1);
326 redisAssertWithInfo(c
,sortval
,ln
!= NULL
);
329 vector
[j
].u
.score
= 0;
330 vector
[j
].u
.cmpobj
= NULL
;
332 ln
= desc
? ln
->backward
: ln
->level
[0].forward
;
334 /* The code producing the output does not know that in the case of
335 * sorted set, 'dontsort', and LIMIT, we are able to get just the
336 * range, already sorted, so we need to adjust "start" and "end"
337 * to make sure start is set to 0. */
340 } else if (sortval
->type
== REDIS_ZSET
) {
341 dict
*set
= ((zset
*)sortval
->ptr
)->dict
;
344 di
= dictGetIterator(set
);
345 while((setele
= dictNext(di
)) != NULL
) {
346 vector
[j
].obj
= dictGetKey(setele
);
347 vector
[j
].u
.score
= 0;
348 vector
[j
].u
.cmpobj
= NULL
;
351 dictReleaseIterator(di
);
353 redisPanic("Unknown type");
355 redisAssertWithInfo(c
,sortval
,j
== vectorlen
);
357 /* Now it's time to load the right scores in the sorting vector */
359 for (j
= 0; j
< vectorlen
; j
++) {
362 /* lookup value to sort by */
363 byval
= lookupKeyByPattern(c
->db
,sortby
,vector
[j
].obj
);
364 if (!byval
) continue;
366 /* use object itself to sort by */
367 byval
= vector
[j
].obj
;
371 if (sortby
) vector
[j
].u
.cmpobj
= getDecodedObject(byval
);
373 if (byval
->encoding
== REDIS_ENCODING_RAW
) {
376 vector
[j
].u
.score
= strtod(byval
->ptr
,&eptr
);
377 if (eptr
[0] != '\0' || errno
== ERANGE
||
378 isnan(vector
[j
].u
.score
))
380 int_convertion_error
= 1;
382 } else if (byval
->encoding
== REDIS_ENCODING_INT
) {
383 /* Don't need to decode the object if it's
384 * integer-encoded (the only encoding supported) so
385 * far. We can just cast it */
386 vector
[j
].u
.score
= (long)byval
->ptr
;
388 redisAssertWithInfo(c
,sortval
,1 != 1);
392 /* when the object was retrieved using lookupKeyByPattern,
393 * its refcount needs to be decreased. */
401 server
.sort_desc
= desc
;
402 server
.sort_alpha
= alpha
;
403 server
.sort_bypattern
= sortby
? 1 : 0;
404 if (sortby
&& (start
!= 0 || end
!= vectorlen
-1))
405 pqsort(vector
,vectorlen
,sizeof(redisSortObject
),sortCompare
, start
,end
);
407 qsort(vector
,vectorlen
,sizeof(redisSortObject
),sortCompare
);
410 /* Send command output to the output buffer, performing the specified
411 * GET/DEL/INCR/DECR operations if any. */
412 outputlen
= getop
? getop
*(end
-start
+1) : end
-start
+1;
413 if (int_convertion_error
) {
414 addReplyError(c
,"One or more scores can't be converted into double");
415 } else if (storekey
== NULL
) {
416 /* STORE option not specified, sent the sorting result to client */
417 addReplyMultiBulkLen(c
,outputlen
);
418 for (j
= start
; j
<= end
; j
++) {
422 if (!getop
) addReplyBulk(c
,vector
[j
].obj
);
423 listRewind(operations
,&li
);
424 while((ln
= listNext(&li
))) {
425 redisSortOperation
*sop
= ln
->value
;
426 robj
*val
= lookupKeyByPattern(c
->db
,sop
->pattern
,
429 if (sop
->type
== REDIS_SORT_GET
) {
431 addReply(c
,shared
.nullbulk
);
438 redisAssertWithInfo(c
,sortval
,sop
->type
== REDIS_SORT_GET
);
443 robj
*sobj
= createZiplistObject();
445 /* STORE option specified, set the sorting result as a List object */
446 for (j
= start
; j
<= end
; j
++) {
451 listTypePush(sobj
,vector
[j
].obj
,REDIS_TAIL
);
453 listRewind(operations
,&li
);
454 while((ln
= listNext(&li
))) {
455 redisSortOperation
*sop
= ln
->value
;
456 robj
*val
= lookupKeyByPattern(c
->db
,sop
->pattern
,
459 if (sop
->type
== REDIS_SORT_GET
) {
460 if (!val
) val
= createStringObject("",0);
462 /* listTypePush does an incrRefCount, so we should take care
463 * care of the incremented refcount caused by either
464 * lookupKeyByPattern or createStringObject("",0) */
465 listTypePush(sobj
,val
,REDIS_TAIL
);
469 redisAssertWithInfo(c
,sortval
,sop
->type
== REDIS_SORT_GET
);
475 setKey(c
->db
,storekey
,sobj
);
476 server
.dirty
+= outputlen
;
477 } else if (dbDelete(c
->db
,storekey
)) {
478 signalModifiedKey(c
->db
,storekey
);
482 addReplyLongLong(c
,outputlen
);
486 if (sortval
->type
== REDIS_LIST
|| sortval
->type
== REDIS_SET
)
487 for (j
= 0; j
< vectorlen
; j
++)
488 decrRefCount(vector
[j
].obj
);
489 decrRefCount(sortval
);
490 listRelease(operations
);
491 for (j
= 0; j
< vectorlen
; j
++) {
492 if (alpha
&& vector
[j
].u
.cmpobj
)
493 decrRefCount(vector
[j
].u
.cmpobj
);