/
store_heads.go
108 lines (89 loc) · 2.29 KB
/
store_heads.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
package gossip
import (
"bytes"
"sort"
"github.com/metatechchain/meta-base/hash"
"github.com/metatechchain/meta-base/inter/idx"
"github.com/metatechchain/go-metatechchain/utils/concurrent"
)
type sortedHead []byte
func (es *epochStore) getCachedHeads() (*concurrent.EventsSet, bool) {
cache := es.cache.Heads.Load()
if cache != nil {
return cache.(*concurrent.EventsSet), true
}
return nil, false
}
func (es *epochStore) loadHeads() *concurrent.EventsSet {
res := make(hash.EventsSet, 100)
b, err := es.table.Heads.Get([]byte{})
if err != nil {
es.Log.Crit("Failed to get key-value", "err", err)
}
if b == nil {
return concurrent.WrapEventsSet(res)
}
for i := 0; i < len(b); i += 32 {
res.Add(hash.BytesToEvent(b[i : i+32]))
}
return concurrent.WrapEventsSet(res)
}
func (es *epochStore) GetHeads() *concurrent.EventsSet {
cached, ok := es.getCachedHeads()
if ok {
return cached
}
heads := es.loadHeads()
if heads == nil {
heads = &concurrent.EventsSet{}
}
es.cache.Heads.Store(heads)
return heads
}
func (es *epochStore) SetHeads(ids *concurrent.EventsSet) {
es.cache.Heads.Store(ids)
}
func (es *epochStore) FlushHeads() {
ids, ok := es.getCachedHeads()
if !ok {
return
}
// sort values for determinism
sortedHeads := make([]sortedHead, 0, len(ids.Val))
for id := range ids.Val {
sortedHeads = append(sortedHeads, id.Bytes())
}
sort.Slice(sortedHeads, func(i, j int) bool {
a, b := sortedHeads[i], sortedHeads[j]
return bytes.Compare(a, b) < 0
})
b := make([]byte, 0, len(sortedHeads)*32)
for _, head := range sortedHeads {
b = append(b, head...)
}
if err := es.table.Heads.Put([]byte{}, b); err != nil {
es.Log.Crit("Failed to put key-value", "err", err)
}
}
// GetHeadsSlice returns IDs of all the epoch events with no descendants
func (s *Store) GetHeadsSlice(epoch idx.Epoch) hash.Events {
heads := s.GetHeads(epoch)
heads.RLock()
defer heads.RUnlock()
return heads.Val.Slice()
}
// GetHeads returns set of all the epoch event IDs with no descendants
func (s *Store) GetHeads(epoch idx.Epoch) *concurrent.EventsSet {
es := s.getEpochStore(epoch)
if es == nil {
return nil
}
return es.GetHeads()
}
func (s *Store) SetHeads(epoch idx.Epoch, ids *concurrent.EventsSet) {
es := s.getEpochStore(epoch)
if es == nil {
return
}
es.SetHeads(ids)
}