Skip to content

Commit

Permalink
Better variadic function, variadic HDEL.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasff committed Aug 1, 2011
1 parent 5c65c03 commit b783d7e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 38 deletions.
2 changes: 1 addition & 1 deletion php_redis.h
Expand Up @@ -172,7 +172,7 @@ PHP_MINFO_FUNCTION(redis);
PHPAPI int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent);
PHPAPI void redis_atomic_increment(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int count);
PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keyword_len,
int min_argc, RedisSock **redis_sock, int has_timeout);
int min_argc, RedisSock **redis_sock, int has_timeout, int all_keys);
PHPAPI void generic_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, char *sort, int use_alpha);
PHPAPI void generic_empty_cmd(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len, ...);
PHPAPI void generic_empty_long_cmd(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len, ...);
Expand Down
78 changes: 48 additions & 30 deletions redis.c
Expand Up @@ -1074,7 +1074,7 @@ PHP_METHOD(Redis, delete)

generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"DEL", sizeof("DEL") - 1,
1, &redis_sock, 0);
1, &redis_sock, 0, 1);
IF_ATOMIC() {
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
}
Expand All @@ -1091,7 +1091,7 @@ PHP_METHOD(Redis, watch)

generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"WATCH", sizeof("WATCH") - 1,
1, &redis_sock, 0);
1, &redis_sock, 0, 1);
IF_ATOMIC() {
redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
}
Expand Down Expand Up @@ -1504,7 +1504,7 @@ PHP_METHOD(Redis, blPop)

generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"BLPOP", sizeof("BLPOP") - 1,
2, &redis_sock, 1);
2, &redis_sock, 1, 1);

IF_ATOMIC() {
if (redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU,
Expand All @@ -1524,7 +1524,7 @@ PHP_METHOD(Redis, brPop)

generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"BRPOP", sizeof("BRPOP") - 1,
2, &redis_sock, 1);
2, &redis_sock, 1, 1);

