Commit c043a4e6 authored by antirez's avatar antirez
Browse files

ZADD RETCH option: Return number of elements added or updated

Normally ZADD only returns the number of elements added to a sorted
set, using the RETCH option it returns the sum of elements added or
for which the score was updated.
parent 5d32abbb
...@@ -1175,6 +1175,7 @@ void zsetConvert(robj *zobj, int encoding) { ...@@ -1175,6 +1175,7 @@ void zsetConvert(robj *zobj, int encoding) {
#define ZADD_INCR (1<<0) /* Increment the score instead of setting it. */ #define ZADD_INCR (1<<0) /* Increment the score instead of setting it. */
#define ZADD_NX (1<<1) /* Don't touch elements not already existing. */ #define ZADD_NX (1<<1) /* Don't touch elements not already existing. */
#define ZADD_XX (1<<2) /* Only touch elements already exisitng. */ #define ZADD_XX (1<<2) /* Only touch elements already exisitng. */
#define ZADD_RETCH (1<<3) /* Return the number of elements added or updated.*/
void zaddGenericCommand(redisClient *c, int flags) { void zaddGenericCommand(redisClient *c, int flags) {
static char *nanerr = "resulting score is not a number (NaN)"; static char *nanerr = "resulting score is not a number (NaN)";
robj *key = c->argv[1]; robj *key = c->argv[1];
...@@ -1199,6 +1200,7 @@ void zaddGenericCommand(redisClient *c, int flags) { ...@@ -1199,6 +1200,7 @@ void zaddGenericCommand(redisClient *c, int flags) {
char *opt = c->argv[scoreidx]->ptr; char *opt = c->argv[scoreidx]->ptr;
if (!strcasecmp(opt,"nx")) flags |= ZADD_NX; if (!strcasecmp(opt,"nx")) flags |= ZADD_NX;
else if (!strcasecmp(opt,"xx")) flags |= ZADD_XX; else if (!strcasecmp(opt,"xx")) flags |= ZADD_XX;
else if (!strcasecmp(opt,"retch")) flags |= ZADD_RETCH;
else if (!strcasecmp(opt,"incr")) flags |= ZADD_INCR; else if (!strcasecmp(opt,"incr")) flags |= ZADD_INCR;
else break; else break;
scoreidx++; scoreidx++;
...@@ -1208,6 +1210,7 @@ void zaddGenericCommand(redisClient *c, int flags) { ...@@ -1208,6 +1210,7 @@ void zaddGenericCommand(redisClient *c, int flags) {
int incr = (flags & ZADD_INCR) != 0; int incr = (flags & ZADD_INCR) != 0;
int nx = (flags & ZADD_NX) != 0; int nx = (flags & ZADD_NX) != 0;
int xx = (flags & ZADD_XX) != 0; int xx = (flags & ZADD_XX) != 0;
int retch = (flags & ZADD_RETCH) != 0;
/* After the options, we expect to have an even number of args, since /* After the options, we expect to have an even number of args, since
* we expect any number of score-element pairs. */ * we expect any number of score-element pairs. */
...@@ -1353,7 +1356,7 @@ reply_to_client: ...@@ -1353,7 +1356,7 @@ reply_to_client:
else else
addReply(c,shared.nullbulk); addReply(c,shared.nullbulk);
} else { /* ZADD. */ } else { /* ZADD. */
addReplyLongLong(c,added); addReplyLongLong(c,retch ? added+updated : added);
} }
cleanup: cleanup:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment