Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit96378b7

Browse files
ImplementVEMB and slightly rework VINFO
Unfortunately `VEMB` has a unique `RESP2` reply as far as I can tell,where it sends the embedding mode (int8, bin, fp32) as a simple string.This would cause any of PhpRedis' generic reply handlers to turn thatinto `true` which isn't useful. For that reason we need a custom replyhandler.Additionally slightly rework `VINFO` to short circuit and return failureif we read anything other than a bulk string or an integer reply type.Otherwise we may get out of sync on the socket.See#2543
1 parent8f8a49b commit96378b7

13 files changed

+194
-15
lines changed

‎library.c‎

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,21 +2491,18 @@ redis_read_vinfo_response(RedisSock *redis_sock, zval *z_ret, long long count) {
24912491
size_tklen,vlen;
24922492
longlval;
24932493

2494-
if (count<0||count %2!=0) {
2494+
if (count<0||count %2!=0||Z_TYPE_P(z_ret)!=IS_ARRAY) {
24952495
zend_error_noreturn(E_ERROR,"Internal finfo handler error");
24962496
}
24972497

24982498
for (long longi=0;i<count;i+=2) {
24992499
if (redis_read_reply_type(redis_sock,&type,&lval)<0||
2500-
type!=TYPE_LINE)
2500+
type!=TYPE_LINE||
2501+
redis_sock_gets(redis_sock,kbuf,sizeof(kbuf),&klen)<0)
25012502
{
25022503
returnFAILURE;
25032504
}
25042505

2505-
if (redis_sock_gets(redis_sock,kbuf,sizeof(kbuf),&klen)<0) {
2506-
returnFAILURE;
2507-
}
2508-
25092506
if (redis_read_reply_type(redis_sock,&type,&lval)<0) {
25102507
returnFAILURE;
25112508
}
@@ -2521,10 +2518,8 @@ redis_read_vinfo_response(RedisSock *redis_sock, zval *z_ret, long long count) {
25212518
add_assoc_long_ex(z_ret,kbuf,klen,lval);
25222519
break;
25232520
default:
2524-
add_assoc_null_ex(z_ret,kbuf,klen);
2525-
break;
2526-
2527-
}
2521+
returnFAILURE;
2522+
}
25282523
}
25292524

25302525
returnSUCCESS;
@@ -2558,8 +2553,77 @@ redis_vinfo_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
25582553
returnSUCCESS;
25592554
}
25602555

