summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-05-29 11:22:03 +0200
committerantirez <antirez@gmail.com>2015-05-29 11:22:03 +0200
commitc043a4e6f407d03e794a07d3b449ebe9cd74c507 (patch)
tree21efde107c69b082040fcfca9a54c2e36ee7513b
parent5d32abbb9e1b74ee08f03cae4305126063744a7a (diff)
downloadredis-c043a4e6f407d03e794a07d3b449ebe9cd74c507.tar.gz
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.
-rw-r--r--src/t_zset.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/t_zset.c b/src/t_zset.c
index 081a01099..63436f25d 100644
--- a/src/t_zset.c
+++ b/src/t_zset.c
@@ -1175,6 +1175,7 @@ void zsetConvert(robj *zobj, int encoding) {
#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_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) {
static char *nanerr = "resulting score is not a number (NaN)";
robj *key = c->argv[1];
@@ -1199,6 +1200,7 @@ void zaddGenericCommand(redisClient *c, int flags) {
char *opt = c->argv[scoreidx]->ptr;
if (!strcasecmp(opt,"nx")) flags |= ZADD_NX;
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 break;
scoreidx++;
@@ -1208,6 +1210,7 @@ void zaddGenericCommand(redisClient *c, int flags) {
int incr = (flags & ZADD_INCR) != 0;
int nx = (flags & ZADD_NX) != 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
* we expect any number of score-element pairs. */
@@ -1353,7 +1356,7 @@ reply_to_client:
else
addReply(c,shared.nullbulk);
} else { /* ZADD. */
- addReplyLongLong(c,added);
+ addReplyLongLong(c,retch ? added+updated : added);
}
cleanup: