Permalink
Browse files

Variadic ZREM

  • Loading branch information...
1 parent 5150151 commit 913090ec6370c7dba8beb478c4876c1fc4e60036 @antirez committed May 31, 2011
Showing with 33 additions and 24 deletions.
  1. +1 −1 src/redis.c
  2. +32 −23 src/t_zset.c
View
@@ -118,7 +118,7 @@ struct redisCommand readonlyCommandTable[] = {
{"smembers",sinterCommand,2,0,NULL,1,1,1},
{"zadd",zaddCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1},
{"zincrby",zincrbyCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1},
- {"zrem",zremCommand,3,0,NULL,1,1,1},
+ {"zrem",zremCommand,-3,0,NULL,1,1,1},
{"zremrangebyscore",zremrangebyscoreCommand,4,0,NULL,1,1,1},
{"zremrangebyrank",zremrangebyrankCommand,4,0,NULL,1,1,1},
{"zunionstore",zunionstoreCommand,-4,REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0},
View
@@ -951,48 +951,57 @@ void zincrbyCommand(redisClient *c) {
void zremCommand(redisClient *c) {
robj *key = c->argv[1];
- robj *ele = c->argv[2];
robj *zobj;
+ int deleted = 0, j;
if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL ||
checkType(c,zobj,REDIS_ZSET)) return;
if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
unsigned char *eptr;
- if ((eptr = zzlFind(zobj->ptr,ele,NULL)) != NULL) {
- zobj->ptr = zzlDelete(zobj->ptr,eptr);
- if (zzlLength(zobj->ptr) == 0) dbDelete(c->db,key);
- } else {
- addReply(c,shared.czero);
- return;
+ for (j = 2; j < c->argc; j++) {
+ if ((eptr = zzlFind(zobj->ptr,c->argv[j],NULL)) != NULL) {
+ deleted++;
+ zobj->ptr = zzlDelete(zobj->ptr,eptr);
+ if (zzlLength(zobj->ptr) == 0) {
+ dbDelete(c->db,key);
+ break;
+ }
+ }
}
} else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
zset *zs = zobj->ptr;
dictEntry *de;
double score;
- de = dictFind(zs->dict,ele);
- if (de != NULL) {
- /* Delete from the skiplist */
- score = *(double*)dictGetEntryVal(de);
- redisAssert(zslDelete(zs->zsl,score,ele));
-
- /* Delete from the hash table */
- dictDelete(zs->dict,ele);
- if (htNeedsResize(zs->dict)) dictResize(zs->dict);
- if (dictSize(zs->dict) == 0) dbDelete(c->db,key);
- } else {
- addReply(c,shared.czero);
- return;
+ for (j = 2; j < c->argc; j++) {
+ de = dictFind(zs->dict,c->argv[j]);
+ if (de != NULL) {
+ deleted++;
+
+ /* Delete from the skiplist */
+ score = *(double*)dictGetEntryVal(de);
+ redisAssert(zslDelete(zs->zsl,score,c->argv[j]));
+
+ /* Delete from the hash table */
+ dictDelete(zs->dict,c->argv[j]);
+ if (htNeedsResize(zs->dict)) dictResize(zs->dict);
+ if (dictSize(zs->dict) == 0) {
+ dbDelete(c->db,key);
+ break;
+ }
+ }
}
} else {
redisPanic("Unknown sorted set encoding");
}
- touchWatchedKey(c->db,key);
- server.dirty++;
- addReply(c,shared.cone);
+ if (deleted) {
+ touchWatchedKey(c->db,key);
+ server.dirty += deleted;
+ }
+ addReplyLongLong(c,deleted);
}
void zremrangebyscoreCommand(redisClient *c) {

0 comments on commit 913090e

Please sign in to comment.