2021-12-10 21:17:51 +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-10 21:17:51 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 13:47:10 +08:00
|
|
|
//
|
2021-12-10 21:17:51 +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-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo CFLAGS: -I${SRCDIR}/../core/output/include
|
2022-02-09 14:27:46 +08:00
|
|
|
#cgo darwin LDFLAGS: -L${SRCDIR}/../core/output/lib -lmilvus_segcore -Wl,-rpath,"${SRCDIR}/../core/output/lib"
|
|
|
|
#cgo linux LDFLAGS: -L${SRCDIR}/../core/output/lib -lmilvus_segcore -Wl,-rpath=${SRCDIR}/../core/output/lib
|
2020-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
#include "segcore/plan_c.h"
|
|
|
|
#include "segcore/reduce_c.h"
|
|
|
|
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
2021-03-30 22:16:58 +08:00
|
|
|
"errors"
|
2020-11-26 16:01:31 +08:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2021-11-05 09:21:21 +08:00
|
|
|
// SearchResult contains a pointer to the search result in C++ memory
|
2020-11-26 16:01:31 +08:00
|
|
|
type SearchResult struct {
|
2021-07-13 22:20:33 +08:00
|
|
|
cSearchResult C.CSearchResult
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 09:23:11 +08:00
|
|
|
// MarshaledHits contains a pointer to the marshaled hits in C++ memory
|
2020-11-26 16:01:31 +08:00
|
|
|
type MarshaledHits struct {
|
|
|
|
cMarshaledHits C.CMarshaledHits
|
|
|
|
}
|
|
|
|
|
2021-11-12 10:04:49 +08:00
|
|
|
// RetrieveResult contains a pointer to the retrieve result in C++ memory
|
|
|
|
type RetrieveResult struct {
|
|
|
|
cRetrieveResult C.CRetrieveResult
|
|
|
|
}
|
|
|
|
|
2021-08-12 18:00:11 +08:00
|
|
|
func reduceSearchResultsAndFillData(plan *SearchPlan, searchResults []*SearchResult, numSegments int64) error {
|
2021-09-10 18:12:01 +08:00
|
|
|
if plan.cSearchPlan == nil {
|
|
|
|
return errors.New("nil search plan")
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:20:33 +08:00
|
|
|
cSearchResults := make([]C.CSearchResult, 0)
|
2020-11-26 16:01:31 +08:00
|
|
|
for _, res := range searchResults {
|
2021-07-13 22:20:33 +08:00
|
|
|
cSearchResults = append(cSearchResults, res.cSearchResult)
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
2021-07-13 22:20:33 +08:00
|
|
|
cSearchResultPtr := (*C.CSearchResult)(&cSearchResults[0])
|
2022-02-07 23:43:46 +08:00
|
|
|
cNumSegments := C.int64_t(numSegments)
|
2020-12-03 19:00:11 +08:00
|
|
|
|
2021-08-12 18:00:11 +08:00
|
|
|
status := C.ReduceSearchResultsAndFillData(plan.cSearchPlan, cSearchResultPtr, cNumSegments)
|
2021-11-11 10:18:42 +08:00
|
|
|
if err := HandleCStatus(&status, "ReduceSearchResultsAndFillData failed"); err != nil {
|
|
|
|
return err
|
2020-12-03 19:00:11 +08:00
|
|
|
}
|
|
|
|
return nil
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
2021-08-12 18:00:11 +08:00
|
|
|
func reorganizeSearchResults(searchResults []*SearchResult, numSegments int64) (*MarshaledHits, error) {
|
2021-07-13 22:20:33 +08:00
|
|
|
cSearchResults := make([]C.CSearchResult, 0)
|
2020-12-03 19:00:11 +08:00
|
|
|
for _, res := range searchResults {
|
2021-07-13 22:20:33 +08:00
|
|
|
cSearchResults = append(cSearchResults, res.cSearchResult)
|
2020-12-03 19:00:11 +08:00
|
|
|
}
|
2021-07-13 22:20:33 +08:00
|
|
|
cSearchResultPtr := (*C.CSearchResult)(&cSearchResults[0])
|
2020-12-03 19:00:11 +08:00
|
|
|
|
2022-02-07 23:43:46 +08:00
|
|
|
var cNumSegments = C.int64_t(numSegments)
|
2021-03-30 22:16:58 +08:00
|
|
|
var cMarshaledHits C.CMarshaledHits
|
|
|
|
|
2021-08-12 18:00:11 +08:00
|
|
|
status := C.ReorganizeSearchResults(&cMarshaledHits, cSearchResultPtr, cNumSegments)
|
2021-11-11 10:18:42 +08:00
|
|
|
if err := HandleCStatus(&status, "ReorganizeSearchResults failed"); err != nil {
|
|
|
|
return nil, err
|
2020-12-03 19:00:11 +08:00
|
|
|
}
|
|
|
|
return &MarshaledHits{cMarshaledHits: cMarshaledHits}, nil
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mh *MarshaledHits) getHitsBlobSize() int64 {
|
|
|
|
res := C.GetHitsBlobSize(mh.cMarshaledHits)
|
|
|
|
return int64(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mh *MarshaledHits) getHitsBlob() ([]byte, error) {
|
|
|
|
byteSize := mh.getHitsBlobSize()
|
|
|
|
result := make([]byte, byteSize)
|
|
|
|
cResultPtr := unsafe.Pointer(&result[0])
|
|
|
|
C.GetHitsBlob(mh.cMarshaledHits, cResultPtr)
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mh *MarshaledHits) hitBlobSizeInGroup(groupOffset int64) ([]int64, error) {
|
2022-02-07 23:43:46 +08:00
|
|
|
cGroupOffset := (C.int64_t)(groupOffset)
|
2021-08-12 18:00:11 +08:00
|
|
|
numQueries := C.GetNumQueriesPerGroup(mh.cMarshaledHits, cGroupOffset)
|
2020-11-26 16:01:31 +08:00
|
|
|
result := make([]int64, int64(numQueries))
|
2022-02-07 23:43:46 +08:00
|
|
|
cResult := (*C.int64_t)(&result[0])
|
2021-08-12 18:00:11 +08:00
|
|
|
C.GetHitSizePerQueries(mh.cMarshaledHits, cGroupOffset, cResult)
|
2020-11-26 16:01:31 +08:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteMarshaledHits(hits *MarshaledHits) {
|
|
|
|
C.DeleteMarshaledHits(hits.cMarshaledHits)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteSearchResults(results []*SearchResult) {
|
|
|
|
for _, result := range results {
|
2021-07-13 22:20:33 +08:00
|
|
|
C.DeleteSearchResult(result.cSearchResult)
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
}
|