-
Notifications
You must be signed in to change notification settings - Fork 179
/
modifiers.go
57 lines (50 loc) · 1.27 KB
/
modifiers.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
package operation
import (
"errors"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/storage/badger/transaction"
)
func SkipDuplicates(op func(*badger.Txn) error) func(tx *badger.Txn) error {
return func(tx *badger.Txn) error {
err := op(tx)
if errors.Is(err, storage.ErrAlreadyExists) {
metrics.GetStorageCollector().SkipDuplicate()
return nil
}
return err
}
}
func SkipNonExist(op func(*badger.Txn) error) func(tx *badger.Txn) error {
return func(tx *badger.Txn) error {
err := op(tx)
if errors.Is(err, badger.ErrKeyNotFound) {
return nil
}
if errors.Is(err, storage.ErrNotFound) {
return nil
}
return err
}
}
func RetryOnConflict(action func(func(*badger.Txn) error) error, op func(tx *badger.Txn) error) error {
for {
err := action(op)
if errors.Is(err, badger.ErrConflict) {
metrics.GetStorageCollector().RetryOnConflict()
continue
}
return err
}
}
func RetryOnConflictTx(db *badger.DB, action func(*badger.DB, func(*transaction.Tx) error) error, op func(*transaction.Tx) error) error {
for {
err := action(db, op)
if errors.Is(err, badger.ErrConflict) {
metrics.GetStorageCollector().RetryOnConflict()
continue
}
return err
}
}