Skip to content

Commit 433caf4

Browse files
committed
refactoring
1 parent 0c351f5 commit 433caf4

File tree

2 files changed

+51
-72
lines changed

2 files changed

+51
-72
lines changed

redis_array.c

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,6 @@ PHP_METHOD(RedisArray, __construct)
229229
ZVAL_NULL(&z_dist);
230230
/* extract options */
231231
if(z_opts) {
232-
zval *z_retry_interval_p;
233-
zval *z_connect_timeout_p;
234-
235232
hOpts = Z_ARRVAL_P(z_opts);
236233

237234
/* extract previous ring. */
@@ -268,36 +265,29 @@ PHP_METHOD(RedisArray, __construct)
268265
}
269266

270267
/* extract retry_interval option. */
271-
if ((z_retry_interval_p = zend_hash_str_find(hOpts, "retry_interval", sizeof("retry_interval") - 1)) != NULL) {
272-
if (Z_TYPE_P(z_retry_interval_p) == IS_LONG || Z_TYPE_P(z_retry_interval_p) == IS_STRING) {
273-
if (Z_TYPE_P(z_retry_interval_p) == IS_LONG) {
274-
l_retry_interval = Z_LVAL_P(z_retry_interval_p);
275-
} else {
276-
l_retry_interval = atol(Z_STRVAL_P(z_retry_interval_p));
277-
}
278-
}
279-
}
268+
if ((zpData = zend_hash_str_find(hOpts, "retry_interval", sizeof("retry_interval") - 1)) != NULL) {
269+
if (Z_TYPE_P(zpData) == IS_LONG) {
270+
l_retry_interval = Z_LVAL_P(zpData);
271+
} else if (Z_TYPE_P(zpData) == IS_STRING) {
272+
l_retry_interval = atol(Z_STRVAL_P(zpData));
273+
}
274+
}
280275

281276
/* extract lazy connect option. */
282277
if ((zpData = zend_hash_str_find(hOpts, "lazy_connect", sizeof("lazy_connect") - 1)) != NULL) {
283278
b_lazy_connect = zval_is_true(zpData);
284279
}
285280

286281
/* extract connect_timeout option */
287-
if ((z_connect_timeout_p = zend_hash_str_find(hOpts, "connect_timeout", sizeof("connect_timeout") - 1)) != NULL) {
288-
if (Z_TYPE_P(z_connect_timeout_p) == IS_DOUBLE ||
289-
Z_TYPE_P(z_connect_timeout_p) == IS_STRING ||
290-
Z_TYPE_P(z_connect_timeout_p) == IS_LONG
291-
) {
292-
if (Z_TYPE_P(z_connect_timeout_p) == IS_DOUBLE) {
293-
d_connect_timeout = Z_DVAL_P(z_connect_timeout_p);
294-
} else if (Z_TYPE_P(z_connect_timeout_p) == IS_LONG) {
295-
d_connect_timeout = Z_LVAL_P(z_connect_timeout_p);
296-
} else {
297-
d_connect_timeout = atof(Z_STRVAL_P(z_connect_timeout_p));
298-
}
299-
}
300-
}
282+
if ((zpData = zend_hash_str_find(hOpts, "connect_timeout", sizeof("connect_timeout") - 1)) != NULL) {
283+
if (Z_TYPE_P(zpData) == IS_DOUBLE) {
284+
d_connect_timeout = Z_DVAL_P(zpData);
285+
} else if (Z_TYPE_P(zpData) == IS_LONG) {
286+
d_connect_timeout = Z_LVAL_P(zpData);
287+
} else if (Z_TYPE_P(zpData) == IS_STRING) {
288+
d_connect_timeout = atof(Z_STRVAL_P(zpData));
289+
}
290+
}
301291
}
302292

303293
/* extract either name of list of hosts from z0 */

redis_array_impl.c

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,14 @@ ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b
4242
ZVAL_STRINGL(&z_cons, "__construct", 11);
4343

