/
test.go
304 lines (270 loc) · 7.33 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* Copyright 2019 The Gamma Authors.
*
* This source code is licensed under the Apache License, Version 2.0 license
* found in the LICENSE file in the root directory of this source tree.
*/
package main
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"math"
"os"
"strconv"
"strings"
"time"
"unsafe"
"github.com/vearch/vearch/internal/engine/sdk/go/gamma"
"github.com/vearch/vearch/internal/proto/vearchpb"
"golang.org/x/exp/mmap"
)
type Options struct {
Nprobe int32
DocID uint64
DocID2 uint64
D int
MaxDocSize int32
AddDocNum int
SearchNum uint64
IndexingSize int
Filter bool
PrintDoc bool
SearchThreadNum int
FieldsVec []string
FieldsType []vearchpb.FieldType
TableFieldsType []gamma.DataType
Path string
LogDir string
VectorName string
RetrievalType string
StoreType string
Profiles []string
Feature []byte
ProfileFile string
FeatureFile string
FeatureMmap *mmap.ReaderAt
Engine unsafe.Pointer
}
var opt *Options
func Init() {
MaxDocSize := 10 * 10000
FieldsVec := []string{"img", "_id", "field1", "field2", "field3"}
opt = &Options{
Nprobe: 256,
DocID: 0,
DocID2: 0,
D: 512,
MaxDocSize: 10 * 10000,
AddDocNum: 10 * 10000,
SearchNum: 10000,
IndexingSize: 10 * 10000,
Filter: false,
PrintDoc: false,
SearchThreadNum: 1,
FieldsVec: FieldsVec,
FieldsType: []vearchpb.FieldType{vearchpb.FieldType_STRING, vearchpb.FieldType_STRING, vearchpb.FieldType_STRING, vearchpb.FieldType_INT, vearchpb.FieldType_INT},
TableFieldsType: []gamma.DataType{gamma.STRING, gamma.STRING, gamma.STRING, gamma.INT, gamma.INT},
Path: "./files",
LogDir: "./log",
VectorName: "abc",
RetrievalType: "IVFPQ",
StoreType: "RocksDB",
Profiles: make([]string, MaxDocSize*len(FieldsVec)),
}
if len(os.Args) != 3 {
fmt.Println("Usage: [Program] [profile_file] [vectors_file]")
os.Exit(-1)
}
fmt.Println(strings.Join(os.Args[1:], "\n"))
opt.ProfileFile = os.Args[1]
opt.FeatureFile = os.Args[2]
opt.FeatureMmap, _ = mmap.Open(opt.FeatureFile)
config := gamma.Config{Path: opt.Path, LogDir: opt.LogDir}
opt.Engine = gamma.Init(&config)
fmt.Println("Inited")
}
func CreteTable() {
kIVFPQParam :=
string("{\"nprobe\" : 10, \"metric_type\" : \"InnerProduct\", \"ncentroids\" : 256,\"nsubvector\" : 64}")
//kHNSWParam_str :=
// "{\"nlinks\" : 32, \"metric_type\" : \"InnerProduct\", \"efSearch\" : 64,\"efConstruction\" : 40}"
//
//kFLATParam_str := "{\"metric_type\" : \"InnerProduct\"}"
table := gamma.Table{
Name: opt.VectorName,
IndexType: opt.RetrievalType,
IndexParams: kIVFPQParam}
for i := 0; i < len(opt.FieldsVec); i++ {
isIndex := false
if opt.Filter && (i == 0 || i == 2 || i == 3 || i == 4) {
isIndex = true
}
fieldInfo := gamma.FieldInfo{
Name: opt.FieldsVec[i],
DataType: opt.TableFieldsType[i],
IsIndex: isIndex,
}
table.Fields = append(table.Fields, fieldInfo)
}
vectorInfo := gamma.VectorInfo{
Name: opt.VectorName,
DataType: gamma.FLOAT,
IsIndex: true,
Dimension: int32(opt.D),
StoreType: opt.StoreType,
StoreParam: string("{\"cache_size\": 2048}"),
}
table.VectorsInfos = append(table.VectorsInfos, vectorInfo)
gamma.CreateTable(opt.Engine, &table)
}
func AddDocToEngine(docNum int) {
for i := 0; i < docNum; i++ {
var doc gamma.Doc
for j := 0; j < len(opt.FieldsVec); j++ {
field := &vearchpb.Field{
Name: opt.FieldsVec[j],
Type: opt.FieldsType[j],
}
profileStr := opt.Profiles[i*len(opt.FieldsVec)+j]
if field.Type == vearchpb.FieldType_INT {
tmp, _ := strconv.Atoi(profileStr)
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
field.Value = bytesBuffer.Bytes()
} else if field.Type == vearchpb.FieldType_LONG {
tmp, _ := strconv.ParseInt(profileStr, 10, 64)
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
field.Value = bytesBuffer.Bytes()
} else {
field.Value = []byte(profileStr)
}
doc.Fields = append(doc.Fields, field)
}
field := &vearchpb.Field{
Name: opt.VectorName,
Type: vearchpb.FieldType_VECTOR,
}
field.Value = make([]byte, opt.D*4)
_, _ = opt.FeatureMmap.ReadAt(field.Value, int64(i*opt.D)*4)
doc.Fields = append(doc.Fields, field)
//for i := 0; i < opt.D; i++ {
// a := Float64frombytes(field.Value[i*4:])
// fmt.Println(a)
//}
byteArray := make([][]byte, 1)
byteArray[0] = doc.Serialize()
gamma.AddOrUpdateDocs(opt.Engine, byteArray)
}
}
func Add() {
file, err := os.OpenFile(opt.ProfileFile, os.O_RDWR, 0666)
if err != nil {
fmt.Println("Open file error!", err)
return
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
panic(err)
}
var size = stat.Size()
fmt.Println("file size=", size)
buf := bufio.NewReader(file)
for idx := 0; idx < opt.AddDocNum; idx++ {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)
if err != nil {
if err == io.EOF {
fmt.Println("File read ok!")
break
} else {
fmt.Println("Read file error!", err)
return
}
}
docs := strings.Split(line, "\t")
i := 0
for _, doc := range docs {
opt.Profiles[idx*len(opt.FieldsVec)+i] = doc
i++
if i > len(opt.FieldsVec)-1 {
break
}
}
}
AddDocToEngine(opt.AddDocNum)
}
func Load() {
gamma.Load(opt.Engine)
}
func Dump() {
gamma.Dump(opt.Engine)
}
func GetDoc() {
//tmp, _ := strconv.ParseInt("100", 10, 64)
//bytesBuffer := bytes.NewBuffer([]byte{})
//binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
//docID := bytesBuffer.Bytes()
docID := []byte("100")
var doc gamma.Doc
gamma.GetDocByID(opt.Engine, docID, &doc)
fmt.Printf("doc [%v]", doc)
}
func Float64frombytes(bytes []byte) float32 {
bits := binary.LittleEndian.Uint32(bytes)
float := math.Float32frombits(bits)
return float
}
func Search() {
request := &vearchpb.SearchRequest{
ReqNum: 1,
TopN: 100,
IsBruteSearch: 0,
MultiVectorRank: 0,
IndexParams: "{\"metric_type\" : \"InnerProduct\", \"recall_num\" : 100, \"nprobe\" : 10, \"ivf_flat\" : 0}",
L2Sqrt: false,
}
request.VecFields = make([]*vearchpb.VectorQuery, 1)
request.VecFields[0] = &vearchpb.VectorQuery{
Name: opt.VectorName,
MinScore: 0,
MaxScore: 1000,
}
request.VecFields[0].Value = make([]byte, opt.D*4)
_, _ = opt.FeatureMmap.ReadAt(request.VecFields[0].Value, int64(0*opt.D)*4)
//for i := 0; i < opt.D; i++ {
// a := Float64frombytes(request.VecFields[0].Value[i*4:])
// fmt.Println(a)
//}
response := &vearchpb.SearchResponse{}
reqByte := gamma.SearchRequestSerialize(request)
respByte, status := gamma.Search(opt.Engine, reqByte)
if status.Code != 0 {
fmt.Printf("code %v\n", status.Code)
return
}
gamma.DeSerialize(respByte, response)
fmt.Printf("%v\n", *(response.Results[0]))
}
func main() {
Init()
defer gamma.Close(opt.Engine)
//Load()
//return
fmt.Println(opt.FeatureFile)
CreteTable()
Add()
GetDoc()
time.Sleep(time.Duration(1) * time.Second)
var status gamma.EngineStatus
for status.IndexStatus != 2 {
gamma.GetEngineStatus(opt.Engine, &status)
}
Search()
Dump()
opt.FeatureMmap.Close()
}