2021-12-06 21:06:26 +08:00
|
|
|
// 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
|
2021-04-19 13:47:10 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-12-06 21:06:26 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 13:47:10 +08:00
|
|
|
//
|
2021-12-06 21:06:26 +08:00
|
|
|
// 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.
|
2021-04-19 13:47:10 +08:00
|
|
|
|
2021-01-16 10:12:14 +08:00
|
|
|
package querynode
|
2020-12-24 20:55:40 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo CFLAGS: -I${SRCDIR}/../core/output/include
|
2022-02-18 18:39:50 +08:00
|
|
|
#cgo darwin LDFLAGS: -L${SRCDIR}/../core/output/lib -lmilvus_common -lmilvus_segcore -Wl,-rpath,"${SRCDIR}/../core/output/lib"
|
|
|
|
#cgo linux LDFLAGS: -L${SRCDIR}/../core/output/lib -lmilvus_common -lmilvus_segcore -Wl,-rpath=${SRCDIR}/../core/output/lib
|
2022-03-17 17:17:22 +08:00
|
|
|
#cgo windows LDFLAGS: -L${SRCDIR}/../core/output/lib -lmilvus_common -lmilvus_segcore -Wl,-rpath=${SRCDIR}/../core/output/lib
|
2020-12-24 20:55:40 +08:00
|
|
|
|
|
|
|
#include "segcore/load_index_c.h"
|
2022-02-18 18:39:50 +08:00
|
|
|
#include "common/vector_index_c.h"
|
2020-12-24 20:55:40 +08:00
|
|
|
|
|
|
|
*/
|
|
|
|
import "C"
|
2022-02-18 18:39:50 +08:00
|
|
|
|
2020-12-24 20:55:40 +08:00
|
|
|
import (
|
2021-01-06 18:19:44 +08:00
|
|
|
"path/filepath"
|
2020-12-24 20:55:40 +08:00
|
|
|
"unsafe"
|
2021-03-05 09:21:35 +08:00
|
|
|
|
2022-04-29 13:35:49 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/schemapb"
|
|
|
|
|
2021-03-05 09:21:35 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
2022-02-08 21:57:46 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
2020-12-24 20:55:40 +08:00
|
|
|
)
|
|
|
|
|
2021-10-05 21:38:16 +08:00
|
|
|
// LoadIndexInfo is a wrapper of the underlying C-structure C.CLoadIndexInfo
|
2020-12-24 20:55:40 +08:00
|
|
|
type LoadIndexInfo struct {
|
|
|
|
cLoadIndexInfo C.CLoadIndexInfo
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:05:20 +08:00
|
|
|
// newLoadIndexInfo returns a new LoadIndexInfo and error
|
2020-12-26 14:16:51 +08:00
|
|
|
func newLoadIndexInfo() (*LoadIndexInfo, error) {
|
2020-12-24 20:55:40 +08:00
|
|
|
var cLoadIndexInfo C.CLoadIndexInfo
|
|
|
|
status := C.NewLoadIndexInfo(&cLoadIndexInfo)
|
2021-11-15 16:13:10 +08:00
|
|
|
if err := HandleCStatus(&status, "NewLoadIndexInfo failed"); err != nil {
|
|
|
|
return nil, err
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
|
|
|
return &LoadIndexInfo{cLoadIndexInfo: cLoadIndexInfo}, nil
|
|
|
|
}
|
|
|
|
|
2022-01-06 23:22:03 +08:00
|
|
|
// deleteLoadIndexInfo would delete C.CLoadIndexInfo
|
2020-12-26 14:16:51 +08:00
|
|
|
func deleteLoadIndexInfo(info *LoadIndexInfo) {
|
|
|
|
C.DeleteLoadIndexInfo(info.cLoadIndexInfo)
|
|
|
|
}
|
|
|
|
|
2022-04-29 13:35:49 +08:00
|
|
|
func (li *LoadIndexInfo) appendIndexInfo(bytesIndex [][]byte, indexInfo *querypb.FieldIndexInfo, fieldType schemapb.DataType) error {
|
2022-02-08 21:57:46 +08:00
|
|
|
fieldID := indexInfo.FieldID
|
|
|
|
indexParams := funcutil.KeyValuePair2Map(indexInfo.IndexParams)
|
|
|
|
indexPaths := indexInfo.IndexFilePaths
|
|
|
|
|
2022-04-29 13:35:49 +08:00
|
|
|
err := li.appendFieldInfo(fieldID, fieldType)
|
2022-02-08 21:57:46 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for key, value := range indexParams {
|
|
|
|
err = li.appendIndexParam(key, value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = li.appendIndexData(bytesIndex, indexPaths)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-06 23:33:55 +08:00
|
|
|
// appendIndexParam append indexParam to index
|
2020-12-26 14:16:51 +08:00
|
|
|
func (li *LoadIndexInfo) appendIndexParam(indexKey string, indexValue string) error {
|
2020-12-24 20:55:40 +08:00
|
|
|
cIndexKey := C.CString(indexKey)
|
2021-03-26 18:40:04 +08:00
|
|
|
defer C.free(unsafe.Pointer(cIndexKey))
|
2020-12-24 20:55:40 +08:00
|
|
|
cIndexValue := C.CString(indexValue)
|
2021-03-26 18:40:04 +08:00
|
|
|
defer C.free(unsafe.Pointer(cIndexValue))
|
2020-12-24 20:55:40 +08:00
|
|
|
status := C.AppendIndexParam(li.cLoadIndexInfo, cIndexKey, cIndexValue)
|
2021-11-15 16:13:10 +08:00
|
|
|
return HandleCStatus(&status, "AppendIndexParam failed")
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
|
|
|
|
2022-04-29 13:35:49 +08:00
|
|
|
// appendFieldInfo appends fieldID & fieldType to index
|
|
|
|
func (li *LoadIndexInfo) appendFieldInfo(fieldID FieldID, fieldType schemapb.DataType) error {
|
2022-02-07 23:43:46 +08:00
|
|
|
cFieldID := C.int64_t(fieldID)
|
2022-04-29 13:35:49 +08:00
|
|
|
cintDType := uint32(fieldType)
|
|
|
|
status := C.AppendFieldInfo(li.cLoadIndexInfo, cFieldID, cintDType)
|
2021-11-15 16:13:10 +08:00
|
|
|
return HandleCStatus(&status, "AppendFieldInfo failed")
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
|
|
|
|
2022-02-08 21:57:46 +08:00
|
|
|
// appendIndexData appends binarySet index to cLoadIndexInfo
|
|
|
|
func (li *LoadIndexInfo) appendIndexData(bytesIndex [][]byte, indexKeys []string) error {
|
2020-12-24 20:55:40 +08:00
|
|
|
var cBinarySet C.CBinarySet
|
|
|
|
status := C.NewBinarySet(&cBinarySet)
|
2021-04-10 10:10:53 +08:00
|
|
|
defer C.DeleteBinarySet(cBinarySet)
|
2020-12-24 20:55:40 +08:00
|
|
|
|
2021-11-15 16:13:10 +08:00
|
|
|
if err := HandleCStatus(&status, "NewBinarySet failed"); err != nil {
|
|
|
|
return err
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, byteIndex := range bytesIndex {
|
|
|
|
indexPtr := unsafe.Pointer(&byteIndex[0])
|
2022-02-07 23:43:46 +08:00
|
|
|
indexLen := C.int64_t(len(byteIndex))
|
2021-01-06 18:19:44 +08:00
|
|
|
binarySetKey := filepath.Base(indexKeys[i])
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Debug("", zap.String("index key", binarySetKey))
|
2021-01-06 18:19:44 +08:00
|
|
|
indexKey := C.CString(binarySetKey)
|
2022-02-18 18:39:50 +08:00
|
|
|
status = C.AppendIndexBinary(cBinarySet, indexPtr, indexLen, indexKey)
|
2021-03-26 18:40:04 +08:00
|
|
|
C.free(unsafe.Pointer(indexKey))
|
2022-02-18 18:39:50 +08:00
|
|
|
if err := HandleCStatus(&status, "LoadIndexInfo AppendIndexBinary failed"); err != nil {
|
2021-11-15 16:13:10 +08:00
|
|
|
return err
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status = C.AppendIndex(li.cLoadIndexInfo, cBinarySet)
|
2021-11-15 16:13:10 +08:00
|
|
|
return HandleCStatus(&status, "AppendIndex failed")
|
2020-12-24 20:55:40 +08:00
|
|
|
}
|