4444
/* init connections */
45-
for (zend_hash_internal_pointer_reset(hosts); zend_hash_has_more_elements(hosts) == SUCCESS; zend_hash_move_forward(hosts))
46-
{
47-
if ((zpData = zend_hash_get_current_data(hosts)) == NULL || Z_TYPE_P(zpData) != IS_STRING)
48-
{
45+
for (zend_hash_internal_pointer_reset(hosts);
46+
zend_hash_has_more_elements(hosts) == SUCCESS;
47+
zend_hash_move_forward(hosts)
48+
) {
49+
if ((zpData = zend_hash_get_current_data(hosts)) == NULL || Z_TYPE_P(zpData) != IS_STRING) {
4950
zval_dtor(&z_cons);
50-
for(i=0;i<ra->count;i++) {
51-
zval_dtor(&ra->redis[i]);
52-
efree(ra->hosts[i]);
53-
}
54-
efree(ra->redis);
55-
efree(ra->hosts);
56-
zval_dtor(&ra->z_pure_cmds);
57-
zval_dtor(&ra->z_dist);
58-
zval_dtor(&ra->z_fun);
59-
efree(ra);
60-
return NULL;
61-
}
62-
51+
return NULL;
52+
}
6353

6454
/* default values */
6555
host = Z_STRVAL_P(zpData);
@@ -272,15 +262,12 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
272262
sapi_module.treat_data(PARSE_STRING, estrdup(iptr), &z_params_retry_interval TSRMLS_CC);
273263
}
274264
if ((z_data = zend_hash_str_find(Z_ARRVAL(z_params_retry_interval), name, name_len)) != NULL) {
275-
if (Z_TYPE_P(z_data) == IS_LONG || Z_TYPE_P(z_data) == IS_STRING) {
276-
if (Z_TYPE_P(z_data) == IS_LONG) {
277-
l_retry_interval = Z_LVAL_P(z_data);
278-
}
279-
else {
280-
l_retry_interval = atol(Z_STRVAL_P(z_data));
281-
}
282-
}
283-
}
265+
if (Z_TYPE_P(z_data) == IS_LONG) {
266+
l_retry_interval = Z_LVAL_P(z_data);
267+
} else if (Z_TYPE_P(z_data) == IS_STRING) {
268+
l_retry_interval = atol(Z_STRVAL_P(z_data));
269+
}
270+
}
284271

285272
/* find pconnect option */
286273
array_init(&z_params_pconnect);
@@ -310,19 +297,14 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
310297
sapi_module.treat_data(PARSE_STRING, estrdup(iptr), &z_params_connect_timeout TSRMLS_CC);
311298
}
312299
if ((z_data = zend_hash_str_find(Z_ARRVAL(z_params_connect_timeout), name, name_len)) != NULL) {
313-
if (Z_TYPE_P(z_data) == IS_DOUBLE ||
314-
Z_TYPE_P(z_data) == IS_STRING ||
315-
Z_TYPE_P(z_data) == IS_LONG
316-
) {
317-
if (Z_TYPE_P(z_data) == IS_DOUBLE) {
318-
d_connect_timeout = Z_DVAL_P(z_data);
319-
} else if (Z_TYPE_P(z_data) == IS_LONG) {
320-
d_connect_timeout = Z_LVAL_P(z_data);
321-
} else {
322-
d_connect_timeout = atof(Z_STRVAL_P(z_data));
323-
}
324-
}
325-
}
300+
if (Z_TYPE_P(z_data) == IS_DOUBLE) {
301+
d_connect_timeout = Z_DVAL_P(z_data);
302+
} else if (Z_TYPE_P(z_data) == IS_STRING) {
303+
d_connect_timeout = atof(Z_STRVAL_P(z_data));
304+
} else if (Z_TYPE_P(z_data) == IS_LONG) {
305+
d_connect_timeout = Z_LVAL_P(z_data);
306+
}
307+
}
326308

327309
/* create RedisArray object */
328310
ra = ra_make_array(hHosts, &z_fun, &z_dist, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect, d_connect_timeout TSRMLS_CC);
@@ -351,7 +333,7 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
351333
RedisArray *
352334
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 TSRMLS_DC) {
353335

354-
int count;
336+
int i, count;
355337
RedisArray *ra;
356338

357339
if (!hosts) return NULL;
@@ -368,13 +350,20 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev
368350
ra->pconnect = b_pconnect;
369351
ra->connect_timeout = connect_timeout;
370352

371-
/* init array data structures */
372-
ra_init_function_table(ra);
353+
if (ra_load_hosts(ra, hosts, retry_interval, b_lazy_connect TSRMLS_CC) == NULL) {
354+
for (i = 0; i < ra->count; ++i) {
355+
zval_dtor(&ra->redis[i]);
356+
efree(ra->hosts[i]);
357+
}
358+
efree(ra->redis);
359+
efree(ra->hosts);
360+
efree(ra);
361+
return NULL;
362+
}
363+
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 TSRMLS_CC) : NULL;
373364

374-
if(NULL == ra_load_hosts(ra, hosts, retry_interval, b_lazy_connect TSRMLS_CC)) {
375-
return NULL;
376-
}
377-
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 TSRMLS_CC) : NULL;
365+
/* init array data structures */
366+
ra_init_function_table(ra);
378367

379368
/* Set hash function and distribtor if provided */
380369
ZVAL_ZVAL(&ra->z_fun, z_fun, 1, 0);

0 commit comments

Comments
 (0)