diff options
author | Nicolas Favre-Felix <n.favrefelix@gmail.com> | 2010-08-31 22:30:05 +0400 |
---|---|---|
committer | Nicolas Favre-Felix <n.favrefelix@gmail.com> | 2010-08-31 22:30:05 +0400 |
commit | 4647f0d149d5a913f7b9c73c51e564ad6cca7eb5 (patch) | |
tree | 9d70e0bcd9ff0829efdfbc4b80c21c8f12250804 | |
parent | aeba44699a78b2958593c22ec5f8be94530541d9 (diff) |
Added new range support for ZRANGEBYSCORE, ZCOUNT.
-rw-r--r-- | README.markdown | 12 | ||||
-rwxr-xr-x | redis.c | 51 | ||||
-rw-r--r-- | tests/TestRedis.php | 17 |
3 files changed, 52 insertions, 28 deletions
diff --git a/README.markdown b/README.markdown index 8a99940b..979c2c21 100644 --- a/README.markdown +++ b/README.markdown @@ -1286,11 +1286,11 @@ $redis->zReverseRange('key', 0, -1, true); /* array('val10' => 10, 'val2' => 2, ## zRangeByScore ##### *Description* -Returns the elements of the sorted set stored at the specified key which have scores in the range [start,end]. +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. ##### *Parameters* *key* -*start*: double -*end*: double +*start*: string +*end*: string *options*: array 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( ## zCount ##### *Description* -Returns the *number* of elements of the sorted set stored at the specified key which have scores in the range [start,end]. +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. ##### *Parameters* *key* -*start*: double -*end*: double +*start*: string +*end*: string ##### *Return value* *LONG* the size of a corresponding zRangeByScore. @@ -2973,7 +2973,7 @@ PHP_METHOD(Redis, zReverseRange) } } /* }}} */ -/* {{{ proto array Redis::zRangeByScore(string key, int start , int end [,array options = NULL]) +/* {{{ proto array Redis::zRangeByScore(string key, string start , string end [,array options = NULL]) */ PHP_METHOD(Redis, zRangeByScore) { @@ -2983,13 +2983,17 @@ PHP_METHOD(Redis, zRangeByScore) char *key = NULL, *limit = NULL, *cmd; int key_len, cmd_len, response_len; zend_bool withscores = 0; - double start, end; + char *start, *end; + int start_len, end_len; int has_limit = 0; long limit_low, limit_high; - if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osdd|a", + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osss|a", &object, redis_ce, - &key, &key_len, &start, &end, &z_options) == FAILURE) { + &key, &key_len, + &start, &start_len, + &end, &end_len, + &z_options) == FAILURE) { RETURN_FALSE; } @@ -3031,10 +3035,10 @@ PHP_METHOD(Redis, zRangeByScore) "%s" _NL /* key */\ \ "$%d" _NL /* start_len */\ - "%F" _NL /* start */\ + "%s" _NL /* start */\ \ "$%d" _NL /* end_len */\ - "%F" _NL /* end */ + "%s" _NL /* end */ #define BASIC_FORMAT_WITH_LIMIT BASIC_FORMAT\ "$5" _NL\ "LIMIT" _NL\ @@ -3053,8 +3057,8 @@ PHP_METHOD(Redis, zRangeByScore) "$10" _NL "WITHSCORES" _NL , key_len, key, key_len - , double_length(start), start - , double_length(end), end + , start_len, start, start_len + , end_len, end, end_len , integer_length(limit_low), limit_low , integer_length(limit_high), limit_high); } else { @@ -3064,8 +3068,8 @@ PHP_METHOD(Redis, zRangeByScore) "$10" _NL "WITHSCORES" _NL , key_len, key, key_len - , double_length(start), start - , double_length(end), end); + , start_len, start, start_len + , end_len, end, end_len); } } else { @@ -3075,8 +3079,8 @@ PHP_METHOD(Redis, zRangeByScore) "*7" _NL BASIC_FORMAT_WITH_LIMIT , key_len, key, key_len - , double_length(start), start - , double_length(end), end + , start_len, start, start_len + , end_len, end, end_len , integer_length(limit_low), limit_low , integer_length(limit_high), limit_high); } else { @@ -3084,8 +3088,8 @@ PHP_METHOD(Redis, zRangeByScore) "*4" _NL BASIC_FORMAT , key_len, key, key_len - , double_length(start), start - , double_length(end), end); + , start_len, start, start_len + , end_len, end, end_len); } } #undef BASIC_FORMAT @@ -3115,7 +3119,7 @@ PHP_METHOD(Redis, zRangeByScore) } /* }}} */ -/* {{{ proto array Redis::zCount(string key, int start , int end) +/* {{{ proto array Redis::zCount(string key, string start , string end) */ PHP_METHOD(Redis, zCount) { @@ -3124,11 +3128,14 @@ PHP_METHOD(Redis, zCount) RedisSock *redis_sock; char *key = NULL, *cmd; int key_len, cmd_len, response_len; - double start, end; + char *start, *end; + int start_len, end_len; - if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osdd", + if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osss", &object, redis_ce, - &key, &key_len, &start, &end) == FAILURE) { + &key, &key_len, + &start, &start_len, + &end, &end_len) == FAILURE) { RETURN_FALSE; } @@ -3146,14 +3153,14 @@ PHP_METHOD(Redis, zCount) "%s" _NL /* key */ "$%d" _NL /* start_len */ - "%F" _NL /* start */ + "%s" _NL /* start */ "$%d" _NL /* end_len */ - "%F" _NL /* end */ + "%s" _NL /* end */ , key_len, key, key_len - , double_length(start), start - , double_length(end), end); + , start_len, start, start_len + , end_len, end, end_len); REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len); IF_ATOMIC() { diff --git a/tests/TestRedis.php b/tests/TestRedis.php index ce74d2fd..4c7a43c8 100644 --- a/tests/TestRedis.php +++ b/tests/TestRedis.php @@ -1385,6 +1385,23 @@ class Redis_Test extends PHPUnit_Framework_TestCase $this->assertFalse($this->redis->zScore('key', 'val')); $this->assertFalse($this->redis->zScore(3, 2)); + // with () and +inf, -inf + $this->redis->delete('zset'); + $this->redis->zAdd('zset', 1, 'foo'); + $this->redis->zAdd('zset', 2, 'bar'); + $this->redis->zAdd('zset', 3, 'biz'); + $this->redis->zAdd('zset', 4, 'foz'); + $this->assertTrue(array('foo' => 1, 'bar' => 2, 'biz' => 3, 'foz' => 4) == $this->redis->zRangeByScore('zset', '-inf', '+inf', array('withscores' => TRUE))); + $this->assertTrue(array('foo' => 1, 'bar' => 2) == $this->redis->zRangeByScore('zset', 1, 2, array('withscores' => TRUE))); + $this->assertTrue(array('bar' => 2) == $this->redis->zRangeByScore('zset', '(1', 2, array('withscores' => TRUE))); + $this->assertTrue(array() == $this->redis->zRangeByScore('zset', '(1', '(2', array('withscores' => TRUE))); + + $this->assertTrue(4 == $this->redis->zCount('zset', '-inf', '+inf')); + $this->assertTrue(2 == $this->redis->zCount('zset', 1, 2)); + $this->assertTrue(1 == $this->redis->zCount('zset', '(1', 2)); + $this->assertTrue(0 == $this->redis->zCount('zset', '(1', '(2')); + + // zincrby $this->redis->delete('key'); $this->assertTrue(1.0 === $this->redis->zIncrBy('key', 1, 'val1')); |