Skip to content
This repository has been archived by the owner on Feb 20, 2021. It is now read-only.

Commit

Permalink
Empty value trigger key removal in all the operations
Browse files Browse the repository at this point in the history
  • Loading branch information
antirez committed Mar 23, 2010
1 parent 44efe66 commit 3ea27d3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions TODO
Expand Up @@ -64,6 +64,7 @@ SMALL ONES:
* Don't save empty lists / sets / zsets on disk with snapshotting.
* Remove keys when a list / set / zset reaches length of 0.
* An option to exec a command slave-side if the master connection is lost: even cooler: if the script returns "0" the slave elects itself as master, otherwise continue trying to reconnect.
* PING the master from time to time to check if it's gone.

THE "MAYBE" TODO LIST: things that may or may not get implemented
=================================================================
Expand Down
48 changes: 38 additions & 10 deletions redis.c
Expand Up @@ -4373,6 +4373,7 @@ static void popGenericCommand(redisClient *c, int where) {
robj *ele = listNodeValue(ln);
addReplyBulk(c,ele);
listDelNode(list,ln);
if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
server.dirty++;
}
}
Expand Down Expand Up @@ -4465,6 +4466,7 @@ static void ltrimCommand(redisClient *c) {
ln = listLast(list);
listDelNode(list,ln);
}
if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
server.dirty++;
addReply(c,shared.ok);
}
Expand Down Expand Up @@ -4498,6 +4500,7 @@ static void lremCommand(redisClient *c) {
}
ln = next;
}
if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",removed));
}

Expand Down Expand Up @@ -4557,6 +4560,7 @@ static void rpoplpushcommand(redisClient *c) {

/* Finally remove the element from the source list */
listDelNode(srclist,ln);
if (listLength(srclist) == 0) deleteKey(c->db,c->argv[1]);
server.dirty++;
}
}
Expand Down Expand Up @@ -4595,6 +4599,7 @@ static void sremCommand(redisClient *c) {
if (dictDelete(set->ptr,c->argv[2]) == DICT_OK) {
server.dirty++;
if (htNeedsResize(set->ptr)) dictResize(set->ptr);
if (dictSize((dict*)set->ptr) == 0) deleteKey(c->db,c->argv[1]);
addReply(c,shared.cone);
} else {
addReply(c,shared.czero);
Expand Down Expand Up @@ -4624,6 +4629,8 @@ static void smoveCommand(redisClient *c) {
addReply(c,shared.czero);
return;
}
if (dictSize((dict*)srcset->ptr) == 0 && srcset != dstset)
deleteKey(c->db,c->argv[1]);
server.dirty++;
/* Add the element to the destination set */
if (!dstset) {
Expand Down Expand Up @@ -4675,6 +4682,7 @@ static void spopCommand(redisClient *c) {
addReplyBulk(c,ele);
dictDelete(set->ptr,ele);
if (htNeedsResize(set->ptr)) dictResize(set->ptr);
if (dictSize((dict*)set->ptr) == 0) deleteKey(c->db,c->argv[1]);
server.dirty++;
}
}
Expand Down Expand Up @@ -4776,10 +4784,15 @@ static void sinterGenericCommand(redisClient *c, robj **setskeys, unsigned long
dictReleaseIterator(di);

if (dstkey) {
/* Store the resulting set into the target */
/* Store the resulting set into the target, if the intersection
* is not an empty set. */
deleteKey(c->db,dstkey);
dictAdd(c->db->dict,dstkey,dstset);
incrRefCount(dstkey);
if (dictSize((dict*)dstset->ptr) > 0) {
dictAdd(c->db->dict,dstkey,dstset);
incrRefCount(dstkey);
} else {
decrRefCount(dstset);
}
}

if (!dstkey) {
Expand Down Expand Up @@ -4878,8 +4891,12 @@ static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnu
/* If we have a target key where to store the resulting set
* create this key with the result set inside */
deleteKey(c->db,dstkey);
dictAdd(c->db->dict,dstkey,dstset);
incrRefCount(dstkey);
if (dictSize((dict*)dstset->ptr) > 0) {
dictAdd(c->db->dict,dstkey,dstset);
incrRefCount(dstkey);
} else {
decrRefCount(dstset);
}
}

/* Cleanup */
Expand Down Expand Up @@ -5339,6 +5356,7 @@ static void zremCommand(redisClient *c) {
/* Delete from the hash table */
dictDelete(zs->dict,c->argv[2]);
if (htNeedsResize(zs->dict)) dictResize(zs->dict);
if (dictSize(zs->dict) == 0) deleteKey(c->db,c->argv[1]);
server.dirty++;
addReply(c,shared.cone);
}
Expand All @@ -5356,6 +5374,7 @@ static void zremrangebyscoreCommand(redisClient *c) {
zs = zsetobj->ptr;
deleted = zslDeleteRangeByScore(zs->zsl,min,max,zs->dict);
if (htNeedsResize(zs->dict)) dictResize(zs->dict);
if (dictSize(zs->dict) == 0) deleteKey(c->db,c->argv[1]);
server.dirty += deleted;
addReplyLong(c,deleted);
}
Expand Down Expand Up @@ -5390,6 +5409,7 @@ static void zremrangebyrankCommand(redisClient *c) {
* use 1-based rank */
deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict);
if (htNeedsResize(zs->dict)) dictResize(zs->dict);
if (dictSize(zs->dict) == 0) deleteKey(c->db,c->argv[1]);
server.dirty += deleted;
addReplyLong(c, deleted);
}
Expand Down Expand Up @@ -5573,11 +5593,15 @@ static void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) {
}

deleteKey(c->db,dstkey);
dictAdd(c->db->dict,dstkey,dstobj);
incrRefCount(dstkey);

addReplyLong(c, dstzset->zsl->length);
server.dirty++;
if (dstzset->zsl->length) {
dictAdd(c->db->dict,dstkey,dstobj);
incrRefCount(dstkey);
addReplyLong(c, dstzset->zsl->length);
server.dirty++;
} else {
decrRefCount(dstzset);
addReply(c, shared.czero);
}
zfree(src);
}

Expand Down Expand Up @@ -5962,8 +5986,12 @@ static void hdelCommand(redisClient *c) {
(unsigned char*) field->ptr,
sdslen(field->ptr), &deleted);
decrRefCount(field);
if (zipmapLen((unsigned char*) o->ptr) == 0)
deleteKey(c->db,c->argv[1]);
} else {
deleted = dictDelete((dict*)o->ptr,c->argv[2]) == DICT_OK;
if (htNeedsResize(o->ptr)) dictResize(o->ptr);
if (dictSize((dict*)o->ptr) == 0) deleteKey(c->db,c->argv[1]);
}
if (deleted) server.dirty++;
addReply(c,deleted ? shared.cone : shared.czero);
Expand Down

0 comments on commit 3ea27d3

Please sign in to comment.