-
Notifications
You must be signed in to change notification settings - Fork 199
/
common.go
103 lines (86 loc) · 2.04 KB
/
common.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 leveldb
import (
"fmt"
"time"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/opt"
)
const resourceUnavailable = "resource temporarily unavailable"
const maxRetries = 10
const timeBetweenRetries = time.Second
func openLevelDB(path string, options *opt.Options) (*leveldb.DB, error) {
retries := 0
for {
db, err := openOneTime(path, options)
if err == nil {
return db, nil
}
if err.Error() != resourceUnavailable {
return nil, err
}
log.Debug("error opening DB",
"error", err,
"path", path,
"retry", retries,
)
time.Sleep(timeBetweenRetries)
retries++
if retries > maxRetries {
return nil, fmt.Errorf("%w, retried %d number of times", err, maxRetries)
}
}
}
func openOneTime(path string, options *opt.Options) (*leveldb.DB, error) {
db, errOpen := leveldb.OpenFile(path, options)
if errOpen == nil {
return db, nil
}
if errors.IsCorrupted(errOpen) {
var errRecover error
log.Warn("corrupted DB file",
"path", path,
"error", errOpen,
)
db, errRecover = leveldb.RecoverFile(path, options)
if errRecover != nil {
return nil, fmt.Errorf("%w while recovering DB %s, after the initial failure %s",
errRecover,
path,
errOpen.Error(),
)
}
log.Info("DB file recovered",
"path", path,
)
return db, nil
}
return nil, errOpen
}
type baseLevelDb struct {
db *leveldb.DB
}
// RangeKeys will call the handler function for each (key, value) pair
// If the handler returns true, the iteration will continue, otherwise will stop
func (bldb *baseLevelDb) RangeKeys(handler func(key []byte, value []byte) bool) {
if handler == nil {
return
}
iterator := bldb.db.NewIterator(nil, nil)
for {
if !iterator.Next() {
break
}
key := iterator.Key()
clonedKey := make([]byte, len(key))
copy(clonedKey, key)
val := iterator.Value()
clonedVal := make([]byte, len(val))
copy(clonedVal, val)
shouldContinue := handler(clonedKey, clonedVal)
if !shouldContinue {
break
}
}
iterator.Release()
}