2021-04-19 13:47:10 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed 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.
|
|
|
|
|
2021-01-16 10:12:14 +08:00
|
|
|
package querynode
|
2020-11-09 16:27:11 +08:00
|
|
|
|
2020-11-17 10:07:42 +08:00
|
|
|
/*
|
|
|
|
#cgo CFLAGS: -I${SRCDIR}/../core/output/include
|
|
|
|
#cgo LDFLAGS: -L${SRCDIR}/../core/output/lib -lmilvus_segcore -Wl,-rpath=${SRCDIR}/../core/output/lib
|
2020-11-26 16:01:31 +08:00
|
|
|
|
2020-11-25 10:31:51 +08:00
|
|
|
#include "segcore/collection_c.h"
|
|
|
|
#include "segcore/segment_c.h"
|
|
|
|
#include "segcore/plan_c.h"
|
2020-11-17 10:07:42 +08:00
|
|
|
*/
|
2020-11-09 16:27:11 +08:00
|
|
|
import "C"
|
2020-11-17 10:07:42 +08:00
|
|
|
import (
|
2021-03-05 10:15:27 +08:00
|
|
|
"errors"
|
2021-06-04 10:38:34 +08:00
|
|
|
"unsafe"
|
2021-06-09 11:37:55 +08:00
|
|
|
|
2021-06-15 14:43:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/segcorepb"
|
2020-11-17 10:07:42 +08:00
|
|
|
)
|
2020-11-09 16:27:11 +08:00
|
|
|
|
2020-11-17 10:07:42 +08:00
|
|
|
type Plan struct {
|
|
|
|
cPlan C.CPlan
|
2020-11-09 16:27:11 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 12:41:40 +08:00
|
|
|
func createPlan(col *Collection, dsl string) (*Plan, error) {
|
2020-11-17 10:07:42 +08:00
|
|
|
cDsl := C.CString(dsl)
|
2021-03-26 18:40:04 +08:00
|
|
|
defer C.free(unsafe.Pointer(cDsl))
|
2020-11-30 17:58:23 +08:00
|
|
|
var cPlan C.CPlan
|
|
|
|
status := C.CreatePlan(col.collectionPtr, cDsl, &cPlan)
|
|
|
|
|
2021-04-29 16:48:06 +08:00
|
|
|
err1 := HandleCStatus(&status, "Create Plan failed")
|
|
|
|
if err1 != nil {
|
|
|
|
return nil, err1
|
2020-11-30 17:58:23 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 10:07:42 +08:00
|
|
|
var newPlan = &Plan{cPlan: cPlan}
|
2020-11-30 17:58:23 +08:00
|
|
|
return newPlan, nil
|
2020-11-09 16:27:11 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 12:41:40 +08:00
|
|
|
func createPlanByExpr(col *Collection, expr []byte) (*Plan, error) {
|
2021-05-07 15:20:47 +08:00
|
|
|
var cPlan C.CPlan
|
2021-05-07 19:27:17 +08:00
|
|
|
status := C.CreatePlanByExpr(col.collectionPtr, (*C.char)(unsafe.Pointer(&expr[0])), (C.int64_t)(len(expr)), &cPlan)
|
2021-05-07 15:20:47 +08:00
|
|
|
|
|
|
|
err1 := HandleCStatus(&status, "Create Plan by expr failed")
|
|
|
|
if err1 != nil {
|
|
|
|
return nil, err1
|
|
|
|
}
|
|
|
|
|
|
|
|
var newPlan = &Plan{cPlan: cPlan}
|
|
|
|
return newPlan, nil
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:01:31 +08:00
|
|
|
func (plan *Plan) getTopK() int64 {
|
2020-11-17 10:07:42 +08:00
|
|
|
topK := C.GetTopK(plan.cPlan)
|
|
|
|
return int64(topK)
|
|
|
|
}
|
|
|
|
|
2020-12-27 09:05:24 +08:00
|
|
|
func (plan *Plan) getMetricType() string {
|
|
|
|
cMetricType := C.GetMetricType(plan.cPlan)
|
|
|
|
defer C.free(unsafe.Pointer(cMetricType))
|
|
|
|
metricType := C.GoString(cMetricType)
|
|
|
|
return metricType
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:01:31 +08:00
|
|
|
func (plan *Plan) delete() {
|
2020-11-17 10:07:42 +08:00
|
|
|
C.DeletePlan(plan.cPlan)
|
|
|
|
}
|
2020-11-09 16:27:11 +08:00
|
|
|
|
2021-03-30 22:16:58 +08:00
|
|
|
type searchRequest struct {
|
2020-11-17 10:07:42 +08:00
|
|
|
cPlaceholderGroup C.CPlaceholderGroup
|
|
|
|
}
|
2020-11-09 16:27:11 +08:00
|
|
|
|
2021-03-30 22:16:58 +08:00
|
|
|
func parseSearchRequest(plan *Plan, searchRequestBlob []byte) (*searchRequest, error) {
|
|
|
|
if len(searchRequestBlob) == 0 {
|
2021-03-22 19:20:56 +08:00
|
|
|
return nil, errors.New("empty search request")
|
|
|
|
}
|
2021-03-30 22:16:58 +08:00
|
|
|
var blobPtr = unsafe.Pointer(&searchRequestBlob[0])
|
|
|
|
blobSize := C.long(len(searchRequestBlob))
|
2020-11-30 17:58:23 +08:00
|
|
|
var cPlaceholderGroup C.CPlaceholderGroup
|
|
|
|
status := C.ParsePlaceholderGroup(plan.cPlan, blobPtr, blobSize, &cPlaceholderGroup)
|
|
|
|
|
2021-03-30 22:16:58 +08:00
|
|
|
if err := HandleCStatus(&status, "parser searchRequest failed"); err != nil {
|
2021-03-26 16:18:30 +08:00
|
|
|
return nil, err
|
2020-11-30 17:58:23 +08:00
|
|
|
}
|
|
|
|
|
2021-03-30 22:16:58 +08:00
|
|
|
var newSearchRequest = &searchRequest{cPlaceholderGroup: cPlaceholderGroup}
|
|
|
|
return newSearchRequest, nil
|
2020-11-17 10:07:42 +08:00
|
|
|
}
|
2020-11-09 16:27:11 +08:00
|
|
|
|
2021-03-30 22:16:58 +08:00
|
|
|
func (pg *searchRequest) getNumOfQuery() int64 {
|
2020-11-17 10:07:42 +08:00
|
|
|
numQueries := C.GetNumOfQueries(pg.cPlaceholderGroup)
|
|
|
|
return int64(numQueries)
|
|
|
|
}
|
2020-11-09 16:27:11 +08:00
|
|
|
|
2021-03-30 22:16:58 +08:00
|
|
|
func (pg *searchRequest) delete() {
|
2020-11-17 10:07:42 +08:00
|
|
|
C.DeletePlaceholderGroup(pg.cPlaceholderGroup)
|
2020-11-09 16:27:11 +08:00
|
|
|
}
|
2021-06-04 10:38:34 +08:00
|
|
|
|
|
|
|
type RetrievePlan struct {
|
|
|
|
RetrievePlanPtr C.CRetrievePlan
|
|
|
|
Timestamp uint64
|
|
|
|
}
|
|
|
|
|
2021-06-15 14:43:57 +08:00
|
|
|
func createRetrievePlan(col *Collection, msg *segcorepb.RetrieveRequest, timestamp uint64) (*RetrievePlan, error) {
|
2021-06-04 10:38:34 +08:00
|
|
|
protoCGo, err := MarshalForCGo(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
plan := new(RetrievePlan)
|
|
|
|
plan.Timestamp = timestamp
|
|
|
|
status := C.CreateRetrievePlan(col.collectionPtr, protoCGo.CProto, &plan.RetrievePlanPtr)
|
|
|
|
err2 := HandleCStatus(&status, "create retrieve plan failed")
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, err2
|
|
|
|
}
|
|
|
|
return plan, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (plan *RetrievePlan) delete() {
|
|
|
|
C.DeleteRetrievePlan(plan.RetrievePlanPtr)
|
|
|
|
}
|