FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Implement `VCARD`, `VDIM`, and `VINFO` · phpredis/phpredis@0fda9f2 · GitHub

Commit 0fda9f2

Browse files
Implement VCARD, VDIM, and VINFO
All of these commands have the same form `<cmd> key`. `VINFO` is a bit of an outlier however that uses simple strings as opposed to bulk strings for the key names, meaning we had to create a custom handler. See #2543
1 parent 0ed0fc0 commit 0fda9f2

12 files changed

Lines changed: 221 additions & 8 deletions

‎cluster_library.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2506,6 +2506,27 @@ cluster_xclaim_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c, void *ctx) {
25062506

25072507
}
25082508

2509+
PHP_REDIS_API void
2510+
cluster_vinfo_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c, void *ctx) {
2511+
zval z_ret;
2512+
2513+
if (c->reply_len < 2 || c->reply_len % 2 != 0) {
2514+
CLUSTER_RETURN_FALSE(c);
2515+
}
2516+
2517+
array_init_size(&z_ret, c->reply_len / 2);
2518+
if (redis_read_vinfo_response(c->cmd_sock, &z_ret, c->reply_len) != SUCCESS) {
2519+
zval_dtor(&z_ret);
2520+
CLUSTER_RETURN_FALSE(c);
2521+
}
2522+
2523+
if (CLUSTER_IS_ATOMIC(c)) {
2524+
RETURN_ZVAL(&z_ret, 0, 1);
2525+
}
2526+
2527+
add_next_index_zval(&c->multi_resp, &z_ret);
2528+
}
2529+
25092530
/* XINFO */
25102531
PHP_REDIS_API void
25112532
cluster_xinfo_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c, void *ctx)

‎cluster_library.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ PHP_REDIS_API void cluster_sub_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *
448448
void *ctx);
449449
PHP_REDIS_API void cluster_unsub_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c,
450450
void *ctx);
451+
PHP_REDIS_API void cluster_vinfo_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c,
452+
void *ctx);
451453

452454
PHP_REDIS_API void cluster_zrange_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c,
453455
void *ctx);

‎library.c‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,6 +2484,80 @@ redis_read_xinfo_response(RedisSock *redis_sock, zval *z_ret, int elements)
24842484
return FAILURE;
24852485
}
24862486

2487+
PHP_REDIS_API int
2488+
redis_read_vinfo_response(RedisSock *redis_sock, zval *z_ret, long long count) {
2489+
char kbuf[256], vbuf[256];
2490+
REDIS_REPLY_TYPE type;
2491+
size_t klen, vlen;
2492+
long lval;
2493+
2494+
if (count < 0 || count % 2 != 0) {
2495+
zend_error_noreturn(E_ERROR, "Internal finfo handler error");
2496+
}
2497+
2498+
for (long long i = 0; i < count; i += 2) {
2499+
if (redis_read_reply_type(redis_sock, &type, &lval) < 0 ||
2500+
type != TYPE_LINE)
2501+
{
2502+
return FAILURE;
2503+
}
2504+
2505+
if (redis_sock_gets(redis_sock, kbuf, sizeof(kbuf), &klen) < 0) {
2506+
return FAILURE;
2507+
}
2508+
2509+
if (redis_read_reply_type(redis_sock, &type, &lval) < 0) {
2510+
return FAILURE;
2511+
}
2512+
2513+
switch (type) {
2514+
case TYPE_LINE:
2515+
if (redis_sock_gets(redis_sock, vbuf, sizeof(vbuf), &vlen) < 0) {
2516+
return FAILURE;
2517+
}
2518+
add_assoc_stringl_ex(z_ret, kbuf, klen, vbuf, vlen);
2519+
break;
2520+
case TYPE_INT:
2521+
add_assoc_long_ex(z_ret, kbuf, klen, lval);
2522+
break;
2523+
default:
2524+
add_assoc_null_ex(z_ret, kbuf, klen);
2525+
break;
2526+
2527+
}
2528+
}
2529+
2530+
return SUCCESS;
2531+
}
2532+
2533+
2534+
PHP_REDIS_API int
2535+
redis_vinfo_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
2536+
zval *z_tab, void *ctx)
2537+
{
2538+
zval z_ret;
2539+
int count;
2540+
2541+
if (read_mbulk_header(redis_sock, &count) < 0 ||
2542+
count < 0 || count % 2 != 0)
2543+
{
2544+
REDIS_RESPONSE_ERROR(redis_sock, z_tab);
2545+
return FAILURE;
2546+
}
2547+
2548+
array_init_size(&z_ret, count / 2);
2549+
2550+
if (redis_read_vinfo_response(redis_sock, &z_ret, count) != SUCCESS) {
2551+
zval_dtor(&z_ret);
2552+
REDIS_RESPONSE_ERROR(redis_sock, z_tab);
2553+
return FAILURE;
2554+
}
2555+
2556+
REDIS_RETURN_ZVAL(redis_sock, z_tab, z_ret);
2557+
2558+
return SUCCESS;
2559+
}
2560+
24872561
PHP_REDIS_API int
24882562
redis_xinfo_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx)
24892563
{

‎library.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ PHP_REDIS_API void redis_sock_set_auth_zval(RedisSock *redis_sock, zval *zv);
9292
PHP_REDIS_API void redis_sock_free_auth(RedisSock *redis_sock);
9393
PHP_REDIS_API int redis_sock_disconnect(RedisSock *redis_sock, int force, int is_reset_mode);
9494
PHP_REDIS_API zval *redis_sock_read_multibulk_reply_zval(RedisSock *redis_sock, zval *z_tab);
95-
PHP_REDIS_API int redis_sock_read_single_line(RedisSock *redis_sock, char *buffer,
96-
size_t buflen, size_t *linelen, int set_err);
9795
PHP_REDIS_API char *redis_sock_read_bulk_reply(RedisSock *redis_sock, int bytes);
9896
PHP_REDIS_API int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *_z_tab, void *ctx);
9997
PHP_REDIS_API void redis_mbulk_reply_loop(RedisSock *redis_sock, zval *z_tab, int count, int unserialize);
@@ -125,6 +123,11 @@ PHP_REDIS_API int redis_read_xclaim_reply(
125123
PHP_REDIS_API int redis_xinfo_reply(INTERNAL_FUNCTION_PARAMETERS,
126124
RedisSock *redis_sock, zval *z_tab, void *ctx);
127125

126+
PHP_REDIS_API int redis_vinfo_reply(INTERNAL_FUNCTION_PARAMETERS,
127+
RedisSock *redis_sock, zval *z_tab, void *ctx);
128+
PHP_REDIS_API int redis_read_vinfo_response(RedisSock *redis_sock,
129+
zval *z_ret, long long count);
130+
128131
PHP_REDIS_API int redis_pubsub_response(INTERNAL_FUNCTION_PARAMETERS,
129132
RedisSock *redis_sock, zval *z_tab, void *ctx);
130133
PHP_REDIS_API int redis_subscribe_response(INTERNAL_FUNCTION_PARAMETERS,

‎redis.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3183,6 +3183,18 @@ PHP_METHOD(Redis, vsim) {
31833183
REDIS_PROCESS_CMD(vsim, redis_zrange_response);
31843184
}
31853185

3186+
PHP_METHOD(Redis, vcard) {
3187+
REDIS_PROCESS_KW_CMD("VCARD", redis_key_cmd, redis_long_response);
3188+
}
3189+
3190+
PHP_METHOD(Redis, vdim) {
3191+
REDIS_PROCESS_KW_CMD("VDIM", redis_key_cmd, redis_long_response);
3192+
}
3193+
3194+
PHP_METHOD(Redis, vinfo) {
3195+
REDIS_PROCESS_KW_CMD("VINFO", redis_key_cmd, redis_vinfo_reply);
3196+
}
3197+
31863198
/*
31873199
* Streams
31883200
*/

‎redis.stub.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4243,6 +4243,33 @@ public function vadd(string $key, array $values, mixed $element, array|null $opt
42434243
*/
42444244
public function vsim(string $key, mixed $member, array|null $options = null): Redis|array|false;
42454245

4246+
/**
4247+
* Get the length of a vector set
4248+
*
4249+
* @param string $key The vector set to query.
4250+
*
4251+
* @return Redis|int|false The number of elements in the vector set or false on failure.
4252+
*/
4253+
public function vcard(string $key): Redis|int|false;
4254+
4255+
/**
4256+
* Get the dimensions of a vector set
4257+
*
4258+
* @param string $key The vector set to query.
4259+
*
4260+
* @return Redis|int|false The number of dimensions in the vector set or false on failure.
4261+
*/
4262+
public function vdim(string $key): Redis|int|false;
4263+
4264+
/**
4265+
* Get various bits of information about a vector set
4266+
*
4267+
* @param string $key The vector set to query.
4268+
*
4269+
* @return Redis|array|false An array of information about the vector set or false on failure.
4270+
*/
4271+
public function vinfo(string $key): Redis|array|false;
4272+
42464273
/**
42474274
* Truncate a STREAM key in various ways.
42484275
*

‎redis_arginfo.h‎

Lines changed: 13 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: e1643951a097b7a2f1bb3c0b6afd2c29f746a0d6 */
2+
* Stub hash: ef50011ff095df387f4b0f043d381e092253c8e8 */
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, 1, "null")
@@ -1076,6 +1076,12 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_vsim, 0, 2, Redi
10761076
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 1, "null")
10771077
ZEND_END_ARG_INFO()
10781078

1079+
#define arginfo_class_Redis_vcard arginfo_class_Redis_expiretime
1080+
1081+
#define arginfo_class_Redis_vdim arginfo_class_Redis_expiretime
1082+
1083+
#define arginfo_class_Redis_vinfo arginfo_class_Redis_getWithMeta
1084+
10791085
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_xtrim, 0, 2, Redis, MAY_BE_LONG|MAY_BE_FALSE)
10801086
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
10811087
ZEND_ARG_TYPE_INFO(0, threshold, IS_STRING, 0)
@@ -1489,6 +1495,9 @@ ZEND_METHOD(Redis, xreadgroup);
14891495
ZEND_METHOD(Redis, xrevrange);
14901496
ZEND_METHOD(Redis, vadd);
14911497
ZEND_METHOD(Redis, vsim);
1498+
ZEND_METHOD(Redis, vcard);
1499+
ZEND_METHOD(Redis, vdim);
1500+
ZEND_METHOD(Redis, vinfo);
14921501
ZEND_METHOD(Redis, xtrim);
14931502
ZEND_METHOD(Redis, zAdd);
14941503
ZEND_METHOD(Redis, zCard);
@@ -1767,6 +1776,9 @@ static const zend_function_entry class_Redis_methods[] = {
17671776
ZEND_ME(Redis, xrevrange, arginfo_class_Redis_xrevrange, ZEND_ACC_PUBLIC)
17681777
ZEND_ME(Redis, vadd, arginfo_class_Redis_vadd, ZEND_ACC_PUBLIC)
17691778
ZEND_ME(Redis, vsim, arginfo_class_Redis_vsim, ZEND_ACC_PUBLIC)
1779+
ZEND_ME(Redis, vcard, arginfo_class_Redis_vcard, ZEND_ACC_PUBLIC)
1780+
ZEND_ME(Redis, vdim, arginfo_class_Redis_vdim, ZEND_ACC_PUBLIC)
1781+
ZEND_ME(Redis, vinfo, arginfo_class_Redis_vinfo, ZEND_ACC_PUBLIC)
17701782
ZEND_ME(Redis, xtrim, arginfo_class_Redis_xtrim, ZEND_ACC_PUBLIC)
17711783
ZEND_ME(Redis, zAdd, arginfo_class_Redis_zAdd, ZEND_ACC_PUBLIC)
17721784
ZEND_ME(Redis, zCard, arginfo_class_Redis_zCard, ZEND_ACC_PUBLIC)

‎redis_cluster.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3187,6 +3187,18 @@ PHP_METHOD(RedisCluster, vsim) {
31873187
CLUSTER_PROCESS_CMD(vsim, cluster_zrange_resp, 1);
31883188
}
31893189

3190+
PHP_METHOD(RedisCluster, vcard) {
3191+
CLUSTER_PROCESS_KW_CMD("VCARD", redis_key_cmd, cluster_long_resp, 1);
3192+
}
3193+
3194+
PHP_METHOD(RedisCluster, vdim) {
3195+
CLUSTER_PROCESS_KW_CMD("VDIM", redis_key_cmd, cluster_long_resp, 1);
3196+
}
3197+
3198+
PHP_METHOD(RedisCluster, vinfo) {
3199+
CLUSTER_PROCESS_KW_CMD("VINFO", redis_key_cmd, cluster_vinfo_resp, 1);
3200+
}
3201+
31903202
/* {{{ proto long RedisCluster::xack(string key, string group, array ids) }}} */
31913203
PHP_METHOD(RedisCluster, xack) {
31923204
CLUSTER_PROCESS_CMD(xack, cluster_long_resp, 0);

‎redis_cluster.stub.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,22 @@ public function vadd(string $key, array $values, mixed $element, array|null $opt
10711071
/**
10721072
* @see Redis::vsim
10731073
*/
1074-
public function vsim(string $key, mixed $member, array|null $options = null): Redis|array|false;
1074+
public function vsim(string $key, mixed $member, array|null $options = null): RedisCluster|array|false;
10751075

1076+
/**
1077+
* @see Redis::vcard
1078+
*/
1079+
public function vcard(string $key): RedisCluster|int|false;
1080+
1081+
/**
1082+
* @see Redis::vdim
1083+
*/
1084+
public function vdim(string $key): RedisCluster|int|false;
1085+
1086+
/**
1087+
* @see Redis::vinfo
1088+
*/
1089+
public function vinfo(string $key): RedisCluster|array|false;
10761090

10771091
/**
10781092
* @see Redis::xack

‎redis_cluster_arginfo.h‎

Lines changed: 14 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: 0f01437681bc6df1d6e2b39e6a0c7c5c77f6237e */
2+
* Stub hash: 6833953df5e7260f7791abe42c5ae9cd9a0125d2 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 1)
@@ -886,12 +886,18 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_vadd, 0,
886886
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 1, "null")
887887
ZEND_END_ARG_INFO()
888888

889-
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_vsim, 0, 2, Redis, MAY_BE_ARRAY|MAY_BE_FALSE)
889+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_vsim, 0, 2, RedisCluster, MAY_BE_ARRAY|MAY_BE_FALSE)
890890
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
891891
ZEND_ARG_TYPE_INFO(0, member, IS_MIXED, 0)
892892
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 1, "null")
893893
ZEND_END_ARG_INFO()
894894

