Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d2dd16

Browse files
yatsukhnenkomichael-grunder
authored andcommitted
Use zend_string for storing key hashing algorithm
1 parent 0c02d57 commit 0d2dd16

4 files changed

Lines changed: 20 additions & 18 deletions

File tree

redis_array.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ redis_array_free(RedisArray *ra)
160160
zval_dtor(&ra->z_dist);
161161

162162
/* Hashing algorithm */
163-
zval_dtor(&ra->z_algo);
163+
if (ra->algorithm) zend_string_release(ra->algorithm);
164164

165165
/* Delete pur commands */
166166
zend_hash_destroy(ra->pure_cmds);
@@ -226,13 +226,14 @@ redis_array_get(zval *id TSRMLS_DC)
226226
Public constructor */
227227
PHP_METHOD(RedisArray, __construct)
228228
{
229-
zval *z0, z_fun, z_dist, z_algo, *zpData, *z_opts = NULL;
229+
zval *z0, z_fun, z_dist, *zpData, *z_opts = NULL;
230230
RedisArray *ra = NULL;
231231
zend_bool b_index = 0, b_autorehash = 0, b_pconnect = 0, consistent = 0;
232232
HashTable *hPrev = NULL, *hOpts = NULL;
233233
long l_retry_interval = 0;
234234
zend_bool b_lazy_connect = 0;
235235
double d_connect_timeout = 0, read_timeout = 0.0;
236+
zend_string *algorithm = NULL;
236237
redis_array_object *obj;
237238

238239
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|a", &z0, &z_opts) == FAILURE) {
@@ -241,7 +242,6 @@ PHP_METHOD(RedisArray, __construct)
241242

242243
ZVAL_NULL(&z_fun);
243244
ZVAL_NULL(&z_dist);
244-
ZVAL_NULL(&z_algo);
245245
/* extract options */
246246
if(z_opts) {
247247
hOpts = Z_ARRVAL_P(z_opts);
@@ -266,7 +266,7 @@ PHP_METHOD(RedisArray, __construct)
266266

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

272272
/* extract index option. */
@@ -333,13 +333,13 @@ PHP_METHOD(RedisArray, __construct)
333333
break;
334334

335335
case IS_ARRAY:
336-
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);
336+
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);
337337
break;
338338

339339
default:
340340
WRONG_PARAM_COUNT;
341341
}
342-
zval_dtor(&z_algo);
342+
if (algorithm) zend_string_release(algorithm);
343343
zval_dtor(&z_dist);
344344
zval_dtor(&z_fun);
345345

