2010-06-22 00:07:48 +02:00
|
|
|
#include "redis.h"
|
|
|
|
#include "pqsort.h" /* Partial qsort for SORT+LIMIT */
|
2012-02-01 15:22:28 +01:00
|
|
|
#include <math.h> /* isnan() */
|
2010-06-22 00:07:48 +02:00
|
|
|
|
|
|
|
redisSortOperation *createSortOperation(int type, robj *pattern) {
|
|
|
|
redisSortOperation *so = zmalloc(sizeof(*so));
|
|
|
|
so->type = type;
|
|
|
|
so->pattern = pattern;
|
|
|
|
return so;
|
|
|
|
}
|
|
|
|
|
2012-04-17 13:05:09 +02:00
|
|
|
/* Return the value associated to the key with a name obtained using
|
|
|
|
* the following rules:
|
|
|
|
*
|
|
|
|
* 1) The first occurence of '*' in 'pattern' is substituted with 'subst'.
|
|
|
|
*
|
|
|
|
* 2) If 'pattern' matches the "->" string, everything on the left of
|
|
|
|
* the arrow is treated as the name of an hash field, and the part on the
|
|
|
|
* left as the key name containing an hash. The value of the specified
|
|
|
|
* field is returned.
|
|
|
|
*
|
|
|
|
* 3) If 'pattern' equals "#", the function simply returns 'subst' itself so
|
|
|
|
* that the SORT command can be used like: SORT key GET # to retrieve
|
|
|
|
* the Set/List elements directly.
|
|
|
|
*
|
2010-06-22 00:07:48 +02:00
|
|
|
* The returned object will always have its refcount increased by 1
|
|
|
|
* when it is non-NULL. */
|
|
|
|
robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst) {
|
2012-04-17 13:05:09 +02:00
|
|
|
char *p, *f, *k;
|
2010-06-22 00:07:48 +02:00
|
|
|
sds spat, ssub;
|
2012-04-18 11:37:14 +02:00
|
|
|
robj *keyobj, *fieldobj = NULL, *o;
|
2010-06-22 00:07:48 +02:00
|
|
|
int prefixlen, sublen, postfixlen, fieldlen;
|
|
|
|
|
|
|
|
/* If the pattern is "#" return the substitution object itself in order
|
|
|
|
* to implement the "SORT ... GET #" feature. */
|
|
|
|
spat = pattern->ptr;
|
|
|
|
if (spat[0] == '#' && spat[1] == '\0') {
|
|
|
|
incrRefCount(subst);
|
|
|
|
return subst;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The substitution object may be specially encoded. If so we create
|
|
|
|
* a decoded object on the fly. Otherwise getDecodedObject will just
|
|
|
|
* increment the ref count, that we'll decrement later. */
|
|
|
|
subst = getDecodedObject(subst);
|
|
|
|
ssub = subst->ptr;
|
2012-04-17 13:05:09 +02:00
|
|
|
|
|
|
|
/* If we can't find '*' in the pattern we return NULL as to GET a
|
|
|
|
* fixed key does not make sense. */
|
2010-06-22 00:07:48 +02:00
|
|
|
p = strchr(spat,'*');
|
|
|
|
if (!p) {
|
|
|
|
decrRefCount(subst);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find out if we're dealing with a hash dereference. */
|
2012-04-17 13:05:09 +02:00
|
|
|
if ((f = strstr(p+1, "->")) != NULL && *(f+2) != '\0') {
|
|
|
|
fieldlen = sdslen(spat)-(f-spat)-2;
|
|
|
|
fieldobj = createStringObject(f+2,fieldlen);
|
2010-06-22 00:07:48 +02:00
|
|
|
} else {
|
|
|
|
fieldlen = 0;
|
|
|
|
}
|
|
|
|
|
2012-04-17 13:05:09 +02:00
|
|
|
/* Perform the '*' substitution. */
|
2010-06-22 00:07:48 +02:00
|
|
|
prefixlen = p-spat;
|
|
|
|
sublen = sdslen(ssub);
|
2012-04-17 13:05:09 +02:00
|
|
|
postfixlen = sdslen(spat)-(prefixlen+1)-(fieldlen ? fieldlen+2 : 0);
|
|
|
|
keyobj = createStringObject(NULL,prefixlen+sublen+postfixlen);
|
|
|
|
k = keyobj->ptr;
|
|
|
|
memcpy(k,spat,prefixlen);
|
|
|
|
memcpy(k+prefixlen,ssub,sublen);
|
|
|
|
memcpy(k+prefixlen+sublen,p+1,postfixlen);
|
|
|
|
decrRefCount(subst); /* Incremented by decodeObject() */
|
2010-06-22 00:07:48 +02:00
|
|
|
|
|
|
|
/* Lookup substituted key */
|
2012-04-17 13:05:09 +02:00
|
|
|
o = lookupKeyRead(db,keyobj);
|
|
|
|
if (o == NULL) goto noobj;
|
2010-06-22 00:07:48 +02:00
|
|
|
|
2012-04-18 11:37:14 +02:00
|
|
|
if (fieldobj) {
|
2012-04-17 13:05:09 +02:00
|
|
|
if (o->type != REDIS_HASH) goto noobj;
|
2010-06-22 00:07:48 +02:00
|
|
|
|
|
|
|
/* Retrieve value from hash by the field name. This operation
|
|
|
|
* already increases the refcount of the returned object. */
|
2012-04-17 13:05:09 +02:00
|
|
|
o = hashTypeGetObject(o, fieldobj);
|
2010-06-22 00:07:48 +02:00
|
|
|
} else {
|
2012-04-17 13:05:09 +02:00
|
|
|
if (o->type != REDIS_STRING) goto noobj;
|
2010-06-22 00:07:48 +02:00
|
|
|
|
|
|
|
/* Every object that this function returns needs to have its refcount
|
|
|
|
* increased. sortCommand decreases it again. */
|
|
|
|
incrRefCount(o);
|
|
|
|
}
|
2012-04-17 13:05:09 +02:00
|
|
|
decrRefCount(keyobj);
|
2012-04-18 11:37:14 +02:00
|
|
|
if (fieldobj) decrRefCount(fieldobj);
|
2010-06-22 00:07:48 +02:00
|
|
|
return o;
|
2012-04-17 13:05:09 +02:00
|
|
|
|
|
|
|
noobj:
|
|
|
|
decrRefCount(keyobj);
|
|
|
|
if (fieldlen) decrRefCount(fieldobj);
|
|
|
|
return NULL;
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
|
|
|
|
* the additional parameter is not standard but a BSD-specific we have to
|
|
|
|
* pass sorting parameters via the global 'server' structure */
|
|
|
|
int sortCompare(const void *s1, const void *s2) {
|
|
|
|
const redisSortObject *so1 = s1, *so2 = s2;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
if (!server.sort_alpha) {
|
|
|
|
/* Numeric sorting. Here it's trivial as we precomputed scores */
|
|
|
|
if (so1->u.score > so2->u.score) {
|
|
|
|
cmp = 1;
|
|
|
|
} else if (so1->u.score < so2->u.score) {
|
|
|
|
cmp = -1;
|
|
|
|
} else {
|
2012-02-01 15:22:28 +01:00
|
|
|
/* Objects have the same score, but we don't want the comparison
|
|
|
|
* to be undefined, so we compare objects lexicographycally.
|
|
|
|
* This way the result of SORT is deterministic. */
|
|
|
|
cmp = compareStringObjects(so1->obj,so2->obj);
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Alphanumeric sorting */
|
|
|
|
if (server.sort_bypattern) {
|
|
|
|
if (!so1->u.cmpobj || !so2->u.cmpobj) {
|
|
|
|
/* At least one compare object is NULL */
|
|
|
|
if (so1->u.cmpobj == so2->u.cmpobj)
|
|
|
|
cmp = 0;
|
|
|
|
else if (so1->u.cmpobj == NULL)
|
|
|
|
cmp = -1;
|
|
|
|
else
|
|
|
|
cmp = 1;
|
|
|
|
} else {
|
|
|
|
/* We have both the objects, use strcoll */
|
|
|
|
cmp = strcoll(so1->u.cmpobj->ptr,so2->u.cmpobj->ptr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Compare elements directly. */
|
|
|
|
cmp = compareStringObjects(so1->obj,so2->obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return server.sort_desc ? -cmp : cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The SORT command is the most complex command in Redis. Warning: this code
|
|
|
|
* is optimized for speed and a bit less for readability */
|
|
|
|
void sortCommand(redisClient *c) {
|
|
|
|
list *operations;
|
|
|
|
unsigned int outputlen = 0;
|
|
|
|
int desc = 0, alpha = 0;
|
2011-12-19 19:29:46 +08:00
|
|
|
long limit_start = 0, limit_count = -1, start, end;
|
2010-06-22 00:07:48 +02:00
|
|
|
int j, dontsort = 0, vectorlen;
|
|
|
|
int getop = 0; /* GET operation counter */
|
2012-02-01 15:22:28 +01:00
|
|
|
int int_convertion_error = 0;
|
2010-06-22 00:07:48 +02:00
|
|
|
robj *sortval, *sortby = NULL, *storekey = NULL;
|
|
|
|
redisSortObject *vector; /* Resulting vector to sort */
|
|
|
|
|
|
|
|
/* Lookup the key to sort. It must be of the right types */
|
|
|
|
sortval = lookupKeyRead(c->db,c->argv[1]);
|
2011-12-01 16:07:55 +01:00
|
|
|
if (sortval && sortval->type != REDIS_SET && sortval->type != REDIS_LIST &&
|
2010-06-22 00:07:48 +02:00
|
|
|
sortval->type != REDIS_ZSET)
|
|
|
|
{
|
|
|
|
addReply(c,shared.wrongtypeerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a list of operations to perform for every sorted element.
|
|
|
|
* Operations can be GET/DEL/INCR/DECR */
|
|
|
|
operations = listCreate();
|
|
|
|
listSetFreeMethod(operations,zfree);
|
|
|
|
j = 2;
|
|
|
|
|
|
|
|
/* Now we need to protect sortval incrementing its count, in the future
|
|
|
|
* SORT may have options able to overwrite/delete keys during the sorting
|
|
|
|
* and the sorted key itself may get destroied */
|
2011-12-01 16:07:55 +01:00
|
|
|
if (sortval)
|
|
|
|
incrRefCount(sortval);
|
|
|
|
else
|
|
|
|
sortval = createListObject();
|
2010-06-22 00:07:48 +02:00
|
|
|
|
|
|
|
/* The SORT command has an SQL-alike syntax, parse it */
|
|
|
|
while(j < c->argc) {
|
|
|
|
int leftargs = c->argc-j-1;
|
|
|
|
if (!strcasecmp(c->argv[j]->ptr,"asc")) {
|
|
|
|
desc = 0;
|
|
|
|
} else if (!strcasecmp(c->argv[j]->ptr,"desc")) {
|
|
|
|
desc = 1;
|
|
|
|
} else if (!strcasecmp(c->argv[j]->ptr,"alpha")) {
|
|
|
|
alpha = 1;
|
|
|
|
} else if (!strcasecmp(c->argv[j]->ptr,"limit") && leftargs >= 2) {
|
2011-12-19 19:29:46 +08:00
|
|
|
if ((getLongFromObjectOrReply(c, c->argv[j+1], &limit_start, NULL) != REDIS_OK) ||
|
|
|
|
(getLongFromObjectOrReply(c, c->argv[j+2], &limit_count, NULL) != REDIS_OK)) return;
|
2010-06-22 00:07:48 +02:00
|
|
|
j+=2;
|
|
|
|
} else if (!strcasecmp(c->argv[j]->ptr,"store") && leftargs >= 1) {
|
|
|
|
storekey = c->argv[j+1];
|
|
|
|
j++;
|
|
|
|
} else if (!strcasecmp(c->argv[j]->ptr,"by") && leftargs >= 1) {
|
|
|
|
sortby = c->argv[j+1];
|
|
|
|
/* If the BY pattern does not contain '*', i.e. it is constant,
|
|
|
|
* we don't need to sort nor to lookup the weight keys. */
|
|
|
|
if (strchr(c->argv[j+1]->ptr,'*') == NULL) dontsort = 1;
|
|
|
|
j++;
|
|
|
|
} else if (!strcasecmp(c->argv[j]->ptr,"get") && leftargs >= 1) {
|
|
|
|
listAddNodeTail(operations,createSortOperation(
|
|
|
|
REDIS_SORT_GET,c->argv[j+1]));
|
|
|
|
getop++;
|
|
|
|
j++;
|
|
|
|
} else {
|
|
|
|
decrRefCount(sortval);
|
|
|
|
listRelease(operations);
|
|
|
|
addReply(c,shared.syntaxerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:05:45 +01:00
|
|
|
/* If we have STORE we need to force sorting for deterministic output
|
|
|
|
* and replication. We use alpha sorting since this is guaranteed to
|
Scripting: Force SORT BY constant determinism inside SORT itself.
SORT is able to return (faster than when ordering) unordered output if
the "BY" clause is used with a constant value. However we try to play
well with scripting requirements of determinism providing always sorted
outputs when SORT (and other similar commands) are called by Lua
scripts.
However we used the general mechanism in place in scripting in order to
reorder SORT output, that is, if the command has the "S" flag set, the
Lua scripting engine will take an additional step when converting a
multi bulk reply to Lua value, calling a Lua sorting function.
This is suboptimal as we can do it faster inside SORT itself.
This is also broken as issue #545 shows us: basically when SORT is used
with a constant BY, and additionally also GET is used, the Lua scripting
engine was trying to order the output as a flat array, while it was
actually a list of key-value pairs.
What we do know is to recognized if the caller of SORT is the Lua client
(since we can check this using the REDIS_LUA_CLIENT flag). If so, and if
a "don't sort" condition is triggered by the BY option with a constant
string, we force the lexicographical sorting.
This commit fixes this bug and improves the performance, and at the same
time simplifies the implementation. This does not mean I'm smart today,
it means I was stupid when I committed the original implementation ;)
2012-09-05 01:12:41 +02:00
|
|
|
* work with any input.
|
|
|
|
*
|
|
|
|
* We also want determinism when SORT is called from Lua scripts, so
|
|
|
|
* in this case we also force alpha sorting. */
|
|
|
|
if ((storekey || c->flags & REDIS_LUA_CLIENT) && dontsort) {
|
2012-02-01 17:05:45 +01:00
|
|
|
dontsort = 0;
|
|
|
|
alpha = 1;
|
|
|
|
sortby = NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-14 13:30:06 +01:00
|
|
|
/* Destructively convert encoded sorted sets for SORT. */
|
2011-12-01 16:07:55 +01:00
|
|
|
if (sortval->type == REDIS_ZSET)
|
|
|
|
zsetConvert(sortval, REDIS_ENCODING_SKIPLIST);
|
2011-03-14 13:30:06 +01:00
|
|
|
|
2010-06-22 00:07:48 +02:00
|
|
|
/* Load the sorting vector with all the objects to sort */
|
|
|
|
switch(sortval->type) {
|
|
|
|
case REDIS_LIST: vectorlen = listTypeLength(sortval); break;
|
2010-08-21 11:15:31 +02:00
|
|
|
case REDIS_SET: vectorlen = setTypeSize(sortval); break;
|
2010-06-22 00:07:48 +02:00
|
|
|
case REDIS_ZSET: vectorlen = dictSize(((zset*)sortval->ptr)->dict); break;
|
|
|
|
default: vectorlen = 0; redisPanic("Bad SORT type"); /* Avoid GCC warning */
|
|
|
|
}
|
|
|
|
vector = zmalloc(sizeof(redisSortObject)*vectorlen);
|
|
|
|
j = 0;
|
|
|
|
|
|
|
|
if (sortval->type == REDIS_LIST) {
|
|
|
|
listTypeIterator *li = listTypeInitIterator(sortval,0,REDIS_TAIL);
|
|
|
|
listTypeEntry entry;
|
|
|
|
while(listTypeNext(li,&entry)) {
|
|
|
|
vector[j].obj = listTypeGet(&entry);
|
|
|
|
vector[j].u.score = 0;
|
|
|
|
vector[j].u.cmpobj = NULL;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
listTypeReleaseIterator(li);
|
2010-08-21 11:15:31 +02:00
|
|
|
} else if (sortval->type == REDIS_SET) {
|
2010-08-21 11:25:13 +02:00
|
|
|
setTypeIterator *si = setTypeInitIterator(sortval);
|
2010-08-21 11:15:31 +02:00
|
|
|
robj *ele;
|
2010-12-09 21:11:56 +01:00
|
|
|
while((ele = setTypeNextObject(si)) != NULL) {
|
2010-08-21 11:15:31 +02:00
|
|
|
vector[j].obj = ele;
|
|
|
|
vector[j].u.score = 0;
|
|
|
|
vector[j].u.cmpobj = NULL;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
setTypeReleaseIterator(si);
|
|
|
|
} else if (sortval->type == REDIS_ZSET) {
|
|
|
|
dict *set = ((zset*)sortval->ptr)->dict;
|
2010-06-22 00:07:48 +02:00
|
|
|
dictIterator *di;
|
|
|
|
dictEntry *setele;
|
|
|
|
di = dictGetIterator(set);
|
|
|
|
while((setele = dictNext(di)) != NULL) {
|
2011-11-08 17:07:55 +01:00
|
|
|
vector[j].obj = dictGetKey(setele);
|
2010-06-22 00:07:48 +02:00
|
|
|
vector[j].u.score = 0;
|
|
|
|
vector[j].u.cmpobj = NULL;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
dictReleaseIterator(di);
|
2010-08-21 11:15:31 +02:00
|
|
|
} else {
|
|
|
|
redisPanic("Unknown type");
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
2011-10-04 18:43:03 +02:00
|
|
|
redisAssertWithInfo(c,sortval,j == vectorlen);
|
2010-06-22 00:07:48 +02:00
|
|
|
|
|
|
|
/* Now it's time to load the right scores in the sorting vector */
|
|
|
|
if (dontsort == 0) {
|
|
|
|
for (j = 0; j < vectorlen; j++) {
|
|
|
|
robj *byval;
|
|
|
|
if (sortby) {
|
|
|
|
/* lookup value to sort by */
|
|
|
|
byval = lookupKeyByPattern(c->db,sortby,vector[j].obj);
|
|
|
|
if (!byval) continue;
|
|
|
|
} else {
|
|
|
|
/* use object itself to sort by */
|
|
|
|
byval = vector[j].obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alpha) {
|
|
|
|
if (sortby) vector[j].u.cmpobj = getDecodedObject(byval);
|
|
|
|
} else {
|
|
|
|
if (byval->encoding == REDIS_ENCODING_RAW) {
|
2012-02-01 15:22:28 +01:00
|
|
|
char *eptr;
|
|
|
|
|
|
|
|
vector[j].u.score = strtod(byval->ptr,&eptr);
|
|
|
|
if (eptr[0] != '\0' || errno == ERANGE ||
|
|
|
|
isnan(vector[j].u.score))
|
|
|
|
{
|
|
|
|
int_convertion_error = 1;
|
|
|
|
}
|
2010-06-22 00:07:48 +02:00
|
|
|
} else if (byval->encoding == REDIS_ENCODING_INT) {
|
|
|
|
/* Don't need to decode the object if it's
|
|
|
|
* integer-encoded (the only encoding supported) so
|
|
|
|
* far. We can just cast it */
|
|
|
|
vector[j].u.score = (long)byval->ptr;
|
|
|
|
} else {
|
2011-10-04 18:43:03 +02:00
|
|
|
redisAssertWithInfo(c,sortval,1 != 1);
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* when the object was retrieved using lookupKeyByPattern,
|
|
|
|
* its refcount needs to be decreased. */
|
|
|
|
if (sortby) {
|
|
|
|
decrRefCount(byval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are ready to sort the vector... perform a bit of sanity check
|
|
|
|
* on the LIMIT option too. We'll use a partial version of quicksort. */
|
|
|
|
start = (limit_start < 0) ? 0 : limit_start;
|
|
|
|
end = (limit_count < 0) ? vectorlen-1 : start+limit_count-1;
|
|
|
|
if (start >= vectorlen) {
|
|
|
|
start = vectorlen-1;
|
|
|
|
end = vectorlen-2;
|
|
|
|
}
|
|
|
|
if (end >= vectorlen) end = vectorlen-1;
|
|
|
|
|
|
|
|
if (dontsort == 0) {
|
|
|
|
server.sort_desc = desc;
|
|
|
|
server.sort_alpha = alpha;
|
|
|
|
server.sort_bypattern = sortby ? 1 : 0;
|
|
|
|
if (sortby && (start != 0 || end != vectorlen-1))
|
|
|
|
pqsort(vector,vectorlen,sizeof(redisSortObject),sortCompare, start,end);
|
|
|
|
else
|
|
|
|
qsort(vector,vectorlen,sizeof(redisSortObject),sortCompare);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send command output to the output buffer, performing the specified
|
|
|
|
* GET/DEL/INCR/DECR operations if any. */
|
|
|
|
outputlen = getop ? getop*(end-start+1) : end-start+1;
|
2012-02-01 15:22:28 +01:00
|
|
|
if (int_convertion_error) {
|
|
|
|
addReplyError(c,"One or more scores can't be converted into double");
|
|
|
|
} else if (storekey == NULL) {
|
2010-06-22 00:07:48 +02:00
|
|
|
/* STORE option not specified, sent the sorting result to client */
|
2010-09-02 12:38:34 +02:00
|
|
|
addReplyMultiBulkLen(c,outputlen);
|
2010-06-22 00:07:48 +02:00
|
|
|
for (j = start; j <= end; j++) {
|
|
|
|
listNode *ln;
|
|
|
|
listIter li;
|
|
|
|
|
|
|
|
if (!getop) addReplyBulk(c,vector[j].obj);
|
|
|
|
listRewind(operations,&li);
|
|
|
|
while((ln = listNext(&li))) {
|
|
|
|
redisSortOperation *sop = ln->value;
|
|
|
|
robj *val = lookupKeyByPattern(c->db,sop->pattern,
|
|
|
|
vector[j].obj);
|
|
|
|
|
|
|
|
if (sop->type == REDIS_SORT_GET) {
|
|
|
|
if (!val) {
|
|
|
|
addReply(c,shared.nullbulk);
|
|
|
|
} else {
|
|
|
|
addReplyBulk(c,val);
|
|
|
|
decrRefCount(val);
|
|
|
|
}
|
|
|
|
} else {
|
2011-10-04 18:43:03 +02:00
|
|
|
/* Always fails */
|
|
|
|
redisAssertWithInfo(c,sortval,sop->type == REDIS_SORT_GET);
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
robj *sobj = createZiplistObject();
|
|
|
|
|
|
|
|
/* STORE option specified, set the sorting result as a List object */
|
|
|
|
for (j = start; j <= end; j++) {
|
|
|
|
listNode *ln;
|
|
|
|
listIter li;
|
|
|
|
|
|
|
|
if (!getop) {
|
|
|
|
listTypePush(sobj,vector[j].obj,REDIS_TAIL);
|
|
|
|
} else {
|
|
|
|
listRewind(operations,&li);
|
|
|
|
while((ln = listNext(&li))) {
|
|
|
|
redisSortOperation *sop = ln->value;
|
|
|
|
robj *val = lookupKeyByPattern(c->db,sop->pattern,
|
|
|
|
vector[j].obj);
|
|
|
|
|
|
|
|
if (sop->type == REDIS_SORT_GET) {
|
|
|
|
if (!val) val = createStringObject("",0);
|
|
|
|
|
|
|
|
/* listTypePush does an incrRefCount, so we should take care
|
|
|
|
* care of the incremented refcount caused by either
|
|
|
|
* lookupKeyByPattern or createStringObject("",0) */
|
|
|
|
listTypePush(sobj,val,REDIS_TAIL);
|
|
|
|
decrRefCount(val);
|
|
|
|
} else {
|
2011-10-04 18:43:03 +02:00
|
|
|
/* Always fails */
|
|
|
|
redisAssertWithInfo(c,sortval,sop->type == REDIS_SORT_GET);
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-30 07:36:49 +01:00
|
|
|
if (outputlen) {
|
|
|
|
setKey(c->db,storekey,sobj);
|
|
|
|
server.dirty += outputlen;
|
|
|
|
} else if (dbDelete(c->db,storekey)) {
|
|
|
|
signalModifiedKey(c->db,storekey);
|
|
|
|
server.dirty++;
|
|
|
|
}
|
2011-06-14 15:34:27 +02:00
|
|
|
decrRefCount(sobj);
|
2010-09-02 14:30:56 +02:00
|
|
|
addReplyLongLong(c,outputlen);
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup */
|
2010-08-21 11:15:31 +02:00
|
|
|
if (sortval->type == REDIS_LIST || sortval->type == REDIS_SET)
|
2010-06-22 00:07:48 +02:00
|
|
|
for (j = 0; j < vectorlen; j++)
|
|
|
|
decrRefCount(vector[j].obj);
|
|
|
|
decrRefCount(sortval);
|
|
|
|
listRelease(operations);
|
|
|
|
for (j = 0; j < vectorlen; j++) {
|
|
|
|
if (alpha && vector[j].u.cmpobj)
|
|
|
|
decrRefCount(vector[j].u.cmpobj);
|
|
|
|
}
|
|
|
|
zfree(vector);
|
|
|
|
}
|
|
|
|
|
|
|
|
|