895+
#define arginfo_class_RedisCluster_vcard arginfo_class_RedisCluster_expiretime
896+
897+
#define arginfo_class_RedisCluster_vdim arginfo_class_RedisCluster_expiretime
898+
899+
#define arginfo_class_RedisCluster_vinfo arginfo_class_RedisCluster_getWithMeta
900+
895901
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_xack, 0, 3, RedisCluster, MAY_BE_LONG|MAY_BE_FALSE)
896902
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
897903
ZEND_ARG_TYPE_INFO(0, group, IS_STRING, 0)
@@ -1329,6 +1335,9 @@ ZEND_METHOD(RedisCluster, unwatch);
13291335
ZEND_METHOD(RedisCluster, watch);
13301336
ZEND_METHOD(RedisCluster, vadd);
13311337
ZEND_METHOD(RedisCluster, vsim);
1338+
ZEND_METHOD(RedisCluster, vcard);
1339+
ZEND_METHOD(RedisCluster, vdim);
1340+
ZEND_METHOD(RedisCluster, vinfo);
13321341
ZEND_METHOD(RedisCluster, xack);
13331342
ZEND_METHOD(RedisCluster, xadd);
13341343
ZEND_METHOD(RedisCluster, xclaim);
@@ -1575,6 +1584,9 @@ static const zend_function_entry class_RedisCluster_methods[] = {
15751584
ZEND_ME(RedisCluster, watch, arginfo_class_RedisCluster_watch, ZEND_ACC_PUBLIC)
15761585
ZEND_ME(RedisCluster, vadd, arginfo_class_RedisCluster_vadd, ZEND_ACC_PUBLIC)
15771586
ZEND_ME(RedisCluster, vsim, arginfo_class_RedisCluster_vsim, ZEND_ACC_PUBLIC)
1587+
ZEND_ME(RedisCluster, vcard, arginfo_class_RedisCluster_vcard, ZEND_ACC_PUBLIC)
1588+
ZEND_ME(RedisCluster, vdim, arginfo_class_RedisCluster_vdim, ZEND_ACC_PUBLIC)
1589+
ZEND_ME(RedisCluster, vinfo, arginfo_class_RedisCluster_vinfo, ZEND_ACC_PUBLIC)
15781590
ZEND_ME(RedisCluster, xack, arginfo_class_RedisCluster_xack, ZEND_ACC_PUBLIC)
15791591
ZEND_ME(RedisCluster, xadd, arginfo_class_RedisCluster_xadd, ZEND_ACC_PUBLIC)
15801592
ZEND_ME(RedisCluster, xclaim, arginfo_class_RedisCluster_xclaim, ZEND_ACC_PUBLIC)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL