diff options
author | michael-grunder <michael.grunder@gmail.com> | 2013-05-09 22:57:42 +0400 |
---|---|---|
committer | michael-grunder <michael.grunder@gmail.com> | 2013-05-09 22:57:42 +0400 |
commit | 2d0f29bdaf29b071aea29b8fb9ee4158c2b69d72 (patch) | |
tree | bf80b59453a292abf1a7865e2e2e0378f2e347c3 | |
parent | da0cd3f65522c5757adc3430a044b36749ebb6a4 (diff) | |
parent | 75ddd072a4f3180329c87669ca643d7b1bb142c7 (diff) |
Merge branch 'hotfix/zcmd_inf_weights'zcmd_inf_weights
-rw-r--r-- | redis.c | 20 | ||||
-rw-r--r-- | tests/TestRedis.php | 24 |
2 files changed, 39 insertions, 5 deletions
@@ -4394,9 +4394,15 @@ PHPAPI void generic_z_command(INTERNAL_FUNCTION_PARAMETERS, char *command, int c zend_hash_get_current_data_ex(arr_weights_hash, (void**) &data, &pointer) == SUCCESS; zend_hash_move_forward_ex(arr_weights_hash, &pointer)) { - if (Z_TYPE_PP(data) != IS_LONG && Z_TYPE_PP(data) != IS_DOUBLE) { - continue; // ignore non-numeric arguments. - } + // Ignore non numeric arguments, unless they're the special Redis numbers + // "inf" ,"-inf", and "+inf" which can be passed as weights + if (Z_TYPE_PP(data) != IS_LONG && Z_TYPE_PP(data) != IS_DOUBLE && + strncasecmp(Z_STRVAL_PP(data), "inf", sizeof("inf")) != 0 && + strncasecmp(Z_STRVAL_PP(data), "-inf", sizeof("-inf")) != 0 && + strncasecmp(Z_STRVAL_PP(data), "+inf", sizeof("+inf")) != 0) + { + continue; + } old_cmd = NULL; if(*cmd) { @@ -4412,12 +4418,18 @@ PHPAPI void generic_z_command(INTERNAL_FUNCTION_PARAMETERS, char *command, int c , integer_length(Z_LVAL_PP(data)), Z_LVAL_PP(data)); } else if(Z_TYPE_PP(data) == IS_DOUBLE) { - cmd_len = redis_cmd_format(&cmd, "%s" /* cmd */ "$%f" _NL /* data, including size */ , cmd, cmd_len , Z_DVAL_PP(data)); + } else if(Z_TYPE_PP(data) == IS_STRING) { + cmd_len = redis_cmd_format(&cmd, + "%s" /* cmd */ + "$%d" _NL /* data len */ + "%s" _NL /* data */ + , cmd, cmd_len, Z_STRLEN_PP(data), + Z_STRVAL_PP(data), Z_STRLEN_PP(data)); } // keep track of elements added diff --git a/tests/TestRedis.php b/tests/TestRedis.php index 20fdb358..70d38355 100644 --- a/tests/TestRedis.php +++ b/tests/TestRedis.php @@ -1974,11 +1974,33 @@ class Redis_Test extends TestSuite $this->assertTrue($this->redis->zunion('key3', array('key1', 'key2'), array(2, 3.0)) === 3); - $this->redis->delete('key1'); $this->redis->delete('key2'); $this->redis->delete('key3'); + // Test 'inf', '-inf', and '+inf' weights (GitHub issue #336) + $this->redis->zadd('key1', 1, 'one', 2, 'two', 3, 'three'); + $this->redis->zadd('key2', 3, 'three', 4, 'four', 5, 'five'); + + // Make sure phpredis handles these weights + $this->assertTrue($this->redis->zunion('key3', array('key1','key2'), array(1, 'inf')) === 5); + $this->assertTrue($this->redis->zunion('key3', array('key1','key2'), array(1, '-inf')) === 5); + $this->assertTrue($this->redis->zunion('key3', array('key1','key2'), array(1, '+inf')) === 5); + + // Now, confirm that they're being sent, and that it works + $arr_weights = Array('inf','-inf','+inf'); + + foreach($arr_weights as $str_weight) { + $r = $this->redis->zunionstore('key3', array('key1','key2'), array(1,$str_weight)); + $this->assertTrue($r===5); + $r = $this->redis->zrangebyscore('key3', '(-inf', '(inf',array('withscores'=>true)); + $this->assertTrue(count($r)===2); + $this->assertTrue(isset($r['one'])); + $this->assertTrue(isset($r['two'])); + } + + $this->redis->del('key1','key2','key3'); + $this->redis->zadd('key1', 2000.1, 'one'); $this->redis->zadd('key1', 3000.1, 'two'); $this->redis->zadd('key1', 4000.1, 'three'); |