-
Notifications
You must be signed in to change notification settings - Fork 45
/
filename.go
96 lines (78 loc) · 2.06 KB
/
filename.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
package store
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/streamingfast/substreams/block"
)
var stateFileRegex = regexp.MustCompile(`([\d]+)-([\d]+)(?:\.([^\.]+))?\.(kv|partial)`)
type FileInfos []*FileInfo
func (f FileInfos) Ranges() (out block.Ranges) {
if len(f) == 0 {
return nil
}
out = make(block.Ranges, len(f))
for i, file := range f {
out[i] = file.Range
}
return
}
func (f FileInfos) String() string {
ranges := make([]string, len(f))
for i, file := range f {
ranges[i] = file.Range.String()
}
return strings.Join(ranges, ",")
}
type FileInfo struct {
ModuleName string
Filename string
Range *block.Range
Partial bool
WithTraceID bool
}
func NewCompleteFileInfo(moduleName string, moduleInitialBlock uint64, exclusiveEndBlock uint64) *FileInfo {
bRange := block.NewRange(moduleInitialBlock, exclusiveEndBlock)
return &FileInfo{
ModuleName: moduleName,
Filename: FullStateFileName(bRange),
Range: block.NewRange(moduleInitialBlock, exclusiveEndBlock),
Partial: false,
}
}
func NewPartialFileInfo(moduleName string, start uint64, exclusiveEndBlock uint64) *FileInfo {
bRange := block.NewRange(start, exclusiveEndBlock)
return &FileInfo{
ModuleName: moduleName,
Filename: PartialFileName(bRange),
Range: bRange,
Partial: true,
}
}
func parseFileName(moduleName, filename string) (*FileInfo, bool) {
res := stateFileRegex.FindAllStringSubmatch(filename, 1)
if len(res) != 1 {
return nil, false
}
return &FileInfo{
ModuleName: moduleName,
Filename: filename,
Range: block.NewRange(uint64(mustAtoi(res[0][2])), uint64(mustAtoi(res[0][1]))),
Partial: res[0][4] == "partial",
WithTraceID: res[0][3] != "",
}, true
}
func PartialFileName(r *block.Range) string {
return fmt.Sprintf("%010d-%010d.partial", r.ExclusiveEndBlock, r.StartBlock)
}
func FullStateFileName(r *block.Range) string {
return fmt.Sprintf("%010d-%010d.kv", r.ExclusiveEndBlock, r.StartBlock)
}
func mustAtoi(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}