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

Skip to content

Commit 6430050

Browse files
SINTERCARD and ZINTERCARD commands
Implement Redis 7.0.0 commands SINTERCARD and ZINTERCARD.
1 parent 239678a commit 6430050

15 files changed

Lines changed: 237 additions & 4 deletions

cluster_library.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,10 @@ unsigned short cluster_hash_key(const char *key, int len) {
530530
return crc16((char*)key+s+1,e-s-1) & REDIS_CLUSTER_MOD;
531531
}
532532

533+
unsigned short cluster_hash_key_zstr(zend_string *key) {
534+
return cluster_hash_key(ZSTR_VAL(key), ZSTR_LEN(key));
535+
}
536+
533537
/* Grab the current time in milliseconds */
534538
long long mstime(void) {
535539
struct timeval tv;

cluster_library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ void cluster_multi_fini(clusterMultiCmd *mc);
352352
/* Hash a key to it's slot, using the Redis Cluster hash algorithm */
353353
unsigned short cluster_hash_key_zval(zval *key);
354354
unsigned short cluster_hash_key(const char *key, int len);
355+
unsigned short cluster_hash_key_zstr(zend_string *key);
355356

356357
/* Validate and sanitize cluster construction args */
357358
zend_string** cluster_validate_args(double timeout, double read_timeout,

library.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3502,6 +3502,47 @@ redis_key_prefix(RedisSock *redis_sock, char **key, size_t *key_len) {
35023502
return 1;
35033503
}
35043504

3505+
/* This is very similar to PHP >= 7.4 zend_string_concat2 only we are taking
3506+
* two zend_string arguments rather than two char*, size_t pairs */
3507+
static zend_string *redis_zstr_concat(zend_string *prefix, zend_string *suffix) {
3508+
zend_string *res;
3509+
size_t len;
3510+
3511+
ZEND_ASSERT(prefix != NULL && suffix != NULL);
3512+
3513+
len = ZSTR_LEN(prefix) + ZSTR_LEN(suffix);
3514+
res = zend_string_alloc(len, 0);
3515+
3516+
memcpy(ZSTR_VAL(res), ZSTR_VAL(prefix), ZSTR_LEN(prefix));
3517+
memcpy(ZSTR_VAL(res) + ZSTR_LEN(prefix), ZSTR_VAL(suffix), ZSTR_LEN(suffix));
3518+
ZSTR_VAL(res)[len] = '\0';
3519+
3520+
return res;
3521+
}
3522+
3523+
PHP_REDIS_API zend_string *
3524+
redis_key_prefix_zval(RedisSock *redis_sock, zval *zv) {
3525+
zend_string *zstr, *dup;
3526+
3527+
zstr = zval_get_string(zv);
3528+
if (redis_sock->prefix == NULL)
3529+
return zstr;
3530+
3531+
dup = redis_zstr_concat(redis_sock->prefix, zstr);
3532+
3533+
zend_string_release(zstr);
3534+
3535+
return dup;
3536+
}
3537+
3538+
PHP_REDIS_API zend_string *
3539+
redis_key_prefix_zstr(RedisSock *redis_sock, zend_string *key) {
3540+
if (redis_sock->prefix == NULL)
3541+
return zend_string_copy(key);
3542+
3543+
return redis_zstr_concat(redis_sock->prefix, key);
3544+
}
3545+
35053546
/*
35063547
* Processing for variant reply types (think EVAL)
35073548
*/

library.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ PHP_REDIS_API int
123123
redis_serialize(RedisSock *redis_sock, zval *z, char **val, size_t *val_len);
124124
PHP_REDIS_API int
125125
redis_key_prefix(RedisSock *redis_sock, char **key, size_t *key_len);
126+
PHP_REDIS_API zend_string*
127+
redis_key_prefix_zval(RedisSock *redis_sock, zval *zv);
128+
PHP_REDIS_API zend_string *
129+
redis_key_prefix_zstr(RedisSock *redis_sock, zend_string *key);
126130

127131
PHP_REDIS_API int
128132
redis_unserialize(RedisSock *redis_sock, const char *val, int val_len, zval *z_ret);

redis.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,10 @@ PHP_METHOD(Redis, sInter) {
14351435
}
14361436
/* }}} */
14371437

1438+
PHP_METHOD(Redis, sintercard) {
1439+
REDIS_PROCESS_KW_CMD("SINTERCARD", redis_intercard_cmd, redis_long_response);
1440+
}
1441+
14381442
/* {{{ proto array Redis::sInterStore(string dst, string key0,...string keyN) */
14391443
PHP_METHOD(Redis, sInterStore) {
14401444
REDIS_PROCESS_CMD(sinterstore, redis_long_response);
@@ -2071,6 +2075,10 @@ PHP_METHOD(Redis, zinter) {
20712075
}
20722076
/* }}} */
20732077

2078+
PHP_METHOD(Redis, zintercard) {
2079+
REDIS_PROCESS_KW_CMD("ZINTERCARD", redis_intercard_cmd, redis_long_response);
2080+
}
2081+
20742082
/* {{{ proto array Redis::zunion(array keys, array|null weights, array options) */
20752083
PHP_METHOD(Redis, zunion) {
20762084
REDIS_PROCESS_KW_CMD("ZUNION", redis_zinterunion_cmd, redis_zdiff_response);

redis.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ public function sDiffStore(string $dst, string $key, string ...$other_keys): int
363363

364364
public function sInter(string $key, string ...$other_keys): array;
365365

366+
public function sintercard(array $keys, int $limit = -1): Redis|int|false;
367+
366368
public function sInterStore(string $dst, string $key, string ...$other_keys): int;
367369

368370
public function sMembers(string $key): array;
@@ -548,6 +550,8 @@ public function zdiffstore(string $dst, array $keys, array $options = null): int
548550

549551
public function zinter(array $keys, array $weights = null, array $options = null): array;
550552

553+
public function zintercard(array $keys, int $limit = -1): Redis|int|false;
554+
551555
public function zinterstore(string $dst, array $keys, array $weights = null, string $aggregate = null): int;
552556

553557
public function zscan(string $key, ?int &$iterator, ?string $pattern = null, int $count = 0): bool|array;

redis_arginfo.h

Lines changed: 12 additions & 1 deletion
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: 122b5ca534302a09a4f072106d097f2831ba6f22 */
2+
* Stub hash: f43a528bc5874c419161b5eb874537fbe9c00e87 */
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")
@@ -628,6 +628,11 @@ ZEND_END_ARG_INFO()
628628

629629
#define arginfo_class_Redis_sInter arginfo_class_Redis_sDiff
630630

631+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_sintercard, 0, 1, Redis, MAY_BE_LONG|MAY_BE_FALSE)
632+
ZEND_ARG_TYPE_INFO(0, keys, IS_ARRAY, 0)
633+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, limit, IS_LONG, 0, "-1")
634+
ZEND_END_ARG_INFO()
635+
631636
#define arginfo_class_Redis_sInterStore arginfo_class_Redis_sDiffStore
632637

633638
#define arginfo_class_Redis_sMembers arginfo_class_Redis_hGetAll
@@ -971,6 +976,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_zinter, 0, 1, IS_ARR
971976
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")
972977
ZEND_END_ARG_INFO()
973978

979+
#define arginfo_class_Redis_zintercard arginfo_class_Redis_sintercard
980+
974981
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_zinterstore, 0, 2, IS_LONG, 0)
975982
ZEND_ARG_TYPE_INFO(0, dst, IS_STRING, 0)
976983
ZEND_ARG_TYPE_INFO(0, keys, IS_ARRAY, 0)
@@ -1130,6 +1137,7 @@ ZEND_METHOD(Redis, sAddArray);
11301137
ZEND_METHOD(Redis, sDiff);
11311138
ZEND_METHOD(Redis, sDiffStore);
11321139
ZEND_METHOD(Redis, sInter);
1140+
ZEND_METHOD(Redis, sintercard);
11331141
ZEND_METHOD(Redis, sInterStore);
11341142
ZEND_METHOD(Redis, sMembers);
11351143
ZEND_METHOD(Redis, sMisMember);
@@ -1209,6 +1217,7 @@ ZEND_METHOD(Redis, zScore);
12091217
ZEND_METHOD(Redis, zdiff);
12101218
ZEND_METHOD(Redis, zdiffstore);
12111219
ZEND_METHOD(Redis, zinter);
1220+
ZEND_METHOD(Redis, zintercard);
12121221
ZEND_METHOD(Redis, zinterstore);
12131222
ZEND_METHOD(Redis, zscan);
12141223
ZEND_METHOD(Redis, zunion);
@@ -1364,6 +1373,7 @@ static const zend_function_entry class_Redis_methods[] = {
13641373
ZEND_ME(Redis, sDiff, arginfo_class_Redis_sDiff, ZEND_ACC_PUBLIC)
13651374
ZEND_ME(Redis, sDiffStore, arginfo_class_Redis_sDiffStore, ZEND_ACC_PUBLIC)
13661375
ZEND_ME(Redis, sInter, arginfo_class_Redis_sInter, ZEND_ACC_PUBLIC)
1376+
ZEND_ME(Redis, sintercard, arginfo_class_Redis_sintercard, ZEND_ACC_PUBLIC)
13671377
ZEND_ME(Redis, sInterStore, arginfo_class_Redis_sInterStore, ZEND_ACC_PUBLIC)
13681378
ZEND_ME(Redis, sMembers, arginfo_class_Redis_sMembers, ZEND_ACC_PUBLIC)
13691379
ZEND_ME(Redis, sMisMember, arginfo_class_Redis_sMisMember, ZEND_ACC_PUBLIC)
@@ -1443,6 +1453,7 @@ static const zend_function_entry class_Redis_methods[] = {
14431453
ZEND_ME(Redis, zdiff, arginfo_class_Redis_zdiff, ZEND_ACC_PUBLIC)
14441454
ZEND_ME(Redis, zdiffstore, arginfo_class_Redis_zdiffstore, ZEND_ACC_PUBLIC)
14451455
ZEND_ME(Redis, zinter, arginfo_class_Redis_zinter, ZEND_ACC_PUBLIC)
1456+
ZEND_ME(Redis, zintercard, arginfo_class_Redis_zintercard, ZEND_ACC_PUBLIC)
14461457
ZEND_ME(Redis, zinterstore, arginfo_class_Redis_zinterstore, ZEND_ACC_PUBLIC)
14471458
ZEND_ME(Redis, zscan, arginfo_class_Redis_zscan, ZEND_ACC_PUBLIC)
14481459
ZEND_ME(Redis, zunion, arginfo_class_Redis_zunion, ZEND_ACC_PUBLIC)

redis_cluster.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,13 @@ PHP_METHOD(RedisCluster, sunionstore) {
10211021
PHP_METHOD(RedisCluster, sinter) {
10221022
CLUSTER_PROCESS_CMD(sinter, cluster_mbulk_resp, 0);
10231023
}
1024+
1025+
/* {{{ proto RedisCluster::sintercard(array $keys, int $count = -1) */
1026+
PHP_METHOD(RedisCluster, sintercard) {
1027+
CLUSTER_PROCESS_KW_CMD("SINTERCARD", redis_intercard_cmd, cluster_long_resp, 0);
1028+
}
1029+
/* }}} */
1030+
10241031
/* }}} */
10251032

10261033
/* {{{ ptoto long RedisCluster::sinterstore(string dst, string k1, ... kN) */
@@ -1457,6 +1464,12 @@ PHP_METHOD(RedisCluster, zinterstore) {
14571464
}
14581465
/* }}} */
14591466

1467+
/* {{{ proto RedisCluster::zintercard(array $keys, int $count = -1) */
1468+
PHP_METHOD(RedisCluster, zintercard) {
1469+
CLUSTER_PROCESS_KW_CMD("ZINTERCARD", redis_intercard_cmd, cluster_long_resp, 0);
1470+
}
1471+
/* }}} */
1472+
14601473
/* {{{ proto RedisCluster::zrem(string key, string val1, ... valN) */
14611474
PHP_METHOD(RedisCluster, zrem) {
14621475
CLUSTER_PROCESS_KW_CMD("ZREM", redis_key_varval_cmd, cluster_long_resp, 0);

redis_cluster.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ public function setrange(string $key, int $offset, string $value): int;
276276

277277
public function sinter(string $key, string ...$other_keys): array;
278278

279+
public function sintercard(array $keys, int $limit = -1): Redis|int|false;
280+
279281
public function sinterstore(string $dst, string $key, string ...$other_keys): bool;
280282

281283
public function sismember(string $key): int;
@@ -354,6 +356,8 @@ public function zincrby(string $key, float $value, string $member): float;
354356

355357
public function zinterstore(string $key, array $keys, array $weights = null, string $aggregate = null): int;
356358

359+
public function zintercard(array $keys, int $limit = -1): Redis|int|false;
360+
357361
public function zlexcount(string $key, string $min, string $max): int;
358362

359363
public function zpopmax(string $key, int $value = null): bool|array;

redis_cluster_arginfo.h

Lines changed: 12 additions & 1 deletion
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: 97fe79bf371074b77d22e1e820903fb2103d10c9 */
2+
* Stub hash: c9bfd7de5c930c9d7190139e207eb6025e33bdb2 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 1)
@@ -584,6 +584,11 @@ ZEND_END_ARG_INFO()
584584

585585
#define arginfo_class_RedisCluster_sinter arginfo_class_RedisCluster_del
586586

587+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_sintercard, 0, 1, Redis, MAY_BE_LONG|MAY_BE_FALSE)
588+
ZEND_ARG_TYPE_INFO(0, keys, IS_ARRAY, 0)
589+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, limit, IS_LONG, 0, "-1")
590+
ZEND_END_ARG_INFO()
591+
587592
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisCluster_sinterstore, 0, 2, _IS_BOOL, 0)
588593
ZEND_ARG_TYPE_INFO(0, dst, IS_STRING, 0)
589594
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
@@ -775,6 +780,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisCluster_zinterstore,
775780
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, aggregate, IS_STRING, 0, "null")
776781
ZEND_END_ARG_INFO()
777782

