mirror of
https://gitee.com/milvus-io/milvus.git
synced 2025-12-06 17:18:35 +08:00
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 <congqi.xia@zilliz.com> Co-authored-by: wangcanjia <phpip@qq.com>
This commit is contained in:
parent
0c0630cc38
commit
555132c834
@ -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
|
||||
|
||||
45
client/row/cache.go
Normal file
45
client/row/cache.go
Normal file
@ -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
|
||||
}
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user