8000 pipeline/exec refactoring · jrtkcoder/phpredis@1d8a393 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d8a393

Browse files
committed
pipeline/exec refactoring
1 parent 3a98610 commit 1d8a393

File tree

2 files changed

+33
-53
lines changed

2 files changed

+33
-53
lines changed

common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ typedef enum _PUBSUB_TYPE {
9797

9898
#define IF_MULTI_OR_PIPELINE() if(redis_sock->mode == MULTI || redis_sock->mode == PIPELINE)
9999
#define IF_PIPELINE() if(redis_sock->mode == PIPELINE)
100+
#define IF_NOT_PIPELINE() if(redis_sock->mode != PIPELINE)
100101
#define IF_NOT_MULTI() if(redis_sock->mode != MULTI)
101102
#define IF_NOT_ATOMIC() if(redis_sock->mode != ATOMIC)
102103
#define IF_ATOMIC() if(redis_sock->mode == ATOMIC)

redis.c

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,15 +2356,13 @@ PHP_METHOD(Redis, exec)
23562356
char *cmd;
23572357
int cmd_len;
23582358
zval *object;
2359-
struct request_item *ri;
23602359

2361-
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
2362-
&object, redis_ce) == FAILURE) {
2360+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(),
2361+
"O", &object, redis_ce) == FAILURE ||
2362+
redis_sock_get(object, &redis_sock TSRMLS_CC, 0) < 0
2363+
) {
23632364
RETURN_FALSE;
23642365
}
2365-
if (redis_sock_get(object, &redis_sock TSRMLS_CC, 0) < 0) {
2366-
RETURN_FALSE;
2367-
}
23682366

23692367
IF_MULTI() {
23702368
cmd_len = redis_cmd_format_static(&cmd, "EXEC", "");
@@ -2392,47 +2390,30 @@ PHP_METHOD(Redis, exec)
23922390

23932391
IF_PIPELINE() {
23942392
char *request = NULL;
2395-
int total = 0;
2396-
int offset = 0;
2393+
int total = 0, offset = 0;
2394+
struct request_item *ri;
23972395

23982396
/* compute the total request size */
23992397
for(ri = redis_sock->pipeline_head; ri; ri = ri->next) {
24002398
total += ri->request_size;
24012399
}
2402-
if(total) {
2403-
request = malloc(total);
2404-
}
2405-
2406-
/* concatenate individual elements one by one in the target buffer */
2407-
for(ri = redis_sock->pipeline_head; ri; ri = ri->next) {
2408-
memcpy(request + offset, ri->request_str, ri->request_size);
2409-
offset += ri->request_size;
2410-
}
2411-
2412-
if(request != NULL) {
2413-
if (redis_sock_write(redis_sock, request, total TSRMLS_CC) < 0) {
2414-
free(request);
2415-
free_reply_callbacks(object, redis_sock);
2416-
redis_sock->mode = ATOMIC;
2417-
RETURN_FALSE;
2400+
if (total) {
2401+
request = emalloc(total);
2402+
/* concatenate individual elements one by one in the target buffer */
2403+
for (ri = redis_sock->pipeline_head; ri; ri = ri->next) {
2404+
memcpy(request + offset, ri->request_str, ri->request_size);
2405+
offset += ri->request_size;
24182406
}
2419-
free(request);
2407+
if (redis_sock_write(redis_sock, request, total TSRMLS_CC) < 0 ||
2408+
redis_sock_read_multibulk_pipeline_reply(
2409+
INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock) < 0
2410+
) {
2411+
ZVAL_FALSE(return_value);
2412+
}
2413+
efree(request);
24202414
} else {
2421-
redis_sock->mode = ATOMIC;
2422-
free_reply_callbacks(object, redis_sock);
2423-
2424-
/* Empty array when no command was run. */
2425-
array_init(return_value);
2426-
return;
2427-
}
2428-
2429-
if (redis_sock_read_multibulk_pipeline_reply(
2430-
INTERNAL_FUNCTION_PARAM_PASSTHRU,
2431-
redis_sock) < 0)
2432-
{
2433-
redis_sock->mode = ATOMIC;
2434-
free_reply_callbacks(object, redis_sock);
2435-
RETURN_FALSE;
2415+
/* Empty array when no command was run. */
2416+
array_init(return_value);
24362417
}
24372418
redis_sock->mode = ATOMIC;
24382419
free_reply_callbacks(object, redis_sock);
@@ -2482,23 +2463,21 @@ PHP_METHOD(Redis, pipeline)
24822463
RedisSock *redis_sock;
24832464
zval *object;
24842465

2485-
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
2486-
&object, redis_ce) == FAILURE) {
2487-
RETURN_FALSE;
2488-
}
2489-
2490-
/* if the flag is activated, send the command, the reply will be "QUEUED"
2491-
* or -ERR */
2492-
if (redis_sock_get(object, &redis_sock TSRMLS_CC, 0) < 0) {
2466+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(),
2467+
"O", &object, redis_ce) == FAILURE ||
2468+
redis_sock_get(object, &redis_sock TSRMLS_CC, 0) < 0
2469+
) {
24932470
RETURN_FALSE;
24942471
}
2495-
redis_sock->mode = PIPELINE;
24962472

2497-
/* NB : we keep the function fold, to detect the last function.
2498-
* We need the response format of the n - 1 command. So, we can delete
2499-
* when n > 2, the { 1 .. n - 2} commands */
2500-
free_reply_callbacks(getThis(), redis_sock);
2473+
IF_NOT_PIPELINE() {
2474+
redis_sock->mode = PIPELINE;
25012475

2476+
/* NB : we keep the function fold, to detect the last function.
2477+
* We need the response format of the n - 1 command. So, we can delete
2478+
* when n > 2, the { 1 .. n - 2} commands */
2479+
free_reply_callbacks(getThis(), redis_sock);
2480+
}
25022481
RETURN_ZVAL(getThis(), 1, 0);
25032482
}
25042483

0 commit comments

Comments
 (0)
0