Merge pushGenericCommand and pushxGenericCommand (#8255)
Merge pushGenericCommand and pushxGenericCommand to remove redundancy and simplify code.
This commit is contained in:
parent
049cf8cdf4
commit
1f5a73a530
63
src/t_list.c
63
src/t_list.c
@ -216,76 +216,55 @@ robj *listTypeDup(robj *o) {
|
|||||||
* List Commands
|
* List Commands
|
||||||
*----------------------------------------------------------------------------*/
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
/* Implements LPUSH/RPUSH. */
|
/* Implements LPUSH/RPUSH/LPUSHX/RPUSHX.
|
||||||
void pushGenericCommand(client *c, int where) {
|
* 'xx': push if key exists. */
|
||||||
int j, pushed = 0;
|
void pushGenericCommand(client *c, int where, int xx) {
|
||||||
robj *lobj = lookupKeyWrite(c->db,c->argv[1]);
|
int j;
|
||||||
|
|
||||||
if (checkType(c,lobj,OBJ_LIST)) {
|
robj *lobj = lookupKeyWrite(c->db, c->argv[1]);
|
||||||
|
if (checkType(c,lobj,OBJ_LIST)) return;
|
||||||
|
if (!lobj) {
|
||||||
|
if (xx) {
|
||||||
|
addReply(c, shared.czero);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 2; j < c->argc; j++) {
|
|
||||||
if (!lobj) {
|
|
||||||
lobj = createQuicklistObject();
|
lobj = createQuicklistObject();
|
||||||
quicklistSetOptions(lobj->ptr, server.list_max_ziplist_size,
|
quicklistSetOptions(lobj->ptr, server.list_max_ziplist_size,
|
||||||
server.list_compress_depth);
|
server.list_compress_depth);
|
||||||
dbAdd(c->db,c->argv[1],lobj);
|
dbAdd(c->db,c->argv[1],lobj);
|
||||||
}
|
}
|
||||||
listTypePush(lobj,c->argv[j],where);
|
|
||||||
pushed++;
|
|
||||||
}
|
|
||||||
addReplyLongLong(c, (lobj ? listTypeLength(lobj) : 0));
|
|
||||||
if (pushed) {
|
|
||||||
char *event = (where == LIST_HEAD) ? "lpush" : "rpush";
|
|
||||||
|
|
||||||
|
for (j = 2; j < c->argc; j++) {
|
||||||
|
listTypePush(lobj,c->argv[j],where);
|
||||||
|
server.dirty++;
|
||||||
|
}
|
||||||
|
|
||||||
|
addReplyLongLong(c, listTypeLength(lobj));
|
||||||
|
|
||||||
|
char *event = (where == LIST_HEAD) ? "lpush" : "rpush";
|
||||||
signalModifiedKey(c,c->db,c->argv[1]);
|
signalModifiedKey(c,c->db,c->argv[1]);
|
||||||
notifyKeyspaceEvent(NOTIFY_LIST,event,c->argv[1],c->db->id);
|
notifyKeyspaceEvent(NOTIFY_LIST,event,c->argv[1],c->db->id);
|
||||||
}
|
}
|
||||||
server.dirty += pushed;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* LPUSH <key> <element> [<element> ...] */
|
/* LPUSH <key> <element> [<element> ...] */
|
||||||
void lpushCommand(client *c) {
|
void lpushCommand(client *c) {
|
||||||
pushGenericCommand(c,LIST_HEAD);
|
pushGenericCommand(c,LIST_HEAD,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* RPUSH <key> <element> [<element> ...] */
|
/* RPUSH <key> <element> [<element> ...] */
|
||||||
void rpushCommand(client *c) {
|
void rpushCommand(client *c) {
|
||||||
pushGenericCommand(c,LIST_TAIL);
|
pushGenericCommand(c,LIST_TAIL,0);
|
||||||
}
|
|
||||||
|
|
||||||
/* Implements LPUSHX/RPUSHX. */
|
|
||||||
void pushxGenericCommand(client *c, int where) {
|
|
||||||
int j, pushed = 0;
|
|
||||||
robj *subject;
|
|
||||||
|
|
||||||
if ((subject = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
|
|
||||||
checkType(c,subject,OBJ_LIST)) return;
|
|
||||||
|
|
||||||
for (j = 2; j < c->argc; j++) {
|
|
||||||
listTypePush(subject,c->argv[j],where);
|
|
||||||
pushed++;
|
|
||||||
}
|
|
||||||
|
|
||||||
addReplyLongLong(c,listTypeLength(subject));
|
|
||||||
|
|
||||||
if (pushed) {
|
|
||||||
char *event = (where == LIST_HEAD) ? "lpush" : "rpush";
|
|
||||||
signalModifiedKey(c,c->db,c->argv[1]);
|
|
||||||
notifyKeyspaceEvent(NOTIFY_LIST,event,c->argv[1],c->db->id);
|
|
||||||
}
|
|
||||||
server.dirty += pushed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* LPUSHX <key> <element> [<element> ...] */
|
/* LPUSHX <key> <element> [<element> ...] */
|
||||||
void lpushxCommand(client *c) {
|
void lpushxCommand(client *c) {
|
||||||
pushxGenericCommand(c,LIST_HEAD);
|
pushGenericCommand(c,LIST_HEAD,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* RPUSH <key> <element> [<element> ...] */
|
/* RPUSH <key> <element> [<element> ...] */
|
||||||
void rpushxCommand(client *c) {
|
void rpushxCommand(client *c) {
|
||||||
pushxGenericCommand(c,LIST_TAIL);
|
pushGenericCommand(c,LIST_TAIL,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* LINSERT <key> (BEFORE|AFTER) <pivot> <element> */
|
/* LINSERT <key> (BEFORE|AFTER) <pivot> <element> */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user