-
Notifications
You must be signed in to change notification settings - Fork 45
/
config.go
104 lines (87 loc) · 2.93 KB
/
config.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
package execout
import (
"context"
"fmt"
"github.com/streamingfast/bstream"
"github.com/streamingfast/derr"
"github.com/streamingfast/dstore"
"go.uber.org/zap"
"github.com/streamingfast/substreams/block"
pbsubstreams "github.com/streamingfast/substreams/pb/sf/substreams/v1"
pboutput "github.com/streamingfast/substreams/storage/execout/pb"
)
type Config struct {
name string
moduleHash string
objStore dstore.Store
modKind pbsubstreams.ModuleKind
moduleInitialBlock uint64
logger *zap.Logger
}
func NewConfig(name string, moduleInitialBlock uint64, modKind pbsubstreams.ModuleKind, moduleHash string, baseStore dstore.Store, logger *zap.Logger) (*Config, error) {
subName := fmt.Sprintf("%s/outputs", moduleHash)
if modKind == pbsubstreams.ModuleKindBlockIndex {
subName = fmt.Sprintf("%s/index", moduleHash)
}
subStore, err := baseStore.SubStore(subName)
if err != nil {
return nil, fmt.Errorf("creating sub store: %w", err)
}
return &Config{
name: name,
objStore: subStore,
modKind: modKind,
moduleInitialBlock: moduleInitialBlock,
moduleHash: moduleHash,
logger: logger.With(zap.String("module", name)),
}, nil
}
func (c *Config) NewFile(targetRange *block.Range) *File {
return &File{
Kv: make(map[string]*pboutput.Item),
ModuleName: c.name,
store: c.objStore,
Range: targetRange,
logger: c.logger,
}
}
func (c *Config) Name() string { return c.name }
func (c *Config) ModuleKind() pbsubstreams.ModuleKind { return c.modKind }
func (c *Config) ModuleInitialBlock() uint64 { return c.moduleInitialBlock }
func (c *Config) ListSnapshotFiles(ctx context.Context, inRange *bstream.Range) (files FileInfos, err error) {
err = derr.RetryContext(ctx, 3, func(ctx context.Context) error {
// We must reset accumulated files between each retry
files = nil
return c.objStore.WalkFrom(ctx, "", computeDBinFilename(inRange.StartBlock(), 0), func(filename string) (err error) {
var fileInfo *FileInfo
switch c.modKind {
case pbsubstreams.ModuleKindBlockIndex:
fileInfo, err = parseIndexFileName(filename)
case pbsubstreams.ModuleKindMap:
fileInfo, err = parseExecoutFileName(filename)
default:
return fmt.Errorf("wrong module kind: %v", c.modKind)
}
if err != nil {
c.logger.Warn("seen exec output file that we don't know how to parse", zap.String("filename", filename), zap.Error(err))
return nil
}
if inRange.ReachedEndBlock(fileInfo.BlockRange.ExclusiveEndBlock - 1) {
return dstore.StopIteration
}
files = append(files, fileInfo)
return nil
})
})
if err != nil {
return nil, fmt.Errorf("walking files: %s", err)
}
return files, nil
}
func (c *Config) ReadFile(ctx context.Context, inrange *block.Range) (*File, error) {
file := c.NewFile(inrange)
if err := file.Load(ctx); err != nil {
return nil, err
}
return file, nil
}