Skip to content

Commit 4647f0d

Browse files
committed
Added new range support for ZRANGEBYSCORE, ZCOUNT.
1 parent aeba446 commit 4647f0d

File tree

3 files changed

+52
-28
lines changed

3 files changed

+52
-28
lines changed

README.markdown

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,11 +1286,11 @@ $redis->zReverseRange('key', 0, -1, true); /* array('val10' => 10, 'val2' => 2,
12861286

12871287
## zRangeByScore
12881288
##### *Description*
1289-
Returns the elements of the sorted set stored at the specified key which have scores in the range [start,end].
1289+
Returns the elements of the sorted set stored at the specified key which have scores in the range [start,end]. Adding a parenthesis before `start` or `end` excludes it from the range. +inf and -inf are also valid limits.
12901290
##### *Parameters*
12911291
*key*
1292-
*start*: double
1293-
*end*: double
1292+
*start*: string
1293+
*end*: string
12941294
*options*: array
12951295

12961296
Two options are available: `withscores => TRUE`, and `limit => array($offset, $count)`
@@ -1310,11 +1310,11 @@ $redis->zRangeByScore('key', 0, 3, array('withscores' => TRUE, 'limit' => array(
13101310

13111311
## zCount
13121312
##### *Description*
1313-
Returns the *number* of elements of the sorted set stored at the specified key which have scores in the range [start,end].
1313+
Returns the *number* of elements of the sorted set stored at the specified key which have scores in the range [start,end]. Adding a parenthesis before `start` or `end` excludes it from the range. +inf and -inf are also valid limits.
13141314
##### *Parameters*
13151315
*key*
1316-
*start*: double
1317-
*end*: double
1316+
*start*: string
1317+
*end*: string
13181318

13191319
##### *Return value*
13201320
*LONG* the size of a corresponding zRangeByScore.

redis.c

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2973,7 +2973,7 @@ PHP_METHOD(Redis, zReverseRange)
29732973
}
29742974
}
29752975
/* }}} */
2976-
/* {{{ proto array Redis::zRangeByScore(string key, int start , int end [,array options = NULL])
2976+
/* {{{ proto array Redis::zRangeByScore(string key, string start , string end [,array options = NULL])
29772977
*/
29782978
PHP_METHOD(Redis, zRangeByScore)
29792979
{
@@ -2983,13 +2983,17 @@ PHP_METHOD(Redis, zRangeByScore)
29832983
char *key = NULL, *limit = NULL, *cmd;
29842984
int key_len, cmd_len, response_len;
29852985
zend_bool withscores = 0;
2986-
double start, end;
2986+
char *start, *end;
2987+
int start_len, end_len;
29872988
int has_limit = 0;
29882989
long limit_low, limit_high;
29892990

2990-
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osdd|a",
2991+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osss|a",
29912992
&object, redis_ce,
2992-
&key, &key_len, &start, &end, &z_options) == FAILURE) {
2993+
&key, &key_len,
2994+
&start, &start_len,
2995+
&end, &end_len,
2996+
&z_options) == FAILURE) {
29932997
RETURN_FALSE;
29942998
}
29952999

@@ -3031,10 +3035,10 @@ PHP_METHOD(Redis, zRangeByScore)
30313035
"%s" _NL /* key */\
30323036
\
30333037
"$%d" _NL /* start_len */\
3034-
"%F" _NL /* start */\
3038+
"%s" _NL /* start */\
30353039
\
30363040
"$%d" _NL /* end_len */\
3037-
"%F" _NL /* end */
3041+
"%s" _NL /* end */
30383042
#define BASIC_FORMAT_WITH_LIMIT BASIC_FORMAT\
30393043
"$5" _NL\
30403044
"LIMIT" _NL\
@@ -3053,8 +3057,8 @@ PHP_METHOD(Redis, zRangeByScore)
30533057
"$10" _NL
30543058
"WITHSCORES" _NL
30553059
, key_len, key, key_len
3056-
, double_length(start), start
3057-
, double_length(end), end
3060+
, start_len, start, start_len
3061+
, end_len, end, end_len
30583062
, integer_length(limit_low), limit_low
30593063
, integer_length(limit_high), limit_high);
30603064
} else {
@@ -3064,8 +3068,8 @@ PHP_METHOD(Redis, zRangeByScore)
30643068
"$10" _NL
30653069
"WITHSCORES" _NL
30663070
, key_len, key, key_len
3067-
, double_length(start), start
3068-
, double_length(end), end);
3071+
, start_len, start, start_len
3072+
, end_len, end, end_len);
30693073
}
30703074
} else {
30713075

@@ -3075,17 +3079,17 @@ PHP_METHOD(Redis, zRangeByScore)
30753079
"*7" _NL
30763080
BASIC_FORMAT_WITH_LIMIT
30773081
, key_len, key, key_len
3078-
, double_length(start), start
3079-
, double_length(end), end
3082+
, start_len, start, start_len
3083+
, end_len, end, end_len
30803084
, integer_length(limit_low), limit_low
30813085
, integer_length(limit_high), limit_high);
30823086
} else {
30833087
cmd_len = redis_cmd_format(&cmd,
30843088
"*4" _NL
30853089
BASIC_FORMAT
30863090
, key_len, key, key_len
3087-
, double_length(start), start
3088-
, double_length(end), end);
3091+
, start_len, start, start_len
3092+
, end_len, end, end_len);
30893093
}
30903094
}
30913095
#undef BASIC_FORMAT
@@ -3115,7 +3119,7 @@ PHP_METHOD(Redis, zRangeByScore)
31153119
}
31163120
/* }}} */
31173121

