mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-22 16:18:28 -05:00
cleanup: move list pop logic to single function (#7997)
BLPOP when there are elements in the list works in the same way as LPOP does. Due to this they also does the same repetitive action and logic for the same is written at two different places. This is a bad code practice as the one needs the context to change the BLPOP list pop code as well when the LPOP code gets changed. Separated the generic logic from LPOP to a function that is being used by the BLPOP code as well.
This commit is contained in:
parent
a0576bdeea
commit
c170365dcf
@ -1793,6 +1793,7 @@ void listTypeDelete(listTypeIterator *iter, listTypeEntry *entry);
|
|||||||
void listTypeConvert(robj *subject, int enc);
|
void listTypeConvert(robj *subject, int enc);
|
||||||
void unblockClientWaitingData(client *c);
|
void unblockClientWaitingData(client *c);
|
||||||
void popGenericCommand(client *c, int where);
|
void popGenericCommand(client *c, int where);
|
||||||
|
void listElementsRemoved(client *c, robj *key, int where, robj *o);
|
||||||
|
|
||||||
/* MULTI/EXEC/WATCH... */
|
/* MULTI/EXEC/WATCH... */
|
||||||
void unwatchAllKeys(client *c);
|
void unwatchAllKeys(client *c);
|
||||||
|
43
src/t_list.c
43
src/t_list.c
@ -363,26 +363,30 @@ void lsetCommand(client *c) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void popGenericCommand(client *c, int where) {
|
void listElementsRemoved(client *c, robj *key, int where, robj *o) {
|
||||||
robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.null[c->resp]);
|
char *event = (where == LIST_HEAD) ? "lpop" : "rpop";
|
||||||
if (o == NULL || checkType(c,o,OBJ_LIST)) return;
|
|
||||||
|
|
||||||
robj *value = listTypePop(o,where);
|
notifyKeyspaceEvent(NOTIFY_LIST, event, key, c->db->id);
|
||||||
|
if (listTypeLength(o) == 0) {
|
||||||
|
notifyKeyspaceEvent(NOTIFY_GENERIC, "del", key, c->db->id);
|
||||||
|
dbDelete(c->db, key);
|
||||||
|
}
|
||||||
|
signalModifiedKey(c, c->db, key);
|
||||||
|
server.dirty++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void popGenericCommand(client *c, int where) {
|
||||||
|
robj *o = lookupKeyWriteOrReply(c, c->argv[1], shared.null[c->resp]);
|
||||||
|
if (o == NULL || checkType(c, o, OBJ_LIST))
|
||||||
|
return;
|
||||||
|
|
||||||
|
robj *value = listTypePop(o, where);
|
||||||
if (value == NULL) {
|
if (value == NULL) {
|
||||||
addReplyNull(c);
|
addReplyNull(c);
|
||||||
} else {
|
} else {
|
||||||
char *event = (where == LIST_HEAD) ? "lpop" : "rpop";
|
|
||||||
|
|
||||||
addReplyBulk(c,value);
|
addReplyBulk(c,value);
|
||||||
decrRefCount(value);
|
decrRefCount(value);
|
||||||
notifyKeyspaceEvent(NOTIFY_LIST,event,c->argv[1],c->db->id);
|
listElementsRemoved(c,c->argv[1],where,o);
|
||||||
if (listTypeLength(o) == 0) {
|
|
||||||
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",
|
|
||||||
c->argv[1],c->db->id);
|
|
||||||
dbDelete(c->db,c->argv[1]);
|
|
||||||
}
|
|
||||||
signalModifiedKey(c,c->db,c->argv[1]);
|
|
||||||
server.dirty++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -859,7 +863,6 @@ void blockingPopGenericCommand(client *c, int where) {
|
|||||||
} else {
|
} else {
|
||||||
if (listTypeLength(o) != 0) {
|
if (listTypeLength(o) != 0) {
|
||||||
/* Non empty list, this is like a normal [LR]POP. */
|
/* Non empty list, this is like a normal [LR]POP. */
|
||||||
char *event = (where == LIST_HEAD) ? "lpop" : "rpop";
|
|
||||||
robj *value = listTypePop(o,where);
|
robj *value = listTypePop(o,where);
|
||||||
serverAssert(value != NULL);
|
serverAssert(value != NULL);
|
||||||
|
|
||||||
@ -867,15 +870,7 @@ void blockingPopGenericCommand(client *c, int where) {
|
|||||||
addReplyBulk(c,c->argv[j]);
|
addReplyBulk(c,c->argv[j]);
|
||||||
addReplyBulk(c,value);
|
addReplyBulk(c,value);
|
||||||
decrRefCount(value);
|
decrRefCount(value);
|
||||||
notifyKeyspaceEvent(NOTIFY_LIST,event,
|
listElementsRemoved(c,c->argv[j],where,o);
|
||||||
c->argv[j],c->db->id);
|
|
||||||
if (listTypeLength(o) == 0) {
|
|
||||||
dbDelete(c->db,c->argv[j]);
|
|
||||||
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",
|
|
||||||
c->argv[j],c->db->id);
|
|
||||||
}
|
|
||||||
signalModifiedKey(c,c->db,c->argv[j]);
|
|
||||||
server.dirty++;
|
|
||||||
|
|
||||||
/* Replicate it as an [LR]POP instead of B[LR]POP. */
|
/* Replicate it as an [LR]POP instead of B[LR]POP. */
|
||||||
rewriteClientCommandVector(c,2,
|
rewriteClientCommandVector(c,2,
|
||||||
|
Loading…
Reference in New Issue
Block a user