-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_topology.go
165 lines (142 loc) · 4.04 KB
/
badger_topology.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
package storage
import (
"encoding/binary"
"fmt"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/dgraph-io/badger/v4"
)
func (s *BadgerStore) ReadSnapshot(hash crypto.Hash) (*common.SnapshotWithTopologicalOrder, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
return readSnapshotWithTopo(txn, hash)
}
func readSnapshotWithTopo(txn *badger.Txn, hash crypto.Hash) (*common.SnapshotWithTopologicalOrder, error) {
item, err := txn.Get(graphSnapTopologyKey(hash))
if err == badger.ErrKeyNotFound {
return nil, nil
}
if err != nil {
return nil, err
}
topo, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
item, err = txn.Get(topo)
if err != nil {
return nil, err
}
key, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
item, err = txn.Get(key)
if err != nil {
return nil, err
}
v, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
snap, err := common.UnmarshalVersionedSnapshot(v)
if err != nil {
return nil, err
}
snap.Hash = hash
snap.TopologicalOrder = graphTopologyOrder(topo)
return snap, nil
}
func (s *BadgerStore) ReadSnapshotWithTransactionsSinceTopology(topologyOffset, count uint64) ([]*common.SnapshotWithTopologicalOrder, []*common.VersionedTransaction, error) {
if count > 500 {
return nil, nil, fmt.Errorf("count %d too large, the maximum is 500", count)
}
snapshots, err := s.ReadSnapshotsSinceTopology(topologyOffset, count)
if err != nil {
return nil, nil, err
}
transactions := make([]*common.VersionedTransaction, len(snapshots))
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
for i, s := range snapshots {
tx, err := readTransaction(txn, s.SoleTransaction())
if err != nil {
return nil, nil, err
}
transactions[i] = tx
}
return snapshots, transactions, nil
}
func (s *BadgerStore) ReadSnapshotsSinceTopology(topologyOffset, count uint64) ([]*common.SnapshotWithTopologicalOrder, error) {
snapshots := make([]*common.SnapshotWithTopologicalOrder, 0)
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
opts := badger.DefaultIteratorOptions
opts.Prefix = []byte(graphPrefixTopology)
it := txn.NewIterator(opts)
defer it.Close()
it.Seek(graphTopologyKey(topologyOffset))
for ; it.Valid() && uint64(len(snapshots)) < count; it.Next() {
item := it.Item()
v, err := item.ValueCopy(nil)
if err != nil {
return snapshots, err
}
topology := graphTopologyOrder(item.KeyCopy(nil))
item, err = txn.Get(v)
if err != nil {
return snapshots, err
}
v, err = item.ValueCopy(nil)
if err != nil {
return snapshots, err
}
snap, err := common.UnmarshalVersionedSnapshot(v)
if err != nil {
return snapshots, err
}
snap.Hash = snap.PayloadHash()
snap.TopologicalOrder = topology
snapshots = append(snapshots, snap)
}
return snapshots, nil
}
func (s *BadgerStore) TopologySequence() uint64 {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
opts.Reverse = true
it := txn.NewIterator(opts)
defer it.Close()
it.Seek(graphTopologyKey(^uint64(0)))
if it.ValidForPrefix([]byte(graphPrefixTopology)) {
key := it.Item().KeyCopy(nil)
return graphTopologyOrder(key)
}
return 0
}
func writeTopology(txn *badger.Txn, snap *common.SnapshotWithTopologicalOrder) error {
key := graphTopologyKey(snap.TopologicalOrder)
val := graphSnapshotKey(snap.NodeId, snap.RoundNumber, snap.SoleTransaction())
_, err := txn.Get(key)
if err != badger.ErrKeyNotFound {
panic(err)
}
err = txn.Set(key, val[:])
if err != nil {
return err
}
return txn.Set(graphSnapTopologyKey(snap.PayloadHash()), key)
}
func graphSnapTopologyKey(hash crypto.Hash) []byte {
return append([]byte(graphPrefixSnapTopology), hash[:]...)
}
func graphTopologyKey(order uint64) []byte {
key := []byte(graphPrefixTopology)
return binary.BigEndian.AppendUint64(key, order)
}
func graphTopologyOrder(key []byte) uint64 {
order := key[len(graphPrefixTopology):]
return binary.BigEndian.Uint64(order)
}