-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_space.go
137 lines (117 loc) · 3.67 KB
/
badger_space.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
package storage
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/crypto"
"github.com/dgraph-io/badger/v4"
)
func (s *BadgerStore) ListAggregatedRoundSpaceCheckpoints(cids []crypto.Hash) (map[crypto.Hash]*common.RoundSpace, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
spaces := make(map[crypto.Hash]*common.RoundSpace)
for _, id := range cids {
batch, round, err := readRoundSpaceCheckpoint(txn, id)
if err != nil {
return nil, err
}
spaces[id] = &common.RoundSpace{
NodeId: id,
Batch: batch,
Round: round,
}
}
return spaces, nil
}
func (s *BadgerStore) ReadNodeRoundSpacesForBatch(nodeId crypto.Hash, batch uint64) ([]*common.RoundSpace, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
var spaces []*common.RoundSpace
key := graphSpaceQueueKey(nodeId, batch, 0)
prefix := key[:len(key)-8]
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
opts.Prefix = prefix
it := txn.NewIterator(opts)
defer it.Close()
for it.Seek(key); it.Valid(); it.Next() {
item := it.Item()
val, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
off := len(graphPrefixSpaceQueue)
if bytes.Compare(nodeId[:], item.Key()[off:off+32]) != 0 {
panic(nodeId)
}
if binary.BigEndian.Uint64(item.Key()[off+32:off+40]) != batch {
panic(batch)
}
space := &common.RoundSpace{
NodeId: nodeId,
Batch: batch,
Round: binary.BigEndian.Uint64(item.Key()[off+40:]),
Duration: binary.BigEndian.Uint64(val),
}
spaces = append(spaces, space)
}
return spaces, nil
}
func (s *BadgerStore) ReadRoundSpaceCheckpoint(nodeId crypto.Hash) (uint64, uint64, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
return readRoundSpaceCheckpoint(txn, nodeId)
}
func (s *BadgerStore) WriteRoundSpaceAndState(space *common.RoundSpace) error {
return s.snapshotsDB.Update(func(txn *badger.Txn) error {
ob, or, err := readRoundSpaceCheckpoint(txn, space.NodeId)
if err != nil {
return err
}
if ob > space.Batch || or > space.Round {
panic(fmt.Errorf("WriteRoundSpaceAndState(%v) => invalid round %d:%d", space, ob, or))
}
key := graphSpaceCheckpointKey(space.NodeId)
val := binary.BigEndian.AppendUint64(nil, space.Batch)
val = binary.BigEndian.AppendUint64(val, space.Round)
err = txn.Set(key, val)
if err != nil || space.Duration == 0 {
return err
}
if space.Round == 0 {
panic(fmt.Errorf("WriteRoundSpaceAndState(%v) => first accepted round", space))
}
if space.Duration < uint64(config.CheckpointDuration) {
panic(fmt.Errorf("WriteRoundSpaceAndState(%v) => invalid space", space))
}
key = graphSpaceQueueKey(space.NodeId, space.Batch, space.Round)
val = binary.BigEndian.AppendUint64(nil, space.Duration)
return txn.Set(key, val)
})
}
func readRoundSpaceCheckpoint(txn *badger.Txn, nodeId crypto.Hash) (uint64, uint64, error) {
key := graphSpaceCheckpointKey(nodeId)
item, err := txn.Get(key)
if err == badger.ErrKeyNotFound {
return 0, 0, nil
} else if err != nil {
return 0, 0, err
}
val, err := item.ValueCopy(nil)
if err != nil {
return 0, 0, err
}
batch := binary.BigEndian.Uint64(val[:8])
round := binary.BigEndian.Uint64(val[8:16])
return batch, round, nil
}
func graphSpaceQueueKey(nodeId crypto.Hash, batch, round uint64) []byte {
key := append([]byte(graphPrefixSpaceQueue), nodeId[:]...)
key = binary.BigEndian.AppendUint64(key, batch)
return binary.BigEndian.AppendUint64(key, round)
}
func graphSpaceCheckpointKey(nodeId crypto.Hash) []byte {
return append([]byte(graphPrefixSpaceCheckpoint), nodeId[:]...)
}