From a56b24054ff019430d09615e93582c3ea1434bd8 Mon Sep 17 00:00:00 2001 From: congqixia Date: Thu, 6 Mar 2025 21:40:04 +0800 Subject: [PATCH] enhance: [GoSDK] Support alter/drop index properties (#40417) Related to #31293 Signed-off-by: Congqi Xia --- client/milvusclient/index.go | 16 +++++++ client/milvusclient/index_options.go | 57 ++++++++++++++++++++++ client/milvusclient/index_test.go | 72 ++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) diff --git a/client/milvusclient/index.go b/client/milvusclient/index.go index 84d51c8127..92c1c7e747 100644 --- a/client/milvusclient/index.go +++ b/client/milvusclient/index.go @@ -178,3 +178,19 @@ func (c *Client) DropIndex(ctx context.Context, opt DropIndexOption, callOptions 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) + }) +} diff --git a/client/milvusclient/index_options.go b/client/milvusclient/index_options.go index 2ae19e428c..dbbf07e73e 100644 --- a/client/milvusclient/index_options.go +++ b/client/milvusclient/index_options.go @@ -150,3 +150,60 @@ func NewDropIndexOption(collectionName string, indexName string) *dropIndexOptio 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, + } +} diff --git a/client/milvusclient/index_test.go b/client/milvusclient/index_test.go index f7bf78c518..586716f793 100644 --- a/client/milvusclient/index_test.go +++ b/client/milvusclient/index_test.go @@ -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) { suite.Run(t, new(IndexSuite)) }