Skip to content
Browse files

Add missing ZREMRANGEBYRANK + doc, tests.

  • Loading branch information...
1 parent 4266095 commit a086b0e55b6ecba72f00e41544119ae3c667cfc3 @nicolasff nicolasff committed Jan 24, 2011
Showing with 65 additions and 0 deletions.
  1. +18 −0 README.markdown
  2. +1 −0 php_redis.h
  3. +36 −0 redis.c
  4. +10 −0 tests/TestRedis.php
View
18 README.markdown
@@ -1842,6 +1842,24 @@ $redis->zAdd('key', 10, 'val10');
$redis->zRemRangeByScore('key', 0, 3); /* 2 */
</pre>
+## zRemRangeByRank, zDeleteRangeByRank
+##### *Description*
+Deletes the elements of the sorted set stored at the specified key which have rank in the range [start,end].
+##### *Parameters*
+*key*
+*start*: LONG
+*end*: LONG
+##### *Return value*
+*LONG* The number of values deleted from the sorted set
+##### *Example*
+<pre>
+$redis->zAdd('key', 1, 'one');
+$redis->zAdd('key', 2, 'two');
+$redis->zAdd('key', 3, 'three');
+$redis->zRemRangeByRank('key', 0, 1); /* 2 */
+$redis->zRange('key', 0, -1, array('withscores' => TRUE)); /* array('three' => 3) */
+</pre>
+
## zSize, zCard
##### *Description*
Returns the cardinality of an ordered set.
View
1 php_redis.h
@@ -106,6 +106,7 @@ PHP_METHOD(Redis, zRangeByScore);
PHP_METHOD(Redis, zRevRangeByScore);
PHP_METHOD(Redis, zCount);
PHP_METHOD(Redis, zDeleteRangeByScore);
+PHP_METHOD(Redis, zDeleteRangeByRank);
PHP_METHOD(Redis, zCard);
PHP_METHOD(Redis, zScore);
PHP_METHOD(Redis, zRank);
View
36 redis.c
@@ -137,6 +137,7 @@ static zend_function_entry redis_functions[] = {
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, zDeleteRangeByRank, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, zCard, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, zScore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, zRank, NULL, ZEND_ACC_PUBLIC)
@@ -189,6 +190,7 @@ static zend_function_entry redis_functions[] = {
PHP_MALIAS(Redis, zRem, zDelete, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(Redis, zRemoveRangeByScore, zDeleteRangeByScore, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(Redis, zRemRangeByScore, zDeleteRangeByScore, NULL, ZEND_ACC_PUBLIC)
+ PHP_MALIAS(Redis, zRemRangeByRank, zDeleteRangeByRank, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(Redis, zSize, zCard, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(Redis, substr, getRange, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(Redis, rename, renameKey, NULL, ZEND_ACC_PUBLIC)
@@ -3241,6 +3243,40 @@ PHP_METHOD(Redis, zDeleteRangeByScore)
}
/* }}} */
+
+/* {{{ proto long Redis::zDeleteRangeByRank(string key, long start, long end)
+ */
+PHP_METHOD(Redis, zDeleteRangeByRank)
+{
+ zval *object;
+ RedisSock *redis_sock;
+ char *key = NULL, *cmd;
+ int key_len, cmd_len;
+ long start, end;
+
+ if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osll",
+ &object, redis_ce,
+ &key, &key_len, &start, &end) == FAILURE) {
+ RETURN_FALSE;
+ }
+
+ if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
+ RETURN_FALSE;
+ }
+
+ int key_free = redis_key_prefix(redis_sock, &key, &key_len);
+ cmd_len = redis_cmd_format_static(&cmd, "ZREMRANGEBYRANK", "sdd", key, key_len, (int)start, (int)end);
+ if(key_free) efree(key);
+
+ REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
+ IF_ATOMIC() {
+ redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
+ }
+ REDIS_PROCESS_RESPONSE(redis_long_response);
+
+}
+/* }}} */
+
/* {{{ proto array Redis::zReverseRange(string key, int start , int end, bool withscores = FALSE)
*/
PHP_METHOD(Redis, zReverseRange)
View
10 tests/TestRedis.php
@@ -1698,6 +1698,16 @@ public function testZX() {
$this->redis->delete('key2');
$this->redis->delete('key3');
+
+ // ZREMRANGEBYRANK
+ $this->redis->zAdd('key1', 1, 'one');
+ $this->redis->zAdd('key1', 2, 'two');
+ $this->redis->zAdd('key1', 3, 'three');
+ $this->assertTrue(2 === $this->redis->zremrangebyrank('key1', 0, 1));
+ $this->assertTrue(array('three' => 3) == $this->redis->zRange('key1', 0, -1, TRUE));
+
+ $this->redis->delete('key1');
+
// zInter
$this->redis->zAdd('key1', 0, 'val0');

0 comments on commit a086b0e

Please sign in to comment.
Something went wrong with that request. Please try again.