2021-01-15 14:38:36 +08:00
|
|
|
package indexnode
|
2020-12-18 15:44:27 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
#cgo CFLAGS: -I${SRCDIR}/../core/output/include
|
|
|
|
|
|
|
|
#cgo LDFLAGS: -L${SRCDIR}/../core/output/lib -lmilvus_indexbuilder -Wl,-rpath=${SRCDIR}/../core/output/lib
|
|
|
|
|
2020-12-21 16:36:07 +08:00
|
|
|
#include <stdlib.h> // free
|
2020-12-18 15:44:27 +08:00
|
|
|
#include "segcore/collection_c.h"
|
|
|
|
#include "indexbuilder/index_c.h"
|
|
|
|
|
|
|
|
*/
|
|
|
|
import "C"
|
2021-01-26 09:38:40 +08:00
|
|
|
|
2020-12-18 15:44:27 +08:00
|
|
|
import (
|
2021-03-10 09:56:09 +08:00
|
|
|
"errors"
|
2021-01-12 18:03:24 +08:00
|
|
|
"fmt"
|
2020-12-21 16:36:07 +08:00
|
|
|
"unsafe"
|
2020-12-18 15:44:27 +08:00
|
|
|
|
2020-12-21 16:36:07 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/indexcgopb"
|
2020-12-25 11:10:31 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/storage"
|
2020-12-18 15:44:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: use storage.Blob instead later
|
2020-12-25 11:10:31 +08:00
|
|
|
type Blob = storage.Blob
|
2020-12-18 15:44:27 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
// just for debugging
|
|
|
|
type QueryResult interface {
|
|
|
|
Delete() error
|
|
|
|
NQ() int64
|
|
|
|
TOPK() int64
|
|
|
|
IDs() []int64
|
|
|
|
Distances() []float32
|
|
|
|
}
|
|
|
|
|
|
|
|
type CQueryResult struct {
|
|
|
|
ptr C.CIndexQueryResult
|
|
|
|
}
|
|
|
|
|
|
|
|
type CFunc func() C.CStatus
|
|
|
|
|
|
|
|
func TryCatch(fn CFunc) error {
|
|
|
|
status := fn()
|
|
|
|
errorCode := status.error_code
|
|
|
|
if errorCode != 0 {
|
|
|
|
errorMsg := C.GoString(status.error_msg)
|
|
|
|
defer C.free(unsafe.Pointer(status.error_msg))
|
2021-03-05 10:15:27 +08:00
|
|
|
return fmt.Errorf("error code = %d, error msg = %s", errorCode, errorMsg)
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateQueryResult() (QueryResult, error) {
|
|
|
|
var ptr C.CIndexQueryResult
|
|
|
|
fn := func() C.CStatus {
|
|
|
|
return C.CreateQueryResult(&ptr)
|
|
|
|
}
|
|
|
|
err := TryCatch(fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &CQueryResult{
|
|
|
|
ptr: ptr,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qs *CQueryResult) Delete() error {
|
|
|
|
fn := func() C.CStatus {
|
2021-02-20 10:14:03 +08:00
|
|
|
return C.DeleteIndexQueryResult(qs.ptr)
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
|
|
|
return TryCatch(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qs *CQueryResult) NQ() int64 {
|
|
|
|
return int64(C.NqOfQueryResult(qs.ptr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qs *CQueryResult) TOPK() int64 {
|
|
|
|
return int64(C.TopkOfQueryResult(qs.ptr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qs *CQueryResult) IDs() []int64 {
|
|
|
|
nq := qs.NQ()
|
|
|
|
topk := qs.TOPK()
|
|
|
|
|
|
|
|
if nq <= 0 || topk <= 0 {
|
|
|
|
return []int64{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: how could we avoid memory copy every time when this called
|
|
|
|
ids := make([]int64, nq*topk)
|
|
|
|
C.GetIdsOfQueryResult(qs.ptr, (*C.int64_t)(&ids[0]))
|
|
|
|
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qs *CQueryResult) Distances() []float32 {
|
|
|
|
nq := qs.NQ()
|
|
|
|
topk := qs.TOPK()
|
|
|
|
|
|
|
|
if nq <= 0 || topk <= 0 {
|
|
|
|
return []float32{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: how could we avoid memory copy every time when this called
|
|
|
|
distances := make([]float32, nq*topk)
|
|
|
|
C.GetDistancesOfQueryResult(qs.ptr, (*C.float)(&distances[0]))
|
|
|
|
|
|
|
|
return distances
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:44:27 +08:00
|
|
|
type Index interface {
|
|
|
|
Serialize() ([]*Blob, error)
|
|
|
|
Load([]*Blob) error
|
2020-12-28 10:00:02 +08:00
|
|
|
BuildFloatVecIndexWithoutIds(vectors []float32) error
|
|
|
|
BuildBinaryVecIndexWithoutIds(vectors []byte) error
|
2021-01-04 12:03:29 +08:00
|
|
|
QueryOnFloatVecIndex(vectors []float32) (QueryResult, error)
|
|
|
|
QueryOnBinaryVecIndex(vectors []byte) (QueryResult, error)
|
|
|
|
QueryOnFloatVecIndexWithParam(vectors []float32, params map[string]string) (QueryResult, error)
|
|
|
|
QueryOnBinaryVecIndexWithParam(vectors []byte, params map[string]string) (QueryResult, error)
|
2020-12-18 15:44:27 +08:00
|
|
|
Delete() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type CIndex struct {
|
|
|
|
indexPtr C.CIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *CIndex) Serialize() ([]*Blob, error) {
|
2021-02-22 18:33:40 +08:00
|
|
|
var cBinary C.CBinary
|
|
|
|
|
|
|
|
status := C.SerializeToSlicedBuffer(index.indexPtr, &cBinary)
|
2021-02-23 14:35:00 +08:00
|
|
|
defer C.DeleteCBinary(cBinary)
|
2020-12-29 17:53:27 +08:00
|
|
|
errorCode := status.error_code
|
|
|
|
if errorCode != 0 {
|
|
|
|
errorMsg := C.GoString(status.error_msg)
|
|
|
|
defer C.free(unsafe.Pointer(status.error_msg))
|
2021-03-05 10:15:27 +08:00
|
|
|
return nil, fmt.Errorf("SerializeToSlicedBuffer failed, C runtime error detected, error code = %d, err msg = %s", errorCode, errorMsg)
|
2020-12-29 17:53:27 +08:00
|
|
|
}
|
|
|
|
|
2021-02-22 18:33:40 +08:00
|
|
|
binarySize := C.GetCBinarySize(cBinary)
|
|
|
|
binaryData := make([]byte, binarySize)
|
|
|
|
C.GetCBinaryData(cBinary, unsafe.Pointer(&binaryData[0]))
|
2020-12-21 16:36:07 +08:00
|
|
|
|
|
|
|
var blobs indexcgopb.BinarySet
|
2021-02-22 18:33:40 +08:00
|
|
|
err := proto.Unmarshal(binaryData, &blobs)
|
2020-12-18 15:44:27 +08:00
|
|
|
if err != nil {
|
2020-12-21 16:36:07 +08:00
|
|
|
return nil, err
|
2020-12-18 15:44:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret := make([]*Blob, 0)
|
2020-12-21 16:36:07 +08:00
|
|
|
for _, data := range blobs.Datas {
|
|
|
|
ret = append(ret, &Blob{Key: data.Key, Value: data.Value})
|
2020-12-18 15:44:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2020-12-21 16:36:07 +08:00
|
|
|
func (index *CIndex) Load(blobs []*Blob) error {
|
|
|
|
binarySet := &indexcgopb.BinarySet{Datas: make([]*indexcgopb.Binary, 0)}
|
|
|
|
for _, blob := range blobs {
|
|
|
|
binarySet.Datas = append(binarySet.Datas, &indexcgopb.Binary{Key: blob.Key, Value: blob.Value})
|
|
|
|
}
|
|
|
|
|
2021-02-24 17:24:51 +08:00
|
|
|
datas, err2 := proto.Marshal(binarySet)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
2020-12-21 16:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-12-30 18:16:34 +08:00
|
|
|
CStatus
|
2020-12-25 11:10:31 +08:00
|
|
|
LoadFromSlicedBuffer(CIndex index, const char* serialized_sliced_blob_buffer, int32_t size);
|
2020-12-21 16:36:07 +08:00
|
|
|
*/
|
2020-12-30 18:16:34 +08:00
|
|
|
status := C.LoadFromSlicedBuffer(index.indexPtr, (*C.char)(unsafe.Pointer(&datas[0])), (C.int32_t)(len(datas)))
|
|
|
|
errorCode := status.error_code
|
|
|
|
if errorCode != 0 {
|
|
|
|
errorMsg := C.GoString(status.error_msg)
|
|
|
|
defer C.free(unsafe.Pointer(status.error_msg))
|
2021-03-05 10:15:27 +08:00
|
|
|
return fmt.Errorf("BuildFloatVecIndexWithoutIds failed, C runtime error detected, error code = %d, err msg = %s", errorCode, errorMsg)
|
2020-12-30 18:16:34 +08:00
|
|
|
}
|
2020-12-18 15:44:27 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-28 10:00:02 +08:00
|
|
|
func (index *CIndex) BuildFloatVecIndexWithoutIds(vectors []float32) error {
|
2020-12-21 16:36:07 +08:00
|
|
|
/*
|
2020-12-29 17:53:27 +08:00
|
|
|
CStatus
|
2020-12-28 10:00:02 +08:00
|
|
|
BuildFloatVecIndexWithoutIds(CIndex index, int64_t float_value_num, const float* vectors);
|
2020-12-21 16:36:07 +08:00
|
|
|
*/
|
2021-01-12 18:03:24 +08:00
|
|
|
fmt.Println("before BuildFloatVecIndexWithoutIds")
|
2020-12-29 17:53:27 +08:00
|
|
|
status := C.BuildFloatVecIndexWithoutIds(index.indexPtr, (C.int64_t)(len(vectors)), (*C.float)(&vectors[0]))
|
|
|
|
errorCode := status.error_code
|
2021-01-12 18:03:24 +08:00
|
|
|
fmt.Println("BuildFloatVecIndexWithoutIds error code: ", errorCode)
|
2020-12-29 17:53:27 +08:00
|
|
|
if errorCode != 0 {
|
|
|
|
errorMsg := C.GoString(status.error_msg)
|
2021-01-12 18:03:24 +08:00
|
|
|
fmt.Println("BuildFloatVecIndexWithoutIds error msg: ", errorMsg)
|
2020-12-29 17:53:27 +08:00
|
|
|
defer C.free(unsafe.Pointer(status.error_msg))
|
2021-03-05 10:15:27 +08:00
|
|
|
return fmt.Errorf("BuildFloatVecIndexWithoutIds failed, C runtime error detected, error code = %d, err msg = %s", errorCode, errorMsg)
|
2020-12-29 17:53:27 +08:00
|
|
|
}
|
2020-12-28 10:00:02 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *CIndex) BuildBinaryVecIndexWithoutIds(vectors []byte) error {
|
|
|
|
/*
|
2020-12-29 17:53:27 +08:00
|
|
|
CStatus
|
2020-12-28 10:00:02 +08:00
|
|
|
BuildBinaryVecIndexWithoutIds(CIndex index, int64_t data_size, const uint8_t* vectors);
|
|
|
|
*/
|
2020-12-29 17:53:27 +08:00
|
|
|
status := C.BuildBinaryVecIndexWithoutIds(index.indexPtr, (C.int64_t)(len(vectors)), (*C.uint8_t)(&vectors[0]))
|
|
|
|
errorCode := status.error_code
|
|
|
|
if errorCode != 0 {
|
|
|
|
errorMsg := C.GoString(status.error_msg)
|
|
|
|
defer C.free(unsafe.Pointer(status.error_msg))
|
2021-03-05 10:15:27 +08:00
|
|
|
return fmt.Errorf("BuildBinaryVecIndexWithoutIds failed, C runtime error detected, error code = %d, err msg = %s", errorCode, errorMsg)
|
2020-12-29 17:53:27 +08:00
|
|
|
}
|
2020-12-18 15:44:27 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *CIndex) Delete() error {
|
2020-12-21 16:36:07 +08:00
|
|
|
/*
|
|
|
|
void
|
|
|
|
DeleteIndex(CIndex index);
|
|
|
|
*/
|
2020-12-18 15:44:27 +08:00
|
|
|
C.DeleteIndex(index.indexPtr)
|
2021-01-04 12:03:29 +08:00
|
|
|
// TODO: check if index.indexPtr will be released by golang, though it occupies little memory
|
|
|
|
// C.free(index.indexPtr)
|
2020-12-18 15:44:27 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCIndex(typeParams, indexParams map[string]string) (Index, error) {
|
2020-12-21 16:36:07 +08:00
|
|
|
protoTypeParams := &indexcgopb.TypeParams{
|
|
|
|
Params: make([]*commonpb.KeyValuePair, 0),
|
|
|
|
}
|
|
|
|
for key, value := range typeParams {
|
|
|
|
protoTypeParams.Params = append(protoTypeParams.Params, &commonpb.KeyValuePair{Key: key, Value: value})
|
|
|
|
}
|
2020-12-29 14:43:40 +08:00
|
|
|
typeParamsStr := proto.MarshalTextString(protoTypeParams)
|
2020-12-18 15:44:27 +08:00
|
|
|
|
2020-12-21 16:36:07 +08:00
|
|
|
protoIndexParams := &indexcgopb.IndexParams{
|
|
|
|
Params: make([]*commonpb.KeyValuePair, 0),
|
|
|
|
}
|
|
|
|
for key, value := range indexParams {
|
|
|
|
protoIndexParams.Params = append(protoIndexParams.Params, &commonpb.KeyValuePair{Key: key, Value: value})
|
|
|
|
}
|
2020-12-29 14:43:40 +08:00
|
|
|
indexParamsStr := proto.MarshalTextString(protoIndexParams)
|
2020-12-18 15:44:27 +08:00
|
|
|
|
2020-12-29 14:43:40 +08:00
|
|
|
typeParamsPointer := C.CString(typeParamsStr)
|
|
|
|
indexParamsPointer := C.CString(indexParamsStr)
|
2021-02-24 18:25:34 +08:00
|
|
|
defer C.free(unsafe.Pointer(typeParamsPointer))
|
|
|
|
defer C.free(unsafe.Pointer(indexParamsPointer))
|
2020-12-28 16:55:50 +08:00
|
|
|
|
2020-12-21 16:36:07 +08:00
|
|
|
/*
|
2020-12-29 17:53:27 +08:00
|
|
|
CStatus
|
2020-12-28 16:55:50 +08:00
|
|
|
CreateIndex(const char* serialized_type_params,
|
2020-12-29 17:53:27 +08:00
|
|
|
const char* serialized_index_params,
|
|
|
|
CIndex* res_index);
|
2020-12-21 16:36:07 +08:00
|
|
|
*/
|
2020-12-29 17:53:27 +08:00
|
|
|
var indexPtr C.CIndex
|
2021-01-12 18:03:24 +08:00
|
|
|
fmt.Println("before create index ........................................")
|
2020-12-29 17:53:27 +08:00
|
|
|
status := C.CreateIndex(typeParamsPointer, indexParamsPointer, &indexPtr)
|
2021-01-12 18:03:24 +08:00
|
|
|
fmt.Println("after create index ........................................")
|
2020-12-29 17:53:27 +08:00
|
|
|
errorCode := status.error_code
|
2021-01-12 18:03:24 +08:00
|
|
|
fmt.Println("EEEEEEEEEEEEEEEEEEEEEEEEEE error code: ", errorCode)
|
2020-12-29 17:53:27 +08:00
|
|
|
if errorCode != 0 {
|
|
|
|
errorMsg := C.GoString(status.error_msg)
|
2021-01-12 18:03:24 +08:00
|
|
|
fmt.Println("EEEEEEEEEEEEEEEEEEEEEEEEEE error msg: ", errorMsg)
|
2020-12-29 17:53:27 +08:00
|
|
|
defer C.free(unsafe.Pointer(status.error_msg))
|
2021-03-05 10:15:27 +08:00
|
|
|
return nil, fmt.Errorf(" failed, C runtime error detected, error code = %d, err msg = %s", errorCode, errorMsg)
|
2020-12-29 17:53:27 +08:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:44:27 +08:00
|
|
|
return &CIndex{
|
2020-12-29 17:53:27 +08:00
|
|
|
indexPtr: indexPtr,
|
2020-12-18 15:44:27 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2021-01-04 12:03:29 +08:00
|
|
|
|
|
|
|
func (index *CIndex) QueryOnFloatVecIndex(vectors []float32) (QueryResult, error) {
|
|
|
|
if len(vectors) <= 0 {
|
|
|
|
return nil, errors.New("nq is zero")
|
|
|
|
}
|
|
|
|
res, err := CreateQueryResult()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fn := func() C.CStatus {
|
|
|
|
cRes, ok := res.(*CQueryResult)
|
|
|
|
if !ok {
|
|
|
|
// TODO: ugly here, fix me later
|
|
|
|
panic("only CQueryResult is supported now!")
|
|
|
|
}
|
|
|
|
return C.QueryOnFloatVecIndex(index.indexPtr, (C.int64_t)(len(vectors)), (*C.float)(&vectors[0]), &cRes.ptr)
|
|
|
|
}
|
|
|
|
err = TryCatch(fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *CIndex) QueryOnBinaryVecIndex(vectors []byte) (QueryResult, error) {
|
|
|
|
if len(vectors) <= 0 {
|
|
|
|
return nil, errors.New("nq is zero")
|
|
|
|
}
|
|
|
|
res, err := CreateQueryResult()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fn := func() C.CStatus {
|
|
|
|
cRes, ok := res.(*CQueryResult)
|
|
|
|
if !ok {
|
|
|
|
// TODO: ugly here, fix me later
|
|
|
|
panic("only CQueryResult is supported now!")
|
|
|
|
}
|
|
|
|
return C.QueryOnBinaryVecIndex(index.indexPtr, (C.int64_t)(len(vectors)), (*C.uint8_t)(&vectors[0]), &cRes.ptr)
|
|
|
|
}
|
|
|
|
err = TryCatch(fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *CIndex) QueryOnFloatVecIndexWithParam(vectors []float32, params map[string]string) (QueryResult, error) {
|
|
|
|
if len(vectors) <= 0 {
|
|
|
|
return nil, errors.New("nq is zero")
|
|
|
|
}
|
|
|
|
|
|
|
|
protoParams := &indexcgopb.MapParams{
|
|
|
|
Params: make([]*commonpb.KeyValuePair, 0),
|
|
|
|
}
|
|
|
|
for key, value := range params {
|
|
|
|
protoParams.Params = append(protoParams.Params, &commonpb.KeyValuePair{Key: key, Value: value})
|
|
|
|
}
|
|
|
|
paramsStr := proto.MarshalTextString(protoParams)
|
|
|
|
paramsPointer := C.CString(paramsStr)
|
|
|
|
|
|
|
|
res, err := CreateQueryResult()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fn := func() C.CStatus {
|
|
|
|
cRes, ok := res.(*CQueryResult)
|
|
|
|
if !ok {
|
|
|
|
// TODO: ugly here, fix me later
|
|
|
|
panic("only CQueryResult is supported now!")
|
|
|
|
}
|
|
|
|
return C.QueryOnFloatVecIndexWithParam(index.indexPtr, (C.int64_t)(len(vectors)), (*C.float)(&vectors[0]), paramsPointer, &cRes.ptr)
|
|
|
|
}
|
|
|
|
err = TryCatch(fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *CIndex) QueryOnBinaryVecIndexWithParam(vectors []byte, params map[string]string) (QueryResult, error) {
|
|
|
|
if len(vectors) <= 0 {
|
|
|
|
return nil, errors.New("nq is zero")
|
|
|
|
}
|
|
|
|
|
|
|
|
protoParams := &indexcgopb.MapParams{
|
|
|
|
Params: make([]*commonpb.KeyValuePair, 0),
|
|
|
|
}
|
|
|
|
for key, value := range params {
|
|
|
|
protoParams.Params = append(protoParams.Params, &commonpb.KeyValuePair{Key: key, Value: value})
|
|
|
|
}
|
|
|
|
paramsStr := proto.MarshalTextString(protoParams)
|
|
|
|
paramsPointer := C.CString(paramsStr)
|
|
|
|
|
|
|
|
res, err := CreateQueryResult()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fn := func() C.CStatus {
|
|
|
|
cRes, ok := res.(*CQueryResult)
|
|
|
|
if !ok {
|
|
|
|
// TODO: ugly here, fix me later
|
|
|
|
panic("only CQueryResult is supported now!")
|
|
|
|
}
|
|
|
|
return C.QueryOnBinaryVecIndexWithParam(index.indexPtr, (C.int64_t)(len(vectors)), (*C.uint8_t)(&vectors[0]), paramsPointer, &cRes.ptr)
|
|
|
|
}
|
|
|
|
err = TryCatch(fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|