8000 Merge branch 'hotfix/zcmd_inf_weights' · jrtkcoder/phpredis@2d0f29b · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d0f29b

Browse files
Merge branch 'hotfix/zcmd_inf_weights'
2 parents da0cd3f + 75ddd07 commit 2d0f29b

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

redis.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4394,9 +4394,15 @@ PHPAPI void generic_z_command(INTERNAL_FUNCTION_PARAMETERS, char *command, int c
43944394
zend_hash_get_current_data_ex(arr_weights_hash, (void**) &data, &pointer) == SUCCESS;
43954395
zend_hash_move_forward_ex(arr_weights_hash, &pointer)) {
43964396

4397-
if (Z_TYPE_PP(data) != IS_LONG && Z_TYPE_PP(data) != IS_DOUBLE) {
4398-
continue; // ignore non-numeric arguments.
4399-
}
4397+
// Ignore non numeric arguments, unless they're the special Redis numbers
4398+
// "inf" ,"-inf", and "+inf" which can be passed as weights
4399+
if (Z_TYPE_PP(data) != IS_LONG && Z_TYPE_PP(data) != IS_DOUBLE &&
4400+
strncasecmp(Z_STRVAL_PP(data), "inf", sizeof("inf")) != 0 &&
4401+
strncasecmp(Z_STRVAL_PP(data), "-inf", sizeof("-inf")) != 0 &&
4402+
strncasecmp(Z_STRVAL_PP(data), "+inf", sizeof("+inf")) != 0)
4403+
{
4404+
continue;
4405+
}
44004406

44014407
old_cmd = NULL;
44024408
if(*cmd) {
@@ -4412,12 +4418,18 @@ PHPAPI void generic_z_command(INTERNAL_FUNCTION_PARAMETERS, char *command, int c
44124418
, integer_length(Z_LVAL_PP(data)), Z_LVAL_PP(data));
44134419

44144420
} else if(Z_TYPE_PP(data) == IS_DOUBLE) {
4415-
44164421
cmd_len = redis_cmd_format(&cmd,
44174422
"%s" /* cmd */
44184423
"$%f" _NL /* data, including size */
44194424
, cmd, cmd_len
44204425
, Z_DVAL_PP(data));
4426+
} else if(Z_TYPE_PP(data) == IS_STRING) {
4427+
cmd_len = redis_cmd_format(&cmd,
4428+
"%s" /* cmd */
4429+
"$%d" _NL /* data len */
4430+
"%s" _NL /* data */
4431+
, cmd, cmd_len, Z_STRLEN_PP(data),
4432+
Z_STRVAL_PP(data), Z_STRLEN_PP(data));
44214433
}
44224434

44234435
// keep track of elements added

tests/TestRedis.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1974,11 +1974,33 @@ public function testZX() {
19741974

19751975
$this->assertTrue($this->redis->zunion('key3', array('key1', 'key2'), array(2, 3.0)) === 3);
19761976

1977-
19781977
$this->redis->delete('key1');
19791978
$this->redis->delete('key2');
19801979
$this->redis->delete('key3');
19811980

1981+
// Test 'inf', '-inf', and '+inf' weights (GitHub issue #336)
1982+
$this->redis->zadd('key1', 1, 'one', 2, 'two', 3, 'three');
1983+
$this->redis->zadd('key2', 3, 'three', 4, 'four', 5, 'five');
1984+
1985+
// Make sure phpredis handles these weights
1986+
$this->assertTrue($this->redis->zunion('key3', array('key1','key2'), array(1, 'inf')) === 5);
1987+
$this->assertTrue($this->redis->zunion('key3', array('key1','key2'), array(1, '-inf')) === 5);
1988+
$this->assertTrue($this->redis->zunion('key3', array('key1','key2'), array(1, '+inf')) === 5);
1989+
1990+
// Now, confirm that they're being sent, and that it works
1991+
$arr_weights = Array('inf','-inf','+inf');
1992+
1993+
foreach($arr_weights as $str_weight) {
1994+
$r = $this->redis->zunionstore('key3', array('key1','key2'), array(1,$str_weight));
1995+
$this->assertTrue($r===5);
1996+
$r = $this->redis->zrangebyscore('key3', '(-inf', '(inf',array('withscores'=>true));
1997+
$this->assertTrue(count($r)===2);
1998+
$this->assertTrue(isset($r['one']));
1999+
$this->assertTrue(isset($r['two']));
2000+
}
2001+
2002+
$this->redis->del('key1','key2','key3');
2003+
19822004
$this->redis->zadd('key1', 2000.1, 'one');
19832005
$this->redis->zadd('key1', 3000.1, 'two');
19842006
$this->redis->zadd('key1', 4000.1, 'three');

0 commit comments

Comments
 (0)
0