Skip to content

Commit bf7afff

Browse files
cxljsndyakov
authored andcommitted
feat: support vectorset (redis#3375)
* feat: support vectorset * fix: char encoding error * use `any` instread of `interface{}` * update vectorset API Signed-off-by: fukua95 <[email protected]> * refact: MapStringFloat64Cmd -> VectorInfoSliceCmd Signed-off-by: fukua95 <[email protected]> * update: * the type of vector attribute: string -> VectorAttributeMarshaller * Add a new API VRemAttr * mark the APIs are experimental Signed-off-by: fukua95 <[email protected]> * trigger CI again Signed-off-by: fukua95 <[email protected]> * rename a API: VRemAttr -> VClearAttributes Signed-off-by: fukua95 <[email protected]> * add test Signed-off-by: fukua95 <[email protected]> * feat(vectorset): improve VSetAttr API and add comprehensive test suite - Simplify VSetAttr to accept interface{} with automatic JSON marshalling - Remove VectorAttributeMarshaller interface for simpler API - Add comprehensive unit tests for all vectorset commands --------- Signed-off-by: fukua95 <[email protected]> Co-authored-by: Nedyalko Dyakov <[email protected]>
1 parent 53a0d34 commit bf7afff

File tree

6 files changed

+1299
-0
lines changed

6 files changed

+1299
-0
lines changed

command.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6826,3 +6826,59 @@ func (cmd *MonitorCmd) Clone() Cmder {
68266826
// Return a new MonitorCmd with the same channel
68276827
return newMonitorCmd(cmd.ctx, cmd.ch)
68286828
}
6829+
6830+
type VectorScoreSliceCmd struct {
6831+
baseCmd
6832+
6833+
val []VectorScore
6834+
}
6835+
6836+
var _ Cmder = (*VectorScoreSliceCmd)(nil)
6837+
6838+
func NewVectorInfoSliceCmd(ctx context.Context, args ...any) *VectorScoreSliceCmd {
6839+
return &VectorScoreSliceCmd{
6840+
baseCmd: baseCmd{
6841+
ctx: ctx,
6842+
args: args,
6843+
},
6844+
}
6845+
}
6846+
6847+
func (cmd *VectorScoreSliceCmd) SetVal(val []VectorScore) {
6848+
cmd.val = val
6849+
}
6850+
6851+
func (cmd *VectorScoreSliceCmd) Val() []VectorScore {
6852+
return cmd.val
6853+
}
6854+
6855+
func (cmd *VectorScoreSliceCmd) Result() ([]VectorScore, error) {
6856+
return cmd.val, cmd.err
6857+
}
6858+
6859+
func (cmd *VectorScoreSliceCmd) String() string {
6860+
return cmdString(cmd, cmd.val)
6861+
}
6862+
6863+
func (cmd *VectorScoreSliceCmd) readReply(rd *proto.Reader) error {
6864+
n, err := rd.ReadMapLen()
6865+
if err != nil {
6866+
return err
6867+
}
6868+
6869+
cmd.val = make([]VectorScore, n)
6870+
for i := 0; i < n; i++ {
6871+
name, err := rd.ReadString()
6872+
if err != nil {
6873+
return err
6874+
}
6875+
cmd.val[i].Name = name
6876+
6877+
score, err := rd.ReadFloat()
6878+
if err != nil {
6879+
return err
6880+
}
6881+
cmd.val[i].Score = score
6882+
}
6883+
return nil
6884+
}

commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ type Cmdable interface {
234234
StreamCmdable
235235
TimeseriesCmdable
236236
JSONCmdable
237+
VectorSetCmdable
237238
}
238239

239240
type StatefulCmdable interface {

unit_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package redis
2+
3+
import (
4+
"context"
5+
)
6+
7+
// mockCmdable is a mock implementation of cmdable that records the last command.
8+
// This is used for unit testing command construction without requiring a Redis server.
9+
type mockCmdable struct {
10+
lastCmd Cmder
11+
returnErr error
12+
}
13+
14+
func (m *mockCmdable) call(ctx context.Context, cmd Cmder) error {
15+
m.lastCmd = cmd
16+
if m.returnErr != nil {
17+
cmd.SetErr(m.returnErr)
18+
}
19+
return m.returnErr
20+
}
21+
22+
func (m *mockCmdable) asCmdable() cmdable {
23+
return func(ctx context.Context, cmd Cmder) error {
24+
return m.call(ctx, cmd)
25+
}
26+
}

0 commit comments

Comments
 (0)