Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6d6b133

Browse files
committed
Added ZRANK, ZREVRANK.
1 parent c27cc5b commit 6d6b133

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

README.markdown

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,25 @@ $redis->zAdd('key', 2.5, 'val2');
13331333
$redis->zScore('key', 'val2'); /* 2.5 */
13341334
</pre>
13351335

1336+
## zRank, zRevRank
1337+
##### *Description*
1338+
Returns the rank of a given member in the specified sorted set, starting at 0 for the item with the smallest score. zRevRank starts at 0 for the item with the *largest* score.
1339+
##### *Parameters*
1340+
*key*
1341+
*member*
1342+
##### *Return value*
1343+
*Long*, the item's score.
1344+
##### *Example*
1345+
<pre>
1346+
$redis->delete('z');
1347+
$redis->zAdd('key', 1, 'one');
1348+
$redis->zAdd('key', 2, 'two');
1349+
$redis->zRank('key', 'one'); /* 0 */
1350+
$redis->zRank('key', 'two'); /* 1 */
1351+
$redis->zRevRank('key', 'one'); /* 1 */
1352+
$redis->zRevRank('key', 'two'); /* 0 */
1353+
</pre>
1354+
13361355
## zIncrBy
13371356
##### Description
13381357
Increments the score of a member from a sorted set by a given amount.

php_redis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ PHP_METHOD(Redis, zRangeByScore);
8888
PHP_METHOD(Redis, zDeleteRangeByScore);
8989
PHP_METHOD(Redis, zCard);
9090
PHP_METHOD(Redis, zScore);
91+
PHP_METHOD(Redis, zRank);
92+
PHP_METHOD(Redis, zRevRank);
9193
PHP_METHOD(Redis, zIncrBy);
9294
PHP_METHOD(Redis, zInter);
9395
PHP_METHOD(Redis, zUnion);

redis.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ static zend_function_entry redis_functions[] = {
116116
PHP_ME(Redis, zDeleteRangeByScore, NULL, ZEND_ACC_PUBLIC)
117117
PHP_ME(Redis, zCard, NULL, ZEND_ACC_PUBLIC)
118118
PHP_ME(Redis, zScore, NULL, ZEND_ACC_PUBLIC)
119+
PHP_ME(Redis, zRank, NULL, ZEND_ACC_PUBLIC)
120+
PHP_ME(Redis, zRevRank, NULL, ZEND_ACC_PUBLIC)
119121
PHP_ME(Redis, zInter, NULL, ZEND_ACC_PUBLIC)
120122
PHP_ME(Redis, zUnion, NULL, ZEND_ACC_PUBLIC)
121123
PHP_ME(Redis, zIncrBy, NULL, ZEND_ACC_PUBLIC)
@@ -3114,6 +3116,7 @@ PHP_METHOD(Redis, zCard)
31143116

31153117
}
31163118
/* }}} */
3119+
31173120
/* {{{ proto double Redis::zScore(string key, string member)
31183121
*/
31193122
PHP_METHOD(Redis, zScore)
@@ -3150,6 +3153,60 @@ PHP_METHOD(Redis, zScore)
31503153
}
31513154
REDIS_PROCESS_RESPONSE(redis_bulk_double_response);
31523155
}
3156+
/* }}} */
3157+
3158+
3159+
PHPAPI void generic_rank_method(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keyword_len TSRMLS_DC) {
3160+
zval *object;
3161+
RedisSock *redis_sock;
3162+
char *key = NULL, *member = NULL, *cmd;
3163+
int key_len, member_len, cmd_len;
3164+
3165+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss",
3166+
&object, redis_ce, &key, &key_len,
3167+
&member, &member_len) == FAILURE) {
3168+
RETURN_FALSE;
3169+
}
3170+
3171+
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
3172+
RETURN_FALSE;
3173+
}
3174+
3175+
cmd_len = redis_cmd_format(&cmd,
3176+
"*3" _NL
3177+
"$%d" _NL
3178+
"%s" _NL
3179+
"$%d" _NL /* key_len */
3180+
"%s" _NL /* key */
3181+
"$%d" _NL /* member_len */
3182+
"%s" _NL /* member */
3183+
, keyword_len, keyword, keyword_len
3184+
, key_len, key, key_len
3185+
, member_len, member, member_len);
3186+
3187+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
3188+
IF_ATOMIC() {
3189+
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL TSRMLS_CC);
3190+
}
3191+
REDIS_PROCESS_RESPONSE(redis_long_response);
3192+
}
3193+
3194+
3195+
/* {{{ proto long Redis::zRank(string key, string member)
3196+
*/
3197+
PHP_METHOD(Redis, zRank) {
3198+
3199+
generic_rank_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZRANK", 5 TSRMLS_CC);
3200+
}
3201+
/* }}} */
3202+
3203+
/* {{{ proto long Redis::zRevRank(string key, string member)
3204+
*/
3205+
PHP_METHOD(Redis, zRevRank) {
3206+
3207+
generic_rank_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZREVRANK", 8 TSRMLS_CC);
3208+
}
3209+
/* }}} */
31533210

31543211
PHPAPI void generic_incrby_method(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keyword_len TSRMLS_DC) {
31553212
zval *object;

tests/TestRedis.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,20 @@ public function testZX() {
14811481
$this->redis->delete('keyI');
14821482
$this->assertTrue( 2 === $this->redis->zInter('keyI', array('key1', 'key2', 'key3'), array(1, 5, 1), 'max'));
14831483
$this->assertTrue(array('val3', 'val1') === $this->redis->zRange('keyI', 0, -1));
1484+
1485+
// zrank, zrevrank
1486+
$this->redis->delete('z');
1487+
$this->redis->zadd('z', 1, 'one');
1488+
$this->redis->zadd('z', 2, 'two');
1489+
$this->redis->zadd('z', 5, 'five');
1490+
1491+
$this->assertTrue(0 === $this->redis->zRank('z', 'one'));
1492+
$this->assertTrue(1 === $this->redis->zRank('z', 'two'));
1493+
$this->assertTrue(2 === $this->redis->zRank('z', 'five'));
1494+
1495+
$this->assertTrue(2 === $this->redis->zRevRank('z', 'one'));
1496+
$this->assertTrue(1 === $this->redis->zRevRank('z', 'two'));
1497+
$this->assertTrue(0 === $this->redis->zRevRank('z', 'five'));
14841498
}
14851499

14861500
public function testHashes() {

0 commit comments

Comments
 (0)