Skip to content

Commit

Permalink
Use zend_string for storing key hashing algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
yatsukhnenko committed Feb 19, 2019
1 parent ae5d7b1 commit 8cd165d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
12 changes: 6 additions & 6 deletions redis_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ redis_array_free(RedisArray *ra)
zval_dtor(&ra->z_dist);

/* Hashing algorithm */
zval_dtor(&ra->z_algo);
if (ra->algorithm) zend_string_release(ra->algorithm);

/* Delete pur commands */
zend_hash_destroy(ra->pure_cmds);
Expand Down Expand Up @@ -272,13 +272,14 @@ redis_array_get(zval *id TSRMLS_DC)
Public constructor */
PHP_METHOD(RedisArray, __construct)
{
zval *z0, z_fun, z_dist, z_algo, *zpData, *z_opts = NULL;
zval *z0, z_fun, z_dist, *zpData, *z_opts = NULL;
RedisArray *ra = NULL;
zend_bool b_index = 0, b_autorehash = 0, b_pconnect = 0, consistent = 0;
HashTable *hPrev = NULL, *hOpts = NULL;
long l_retry_interval = 0;
zend_bool b_lazy_connect = 0;
double d_connect_timeout = 0, read_timeout = 0.0;
zend_string *algorithm = NULL;
redis_array_object *obj;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|a", &z0, &z_opts) == FAILURE) {
Expand All @@ -287,7 +288,6 @@ PHP_METHOD(RedisArray, __construct)

ZVAL_NULL(&z_fun);
ZVAL_NULL(&z_dist);
ZVAL_NULL(&z_algo);
/* extract options */
if(z_opts) {
hOpts = Z_ARRVAL_P(z_opts);
Expand All @@ -312,7 +312,7 @@ PHP_METHOD(RedisArray, __construct)

/* extract function name. */
if ((zpData = zend_hash_str_find(hOpts, "algorithm", sizeof("algorithm") - 1)) != NULL && Z_TYPE_P(zpData) == IS_STRING) {
ZVAL_ZVAL(&z_algo, zpData, 1, 0);
algorithm = zval_get_string(zpData);
}

/* extract index option. */
Expand Down Expand Up @@ -379,13 +379,13 @@ PHP_METHOD(RedisArray, __construct)
break;

case IS_ARRAY:
ra = ra_make_array(Z_ARRVAL_P(z0), &z_fun, &z_dist, &z_algo, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect, d_connect_timeout, read_timeout, consistent TSRMLS_CC);
ra = ra_make_array(Z_ARRVAL_P(z0), &z_fun, &z_dist, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect, d_connect_timeout, read_timeout, consistent, algorithm TSRMLS_CC);
break;

default:
WRONG_PARAM_COUNT;
}
zval_dtor(&z_algo);
if (algorithm) zend_string_release(algorithm);
zval_dtor(&z_dist);
zval_dtor(&z_fun);

Expand Down
2 changes: 1 addition & 1 deletion redis_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ typedef struct RedisArray_ {
zend_bool pconnect; /* should we use pconnect */
zval z_fun; /* key extractor, callable */
zval z_dist; /* key distributor, callable */
zval z_algo; /* key hashing algorithm name */
zend_string *algorithm; /* key hashing algorithm name */
HashTable *pure_cmds; /* hash table */
double connect_timeout; /* socket connect timeout */
double read_timeout; /* socket read timeout */
Expand Down
22 changes: 12 additions & 10 deletions redis_array_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ ra_find_name(const char *name) {
/* laod array from INI settings */
RedisArray *ra_load_array(const char *name TSRMLS_DC) {

zval *z_data, z_fun, z_dist, z_algo;
zval *z_data, z_fun, z_dist;
zval z_params_hosts;
zval z_params_prev;
zval z_params_funs;
Expand All @@ -180,6 +180,7 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
zval z_params_consistent;
RedisArray *ra = NULL;

zend_string *algorithm= NULL;
zend_bool b_index = 0, b_autorehash = 0, b_pconnect = 0, consistent = 0;
long l_retry_interval = 0;
zend_bool b_lazy_connect = 0;
Expand Down Expand Up @@ -235,9 +236,8 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
if ((iptr = INI_STR("redis.arrays.algorithm")) != NULL) {
sapi_module.treat_data(PARSE_STRING, estrdup(iptr), &z_params_algo TSRMLS_CC);
}
ZVAL_NULL(&z_algo);
if ((z_data = zend_hash_str_find(Z_ARRVAL(z_params_algo), name, name_len)) != NULL) {
ZVAL_ZVAL(&z_algo, z_data, 1, 0);
algorithm = zval_get_string(z_data);
}

/* find index option */
Expand Down Expand Up @@ -340,13 +340,15 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {


/* create RedisArray object */
ra = ra_make_array(hHosts, &z_fun, &z_dist, &z_algo, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect, d_connect_timeout, read_timeout, consistent TSRMLS_CC);
ra = ra_make_array(hHosts, &z_fun, &z_dist, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect, d_connect_timeout, read_timeout, consistent, algorithm TSRMLS_CC);
if (ra) {
ra->auto_rehash = b_autorehash;
if(ra->prev) ra->prev->auto_rehash = b_autorehash;
}

/* cleanup */
if (algorithm) zend_string_release(algorithm);

zval_dtor(&z_params_hosts);
zval_dtor(&z_params_prev);
zval_dtor(&z_params_funs);
Expand All @@ -360,7 +362,6 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
zval_dtor(&z_params_read_timeout);
zval_dtor(&z_params_lazy_connect);
zval_dtor(&z_params_consistent);
zval_dtor(&z_algo);
zval_dtor(&z_dist);
zval_dtor(&z_fun);

Expand Down Expand Up @@ -408,7 +409,7 @@ ra_make_continuum(zend_string **hosts, int nb_hosts)
}

RedisArray *
ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, zval *z_algo, HashTable *hosts_prev, zend_bool b_index, zend_bool b_pconnect, long retry_interval, zend_bool b_lazy_connect, double connect_timeout, double read_timeout, zend_bool consistent TSRMLS_DC) {
ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev, zend_bool b_index, zend_bool b_pconnect, long retry_interval, zend_bool b_lazy_connect, double connect_timeout, double read_timeout, zend_bool consistent, zend_string *algorithm TSRMLS_DC) {

int i, count;
RedisArray *ra;
Expand All @@ -418,7 +419,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, zval *z_algo, HashTab
/* create object */
ra = emalloc(sizeof(RedisArray));
ra->hosts = ecalloc(count, sizeof(*ra->hosts));
ra->redis = ecalloc(count, sizeof(zval));
ra->redis = ecalloc(count, sizeof(*ra->redis));
ra->count = 0;
ra->z_multi_exec = NULL;
ra->index = b_index;
Expand All @@ -427,6 +428,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, zval *z_algo, HashTab
ra->connect_timeout = connect_timeout;
ra->read_timeout = read_timeout;
ra->continuum = NULL;
ra->algorithm = NULL;

if (ra_load_hosts(ra, hosts, retry_interval, b_lazy_connect TSRMLS_CC) == NULL || !ra->count) {
for (i = 0; i < ra->count; ++i) {
Expand All @@ -438,15 +440,15 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, zval *z_algo, HashTab
efree(ra);
return NULL;
}
ra->prev = hosts_prev ? ra_make_array(hosts_prev, z_fun, z_dist, z_algo, NULL, b_index, b_pconnect, retry_interval, b_lazy_connect, connect_timeout, read_timeout, consistent TSRMLS_CC) : NULL;
ra->prev = hosts_prev ? ra_make_array(hosts_prev, z_fun, z_dist, NULL, b_index, b_pconnect, retry_interval, b_lazy_connect, connect_timeout, read_timeout, consistent, algorithm TSRMLS_CC) : NULL;

/* init array data structures */
ra_init_function_table(ra);

/* Set hash function and distribtor if provided */
ZVAL_ZVAL(&ra->z_fun, z_fun, 1, 0);
ZVAL_ZVAL(&ra->z_dist, z_dist, 1, 0);
ZVAL_ZVAL(&ra->z_algo, z_algo, 1, 0);
if (algorithm) ra->algorithm = zend_string_copy(algorithm);

/* init continuum */
if (consistent) {
Expand Down Expand Up @@ -552,7 +554,7 @@ ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos TSRMLS_D
const php_hash_ops *ops;

/* hash */
if (Z_TYPE(ra->z_algo) == IS_STRING && (ops = php_hash_fetch_ops(Z_STRVAL(ra->z_algo), Z_STRLEN(ra->z_algo))) != NULL) {
if (ra->algorithm && (ops = php_hash_fetch_ops(ZSTR_VAL(ra->algorithm), ZSTR_LEN(ra->algorithm))) != NULL) {
void *ctx = emalloc(ops->context_size);
unsigned char *digest = emalloc(ops->digest_size);

Expand Down
2 changes: 1 addition & 1 deletion redis_array_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

RedisArray *ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b_lazy_connect TSRMLS_DC);
RedisArray *ra_load_array(const char *name TSRMLS_DC);
RedisArray *ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, zval *z_algo, HashTable *hosts_prev, zend_bool b_index, zend_bool b_pconnect, long retry_interval, zend_bool b_lazy_connect, double connect_timeout, double read_timeout, zend_bool consistent TSRMLS_DC);
RedisArray *ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev, zend_bool b_index, zend_bool b_pconnect, long retry_interval, zend_bool b_lazy_connect, double connect_timeout, double read_timeout, zend_bool consistent, zend_string *algorithm TSRMLS_DC);
zval *ra_find_node_by_name(RedisArray *ra, const char *host, int host_len TSRMLS_DC);
zval *ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos TSRMLS_DC);
void ra_init_function_table(RedisArray *ra);
Expand Down

0 comments on commit 8cd165d

Please sign in to comment.