/
store_vectors.go
65 lines (54 loc) · 1.61 KB
/
store_vectors.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
package vecfc
import (
"github.com/Deepchain-foundation/lachesis-base/hash"
"github.com/Deepchain-foundation/lachesis-base/kvdb"
)
func (vi *Index) getBytes(table kvdb.Store, id hash.Event) []byte {
key := id.Bytes()
b, err := table.Get(key)
if err != nil {
vi.crit(err)
}
return b
}
func (vi *Index) setBytes(table kvdb.Store, id hash.Event, b []byte) {
key := id.Bytes()
err := table.Put(key, b)
if err != nil {
vi.crit(err)
}
}
// GetLowestAfter reads the vector from DB
func (vi *Index) GetLowestAfter(id hash.Event) *LowestAfterSeq {
if bVal, okGet := vi.cache.LowestAfterSeq.Get(id); okGet {
return bVal.(*LowestAfterSeq)
}
b := LowestAfterSeq(vi.getBytes(vi.table.LowestAfterSeq, id))
if b == nil {
return nil
}
vi.cache.LowestAfterSeq.Add(id, &b, uint(len(b)))
return &b
}
// GetHighestBefore reads the vector from DB
func (vi *Index) GetHighestBefore(id hash.Event) *HighestBeforeSeq {
if bVal, okGet := vi.cache.HighestBeforeSeq.Get(id); okGet {
return bVal.(*HighestBeforeSeq)
}
b := HighestBeforeSeq(vi.getBytes(vi.table.HighestBeforeSeq, id))
if b == nil {
return nil
}
vi.cache.HighestBeforeSeq.Add(id, &b, uint(len(b)))
return &b
}
// SetLowestAfter stores the vector into DB
func (vi *Index) SetLowestAfter(id hash.Event, seq *LowestAfterSeq) {
vi.setBytes(vi.table.LowestAfterSeq, id, *seq)
vi.cache.LowestAfterSeq.Add(id, seq, uint(len(*seq)))
}
// SetHighestBefore stores the vectors into DB
func (vi *Index) SetHighestBefore(id hash.Event, seq *HighestBeforeSeq) {
vi.setBytes(vi.table.HighestBeforeSeq, id, *seq)
vi.cache.HighestBeforeSeq.Add(id, seq, uint(len(*seq)))
}