IF_ATOMIC() {
if (redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU,
Expand Down Expand Up @@ -1936,7 +1936,7 @@ PHP_METHOD(Redis, sMembers)


PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keyword_len,
int min_argc, RedisSock **out_sock, int has_timeout)
int min_argc, RedisSock **out_sock, int has_timeout, int all_keys)
{
zval *object, **z_args, *z_array;
char **keys, *cmd;
Expand Down Expand Up @@ -2004,9 +2004,9 @@ PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword
if(single_array) { /* loop over the array */
HashTable *keytable = Z_ARRVAL_P(z_array);

for(i = 0, j = 0, zend_hash_internal_pointer_reset(keytable);
for(j = 0, zend_hash_internal_pointer_reset(keytable);
zend_hash_has_more_elements(keytable) == SUCCESS;
zend_hash_move_forward(keytable), i++) {
zend_hash_move_forward(keytable)) {

char *key;
unsigned int key_len;
Expand All @@ -2019,17 +2019,24 @@ PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword
continue; /* this should never happen, according to the PHP people. */
}

if(Z_TYPE_PP(z_value_pp) == IS_STRING) {

if(!all_keys && j != 0) { /* not just operating on keys */

redis_serialize(redis_sock, *z_value_pp, &keys[j], &keys_len[j] TSRMLS_CC);

} else if(Z_TYPE_PP(z_value_pp) == IS_STRING) {
/* get current value */
keys[j] = Z_STRVAL_PP(z_value_pp);
keys_len[j] = Z_STRLEN_PP(z_value_pp);

redis_key_prefix(redis_sock, &keys[j], &keys_len[j] TSRMLS_CC); /* add optional prefix */
} else {
continue;
}

cmd_len += 1 + integer_length(keys_len[j]) + 2 + keys_len[j] + 2; /* $ + size + NL + string + NL */
j++;
real_argc++;
}
cmd_len += 1 + integer_length(keys_len[j]) + 2 + keys_len[j] + 2; /* $ + size + NL + string + NL */
j++;
real_argc++;
}
if(has_timeout) {
keys_len[j] = spprintf(&keys[j], 0, "%d", timeout);
Expand All @@ -2048,16 +2055,23 @@ PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword
}

for(i = 0, j = 0; i < argc; ++i) { /* store each key */
if(Z_TYPE_P(z_args[i]) == IS_STRING) {
if(!all_keys && j != 0) { /* not just operating on keys */

redis_serialize(redis_sock, z_args[i], &keys[j], &keys_len[j] TSRMLS_CC);

} else if(Z_TYPE_P(z_args[i]) == IS_STRING) {
keys[j] = Z_STRVAL_P(z_args[i]);
keys_len[j] = Z_STRLEN_P(z_args[i]);

redis_key_prefix(redis_sock, &keys[j], &keys_len[j] TSRMLS_CC); /* add optional prefix TSRMLS_CC*/

cmd_len += 1 + integer_length(keys_len[j]) + 2 + keys_len[j] + 2; /* $ + size + NL + string + NL */
j++;
real_argc++;
}
} else {
continue;
}

cmd_len += 1 + integer_length(keys_len[j]) + 2 + keys_len[j] + 2; /* $ + size + NL + string + NL */
j++;
real_argc++;
}
if(has_timeout) {
keys_len[j] = spprintf(&keys[j], 0, "%ld", Z_LVAL_P(z_args[j]));
Expand Down Expand Up @@ -2089,7 +2103,9 @@ PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword
/* cleanup prefixed keys. */
if(redis_sock->prefix && redis_sock->prefix_len) {
for(i = 0; i < real_argc + (has_timeout?-1:0); ++i) {
efree(keys[i]);
if(all_keys || i == 0) {
efree(keys[i]);
}
}
}
if(has_timeout) { /* cleanup string created to contain timeout value */
Expand All @@ -2102,6 +2118,7 @@ PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword
if(z_args) efree(z_args);

object = getThis();
//php_printf("cmd=[%s]\n", cmd);
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);

return 0;
Expand All @@ -2115,7 +2132,7 @@ PHP_METHOD(Redis, sInter) {

generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"SINTER", sizeof("SINTER") - 1,
0, &redis_sock, 0);
0, &redis_sock, 0, 1);

IF_ATOMIC() {
if (redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU,
Expand All @@ -2135,7 +2152,7 @@ PHP_METHOD(Redis, sInterStore) {

generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"SINTERSTORE", sizeof("SINTERSTORE") - 1,
1, &redis_sock, 0);
1, &redis_sock, 0, 1);

IF_ATOMIC() {
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
Expand All @@ -2154,7 +2171,7 @@ PHP_METHOD(Redis, sUnion) {

generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"SUNION", sizeof("SUNION") - 1,
0, &redis_sock, 0);
0, &redis_sock, 0, 1);

IF_ATOMIC() {
if (redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU,
Expand All @@ -2173,7 +2190,7 @@ PHP_METHOD(Redis, sUnionStore) {

generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"SUNIONSTORE", sizeof("SUNIONSTORE") - 1,
1, &redis_sock, 0);
1, &redis_sock, 0, 1);

IF_ATOMIC() {
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
Expand All @@ -2191,7 +2208,7 @@ PHP_METHOD(Redis, sDiff) {

generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"SDIFF", sizeof("SDIFF") - 1,
0, &redis_sock, 0);
0, &redis_sock, 0, 1);

IF_ATOMIC() {
/* read multibulk reply */
Expand All @@ -2212,7 +2229,7 @@ PHP_METHOD(Redis, sDiffStore) {

generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"SDIFFSTORE", sizeof("SDIFFSTORE") - 1,
1, &redis_sock, 0);
1, &redis_sock, 0, 1);

IF_ATOMIC() {
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
Expand Down Expand Up @@ -4026,15 +4043,16 @@ generic_hash_command_2(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keyword_
/* hDel */
PHP_METHOD(Redis, hDel)
{
char *cmd;
int cmd_len;
RedisSock *redis_sock = generic_hash_command_2(INTERNAL_FUNCTION_PARAM_PASSTHRU, "HDEL", 4, &cmd, &cmd_len);
RedisSock *redis_sock;

generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"HDEL", sizeof("HDEL") - 1,
2, &redis_sock, 0, 0);

REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
IF_ATOMIC() {
redis_1_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
}
REDIS_PROCESS_RESPONSE(redis_1_response);
REDIS_PROCESS_RESPONSE(redis_long_response);

}

Expand Down
14 changes: 7 additions & 7 deletions tests/TestRedis.php
Expand Up @@ -1681,10 +1681,9 @@ public function testZX() {
$this->assertTrue(0 === $this->redis->zUnion('keyU', array('X', 'Y')));
$this->assertTrue(array() === $this->redis->zRange('keyU', 0, -1));

// !Exist U Exist → 0
// !Exist U Exist → copy of existing zset.
$this->redis->delete('keyU', 'X');
var_dump($this->redis->zUnion('keyU', array('key1', 'X')));
$this->assertTrue(0 === $this->redis->zUnion('keyU', array('key1', 'X')));
$this->assertTrue(2 === $this->redis->zUnion('keyU', array('key1', 'X')));

// test weighted zUnion
$this->redis->delete('keyZ');
Expand Down Expand Up @@ -1796,12 +1795,13 @@ public function testHashes() {
$this->assertTrue(FALSE === $this->redis->hGet('key', 'c')); // unknownkey

// hDel
$this->assertTrue(TRUE === $this->redis->hDel('h', 'a')); // TRUE on success
$this->assertTrue(FALSE === $this->redis->hDel('h', 'a')); // FALSE on failure
$this->assertTrue(1 === $this->redis->hDel('h', 'a')); // 1 on success
$this->assertTrue(0 === $this->redis->hDel('h', 'a')); // 0 on failure

$this->redis->delete('h');
$this->redis->hSet('h', 'x', 'a');
$this->redis->hSet('h', 'y', 'b');
$this->assertTrue(2 === $this->redis->hDel('h', 'x', 'y')); // variadic

// hsetnx
$this->redis->delete('h');
Expand Down Expand Up @@ -2508,8 +2508,8 @@ protected function sequence($mode) {
$this->assertTrue($ret[$i++] === array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3')); // hmget, 3 elements
$this->assertTrue($ret[$i++] === 'value1'); // hget
$this->assertTrue($ret[$i++] === 3); // hlen
$this->assertTrue($ret[$i++] === TRUE); // hdel succeeded
$this->assertTrue($ret[$i++] === FALSE); // hdel failed
$this->assertTrue($ret[$i++] === 1); // hdel succeeded
$this->assertTrue($ret[$i++] === 0); // hdel failed
$this->assertTrue($ret[$i++] === FALSE); // hexists didn't find the deleted key
$this->assertTrue($ret[$i] === array('key1', 'key3') || $ret[$i] === array('key3', 'key1')); $i++; // hkeys
$this->assertTrue($ret[$i] === array('value1', 'value3') || $ret[$i] === array('value3', 'value1')); $i++; // hvals
Expand Down

0 comments on commit b783d7e

Please sign in to comment.