-
Notifications
You must be signed in to change notification settings - Fork 178
/
epoch_setups.go
64 lines (53 loc) · 1.61 KB
/
epoch_setups.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
package badger
import (
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/storage/badger/operation"
)
type EpochSetups struct {
db *badger.DB
cache *Cache
}
func NewEpochSetups(collector module.CacheMetrics, db *badger.DB) *EpochSetups {
store := func(key interface{}, val interface{}) func(*badger.Txn) error {
id := key.(flow.Identifier)
setup := val.(*flow.EpochSetup)
return operation.InsertEpochSetup(id, setup)
}
retrieve := func(key interface{}) func(*badger.Txn) (interface{}, error) {
id := key.(flow.Identifier)
var setup flow.EpochSetup
return func(tx *badger.Txn) (interface{}, error) {
err := operation.RetrieveEpochSetup(id, &setup)(tx)
return &setup, err
}
}
es := &EpochSetups{
db: db,
cache: newCache(collector,
withLimit(4*flow.DefaultTransactionExpiry),
withStore(store),
withRetrieve(retrieve),
withResource(metrics.ResourceEpochSetup)),
}
return es
}
func (es *EpochSetups) StoreTx(setup *flow.EpochSetup) func(tx *badger.Txn) error {
return es.cache.Put(setup.ID(), setup)
}
func (es *EpochSetups) retrieveTx(setupID flow.Identifier) func(tx *badger.Txn) (*flow.EpochSetup, error) {
return func(tx *badger.Txn) (*flow.EpochSetup, error) {
val, err := es.cache.Get(setupID)(tx)
if err != nil {
return nil, err
}
return val.(*flow.EpochSetup), nil
}
}
func (es *EpochSetups) ByID(setupID flow.Identifier) (*flow.EpochSetup, error) {
tx := es.db.NewTransaction(false)
defer tx.Discard()
return es.retrieveTx(setupID)(tx)
}