}
/* Return the value associated to the key with a name obtained
- * substituting the first occurence of '*' in 'pattern' with 'subst' */
+ * substituting the first occurence of '*' in 'pattern' with 'subst'.
+ * The returned object will always have its refcount increased by 1
+ * when it is non-NULL. */
static robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst) {
char *p, *f;
sds spat, ssub;
* to implement the "SORT ... GET #" feature. */
spat = pattern->ptr;
if (spat[0] == '#' && spat[1] == '\0') {
+ incrRefCount(subst);
return subst;
}
/* Lookup substituted key */
initStaticStringObject(keyobj,((char*)&keyname)+(sizeof(long)*2));
o = lookupKeyRead(db,&keyobj);
+ if (o == NULL) return NULL;
+
+ if (fieldlen > 0) {
+ if (o->type != REDIS_HASH || fieldname.len < 1) return NULL;
- if (o != NULL && fieldlen > 0) {
/* Retrieve value from hash by the field name. This operation
* already increases the refcount of the returned object. */
- if (o->type != REDIS_HASH || fieldname.len < 1) {
- return NULL;
- }
initStaticStringObject(fieldobj,((char*)&fieldname)+(sizeof(long)*2));
o = hashGet(o, &fieldobj);
} else {
- if (o->type != REDIS_STRING) {
- return NULL;
- }
+ if (o->type != REDIS_STRING) return NULL;
/* Every object that this function returns needs to have its refcount
* increased. sortCommand decreases it again. */
vector[j].obj);
if (sop->type == REDIS_SORT_GET) {
- if (!val || val->type != REDIS_STRING) {
+ if (!val) {
addReply(c,shared.nullbulk);
} else {
addReplyBulk(c,val);
+ decrRefCount(val);
}
} else {
redisAssert(sop->type == REDIS_SORT_GET); /* always fails */
vector[j].obj);
if (sop->type == REDIS_SORT_GET) {
- if (!val || val->type != REDIS_STRING) {
+ if (!val) {
listAddNodeTail(listPtr,createStringObject("",0));
} else {
+ /* We should do a incrRefCount on val because it is
+ * added to the list, but also a decrRefCount because
+ * it is returned by lookupKeyByPattern. This results
+ * in doing nothing at all. */
listAddNodeTail(listPtr,val);
- incrRefCount(val);
}
} else {
redisAssert(sop->type == REDIS_SORT_GET); /* always fails */