Commit 0500ef27 authored by Sam Hendley's avatar Sam Hendley
Browse files

Added "withscores" option to zrangebyscore command. Based on withscores...

Added "withscores" option to zrangebyscore command. Based on withscores support in zrange function, ugliest part was the argument parsing to handle
using it with the limit option.
parent 59146ef3
...@@ -5197,15 +5197,29 @@ static void zrangebyscoreCommand(redisClient *c) { ...@@ -5197,15 +5197,29 @@ static void zrangebyscoreCommand(redisClient *c) {
double min = strtod(c->argv[2]->ptr,NULL); double min = strtod(c->argv[2]->ptr,NULL);
double max = strtod(c->argv[3]->ptr,NULL); double max = strtod(c->argv[3]->ptr,NULL);
int offset = 0, limit = -1; int offset = 0, limit = -1;
int withscores = 0;
int badsyntax = 0;
if (c->argc == 5 || c->argc == 8) {
if (strcasecmp(c->argv[c->argc-1]->ptr,"withscores") == 0) withscores = 1;
else badsyntax = 1;
}
if (c->argc != (4 + withscores) && c->argc != (7 + withscores)) {
badsyntax = 1;
}
if (c->argc != 4 && c->argc != 7) {
if (badsyntax) {
addReplySds(c, addReplySds(c,
sdsnew("-ERR wrong number of arguments for ZRANGEBYSCORE\r\n")); sdsnew("-ERR wrong number of arguments for ZRANGEBYSCORE\r\n"));
return; return;
} else if (c->argc == 7 && strcasecmp(c->argv[4]->ptr,"limit")) { }
if (c->argc == (7 + withscores) && strcasecmp(c->argv[4]->ptr,"limit")) {
addReply(c,shared.syntaxerr); addReply(c,shared.syntaxerr);
return; return;
} else if (c->argc == 7) { } else if (c->argc == (7 + withscores)) {
offset = atoi(c->argv[5]->ptr); offset = atoi(c->argv[5]->ptr);
limit = atoi(c->argv[6]->ptr); limit = atoi(c->argv[6]->ptr);
if (offset < 0) offset = 0; if (offset < 0) offset = 0;
...@@ -5251,11 +5265,14 @@ static void zrangebyscoreCommand(redisClient *c) { ...@@ -5251,11 +5265,14 @@ static void zrangebyscoreCommand(redisClient *c) {
addReplyBulkLen(c,ele); addReplyBulkLen(c,ele);
addReply(c,ele); addReply(c,ele);
addReply(c,shared.crlf); addReply(c,shared.crlf);
if (withscores)
addReplyDouble(c,ln->score);
ln = ln->forward[0]; ln = ln->forward[0];
rangelen++; rangelen++;
if (limit > 0) limit--; if (limit > 0) limit--;
} }
lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",rangelen); lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",
withscores ? (rangelen*2) : rangelen);
} }
} }
} }
......
...@@ -1310,6 +1310,16 @@ proc main {server port} { ...@@ -1310,6 +1310,16 @@ proc main {server port} {
$r zrangebyscore zset 2 4 $r zrangebyscore zset 2 4
} {b c d} } {b c d}
test {ZRANGEBYSCORE withscores} {
$r del zset
$r zadd zset 1 a
$r zadd zset 2 b
$r zadd zset 3 c
$r zadd zset 4 d
$r zadd zset 5 e
$r zrangebyscore zset 2 4 withscores
} {b 2 c 3 d 4}
test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} { test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} {
set err {} set err {}
$r del zset $r del zset
...@@ -1363,6 +1373,16 @@ proc main {server port} { ...@@ -1363,6 +1373,16 @@ proc main {server port} {
[$r zrangebyscore zset 0 10 LIMIT 20 10] [$r zrangebyscore zset 0 10 LIMIT 20 10]
} {{a b} {c d e} {c d e} {}} } {{a b} {c d e} {c d e} {}}
test {ZRANGEBYSCORE with LIMIT and withscores} {
$r del zset
$r zadd zset 10 a
$r zadd zset 20 b
$r zadd zset 30 c
$r zadd zset 40 d
$r zadd zset 50 e
$r zrangebyscore zset 20 50 LIMIT 2 3 withscores
} {d 40 e 50}
test {ZREMRANGE basics} { test {ZREMRANGE basics} {
$r del zset $r del zset
$r zadd zset 1 a $r zadd zset 1 a
......
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