-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectoria.go
266 lines (198 loc) · 5.42 KB
/
vectoria.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
// Package vectoria provides an embedded vector database for simple use cases.
package vectoria
import (
"fmt"
"sync"
"github.com/google/uuid"
"github.com/mastrasec/vectoria/internal/lsh"
"github.com/mastrasec/vectoria/internal/storage"
"golang.org/x/exp/maps"
)
type DB struct {
log bool
stg storage.Contract
// index ID -> index pointer
indexRef safeMap
// Keeps track of option functions called in Open to avoid duplication
called map[string]bool
}
type Options func(*DB) error
func newDB(stg storage.Contract) *DB {
db := &DB{
stg: stg,
called: make(map[string]bool),
}
db.indexRef.make()
return db
}
type safeMap struct {
mu sync.RWMutex
items map[string]index
}
func (sm *safeMap) make() {
sm.items = make(map[string]index)
}
func (sm *safeMap) add(key string, value index) {
sm.mu.Lock()
defer sm.mu.Unlock()
sm.items[key] = value
}
func (sm *safeMap) get(key string) (index, bool) {
sm.mu.RLock()
defer sm.mu.RUnlock()
idx, ok := sm.items[key] // Compiler does not allow direct return
return idx, ok
}
func (sm *safeMap) del(key string) {
sm.mu.Lock()
defer sm.mu.Unlock()
delete(sm.items, key)
}
func (sm *safeMap) keyExists(key string) bool {
sm.mu.RLock()
defer sm.mu.RUnlock()
_, exists := sm.items[key]
return exists
}
func (sm *safeMap) len() int {
return len(sm.items)
}
// =================================== API ===================================
type DBConfig struct {
Path string
log bool
LSH []LSHConfig
}
func (conf LSHConfig) indexName() string {
return conf.IndexName
}
func New(config DBConfig) (db *DB, err error) {
stg, err := storage.New(config.Path)
if err != nil {
return nil, err
}
db = newDB(stg)
if err := db.addLSH(config.LSH...); err != nil {
return nil, err
}
return db, nil
}
func (db *DB) addLSH(configs ...LSHConfig) error {
for i, config := range configs {
if exists := db.indexExists(config.IndexName); exists {
db.addLSHRollback(configs[:i+1]...)
return &indexAlreadyExistsError{config.IndexName}
}
if config.IndexName == "" {
config.IndexName = uuid.NewString()
}
locality, err := lsh.New(config.IndexName, db.stg, config.NumRounds, config.NumHyperPlanes, config.SpaceDim)
if err != nil {
return err
}
db.indexRef.add(config.IndexName, &lshIndex{locality: locality})
}
return nil
}
func (db *DB) addLSHRollback(configs ...LSHConfig) {
for _, config := range configs {
db.indexRef.del(config.IndexName)
}
}
func (db *DB) Indexes() []string {
return maps.Keys(db.indexRef.items)
}
func (db *DB) NumIndexes() uint32 {
return uint32(len(db.Indexes()))
}
// TODO: rollback on err
func (db *DB) Add(itemID string, itemVec []float64, indexNames ...string) error {
if db.NumIndexes() == 0 {
return &dbHasNoIndexError{}
}
if len(indexNames) == 0 {
indexNames = db.Indexes()
}
for _, indexName := range indexNames {
idx, ok := db.indexRef.get(indexName)
if !ok {
return &indexDoesNotExistError{name: indexName}
}
if err := idx.add(itemID, itemVec); err != nil {
return err
}
}
return nil
}
func (db *DB) Get(queryVec []float64, threshold float64, k uint32, indexNames ...string) (res map[string][]string, err error) {
if len(indexNames) == 0 {
indexNames = db.Indexes()
}
res = make(map[string][]string, len(indexNames))
for _, indexName := range indexNames {
idx, ok := db.indexRef.get(indexName)
if !ok {
return nil, &indexDoesNotExistError{name: indexName}
}
ids, err := idx.get(queryVec, threshold, k)
if err != nil {
return nil, err
}
res[indexName] = ids
}
return res, nil
}
func (db *DB) clean(itemID string, indexNames ...string) {
// TODO: delete all
}
func (db *DB) indexExists(indexName string) bool {
return db.indexRef.keyExists(indexName)
}
// =================================== INDEXES ===================================
type index interface {
add(itemID string, itemVec []float64) error
get(queryVec []float64, threshold float64, k uint32) (ids []string, err error)
info() map[string]any
}
type LSHConfig struct {
IndexName string `json:"index_name"`
// Number of rounds. More rounds improve quality, but also adds computation overhead.
// It must be at least 1.
// If invalid value is given, default value is used.
NumRounds uint32 `json:"num_rounds"`
// Number of hyperplanes to split the space on. It must be at least 1.
// If invalid value is given, default value is used.
NumHyperPlanes uint32 `json:"num_hyper_planes"`
// Dimension of the space (vector length). It must be at least 2.
// If invalid value is given, default value is used.
SpaceDim uint32 `json:"space_dim"`
}
type lshIndex struct {
locality *lsh.LSH
}
func (l *lshIndex) add(itemID string, itemVec []float64) error {
return l.locality.Add(itemID, itemVec)
}
func (l *lshIndex) get(queryVec []float64, threshold float64, k uint32) (ids []string, err error) {
return l.locality.Get(queryVec, threshold, k)
}
func (l *lshIndex) info() map[string]any {
return l.locality.Info()
}
// =================================== ERRORS ===================================
type indexAlreadyExistsError struct {
name string
}
func (e *indexAlreadyExistsError) Error() string {
return fmt.Sprintf("index %s already exists.", e.name)
}
type indexDoesNotExistError struct {
name string
}
func (e *indexDoesNotExistError) Error() string {
return fmt.Sprintf("index %s does not exist.", e.name)
}
type dbHasNoIndexError struct{}
func (e *dbHasNoIndexError) Error() string {
return "database has no index."
}