]> git.saurik.com Git - redis.git/commitdiff
variadic HDEL with tests
authorantirez <antirez@gmail.com>
Tue, 19 Apr 2011 15:07:55 +0000 (17:07 +0200)
committerantirez <antirez@gmail.com>
Tue, 19 Apr 2011 15:07:55 +0000 (17:07 +0200)
src/redis.c
src/t_hash.c
tests/unit/type/hash.tcl

index 5f07903cb89e5304c29e367fe5efbbb0756c423f..bf9ced2cd0a3b30f06ad19c569c2d1a47cb435a5 100644 (file)
@@ -138,7 +138,7 @@ struct redisCommand redisCommandTable[] = {
     {"hmset",hmsetCommand,-4,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
     {"hmget",hmgetCommand,-3,0,NULL,1,1,1,0,0},
     {"hincrby",hincrbyCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
     {"hmset",hmsetCommand,-4,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
     {"hmget",hmgetCommand,-3,0,NULL,1,1,1,0,0},
     {"hincrby",hincrbyCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
-    {"hdel",hdelCommand,3,0,NULL,1,1,1,0,0},
+    {"hdel",hdelCommand,-3,0,NULL,1,1,1,0,0},
     {"hlen",hlenCommand,2,0,NULL,1,1,1,0,0},
     {"hkeys",hkeysCommand,2,0,NULL,1,1,1,0,0},
     {"hvals",hvalsCommand,2,0,NULL,1,1,1,0,0},
     {"hlen",hlenCommand,2,0,NULL,1,1,1,0,0},
     {"hkeys",hkeysCommand,2,0,NULL,1,1,1,0,0},
     {"hvals",hvalsCommand,2,0,NULL,1,1,1,0,0},
index 5e7525bd13a383f38dc54f1420783c01a4acaf64..4b9b37d69fed10b3c3586583dc0b38997647e887 100644 (file)
@@ -396,17 +396,22 @@ void hmgetCommand(redisClient *c) {
 
 void hdelCommand(redisClient *c) {
     robj *o;
 
 void hdelCommand(redisClient *c) {
     robj *o;
+    int j, deleted = 0;
+
     if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
         checkType(c,o,REDIS_HASH)) return;
 
     if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
         checkType(c,o,REDIS_HASH)) return;
 
-    if (hashTypeDelete(o,c->argv[2])) {
-        if (hashTypeLength(o) == 0) dbDelete(c->db,c->argv[1]);
-        addReply(c,shared.cone);
+    for (j = 2; j < c->argc; j++) {
+        if (hashTypeDelete(o,c->argv[j])) {
+            if (hashTypeLength(o) == 0) dbDelete(c->db,c->argv[1]);
+            deleted++;
+        }
+    }
+    if (deleted) {
         signalModifiedKey(c->db,c->argv[1]);
         signalModifiedKey(c->db,c->argv[1]);
-        server.dirty++;
-    } else {
-        addReply(c,shared.czero);
+        server.dirty += deleted;
     }
     }
+    addReplyLongLong(c,deleted);
 }
 
 void hlenCommand(redisClient *c) {
 }
 
 void hlenCommand(redisClient *c) {
index 8559dc3c3801c4a21513906318edf9141103b930..9b043d3f3c3614f8f8154922a1c36dc4bd7dbacc 100644 (file)
@@ -226,6 +226,15 @@ start_server {tags {"hash"}} {
         set _ $rv
     } {0 0 1 0 {} 1 0 {}}
 
         set _ $rv
     } {0 0 1 0 {} 1 0 {}}
 
+    test {HDEL - more than a single value} {
+        set rv {}
+        r del myhash
+        r hmset myhash a 1 b 2 c 3
+        assert_equal 0 [r hdel myhash x y]
+        assert_equal 2 [r hdel myhash a c f]
+        r hgetall myhash
+    } {b 2}
+
     test {HEXISTS} {
         set rv {}
         set k [lindex [array names smallhash *] 0]
     test {HEXISTS} {
         set rv {}
         set k [lindex [array names smallhash *] 0]