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

Skip to content

Commit 19fd7e0

Browse files
Refactor PFCOUNT command.
1 parent 90eb047 commit 19fd7e0

4 files changed

Lines changed: 34 additions & 75 deletions

File tree

redis.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,11 +2248,11 @@ public function pfadd(string $key, array $elements): Redis|int;
22482248
*
22492249
* @see https://redis.io/commands/pfcount
22502250
*
2251-
* @param string $key The key name we wish to query.
2251+
* @param string $key_or_keys Either one key or an array of keys
22522252
*
22532253
* @return Redis|int The estimated cardinality of the set.
22542254
*/
2255-
public function pfcount(string $key): Redis|int;
2255+
public function pfcount(array|string $key_or_keys): Redis|int|false;
22562256

22572257
/**
22582258
* Merge one or more source HyperLogLog sets into a destination set.

redis_arginfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 399e9506bc58ff0da1abc8f46a02b2499ed1223a */
2+
* Stub hash: 2d2bd3a96ba44622f4157ee2e06695ffb87a4949 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")
@@ -604,8 +604,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_pfadd, 0, 2, Red
604604
ZEND_ARG_TYPE_INFO(0, elements, IS_ARRAY, 0)
605605
ZEND_END_ARG_INFO()
606606

607-
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_pfcount, 0, 1, Redis, MAY_BE_LONG)
608-
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
607+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_pfcount, 0, 1, Redis, MAY_BE_LONG|MAY_BE_FALSE)
608+
ZEND_ARG_TYPE_MASK(0, key_or_keys, MAY_BE_ARRAY|MAY_BE_STRING, NULL)
609609
ZEND_END_ARG_INFO()
610610

611611
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_pfmerge, 0, 2, Redis, MAY_BE_BOOL)

redis_commands.c

