Skip to content

Commit 01a4b98

Browse files
committed
HMSET and MSET implementations unified. HSET now variadic.
This is the first step towards getting rid of HMSET which is a command that does not make much sense once HSET is variadic, and has a saner return value.
1 parent 634c64d commit 01a4b98

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

src/server.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct redisCommand redisCommandTable[] = {
144144
{"substr",getrangeCommand,4,"r",0,NULL,1,1,1,0,0},
145145
{"incr",incrCommand,2,"wmF",0,NULL,1,1,1,0,0},
146146
{"decr",decrCommand,2,"wmF",0,NULL,1,1,1,0,0},
147-
{"mget",mgetCommand,-2,"r",0,NULL,1,-1,1,0,0},
147+
{"mget",mgetCommand,-2,"rF",0,NULL,1,-1,1,0,0},
148148
{"rpush",rpushCommand,-3,"wmF",0,NULL,1,1,1,0,0},
149149
{"lpush",lpushCommand,-3,"wmF",0,NULL,1,1,1,0,0},
150150
{"rpushx",rpushxCommand,-3,"wmF",0,NULL,1,1,1,0,0},
@@ -198,11 +198,11 @@ struct redisCommand redisCommandTable[] = {
198198
{"zrank",zrankCommand,3,"rF",0,NULL,1,1,1,0,0},
199199
{"zrevrank",zrevrankCommand,3,"rF",0,NULL,1,1,1,0,0},
200200
{"zscan",zscanCommand,-3,"rR",0,NULL,1,1,1,0,0},
201-
{"hset",hsetCommand,4,"wmF",0,NULL,1,1,1,0,0},
201+
{"hset",hsetCommand,-4,"wmF",0,NULL,1,1,1,0,0},
202202
{"hsetnx",hsetnxCommand,4,"wmF",0,NULL,1,1,1,0,0},
203203
{"hget",hgetCommand,3,"rF",0,NULL,1,1,1,0,0},
204-
{"hmset",hmsetCommand,-4,"wm",0,NULL,1,1,1,0,0},
205-
{"hmget",hmgetCommand,-3,"r",0,NULL,1,1,1,0,0},
204+
{"hmset",hsetCommand,-4,"wmF",0,NULL,1,1,1,0,0},
205+
{"hmget",hmgetCommand,-3,"rF",0,NULL,1,1,1,0,0},
206206
{"hincrby",hincrbyCommand,4,"wmF",0,NULL,1,1,1,0,0},
207207
{"hincrbyfloat",hincrbyfloatCommand,4,"wmF",0,NULL,1,1,1,0,0},
208208
{"hdel",hdelCommand,-3,"wF",0,NULL,1,1,1,0,0},

src/t_hash.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -511,19 +511,6 @@ void hashTypeConvert(robj *o, int enc) {
511511
* Hash type commands
512512
*----------------------------------------------------------------------------*/
513513

514-
void hsetCommand(client *c) {
515-
int update;
516-
robj *o;
517-
518-
if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
519-
hashTypeTryConversion(o,c->argv,2,3);
520-
update = hashTypeSet(o,c->argv[2]->ptr,c->argv[3]->ptr,HASH_SET_COPY);
521-
addReply(c, update ? shared.czero : shared.cone);
522-
signalModifiedKey(c->db,c->argv[1]);
523-
notifyKeyspaceEvent(NOTIFY_HASH,"hset",c->argv[1],c->db->id);
524-
server.dirty++;
525-
}
526-
527514
void hsetnxCommand(client *c) {
528515
robj *o;
529516
if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
@@ -540,8 +527,8 @@ void hsetnxCommand(client *c) {
540527
}
541528
}
542529

543-
void hmsetCommand(client *c) {
544-
int i;
530+
void hsetCommand(client *c) {
531+
int i, created = 0;
545532
robj *o;
546533

547534
if ((c->argc % 2) == 1) {
@@ -551,10 +538,19 @@ void hmsetCommand(client *c) {
551538

552539
if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
553540
hashTypeTryConversion(o,c->argv,2,c->argc-1);
554-
for (i = 2; i < c->argc; i += 2) {
555-
hashTypeSet(o,c->argv[i]->ptr,c->argv[i+1]->ptr,HASH_SET_COPY);
541+
542+
for (i = 2; i < c->argc; i += 2)
543+
created += !hashTypeSet(o,c->argv[i]->ptr,c->argv[i+1]->ptr,HASH_SET_COPY);
544+
545+
/* HMSET (deprecated) and HSET return value is different. */
546+
char *cmdname = c->argv[0]->ptr;
547+
if (cmdname[1] == 's' || cmdname[1] == 'S') {
548+
/* HSET */
549+
addReplyLongLong(c, created);
550+
} else {
551+
/* HMSET */
552+
addReply(c, shared.ok);
556553
}
557-
addReply(c, shared.ok);
558554
signalModifiedKey(c->db,c->argv[1]);
559555
notifyKeyspaceEvent(NOTIFY_HASH,"hset",c->argv[1],c->db->id);
560556
server.dirty++;

0 commit comments

Comments
 (0)