-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.go
53 lines (43 loc) · 1.35 KB
/
utils.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
package prunable
import (
"os"
"path/filepath"
"sort"
"strconv"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/ioutils"
iotago "github.com/iotaledger/iota.go/v4"
)
func dbPathFromIndex(base string, epoch iotago.EpochIndex) string {
return filepath.Join(base, strconv.FormatInt(int64(epoch), 10))
}
type dbInstanceFileInfo struct {
baseEpoch iotago.EpochIndex
path string
}
// getSortedDBInstancesFromDisk returns an ASC sorted list of db instances from the given base directory.
func getSortedDBInstancesFromDisk(baseDir string) (dbInfos []*dbInstanceFileInfo) {
files, err := os.ReadDir(baseDir)
if err != nil {
panic(err)
}
files = lo.Filter(files, func(e os.DirEntry) bool { return e.IsDir() })
dbInfos = lo.Map(files, func(e os.DirEntry) *dbInstanceFileInfo {
atoi, convErr := strconv.Atoi(e.Name())
if convErr != nil {
return nil
}
return &dbInstanceFileInfo{
baseEpoch: iotago.EpochIndex(atoi),
path: filepath.Join(baseDir, e.Name()),
}
})
dbInfos = lo.Filter(dbInfos, func(info *dbInstanceFileInfo) bool { return info != nil })
sort.Slice(dbInfos, func(i int, j int) bool {
return dbInfos[i].baseEpoch < dbInfos[j].baseEpoch
})
return dbInfos
}
func dbPrunableDirectorySize(base string, epoch iotago.EpochIndex) (int64, error) {
return ioutils.FolderSize(dbPathFromIndex(base, epoch))
}