Lines changed: 25 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3163,89 +3163,46 @@ int redis_pfmerge_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
31633163
int redis_pfcount_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
31643164
char **cmd, int *cmd_len, short *slot, void **ctx)
31653165
{
3166-
zval *z_keys, *z_key;
3167-
HashTable *ht_keys;
31683166
smart_string cmdstr = {0};
3169-
int num_keys, key_free;
3170-
size_t key_len;
3171-
char *key;
3172-
short kslot=-1;
3173-
zend_string *zstr;
3167+
zval *zarg = NULL, *zv;
3168+
short slot2 = -1;
3169+
uint32_t keys;
31743170

3175-
if (zend_parse_parameters(ZEND_NUM_ARGS(),"z",&z_keys) == FAILURE) {
3176-
return FAILURE;
3177-
}
3178-
3179-
/* If we were passed an array of keys, iterate through them prefixing if
3180-
* required and capturing lengths and if we need to free them. Otherwise
3181-
* attempt to treat the argument as a string and just pass one */
3182-
if (Z_TYPE_P(z_keys) == IS_ARRAY) {
3183-
/* Grab key hash table and the number of keys */
3184-
ht_keys = Z_ARRVAL_P(z_keys);
3185-
num_keys = zend_hash_num_elements(ht_keys);
3171+
ZEND_PARSE_PARAMETERS_START(1, 1)
3172+
Z_PARAM_ZVAL(zarg)
3173+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
31863174

3187-
/* There is no reason to send zero keys */
3188-
if (num_keys == 0) {
3175+
if (Z_TYPE_P(zarg) == IS_STRING) {
3176+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1, "PFCOUNT");
3177+
redis_cmd_append_sstr_key_zstr(&cmdstr, Z_STR_P(zarg), redis_sock, slot);
3178+
} else if (Z_TYPE_P(zarg) == IS_ARRAY) {
3179+
keys = zend_hash_num_elements(Z_ARRVAL_P(zarg));
3180+
if (keys == 0)
31893181
return FAILURE;
3190-
}
31913182

3192-
/* Initialize the command with our number of arguments */
3193-
redis_cmd_init_sstr(&cmdstr, num_keys, ZEND_STRL("PFCOUNT"));
3183+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, keys, "PFCOUNT");
31943184

3195-
/* Append our key(s) */
3196-
ZEND_HASH_FOREACH_VAL(ht_keys, z_key) {
3197-
/* Turn our value into a string if it isn't one */
3198-
zstr = zval_get_string(z_key);
3199-
key = ZSTR_VAL(zstr);
3200-
key_len = ZSTR_LEN(zstr);
3201-
3202-
/* Append this key to our command */
3203-
key_free = redis_key_prefix(redis_sock, &key, &key_len);
3204-
redis_cmd_append_sstr(&cmdstr, key, key_len);
3205-
3206-
/* Protect against CROSSLOT errors */
3185+
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zarg), zv) {
3186+
redis_cmd_append_sstr_key_zval(&cmdstr, zv, redis_sock, slot);
32073187
if (slot) {
3208-
if (kslot == -1) {
3209-
kslot = cluster_hash_key(key, key_len);
3210-
} else if (cluster_hash_key(key,key_len)!=kslot) {
3211-
zend_string_release(zstr);
3212-
if (key_free) efree(key);
3213-
efree(cmdstr.c);
3214-
3215-
php_error_docref(NULL, E_WARNING,
3216-
"Not all keys hash to the same slot!");
3217-
return FAILURE;
3218-
}
3188+
if (slot2 != -1 && slot2 != *slot)
3189+
goto cross_slot;
3190+
slot2 = *slot;
32193191
}
3220-
3221-
/* Cleanup */
3222-
zend_string_release(zstr);
3223-
if (key_free) efree(key);
32243192
} ZEND_HASH_FOREACH_END();
32253193
} else {
3226-
/* Construct our whole command */
3227-
redis_cmd_init_sstr(&cmdstr, 1, ZEND_STRL("PFCOUNT"));
3228-
3229-
/* Turn our key into a string if it's a different type */
3230-
zstr = zval_get_string(z_keys);
3231-
key = ZSTR_VAL(zstr);
3232-
key_len = ZSTR_LEN(zstr);
3233-
key_free = redis_key_prefix(redis_sock, &key, &key_len);
3234-
redis_cmd_append_sstr(&cmdstr, key, key_len);
3235-
3236-
/* Hash our key */
3237-
CMD_SET_SLOT(slot, key, key_len);
3238-
3239-
/* Cleanup */
3240-
zend_string_release(zstr);
3241-
if (key_free) efree(key);
3194+
php_error_docref(NULL, E_WARNING, "Argument must be either an array or a string");
3195+
return FAILURE;
32423196
}
32433197

3244-
/* Push our command and length to the caller */
32453198
*cmd = cmdstr.c;
32463199
*cmd_len = cmdstr.len;
3247-
32483200
return SUCCESS;
3201+
3202+
cross_slot:
3203+
php_error_docref(NULL, E_WARNING, "Not all keys hash to the same slot!");
3204+
efree(cmdstr.c);
3205+
return FAILURE;
32493206
}
32503207

32513208
int redis_auth_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,

redis_legacy_arginfo.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 399e9506bc58ff0da1abc8f46a02b2499ed1223a */
2+
* Stub hash: 2d2bd3a96ba44622f4157ee2e06695ffb87a4949 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_INFO(0, options)
@@ -527,7 +527,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_pfadd, 0, 0, 2)
527527
ZEND_ARG_INFO(0, elements)
528528
ZEND_END_ARG_INFO()
529529

530-
#define arginfo_class_Redis_pfcount arginfo_class_Redis__prefix
530+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_pfcount, 0, 0, 1)
531+
ZEND_ARG_INFO(0, key_or_keys)
532+
ZEND_END_ARG_INFO()
531533

532534
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_pfmerge, 0, 0, 2)
533535
ZEND_ARG_INFO(0, dst)

0 commit comments

Comments
 (0)