Skip to content
Browse files

Fix prefix problems with hashes, add more tests.

  • Loading branch information...
1 parent 392150e commit fd03564301bb43c47c7a5661aa3a7a0b0aa3be98 @nicolasff nicolasff committed
Showing with 81 additions and 11 deletions.
  1. +45 −5 library.c
  2. +2 −1 library.h
  3. +14 −5 redis.c
  4. +20 −0 tests/TestRedis.php
View
50 library.c
@@ -79,7 +79,7 @@ PHPAPI zval *redis_sock_read_multibulk_reply_zval(INTERNAL_FUNCTION_PARAMETERS,
array_init(z_tab);
redis_sock_read_multibulk_reply_loop(INTERNAL_FUNCTION_PARAM_PASSTHRU,
- redis_sock, z_tab, numElems);
+ redis_sock, z_tab, numElems, 1);
return z_tab;
}
@@ -633,7 +633,7 @@ PHPAPI int redis_sock_read_multibulk_reply_zipped_with_flag(INTERNAL_FUNCTION_PA
array_init(z_multi_result); /* pre-allocate array for multi's results. */
redis_sock_read_multibulk_reply_loop(INTERNAL_FUNCTION_PARAM_PASSTHRU,
- redis_sock, z_multi_result, numElems);
+ redis_sock, z_multi_result, numElems, 1);
array_zip_values_and_scores(redis_sock, z_multi_result, 0 TSRMLS_CC);
@@ -919,7 +919,47 @@ PHPAPI int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSo
array_init(z_multi_result); /* pre-allocate array for multi's results. */
redis_sock_read_multibulk_reply_loop(INTERNAL_FUNCTION_PARAM_PASSTHRU,
- redis_sock, z_multi_result, numElems);
+ redis_sock, z_multi_result, numElems, 1);
+
+ IF_MULTI_OR_PIPELINE() {
+ add_next_index_zval(z_tab, z_multi_result);
+ } else {
+ *return_value = *z_multi_result;
+ efree(z_multi_result);
+ }
+ //zval_copy_ctor(return_value);
+ return 0;
+}
+
+/**
+ * Like multibulk reply, but don't touch the values, they won't be compressed. (this is used by HKEYS).
+ */
+PHPAPI int redis_sock_read_multibulk_reply_raw(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx)
+{
+ char inbuf[1024];
+
+ if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
+ return -1;
+ }
+ if(php_stream_gets(redis_sock->stream, inbuf, 1024) == NULL) {
+ redis_stream_close(redis_sock TSRMLS_CC);
+ redis_sock->stream = NULL;
+ redis_sock->status = REDIS_SOCK_STATUS_FAILED;
+ redis_sock->mode = ATOMIC;
+ zend_throw_exception(redis_exception_ce, "read error on connection", 0 TSRMLS_CC);
+ return -1;
+ }
+
+ if(inbuf[0] != '*') {
+ return -1;
+ }
+ int numElems = atoi(inbuf+1);
+ zval *z_multi_result;
+ MAKE_STD_ZVAL(z_multi_result);
+ array_init(z_multi_result); /* pre-allocate array for multi's results. */
+
+ redis_sock_read_multibulk_reply_loop(INTERNAL_FUNCTION_PARAM_PASSTHRU,
+ redis_sock, z_multi_result, numElems, 0);
IF_MULTI_OR_PIPELINE() {
add_next_index_zval(z_tab, z_multi_result);
@@ -933,7 +973,7 @@ PHPAPI int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSo
PHPAPI int
redis_sock_read_multibulk_reply_loop(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
- zval *z_tab, int numElems)
+ zval *z_tab, int numElems, int unwrap_key)
{
char *response;
int response_len;
@@ -942,7 +982,7 @@ redis_sock_read_multibulk_reply_loop(INTERNAL_FUNCTION_PARAMETERS, RedisSock *re
response = redis_sock_read(redis_sock, &response_len TSRMLS_CC);
if(response != NULL) {
zval *z = NULL;
- if(redis_unserialize(redis_sock, response, response_len, &z TSRMLS_CC) == 1) {
+ if(unwrap_key && redis_unserialize(redis_sock, response, response_len, &z TSRMLS_CC) == 1) {
efree(response);
add_next_index_zval(z_tab, z);
} else {
View
3 library.h
@@ -21,7 +21,8 @@ PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC);
PHPAPI zval *redis_sock_read_multibulk_reply_zval(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock);
PHPAPI char *redis_sock_read_bulk_reply(RedisSock *redis_sock, int bytes TSRMLS_DC);
PHPAPI int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *_z_tab, void *ctx);
-PHPAPI int redis_sock_read_multibulk_reply_loop(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, int numElems);
+PHPAPI int redis_sock_read_multibulk_reply_raw(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
+PHPAPI int redis_sock_read_multibulk_reply_loop(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, int numElems, int unwrap_key);
PHPAPI int redis_sock_read_multibulk_reply_zipped(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
PHPAPI int redis_sock_read_multibulk_reply_zipped_strings(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
PHPAPI int redis_sock_read_multibulk_reply_assoc(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
View
19 redis.c
@@ -3615,7 +3615,8 @@ PHPAPI void generic_z_command(INTERNAL_FUNCTION_PARAMETERS, char *command, int c
}
- cmd_elements = 3;
+ int free_key_output = redis_key_prefix(redis_sock, &key_output, &key_output_len);
+ cmd_elements = 3;
cmd_len = redis_cmd_format(&cmd,
"$%d" _NL /* command_len */
"%s" _NL /* command */
@@ -3629,6 +3630,7 @@ PHPAPI void generic_z_command(INTERNAL_FUNCTION_PARAMETERS, char *command, int c
, command_len, command, command_len
, key_output_len, key_output, key_output_len
, integer_length(array_keys_count), array_keys_count);
+ if(free_key_output) efree(key_output);
/* keys */
for (zend_hash_internal_pointer_reset_ex(arr_keys_hash, &pointer);
@@ -3641,6 +3643,10 @@ PHPAPI void generic_z_command(INTERNAL_FUNCTION_PARAMETERS, char *command, int c
if(*cmd) {
old_cmd = cmd;
}
+ char *data_str = Z_STRVAL_PP(data);
+ int data_len = Z_STRLEN_PP(data);
+
+ int free_data = redis_key_prefix(redis_sock, &data_str, &data_len);
cmd_len = redis_cmd_format(&cmd,
"%s" /* cmd */
"$%d" _NL
@@ -3648,6 +3654,7 @@ PHPAPI void generic_z_command(INTERNAL_FUNCTION_PARAMETERS, char *command, int c
, cmd, cmd_len
, Z_STRLEN_PP(data), Z_STRVAL_PP(data), Z_STRLEN_PP(data));
cmd_elements++;
+ if(free_data) efree(data_str);
if(old_cmd) {
efree(old_cmd);
}
@@ -3920,12 +3927,12 @@ PHP_METHOD(Redis, hKeys)
RedisSock *redis_sock = generic_hash_command_1(INTERNAL_FUNCTION_PARAM_PASSTHRU, "HKEYS", sizeof("HKEYS")-1);
IF_ATOMIC() {
- if (redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU,
+ if (redis_sock_read_multibulk_reply_raw(INTERNAL_FUNCTION_PARAM_PASSTHRU,
redis_sock, NULL, NULL) < 0) {
RETURN_FALSE;
}
}
- REDIS_PROCESS_RESPONSE(redis_sock_read_multibulk_reply);
+ REDIS_PROCESS_RESPONSE(redis_sock_read_multibulk_reply_raw);
}
@@ -4078,6 +4085,7 @@ PHP_METHOD(Redis, hMget) {
for(i = 0; i < nb_fields; ++i) {
z_keys[i] = NULL;
}
+ int key_free = redis_key_prefix(redis_sock, &key, &key_len);
cmd_len = redis_cmd_format(&cmd,
"*%d" _NL
@@ -4088,6 +4096,7 @@ PHP_METHOD(Redis, hMget) {
"%s" _NL
, nb_fields + 2
, key_len, key, key_len);
+ if(key_free) efree(key);
char *old_cmd = NULL;
@@ -4154,10 +4163,12 @@ PHP_METHOD(Redis, hMset)
RETURN_FALSE;
}
+ int key_free = redis_key_prefix(redis_sock, &key, &key_len);
cmd_len = redis_cmd_format(&cmd,
"$5" _NL "HMSET" _NL
"$%d" _NL "%s" _NL
, key_len, key, key_len);
+ if(key_free) efree(key);
/* looping on each item of the array */
for(i =0, zend_hash_internal_pointer_reset(ht_hash);
@@ -4185,7 +4196,6 @@ PHP_METHOD(Redis, hMset)
char *hval;
int hval_len;
int hval_free = redis_serialize(redis_sock, *z_value_p, &hval, &hval_len TSRMLS_CC);
- int hkey_free = redis_key_prefix(redis_sock, &hkey, &hkey_len);
old_cmd = cmd;
cmd_len = redis_cmd_format(&cmd, "%s"
@@ -4196,7 +4206,6 @@ PHP_METHOD(Redis, hMset)
, hval_len, hval, hval_len);
efree(old_cmd);
if(hval_free) efree(hval);
- if(hkey_free) efree(hkey);
}
old_cmd = cmd;
View
20 tests/TestRedis.php
@@ -1850,6 +1850,11 @@ public function testSetRange() {
public function testMultiExec() {
$this->sequence(Redis::MULTI);
+ // with prefix as well
+ $this->redis->setOption(Redis::OPT_PREFIX, "test:");
+ $this->sequence(Redis::MULTI);
+ $this->redis->setOption(Redis::OPT_PREFIX, "");
+
$this->redis->set('x', '42');
$this->assertTrue(TRUE === $this->redis->watch('x'));
@@ -1884,6 +1889,11 @@ public function testMultiExec() {
public function testPipeline() {
$this->sequence(Redis::PIPELINE);
+
+ // with prefix as well
+ $this->redis->setOption(Redis::OPT_PREFIX, "test:");
+ $this->sequence(Redis::PIPELINE);
+ $this->redis->setOption(Redis::OPT_PREFIX, "");
}
protected function sequence($mode) {
@@ -2443,11 +2453,21 @@ protected function sequence($mode) {
public function testSerializerPHP() {
$this->checkSerializer(Redis::SERIALIZER_PHP);
+
+ // with prefix
+ $this->redis->setOption(Redis::OPT_PREFIX, "test:");
+ $this->checkSerializer(Redis::SERIALIZER_PHP);
+ $this->redis->setOption(Redis::OPT_PREFIX, "");
}
public function testSerializerIGBinary() {
$this->checkSerializer(Redis::SERIALIZER_IGBINARY);
+
+ // with prefix
+ $this->redis->setOption(Redis::OPT_PREFIX, "test:");
+ $this->checkSerializer(Redis::SERIALIZER_IGBINARY);
+ $this->redis->setOption(Redis::OPT_PREFIX, "");
}
private function checkSerializer($mode) {

0 comments on commit fd03564

Please sign in to comment.
Something went wrong with that request. Please try again.