-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_cache.go
160 lines (141 loc) · 3.99 KB
/
badger_cache.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
package storage
import (
"encoding/binary"
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/dgraph-io/badger/v4"
)
const (
cachePrefixTransactionQueue = "CACHETRANSACTIONQUEUE"
cachePrefixTransactionOrder = "CACHETRANSACTIONORDER"
cachePrefixTransactionCache = "CACHETRANSACTIONPAYLOAD"
cachePrefixSnapshotNodeQueue = "SNAPSHOTNODEQUEUE"
cachePrefixSnapshotNodeMeta = "SNAPSHOTNODEMETA"
)
func (s *BadgerStore) CacheRetrieveTransactions(limit int) ([]*common.VersionedTransaction, error) {
var txs []*common.VersionedTransaction
err := s.cacheDB.Update(func(txn *badger.Txn) error {
prefix := []byte(cachePrefixTransactionQueue)
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
opts.Prefix = prefix
it := txn.NewIterator(opts)
defer it.Close()
var processed [][]byte
var hash crypto.Hash
filter := make(map[crypto.Hash]bool)
it.Seek(cacheTransactionQueueKey(0, hash))
for ; len(txs) < limit && it.Valid(); it.Next() {
key := it.Item().KeyCopy(nil)
copy(hash[:], key[len(cachePrefixTransactionQueue)+8:])
processed = append(processed, key)
processed = append(processed, cacheTransactionOrderKey(hash))
if filter[hash] {
continue
}
filter[hash] = true
ver, err := s.cacheReadTransaction(txn, hash)
if err != nil {
return err
}
if ver != nil {
txs = append(txs, ver)
}
}
for _, k := range processed {
err := txn.Delete(k)
if err != nil {
return err
}
}
return nil
})
return txs, err
}
func (s *BadgerStore) CacheRemoveTransactions(hashes []crypto.Hash) error {
batch := 100
for {
err := s.cacheDB.Update(func(txn *badger.Txn) error {
for i := range hashes {
key := cacheTransactionCacheKey(hashes[i])
err := txn.Delete(key)
if err != nil {
return err
}
key = cacheTransactionOrderKey(hashes[i])
err = txn.Delete(key)
if err != nil {
return err
}
if i == batch {
break
}
}
return nil
})
if err != nil || len(hashes) <= batch {
return err
}
hashes = hashes[batch:]
}
}
func (s *BadgerStore) CachePutTransaction(tx *common.VersionedTransaction) error {
txn := s.cacheDB.NewTransaction(true)
defer txn.Discard()
hash := tx.PayloadHash()
key := cacheTransactionOrderKey(hash)
_, err := txn.Get(key)
if err == nil {
return nil
}
etr := badger.NewEntry(key, []byte{}).WithTTL(time.Duration(s.custom.Node.CacheTTL) * time.Second)
err = txn.SetEntry(etr)
if err != nil {
return err
}
key = cacheTransactionCacheKey(hash)
val := tx.CompressMarshal()
etr = badger.NewEntry(key, val).WithTTL(time.Duration(s.custom.Node.CacheTTL+60) * time.Second)
err = txn.SetEntry(etr)
if err != nil {
return err
}
key = cacheTransactionQueueKey(uint64(time.Now().UnixNano()), hash)
etr = badger.NewEntry(key, []byte{}).WithTTL(time.Duration(s.custom.Node.CacheTTL) * time.Second)
err = txn.SetEntry(etr)
if err != nil {
return err
}
return txn.Commit()
}
func (s *BadgerStore) CacheGetTransaction(hash crypto.Hash) (*common.VersionedTransaction, error) {
txn := s.cacheDB.NewTransaction(false)
defer txn.Discard()
return s.cacheReadTransaction(txn, hash)
}
func (s *BadgerStore) cacheReadTransaction(txn *badger.Txn, tx crypto.Hash) (*common.VersionedTransaction, error) {
key := cacheTransactionCacheKey(tx)
item, err := txn.Get(key)
if err == badger.ErrKeyNotFound {
return nil, nil
} else if err != nil {
return nil, err
}
val, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
return common.DecompressUnmarshalVersionedTransaction(val)
}
func cacheTransactionCacheKey(hash crypto.Hash) []byte {
return append([]byte(cachePrefixTransactionCache), hash[:]...)
}
func cacheTransactionQueueKey(ts uint64, hash crypto.Hash) []byte {
key := []byte(cachePrefixTransactionQueue)
key = binary.BigEndian.AppendUint64(key, ts)
return append(key, hash[:]...)
}
func cacheTransactionOrderKey(hash crypto.Hash) []byte {
return append([]byte(cachePrefixTransactionOrder), hash[:]...)
}