enhance: [GoSDK] Support alter/drop index properties (#40417)

Related to #31293

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
pull/40394/head^2
congqixia 2025-03-06 21:40:04 +08:00 committed by GitHub
parent 2cd03a20d4
commit a56b24054f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 145 additions and 0 deletions

View File

@ -178,3 +178,19 @@ func (c *Client) DropIndex(ctx context.Context, opt DropIndexOption, callOptions
return merr.CheckRPCCall(resp, err) return merr.CheckRPCCall(resp, err)
}) })
} }
func (c *Client) AlterIndexProperties(ctx context.Context, opt AlterIndexPropertiesOption, callOptions ...grpc.CallOption) error {
req := opt.Request()
return c.callService(func(milvusService milvuspb.MilvusServiceClient) error {
resp, err := milvusService.AlterIndex(ctx, req, callOptions...)
return merr.CheckRPCCall(resp, err)
})
}
func (c *Client) DropIndexProperties(ctx context.Context, opt DropIndexPropertiesOption, callOptions ...grpc.CallOption) error {
req := opt.Request()
return c.callService(func(milvusService milvuspb.MilvusServiceClient) error {
resp, err := milvusService.AlterIndex(ctx, req, callOptions...)
return merr.CheckRPCCall(resp, err)
})
}

View File

@ -150,3 +150,60 @@ func NewDropIndexOption(collectionName string, indexName string) *dropIndexOptio
indexName: indexName, indexName: indexName,
} }
} }
type AlterIndexPropertiesOption interface {
Request() *milvuspb.AlterIndexRequest
}
type alterIndexPropertiesOption struct {
collectionName string
indexName string
properties map[string]string
}
func (opt *alterIndexPropertiesOption) Request() *milvuspb.AlterIndexRequest {
return &milvuspb.AlterIndexRequest{
CollectionName: opt.collectionName,
IndexName: opt.indexName,
ExtraParams: entity.MapKvPairs(opt.properties),
}
}
func (opt *alterIndexPropertiesOption) WithProperty(key string, value any) *alterIndexPropertiesOption {
opt.properties[key] = fmt.Sprintf("%v", value)
return opt
}
func NewAlterIndexPropertiesOption(collectionName string, indexName string) *alterIndexPropertiesOption {
return &alterIndexPropertiesOption{
collectionName: collectionName,
indexName: indexName,
properties: make(map[string]string),
}
}
type DropIndexPropertiesOption interface {
Request() *milvuspb.AlterIndexRequest
}
type dropIndexPropertiesOption struct {
collectionName string
indexName string
keys []string
}
func (opt *dropIndexPropertiesOption) Request() *milvuspb.AlterIndexRequest {
return &milvuspb.AlterIndexRequest{
CollectionName: opt.collectionName,
IndexName: opt.indexName,
DeleteKeys: opt.keys,
}
}
func NewDropIndexPropertiesOption(collectionName string, indexName string, keys ...string) *dropIndexPropertiesOption {
return &dropIndexPropertiesOption{
collectionName: collectionName,
indexName: indexName,
keys: keys,
}
}

View File

@ -217,6 +217,78 @@ func (s *IndexSuite) TestDropIndex() {
}) })
} }
func (s *IndexSuite) TestAlterIndexProperties() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.Run("success", func() {
collectionName := fmt.Sprintf("coll_%s", s.randString(6))
indexName := fmt.Sprintf("idx_%s", s.randString(6))
key := fmt.Sprintf("key_%s", s.randString(6))
val := fmt.Sprintf("val_%s", s.randString(6))
s.mock.EXPECT().AlterIndex(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, air *milvuspb.AlterIndexRequest) (*commonpb.Status, error) {
s.Equal(collectionName, air.GetCollectionName())
s.Equal(indexName, air.GetIndexName())
if s.Len(air.GetExtraParams(), 1) {
kv := air.GetExtraParams()[0]
s.Equal(key, kv.GetKey())
s.Equal(val, kv.GetValue())
}
return merr.Success(), nil
}).Once()
err := s.client.AlterIndexProperties(ctx, NewAlterIndexPropertiesOption(collectionName, indexName).WithProperty(key, val))
s.NoError(err)
})
s.Run("failure", func() {
collectionName := fmt.Sprintf("coll_%s", s.randString(6))
indexName := fmt.Sprintf("idx_%s", s.randString(6))
key := fmt.Sprintf("key_%s", s.randString(6))
val := fmt.Sprintf("val_%s", s.randString(6))
s.mock.EXPECT().AlterIndex(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
err := s.client.AlterIndexProperties(ctx, NewAlterIndexPropertiesOption(collectionName, indexName).WithProperty(key, val))
s.Error(err)
})
}
func (s *IndexSuite) TestDropIndexProperties() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.Run("success", func() {
collectionName := fmt.Sprintf("coll_%s", s.randString(6))
indexName := fmt.Sprintf("idx_%s", s.randString(6))
key := fmt.Sprintf("key_%s", s.randString(6))
s.mock.EXPECT().AlterIndex(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, air *milvuspb.AlterIndexRequest) (*commonpb.Status, error) {
s.Equal(collectionName, air.GetCollectionName())
s.Equal(indexName, air.GetIndexName())
s.ElementsMatch([]string{key}, air.GetDeleteKeys())
return merr.Success(), nil
}).Once()
err := s.client.DropIndexProperties(ctx, NewDropIndexPropertiesOption(collectionName, indexName, key))
s.NoError(err)
})
s.Run("failure", func() {
collectionName := fmt.Sprintf("coll_%s", s.randString(6))
indexName := fmt.Sprintf("idx_%s", s.randString(6))
key := fmt.Sprintf("coll_%s", s.randString(6))
s.mock.EXPECT().AlterIndex(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
err := s.client.DropIndexProperties(ctx, NewDropIndexPropertiesOption(collectionName, indexName, key))
s.Error(err)
})
}
func TestIndex(t *testing.T) { func TestIndex(t *testing.T) {
suite.Run(t, new(IndexSuite)) suite.Run(t, new(IndexSuite))
} }