Skip to content

Commit d2bb4dc

Browse files
committed
refact: MapStringFloat64Cmd -> VectorInfoSliceCmd
Signed-off-by: fukua95 <[email protected]>
1 parent 1c3bf4c commit d2bb4dc

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

command.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5621,57 +5621,63 @@ func (cmd *MonitorCmd) Stop() {
56215621
cmd.status = monitorStatusStop
56225622
}
56235623

5624-
type MapStringFloatCmd struct {
5624+
type VectorInfo struct {
5625+
Name string
5626+
Score float64
5627+
}
5628+
5629+
type VectorInfoSliceCmd struct {
56255630
baseCmd
56265631

5627-
val map[string]float64
5632+
val []VectorInfo
56285633
}
56295634

5630-
var _ Cmder = (*MapStringFloatCmd)(nil)
5635+
var _ Cmder = (*VectorInfoSliceCmd)(nil)
56315636

5632-
func NewMapStringFloatCmd(ctx context.Context, args ...any) *MapStringFloatCmd {
5633-
return &MapStringFloatCmd{
5637+
func NewVectorInfoSliceCmd(ctx context.Context, args ...any) *VectorInfoSliceCmd {
5638+
return &VectorInfoSliceCmd{
56345639
baseCmd: baseCmd{
56355640
ctx: ctx,
56365641
args: args,
56375642
},
56385643
}
56395644
}
56405645

5641-
func (cmd *MapStringFloatCmd) SetVal(val map[string]float64) {
5646+
func (cmd *VectorInfoSliceCmd) SetVal(val []VectorInfo) {
56425647
cmd.val = val
56435648
}
56445649

5645-
func (cmd *MapStringFloatCmd) Val() map[string]float64 {
5650+
func (cmd *VectorInfoSliceCmd) Val() []VectorInfo {
56465651
return cmd.val
56475652
}
56485653

5649-
func (cmd *MapStringFloatCmd) Result() (map[string]float64, error) {
5654+
func (cmd *VectorInfoSliceCmd) Result() ([]VectorInfo, error) {
56505655
return cmd.val, cmd.err
56515656
}
56525657

5653-
func (cmd *MapStringFloatCmd) String() string {
5658+
func (cmd *VectorInfoSliceCmd) String() string {
56545659
return cmdString(cmd, cmd.val)
56555660
}
56565661

5657-
func (cmd *MapStringFloatCmd) readReply(rd *proto.Reader) error {
5662+
func (cmd *VectorInfoSliceCmd) readReply(rd *proto.Reader) error {
56585663
n, err := rd.ReadMapLen()
56595664
if err != nil {
56605665
return err
56615666
}
56625667

5663-
cmd.val = make(map[string]float64, n)
5668+
cmd.val = make([]VectorInfo, n)
56645669
for i := 0; i < n; i++ {
5665-
key, err := rd.ReadString()
5670+
name, err := rd.ReadString()
56665671
if err != nil {
56675672
return err
56685673
}
5674+
cmd.val[i].Name = name
56695675

5670-
nn, err := rd.ReadFloat()
5676+
score, err := rd.ReadFloat()
56715677
if err != nil {
56725678
return err
56735679
}
5674-
cmd.val[key] = nn
5680+
cmd.val[i].Score = score
56755681
}
56765682
return nil
56775683
}

vectorset_commands.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ type VectorSetCmdable interface {
1414
VGetAttr(ctx context.Context, key, element string) *StringCmd
1515
VInfo(ctx context.Context, key string) *MapStringInterfaceCmd
1616
VLinks(ctx context.Context, key, element string) *StringSliceCmd
17-
VLinksWithScores(ctx context.Context, key, element string) *MapStringFloatCmd
17+
VLinksWithScores(ctx context.Context, key, element string) *VectorInfoSliceCmd
1818
VRandMember(ctx context.Context, key string) *StringCmd
1919
VRandMemberCount(ctx context.Context, key string, count int) *StringSliceCmd
2020
VRem(ctx context.Context, key, element string) *BoolCmd
2121
VSetAttr(ctx context.Context, key, element, attr string) *BoolCmd
2222
VSim(ctx context.Context, key string, val Vector) *StringSliceCmd
23-
VSimWithScores(ctx context.Context, key string, val Vector) *MapStringFloatCmd
23+
VSimWithScores(ctx context.Context, key string, val Vector) *VectorInfoSliceCmd
2424
VSimWithArgs(ctx context.Context, key string, val Vector, args *VSimArgs) *StringSliceCmd
25-
VSimWithArgsWithScores(ctx context.Context, key string, val Vector, args *VSimArgs) *MapStringFloatCmd
25+
VSimWithArgsWithScores(ctx context.Context, key string, val Vector, args *VSimArgs) *VectorInfoSliceCmd
2626
}
2727

2828
type Vector interface {
@@ -183,8 +183,8 @@ func (c cmdable) VLinks(ctx context.Context, key, element string) *StringSliceCm
183183
}
184184

185185
// `VLINKS key element WITHSCORES`
186-
func (c cmdable) VLinksWithScores(ctx context.Context, key, element string) *MapStringFloatCmd {
187-
cmd := NewMapStringFloatCmd(ctx, "vlinks", key, element, "withscores")
186+
func (c cmdable) VLinksWithScores(ctx context.Context, key, element string) *VectorInfoSliceCmd {
187+
cmd := NewVectorInfoSliceCmd(ctx, "vlinks", key, element, "withscores")
188188
_ = c(ctx, cmd)
189189
return cmd
190190
}
@@ -223,7 +223,7 @@ func (c cmdable) VSim(ctx context.Context, key string, val Vector) *StringSliceC
223223
}
224224

225225
// `VSIM key (ELE | FP32 | VALUES num) (vector | element) WITHSCORES`
226-
func (c cmdable) VSimWithScores(ctx context.Context, key string, val Vector) *MapStringFloatCmd {
226+
func (c cmdable) VSimWithScores(ctx context.Context, key string, val Vector) *VectorInfoSliceCmd {
227227
return c.VSimWithArgsWithScores(ctx, key, val, &VSimArgs{})
228228
}
229229

@@ -279,15 +279,15 @@ func (c cmdable) VSimWithArgs(ctx context.Context, key string, val Vector, simAr
279279

280280
// `VSIM key (ELE | FP32 | VALUES num) (vector | element) [WITHSCORES] [COUNT num]
281281
// [EF search-exploration-factor] [FILTER expression] [FILTER-EF max-filtering-effort] [TRUTH] [NOTHREAD]`
282-
func (c cmdable) VSimWithArgsWithScores(ctx context.Context, key string, val Vector, simArgs *VSimArgs) *MapStringFloatCmd {
282+
func (c cmdable) VSimWithArgsWithScores(ctx context.Context, key string, val Vector, simArgs *VSimArgs) *VectorInfoSliceCmd {
283283
if simArgs == nil {
284284
simArgs = &VSimArgs{}
285285
}
286286
args := []any{"vsim", key}
287287
args = append(args, val.Value()...)
288288
args = append(args, "withscores")
289289
args = simArgs.appendArgs(args)
290-
cmd := NewMapStringFloatCmd(ctx, args...)
290+
cmd := NewVectorInfoSliceCmd(ctx, args...)
291291
_ = c(ctx, cmd)
292292
return cmd
293293
}

vectorset_commands_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,13 @@ var _ = Describe("Redis VectorSet commands", Label("vectorset"), func() {
124124
}).Result()
125125
expectNil(err)
126126
expectEqual(len(sim), 3)
127-
expectTrue(sim["k1"] > 0.99)
128-
expectTrue(sim["k2"] > 0.99)
129-
expectTrue(sim["k3"] < 0.8)
127+
simMap := make(map[string]float64)
128+
for _, vi := range sim {
129+
simMap[vi.Name] = vi.Score
130+
}
131+
expectTrue(simMap["k1"] > 0.99)
132+
expectTrue(simMap["k2"] > 0.99)
133+
expectTrue(simMap["k3"] < 0.8)
130134
})
131135

132136
It("dimension operation", func() {

0 commit comments

Comments
 (0)