Skip to content

Commit

Permalink
Added hmsetnx command
Browse files Browse the repository at this point in the history
  • Loading branch information
sammyyu committed Jun 15, 2012
1 parent 05ad034 commit dc506bd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ struct redisCommand readonlyCommandTable[] = {
{"hsetnx",hsetnxCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1},
{"hget",hgetCommand,3,0,NULL,1,1,1},
{"hmset",hmsetCommand,-4,REDIS_CMD_DENYOOM,NULL,1,1,1},
{"hmsetnx",hmsetnxCommand,-4,REDIS_CMD_DENYOOM,NULL,1,1,1},
{"hmget",hmgetCommand,-3,0,NULL,1,1,1},
{"hincrby",hincrbyCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1},
{"hdel",hdelCommand,-3,0,NULL,1,1,1},
Expand Down
1 change: 1 addition & 0 deletions src/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ void hsetCommand(redisClient *c);
void hsetnxCommand(redisClient *c);
void hgetCommand(redisClient *c);
void hmsetCommand(redisClient *c);
void hmsetnxCommand(redisClient *c);
void hmgetCommand(redisClient *c);
void hdelCommand(redisClient *c);
void hlenCommand(redisClient *c);
Expand Down
33 changes: 33 additions & 0 deletions src/t_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,39 @@ void hmsetCommand(redisClient *c) {
server.dirty++;
}

void hmsetnxCommand(redisClient *c) {
int i, busykeys = 0;
robj *o;

if ((c->argc % 2) == 1) {
addReplyError(c,"wrong number of arguments for HMSETNX");
return;
}

if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
hashTypeTryConversion(o,c->argv,2,c->argc-1);

/* Handle the NX flag. The HMSETNX semantic is to return zero and don't
* set nothing at all if at least one already key exists. */
for (i = 2; i < c->argc; i += 2) {
if (hashTypeExists(o, c->argv[i])) {
busykeys++;
}
if (busykeys) {
addReply(c, shared.czero);
return;
}
}

for (i = 2; i < c->argc; i += 2) {
hashTypeTryObjectEncoding(o,&c->argv[i], &c->argv[i+1]);
hashTypeSet(o,c->argv[i],c->argv[i+1]);
}
addReply(c, shared.cone);
signalModifiedKey(c->db,c->argv[1]);
server.dirty++;
}

void hincrbyCommand(redisClient *c) {
long long value, incr, oldvalue;
robj *o, *current, *new;
Expand Down

0 comments on commit dc506bd

Please sign in to comment.