-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_cache.go
103 lines (91 loc) · 2.46 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
package storage
import (
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/dgraph-io/badger/v3"
)
const (
cachePrefixTransactionCache = "TRANSACTIONCACHE"
cachePrefixSnapshotNodeQueue = "SNAPSHOTNODEQUEUE"
cachePrefixSnapshotNodeMeta = "SNAPSHOTNODEMETA"
)
func (s *BadgerStore) CacheListTransactions(offset crypto.Hash, limit int) ([]*common.VersionedTransaction, error) {
txn := s.cacheDB.NewTransaction(false)
defer txn.Discard()
prefix := []byte(cachePrefixTransactionCache)
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
opts.Prefix = prefix
it := txn.NewIterator(opts)
defer it.Close()
var txs []*common.VersionedTransaction
it.Seek(cacheTransactionCacheKey(offset))
for ; len(txs) < limit && it.Valid(); it.Next() {
err := it.Item().Value(func(v []byte) error {
ver, err := common.DecompressUnmarshalVersionedTransaction(v)
if err != nil {
return err
}
txs = append(txs, ver)
return nil
})
if err != nil {
return nil, err
}
}
return txs, nil
}
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
}
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()
key := cacheTransactionCacheKey(tx.PayloadHash())
val := tx.CompressMarshal()
etr := badger.NewEntry(key, val).WithTTL(time.Duration(s.custom.Node.CacheTTL) * time.Second * 8)
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()
key := cacheTransactionCacheKey(hash)
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[:]...)
}