forked from barakmich/bbqvec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
56 lines (47 loc) · 1.13 KB
/
backend.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
package bbq
import (
"errors"
"github.com/RoaringBitmap/roaring"
)
type VectorBackend interface {
PutVector(id ID, v Vector) error
ComputeSimilarity(targetVector Vector, targetID ID) (float32, error)
Info() BackendInfo
Exists(id ID) bool
Close() error
}
type scannableBackend interface {
VectorBackend
ForEachVector(func(ID) error) error
}
type VectorGetter[T any] interface {
GetVector(id ID) (T, error)
}
type IndexBackend interface {
SaveBases(bases []Basis, token uint64) (uint64, error)
LoadBases() ([]Basis, error)
SaveBitmap(basis int, index int, bitmap *roaring.Bitmap) error
LoadBitmap(basis, index int) (*roaring.Bitmap, error)
Sync() error
}
type BackendInfo struct {
HasIndexData bool
Dimensions int
Quantization string
}
func FullTableScanSearch(be VectorBackend, target Vector, k int) (*ResultSet, error) {
rs := NewResultSet(k)
b, ok := be.(scannableBackend)
if !ok {
return nil, errors.New("Backend is incompatible")
}
err := b.ForEachVector(func(id ID) error {
sim, err := b.ComputeSimilarity(target, id)
if err != nil {
return err
}
rs.AddResult(id, sim)
return nil
})
return rs, err
}