-
Notifications
You must be signed in to change notification settings - Fork 4
/
backend_memory.go
94 lines (81 loc) · 1.8 KB
/
backend_memory.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
package bbq
import (
"errors"
"math/rand"
"time"
)
type MemoryBackend struct {
vecs []Vector
rng *rand.Rand
dim int
}
var _ scannableBackend = &MemoryBackend{}
var _ VectorGetter[Vector] = &MemoryBackend{}
func NewMemoryBackend(dimensions int) *MemoryBackend {
return &MemoryBackend{
rng: rand.New(rand.NewSource(time.Now().UnixMicro())),
dim: dimensions,
}
}
func (mem *MemoryBackend) Close() error {
return nil
}
func (mem *MemoryBackend) PutVector(id ID, vector Vector) error {
if len(vector) != mem.dim {
return errors.New("MemoryBackend: vector dimension doesn't match")
}
if int(id) < len(mem.vecs) {
mem.vecs[int(id)] = vector
} else if int(id) == len(mem.vecs) {
mem.vecs = append(mem.vecs, vector)
} else {
mem.grow(int(id))
mem.vecs[int(id)] = vector
}
return nil
}
func (mem *MemoryBackend) grow(to int) {
diff := (to - len(mem.vecs)) + 1
mem.vecs = append(mem.vecs, make([]Vector, diff)...)
}
func (mem *MemoryBackend) ComputeSimilarity(vector Vector, targetID ID) (float32, error) {
target, err := mem.GetVector(targetID)
if err != nil {
return 0, err
}
return target.CosineSimilarity(vector), nil
}
func (mem *MemoryBackend) Info() BackendInfo {
return BackendInfo{
HasIndexData: false,
Dimensions: mem.dim,
}
}
func (mem *MemoryBackend) GetVector(id ID) (Vector, error) {
if int(id) > len(mem.vecs)-1 {
return nil, ErrIDNotFound
}
if mem.vecs[int(id)] == nil {
return nil, ErrIDNotFound
}
return mem.vecs[int(id)], nil
}
func (mem *MemoryBackend) Exists(id ID) bool {
i := int(id)
if len(mem.vecs) <= i {
return false
}
return mem.vecs[i] != nil
}
func (mem *MemoryBackend) ForEachVector(cb func(ID) error) error {
for i, v := range mem.vecs {
if v == nil {
continue
}
err := cb(ID(i))
if err != nil {
return err
}
}
return nil
}