783+
#define arginfo_class_RedisCluster_zintercard arginfo_class_RedisCluster_sintercard
784+
778785
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisCluster_zlexcount, 0, 3, IS_LONG, 0)
779786
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
780787
ZEND_ARG_TYPE_INFO(0, min, IS_STRING, 0)
@@ -973,6 +980,7 @@ ZEND_METHOD(RedisCluster, setnx);
973980
ZEND_METHOD(RedisCluster, setoption);
974981
ZEND_METHOD(RedisCluster, setrange);
975982
ZEND_METHOD(RedisCluster, sinter);
983+
ZEND_METHOD(RedisCluster, sintercard);
976984
ZEND_METHOD(RedisCluster, sinterstore);
977985
ZEND_METHOD(RedisCluster, sismember);
978986
ZEND_METHOD(RedisCluster, slowlog);
@@ -1012,6 +1020,7 @@ ZEND_METHOD(RedisCluster, zcard);
10121020
ZEND_METHOD(RedisCluster, zcount);
10131021
ZEND_METHOD(RedisCluster, zincrby);
10141022
ZEND_METHOD(RedisCluster, zinterstore);
1023+
ZEND_METHOD(RedisCluster, zintercard);
10151024
ZEND_METHOD(RedisCluster, zlexcount);
10161025
ZEND_METHOD(RedisCluster, zpopmax);
10171026
ZEND_METHOD(RedisCluster, zpopmin);
@@ -1167,6 +1176,7 @@ static const zend_function_entry class_RedisCluster_methods[] = {
11671176
ZEND_ME(RedisCluster, setoption, arginfo_class_RedisCluster_setoption, ZEND_ACC_PUBLIC)
11681177
ZEND_ME(RedisCluster, setrange, arginfo_class_RedisCluster_setrange, ZEND_ACC_PUBLIC)
11691178
ZEND_ME(RedisCluster, sinter, arginfo_class_RedisCluster_sinter, ZEND_ACC_PUBLIC)
1179+
ZEND_ME(RedisCluster, sintercard, arginfo_class_RedisCluster_sintercard, ZEND_ACC_PUBLIC)
11701180
ZEND_ME(RedisCluster, sinterstore, arginfo_class_RedisCluster_sinterstore, ZEND_ACC_PUBLIC)
11711181
ZEND_ME(RedisCluster, sismember, arginfo_class_RedisCluster_sismember, ZEND_ACC_PUBLIC)
11721182
ZEND_ME(RedisCluster, slowlog, arginfo_class_RedisCluster_slowlog, ZEND_ACC_PUBLIC)
@@ -1206,6 +1216,7 @@ static const zend_function_entry class_RedisCluster_methods[] = {
12061216
ZEND_ME(RedisCluster, zcount, arginfo_class_RedisCluster_zcount, ZEND_ACC_PUBLIC)
12071217
ZEND_ME(RedisCluster, zincrby, arginfo_class_RedisCluster_zincrby, ZEND_ACC_PUBLIC)
12081218
ZEND_ME(RedisCluster, zinterstore, arginfo_class_RedisCluster_zinterstore, ZEND_ACC_PUBLIC)
1219+
ZEND_ME(RedisCluster, zintercard, arginfo_class_RedisCluster_zintercard, ZEND_ACC_PUBLIC)
12091220
ZEND_ME(RedisCluster, zlexcount, arginfo_class_RedisCluster_zlexcount, ZEND_ACC_PUBLIC)
12101221
ZEND_ME(RedisCluster, zpopmax, arginfo_class_RedisCluster_zpopmax, ZEND_ACC_PUBLIC)
12111222
ZEND_ME(RedisCluster, zpopmin, arginfo_class_RedisCluster_zpopmin, ZEND_ACC_PUBLIC)

0 commit comments

Comments
 (0)