Skip to content

Commit fb0d85b

Browse files
authored
Small improvements (#25)
- remove dependences by adding Block.IsZero - export FileRollPolicy methods - surface swallowed errors
1 parent b13b9d5 commit fb0d85b

8 files changed

Lines changed: 49 additions & 42 deletions

File tree

block.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ type Block[T any] struct {
1212
Data T `json:"blockData"`
1313
}
1414

15+
func (b Block[T]) IsZero() bool {
16+
return b.Number == 0 && b.TS == 0 && b.Hash == (common.Hash{}) && b.Parent == (common.Hash{})
17+
}
18+
1519
type Blocks[T any] []Block[T]

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/RoaringBitmap/roaring/v2 v2.3.4
1010
github.com/Shopify/go-storage v1.3.2
1111
github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500
12-
github.com/fatih/structs v1.1.0
1312
github.com/fxamacker/cbor/v2 v2.7.0
1413
github.com/stretchr/testify v1.9.0
1514
github.com/urfave/cli/v2 v2.27.2

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
3737
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
3838
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
3939
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
40-
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
41-
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
4240
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
4341
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
4442
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=

index_file.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,15 @@ func (i *IndexFile) Write(ctx context.Context, bmap *roaring64.Bitmap) error {
5454
defer file.Close()
5555

5656
comp := NewZSTDCompressor(file)
57-
defer comp.Close()
5857

5958
_, err = bmap.WriteTo(comp)
60-
return err
59+
if err != nil {
60+
_ = comp.Close()
61+
return err
62+
}
63+
64+
if err := comp.Close(); err != nil {
65+
return fmt.Errorf("failed to close compressor: %w", err)
66+
}
67+
return nil
6168
}

reader.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"time"
1111

1212
"github.com/0xsequence/ethwal/storage/stub"
13-
"github.com/fatih/structs"
1413

1514
"github.com/0xsequence/ethwal/storage"
1615
"github.com/0xsequence/ethwal/storage/local"
@@ -141,7 +140,7 @@ func (r *reader[T]) Read(ctx context.Context) (Block[T], error) {
141140
}
142141

143142
var block Block[T]
144-
for structs.IsZero(block) || block.Number <= r.lastBlockNum {
143+
for block.IsZero() || block.Number <= r.lastBlockNum {
145144
select {
146145
case <-ctx.Done():
147146
return Block[T]{}, ctx.Err()
@@ -164,7 +163,7 @@ func (r *reader[T]) Read(ctx context.Context) (Block[T], error) {
164163
return Block[T]{}, fmt.Errorf("failed to decode data: %w", err)
165164
}
166165

167-
if !structs.IsZero(block) {
166+
if !block.IsZero() {
168167
r.lastBlockNum = block.Number
169168
}
170169

@@ -190,7 +189,7 @@ func (r *reader[T]) Read(ctx context.Context) (Block[T], error) {
190189
}
191190
}
192191

193-
if !structs.IsZero(block) {
192+
if !block.IsZero() {
194193
r.lastBlockNum = block.Number
195194
}
196195

writer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (w *writer[T]) Write(ctx context.Context, b Block[T]) error {
119119
}
120120

121121
w.lastBlockNum = b.Number
122-
w.options.FileRollPolicy.onBlockProcessed(w.lastBlockNum)
122+
w.options.FileRollPolicy.OnBlockProcessed(w.lastBlockNum)
123123
return nil
124124
}
125125

@@ -206,7 +206,7 @@ func (w *writer[T]) rollFile(ctx context.Context) error {
206206
func (w *writer[T]) writeFile(ctx context.Context) error {
207207
// create new file
208208
newFile := &File{FirstBlockNum: w.firstBlockNum, LastBlockNum: w.lastBlockNum}
209-
w.options.FileRollPolicy.onFlush(ctx)
209+
w.options.FileRollPolicy.OnFlush(ctx)
210210

211211
// add file to file index
212212
err := w.fileIndex.AddFile(newFile)

writer_file_roll_policy.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ type FileRollPolicy interface {
1111
ShouldRoll() bool
1212
Reset()
1313

14-
onWrite(data []byte)
15-
onBlockProcessed(blockNum uint64)
16-
onFlush(ctx context.Context)
14+
OnWrite(data []byte)
15+
OnBlockProcessed(blockNum uint64)
16+
OnFlush(ctx context.Context)
1717
}
1818

1919
type fileSizeRollPolicy struct {
@@ -33,13 +33,13 @@ func (p *fileSizeRollPolicy) Reset() {
3333
p.bytesWritten = 0
3434
}
3535

36-
func (p *fileSizeRollPolicy) onWrite(data []byte) {
36+
func (p *fileSizeRollPolicy) OnWrite(data []byte) {
3737
p.bytesWritten += uint64(len(data))
3838
}
3939

40-
func (p *fileSizeRollPolicy) onBlockProcessed(blockNum uint64) {}
40+
func (p *fileSizeRollPolicy) OnBlockProcessed(blockNum uint64) {}
4141

42-
func (p *fileSizeRollPolicy) onFlush(ctx context.Context) {}
42+
func (p *fileSizeRollPolicy) OnFlush(ctx context.Context) {}
4343

4444
// fileStats is a writer that keeps track of the number of bytes written to it.
4545
type writerWrapper struct {
@@ -49,7 +49,7 @@ type writerWrapper struct {
4949
}
5050

5151
func (w *writerWrapper) Write(p []byte) (n int, err error) {
52-
defer w.fsrp.onWrite(p)
52+
defer w.fsrp.OnWrite(p)
5353
return w.Writer.Write(p)
5454
}
5555

@@ -59,7 +59,7 @@ type lastBlockNumberRollPolicy struct {
5959
lastBlockNum uint64
6060
}
6161

62-
func (l *lastBlockNumberRollPolicy) onWrite(data []byte) {}
62+
func (l *lastBlockNumberRollPolicy) OnWrite(data []byte) {}
6363

6464
func NewLastBlockNumberRollPolicy(rollInterval uint64) FileRollPolicy {
6565
return &lastBlockNumberRollPolicy{rollInterval: rollInterval}
@@ -73,11 +73,11 @@ func (l *lastBlockNumberRollPolicy) Reset() {
7373
// noop
7474
}
7575

76-
func (l *lastBlockNumberRollPolicy) onBlockProcessed(blockNum uint64) {
76+
func (l *lastBlockNumberRollPolicy) OnBlockProcessed(blockNum uint64) {
7777
l.lastBlockNum = blockNum
7878
}
7979

80-
func (l *lastBlockNumberRollPolicy) onFlush(ctx context.Context) {}
80+
func (l *lastBlockNumberRollPolicy) OnFlush(ctx context.Context) {}
8181

8282
type timeBasedRollPolicy struct {
8383
rollInterval time.Duration
@@ -108,11 +108,11 @@ func (t *timeBasedRollPolicy) Reset() {
108108
t.lastTimeRolled = time.Now()
109109
}
110110

111-
func (t *timeBasedRollPolicy) onWrite(data []byte) {}
111+
func (t *timeBasedRollPolicy) OnWrite(data []byte) {}
112112

113-
func (t *timeBasedRollPolicy) onBlockProcessed(blockNum uint64) {}
113+
func (t *timeBasedRollPolicy) OnBlockProcessed(blockNum uint64) {}
114114

115-
func (t *timeBasedRollPolicy) onFlush(ctx context.Context) {}
115+
func (t *timeBasedRollPolicy) OnFlush(ctx context.Context) {}
116116

117117
type FileRollPolicies []FileRollPolicy
118118

@@ -131,21 +131,21 @@ func (policies FileRollPolicies) Reset() {
131131
}
132132
}
133133

134-
func (policies FileRollPolicies) onWrite(data []byte) {
134+
func (policies FileRollPolicies) OnWrite(data []byte) {
135135
for _, p := range policies {
136-
p.onWrite(data)
136+
p.OnWrite(data)
137137
}
138138
}
139139

140-
func (policies FileRollPolicies) onBlockProcessed(blockNum uint64) {
140+
func (policies FileRollPolicies) OnBlockProcessed(blockNum uint64) {
141141
for _, p := range policies {
142-
p.onBlockProcessed(blockNum)
142+
p.OnBlockProcessed(blockNum)
143143
}
144144
}
145145

146-
func (policies FileRollPolicies) onFlush(ctx context.Context) {
146+
func (policies FileRollPolicies) OnFlush(ctx context.Context) {
147147
for _, p := range policies {
148-
p.onFlush(ctx)
148+
p.OnFlush(ctx)
149149
}
150150
}
151151

@@ -166,16 +166,16 @@ func (w *wrappedRollPolicy) Reset() {
166166
w.rollPolicy.Reset()
167167
}
168168

169-
func (w *wrappedRollPolicy) onWrite(data []byte) {
170-
w.rollPolicy.onWrite(data)
169+
func (w *wrappedRollPolicy) OnWrite(data []byte) {
170+
w.rollPolicy.OnWrite(data)
171171
}
172172

173-
func (w *wrappedRollPolicy) onBlockProcessed(blockNum uint64) {
174-
w.rollPolicy.onBlockProcessed(blockNum)
173+
func (w *wrappedRollPolicy) OnBlockProcessed(blockNum uint64) {
174+
w.rollPolicy.OnBlockProcessed(blockNum)
175175
}
176176

177-
func (w *wrappedRollPolicy) onFlush(ctx context.Context) {
178-
w.rollPolicy.onFlush(ctx)
177+
func (w *wrappedRollPolicy) OnFlush(ctx context.Context) {
178+
w.rollPolicy.OnFlush(ctx)
179179
w.flushFunc(ctx)
180180
}
181181

writer_file_roll_policy_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ func TestLastBlockNumberRollPolicy(t *testing.T) {
4141
p := NewLastBlockNumberRollPolicy(10)
4242
assert.False(t, p.ShouldRoll())
4343

44-
p.onBlockProcessed(5)
44+
p.OnBlockProcessed(5)
4545
assert.False(t, p.ShouldRoll())
4646

47-
p.onBlockProcessed(10)
47+
p.OnBlockProcessed(10)
4848
assert.True(t, p.ShouldRoll())
4949

50-
p.onBlockProcessed(11)
50+
p.OnBlockProcessed(11)
5151
assert.False(t, p.ShouldRoll())
5252
}
5353

@@ -77,10 +77,10 @@ func TestNewFileSizeOrLastBlockNumberRollPolicy(t *testing.T) {
7777

7878
assert.False(t, fol.ShouldRoll())
7979

80-
fol.onBlockProcessed(10)
80+
fol.OnBlockProcessed(10)
8181
assert.True(t, fol.ShouldRoll())
8282

83-
fol.onBlockProcessed(11)
83+
fol.OnBlockProcessed(11)
8484
assert.False(t, fol.ShouldRoll())
8585

8686
_, err := w.Write([]byte("hello world"))
@@ -91,6 +91,6 @@ func TestNewFileSizeOrLastBlockNumberRollPolicy(t *testing.T) {
9191
fol.Reset()
9292
assert.False(t, fol.ShouldRoll())
9393

94-
fol.onBlockProcessed(20)
94+
fol.OnBlockProcessed(20)
9595
assert.True(t, fol.ShouldRoll())
9696
}

0 commit comments

Comments
 (0)