2020-08-25 15:45:19 +08:00
|
|
|
package reader
|
|
|
|
|
2020-08-29 17:42:41 +08:00
|
|
|
/*
|
|
|
|
|
2020-10-23 18:01:24 +08:00
|
|
|
#cgo CFLAGS: -I${SRCDIR}/../core/output/include
|
2020-08-29 17:42:41 +08:00
|
|
|
|
2020-10-31 15:11:47 +08:00
|
|
|
#cgo LDFLAGS: -L${SRCDIR}/../core/output/lib -lmilvus_segcore -Wl,-rpath=${SRCDIR}/../core/output/lib
|
2020-08-29 17:42:41 +08:00
|
|
|
|
2020-09-02 10:38:08 +08:00
|
|
|
#include "collection_c.h"
|
|
|
|
#include "partition_c.h"
|
2020-08-29 17:42:41 +08:00
|
|
|
#include "segment_c.h"
|
|
|
|
|
|
|
|
*/
|
2020-08-25 15:45:19 +08:00
|
|
|
import "C"
|
2020-08-28 17:29:26 +08:00
|
|
|
import (
|
2020-10-19 18:31:00 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/errors"
|
2020-11-03 17:09:51 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2020-10-19 18:31:00 +08:00
|
|
|
msgPb "github.com/zilliztech/milvus-distributed/internal/proto/message"
|
2020-11-03 17:09:51 +08:00
|
|
|
"strconv"
|
2020-08-28 17:29:26 +08:00
|
|
|
)
|
|
|
|
|
2020-08-28 18:39:00 +08:00
|
|
|
const SegmentLifetime = 20000
|
2020-08-25 15:45:19 +08:00
|
|
|
|
2020-09-03 19:58:33 +08:00
|
|
|
const (
|
2020-10-24 10:45:57 +08:00
|
|
|
//SegmentOpened = 0
|
|
|
|
//SegmentClosed = 1
|
|
|
|
//SegmentIndexing = 2
|
|
|
|
//SegmentIndexed = 3
|
2020-09-03 19:58:33 +08:00
|
|
|
)
|
|
|
|
|
2020-08-25 15:45:19 +08:00
|
|
|
type Segment struct {
|
2020-09-09 15:24:07 +08:00
|
|
|
SegmentPtr C.CSegmentBase
|
|
|
|
SegmentId int64
|
2020-08-25 15:45:19 +08:00
|
|
|
SegmentCloseTime uint64
|
2020-10-29 19:55:57 +08:00
|
|
|
LastMemSize int64
|
2020-09-21 15:40:56 +08:00
|
|
|
SegmentStatus int
|
2020-08-25 15:45:19 +08:00
|
|
|
}
|
|
|
|
|
2020-10-24 10:45:57 +08:00
|
|
|
//func (s *Segment) GetStatus() int {
|
|
|
|
// /*
|
|
|
|
// bool
|
|
|
|
// IsOpened(CSegmentBase c_segment);
|
|
|
|
// */
|
|
|
|
// var isOpened = C.IsOpened(s.SegmentPtr)
|
|
|
|
// if isOpened {
|
|
|
|
// return SegmentOpened
|
|
|
|
// } else {
|
|
|
|
// return SegmentClosed
|
|
|
|
// }
|
|
|
|
//}
|
2020-08-25 15:45:19 +08:00
|
|
|
|
2020-09-03 19:58:33 +08:00
|
|
|
func (s *Segment) GetRowCount() int64 {
|
2020-09-21 18:16:06 +08:00
|
|
|
/*
|
2020-10-24 10:45:57 +08:00
|
|
|
long int
|
|
|
|
GetRowCount(CSegmentBase c_segment);
|
2020-09-03 19:58:33 +08:00
|
|
|
*/
|
|
|
|
var rowCount = C.GetRowCount(s.SegmentPtr)
|
|
|
|
return int64(rowCount)
|
2020-08-25 15:45:19 +08:00
|
|
|
}
|
|
|
|
|
2020-09-03 19:58:33 +08:00
|
|
|
func (s *Segment) GetDeletedCount() int64 {
|
2020-09-21 18:16:06 +08:00
|
|
|
/*
|
2020-10-24 10:45:57 +08:00
|
|
|
long int
|
|
|
|
GetDeletedCount(CSegmentBase c_segment);
|
2020-09-03 19:58:33 +08:00
|
|
|
*/
|
|
|
|
var deletedCount = C.GetDeletedCount(s.SegmentPtr)
|
|
|
|
return int64(deletedCount)
|
|
|
|
}
|
|
|
|
|
2020-10-24 10:45:57 +08:00
|
|
|
//func (s *Segment) CloseSegment(collection* Collection) error {
|
|
|
|
// /*
|
|
|
|
// int
|
|
|
|
// Close(CSegmentBase c_segment);
|
|
|
|
// */
|
|
|
|
// fmt.Println("Closing segment :", s.SegmentId)
|
|
|
|
//
|
|
|
|
// var status = C.Close(s.SegmentPtr)
|
|
|
|
// s.SegmentStatus = SegmentClosed
|
|
|
|
//
|
|
|
|
// if status != 0 {
|
|
|
|
// return errors.New("Close segment failed, error code = " + strconv.Itoa(int(status)))
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // Build index after closing segment
|
|
|
|
// //s.SegmentStatus = SegmentIndexing
|
|
|
|
// //fmt.Println("Building index...")
|
|
|
|
// //s.BuildIndex(collection)
|
|
|
|
//
|
|
|
|
// // TODO: remove redundant segment indexed status
|
|
|
|
// // Change segment status to indexed
|
|
|
|
// //s.SegmentStatus = SegmentIndexed
|
|
|
|
// //fmt.Println("Segment closed and indexed")
|
|
|
|
//
|
|
|
|
// fmt.Println("Segment closed")
|
|
|
|
// return nil
|
|
|
|
//}
|
2020-08-28 17:29:26 +08:00
|
|
|
|
2020-10-29 19:55:57 +08:00
|
|
|
func (s *Segment) GetMemSize() int64 {
|
2020-09-21 18:16:06 +08:00
|
|
|
/*
|
2020-10-24 10:45:57 +08:00
|
|
|
long int
|
|
|
|
GetMemoryUsageInBytes(CSegmentBase c_segment);
|
2020-09-21 15:10:54 +08:00
|
|
|
*/
|
|
|
|
var memoryUsageInBytes = C.GetMemoryUsageInBytes(s.SegmentPtr)
|
|
|
|
|
2020-10-29 19:55:57 +08:00
|
|
|
return int64(memoryUsageInBytes)
|
2020-09-16 15:21:10 +08:00
|
|
|
}
|
|
|
|
|
2020-08-28 17:29:26 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2020-09-09 15:24:07 +08:00
|
|
|
func (s *Segment) SegmentPreInsert(numOfRecords int) int64 {
|
2020-09-21 18:16:06 +08:00
|
|
|
/*
|
2020-10-24 10:45:57 +08:00
|
|
|
long int
|
|
|
|
PreInsert(CSegmentBase c_segment, long int size);
|
2020-09-09 15:24:07 +08:00
|
|
|
*/
|
2020-09-12 16:57:37 +08:00
|
|
|
var offset = C.PreInsert(s.SegmentPtr, C.long(int64(numOfRecords)))
|
2020-09-09 15:24:07 +08:00
|
|
|
|
2020-09-12 16:57:37 +08:00
|
|
|
return int64(offset)
|
2020-09-09 15:24:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Segment) SegmentPreDelete(numOfRecords int) int64 {
|
2020-09-21 18:16:06 +08:00
|
|
|
/*
|
2020-10-24 10:45:57 +08:00
|
|
|
long int
|
|
|
|
PreDelete(CSegmentBase c_segment, long int size);
|
2020-09-09 15:24:07 +08:00
|
|
|
*/
|
2020-09-12 16:57:37 +08:00
|
|
|
var offset = C.PreDelete(s.SegmentPtr, C.long(int64(numOfRecords)))
|
2020-09-09 15:24:07 +08:00
|
|
|
|
2020-09-12 16:57:37 +08:00
|
|
|
return int64(offset)
|
2020-09-09 15:24:07 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 17:09:51 +08:00
|
|
|
func (s *Segment) SegmentInsert(offset int64, entityIDs *[]int64, timestamps *[]uint64, records *[]*commonpb.Blob) error {
|
2020-09-21 18:16:06 +08:00
|
|
|
/*
|
2020-10-24 10:45:57 +08:00
|
|
|
int
|
|
|
|
Insert(CSegmentBase c_segment,
|
|
|
|
long int reserved_offset,
|
|
|
|
signed long int size,
|
|
|
|
const long* primary_keys,
|
|
|
|
const unsigned long* timestamps,
|
|
|
|
void* raw_data,
|
|
|
|
int sizeof_per_row,
|
|
|
|
signed long int count);
|
2020-09-02 16:23:50 +08:00
|
|
|
*/
|
2020-09-08 10:39:09 +08:00
|
|
|
// Blobs to one big blob
|
2020-11-03 17:09:51 +08:00
|
|
|
//var numOfRow = len(*entityIDs)
|
|
|
|
//var sizeofPerRow = len((*records)[0])
|
|
|
|
//
|
|
|
|
//assert.Equal(nil, numOfRow, len(*records))
|
|
|
|
//
|
|
|
|
//var rawData = make([]byte, numOfRow*sizeofPerRow)
|
|
|
|
//var copyOffset = 0
|
|
|
|
//for i := 0; i < len(*records); i++ {
|
|
|
|
// copy(rawData[copyOffset:], (*records)[i])
|
|
|
|
// copyOffset += sizeofPerRow
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//var cOffset = C.long(offset)
|
|
|
|
//var cNumOfRows = C.long(numOfRow)
|
|
|
|
//var cEntityIdsPtr = (*C.long)(&(*entityIDs)[0])
|
|
|
|
//var cTimestampsPtr = (*C.ulong)(&(*timestamps)[0])
|
|
|
|
//var cSizeofPerRow = C.int(sizeofPerRow)
|
|
|
|
//var cRawDataVoidPtr = unsafe.Pointer(&rawData[0])
|
|
|
|
//
|
|
|
|
//var status = C.Insert(s.SegmentPtr,
|
|
|
|
// cOffset,
|
|
|
|
// cNumOfRows,
|
|
|
|
// cEntityIdsPtr,
|
|
|
|
// cTimestampsPtr,
|
|
|
|
// cRawDataVoidPtr,
|
|
|
|
// cSizeofPerRow,
|
|
|
|
// cNumOfRows)
|
|
|
|
//
|
|
|
|
//if status != 0 {
|
|
|
|
// return errors.New("Insert failed, error code = " + strconv.Itoa(int(status)))
|
|
|
|
//}
|
2020-09-02 17:18:49 +08:00
|
|
|
|
2020-09-07 17:01:46 +08:00
|
|
|
return nil
|
2020-08-28 17:29:26 +08:00
|
|
|
}
|
|
|
|
|
2020-09-09 15:24:07 +08:00
|
|
|
func (s *Segment) SegmentDelete(offset int64, entityIDs *[]int64, timestamps *[]uint64) error {
|
2020-09-21 18:16:06 +08:00
|
|
|
/*
|
2020-10-24 10:45:57 +08:00
|
|
|
int
|
|
|
|
Delete(CSegmentBase c_segment,
|
|
|
|
long int reserved_offset,
|
|
|
|
long size,
|
|
|
|
const long* primary_keys,
|
|
|
|
const unsigned long* timestamps);
|
2020-09-02 16:23:50 +08:00
|
|
|
*/
|
2020-09-09 15:24:07 +08:00
|
|
|
var cOffset = C.long(offset)
|
|
|
|
var cSize = C.long(len(*entityIDs))
|
2020-09-12 16:57:37 +08:00
|
|
|
var cEntityIdsPtr = (*C.long)(&(*entityIDs)[0])
|
2020-09-09 15:24:07 +08:00
|
|
|
var cTimestampsPtr = (*C.ulong)(&(*timestamps)[0])
|
2020-09-02 16:23:50 +08:00
|
|
|
|
2020-09-09 15:24:07 +08:00
|
|
|
var status = C.Delete(s.SegmentPtr, cOffset, cSize, cEntityIdsPtr, cTimestampsPtr)
|
2020-09-02 17:18:49 +08:00
|
|
|
|
|
|
|
if status != 0 {
|
2020-09-07 17:01:46 +08:00
|
|
|
return errors.New("Delete failed, error code = " + strconv.Itoa(int(status)))
|
2020-09-02 17:18:49 +08:00
|
|
|
}
|
2020-09-02 16:23:50 +08:00
|
|
|
|
2020-09-07 17:01:46 +08:00
|
|
|
return nil
|
2020-08-28 17:29:26 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 11:21:19 +08:00
|
|
|
func (s *Segment) SegmentSearch(query *QueryInfo, timestamp uint64, vectorRecord *msgPb.VectorRowRecord) (*SearchResult, error) {
|
2020-09-21 18:16:06 +08:00
|
|
|
/*
|
2020-10-24 10:45:57 +08:00
|
|
|
int
|
|
|
|
Search(CSegmentBase c_segment,
|
|
|
|
CQueryInfo c_query_info,
|
|
|
|
unsigned long timestamp,
|
|
|
|
float* query_raw_data,
|
|
|
|
int num_of_query_raw_data,
|
|
|
|
long int* result_ids,
|
|
|
|
float* result_distances);
|
2020-09-02 16:23:50 +08:00
|
|
|
*/
|
2020-09-22 11:21:19 +08:00
|
|
|
//type CQueryInfo C.CQueryInfo
|
2020-09-18 01:53:18 +08:00
|
|
|
|
|
|
|
cQuery := C.CQueryInfo{
|
|
|
|
num_queries: C.long(query.NumQueries),
|
|
|
|
topK: C.int(query.TopK),
|
|
|
|
field_name: C.CString(query.FieldName),
|
|
|
|
}
|
2020-09-02 16:23:50 +08:00
|
|
|
|
2020-10-24 10:45:57 +08:00
|
|
|
resultIds := make([]int64, int64(query.TopK)*query.NumQueries)
|
|
|
|
resultDistances := make([]float32, int64(query.TopK)*query.NumQueries)
|
2020-09-02 16:23:50 +08:00
|
|
|
|
2020-09-09 15:24:07 +08:00
|
|
|
var cTimestamp = C.ulong(timestamp)
|
|
|
|
var cResultIds = (*C.long)(&resultIds[0])
|
|
|
|
var cResultDistances = (*C.float)(&resultDistances[0])
|
2020-09-15 17:41:05 +08:00
|
|
|
var cQueryRawData *C.float
|
|
|
|
var cQueryRawDataLength C.int
|
2020-09-09 15:24:07 +08:00
|
|
|
|
2020-09-15 17:41:05 +08:00
|
|
|
if vectorRecord.BinaryData != nil {
|
|
|
|
return nil, errors.New("Data of binary type is not supported yet")
|
|
|
|
} else if len(vectorRecord.FloatData) <= 0 {
|
|
|
|
return nil, errors.New("Null query vector data")
|
|
|
|
} else {
|
|
|
|
cQueryRawData = (*C.float)(&vectorRecord.FloatData[0])
|
|
|
|
cQueryRawDataLength = (C.int)(len(vectorRecord.FloatData))
|
|
|
|
}
|
|
|
|
|
2020-09-18 01:53:18 +08:00
|
|
|
var status = C.Search(s.SegmentPtr, cQuery, cTimestamp, cQueryRawData, cQueryRawDataLength, cResultIds, cResultDistances)
|
2020-09-09 15:24:07 +08:00
|
|
|
|
2020-09-07 17:01:46 +08:00
|
|
|
if status != 0 {
|
|
|
|
return nil, errors.New("Search failed, error code = " + strconv.Itoa(int(status)))
|
2020-09-02 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2020-10-15 21:31:50 +08:00
|
|
|
//fmt.Println("Search Result---- Ids =", resultIds, ", Distances =", resultDistances)
|
2020-09-15 15:53:10 +08:00
|
|
|
|
2020-09-07 17:01:46 +08:00
|
|
|
return &SearchResult{ResultIds: resultIds, ResultDistances: resultDistances}, nil
|
2020-08-28 17:29:26 +08:00
|
|
|
}
|