Skip to content

Commit 40c0ecf

Browse files
committed
update:
* the type of vector attribute: string -> VectorAttributeMarshaller * Add a new API VRemAttr * mark the APIs are experimental Signed-off-by: fukua95 <[email protected]>
1 parent d2bb4dc commit 40c0ecf

File tree

3 files changed

+70
-30
lines changed

3 files changed

+70
-30
lines changed

command.go

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

5624-
type VectorInfo struct {
5625-
Name string
5626-
Score float64
5627-
}
5628-
5629-
type VectorInfoSliceCmd struct {
5624+
type VectorScoreSliceCmd struct {
56305625
baseCmd
56315626

5632-
val []VectorInfo
5627+
val []VectorScore
56335628
}
56345629

5635-
var _ Cmder = (*VectorInfoSliceCmd)(nil)
5630+
var _ Cmder = (*VectorScoreSliceCmd)(nil)
56365631

5637-
func NewVectorInfoSliceCmd(ctx context.Context, args ...any) *VectorInfoSliceCmd {
5638-
return &VectorInfoSliceCmd{
5632+
func NewVectorInfoSliceCmd(ctx context.Context, args ...any) *VectorScoreSliceCmd {
5633+
return &VectorScoreSliceCmd{
56395634
baseCmd: baseCmd{
56405635
ctx: ctx,
56415636
args: args,
56425637
},
56435638
}
56445639
}
56455640

5646-
func (cmd *VectorInfoSliceCmd) SetVal(val []VectorInfo) {
5641+
func (cmd *VectorScoreSliceCmd) SetVal(val []VectorScore) {
56475642
cmd.val = val
56485643
}
56495644

5650-
func (cmd *VectorInfoSliceCmd) Val() []VectorInfo {
5645+
func (cmd *VectorScoreSliceCmd) Val() []VectorScore {
56515646
return cmd.val
56525647
}
56535648

5654-
func (cmd *VectorInfoSliceCmd) Result() ([]VectorInfo, error) {
5649+
func (cmd *VectorScoreSliceCmd) Result() ([]VectorScore, error) {
56555650
return cmd.val, cmd.err
56565651
}
56575652

5658-
func (cmd *VectorInfoSliceCmd) String() string {
5653+
func (cmd *VectorScoreSliceCmd) String() string {
56595654
return cmdString(cmd, cmd.val)
56605655
}
56615656

5662-
func (cmd *VectorInfoSliceCmd) readReply(rd *proto.Reader) error {
5657+
func (cmd *VectorScoreSliceCmd) readReply(rd *proto.Reader) error {
56635658
n, err := rd.ReadMapLen()
56645659
if err != nil {
56655660
return err
56665661
}
56675662

5668-
cmd.val = make([]VectorInfo, n)
5663+
cmd.val = make([]VectorScore, n)
56695664
for i := 0; i < n; i++ {
56705665
name, err := rd.ReadString()
56715666
if err != nil {

vectorset_commands.go

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strconv"
66
)
77

8+
// note: the APIs is experimental and may be subject to change.
89
type VectorSetCmdable interface {
910
VAdd(ctx context.Context, key, element string, val Vector) *BoolCmd
1011
VAddWithArgs(ctx context.Context, key, element string, val Vector, addArgs *VAddArgs) *BoolCmd
@@ -14,15 +15,16 @@ type VectorSetCmdable interface {
1415
VGetAttr(ctx context.Context, key, element string) *StringCmd
1516
VInfo(ctx context.Context, key string) *MapStringInterfaceCmd
1617
VLinks(ctx context.Context, key, element string) *StringSliceCmd
17-
VLinksWithScores(ctx context.Context, key, element string) *VectorInfoSliceCmd
18+
VLinksWithScores(ctx context.Context, key, element string) *VectorScoreSliceCmd
1819
VRandMember(ctx context.Context, key string) *StringCmd
1920
VRandMemberCount(ctx context.Context, key string, count int) *StringSliceCmd
2021
VRem(ctx context.Context, key, element string) *BoolCmd
21-
VSetAttr(ctx context.Context, key, element, attr string) *BoolCmd
22+
VSetAttr(ctx context.Context, key, element string, attr VectorAttributeMarshaller) *BoolCmd
23+
VRemAttr(ctx context.Context, key, element string) *BoolCmd
2224
VSim(ctx context.Context, key string, val Vector) *StringSliceCmd
23-
VSimWithScores(ctx context.Context, key string, val Vector) *VectorInfoSliceCmd
25+
VSimWithScores(ctx context.Context, key string, val Vector) *VectorScoreSliceCmd
2426
VSimWithArgs(ctx context.Context, key string, val Vector, args *VSimArgs) *StringSliceCmd
25-
VSimWithArgsWithScores(ctx context.Context, key string, val Vector, args *VSimArgs) *VectorInfoSliceCmd
27+
VSimWithArgsWithScores(ctx context.Context, key string, val Vector, args *VSimArgs) *VectorScoreSliceCmd
2628
}
2729

2830
type Vector interface {
@@ -42,7 +44,7 @@ func (v *VectorFP32) Value() []any {
4244
return []any{vectorFormatFP32, v.Val}
4345
}
4446

45-
var _ Vector = &VectorFP32{}
47+
var _ Vector = (*VectorFP32)(nil)
4648

4749
type VectorValues struct {
4850
Val []float64
@@ -58,7 +60,7 @@ func (v *VectorValues) Value() []any {
5860
return res
5961
}
6062

61-
var _ Vector = &VectorValues{}
63+
var _ Vector = (*VectorValues)(nil)
6264

6365
type VectorRef struct {
6466
Name string // the name of the referent vector
@@ -68,9 +70,27 @@ func (v *VectorRef) Value() []any {
6870
return []any{"ele", v.Name}
6971
}
7072

71-
var _ Vector = &VectorRef{}
73+
var _ Vector = (*VectorRef)(nil)
74+
75+
type VectorScore struct {
76+
Name string
77+
Score float64
78+
}
79+
80+
type VectorAttributeMarshaller interface {
81+
Marshall() string
82+
}
83+
84+
type VectorAttributeRawString string
85+
86+
func (a *VectorAttributeRawString) Marshall() string {
87+
return string(*a)
88+
}
89+
90+
var _ VectorAttributeMarshaller = (*VectorAttributeRawString)(nil)
7291

7392
// `VADD key (FP32 | VALUES num) vector element`
93+
// note: the API is experimental and may be subject to change.
7494
func (c cmdable) VAdd(ctx context.Context, key, element string, val Vector) *BoolCmd {
7595
return c.VAddWithArgs(ctx, key, element, val, &VAddArgs{})
7696
}
@@ -120,6 +140,7 @@ func (v VAddArgs) appendArgs(args []any) []any {
120140
}
121141

122142
// `VADD key [REDUCE dim] (FP32 | VALUES num) vector element [CAS] [NOQUANT | Q8 | BIN] [EF build-exploration-factor] [SETATTR attributes] [M numlinks]`
143+
// note: the API is experimental and may be subject to change.
123144
func (c cmdable) VAddWithArgs(ctx context.Context, key, element string, val Vector, addArgs *VAddArgs) *BoolCmd {
124145
if addArgs == nil {
125146
addArgs = &VAddArgs{}
@@ -137,20 +158,23 @@ func (c cmdable) VAddWithArgs(ctx context.Context, key, element string, val Vect
137158
}
138159

139160
// `VCARD key`
161+
// note: the API is experimental and may be subject to change.
140162
func (c cmdable) VCard(ctx context.Context, key string) *IntCmd {
141163
cmd := NewIntCmd(ctx, "vcard", key)
142164
_ = c(ctx, cmd)
143165
return cmd
144166
}
145167

146168
// `VDIM key`
169+
// note: the API is experimental and may be subject to change.
147170
func (c cmdable) VDim(ctx context.Context, key string) *IntCmd {
148171
cmd := NewIntCmd(ctx, "vdim", key)
149172
_ = c(ctx, cmd)
150173
return cmd
151174
}
152175

153176
// `VEMB key element [RAW]`
177+
// note: the API is experimental and may be subject to change.
154178
func (c cmdable) VEmb(ctx context.Context, key, element string, raw bool) *SliceCmd {
155179
args := []any{"vemb", key, element}
156180
if raw {
@@ -162,68 +186,87 @@ func (c cmdable) VEmb(ctx context.Context, key, element string, raw bool) *Slice
162186
}
163187

164188
// `VGETATTR key element`
189+
// note: the API is experimental and may be subject to change.
165190
func (c cmdable) VGetAttr(ctx context.Context, key, element string) *StringCmd {
166191
cmd := NewStringCmd(ctx, "vgetattr", key, element)
167192
_ = c(ctx, cmd)
168193
return cmd
169194
}
170195

171196
// `VINFO key`
197+
// note: the API is experimental and may be subject to change.
172198
func (c cmdable) VInfo(ctx context.Context, key string) *MapStringInterfaceCmd {
173199
cmd := NewMapStringInterfaceCmd(ctx, "vinfo", key)
174200
_ = c(ctx, cmd)
175201
return cmd
176202
}
177203

178204
// `VLINKS key element`
205+
// note: the API is experimental and may be subject to change.
179206
func (c cmdable) VLinks(ctx context.Context, key, element string) *StringSliceCmd {
180207
cmd := NewStringSliceCmd(ctx, "vlinks", key, element)
181208
_ = c(ctx, cmd)
182209
return cmd
183210
}
184211

185212
// `VLINKS key element WITHSCORES`
186-
func (c cmdable) VLinksWithScores(ctx context.Context, key, element string) *VectorInfoSliceCmd {
213+
// note: the API is experimental and may be subject to change.
214+
func (c cmdable) VLinksWithScores(ctx context.Context, key, element string) *VectorScoreSliceCmd {
187215
cmd := NewVectorInfoSliceCmd(ctx, "vlinks", key, element, "withscores")
188216
_ = c(ctx, cmd)
189217
return cmd
190218
}
191219

192220
// `VRANDMEMBER key`
221+
// note: the API is experimental and may be subject to change.
193222
func (c cmdable) VRandMember(ctx context.Context, key string) *StringCmd {
194223
cmd := NewStringCmd(ctx, "vrandmember", key)
195224
_ = c(ctx, cmd)
196225
return cmd
197226
}
198227

199228
// `VRANDMEMBER key [count]`
229+
// note: the API is experimental and may be subject to change.
200230
func (c cmdable) VRandMemberCount(ctx context.Context, key string, count int) *StringSliceCmd {
201231
cmd := NewStringSliceCmd(ctx, "vrandmember", key, count)
202232
_ = c(ctx, cmd)
203233
return cmd
204234
}
205235

206236
// `VREM key element`
237+
// note: the API is experimental and may be subject to change.
207238
func (c cmdable) VRem(ctx context.Context, key, element string) *BoolCmd {
208239
cmd := NewBoolCmd(ctx, "vrem", key, element)
209240
_ = c(ctx, cmd)
210241
return cmd
211242
}
212243

213244
// `VSETATTR key element "{ JSON obj }"`
214-
func (c cmdable) VSetAttr(ctx context.Context, key, element, attr string) *BoolCmd {
215-
cmd := NewBoolCmd(ctx, "vsetattr", key, element, attr)
245+
// note: the API is experimental and may be subject to change.
246+
func (c cmdable) VSetAttr(ctx context.Context, key, element string, attr VectorAttributeMarshaller) *BoolCmd {
247+
cmd := NewBoolCmd(ctx, "vsetattr", key, element, attr.Marshall())
248+
_ = c(ctx, cmd)
249+
return cmd
250+
}
251+
252+
// `VREMATTR` removes attributes on a vector set element.
253+
// is equal to `VSETATTR key element ""`
254+
// note: the API is experimental and may be subject to change.
255+
func (c cmdable) VRemAttr(ctx context.Context, key, element string) *BoolCmd {
256+
cmd := NewBoolCmd(ctx, "vsetattr", key, element, "")
216257
_ = c(ctx, cmd)
217258
return cmd
218259
}
219260

220261
// `VSIM key (ELE | FP32 | VALUES num) (vector | element)`
262+
// note: the API is experimental and may be subject to change.
221263
func (c cmdable) VSim(ctx context.Context, key string, val Vector) *StringSliceCmd {
222264
return c.VSimWithArgs(ctx, key, val, &VSimArgs{})
223265
}
224266

225267
// `VSIM key (ELE | FP32 | VALUES num) (vector | element) WITHSCORES`
226-
func (c cmdable) VSimWithScores(ctx context.Context, key string, val Vector) *VectorInfoSliceCmd {
268+
// note: the API is experimental and may be subject to change.
269+
func (c cmdable) VSimWithScores(ctx context.Context, key string, val Vector) *VectorScoreSliceCmd {
227270
return c.VSimWithArgsWithScores(ctx, key, val, &VSimArgs{})
228271
}
229272

@@ -265,6 +308,7 @@ func (v VSimArgs) appendArgs(args []any) []any {
265308

266309
// `VSIM key (ELE | FP32 | VALUES num) (vector | element) [COUNT num]
267310
// [EF search-exploration-factor] [FILTER expression] [FILTER-EF max-filtering-effort] [TRUTH] [NOTHREAD]`
311+
// note: the API is experimental and may be subject to change.
268312
func (c cmdable) VSimWithArgs(ctx context.Context, key string, val Vector, simArgs *VSimArgs) *StringSliceCmd {
269313
if simArgs == nil {
270314
simArgs = &VSimArgs{}
@@ -279,7 +323,8 @@ func (c cmdable) VSimWithArgs(ctx context.Context, key string, val Vector, simAr
279323

280324
// `VSIM key (ELE | FP32 | VALUES num) (vector | element) [WITHSCORES] [COUNT num]
281325
// [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) *VectorInfoSliceCmd {
326+
// note: the API is experimental and may be subject to change.
327+
func (c cmdable) VSimWithArgsWithScores(ctx context.Context, key string, val Vector, simArgs *VSimArgs) *VectorScoreSliceCmd {
283328
if simArgs == nil {
284329
simArgs = &VSimArgs{}
285330
}

vectorset_commands_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ var _ = Describe("Redis VectorSet commands", Label("vectorset"), func() {
195195
vals := []struct {
196196
name string
197197
v redis.VectorValues
198-
attr string
198+
attr redis.VectorAttributeRawString
199199
}{
200200
{
201201
name: "k0",
@@ -237,7 +237,7 @@ var _ = Describe("Redis VectorSet commands", Label("vectorset"), func() {
237237
expectNil(err)
238238
expectTrue(ok)
239239
if len(v.attr) > 0 {
240-
ok, err = client.VSetAttr(ctx, vecName, v.name, v.attr).Result()
240+
ok, err = client.VSetAttr(ctx, vecName, v.name, &v.attr).Result()
241241
expectNil(err)
242242
expectTrue(ok)
243243
}

0 commit comments

Comments
 (0)