Skip to content

Commit 14ab085

Browse files
committed
Fixed HINCRBY.
1 parent 9fabfa6 commit 14ab085

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

README.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,13 +1571,13 @@ $this->hExists('h', 'NonExistingKey'); /* FALSE */
15711571
Increments the value of a member from a hash by a given amount.
15721572
##### Parameters
15731573
*key*
1574-
*value*: (double) value that will be added to the member's value
15751574
*member*
1575+
*value*: (integer) value that will be added to the member's value
15761576
##### Return value
1577-
*DOUBLE* the new value
1577+
*LONG* the new value
15781578
##### Examples
15791579
<pre>
15801580
$redis->delete('h');
1581-
$redis->hIncrBy('h', 2.5, 'x'); /* returns 2.5: h[x] = 2.5 now. */
1582-
$redis->zIncrBy('h', 1, 'x'); /* h[x] ← 2.5 + 1. Returns 3.5 */
1581+
$redis->hIncrBy('h', 'x', 2); /* returns 2: h[x] = 2 now. */
1582+
$redis->zIncrBy('h', 'x', 1); /* h[x] ← 2 + 1. Returns 3 */
15831583
</pre>

redis.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3642,7 +3642,47 @@ PHPAPI void array_zip_values_and_scores(INTERNAL_FUNCTION_PARAMETERS, int use_at
36423642

36433643
PHP_METHOD(Redis, hIncrBy)
36443644
{
3645-
generic_incrby_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, "HINCRBY", sizeof("HINCRBY")-1 TSRMLS_CC);
3645+
zval *object;
3646+
RedisSock *redis_sock;
3647+
char *key = NULL, *cmd, *member, *response;
3648+
int key_len, member_len, cmd_len, response_len;
3649+
long val;
3650+
3651+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ossl",
3652+
&object, redis_ce,
3653+
&key, &key_len, &member, &member_len, &val) == FAILURE) {
3654+
RETURN_FALSE;
3655+
}
3656+
3657+
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
3658+
RETURN_FALSE;
3659+
}
3660+
3661+
/* HINCRBY key member amount */
3662+
cmd_len = redis_cmd_format(&cmd,
3663+
"*4" _NL
3664+
"$7" _NL
3665+
"HINCRBY" _NL
3666+
3667+
"$%d" _NL /* key_len */
3668+
"%s" _NL /* key */
3669+
3670+
"$%d" _NL /* member_len */
3671+
"%s" _NL /* member */
3672+
3673+
"$%d" _NL /* val_len */
3674+
"%d" _NL /* val */
3675+
3676+
, key_len, key, key_len
3677+
, member_len, member, member_len
3678+
, integer_length(val), val);
3679+
3680+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
3681+
IF_ATOMIC() {
3682+
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL TSRMLS_CC);
3683+
}
3684+
REDIS_PROCESS_RESPONSE(redis_long_response);
3685+
36463686
}
36473687

36483688
PHPAPI int redis_response_enqueued(RedisSock *redis_sock TSRMLS_DC) {

tests/TestRedis.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,8 +1450,8 @@ public function testZX() {
14501450
$this->redis->delete('keyI');
14511451
$this->assertTrue( 2 === $this->redis->zInter('keyI', array('key1', 'key2', 'key3'), array(1, 5, 1), 'max'));
14521452
$this->assertTrue(array('val3', 'val1') === $this->redis->zRange('keyI', 0, -1));
1453-
14541453
}
1454+
14551455
public function testHashes() {
14561456
$this->redis->delete('h', 'key');
14571457

@@ -1499,14 +1499,12 @@ public function testHashes() {
14991499
$this->assertTrue(FALSE === $this->redis->hExists('h', 'x'));
15001500

15011501
// hIncrBy
1502-
/*
15031502
$this->redis->delete('h');
1504-
$this->assertTrue(2 === $this->redis->hIncrBy('h', 2, 'x'));
1505-
$this->assertTrue(3 === $this->redis->hIncrBy('h', 1, 'x'));
1503+
$this->assertTrue(2 === $this->redis->hIncrBy('h', 'x', 2));
1504+
$this->assertTrue(3 === $this->redis->hIncrBy('h', 'x', 1));
15061505

15071506
$this->redis->hSet('h', 'y', 'not-a-number');
1508-
$this->assertTrue(FALSE === $this->redis->hIncrBy('h', 1, 'y'));
1509-
*/
1507+
$this->assertTrue(1 === $this->redis->hIncrBy('h', 'y', 1));
15101508

15111509
}
15121510
}

tests/executeTests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/sh
22

3-
phpunit --repeat 1 Redis_Test ./TestRedis.php
3+
phpunit Redis_Test ./TestRedis.php

0 commit comments

Comments
 (0)