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

Skip to content

Commit 77c4f7a

Browse files
committed
Refactor CLIENT command
1 parent 350aff4 commit 77c4f7a

1 file changed

Lines changed: 83 additions & 101 deletions

File tree

redis_commands.c

Lines changed: 83 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4795,11 +4795,11 @@ redis_build_client_list_command(smart_string *cmdstr, int argc, zval *z_args)
47954795
zend_string *zkey;
47964796
zval *z_ele, *type = NULL, *id = NULL;
47974797

4798-
if (argc > 1) {
4799-
if (Z_TYPE(z_args[1]) != IS_ARRAY) {
4798+
if (argc > 0) {
4799+
if (Z_TYPE(z_args[0]) != IS_ARRAY) {
48004800
return FAILURE;
48014801
}
4802-
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(z_args[1]), zkey, z_ele) {
4802+
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(z_args[0]), zkey, z_ele) {
48034803
if (zkey != NULL) {
48044804
ZVAL_DEREF(z_ele);
48054805
if (zend_string_equals_literal_ci(zkey, "type")) {
@@ -4858,17 +4858,17 @@ redis_build_client_kill_command(smart_string *cmdstr, int argc, zval *z_args)
48584858
zval *z_ele, *id = NULL, *type = NULL, *address = NULL, *opts = NULL,
48594859
*user = NULL, *addr = NULL, *laddr = NULL, *skipme = NULL;
48604860

4861-
if (argc > 1) {
4862-
if (argc > 2) {
4863-
if (Z_TYPE(z_args[1]) != IS_STRING || Z_TYPE(z_args[2]) != IS_ARRAY) {
4861+
if (argc > 0) {
4862+
if (argc > 1) {
4863+
if (Z_TYPE(z_args[0]) != IS_STRING || Z_TYPE(z_args[1]) != IS_ARRAY) {
48644864
return FAILURE;
48654865
}
4866-
address = &z_args[1];
4867-
opts = &z_args[2];
4868-
} else if (Z_TYPE(z_args[1]) == IS_STRING) {
4869-
address = &z_args[1];
4870-
} else if (Z_TYPE(z_args[1]) == IS_ARRAY) {
4866+
address = &z_args[0];
48714867
opts = &z_args[1];
4868+
} else if (Z_TYPE(z_args[0]) == IS_STRING) {
4869+
address = &z_args[0];
4870+
} else if (Z_TYPE(z_args[0]) == IS_ARRAY) {
4871+
opts = &z_args[0];
48724872
} else {
48734873
return FAILURE;
48744874
}
@@ -4950,14 +4950,14 @@ redis_build_client_tracking_command(smart_string *cmdstr, int argc, zval *z_args
49504950
zval *z_ele, *redirect = NULL, *prefix = NULL;
49514951
zend_bool bcast = 0, optin = 0, optout = 0, noloop = 0;
49524952

4953-
if (argc < 2) {
4953+
if (argc < 1) {
49544954
return FAILURE;
49554955
}
4956-
if (argc > 2) {
4957-
if (Z_TYPE(z_args[2]) != IS_ARRAY) {
4956+
if (argc > 1) {
4957+
if (Z_TYPE(z_args[1]) != IS_ARRAY) {
49584958
return FAILURE;
49594959
}
4960-
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(z_args[2]), zkey, z_ele) {
4960+
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(z_args[1]), zkey, z_ele) {
49614961
if (zkey != NULL) {
49624962
ZVAL_DEREF(z_ele);
49634963
if (zend_string_equals_literal_ci(zkey, "redirect")) {
@@ -4986,12 +4986,12 @@ redis_build_client_tracking_command(smart_string *cmdstr, int argc, zval *z_args
49864986
+ (prefix ? 2 * zend_hash_num_elements(Z_ARRVAL_P(prefix)) : 0)
49874987
+ bcast + optin + optout + noloop, "CLIENT");
49884988
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "TRACKING");
4989-
if (Z_TYPE(z_args[1]) == IS_STRING && (
4990-
ZVAL_STRICMP_STATIC(&z_args[1], "on") ||
4991-
ZVAL_STRICMP_STATIC(&z_args[1], "off")
4989+
if (Z_TYPE(z_args[0]) == IS_STRING && (
4990+
ZVAL_STRICMP_STATIC(&z_args[0], "on") ||
4991+
ZVAL_STRICMP_STATIC(&z_args[0], "off")
49924992
)) {
4993-
redis_cmd_append_sstr(cmdstr, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
4994-
} else if (zval_is_true(&z_args[1])) {
4993+
redis_cmd_append_sstr(cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
4994+
} else if (zval_is_true(&z_args[0])) {
49954995
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "ON");
49964996
} else {
49974997
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "OFF");
@@ -5036,171 +5036,153 @@ int
50365036
redis_client_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
50375037
char **cmd, int *cmd_len, short *slot, void **ctx)
50385038
{
5039-
int argc;
50405039
smart_string cmdstr = {0};
5041-
zval *z_args;
5042-
5043-
if ((argc = ZEND_NUM_ARGS()) < 1) {
5044-
return FAILURE;
5045-
}
5040+
zend_string *op = NULL;
5041+
zval *z_args = NULL;
5042+
int argc = 0;
50465043

5047-
z_args = ecalloc(argc, sizeof(*z_args));
5048-
if (zend_get_parameters_array(ht, argc, z_args) == FAILURE ||
5049-
Z_TYPE(z_args[0]) != IS_STRING
5050-
) {
5051-
efree(z_args);
5052-
return FAILURE;
5053-
}
5044+
ZEND_PARSE_PARAMETERS_START(1, -1)
5045+
Z_PARAM_STR(op)
5046+
Z_PARAM_OPTIONAL
5047+
Z_PARAM_VARIADIC('*', z_args, argc)
5048+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
50545049

5055-
if (ZVAL_STRICMP_STATIC(&z_args[0], "info")) {
5050+
if (zend_string_equals_literal_ci(op, "INFO")) {
50565051
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1, "CLIENT");
50575052
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "INFO");
5058-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "list")) {
5053+
} else if (zend_string_equals_literal_ci(op, "LIST")) {
50595054
if (redis_build_client_list_command(&cmdstr, argc, z_args) != 0) {
5060-
efree(z_args);
50615055
return FAILURE;
50625056
}
50635057
*ctx = PHPREDIS_CTX_PTR;
5064-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "caching")) {
5065-
if (argc < 2) {
5066-
efree(z_args);
5058+
} else if (zend_string_equals_literal_ci(op, "CACHING")) {
5059+
if (argc < 1) {
50675060
return FAILURE;
50685061
}
50695062
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2, "CLIENT");
50705063
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "CACHING");
5071-
if (Z_TYPE(z_args[1]) == IS_STRING && (
5072-
ZVAL_STRICMP_STATIC(&z_args[1], "yes") ||
5073-
ZVAL_STRICMP_STATIC(&z_args[1], "no")
5064+
if (Z_TYPE(z_args[0]) == IS_STRING && (
5065+
ZVAL_STRICMP_STATIC(&z_args[0], "yes") ||
5066+
ZVAL_STRICMP_STATIC(&z_args[0], "no")
50745067
)) {
5075-
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
5076-
} else if (zval_is_true(&z_args[1])) {
5068+
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
5069+
} else if (zval_is_true(&z_args[0])) {
50775070
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "YES");
50785071
} else {
50795072
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "NO");
50805073
}
50815074
*ctx = PHPREDIS_CTX_PTR + 1;
5082-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "getname")) {
5075+
} else if (zend_string_equals_literal_ci(op, "GETNAME")) {
50835076
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1, "CLIENT");
50845077
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "GETNAME");
50855078
*ctx = PHPREDIS_CTX_PTR + 3;
5086-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "getredir") || ZVAL_STRICMP_STATIC(&z_args[0], "id")) {
5079+
} else if (zend_string_equals_literal_ci(op, "GETREDIR") || zend_string_equals_literal_ci(op, "ID")) {
50875080
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1, "CLIENT");
5088-
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
5081+
redis_cmd_append_sstr(&cmdstr, ZSTR_VAL(op), ZSTR_LEN(op));
50895082
*ctx = PHPREDIS_CTX_PTR + 2;
5090-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "kill")) {
5083+
} else if (zend_string_equals_literal_ci(op, "KILL")) {
50915084
if (redis_build_client_kill_command(&cmdstr, argc, z_args) != 0) {
5092-
efree(z_args);
50935085
return FAILURE;
50945086
}
50955087
*ctx = PHPREDIS_CTX_PTR + 1;
5096-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "no-evict")) {
5097-
if (argc < 2) {
5098-
efree(z_args);
5088+
} else if (zend_string_equals_literal_ci(op, "NO-EVICT")) {
5089+
if (argc < 1) {
50995090
return FAILURE;
51005091
}
51015092
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2, "CLIENT");
51025093
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "NO-EVICT");
5103-
if (Z_TYPE(z_args[1]) == IS_STRING && (
5104-
ZVAL_STRICMP_STATIC(&z_args[1], "on") ||
5105-
ZVAL_STRICMP_STATIC(&z_args[1], "off")
5094+
if (Z_TYPE(z_args[0]) == IS_STRING && (
5095+
ZVAL_STRICMP_STATIC(&z_args[0], "on") ||
5096+
ZVAL_STRICMP_STATIC(&z_args[0], "off")
51065097
)) {
5107-
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
5108-
} else if (zval_is_true(&z_args[1])) {
5098+
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
5099+
} else if (zval_is_true(&z_args[0])) {
51095100
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "ON");
51105101
} else {
51115102
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "OFF");
51125103
}
51135104
*ctx = PHPREDIS_CTX_PTR + 1;
5114-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "pause")) {
5115-
if (argc < 2 || Z_TYPE(z_args[1]) != IS_LONG || (
5116-
argc > 2 && (
5117-
Z_TYPE(z_args[2]) != IS_STRING || (
5118-
!ZVAL_STRICMP_STATIC(&z_args[2], "write") &&
5119-
!ZVAL_STRICMP_STATIC(&z_args[2], "all")
5105+
} else if (zend_string_equals_literal_ci(op, "PAUSE")) {
5106+
if (argc < 1 || Z_TYPE(z_args[0]) != IS_LONG || (
5107+
argc > 1 && (
5108+
Z_TYPE(z_args[1]) != IS_STRING || (
5109+
!ZVAL_STRICMP_STATIC(&z_args[1], "write") &&
5110+
!ZVAL_STRICMP_STATIC(&z_args[1], "all")
51205111
)
51215112
)
51225113
)) {
5123-
efree(z_args);
51245114
return FAILURE;
51255115
}
5126-
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc > 2 ? 3 : 2, "CLIENT");
5116+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc > 1 ? 3 : 2, "CLIENT");
51275117
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "PAUSE");
5128-
redis_cmd_append_sstr_long(&cmdstr, Z_LVAL(z_args[1]));
5129-
if (argc > 2) {
5130-
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[2]), Z_STRLEN(z_args[2]));
5118+
redis_cmd_append_sstr_long(&cmdstr, Z_LVAL(z_args[0]));
5119+
if (argc > 1) {
5120+
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
51315121
}
51325122
*ctx = PHPREDIS_CTX_PTR + 1;
5133-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "reply")) {
5134-
if (argc > 1 && (
5135-
Z_TYPE(z_args[1]) != IS_STRING || (
5136-
!ZVAL_STRICMP_STATIC(&z_args[1], "on") &&
5137-
!ZVAL_STRICMP_STATIC(&z_args[1], "off") &&
5138-
!ZVAL_STRICMP_STATIC(&z_args[1], "skip")
5123+
} else if (zend_string_equals_literal_ci(op, "REPLY")) {
5124+
if (argc > 0 && (
5125+
Z_TYPE(z_args[0]) != IS_STRING || (
5126+
!ZVAL_STRICMP_STATIC(&z_args[0], "on") &&
5127+
!ZVAL_STRICMP_STATIC(&z_args[0], "off") &&
5128+
!ZVAL_STRICMP_STATIC(&z_args[0], "skip")
51395129
)
51405130
)) {
5141-
efree(z_args);
51425131
return FAILURE;
51435132
}
5144-
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc > 1 ? 2 : 1, "CLIENT");
5133+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc > 0 ? 2 : 1, "CLIENT");
51455134
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "REPLY");
5146-
if (argc > 1) {
5147-
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
5135+
if (argc > 0) {
5136+
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
51485137
}
51495138
*ctx = PHPREDIS_CTX_PTR + 1;
5150-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "setname")) {
5151-
if (argc < 2 || Z_TYPE(z_args[1]) != IS_STRING) {
5152-
efree(z_args);
5139+
} else if (zend_string_equals_literal_ci(op, "SETNAME")) {
5140+
if (argc < 1 || Z_TYPE(z_args[0]) != IS_STRING) {
51535141
return FAILURE;
51545142
}
51555143
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2, "CLIENT");
51565144
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "SETNAME");
5157-
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
5145+
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
51585146
*ctx = PHPREDIS_CTX_PTR + 1;
5159-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "tracking")) {
5147+
} else if (zend_string_equals_literal_ci(op, "TRACKING")) {
51605148
if (redis_build_client_tracking_command(&cmdstr, argc, z_args) != 0) {
5161-
efree(z_args);
51625149
return FAILURE;
51635150
}
51645151
*ctx = PHPREDIS_CTX_PTR + 1;
5165-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "trackinginfo")) {
5152+
} else if (zend_string_equals_literal_ci(op, "TRACKINGINFO")) {
51665153
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1, "CLIENT");
51675154
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "TRACKINGINFO");
51685155
*ctx = PHPREDIS_CTX_PTR + 4;
5169-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "unblock")) {
5170-
if (argc < 2 || Z_TYPE(z_args[1]) != IS_STRING || (
5171-
argc > 2 && (
5172-
Z_TYPE(z_args[2]) != IS_STRING || (
5173-
!ZVAL_STRICMP_STATIC(&z_args[2], "timeout") &&
5174-
!ZVAL_STRICMP_STATIC(&z_args[2], "error")
5156+
} else if (zend_string_equals_literal_ci(op, "UNBLOCK")) {
5157+
if (argc < 1 || Z_TYPE(z_args[0]) != IS_STRING || (
5158+
argc > 1 && (
5159+
Z_TYPE(z_args[1]) != IS_STRING || (
5160+
!ZVAL_STRICMP_STATIC(&z_args[1], "timeout") &&
5161+
!ZVAL_STRICMP_STATIC(&z_args[1], "error")
51755162
)
51765163
)
51775164
)) {
5178-
efree(z_args);
51795165
return FAILURE;
51805166
}
5181-
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc > 2 ? 3 : 2, "CLIENT");
5167+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc > 1 ? 3 : 2, "CLIENT");
51825168
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "UNBLOCK");
5183-
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
5184-
if (argc > 2) {
5185-
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[2]), Z_STRLEN(z_args[2]));
5169+
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
5170+
if (argc > 1) {
5171+
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
51865172
}
51875173
*ctx = PHPREDIS_CTX_PTR + 2;
5188-
} else if (ZVAL_STRICMP_STATIC(&z_args[0], "unpause")) {
5174+
} else if (zend_string_equals_literal_ci(op, "UNPAUSE")) {
51895175
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2, "CLIENT");
51905176
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "UNPAUSE");
51915177
*ctx = PHPREDIS_CTX_PTR + 1;
51925178
} else {
5193-
efree(z_args);
51945179
return FAILURE;
51955180
}
51965181

51975182
// Push out values
51985183
*cmd = cmdstr.c;
51995184
*cmd_len = cmdstr.len;
52005185

5201-
// Cleanup arg array
5202-
efree(z_args);
5203-
52045186
return SUCCESS;
52055187
}
52065188

0 commit comments

Comments
 (0)