index_test.go 2.2 KB
Newer Older
Z
zhenshan.cao 已提交
1 2 3
package reader

import (
4
	"context"
Z
zhenshan.cao 已提交
5
	"encoding/binary"
B
bigsheeper 已提交
6
	"fmt"
7
	msgPb "github.com/zilliztech/milvus-distributed/internal/proto/message"
F
FluorineDog 已提交
8 9
	"math"
	"testing"
10 11

	"github.com/stretchr/testify/assert"
Z
zhenshan.cao 已提交
12 13 14 15
)

func TestIndex_BuildIndex(t *testing.T) {
	// 1. Construct node, collection, partition and segment
16 17
	ctx := context.Background()
	node := NewQueryNode(ctx, 0, 0)
18
	var collection = node.NewCollection(0, "collection0", "")
Z
zhenshan.cao 已提交
19 20 21 22 23 24 25 26 27 28 29
	var partition = collection.NewPartition("partition0")
	var segment = partition.NewSegment(0)

	// 2. Create ids and timestamps
	ids := make([]int64, 0)
	timestamps := make([]uint64, 0)

	// 3. Create records, use schema below:
	// schema_tmp->AddField("fakeVec", DataType::VECTOR_FLOAT, 16);
	// schema_tmp->AddField("age", DataType::INT32);
	const DIM = 16
B
bigsheeper 已提交
30
	const N = 100
Z
zhenshan.cao 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	var vec = [DIM]float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
	var rawData []byte
	for _, ele := range vec {
		buf := make([]byte, 4)
		binary.LittleEndian.PutUint32(buf, math.Float32bits(ele))
		rawData = append(rawData, buf...)
	}
	bs := make([]byte, 4)
	binary.LittleEndian.PutUint32(bs, 1)
	rawData = append(rawData, bs...)
	var records [][]byte
	for i := 0; i < N; i++ {
		ids = append(ids, int64(i))
		timestamps = append(timestamps, uint64(i))
		records = append(records, rawData)
	}

	// 4. Do PreInsert
	var offset = segment.SegmentPreInsert(N)
	assert.GreaterOrEqual(t, offset, int64(0))

	// 5. Do Insert
	var err = segment.SegmentInsert(offset, &ids, &timestamps, &records)
	assert.NoError(t, err)

	// 6. Close segment, and build index
B
bigsheeper 已提交
57
	err = segment.CloseSegment(collection)
Z
zhenshan.cao 已提交
58 59 60
	assert.NoError(t, err)

	// 7. Do search
B
bigsheeper 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73
	var queryJson = "{\"field_name\":\"fakevec\",\"num_queries\":1,\"topK\":10}"
	var queryRawData = make([]float32, 0)
	for i := 0; i < 16; i++ {
		queryRawData = append(queryRawData, float32(i))
	}
	var vectorRecord = msgPb.VectorRowRecord{
		FloatData: queryRawData,
	}

	query := node.QueryJson2Info(&queryJson)
	var searchRes, searchErr = segment.SegmentSearch(query, timestamps[N/2], &vectorRecord)
	assert.NoError(t, searchErr)
	fmt.Println(searchRes)
Z
zhenshan.cao 已提交
74 75 76 77 78

	// 8. Destruct node, collection, and segment
	partition.DeleteSegment(segment)
	collection.DeletePartition(partition)
	node.DeleteCollection(collection)
79
	node.Close()
Z
zhenshan.cao 已提交
80
}