-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter_no_gap.go
64 lines (49 loc) · 1.21 KB
/
writer_no_gap.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
package ethwal
import (
"context"
"github.com/0xsequence/ethwal/storage"
)
type noGapWriter[T any] struct {
w Writer[T]
lastBlockNum uint64
}
func NewWriterNoGap[T any](w Writer[T]) Writer[T] {
return &noGapWriter[T]{w: w}
}
func (n *noGapWriter[T]) FileSystem() storage.FS {
return n.w.FileSystem()
}
func (n *noGapWriter[T]) Write(ctx context.Context, b Block[T]) error {
defer func() { n.lastBlockNum = b.Number }()
// skip if block number is less than or equal to last block number
if b.Number <= n.lastBlockNum {
return nil
}
// write blocks as there is no gap
if b.Number == n.lastBlockNum+1 {
return n.w.Write(ctx, b)
}
// write missing blocks
for i := n.lastBlockNum + 1; i < b.Number; i++ {
err := n.w.Write(ctx, Block[T]{Number: i})
if err != nil {
return err
}
}
return n.w.Write(ctx, b)
}
func (n *noGapWriter[T]) RollFile(ctx context.Context) error {
return n.w.RollFile(ctx)
}
func (n *noGapWriter[T]) BlockNum() uint64 {
return n.w.BlockNum()
}
func (n *noGapWriter[T]) Close(ctx context.Context) error {
return n.w.Close(ctx)
}
func (n *noGapWriter[T]) Options() Options {
return n.w.Options()
}
func (n *noGapWriter[T]) SetOptions(opts Options) {
n.w.SetOptions(opts)
}