Use zend_string for storing key hashing algorithm

This commit is contained in:
Pavlo Yatsukhnenko
2019-02-18 14:33:29 +02:00
committed by michael-grunder
parent 0c02d5762b
commit 0d2dd169fa
4 changed files with 20 additions and 18 deletions
+6 -6
View File
@@ -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);
@@ -226,13 +226,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) {
@@ -241,7 +242,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);
@@ -266,7 +266,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. */
@@ -333,13 +333,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);
+1 -1
View File
@@ -58,7 +58,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 */
+12 -10
View File
@@ -161,7 +161,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;
@@ -177,6 +177,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;
@@ -232,9 +233,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 */
@@ -337,13 +337,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);
@@ -357,7 +359,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);
@@ -405,7 +406,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;
@@ -415,7 +416,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;
@@ -424,6 +425,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) {
@@ -435,7 +437,7 @@ 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);
@@ -443,7 +445,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, zval *z_algo, HashTab
/* 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) {
@@ -537,7 +539,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);
+1 -1
View File
@@ -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);