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.
This commit is contained in:
Sam Hendley 2010-02-06 18:41:25 -05:00
parent 59146ef3c1
commit 0500ef2736
2 changed files with 41 additions and 4 deletions

25
redis.c
View File

@ -5197,15 +5197,29 @@ static void zrangebyscoreCommand(redisClient *c) {
double min = strtod(c->argv[2]->ptr,NULL);
double max = strtod(c->argv[3]->ptr,NULL);
int offset = 0, limit = -1;
int withscores = 0;
int badsyntax = 0;
if (c->argc != 4 && c->argc != 7) {
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 (badsyntax) {
addReplySds(c,
sdsnew("-ERR wrong number of arguments for ZRANGEBYSCORE\r\n"));
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);
return;
} else if (c->argc == 7) {
} else if (c->argc == (7 + withscores)) {
offset = atoi(c->argv[5]->ptr);
limit = atoi(c->argv[6]->ptr);
if (offset < 0) offset = 0;
@ -5251,11 +5265,14 @@ static void zrangebyscoreCommand(redisClient *c) {
addReplyBulkLen(c,ele);
addReply(c,ele);
addReply(c,shared.crlf);
if (withscores)
addReplyDouble(c,ln->score);
ln = ln->forward[0];
rangelen++;
if (limit > 0) limit--;
}
lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",rangelen);
lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",
withscores ? (rangelen*2) : rangelen);
}
}
}

View File

@ -1310,6 +1310,16 @@ proc main {server port} {
$r zrangebyscore zset 2 4
} {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} {
set err {}
$r del zset
@ -1363,6 +1373,16 @@ proc main {server port} {
[$r zrangebyscore zset 0 10 LIMIT 20 10]
} {{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} {
$r del zset
$r zadd zset 1 a