From 555132c834514f7bf869d68935232234fea5014e Mon Sep 17 00:00:00 2001 From: congqixia Date: Tue, 2 Sep 2025 16:33:52 +0800 Subject: [PATCH] enhance: [GoSDK] Add reciever parse result cache (#44166) Related to #44161 This PR: - Add reciever parse result cache - Fix for nil IDs unmarshal panicking (#42895) Signed-off-by: Congqi Xia Co-authored-by: wangcanjia --- client/milvusclient/results.go | 11 ++++++--- client/row/cache.go | 45 ++++++++++++++++++++++++++++++++++ client/row/type.go | 14 ++++++++++- 3 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 client/row/cache.go diff --git a/client/milvusclient/results.go b/client/milvusclient/results.go index e8e76798ad..2fe0dcf9e4 100644 --- a/client/milvusclient/results.go +++ b/client/milvusclient/results.go @@ -86,6 +86,9 @@ func (sr *ResultSet) Unmarshal(receiver any) (err error) { if err != nil { return err } + if sr.IDs == nil { + return nil + } return sr.fillPKEntry(receiver) } @@ -116,8 +119,8 @@ func (sr *ResultSet) fillPKEntry(receiver any) (err error) { et = et.Elem() } - candidates := row.ParseCandidate(et) - candi, ok := candidates[pkField.Name] + rc := row.GetReceiverCandidate(et) + candi, ok := rc.Name2FieldIndex(pkField.Name) if !ok { // pk field not found in struct, skip return nil @@ -202,10 +205,10 @@ func (ds DataSet) Unmarshal(receiver any) (err error) { } func (ds DataSet) fillData(data reflect.Value, dataType reflect.Type, idx int) error { - m := row.ParseCandidate(dataType) + rc := row.GetReceiverCandidate(dataType) for i := 0; i < len(ds); i++ { name := ds[i].Name() - fidx, ok := m[name] + fidx, ok := rc.Name2FieldIndex(name) if !ok { // if target is not found, the behavior here is to ignore the column // `strict` mode could be added in the future to return error if any column missing diff --git a/client/row/cache.go b/client/row/cache.go new file mode 100644 index 0000000000..67e8ffa04d --- /dev/null +++ b/client/row/cache.go @@ -0,0 +1,45 @@ +// Licensed to the LF AI & Data foundation under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package row + +import ( + "reflect" + + "github.com/milvus-io/milvus/pkg/v2/util/typeutil" +) + +var cachedCandidates *typeutil.ConcurrentMap[reflect.Type, *ReceiverCandidate] + +func init() { + cachedCandidates = typeutil.NewConcurrentMap[reflect.Type, *ReceiverCandidate]() +} + +func GetReceiverCandidate(v reflect.Type) *ReceiverCandidate { + rc, ok := cachedCandidates.Get(v) + if ok { + return rc + } + + // reflect.Type.String() cannot work as unique identifier for singleflight + // accept multiple parse for now + rc = &ReceiverCandidate{ + name2Index: parseCandidate(v), + } + cachedCandidates.Insert(v, rc) + + return rc +} diff --git a/client/row/type.go b/client/row/type.go index 5815487163..c82f64839c 100644 --- a/client/row/type.go +++ b/client/row/type.go @@ -21,7 +21,19 @@ import ( "reflect" ) -func ParseCandidate(dataType reflect.Type) map[string]int { +// ReceiverCandidate is the struct stores the information needed for +// receiver setting when unmarshalling dataset to user defined struct +type ReceiverCandidate struct { + name2Index map[string]int +} + +func (rc *ReceiverCandidate) Name2FieldIndex(fieldName string) (int, bool) { + // lock not needed, read ops only + index, ok := rc.name2Index[fieldName] + return index, ok +} + +func parseCandidate(dataType reflect.Type) map[string]int { result := make(map[string]int) for i := 0; i < dataType.NumField(); i++ { f := dataType.Field(i)