-
Notifications
You must be signed in to change notification settings - Fork 6
/
vectodb_multi.go
234 lines (213 loc) · 4.78 KB
/
vectodb_multi.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
package vectodb
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"sync/atomic"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
/**
* VectodbMulti consists of multiple VectoDB instances on the same machine.
* It's the POC of the VectoDB cluster which involves multiple machines.
*/
type VectodbMulti struct {
//configurations
dim int
metricType int
indexKey string
queryParams string
distThr float32
workDir string //the working directory of each VectoDB instance is <workDir>/vdb-<seq>
sizeLimit int //size limit of each VectoDB instance
//state
curXidBatch int64
maxSeq int
vdbs []*VectoDB
cancel context.CancelFunc
}
func MinInt(x, y int) int {
if x < y {
return x
}
return y
}
func MaxInt(x, y int) int {
if x > y {
return x
}
return y
}
func getWorkDir(seq int) string {
return fmt.Sprintf("vdb-%d", seq)
}
func VectodbMultiClearWorkDir(workDir string) (err error) {
re := regexp.MustCompile("vdb-(?P<seq>[0-9]+)")
entries, err := ioutil.ReadDir(workDir)
if err != nil && !os.IsNotExist(err) {
err = errors.Wrap(err, "")
return
}
for _, entry := range entries {
subs := re.FindStringSubmatch(entry.Name())
if subs == nil {
continue
}
dp := filepath.Join(workDir, subs[0])
if err = os.RemoveAll(dp); err != nil {
return
}
}
return
}
func NewVectodbMulti(workDir string, dim int, sizeLimit int) (vm *VectodbMulti, err error) {
vm = &VectodbMulti{
dim: dim,
workDir: workDir,
sizeLimit: sizeLimit,
curXidBatch: 0,
}
if err = os.MkdirAll(workDir, 0700); err != nil {
err = errors.Wrap(err, "")
return
}
seqs := make([]int, 0)
re := regexp.MustCompile("vdb-(?P<seq>[0-9]+)")
entries, err := ioutil.ReadDir(workDir)
if err != nil {
err = errors.Wrap(err, "")
return
}
var seq int
for _, entry := range entries {
subs := re.FindStringSubmatch(entry.Name())
if subs == nil {
continue
}
if seq, err = strconv.Atoi(subs[1]); err != nil {
return
}
seqs = append(seqs, seq)
}
var vdb *VectoDB
if len(seqs) == 0 {
seqs = append(seqs, 0)
}
sort.Ints(seqs)
for _, seq := range seqs {
dp := filepath.Join(workDir, getWorkDir(seq))
vdb, err = NewVectoDB(dp, dim)
vm.vdbs = append(vm.vdbs, vdb)
}
vm.maxSeq = seqs[len(seqs)-1]
return
}
//Search perform batch search
/**
* nq number of query points, shall be equal to len(xids)
* k kNN
* xq query points
*/
func (vm *VectodbMulti) Search(nq, k int, xq []float32) (res [][]XidScore, err error) {
res = make([][]XidScore, nq)
var res2 [][]XidScore
for _, vdb := range vm.vdbs {
if res2, err = vdb.Search(k, xq, nil); err != nil {
return
}
for i := 0; i < nq; i++ {
res[i] = append(res[i], res2[i]...)
}
}
for i := 0; i < nq; i++ {
sort.Slice(res[i], func(i1, i2 int) bool { return res[i][i1].Score > res[i][i2].Score })
}
return
}
//AddWithIds add vectors
/**
* nb number of vectors, shall be equal to len(xids)
* xb vectors
* xids vector identifiers
*/
func (vm *VectodbMulti) AddWithIds(xb []float32, xids []int64) (err error) {
var quota, total, added int
nb := len(xids)
vdb := vm.vdbs[len(vm.vdbs)-1]
for added < nb {
if total, err = vdb.GetTotal(); err != nil {
return
}
quota = vm.sizeLimit - total
if quota > 0 {
batch := MinInt(quota, nb-added)
if err = vdb.AddWithIds(xb[added*vm.dim:(added+batch)*vm.dim], xids[added:added+batch]); err != nil {
return
}
added += batch
} else {
vm.maxSeq++
dp := filepath.Join(vm.workDir, getWorkDir(vm.maxSeq))
if vdb, err = NewVectoDB(dp, vm.dim); err != nil {
return
}
vm.vdbs = append(vm.vdbs, vdb)
}
}
return
}
//StartBuilderLoop starts a goroutine to build index in loop
func (vm *VectodbMulti) StartBuilderLoop() {
if vm.cancel != nil {
return
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
ticker := time.Tick(2 * time.Second)
var err error
for {
select {
case <-ctx.Done():
return
case <-ticker:
log.Infof("build iteration begin")
vdbs := vm.vdbs
for _, vdb := range vdbs {
if err = vdb.SyncIndex(); err != nil {
log.Fatalf("%+v", err)
}
// sleep a while to avoid busy loop
select {
case <-ctx.Done():
return
case <-ticker:
}
}
log.Infof("build iteration done")
}
}
}()
vm.cancel = cancel
return
}
//StopBuilderLoop stops the build goroutinep
func (vm *VectodbMulti) StopBuilderLoop() {
if vm.cancel == nil {
return
}
vm.cancel()
vm.cancel = nil
return
}
//AllocateIds allocate a batch of identifiers. The batch size is 2<<20.
func (vm *VectodbMulti) AllocateIds() (xidBegin int64, err error) {
xidBatch := atomic.AddInt64(&vm.curXidBatch, int64(1)) - 1
xidBegin = xidBatch * int64(2<<20)
return
}