3118-
/* {{{ proto array Redis::zCount(string key, int start , int end)
3122+
/* {{{ proto array Redis::zCount(string key, string start , string end)
31193123
*/
31203124
PHP_METHOD(Redis, zCount)
31213125
{
@@ -3124,11 +3128,14 @@ PHP_METHOD(Redis, zCount)
31243128
RedisSock *redis_sock;
31253129
char *key = NULL, *cmd;
31263130
int key_len, cmd_len, response_len;
3127-
double start, end;
3131+
char *start, *end;
3132+
int start_len, end_len;
31283133

3129-
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osdd",
3134+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osss",
31303135
&object, redis_ce,
3131-
&key, &key_len, &start, &end) == FAILURE) {
3136+
&key, &key_len,
3137+
&start, &start_len,
3138+
&end, &end_len) == FAILURE) {
31323139
RETURN_FALSE;
31333140
}
31343141

@@ -3146,14 +3153,14 @@ PHP_METHOD(Redis, zCount)
31463153
"%s" _NL /* key */
31473154

31483155
"$%d" _NL /* start_len */
3149-
"%F" _NL /* start */
3156+
"%s" _NL /* start */
31503157

31513158
"$%d" _NL /* end_len */
3152-
"%F" _NL /* end */
3159+
"%s" _NL /* end */
31533160

31543161
, key_len, key, key_len
3155-
, double_length(start), start
3156-
, double_length(end), end);
3162+
, start_len, start, start_len
3163+
, end_len, end, end_len);
31573164

31583165
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
31593166
IF_ATOMIC() {

tests/TestRedis.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,23 @@ public function testZX() {
13851385
$this->assertFalse($this->redis->zScore('key', 'val'));
13861386
$this->assertFalse($this->redis->zScore(3, 2));
13871387

1388+
// with () and +inf, -inf
1389+
$this->redis->delete('zset');
1390+
$this->redis->zAdd('zset', 1, 'foo');
1391+
$this->redis->zAdd('zset', 2, 'bar');
1392+
$this->redis->zAdd('zset', 3, 'biz');
1393+
$this->redis->zAdd('zset', 4, 'foz');
1394+
$this->assertTrue(array('foo' => 1, 'bar' => 2, 'biz' => 3, 'foz' => 4) == $this->redis->zRangeByScore('zset', '-inf', '+inf', array('withscores' => TRUE)));
1395+
$this->assertTrue(array('foo' => 1, 'bar' => 2) == $this->redis->zRangeByScore('zset', 1, 2, array('withscores' => TRUE)));
1396+
$this->assertTrue(array('bar' => 2) == $this->redis->zRangeByScore('zset', '(1', 2, array('withscores' => TRUE)));
1397+
$this->assertTrue(array() == $this->redis->zRangeByScore('zset', '(1', '(2', array('withscores' => TRUE)));
1398+
1399+
$this->assertTrue(4 == $this->redis->zCount('zset', '-inf', '+inf'));
1400+
$this->assertTrue(2 == $this->redis->zCount('zset', 1, 2));
1401+
$this->assertTrue(1 == $this->redis->zCount('zset', '(1', 2));
1402+
$this->assertTrue(0 == $this->redis->zCount('zset', '(1', '(2'));
1403+
1404+
13881405
// zincrby
13891406
$this->redis->delete('key');
13901407
$this->assertTrue(1.0 === $this->redis->zIncrBy('key', 1, 'val1'));

0 commit comments

Comments
 (0)