-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch.go
93 lines (77 loc) · 2.08 KB
/
batch.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
package pebble
import (
"sync"
"github.com/Taraxa-project/taraxa-indexer/internal/storage"
"github.com/cockroachdb/pebble"
"github.com/ethereum/go-ethereum/rlp"
log "github.com/sirupsen/logrus"
)
type Batch struct {
*pebble.Batch
Mutex *sync.RWMutex
}
func (b *Batch) CommitBatch() {
b.Mutex.Lock()
defer b.Mutex.Unlock()
err := b.Commit(pebble.NoSync)
if err != nil {
log.WithError(err).Fatal("CommitBatch failed")
}
}
func (b *Batch) SetTotalSupply(s *storage.TotalSupply) {
err := b.AddToBatchFullKey(s, []byte(GetPrefix((*storage.TotalSupply)(s))))
if err != nil {
log.WithError(err).Fatal("SetTotalSupply failed")
}
}
func (b *Batch) SetFinalizationData(f *storage.FinalizationData) {
err := b.AddToBatchFullKey(f, []byte(GetPrefix(f)))
if err != nil {
log.WithError(err).Fatal("SetFinalizationData failed")
}
}
func (b *Batch) SetGenesisHash(h storage.GenesisHash) {
err := b.AddToBatchFullKey(&h, []byte(GetPrefix(&h)))
if err != nil {
log.WithError(err).Fatal("SetGenesisHash failed")
}
}
func (b *Batch) UpdateWeekStats(w storage.WeekStats) {
err := b.AddToBatchFullKey(&w, w.Key)
if err != nil {
log.WithError(err).Fatal("UpdateWeekStats failed")
}
}
func (b *Batch) SaveAccounts(a *storage.Balances) {
a.SortByBalanceDescending()
b.AddToBatchSingleKey(a.Accounts, "")
}
func (b *Batch) AddToBatch(o interface{}, key1 string, key2 uint64) {
err := b.AddToBatchFullKey(o, getKey(GetPrefix(o), key1, key2))
if err != nil {
log.WithError(err).Fatal("AddToBatch failed")
}
}
func (b *Batch) AddToBatchSingleKey(o interface{}, key string) {
err := b.AddToBatchFullKey(o, getPrefixKey(GetPrefix(o), key))
if err != nil {
log.WithError(err).Fatal("AddToBatchSingleKey failed")
}
}
func (b *Batch) AddToBatchFullKey(o interface{}, key []byte) error {
b.Mutex.Lock()
defer b.Mutex.Unlock()
data, err := rlp.EncodeToBytes(o)
if err != nil {
return err
}
return b.Set(key, data, nil)
}
func (b *Batch) Remove(key []byte) {
b.Mutex.Lock()
defer b.Mutex.Unlock()
err := b.Delete(key, nil)
if err != nil {
log.WithError(err).Fatal("Remove failed")
}
}