-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_topology.go
104 lines (88 loc) · 2.75 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
package storage
import (
"encoding/binary"
"github.com/MixinNetwork/mixin/common"
"github.com/dgraph-io/badger"
"github.com/vmihailenco/msgpack"
)
func (s *BadgerStore) ReadSnapshotWithTransactionsSinceTopology(topologyOffset, count uint64) ([]*common.SnapshotWithTopologicalOrder, []*common.Transaction, error) {
snapshots, err := s.ReadSnapshotsSinceTopology(topologyOffset, count)
if err != nil {
return nil, nil, err
}
transactions := make([]*common.Transaction, len(snapshots))
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
for i, s := range snapshots {
tx, err := readTransaction(txn, s.Transaction)
if err != nil {
return nil, nil, err
}
transactions[i] = &tx.Transaction
}
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()
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
prefix := []byte(graphPrefixTopology)
it.Seek(graphTopologyKey(topologyOffset))
for ; it.ValidForPrefix(prefix) && uint64(len(snapshots)) < count; it.Next() {
item := it.Item()
v, err := item.ValueCopy(nil)
if err != nil {
return snapshots, err
}
topology := graphTopologyOrder(item.Key())
item, err = txn.Get(v)
if err != nil {
return snapshots, err
}
v, err = item.ValueCopy(nil)
if err != nil {
return snapshots, err
}
var snap common.SnapshotWithTopologicalOrder
err = msgpack.Unmarshal(v, &snap)
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 {
var sequence 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)) {
item := it.Item()
sequence = graphTopologyOrder(item.Key()) + 1
}
return sequence
}
func writeTopology(txn *badger.Txn, snap *common.SnapshotWithTopologicalOrder) error {
key := graphTopologyKey(snap.TopologicalOrder)
val := graphSnapshotKey(snap.NodeId, snap.RoundNumber, snap.Transaction)
return txn.Set(key, val[:])
}
func graphTopologyKey(order uint64) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, order)
return append([]byte(graphPrefixTopology), buf...)
}
func graphTopologyOrder(key []byte) uint64 {
order := key[len(graphPrefixTopology):]
return binary.BigEndian.Uint64(order)
}