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

Skip to content

Commit 5b560cc

Browse files
Refactor a couple more command methods.
Use the new argument parsing API
1 parent 15347c7 commit 5b560cc

1 file changed

Lines changed: 29 additions & 52 deletions

File tree

redis_commands.c

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -363,46 +363,27 @@ int redis_key_key_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
363363
char *kw, char **cmd, int *cmd_len, short *slot,
364364
void **ctx)
365365
{
366-
char *k1, *k2;
367-
size_t k1len, k2len;
368-
int k1free, k2free;
369-
370-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &k1, &k1len,
371-
&k2, &k2len) == FAILURE)
372-
{
373-
return FAILURE;
374-
}
366+
zend_string *key1 = NULL, *key2 = NULL;
367+
smart_string cmdstr = {0};
368+
short slot2;
375369

376-
// Prefix both keys
377-
k1free = redis_key_prefix(redis_sock, &k1, &k1len);
378-
k2free = redis_key_prefix(redis_sock, &k2, &k2len);
370+
ZEND_PARSE_PARAMETERS_START(2, 2)
371+
Z_PARAM_STR(key1)
372+
Z_PARAM_STR(key2)
373+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
379374

380-
// If a slot is requested, we can test that they hash the same
381-
if (slot) {
382-
// Slots where these keys resolve
383-
short slot1 = cluster_hash_key(k1, k1len);
384-
short slot2 = cluster_hash_key(k2, k2len);
385-
386-
// Check if Redis would give us a CROSSLOT error
387-
if (slot1 != slot2) {
388-
php_error_docref(0, E_WARNING, "Keys don't hash to the same slot");
389-
if (k1free) efree(k1);
390-
if (k2free) efree(k2);
391-
return FAILURE;
392-
}
375+
redis_cmd_init_sstr(&cmdstr, 2, kw, strlen(kw));
376+
redis_cmd_append_sstr_key_zstr(&cmdstr, key1, redis_sock, slot);
377+
redis_cmd_append_sstr_key_zstr(&cmdstr, key2, redis_sock, slot ? &slot2 : NULL);
393378

394-
// They're both the same
395-
*slot = slot1;
379+
if (slot && *slot != slot2) {
380+
php_error_docref(0, E_WARNING, "Keys don't hash to the same slot");
381+
smart_string_free(&cmdstr);
382+
return FAILURE;
396383
}
397384

398-
/* Send keys as normal strings because we manually prefixed to check against
399-
* cross slot error. */
400-
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ss", k1, k1len, k2, k2len);
401-
402-
/* Clean keys up if we prefixed */
403-
if (k1free) efree(k1);
404-
if (k2free) efree(k2);
405-
385+
*cmd = cmdstr.c;
386+
*cmd_len = cmdstr.len;
406387
return SUCCESS;
407388
}
408389

@@ -411,19 +392,16 @@ int redis_key_long_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
411392
char *kw, char **cmd, int *cmd_len, short *slot,
412393
void **ctx)
413394
{
414-
char *key;
415-
size_t keylen;
416-
zend_long lval;
395+
zend_string *key = NULL;
396+
zend_long lval = 0;
417397

418-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &key, &keylen, &lval)
419-
==FAILURE)
420-
{
421-
return FAILURE;
422-
}
398+
ZEND_PARSE_PARAMETERS_START(2, 2)
399+
Z_PARAM_STR(key)
400+
Z_PARAM_LONG(lval)
401+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
423402

424-
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kl", key, keylen, lval);
403+
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kl", ZSTR_VAL(key), ZSTR_LEN(key), lval);
425404

426-
// Success!
427405
return SUCCESS;
428406
}
429407

@@ -432,15 +410,14 @@ int redis_long_long_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
432410
char *kw, char **cmd, int *cmd_len, short *slot,
433411
void **ctx)
434412
{
435-
zend_long v1, v2;
413+
zend_long l1 = 0, l2 = 0;
436414

437-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &v1, &v2)
438-
== FAILURE)
439-
{
440-
return FAILURE;
441-
}
415+
ZEND_PARSE_PARAMETERS_START(2, 2)
416+
Z_PARAM_LONG(l1)
417+
Z_PARAM_LONG(l2)
418+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
442419

443-
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ll", v1, v2);
420+
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ll", l1, l2);
444421

445422
return SUCCESS;
446423
}

0 commit comments

Comments
 (0)