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

Skip to content

Commit 1250f00

Browse files
yatsukhnenkomichael-grunder
authored andcommitted
Refactor ra_generic_del
1 parent 017b2ea commit 1250f00

1 file changed

Lines changed: 26 additions & 40 deletions

File tree

redis_array.c

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,15 +1060,14 @@ PHP_METHOD(RedisArray, mset)
10601060
}
10611061

10621062
/* Generic handler for DEL or UNLINK which behave identically to phpredis */
1063-
static void ra_generic_del(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len) {
1064-
zval *object, z_keys, z_fun, *data, z_ret, *z_args;
1065-
int i, n;
1066-
RedisArray *ra;
1067-
int *pos, argc = ZEND_NUM_ARGS(), *argc_each;
1063+
static void
1064+
ra_generic_del(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len)
1065+
{
1066+
zval *object, z_keys, z_fun, *data, z_ret, *z_args, **argv;
1067+
int i, n, *pos, argc = ZEND_NUM_ARGS(), *argc_each, free_zkeys = 0;
10681068
HashTable *h_keys;
1069-
zval **argv;
1069+
RedisArray *ra;
10701070
long total = 0;
1071-
int free_zkeys = 0;
10721071

10731072
if ((ra = redis_array_get(getThis())) == NULL) {
10741073
RETURN_FALSE;
@@ -1078,7 +1077,7 @@ static void ra_generic_del(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len) {
10781077
HANDLE_MULTI_EXEC(ra, kw, kw_len);
10791078

10801079
/* get all args in z_args */
1081-
z_args = emalloc(argc * sizeof(zval));
1080+
z_args = ecalloc(argc, sizeof(*z_args));
10821081
if (zend_get_parameters_array(ht, argc, z_args) == FAILURE) {
10831082
efree(z_args);
10841083
RETURN_FALSE;
@@ -1091,11 +1090,7 @@ static void ra_generic_del(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len) {
10911090
/* copy all elements to z_keys */
10921091
array_init(&z_keys);
10931092
for (i = 0; i < argc; ++i) {
1094-
zval *z_arg = &z_args[i];
1095-
zval z_ret;
1096-
ZVAL_ZVAL(&z_ret, z_arg, 1, 0);
1097-
/* add copy to z_keys */
1098-
add_next_index_zval(&z_keys, &z_ret);
1093+
add_next_index_zval(&z_keys, &z_args[i]);
10991094
}
11001095
free_zkeys = 1;
11011096
}
@@ -1107,27 +1102,23 @@ static void ra_generic_del(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len) {
11071102
efree(z_args);
11081103
RETURN_FALSE;
11091104
}
1110-
argv = emalloc(argc * sizeof(zval*));
1111-
pos = emalloc(argc * sizeof(int));
1105+
argv = ecalloc(argc, sizeof(*argv));
1106+
pos = ecalloc(argc, sizeof(*pos));
11121107

1113-
argc_each = emalloc(ra->count * sizeof(int));
1114-
memset(argc_each, 0, ra->count * sizeof(int));
1108+
argc_each = ecalloc(ra->count, sizeof(*argc_each));
11151109

11161110
/* associate each key to a redis node */
11171111
i = 0;
11181112
ZEND_HASH_FOREACH_VAL(h_keys, data) {
11191113
if (Z_TYPE_P(data) != IS_STRING) {
11201114
php_error_docref(NULL, E_ERROR, "DEL: all keys must be string.");
1121-
if (free_zkeys) zval_dtor(&z_keys);
1122-
efree(z_args);
1123-
efree(argv);
1124-
efree(pos);
1125-
efree(argc_each);
1126-
RETURN_FALSE;
1115+
RETVAL_FALSE;
1116+
goto cleanup;
11271117
}
11281118

11291119
if (ra_find_node(ra, Z_STRVAL_P(data), Z_STRLEN_P(data), &pos[i]) == NULL) {
1130-
// TODO: handle
1120+
RETVAL_FALSE;
1121+
goto cleanup;
11311122
}
11321123
argc_each[pos[i]]++; /* count number of keys per node */
11331124
argv[i++] = data;
@@ -1147,12 +1138,10 @@ static void ra_generic_del(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len) {
11471138
/* copy args */
11481139
array_init(&z_argarray);
11491140
for(i = 0; i < argc; ++i) {
1150-
if(pos[i] != n) continue;
1151-
1152-
zval z_ret;
1153-
ZVAL_ZVAL(&z_ret, argv[i], 1, 0);
1154-
add_next_index_zval(&z_argarray, &z_ret);
1155-
found++;
1141+
if (pos[i] == n) {
1142+
add_next_index_zval(&z_argarray, argv[i]);
1143+
found++;
1144+
}
11561145
}
11571146

11581147
if(!found) { /* don't run empty DEL or UNLINK commands */
@@ -1162,34 +1151,31 @@ static void ra_generic_del(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len) {
11621151

11631152
if(ra->index) { /* add MULTI */
11641153
ra_index_multi(&ra->redis[n], MULTI);
1165-
}
1166-
1167-
/* call */
1168-
call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
1169-
1170-
if(ra->index) {
1171-
zval_dtor(&z_ret);
1154+
call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
11721155
ra_index_del(&z_argarray, &ra->redis[n]); /* use SREM to remove keys from node index */
11731156
ra_index_exec(&ra->redis[n], &z_ret, 0); /* run EXEC */
1157+
} else {
1158+
call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
11741159
}
11751160
total += Z_LVAL(z_ret); /* increment total */
11761161

11771162
zval_dtor(&z_argarray);
11781163
zval_dtor(&z_ret);
11791164
}
11801165

1181-
/* cleanup */
11821166
zval_dtor(&z_fun);
1167+
1168+
RETVAL_LONG(total);
1169+
1170+
cleanup:
11831171
efree(argv);
11841172
efree(pos);
11851173
efree(argc_each);
11861174

11871175
if(free_zkeys) {
11881176
zval_dtor(&z_keys);
11891177
}
1190-
11911178
efree(z_args);
1192-
RETURN_LONG(total);
11931179
}
11941180

11951181
/* DEL will distribute the call to several nodes and regroup the values. */

0 commit comments

Comments
 (0)