mirror of
https://github.com/fluencelabs/redis
synced 2025-03-19 17:10:50 +00:00
generic pop and length function for ziplist encoding
This commit is contained in:
parent
c7d9d662a4
commit
d72562f7ba
81
redis.c
81
redis.c
@ -4790,6 +4790,52 @@ static void lPush(robj *subject, robj *value, int where) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static robj *lPop(robj *subject, int where) {
|
||||||
|
robj *value = NULL;
|
||||||
|
if (subject->encoding == REDIS_ENCODING_ZIPLIST) {
|
||||||
|
unsigned char *p;
|
||||||
|
char *v;
|
||||||
|
unsigned int vlen;
|
||||||
|
long long vval;
|
||||||
|
int pos = (where == REDIS_HEAD) ? 0 : -1;
|
||||||
|
p = ziplistIndex(subject->ptr,pos);
|
||||||
|
if (ziplistGet(p,&v,&vlen,&vval)) {
|
||||||
|
if (v) {
|
||||||
|
value = createStringObject(v,vlen);
|
||||||
|
} else {
|
||||||
|
value = createStringObjectFromLongLong(vval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
subject->ptr = ziplistDelete(subject->ptr,&p,ZIPLIST_TAIL);
|
||||||
|
} else if (subject->encoding == REDIS_ENCODING_LIST) {
|
||||||
|
list *list = subject->ptr;
|
||||||
|
listNode *ln;
|
||||||
|
if (where == REDIS_HEAD) {
|
||||||
|
ln = listFirst(list);
|
||||||
|
} else {
|
||||||
|
ln = listLast(list);
|
||||||
|
}
|
||||||
|
if (ln != NULL) {
|
||||||
|
value = listNodeValue(ln);
|
||||||
|
incrRefCount(value);
|
||||||
|
listDelNode(list,ln);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
redisPanic("Unknown list encoding");
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned long lLength(robj *subject) {
|
||||||
|
if (subject->encoding == REDIS_ENCODING_ZIPLIST) {
|
||||||
|
return ziplistLen(subject->ptr);
|
||||||
|
} else if (subject->encoding == REDIS_ENCODING_LIST) {
|
||||||
|
return listLength((list*)subject->ptr);
|
||||||
|
} else {
|
||||||
|
redisPanic("Unknown list encoding");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void pushGenericCommand(redisClient *c, int where) {
|
static void pushGenericCommand(redisClient *c, int where) {
|
||||||
robj *lobj = lookupKeyWrite(c->db,c->argv[1]);
|
robj *lobj = lookupKeyWrite(c->db,c->argv[1]);
|
||||||
@ -4826,14 +4872,9 @@ static void rpushCommand(redisClient *c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void llenCommand(redisClient *c) {
|
static void llenCommand(redisClient *c) {
|
||||||
robj *o;
|
robj *o = lookupKeyReadOrReply(c,c->argv[1],shared.czero);
|
||||||
list *l;
|
if (o == NULL || checkType(c,o,REDIS_LIST)) return;
|
||||||
|
addReplyUlong(c,lLength(o));
|
||||||
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
|
|
||||||
checkType(c,o,REDIS_LIST)) return;
|
|
||||||
|
|
||||||
l = o->ptr;
|
|
||||||
addReplyUlong(c,listLength(l));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void lindexCommand(redisClient *c) {
|
static void lindexCommand(redisClient *c) {
|
||||||
@ -4880,26 +4921,16 @@ static void lsetCommand(redisClient *c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void popGenericCommand(redisClient *c, int where) {
|
static void popGenericCommand(redisClient *c, int where) {
|
||||||
robj *o;
|
robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk);
|
||||||
list *list;
|
if (o == NULL || checkType(c,o,REDIS_LIST)) return;
|
||||||
listNode *ln;
|
|
||||||
|
|
||||||
if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
|
robj *value = lPop(o,where);
|
||||||
checkType(c,o,REDIS_LIST)) return;
|
if (value == NULL) {
|
||||||
list = o->ptr;
|
|
||||||
|
|
||||||
if (where == REDIS_HEAD)
|
|
||||||
ln = listFirst(list);
|
|
||||||
else
|
|
||||||
ln = listLast(list);
|
|
||||||
|
|
||||||
if (ln == NULL) {
|
|
||||||
addReply(c,shared.nullbulk);
|
addReply(c,shared.nullbulk);
|
||||||
} else {
|
} else {
|
||||||
robj *ele = listNodeValue(ln);
|
addReplyBulk(c,value);
|
||||||
addReplyBulk(c,ele);
|
decrRefCount(value);
|
||||||
listDelNode(list,ln);
|
if (lLength(o) == 0) deleteKey(c->db,c->argv[1]);
|
||||||
if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
|
|
||||||
server.dirty++;
|
server.dirty++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user