Skip to content

Commit

Permalink
Add ZPOPMAX and ZPOPMIN support
Browse files Browse the repository at this point in the history
  • Loading branch information
mbezhanov committed Feb 17, 2019
1 parent 22d81a9 commit 46f0356
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions php_redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ PHP_METHOD(Redis, zRevRank);
PHP_METHOD(Redis, zIncrBy);
PHP_METHOD(Redis, zInter);
PHP_METHOD(Redis, zUnion);
PHP_METHOD(Redis, zPopMax);
PHP_METHOD(Redis, zPopMin);
PHP_METHOD(Redis, expireAt);
PHP_METHOD(Redis, pexpireAt);
PHP_METHOD(Redis, bgrewriteaof);
Expand Down
28 changes: 28 additions & 0 deletions redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ static zend_function_entry redis_functions[] = {
PHP_ME(Redis, zScore, arginfo_key_member, ZEND_ACC_PUBLIC)
PHP_ME(Redis, zUnion, arginfo_zstore, ZEND_ACC_PUBLIC)
PHP_ME(Redis, zscan, arginfo_kscan, ZEND_ACC_PUBLIC)
PHP_ME(Redis, zPopMax, arginfo_key, ZEND_ACC_PUBLIC)
PHP_ME(Redis, zPopMin, arginfo_key, ZEND_ACC_PUBLIC)
PHP_MALIAS(Redis, del, delete, arginfo_del, ZEND_ACC_PUBLIC)
PHP_MALIAS(Redis, evaluate, eval, arginfo_eval, ZEND_ACC_PUBLIC)
PHP_MALIAS(Redis, evaluateSha, evalsha, arginfo_evalsha, ZEND_ACC_PUBLIC)
Expand Down Expand Up @@ -2138,6 +2140,32 @@ PHP_METHOD(Redis, zUnion) {
REDIS_PROCESS_KW_CMD("ZUNIONSTORE", redis_zinter_cmd, redis_long_response);
}

/* {{{ proto array Redis::zPopMax(string key) */
PHP_METHOD(Redis, zPopMax)
{
if (ZEND_NUM_ARGS() == 1) {
REDIS_PROCESS_KW_CMD("ZPOPMAX", redis_key_cmd, redis_sock_read_multibulk_reply);
} else if (ZEND_NUM_ARGS() == 2) {
REDIS_PROCESS_KW_CMD("ZPOPMAX", redis_key_long_cmd, redis_sock_read_multibulk_reply);
} else {
ZEND_WRONG_PARAM_COUNT();
}
}
/* }}} */

/* {{{ proto array Redis::zPopMin(string key) */
PHP_METHOD(Redis, zPopMin)
{
if (ZEND_NUM_ARGS() == 1) {
REDIS_PROCESS_KW_CMD("ZPOPMIN", redis_key_cmd, redis_sock_read_multibulk_reply);
} else if (ZEND_NUM_ARGS() == 2) {
REDIS_PROCESS_KW_CMD("ZPOPMIN", redis_key_long_cmd, redis_sock_read_multibulk_reply);
} else {
ZEND_WRONG_PARAM_COUNT();
}
}
/* }}} */

/* hashes */

/* {{{ proto long Redis::hset(string key, string mem, string val) */
Expand Down
26 changes: 26 additions & 0 deletions redis_cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ zend_function_entry redis_cluster_functions[] = {
PHP_ME(RedisCluster, zincrby, arginfo_zincrby, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zinterstore, arginfo_zstore, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zlexcount, arginfo_key_min_max, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zpopmax, arginfo_key, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zpopmin, arginfo_key, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zrange, arginfo_zrange, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zrangebylex, arginfo_zrangebylex, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zrangebyscore, arginfo_zrangebyscore, ZEND_ACC_PUBLIC)
Expand Down Expand Up @@ -1844,6 +1846,30 @@ PHP_METHOD(RedisCluster, zremrangebylex) {
}
/* }}} */

/* {{{ proto array RedisCluster::zpopmax(string key) */
PHP_METHOD(RedisCluster, zpopmax) {
if (ZEND_NUM_ARGS() == 1) {
CLUSTER_PROCESS_KW_CMD("ZPOPMAX", redis_key_cmd, cluster_mbulk_resp, 0);
} else if (ZEND_NUM_ARGS() == 2) {
CLUSTER_PROCESS_KW_CMD("ZPOPMAX", redis_key_long_cmd, cluster_mbulk_resp, 0);
} else {
ZEND_WRONG_PARAM_COUNT();
}
}
/* }}} */

/* {{{ proto array RedisCluster::zpopmin(string key) */
PHP_METHOD(RedisCluster, zpopmin) {
if (ZEND_NUM_ARGS() == 1) {
CLUSTER_PROCESS_KW_CMD("ZPOPMIN", redis_key_cmd, cluster_mbulk_resp, 0);
} else if (ZEND_NUM_ARGS() == 2) {
CLUSTER_PROCESS_KW_CMD("ZPOPMIN", redis_key_long_cmd, cluster_mbulk_resp, 0);
} else {
ZEND_WRONG_PARAM_COUNT();
}
}
/* }}} */

/* {{{ proto RedisCluster::sort(string key, array options) */
PHP_METHOD(RedisCluster, sort) {
redisCluster *c = GET_CONTEXT();
Expand Down
2 changes: 2 additions & 0 deletions redis_cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ PHP_METHOD(RedisCluster, restore);
PHP_METHOD(RedisCluster, setrange);
PHP_METHOD(RedisCluster, smove);
PHP_METHOD(RedisCluster, srandmember);
PHP_METHOD(RedisCluster, zpopmin);
PHP_METHOD(RedisCluster, zpopmax);
PHP_METHOD(RedisCluster, zrange);
PHP_METHOD(RedisCluster, zrevrange);
PHP_METHOD(RedisCluster, zrangebyscore);
Expand Down
23 changes: 23 additions & 0 deletions tests/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2409,6 +2409,29 @@ public function testZRemRangeByLex() {
$this->assertEquals($this->redis->zRemRangeByLex('key', '[a', '[c'), 2);
}

public function testZPop() {
if (version_compare($this->version, "5.0.0", "lt")) {
$this->MarkTestSkipped();
return;
}

// zPopMax and zPopMin without a COUNT argument
$this->redis->del('key');
$this->redis->zAdd('key', 0, 'a', 1, 'b', 2, 'c', 3, 'd', 4, 'e');
$this->assertTrue(array('e', '4') === $this->redis->zPopMax('key'));
$this->assertTrue(array('a', '0') === $this->redis->zPopMin('key'));

// zPopMax with a COUNT argument
$this->redis->del('key');
$this->redis->zAdd('key', 0, 'a', 1, 'b', 2, 'c', 3, 'd', 4, 'e');
$this->assertTrue(array('e', '4', 'd', '3', 'c', '2') === $this->redis->zPopMax('key', 3));

// zPopMin with a COUNT argument
$this->redis->del('key');
$this->redis->zAdd('key', 0, 'a', 1, 'b', 2, 'c', 3, 'd', 4, 'e');
$this->assertTrue(array('a', '0', 'b', '1', 'c', '2') === $this->redis->zPopMin('key', 3));
}

public function testHashes() {
$this->redis->del('h', 'key');
$this->assertTrue(0 === $this->redis->hLen('h'));
Expand Down

0 comments on commit 46f0356

Please sign in to comment.