redis_array.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef struct RedisArray_ {
5858
zend_bool pconnect; /* should we use pconnect */
5959
zval z_fun; /* key extractor, callable */
6060
zval z_dist; /* key distributor, callable */
61-
zval z_algo; /* key hashing algorithm name */
61+
zend_string *algorithm; /* key hashing algorithm name */
6262
HashTable *pure_cmds; /* hash table */
6363
double connect_timeout; /* socket connect timeout */
6464
double read_timeout; /* socket read timeout */

redis_array_impl.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ ra_find_name(const char *name) {
161161
/* laod array from INI settings */
162162
RedisArray *ra_load_array(const char *name TSRMLS_DC) {
163163

164-
zval *z_data, z_fun, z_dist, z_algo;
164+
zval *z_data, z_fun, z_dist;
165165
zval z_params_hosts;
166166
zval z_params_prev;
167167
zval z_params_funs;
@@ -177,6 +177,7 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
177177
zval z_params_consistent;
178178
RedisArray *ra = NULL;
179179

180+
zend_string *algorithm= NULL;
180181
zend_bool b_index = 0, b_autorehash = 0, b_pconnect = 0, consistent = 0;
181182
long l_retry_interval = 0;
182183
zend_bool b_lazy_connect = 0;
@@ -232,9 +233,8 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
232233
if ((iptr = INI_STR("redis.arrays.algorithm")) != NULL) {
233234
sapi_module.treat_data(PARSE_STRING, estrdup(iptr), &z_params_algo TSRMLS_CC);
234235
}
235-
ZVAL_NULL(&z_algo);
236236
if ((z_data = zend_hash_str_find(Z_ARRVAL(z_params_algo), name, name_len)) != NULL) {
237-
ZVAL_ZVAL(&z_algo, z_data, 1, 0);
237+
algorithm = zval_get_string(z_data);
238238
}
239239

240240
/* find index option */
@@ -337,13 +337,15 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
337337

338338

339339
/* create RedisArray object */
340-
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);
340+
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);
341341
if (ra) {
342342
ra->auto_rehash = b_autorehash;
343343
if(ra->prev) ra->prev->auto_rehash = b_autorehash;
344344
}
345345

346346
/* cleanup */
347+
if (algorithm) zend_string_release(algorithm);
348+
347349
zval_dtor(&z_params_hosts);
348350
zval_dtor(&z_params_prev);
349351
zval_dtor(&z_params_funs);
@@ -357,7 +359,6 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
357359
zval_dtor(&z_params_read_timeout);
358360
zval_dtor(&z_params_lazy_connect);
359361
zval_dtor(&z_params_consistent);
360-
zval_dtor(&z_algo);
361362
zval_dtor(&z_dist);
362363
zval_dtor(&z_fun);
363364

@@ -405,7 +406,7 @@ ra_make_continuum(zend_string **hosts, int nb_hosts)
405406
}
406407

407408
RedisArray *
408-
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) {
409+
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) {
409410

410411
int i, count;
411412
RedisArray *ra;
@@ -415,7 +416,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, zval *z_algo, HashTab
415416
/* create object */
416417
ra = emalloc(sizeof(RedisArray));
417418
ra->hosts = ecalloc(count, sizeof(*ra->hosts));
418-
ra->redis = ecalloc(count, sizeof(zval));
419+
ra->redis = ecalloc(count, sizeof(*ra->redis));
419420
ra->count = 0;
420421
ra->z_multi_exec = NULL;
421422
ra->index = b_index;
@@ -424,6 +425,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, zval *z_algo, HashTab
424425
ra->connect_timeout = connect_timeout;
425426
ra->read_timeout = read_timeout;
426427
ra->continuum = NULL;
428+
ra->algorithm = NULL;
427429

428430
if (ra_load_hosts(ra, hosts, retry_interval, b_lazy_connect TSRMLS_CC) == NULL || !ra->count) {
429431
for (i = 0; i < ra->count; ++i) {
@@ -435,15 +437,15 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, zval *z_algo, HashTab
435437
efree(ra);
436438
return NULL;
437439
}
438-
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;
440+
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;
439441

440442
/* init array data structures */
441443
ra_init_function_table(ra);
442444

443445
/* Set hash function and distribtor if provided */
444446
ZVAL_ZVAL(&ra->z_fun, z_fun, 1, 0);
445447
ZVAL_ZVAL(&ra->z_dist, z_dist, 1, 0);
446-
ZVAL_ZVAL(&ra->z_algo, z_algo, 1, 0);
448+
if (algorithm) ra->algorithm = zend_string_copy(algorithm);
447449

448450
/* init continuum */
449451
if (consistent) {
@@ -537,7 +539,7 @@ ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos TSRMLS_D
537539
const php_hash_ops *ops;
538540

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

redis_array_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
RedisArray *ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b_lazy_connect TSRMLS_DC);
1313
RedisArray *ra_load_array(const char *name TSRMLS_DC);
14-
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);
14+
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);
1515
zval *ra_find_node_by_name(RedisArray *ra, const char *host, int host_len TSRMLS_DC);
1616
zval *ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos TSRMLS_DC);
1717
void ra_init_function_table(RedisArray *ra);

0 commit comments

Comments
 (0)