-
Notifications
You must be signed in to change notification settings - Fork 202
/
common.go
106 lines (89 loc) · 2.55 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
104
105
106
package factory
import (
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/storage/storageUnit"
)
// GetCacherFromConfig will return the cache config needed for storage unit from a config came from the toml file
func GetCacherFromConfig(cfg config.CacheConfig) storageUnit.CacheConfig {
return storageUnit.CacheConfig{
Size: cfg.Size,
SizeInBytes: cfg.SizeInBytes,
Type: storageUnit.CacheType(cfg.Type),
Shards: cfg.Shards,
}
}
// GetDBFromConfig will return the db config needed for storage unit from a config came from the toml file
func GetDBFromConfig(cfg config.DBConfig) storageUnit.DBConfig {
return storageUnit.DBConfig{
Type: storageUnit.DBType(cfg.Type),
MaxBatchSize: cfg.MaxBatchSize,
BatchDelaySeconds: cfg.BatchDelaySeconds,
MaxOpenFiles: cfg.MaxOpenFiles,
}
}
// GetBloomFromConfig will return the bloom config needed for storage unit from a config came from the toml file
func GetBloomFromConfig(cfg config.BloomFilterConfig) storageUnit.BloomConfig {
var hashFuncs []storageUnit.HasherType
if cfg.HashFunc != nil {
hashFuncs = make([]storageUnit.HasherType, len(cfg.HashFunc))
idx := 0
for _, hf := range cfg.HashFunc {
hashFuncs[idx] = storageUnit.HasherType(hf)
idx++
}
}
return storageUnit.BloomConfig{
Size: cfg.Size,
HashFunc: hashFuncs,
}
}
// FindLastEpochFromStorage finds the last epoch by searching over the storage folders
// TODO: something similar should be done for determining the correct shardId
// when booting from storage with an epoch > 0 or add ShardId in boot storer
func FindLastEpochFromStorage(
workingDir string,
chainID string,
defaultDBPath string,
defaultEpochString string,
) (uint32, error) {
parentDir := filepath.Join(
workingDir,
defaultDBPath,
chainID)
f, err := os.Open(parentDir)
if err != nil {
return 0, err
}
files, err := f.Readdir(-1)
_ = f.Close()
if err != nil {
return 0, err
}
epochDirs := make([]string, 0, len(files))
for _, file := range files {
if !file.IsDir() {
continue
}
isEpochDir := strings.HasPrefix(file.Name(), defaultEpochString)
if !isEpochDir {
continue
}
epochDirs = append(epochDirs, file.Name())
}
if len(epochDirs) == 0 {
return 0, nil
}
sort.Slice(epochDirs, func(i, j int) bool {
return epochDirs[i] > epochDirs[j]
})
re := regexp.MustCompile("[0-9]+")
epochStr := re.FindString(epochDirs[0])
epoch, err := strconv.ParseInt(epochStr, 10, 64)
return uint32(epoch), err
}