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

Skip to content

Commit 64e6a57

Browse files
yatsukhnenkomichael-grunder
authored andcommitted
Use zend_string for storing key hashing algorithm
1 parent 5de7a26 commit 64e6a57

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);
@@ -272,13 +272,14 @@ redis_array_get(zval *id TSRMLS_DC)
272272
Public constructor */
273273
PHP_METHOD(RedisArray, __construct)
274274
{
275-
zval *z0, z_fun, z_dist, z_algo, *zpData, *z_opts = NULL;
275+
zval *z0, z_fun, z_dist, *zpData, *z_opts = NULL;
276276
RedisArray *ra = NULL;
277277
zend_bool b_index = 0, b_autorehash = 0, b_pconnect = 0, consistent = 0;
278278
HashTable *hPrev = NULL, *hOpts = NULL;
279279
long l_retry_interval = 0;
280280
zend_bool b_lazy_connect = 0;
281281
double d_connect_timeout = 0, read_timeout = 0.0;
282+
zend_string *algorithm = NULL;
282283
redis_array_object *obj;
283284

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

288289
ZVAL_NULL(&z_fun);
289290
ZVAL_NULL(&z_dist);
290-
ZVAL_NULL(&z_algo);
291291
/* extract options */
292292
if(z_opts) {
293293
hOpts = Z_ARRVAL_P(z_opts);
@@ -312,7 +312,7 @@ PHP_METHOD(RedisArray, __construct)
312312

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

318318
/* extract index option. */
@@ -379,13 +379,13 @@ PHP_METHOD(RedisArray, __construct)
379379
break;
380380

381381
case IS_ARRAY:
382-
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);
382+
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);
383383
break;
384384

385385
default:
386386
WRONG_PARAM_COUNT;
387387
}
388-
zval_dtor(&z_algo);
388+
if (algorithm) zend_string_release(algorithm);
389389
zval_dtor(&z_dist);
390390
zval_dtor(&z_fun);
391391

redis_array.h

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

redis_array_impl.c

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

167-
zval *z_data, z_fun, z_dist, z_algo;
167+
zval *z_data, z_fun, z_dist;
168168
zval z_params_hosts;
169169
zval z_params_prev;
170170
zval z_params_funs;
@@ -180,6 +180,7 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
180180
zval z_params_consistent;
181181
RedisArray *ra = NULL;
182182

183+
zend_string *algorithm= NULL;
183184
zend_bool b_index = 0, b_autorehash = 0, b_pconnect = 0, consistent = 0;
184185
long l_retry_interval = 0;
185186
zend_bool b_lazy_connect = 0;
@@ -235,9 +236,8 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
235236
if ((iptr = INI_STR("redis.arrays.algorithm")) != NULL) {
236237
sapi_module.treat_data(PARSE_STRING, estrdup(iptr), &z_params_algo TSRMLS_CC);
237238
}
238-
ZVAL_NULL(&z_algo);
239239
if ((z_data = zend_hash_str_find(Z_ARRVAL(z_params_algo), name, name_len)) != NULL) {
240-
ZVAL_ZVAL(&z_algo, z_data, 1, 0);
240+
algorithm = zval_get_string(z_data);
241241
}
242242

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

341341

342342
/* create RedisArray object */
343-
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);
343+
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);
344344
if (ra) {
345345
ra->auto_rehash = b_autorehash;
346346
if(ra->prev) ra->prev->auto_rehash = b_autorehash;
347347
}
348348

349349
/* cleanup */
350+
if (algorithm) zend_string_release(algorithm);
351+
350352
zval_dtor(&z_params_hosts);
351353
zval_dtor(&z_params_prev);
352354
zval_dtor(&z_params_funs);
@@ -360,7 +362,6 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
360362
zval_dtor(&z_params_read_timeout);
361363
zval_dtor(&z_params_lazy_connect);
362364
zval_dtor(&z_params_consistent);
363-
zval_dtor(&z_algo);
364365
zval_dtor(&z_dist);
365366
zval_dtor(&z_fun);
366367

@@ -408,7 +409,7 @@ ra_make_continuum(zend_string **hosts, int nb_hosts)
408409
}
409410

410411
RedisArray *
411-
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) {
412+
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) {
412413

413414
int i, count;
414415
RedisArray *ra;
@@ -418,7 +419,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, zval *z_algo, HashTab
418419
/* create object */
419420
ra = emalloc(sizeof(RedisArray));
420421
ra->hosts = ecalloc(count, sizeof(*ra->hosts));
421-
ra->redis = ecalloc(count, sizeof(zval));
422+
ra->redis = ecalloc(count, sizeof(*ra->redis));
422423
ra->count = 0;
423424
ra->z_multi_exec = NULL;
424425
ra->index = b_index;
@@ -427,6 +428,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, zval *z_algo, HashTab
427428
ra->connect_timeout = connect_timeout;
428429
ra->read_timeout = read_timeout;
429430
ra->continuum = NULL;
431+
ra->algorithm = NULL;
430432

431433
if (ra_load_hosts(ra, hosts, retry_interval, b_lazy_connect TSRMLS_CC) == NULL || !ra->count) {
432434
for (i = 0; i < ra->count; ++i) {
@@ -438,15 +440,15 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, zval *z_algo, HashTab
438440
efree(ra);
439441
return NULL;
440442
}
441-
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;
443+
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;
442444

443445
/* init array data structures */
444446
ra_init_function_table(ra);
445447

446448
/* Set hash function and distribtor if provided */
447449
ZVAL_ZVAL(&ra->z_fun, z_fun, 1, 0);
448450
ZVAL_ZVAL(&ra->z_dist, z_dist, 1, 0);
449-
ZVAL_ZVAL(&ra->z_algo, z_algo, 1, 0);
451+
if (algorithm) ra->algorithm = zend_string_copy(algorithm);
450452

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

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

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)