Use API HandleCStatus to handle CStatus returned by CGo in querynode (#11789)

Signed-off-by: yudong.cai <yudong.cai@zilliz.com>
This commit is contained in:
Cai Yudong 2021-11-15 16:13:10 +08:00 committed by GitHub
parent 106ed4d4ee
commit 1f28dba497
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,9 +20,7 @@ package querynode
*/ */
import "C" import "C"
import ( import (
"errors"
"path/filepath" "path/filepath"
"strconv"
"unsafe" "unsafe"
"go.uber.org/zap" "go.uber.org/zap"
@ -38,12 +36,8 @@ type LoadIndexInfo struct {
func newLoadIndexInfo() (*LoadIndexInfo, error) { func newLoadIndexInfo() (*LoadIndexInfo, error) {
var cLoadIndexInfo C.CLoadIndexInfo var cLoadIndexInfo C.CLoadIndexInfo
status := C.NewLoadIndexInfo(&cLoadIndexInfo) status := C.NewLoadIndexInfo(&cLoadIndexInfo)
errorCode := status.error_code if err := HandleCStatus(&status, "NewLoadIndexInfo failed"); err != nil {
return nil, err
if errorCode != 0 {
errorMsg := C.GoString(status.error_msg)
defer C.free(unsafe.Pointer(status.error_msg))
return nil, errors.New("NewLoadIndexInfo failed, C runtime error detected, error code = " + strconv.Itoa(int(errorCode)) + ", error msg = " + errorMsg)
} }
return &LoadIndexInfo{cLoadIndexInfo: cLoadIndexInfo}, nil return &LoadIndexInfo{cLoadIndexInfo: cLoadIndexInfo}, nil
} }
@ -58,27 +52,13 @@ func (li *LoadIndexInfo) appendIndexParam(indexKey string, indexValue string) er
cIndexValue := C.CString(indexValue) cIndexValue := C.CString(indexValue)
defer C.free(unsafe.Pointer(cIndexValue)) defer C.free(unsafe.Pointer(cIndexValue))
status := C.AppendIndexParam(li.cLoadIndexInfo, cIndexKey, cIndexValue) status := C.AppendIndexParam(li.cLoadIndexInfo, cIndexKey, cIndexValue)
errorCode := status.error_code return HandleCStatus(&status, "AppendIndexParam failed")
if errorCode != 0 {
errorMsg := C.GoString(status.error_msg)
defer C.free(unsafe.Pointer(status.error_msg))
return errors.New("AppendIndexParam failed, C runtime error detected, error code = " + strconv.Itoa(int(errorCode)) + ", error msg = " + errorMsg)
}
return nil
} }
func (li *LoadIndexInfo) appendFieldInfo(fieldID FieldID) error { func (li *LoadIndexInfo) appendFieldInfo(fieldID FieldID) error {
cFieldID := C.long(fieldID) cFieldID := C.long(fieldID)
status := C.AppendFieldInfo(li.cLoadIndexInfo, cFieldID) status := C.AppendFieldInfo(li.cLoadIndexInfo, cFieldID)
errorCode := status.error_code return HandleCStatus(&status, "AppendFieldInfo failed")
if errorCode != 0 {
errorMsg := C.GoString(status.error_msg)
defer C.free(unsafe.Pointer(status.error_msg))
return errors.New("AppendFieldInfo failed, C runtime error detected, error code = " + strconv.Itoa(int(errorCode)) + ", error msg = " + errorMsg)
}
return nil
} }
func (li *LoadIndexInfo) appendIndex(bytesIndex [][]byte, indexKeys []string) error { func (li *LoadIndexInfo) appendIndex(bytesIndex [][]byte, indexKeys []string) error {
@ -86,11 +66,8 @@ func (li *LoadIndexInfo) appendIndex(bytesIndex [][]byte, indexKeys []string) er
status := C.NewBinarySet(&cBinarySet) status := C.NewBinarySet(&cBinarySet)
defer C.DeleteBinarySet(cBinarySet) defer C.DeleteBinarySet(cBinarySet)
errorCode := status.error_code if err := HandleCStatus(&status, "NewBinarySet failed"); err != nil {
if errorCode != 0 { return err
errorMsg := C.GoString(status.error_msg)
defer C.free(unsafe.Pointer(status.error_msg))
return errors.New("newBinarySet failed, C runtime error detected, error code = " + strconv.Itoa(int(errorCode)) + ", error msg = " + errorMsg)
} }
for i, byteIndex := range bytesIndex { for i, byteIndex := range bytesIndex {
@ -101,24 +78,11 @@ func (li *LoadIndexInfo) appendIndex(bytesIndex [][]byte, indexKeys []string) er
indexKey := C.CString(binarySetKey) indexKey := C.CString(binarySetKey)
status = C.AppendBinaryIndex(cBinarySet, indexPtr, indexLen, indexKey) status = C.AppendBinaryIndex(cBinarySet, indexPtr, indexLen, indexKey)
C.free(unsafe.Pointer(indexKey)) C.free(unsafe.Pointer(indexKey))
errorCode = status.error_code if err := HandleCStatus(&status, "AppendBinaryIndex failed"); err != nil {
if errorCode != 0 { return err
break
} }
} }
if errorCode != 0 {
errorMsg := C.GoString(status.error_msg)
defer C.free(unsafe.Pointer(status.error_msg))
return errors.New("AppendBinaryIndex failed, C runtime error detected, error code = " + strconv.Itoa(int(errorCode)) + ", error msg = " + errorMsg)
}
status = C.AppendIndex(li.cLoadIndexInfo, cBinarySet) status = C.AppendIndex(li.cLoadIndexInfo, cBinarySet)
errorCode = status.error_code return HandleCStatus(&status, "AppendIndex failed")
if errorCode != 0 {
errorMsg := C.GoString(status.error_msg)
defer C.free(unsafe.Pointer(status.error_msg))
return errors.New("AppendIndex failed, C runtime error detected, error code = " + strconv.Itoa(int(errorCode)) + ", error msg = " + errorMsg)
}
return nil
} }