Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added ZREVRANGEBYSCORE.

  • Loading branch information...
commit d93b1352c355961049e8f39b6ec442c5c7e044cd 1 parent 4c237c7
@nicolasff nicolasff authored
Showing with 34 additions and 10 deletions.
  1. +2 −2 README.markdown
  2. +1 −0  php_redis.h
  3. +22 −8 redis.c
  4. +9 −0 tests/TestRedis.php
View
4 README.markdown
@@ -1775,9 +1775,9 @@ $redis->zReverseRange('key', 0, -1); /* array('val10', 'val2', 'val0') */
$redis->zReverseRange('key', 0, -1, true); /* array('val10' => 10, 'val2' => 2, 'val0' => 0) */
</pre>
-## zRangeByScore
+## zRangeByScore, zRevRangeByScore
##### *Description*
-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.
+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. zRevRangeByScore returns the same items in reverse order, when the `start` and `end` parameters are swapped.
##### *Parameters*
*key*
*start*: string
View
1  php_redis.h
@@ -103,6 +103,7 @@ PHP_METHOD(Redis, zDelete);
PHP_METHOD(Redis, zRange);
PHP_METHOD(Redis, zReverseRange);
PHP_METHOD(Redis, zRangeByScore);
+PHP_METHOD(Redis, zRevRangeByScore);
PHP_METHOD(Redis, zCount);
PHP_METHOD(Redis, zDeleteRangeByScore);
PHP_METHOD(Redis, zCard);
View
30 redis.c
@@ -133,6 +133,7 @@ static zend_function_entry redis_functions[] = {
PHP_ME(Redis, zRange, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, zReverseRange, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, zRangeByScore, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(Redis, zRevRangeByScore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, zCount, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, zDeleteRangeByScore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, zCard, NULL, ZEND_ACC_PUBLIC)
@@ -3257,10 +3258,10 @@ PHP_METHOD(Redis, zReverseRange)
}
}
/* }}} */
-/* {{{ proto array Redis::zRangeByScore(string key, string start , string end [,array options = NULL])
- */
-PHP_METHOD(Redis, zRangeByScore)
-{
+
+PHPAPI void
+redis_generic_zrange_by_score(INTERNAL_FUNCTION_PARAMETERS, char *keyword TSRMLS_DC) {
+
zval *object, *z_options = NULL, **z_limit_val_pp = NULL, **z_withscores_val_pp = NULL;
RedisSock *redis_sock;
@@ -3314,18 +3315,18 @@ PHP_METHOD(Redis, zRangeByScore)
int key_free = redis_key_prefix(redis_sock, &key, &key_len);
if(withscores) {
if(has_limit) {
- cmd_len = redis_cmd_format_static(&cmd, "ZRANGEBYSCORE", "ssssdds",
+ cmd_len = redis_cmd_format_static(&cmd, keyword, "ssssdds",
key, key_len, start, start_len, end, end_len, "LIMIT", 5, limit_low, limit_high, "WITHSCORES", 10);
} else {
- cmd_len = redis_cmd_format_static(&cmd, "ZRANGEBYSCORE", "ssss",
+ cmd_len = redis_cmd_format_static(&cmd, keyword, "ssss",
key, key_len, start, start_len, end, end_len, "WITHSCORES", 10);
}
} else {
if(has_limit) {
- cmd_len = redis_cmd_format_static(&cmd, "ZRANGEBYSCORE", "ssssdd",
+ cmd_len = redis_cmd_format_static(&cmd, keyword, "ssssdd",
key, key_len, start, start_len, end, end_len, "LIMIT", 5, limit_low, limit_high);
} else {
- cmd_len = redis_cmd_format_static(&cmd, "ZRANGEBYSCORE", "sss", key, key_len, start, start_len, end, end_len);
+ cmd_len = redis_cmd_format_static(&cmd, keyword, "sss", key, key_len, start, start_len, end, end_len);
}
}
if(key_free) efree(key);
@@ -3352,7 +3353,20 @@ PHP_METHOD(Redis, zRangeByScore)
REDIS_PROCESS_RESPONSE(redis_sock_read_multibulk_reply);
}
}
+
+/* {{{ proto array Redis::zRangeByScore(string key, string start , string end [,array options = NULL])
+ */
+PHP_METHOD(Redis, zRangeByScore)
+{
+ redis_generic_zrange_by_score(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZRANGEBYSCORE" TSRMLS_CC);
+}
/* }}} */
+/* {{{ proto array Redis::zRevRangeByScore(string key, string start , string end [,array options = NULL])
+ */
+PHP_METHOD(Redis, zRevRangeByScore)
+{
+ redis_generic_zrange_by_score(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZREVRANGEBYSCORE" TSRMLS_CC);
+}
/* {{{ proto array Redis::zCount(string key, string start , string end)
*/
View
9 tests/TestRedis.php
@@ -1592,6 +1592,10 @@ public function testZX() {
$zero_to_three = $this->redis->zRangeByScore('key', 0, 3);
$this->assertTrue(array('val0', 'val1', 'val2', 'aal3', 'val3') === $zero_to_three || array('val0', 'val1', 'val2', 'val3', 'aal3') === $zero_to_three);
+
+ $three_to_zero = $this->redis->zRevRangeByScore('key', 3, 0);
+ $this->assertTrue(array_reverse(array('val0', 'val1', 'val2', 'aal3', 'val3')) === $three_to_zero || array_reverse(array('val0', 'val1', 'val2', 'val3', 'aal3')) === $three_to_zero);
+
$this->assertTrue(5 === $this->redis->zCount('key', 0, 3));
// withscores
@@ -1606,6 +1610,11 @@ public function testZX() {
$this->assertTrue(array('val1', 'val2') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(1, 2))));
$this->assertTrue(array('val0', 'val1') === $this->redis->zRangeByScore('key', 0, 1, array('limit' => array(0, 100))));
+ $this->assertTrue(array('val3') === $this->redis->zRevRangeByScore('key', 3, 0, array('limit' => array(0, 1))));
+ $this->assertTrue(array('val3', 'val2') === $this->redis->zRevRangeByScore('key', 3, 0, array('limit' => array(0, 2))));
+ $this->assertTrue(array('val2', 'val1') === $this->redis->zRevRangeByScore('key', 3, 0, array('limit' => array(1, 2))));
+ $this->assertTrue(array('val1', 'val0') === $this->redis->zRevRangeByScore('key', 1, 0, array('limit' => array(0, 100))));
+
$this->assertTrue(4 === $this->redis->zSize('key'));
$this->assertTrue(1.0 === $this->redis->zScore('key', 'val1'));
$this->assertFalse($this->redis->zScore('key', 'val'));
Please sign in to comment.
Something went wrong with that request. Please try again.