2556+
/* Unfortunately VEMB decided to use +string\r\n for the encoding when RAW is
2557+
* sent which PhpRedis will parse as `(true)` so we need a specific handler for
2558+
* it */
25612559
PHP_REDIS_APIint
2562-
redis_xinfo_reply(INTERNAL_FUNCTION_PARAMETERS,RedisSock*redis_sock,zval*z_tab,void*ctx)
2560+
redis_read_vemb_response(RedisSock*redis_sock,zval*z_ret,long longcount) {
2561+
REDIS_REPLY_TYPEtype;
2562+
charkbuf[256],*str;
2563+
size_tklen;
2564+
doubledval;
2565+
longtlen;
2566+
2567+
if (count<0||Z_TYPE_P(z_ret)!=IS_ARRAY) {
2568+
zend_error_noreturn(E_ERROR,"Internal vemb handler error");
2569+
}
2570+
2571+
for (long longi=0;i<count;i++) {
2572+
if (redis_read_reply_type(redis_sock,&type,&tlen)<0) {
2573+
returnFAILURE;
2574+
}
2575+
2576+
if (type==TYPE_LINE) {
2577+
if (redis_sock_gets(redis_sock,kbuf,sizeof(kbuf),&klen)<0)
2578+
returnFAILURE;
2579+
add_next_index_stringl(z_ret,kbuf,klen);
2580+
}elseif (type==TYPE_BULK) {
2581+
if ((str=redis_sock_read_bulk_reply(redis_sock,tlen))==NULL)
2582+
returnFAILURE;
2583+
2584+
if (is_numeric_string(str,tlen,NULL,&dval,0)==IS_DOUBLE) {
2585+
add_next_index_double(z_ret,dval);
2586+
}else {
2587+
add_next_index_stringl(z_ret,str,tlen);
2588+
}
2589+
2590+
efree(str);
2591+
}else {
2592+
returnFAILURE;
2593+
}
2594+
}
2595+
2596+
returnSUCCESS;
2597+
}
2598+
2599+
PHP_REDIS_APIint
2600+
redis_vemb_reply(INTERNAL_FUNCTION_PARAMETERS,RedisSock*redis_sock,
2601+
zval*z_tab,void*ctx)
2602+
{
2603+
zvalz_ret;
2604+
intcount;
2605+
2606+
if (read_mbulk_header(redis_sock,&count)<0||count<1) {
2607+
REDIS_RESPONSE_ERROR(redis_sock,z_tab);
2608+
returnFAILURE;
2609+
}
2610+
2611+
array_init_size(&z_ret,count);
2612+
2613+
if (redis_read_vemb_response(redis_sock,&z_ret,count)!=SUCCESS) {
2614+
zval_dtor(&z_ret);
2615+
REDIS_RESPONSE_ERROR(redis_sock,z_tab);
2616+
returnFAILURE;
2617+
}
2618+
2619+
REDIS_RETURN_ZVAL(redis_sock,z_tab,z_ret);
2620+
2621+
returnSUCCESS;
2622+
}
2623+
2624+
PHP_REDIS_APIint
2625+
redis_xinfo_reply(INTERNAL_FUNCTION_PARAMETERS,RedisSock*redis_sock,
2626+
zval*z_tab,void*ctx)
25632627
{
25642628
zvalz_ret;
25652629
intelements;

‎library.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ PHP_REDIS_API int redis_vinfo_reply(INTERNAL_FUNCTION_PARAMETERS,
128128
PHP_REDIS_APIintredis_read_vinfo_response(RedisSock*redis_sock,
129129
zval*z_ret,long longcount);
130130

131+
PHP_REDIS_APIintredis_vemb_reply(INTERNAL_FUNCTION_PARAMETERS,
132+
RedisSock*redis_sock,zval*z_tab,void*ctx);
133+
PHP_REDIS_APIintredis_read_vemb_response(RedisSock*redis_sock,zval*z_ret,
134+
long longcount);
135+
131136
PHP_REDIS_APIintredis_pubsub_response(INTERNAL_FUNCTION_PARAMETERS,
132137
RedisSock*redis_sock,zval*z_tab,void*ctx);
133138
PHP_REDIS_APIintredis_subscribe_response(INTERNAL_FUNCTION_PARAMETERS,

‎redis.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3195,6 +3195,10 @@ PHP_METHOD(Redis, vinfo) {
31953195
REDIS_PROCESS_KW_CMD("VINFO",redis_key_cmd,redis_vinfo_reply);
31963196
}
31973197

3198+
PHP_METHOD(Redis,vemb) {
3199+
REDIS_PROCESS_CMD(vemb,redis_vemb_reply);
3200+
}
3201+
31983202
/*
31993203
* Streams
32003204
*/

‎redis.stub.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4270,6 +4270,17 @@ public function vdim(string $key): Redis|int|false;
42704270
*/
42714271
publicfunctionvinfo(string$key):Redis|array|false;
42724272

4273+
/**
4274+
* Get the embeddings for a specific member
4275+
*
4276+
* @param string $key The vector set to query.
4277+
* @param mixed $member The member to query.
4278+
* @param bool $raw If set to `true`, the raw embeddings will be returned
4279+
*
4280+
* @return Redis|array|false An array of embeddings for the member or false on failure.
4281+
*/
4282+
publicfunctionvemb(string$key,mixed$member,bool$raw =false):Redis|array|false;
4283+
42734284
/**
42744285
* Truncate a STREAM key in various ways.
42754286
*

‎redis_arginfo.h‎

Lines changed: 9 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:ef50011ff095df387f4b0f043d381e092253c8e8 */
2+
* Stub hash:8cbcd94f8610fdfd08566cdf137ce63dbcfbf609 */
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")
@@ -1082,6 +1082,12 @@ ZEND_END_ARG_INFO()
10821082

10831083
#definearginfo_class_Redis_vinfo arginfo_class_Redis_getWithMeta
10841084

1085+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_vemb,0,2,Redis,MAY_BE_ARRAY|MAY_BE_FALSE)
1086+
ZEND_ARG_TYPE_INFO(0,key,IS_STRING,0)
1087+
ZEND_ARG_TYPE_INFO(0,member,IS_MIXED,0)
1088+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0,raw,_IS_BOOL,0,"false")
1089+
ZEND_END_ARG_INFO()
1090+
10851091
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_xtrim,0,2,Redis,MAY_BE_LONG|MAY_BE_FALSE)
10861092
ZEND_ARG_TYPE_INFO(0,key,IS_STRING,0)
10871093
ZEND_ARG_TYPE_INFO(0,threshold,IS_STRING,0)
@@ -1498,6 +1504,7 @@ ZEND_METHOD(Redis, vsim);
14981504
ZEND_METHOD(Redis,vcard);
14991505
ZEND_METHOD(Redis,vdim);
15001506
ZEND_METHOD(Redis,vinfo);
1507+
ZEND_METHOD(Redis,vemb);
15011508
ZEND_METHOD(Redis,xtrim);
15021509
ZEND_METHOD(Redis,zAdd);
15031510
ZEND_METHOD(Redis,zCard);
@@ -1779,6 +1786,7 @@ static const zend_function_entry class_Redis_methods[] = {
17791786
ZEND_ME(Redis,vcard,arginfo_class_Redis_vcard,ZEND_ACC_PUBLIC)
17801787
ZEND_ME(Redis,vdim,arginfo_class_Redis_vdim,ZEND_ACC_PUBLIC)
17811788
ZEND_ME(Redis,vinfo,arginfo_class_Redis_vinfo,ZEND_ACC_PUBLIC)
1789+
ZEND_ME(Redis,vemb,arginfo_class_Redis_vemb,ZEND_ACC_PUBLIC)
17821790
ZEND_ME(Redis,xtrim,arginfo_class_Redis_xtrim,ZEND_ACC_PUBLIC)
17831791
ZEND_ME(Redis,zAdd,arginfo_class_Redis_zAdd,ZEND_ACC_PUBLIC)
17841792
ZEND_ME(Redis,zCard,arginfo_class_Redis_zCard,ZEND_ACC_PUBLIC)

‎redis_cluster.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3199,6 +3199,10 @@ PHP_METHOD(RedisCluster, vinfo) {
31993199
CLUSTER_PROCESS_KW_CMD("VINFO",redis_key_cmd,cluster_vinfo_resp,1);
32003200
}
32013201

3202+
PHP_METHOD(RedisCluster,vemb) {
3203+
CLUSTER_PROCESS_CMD(vemb,cluster_variant_resp,1);
3204+
}
3205+
32023206
/* {{{ proto long RedisCluster::xack(string key, string group, array ids) }}} */
32033207
PHP_METHOD(RedisCluster,xack) {
32043208
CLUSTER_PROCESS_CMD(xack,cluster_long_resp,0);

‎redis_cluster.stub.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,11 @@ public function vdim(string $key): RedisCluster|int|false;
10881088
*/
10891089
publicfunctionvinfo(string$key):RedisCluster|array|false;
10901090

1091+
/**
1092+
* @see Redis::vemb
1093+
*/
1094+
publicfunctionvemb(string$key,mixed$member,bool$raw =false):RedisCluster|array|false;
1095+
10911096
/**
10921097
* @see Redis::xack
10931098
*/

‎redis_cluster_arginfo.h‎

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

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct,0,0,1)
55
ZEND_ARG_TYPE_INFO(0,name,IS_STRING,1)
@@ -898,6 +898,12 @@ ZEND_END_ARG_INFO()
898898

899899
#definearginfo_class_RedisCluster_vinfo arginfo_class_RedisCluster_getWithMeta
900900

901+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_vemb,0,2,RedisCluster,MAY_BE_ARRAY|MAY_BE_FALSE)
902+
ZEND_ARG_TYPE_INFO(0,key,IS_STRING,0)
903+
ZEND_ARG_TYPE_INFO(0,member,IS_MIXED,0)
904+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0,raw,_IS_BOOL,0,"false")
905+
ZEND_END_ARG_INFO()
906+
901907
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_xack,0,3,RedisCluster,MAY_BE_LONG|MAY_BE_FALSE)
902908
ZEND_ARG_TYPE_INFO(0,key,IS_STRING,0)
903909
ZEND_ARG_TYPE_INFO(0,group,IS_STRING,0)
@@ -1338,6 +1344,7 @@ ZEND_METHOD(RedisCluster, vsim);
13381344
ZEND_METHOD(RedisCluster,vcard);
13391345
ZEND_METHOD(RedisCluster,vdim);
13401346
ZEND_METHOD(RedisCluster,vinfo);
1347+
ZEND_METHOD(RedisCluster,vemb);
13411348
ZEND_METHOD(RedisCluster,xack);
13421349
ZEND_METHOD(RedisCluster,xadd);
13431350
ZEND_METHOD(RedisCluster,xclaim);
@@ -1587,6 +1594,7 @@ static const zend_function_entry class_RedisCluster_methods[] = {
15871594
ZEND_ME(RedisCluster,vcard,arginfo_class_RedisCluster_vcard,ZEND_ACC_PUBLIC)
15881595
ZEND_ME(RedisCluster,vdim,arginfo_class_RedisCluster_vdim,ZEND_ACC_PUBLIC)
15891596
ZEND_ME(RedisCluster,vinfo,arginfo_class_RedisCluster_vinfo,ZEND_ACC_PUBLIC)
1597+
ZEND_ME(RedisCluster,vemb,arginfo_class_RedisCluster_vemb,ZEND_ACC_PUBLIC)
15901598
ZEND_ME(RedisCluster,xack,arginfo_class_RedisCluster_xack,ZEND_ACC_PUBLIC)
15911599
ZEND_ME(RedisCluster,xadd,arginfo_class_RedisCluster_xadd,ZEND_ACC_PUBLIC)
15921600
ZEND_ME(RedisCluster,xclaim,arginfo_class_RedisCluster_xclaim,ZEND_ACC_PUBLIC)

‎redis_cluster_legacy_arginfo.h‎

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

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct,0,0,1)
55
ZEND_ARG_INFO(0,name)
@@ -761,6 +761,12 @@ ZEND_END_ARG_INFO()
761761

