-
Notifications
You must be signed in to change notification settings - Fork 45
/
writer.go
52 lines (42 loc) · 1.38 KB
/
writer.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
package execout
import (
"context"
"fmt"
"sync"
"github.com/streamingfast/substreams/block"
pbsubstreams "github.com/streamingfast/substreams/pb/sf/substreams/v1"
)
// The Writer writes a single file with executionOutputs that will be read by the LinearExecOutReader.
// `initialBlockBoundary` is expected to be on a boundary, or to be the module's initial block.
type Writer struct {
wg *sync.WaitGroup
CurrentFile *File
outputModule string
isWriterForIndex bool
}
func NewWriter(initialBlockBoundary, exclusiveEndBlock uint64, outputModule string, configs *Configs, isWriterForIndex bool) *Writer {
w := &Writer{
wg: &sync.WaitGroup{},
outputModule: outputModule,
isWriterForIndex: isWriterForIndex,
}
segmenter := block.NewSegmenter(configs.execOutputSaveInterval, initialBlockBoundary, exclusiveEndBlock)
walker := configs.NewFileWalker(outputModule, segmenter)
w.CurrentFile = walker.File()
return w
}
func (w *Writer) Write(clock *pbsubstreams.Clock, buffer *Buffer) {
if val, found := buffer.valuesForFileOutput[w.outputModule]; found {
w.CurrentFile.SetItem(clock, val)
}
}
func (w *Writer) Close(ctx context.Context) error {
// Skip outputs file saving for blockIndex module
if w.isWriterForIndex {
return nil
}
if err := w.CurrentFile.Save(ctx); err != nil {
return fmt.Errorf("flushing exec output writer: %w", err)
}
return nil
}