Skip to content

Commit faba6db

Browse files
committed
apply Vojtech suggestions
1 parent 50fcdfa commit faba6db

File tree

3 files changed

+30
-36
lines changed

3 files changed

+30
-36
lines changed

cmd/ethwalcat/ethwalcat.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -148,29 +148,29 @@ func main() {
148148
FileRollOnCloseFlag,
149149
GoogleCloudBucket,
150150
},
151-
Action: func(context *cli.Context) error {
152-
switch context.String(ModeFlag.Name) {
151+
Action: func(c *cli.Context) error {
152+
switch c.String(ModeFlag.Name) {
153153
case "read":
154-
dec, err := decoder(context)
154+
dec, err := decoder(c)
155155
if err != nil {
156156
return err
157157
}
158158

159-
decomp, err := decompressor(context)
159+
decomp, err := decompressor(c)
160160
if err != nil {
161161
return err
162162
}
163163

164164
var fs storage.FS
165-
if bucket := context.String(GoogleCloudBucket.Name); bucket != "" {
165+
if bucket := c.String(GoogleCloudBucket.Name); bucket != "" {
166166
fs = gcloud.NewGCloudFS(bucket, nil)
167167
}
168168

169169
r, err := ethwal.NewReader[any](ethwal.Options{
170170
Dataset: ethwal.Dataset{
171-
Name: context.String(DatasetNameFlag.Name),
172-
Version: context.String(DatasetVersion.Name),
173-
Path: context.String(DatasetPathFlag.Name),
171+
Name: c.String(DatasetNameFlag.Name),
172+
Version: c.String(DatasetVersion.Name),
173+
Path: c.String(DatasetPathFlag.Name),
174174
},
175175
FileSystem: fs,
176176
NewDecoder: dec,
@@ -180,22 +180,22 @@ func main() {
180180
return err
181181
}
182182

183-
if context.Uint64(FromBlockNumFlag.Name) > 0 {
184-
err = r.Seek(context.Context, context.Uint64(FromBlockNumFlag.Name))
183+
if c.Uint64(FromBlockNumFlag.Name) > 0 {
184+
err = r.Seek(c.Context, c.Uint64(FromBlockNumFlag.Name))
185185
if err != nil {
186186
return err
187187
}
188188
}
189189

190-
var toBlockNumber = context.Uint64(ToBlockNumFlag.Name)
190+
var toBlockNumber = c.Uint64(ToBlockNumFlag.Name)
191191

192-
for b, err := r.Read(context.Context); err == nil; b, err = r.Read(context.Context) {
192+
for b, err := r.Read(c.Context); err == nil; b, err = r.Read(c.Context) {
193193
if toBlockNumber != 0 && b.Number >= toBlockNumber {
194194
break
195195
}
196196

197197
// cbor deserializes into map[interface{}]interface{} which can not be serialized into json
198-
if context.String(DecoderFlag.Name) == "cbor" {
198+
if c.String(DecoderFlag.Name) == "cbor" {
199199
b.Data = normalizeDataFromCBOR(b.Data)
200200
}
201201

@@ -220,32 +220,32 @@ func main() {
220220
return err
221221
}
222222
case "write":
223-
enc, err := encoder(context)
223+
enc, err := encoder(c)
224224
if err != nil {
225225
return err
226226
}
227227

228-
compres, err := compressor(context)
228+
compres, err := compressor(c)
229229
if err != nil {
230230
return err
231231
}
232232

233233
var fs storage.FS
234-
if bucket := context.String(GoogleCloudBucket.Name); bucket != "" {
234+
if bucket := c.String(GoogleCloudBucket.Name); bucket != "" {
235235
fs = gcloud.NewGCloudFS(bucket, nil)
236236
}
237237

238238
w, err := ethwal.NewWriter[any](ethwal.Options{
239239
Dataset: ethwal.Dataset{
240-
Name: context.String(DatasetNameFlag.Name),
241-
Version: context.String(DatasetVersion.Name),
242-
Path: context.String(DatasetPathFlag.Name),
240+
Name: c.String(DatasetNameFlag.Name),
241+
Version: c.String(DatasetVersion.Name),
242+
Path: c.String(DatasetPathFlag.Name),
243243
},
244244
FileSystem: fs,
245245
NewEncoder: enc,
246246
NewCompressor: compres,
247247
FileRollPolicy: ethwal.NewFileSizeRollPolicy(uint64(8 << 20)), // 8 MB
248-
FileRollOnClose: context.Bool(FileRollOnCloseFlag.Name),
248+
FileRollOnClose: c.Bool(FileRollOnCloseFlag.Name),
249249
})
250250
if err != nil {
251251
return err
@@ -260,11 +260,11 @@ func main() {
260260
}
261261

262262
// cbor needs to have hashes represented as byte slices
263-
if context.String(EncoderFlag.Name) == "cbor" {
263+
if c.String(EncoderFlag.Name) == "cbor" {
264264
b.Data = normalizeDataToCBOR(b.Data)
265265
}
266266

267-
err = w.Write(context.Context, b)
267+
err = w.Write(c.Context, b)
268268
if err != nil {
269269
return err
270270
}
@@ -274,12 +274,12 @@ func main() {
274274
return err
275275
}
276276

277-
err = w.Close(context.Context)
277+
err = w.Close(c.Context)
278278
if err != nil {
279279
return err
280280
}
281281
default:
282-
return fmt.Errorf("unknown mode: %s", context.String(ModeFlag.Name))
282+
return fmt.Errorf("unknown mode: %s", c.String(ModeFlag.Name))
283283
}
284284

285285
return nil

common.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ethwal
22

33
import (
44
"bytes"
5+
"cmp"
56
"context"
67
"crypto/sha256"
78
"encoding/binary"
@@ -18,6 +19,7 @@ import (
1819

1920
"github.com/0xsequence/ethwal/storage"
2021
"github.com/0xsequence/ethwal/storage/local"
22+
"github.com/c2h5oh/datasize"
2123
)
2224

2325
type Dataset struct {
@@ -56,6 +58,7 @@ func buildETHWALPath(name, version, rootPath string) string {
5658
}
5759

5860
const (
61+
defaultFileSize = 8 * datasize.MB
5962
defaultPrefetchTimeout = 30 * time.Second
6063
)
6164

@@ -77,21 +80,15 @@ type Options struct {
7780
}
7881

7982
func (o Options) WithDefaults() Options {
80-
if o.FileSystem == nil {
81-
o.FileSystem = local.NewLocalFS("")
82-
}
83-
if o.FilePrefetchTimeout == 0 {
84-
o.FilePrefetchTimeout = defaultPrefetchTimeout
85-
}
83+
o.FileSystem = cmp.Or(o.FileSystem, local.NewLocalFS(""))
84+
o.FilePrefetchTimeout = cmp.Or(o.FilePrefetchTimeout, defaultPrefetchTimeout)
85+
o.FileRollPolicy = cmp.Or(o.FileRollPolicy, NewFileSizeRollPolicy(uint64(defaultFileSize)))
8686
if o.NewEncoder == nil {
8787
o.NewEncoder = NewCBOREncoder
8888
}
8989
if o.NewDecoder == nil {
9090
o.NewDecoder = NewCBORDecoder
9191
}
92-
if o.FileRollPolicy == nil {
93-
o.FileRollPolicy = NewFileSizeRollPolicy(uint64(defaultBufferSize))
94-
}
9592
return o
9693
}
9794

writer.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ import (
1010

1111
"github.com/0xsequence/ethwal/storage"
1212
"github.com/0xsequence/ethwal/storage/local"
13-
"github.com/c2h5oh/datasize"
1413
)
1514

16-
const defaultBufferSize = 8 * datasize.MB
17-
1815
type Writer[T any] interface {
1916
Write(ctx context.Context, b Block[T]) error
2017
BlockNum() uint64

0 commit comments

Comments
 (0)