Skip to content

Commit

Permalink
Use zend_string for storing RedisArray hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
yatsukhnenko committed Dec 19, 2018
1 parent eabc5ec commit 602740d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions redis_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ redis_array_free(RedisArray *ra)
/* Redis objects */
for(i = 0; i< ra->count; i++) {
zval_dtor(&ra->redis[i]);
efree(ra->hosts[i]);
zend_string_release(ra->hosts[i]);
}
efree(ra->redis);
efree(ra->hosts);
Expand Down Expand Up @@ -514,7 +514,7 @@ PHP_METHOD(RedisArray, _hosts)

array_init(return_value);
for(i = 0; i < ra->count; ++i) {
add_next_index_string(return_value, ra->hosts[i]);
add_next_index_stringl(return_value, ZSTR_VAL(ra->hosts[i]), ZSTR_LEN(ra->hosts[i]));
}
}

Expand All @@ -538,7 +538,7 @@ PHP_METHOD(RedisArray, _target)

redis_inst = ra_find_node(ra, key, key_len, &i TSRMLS_CC);
if(redis_inst) {
RETURN_STRING(ra->hosts[i]);
RETURN_STRINGL(ZSTR_VAL(ra->hosts[i]), ZSTR_LEN(ra->hosts[i]));
} else {
RETURN_NULL();
}
Expand Down Expand Up @@ -646,7 +646,7 @@ multihost_distribute_call(RedisArray *ra, zval *return_value, zval *z_fun, int a
call_user_function(&redis_array_ce->function_table, &ra->redis[i], z_fun, z_tmp, argc, argv);

/* Add the result for this host */
add_assoc_zval(return_value, ra->hosts[i], z_tmp);
add_assoc_zval_ex(return_value, ZSTR_VAL(ra->hosts[i]), ZSTR_LEN(ra->hosts[i]), z_tmp);
}
}

Expand Down
2 changes: 1 addition & 1 deletion redis_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ PHP_METHOD(RedisArray, unwatch);
typedef struct RedisArray_ {

int count;
char **hosts; /* array of host:port strings */
zend_string **hosts; /* array of host:port strings */
zval *redis; /* array of Redis instances */
zval *z_multi_exec; /* Redis instance to be used in multi-exec */
zend_bool index; /* use per-node index */
Expand Down
18 changes: 9 additions & 9 deletions redis_array_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b
/* default values */
host = Z_STRVAL_P(zpData);
host_len = Z_STRLEN_P(zpData);
ra->hosts[i] = estrndup(host, host_len);
ra->hosts[i] = zend_string_init(host, host_len, 0);
port = 6379;

if((p = strrchr(host, ':'))) { /* found port */
Expand Down Expand Up @@ -348,7 +348,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev

/* create object */
ra = emalloc(sizeof(RedisArray));
ra->hosts = ecalloc(count, sizeof(char *));
ra->hosts = ecalloc(count, sizeof(*ra->hosts));
ra->redis = ecalloc(count, sizeof(zval));
ra->count = 0;
ra->z_multi_exec = NULL;
Expand All @@ -361,7 +361,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev
if (ra_load_hosts(ra, hosts, retry_interval, b_lazy_connect TSRMLS_CC) == NULL || !ra->count) {
for (i = 0; i < ra->count; ++i) {
zval_dtor(&ra->redis[i]);
efree(ra->hosts[i]);
zend_string_release(ra->hosts[i]);
}
efree(ra->redis);
efree(ra->hosts);
Expand Down Expand Up @@ -500,7 +500,7 @@ ra_find_node_by_name(RedisArray *ra, const char *host, int host_len TSRMLS_DC) {

int i;
for(i = 0; i < ra->count; ++i) {
if(strncmp(ra->hosts[i], host, host_len) == 0) {
if (ZSTR_LEN(ra->hosts[i]) == host_len && strcmp(ZSTR_VAL(ra->hosts[i]), host) == 0) {
return &ra->redis[i];
}
}
Expand Down Expand Up @@ -1079,7 +1079,7 @@ ra_move_key(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC) {
/* callback with the current progress, with hostname and count */
static void
zval_rehash_callback(zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache,
const char *hostname, long count TSRMLS_DC) {
zend_string *hostname, long count TSRMLS_DC) {

zval zv, *z_ret = &zv;

Expand All @@ -1088,7 +1088,7 @@ zval_rehash_callback(zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache,
zval *z_host, *z_count, **z_args_pp[2];

MAKE_STD_ZVAL(z_host);
ZVAL_STRING(z_host, hostname);
ZVAL_STRINGL(z_host, ZSTR_VAL(hostname), ZSTR_LEN(hostname));
z_args_pp[0] = &z_host;

MAKE_STD_ZVAL(z_count);
Expand All @@ -1100,7 +1100,7 @@ zval_rehash_callback(zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache,
#else
zval z_args[2];

ZVAL_STRING(&z_args[0], hostname);
ZVAL_STRINGL(&z_args[0], ZSTR_VAL(hostname), ZSTR_LEN(hostname));
ZVAL_LONG(&z_args[1], count);

z_cb->params = z_args;
Expand All @@ -1123,7 +1123,7 @@ zval_rehash_callback(zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache,
}

static void
ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool b_index,
ra_rehash_server(RedisArray *ra, zval *z_redis, zend_string *hostname, zend_bool b_index,
zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache TSRMLS_DC) {

HashTable *h_keys;
Expand Down Expand Up @@ -1164,7 +1164,7 @@ ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool
/* check that we're not moving to the same node. */
zval *z_target = ra_find_node(ra, Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele), &pos TSRMLS_CC);

if (z_target && strcmp(hostname, ra->hosts[pos])) { /* different host */
if (z_target && zend_string_equals(hostname, ra->hosts[pos])) { /* different host */
ra_move_key(Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele), z_redis, z_target TSRMLS_CC);
}

Expand Down

0 comments on commit 602740d

Please sign in to comment.