762762
#definearginfo_class_RedisCluster_vinfo arginfo_class_RedisCluster__prefix
763763

764+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_vemb,0,0,2)
765+
ZEND_ARG_INFO(0,key)
766+
ZEND_ARG_INFO(0,member)
767+
ZEND_ARG_INFO(0,raw)
768+
ZEND_END_ARG_INFO()
769+
764770
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_xack,0,0,3)
765771
ZEND_ARG_INFO(0,key)
766772
ZEND_ARG_INFO(0,group)
@@ -1173,6 +1179,7 @@ ZEND_METHOD(RedisCluster, vsim);
11731179
ZEND_METHOD(RedisCluster,vcard);
11741180
ZEND_METHOD(RedisCluster,vdim);
11751181
ZEND_METHOD(RedisCluster,vinfo);
1182+
ZEND_METHOD(RedisCluster,vemb);
11761183
ZEND_METHOD(RedisCluster,xack);
11771184
ZEND_METHOD(RedisCluster,xadd);
11781185
ZEND_METHOD(RedisCluster,xclaim);
@@ -1422,6 +1429,7 @@ static const zend_function_entry class_RedisCluster_methods[] = {
14221429
ZEND_ME(RedisCluster,vcard,arginfo_class_RedisCluster_vcard,ZEND_ACC_PUBLIC)
14231430
ZEND_ME(RedisCluster,vdim,arginfo_class_RedisCluster_vdim,ZEND_ACC_PUBLIC)
14241431
ZEND_ME(RedisCluster,vinfo,arginfo_class_RedisCluster_vinfo,ZEND_ACC_PUBLIC)
1432+
ZEND_ME(RedisCluster,vemb,arginfo_class_RedisCluster_vemb,ZEND_ACC_PUBLIC)
14251433
ZEND_ME(RedisCluster,xack,arginfo_class_RedisCluster_xack,ZEND_ACC_PUBLIC)
14261434
ZEND_ME(RedisCluster,xadd,arginfo_class_RedisCluster_xadd,ZEND_ACC_PUBLIC)
14271435
ZEND_ME(RedisCluster,xclaim,arginfo_class_RedisCluster_xclaim,ZEND_ACC_PUBLIC)

‎redis_commands.c‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7007,6 +7007,38 @@ redis_vsim_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
70077007

70087008
returnSUCCESS;
70097009
}
7010+
7011+
int
7012+
redis_vemb_cmd(INTERNAL_FUNCTION_PARAMETERS,RedisSock*redis_sock,
7013+
char**cmd,int*cmd_len,short*slot,void**ctx)
7014+
{
7015+
smart_stringcmdstr= {0};
7016+
zend_boolraw=0;
7017+
zend_string*key;
7018+
zval*member;
7019+
7020+
ZEND_PARSE_PARAMETERS_START(2,3) {
7021+
Z_PARAM_STR(key)
7022+
Z_PARAM_ZVAL(member);
7023+
Z_PARAM_OPTIONAL
7024+
Z_PARAM_BOOL(raw)
7025+
}ZEND_PARSE_PARAMETERS_END_EX(returnFAILURE);
7026+
7027+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr,2+ !!raw, "VEMB");
7028+
redis_cmd_append_sstr_key_zstr(&cmdstr,key,redis_sock,slot);
7029+
redis_cmd_append_sstr_zval(&cmdstr,member,redis_sock);
7030+
7031+
if (raw) {
7032+
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr,"RAW");
7033+
}
7034+
7035+
*cmd=cmdstr.c;
7036+
*cmd_len=cmdstr.len;
7037+
7038+
returnSUCCESS;
7039+
}
7040+
7041+
70107042
/*
70117043
* Redis commands that don't deal with the server at all. The RedisSock*
70127044
* pointer is the only thing retrieved differently, so we just take that

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp