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

Skip to content

Commit f89e941

Browse files
Change ZPOP* return type and implement blocking variants
This commit updates ZPOPMIN/ZPOPMAX to return the same format that zRange WITHSCORES and zRangeByScore WITHSCORES does. In addition the blocking variants BZPOPMIN and BZPOPMAX are implemented.
1 parent 46f0356 commit f89e941

8 files changed

Lines changed: 81 additions & 30 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ mkinstalldirs
1616
run-tests.php
1717
idea/*
1818
.cquery
19+
tags

php_redis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ PHP_METHOD(Redis, zInter);
133133
PHP_METHOD(Redis, zUnion);
134134
PHP_METHOD(Redis, zPopMax);
135135
PHP_METHOD(Redis, zPopMin);
136+
PHP_METHOD(Redis, bzPopMax);
137+
PHP_METHOD(Redis, bzPopMin);
136138
PHP_METHOD(Redis, expireAt);
137139
PHP_METHOD(Redis, pexpireAt);
138140
PHP_METHOD(Redis, bgrewriteaof);

redis.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ static zend_function_entry redis_functions[] = {
259259
PHP_ME(Redis, blPop, arginfo_blrpop, ZEND_ACC_PUBLIC)
260260
PHP_ME(Redis, brPop, arginfo_blrpop, ZEND_ACC_PUBLIC)
261261
PHP_ME(Redis, brpoplpush, arginfo_brpoplpush, ZEND_ACC_PUBLIC)
262+
PHP_ME(Redis, bzPopMax, arginfo_blrpop, ZEND_ACC_PUBLIC)
263+
PHP_ME(Redis, bzPopMin, arginfo_blrpop, ZEND_ACC_PUBLIC)
262264
PHP_ME(Redis, clearLastError, arginfo_void, ZEND_ACC_PUBLIC)
263265
PHP_ME(Redis, client, arginfo_client, ZEND_ACC_PUBLIC)
264266
PHP_ME(Redis, close, arginfo_void, ZEND_ACC_PUBLIC)
@@ -1376,14 +1378,14 @@ PHP_METHOD(Redis, rPop)
13761378
/* {{{ proto string Redis::blPop(string key1, string key2, ..., int timeout) */
13771379
PHP_METHOD(Redis, blPop)
13781380
{
1379-
REDIS_PROCESS_CMD(blpop, redis_sock_read_multibulk_reply);
1381+
REDIS_PROCESS_KW_CMD("BLPOP", redis_varkey_timeout_cmd, redis_sock_read_multibulk_reply);
13801382
}
13811383
/* }}} */
13821384

13831385
/* {{{ proto string Redis::brPop(string key1, string key2, ..., int timeout) */
13841386
PHP_METHOD(Redis, brPop)
13851387
{
1386-
REDIS_PROCESS_CMD(brpop, redis_sock_read_multibulk_reply);
1388+
REDIS_PROCESS_KW_CMD("BRPOP", redis_varkey_timeout_cmd, redis_sock_read_multibulk_reply);
13871389
}
13881390
/* }}} */
13891391

@@ -2144,9 +2146,9 @@ PHP_METHOD(Redis, zUnion) {
21442146
PHP_METHOD(Redis, zPopMax)
21452147
{
21462148
if (ZEND_NUM_ARGS() == 1) {
2147-
REDIS_PROCESS_KW_CMD("ZPOPMAX", redis_key_cmd, redis_sock_read_multibulk_reply);
2149+
REDIS_PROCESS_KW_CMD("ZPOPMAX", redis_key_cmd, redis_mbulk_reply_zipped_keys_dbl);
21482150
} else if (ZEND_NUM_ARGS() == 2) {
2149-
REDIS_PROCESS_KW_CMD("ZPOPMAX", redis_key_long_cmd, redis_sock_read_multibulk_reply);
2151+
REDIS_PROCESS_KW_CMD("ZPOPMAX", redis_key_long_cmd, redis_mbulk_reply_zipped_keys_dbl);
21502152
} else {
21512153
ZEND_WRONG_PARAM_COUNT();
21522154
}
@@ -2157,15 +2159,27 @@ PHP_METHOD(Redis, zPopMax)
21572159
PHP_METHOD(Redis, zPopMin)
21582160
{
21592161
if (ZEND_NUM_ARGS() == 1) {
2160-
REDIS_PROCESS_KW_CMD("ZPOPMIN", redis_key_cmd, redis_sock_read_multibulk_reply);
2162+
REDIS_PROCESS_KW_CMD("ZPOPMIN", redis_key_cmd, redis_mbulk_reply_zipped_keys_dbl);
21612163
} else if (ZEND_NUM_ARGS() == 2) {
2162-
REDIS_PROCESS_KW_CMD("ZPOPMIN", redis_key_long_cmd, redis_sock_read_multibulk_reply);
2164+
REDIS_PROCESS_KW_CMD("ZPOPMIN", redis_key_long_cmd, redis_mbulk_reply_zipped_keys_dbl);
21632165
} else {
21642166
ZEND_WRONG_PARAM_COUNT();
21652167
}
21662168
}
21672169
/* }}} */
21682170

2171+
/* {{{ proto Redis::bzPopMax(Array(keys) [, timeout]): Array */
2172+
PHP_METHOD(Redis, bzPopMax) {
2173+
REDIS_PROCESS_KW_CMD("BZPOPMAX", redis_varkey_timeout_cmd, redis_sock_read_multibulk_reply);
2174+
}
2175+
/* }}} */
2176+
2177+
/* {{{ proto Redis::bzPopMin(Array(keys) [, timeout]): Array */
2178+
PHP_METHOD(Redis, bzPopMin) {
2179+
REDIS_PROCESS_KW_CMD("BZPOPMIN", redis_varkey_timeout_cmd, redis_sock_read_multibulk_reply);
2180+
}
2181+
/* }}} */
2182+
21692183
/* hashes */
21702184

21712185
/* {{{ proto long Redis::hset(string key, string mem, string val) */

redis_cluster.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ zend_function_entry redis_cluster_functions[] = {
124124
PHP_ME(RedisCluster, brpop, arginfo_blrpop, ZEND_ACC_PUBLIC)
125125
PHP_ME(RedisCluster, brpoplpush, arginfo_brpoplpush, ZEND_ACC_PUBLIC)
126126
PHP_ME(RedisCluster, clearlasterror, arginfo_void, ZEND_ACC_PUBLIC)
127+
PHP_ME(RedisCluster, bzpopmax, arginfo_blrpop, ZEND_ACC_PUBLIC)
128+
PHP_ME(RedisCluster, bzpopmin, arginfo_blrpop, ZEND_ACC_PUBLIC)
127129
PHP_ME(RedisCluster, client, arginfo_key_or_address_variadic, ZEND_ACC_PUBLIC)
128130
PHP_ME(RedisCluster, close, arginfo_void, ZEND_ACC_PUBLIC)
129131
PHP_ME(RedisCluster, cluster, arginfo_key_or_address_variadic, ZEND_ACC_PUBLIC)
@@ -1257,13 +1259,13 @@ PHP_METHOD(RedisCluster, rpush) {
12571259

12581260
/* {{{ proto array RedisCluster::blpop(string key1, ... keyN, long timeout) */
12591261
PHP_METHOD(RedisCluster, blpop) {
1260-
CLUSTER_PROCESS_CMD(blpop, cluster_mbulk_resp, 0);
1262+
CLUSTER_PROCESS_KW_CMD("BLPOP", redis_varkey_timeout_cmd, cluster_mbulk_resp, 0);
12611263
}
12621264
/* }}} */
12631265

12641266
/* {{{ proto array RedisCluster::brpop(string key1, ... keyN, long timeout */
12651267
PHP_METHOD(RedisCluster, brpop) {
1266-
CLUSTER_PROCESS_CMD(brpop, cluster_mbulk_resp, 0);
1268+
CLUSTER_PROCESS_KW_CMD("BRPOP", redis_varkey_timeout_cmd, cluster_mbulk_resp, 0);
12671269
}
12681270
/* }}} */
12691271

@@ -1849,9 +1851,9 @@ PHP_METHOD(RedisCluster, zremrangebylex) {
18491851
/* {{{ proto array RedisCluster::zpopmax(string key) */
18501852
PHP_METHOD(RedisCluster, zpopmax) {
18511853
if (ZEND_NUM_ARGS() == 1) {
1852-
CLUSTER_PROCESS_KW_CMD("ZPOPMAX", redis_key_cmd, cluster_mbulk_resp, 0);
1854+
CLUSTER_PROCESS_KW_CMD("ZPOPMAX", redis_key_cmd, cluster_mbulk_zipdbl_resp, 0);
18531855
} else if (ZEND_NUM_ARGS() == 2) {
1854-
CLUSTER_PROCESS_KW_CMD("ZPOPMAX", redis_key_long_cmd, cluster_mbulk_resp, 0);
1856+
CLUSTER_PROCESS_KW_CMD("ZPOPMAX", redis_key_long_cmd, cluster_mbulk_zipdbl_resp, 0);
18551857
} else {
18561858
ZEND_WRONG_PARAM_COUNT();
18571859
}
@@ -1861,15 +1863,25 @@ PHP_METHOD(RedisCluster, zpopmax) {
18611863
/* {{{ proto array RedisCluster::zpopmin(string key) */
18621864
PHP_METHOD(RedisCluster, zpopmin) {
18631865
if (ZEND_NUM_ARGS() == 1) {
1864-
CLUSTER_PROCESS_KW_CMD("ZPOPMIN", redis_key_cmd, cluster_mbulk_resp, 0);
1866+
CLUSTER_PROCESS_KW_CMD("ZPOPMIN", redis_key_cmd, cluster_mbulk_zipdbl_resp, 0);
18651867
} else if (ZEND_NUM_ARGS() == 2) {
1866-
CLUSTER_PROCESS_KW_CMD("ZPOPMIN", redis_key_long_cmd, cluster_mbulk_resp, 0);
1868+
CLUSTER_PROCESS_KW_CMD("ZPOPMIN", redis_key_long_cmd, cluster_mbulk_zipdbl_resp, 0);
18671869
} else {
18681870
ZEND_WRONG_PARAM_COUNT();
18691871
}
18701872
}
18711873
/* }}} */
18721874

1875+
/* {{{ proto array RedisCluster::bzPopMin(Array keys [, timeout]) }}} */
1876+
PHP_METHOD(RedisCluster, bzpopmax) {
1877+
CLUSTER_PROCESS_KW_CMD("BZPOPMAX", redis_varkey_timeout_cmd, cluster_mbulk_resp, 0);
1878+
}
1879+
1880+
/* {{{ proto array RedisCluster::bzPopMax(Array keys [, timeout]) }}} */
1881+
PHP_METHOD(RedisCluster, bzpopmin) {
1882+
CLUSTER_PROCESS_KW_CMD("BZPOPMIN", redis_varkey_timeout_cmd, cluster_mbulk_resp, 0);
1883+
}
1884+
18731885
/* {{{ proto RedisCluster::sort(string key, array options) */
18741886
PHP_METHOD(RedisCluster, sort) {
18751887
redisCluster *c = GET_CONTEXT();

redis_cluster.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ PHP_METHOD(RedisCluster, smove);
226226
PHP_METHOD(RedisCluster, srandmember);
227227
PHP_METHOD(RedisCluster, zpopmin);
228228
PHP_METHOD(RedisCluster, zpopmax);
229+
PHP_METHOD(RedisCluster, bzpopmax);
230+
PHP_METHOD(RedisCluster, bzpopmin);
229231
PHP_METHOD(RedisCluster, zrange);
230232
PHP_METHOD(RedisCluster, zrevrange);
231233
PHP_METHOD(RedisCluster, zrangebyscore);

redis_commands.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,15 @@ static int gen_varkey_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
12511251
return SUCCESS;
12521252
}
12531253

1254+
/* Generic handling of every blocking pop command (BLPOP, BZPOP[MIN/MAX], etc */
1255+
int redis_varkey_timeout_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
1256+
char *kw, char **cmd, int *cmd_len, short *slot,
1257+
void **ctx)
1258+
{
1259+
return gen_varkey_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, kw,
1260+
strlen(kw), 2, 1, cmd, cmd_len, slot);
1261+
}
1262+
12541263
/*
12551264
* Commands with specific signatures or that need unique functions because they
12561265
* have specific processing (argument validation, etc) that make them unique
@@ -3052,14 +3061,6 @@ int redis_blpop_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
30523061
"BLPOP", sizeof("BLPOP")-1, 2, 1, cmd, cmd_len, slot);
30533062
}
30543063

3055-
/* BRPOP */
3056-
int redis_brpop_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
3057-
char **cmd, int *cmd_len, short *slot, void **ctx)
3058-
{
3059-
return gen_varkey_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock,
3060-
"BRPOP", sizeof("BRPOP")-1, 1, 1, cmd, cmd_len, slot);
3061-
}
3062-
30633064
/* SINTER */
30643065
int redis_sinter_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
30653066
char **cmd, int *cmd_len, short *slot, void **ctx)

redis_commands.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ int redis_key_val_arr_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
7878
int redis_key_str_arr_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
7979
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx);
8080

81+
int redis_varkey_timeout_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
82+
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx);
83+
8184
/* Construct SCAN and similar commands, as well as check iterator */
8285
int redis_scan_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
8386
REDIS_SCAN_TYPE type, char **cmd, int *cmd_len);
@@ -226,12 +229,6 @@ int redis_unlink_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
226229
int redis_watch_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
227230
char **cmd, int *cmd_len, short *slot, void **ctx);
228231

229-
int redis_blpop_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
230-
char **cmd, int *cmd_len, short *slot, void **ctx);
231-
232-
int redis_brpop_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
233-
char **cmd, int *cmd_len, short *slot, void **ctx);
234-
235232
int redis_sinter_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
236233
char **cmd, int *cmd_len, short *slot, void **ctx);
237234

tests/RedisTest.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,6 +2409,28 @@ public function testZRemRangeByLex() {
24092409
$this->assertEquals($this->redis->zRemRangeByLex('key', '[a', '[c'), 2);
24102410
}
24112411

2412+
public function testBZPop() {
2413+
if (version_compare($this->version, "5.0.0", "lt")) {
2414+
$this->MarkTestSkipped();
2415+
return;
2416+
}
2417+
2418+
$this->redis->del('{zs}1', '{zs}2');
2419+
$this->redis->zAdd('{zs}1', 0, 'a', 1, 'b', 2, 'c');
2420+
$this->redis->zAdd('{zs}2', 3, 'A', 4, 'B', 5, 'D');
2421+
2422+
$this->assertEquals(Array('{zs}1', 'a', '0'), $this->redis->bzPopMin('{zs}1', '{zs}2', 0));
2423+
$this->assertEquals(Array('{zs}1', 'c', '2'), $this->redis->bzPopMax(Array('{zs}1', '{zs}2'), 0));
2424+
$this->assertEquals(Array('{zs}2', 'A', '3'), $this->redis->bzPopMin('{zs}2', '{zs}1', 0));
2425+
2426+
/* Verify timeout is being sent */
2427+
$this->redis->del('{zs}1', '{zs}2');
2428+
$st = microtime(true) * 1000;
2429+
$this->redis->bzPopMin('{zs}1', '{zs}2', 1);
2430+
$et = microtime(true) * 1000;
2431+
$this->assertTrue($et - $st > 100);
2432+
}
2433+
24122434
public function testZPop() {
24132435
if (version_compare($this->version, "5.0.0", "lt")) {
24142436
$this->MarkTestSkipped();
@@ -2418,18 +2440,18 @@ public function testZPop() {
24182440
// zPopMax and zPopMin without a COUNT argument
24192441
$this->redis->del('key');
24202442
$this->redis->zAdd('key', 0, 'a', 1, 'b', 2, 'c', 3, 'd', 4, 'e');
2421-
$this->assertTrue(array('e', '4') === $this->redis->zPopMax('key'));
2422-
$this->assertTrue(array('a', '0') === $this->redis->zPopMin('key'));
2443+
$this->assertTrue(array('e' => 4.0) === $this->redis->zPopMax('key'));
2444+
$this->assertTrue(array('a' => 0.0) === $this->redis->zPopMin('key'));
24232445

24242446
// zPopMax with a COUNT argument
24252447
$this->redis->del('key');
24262448
$this->redis->zAdd('key', 0, 'a', 1, 'b', 2, 'c', 3, 'd', 4, 'e');
2427-
$this->assertTrue(array('e', '4', 'd', '3', 'c', '2') === $this->redis->zPopMax('key', 3));
2449+
$this->assertTrue(array('e' => 4.0, 'd' => 3.0, 'c' => 2.0) === $this->redis->zPopMax('key', 3));
24282450

24292451
// zPopMin with a COUNT argument
24302452
$this->redis->del('key');
24312453
$this->redis->zAdd('key', 0, 'a', 1, 'b', 2, 'c', 3, 'd', 4, 'e');
2432-
$this->assertTrue(array('a', '0', 'b', '1', 'c', '2') === $this->redis->zPopMin('key', 3));
2454+
$this->assertTrue(array('a' => 0.0, 'b' => 1.0, 'c' => 2.0) === $this->redis->zPopMin('key', 3));
24332455
}
24342456

24352457
public function testHashes() {

0 commit comments

Comments
 (0)