-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
303 lines (247 loc) · 6.51 KB
/
reader.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package ethwal
import (
"context"
"errors"
"fmt"
"io"
"os"
"sync"
"time"
"github.com/0xsequence/ethwal/storage/stub"
"github.com/fatih/structs"
"github.com/0xsequence/ethwal/storage"
"github.com/0xsequence/ethwal/storage/local"
)
const firstFileIndex = 0
const loadIndexFileTimeout = 30 * time.Second
type Reader[T any] interface {
FileNum() int
FileIndex() *FileIndex
Read(ctx context.Context) (Block[T], error)
Seek(ctx context.Context, blockNum uint64) error
BlockNum() uint64
Close() error
}
type reader[T any] struct {
options Options
path string
fs storage.FS
useCompression bool
closer io.Closer
fileIndex *FileIndex
currFileIndex int
lastBlockNum uint64
decoder Decoder
mu sync.Mutex
}
func NewReader[T any](opt Options) (Reader[T], error) {
// apply default options on uninitialized fields
opt = opt.WithDefaults()
if opt.Dataset.Path == "" {
return nil, fmt.Errorf("path cannot be empty")
}
// build dataset path
datasetPath := opt.Dataset.FullPath()
// set file system
fs := opt.FileSystem
// create dataset directory if it doesn't exist on local FS
if _, ok := opt.FileSystem.(*local.LocalFS); ok {
if _, err := os.Stat(datasetPath); os.IsNotExist(err) {
err := os.MkdirAll(datasetPath, 0755)
if err != nil {
return nil, fmt.Errorf("failed to create ethwal directory")
}
}
} else {
// add cache wrapper to file system, so that we can cache the files locally
if opt.Dataset.CachePath != "" {
if _, err := os.Stat(opt.Dataset.CachePath); os.IsNotExist(err) {
err := os.MkdirAll(opt.Dataset.CachePath, 0755)
if err != nil {
return nil, fmt.Errorf("failed to create ethwal cache directory")
}
}
fs = storage.NewCacheWrapper(fs, local.NewLocalFS(opt.Dataset.CachePath), nil)
}
}
// add prefix to file system
fs = storage.NewPrefixWrapper(fs, datasetPath)
// create file index
fileIndex := NewFileIndex(fs)
// load file index
ctx, cancel := context.WithTimeout(context.Background(), loadIndexFileTimeout)
defer cancel()
err := fileIndex.Load(ctx)
if err != nil {
return nil, fmt.Errorf("failed to load file index: %w", err)
}
return &reader[T]{
options: opt,
path: datasetPath,
fs: fs,
fileIndex: fileIndex,
}, nil
}
func (r *reader[T]) FileNum() int {
r.mu.Lock()
defer r.mu.Unlock()
return len(r.fileIndex.Files())
}
func (r *reader[T]) FileIndex() *FileIndex {
r.mu.Lock()
defer r.mu.Unlock()
newfiles := make([]*File, len(r.fileIndex.Files()))
for index, file := range r.fileIndex.Files() {
newfiles[index] = &File{
FirstBlockNum: file.FirstBlockNum,
LastBlockNum: file.LastBlockNum,
}
}
return NewFileIndexFromFiles(stub.Stub{}, newfiles)
}
func (r *reader[T]) Read(ctx context.Context) (Block[T], error) {
r.mu.Lock()
defer r.mu.Unlock()
var err error
if r.decoder == nil {
err = r.readFile(ctx, firstFileIndex)
if errors.Is(err, io.EOF) {
return Block[T]{}, io.EOF
}
if err != nil {
return Block[T]{}, fmt.Errorf("failed to read first file: %w", err)
}
}
var block Block[T]
for structs.IsZero(block) || block.Number <= r.lastBlockNum {
select {
case <-ctx.Done():
return Block[T]{}, ctx.Err()
default:
}
err = r.decoder.Decode(&block)
if err != nil {
if err == io.EOF {
err = r.readNextFile(ctx)
if errors.Is(err, io.EOF) {
return Block[T]{}, io.EOF
}
if err != nil {
return Block[T]{}, fmt.Errorf("failed to read next file: %w", err)
}
err = r.decoder.Decode(&block)
if err != nil {
return Block[T]{}, fmt.Errorf("failed to decode data: %w", err)
}
if !structs.IsZero(block) {
r.lastBlockNum = block.Number
}
if !r.isBlockWithin(block) {
currentFile := r.fileIndex.At(r.currFileIndex)
return Block[T]{}, fmt.Errorf("block number %d is out of file block %d-%d range",
block.Number,
currentFile.FirstBlockNum,
currentFile.LastBlockNum)
}
return block, nil
}
return Block[T]{}, fmt.Errorf("failed to decode file data: %w", err)
}
if !r.isBlockWithin(block) {
currentFile := r.fileIndex.At(r.currFileIndex)
return Block[T]{}, fmt.Errorf("block number %d is out of file block %d-%d range",
block.Number,
currentFile.FirstBlockNum,
currentFile.LastBlockNum)
}
}
if !structs.IsZero(block) {
r.lastBlockNum = block.Number
}
return block, nil
}
func (r *reader[T]) Seek(ctx context.Context, blockNum uint64) error {
r.mu.Lock()
defer r.mu.Unlock()
_, fileIndex, err := r.fileIndex.FindFile(blockNum)
if err != nil && errors.Is(err, ErrFileNotExist) {
return io.EOF
}
if err != nil {
return err
}
if r.currFileIndex != fileIndex {
// clear prefetched file
if r.currFileIndex+1 < len(r.fileIndex.Files()) {
r.fileIndex.At(r.currFileIndex + 1).PrefetchClear()
}
// read file
r.currFileIndex = fileIndex
err = r.readFile(ctx, r.currFileIndex)
if err != nil {
return err
}
}
r.lastBlockNum = blockNum - 1
return nil
}
func (r *reader[T]) BlockNum() uint64 {
r.mu.Lock()
defer r.mu.Unlock()
return r.lastBlockNum
}
func (r *reader[T]) Close() error {
r.mu.Lock()
defer r.mu.Unlock()
if r.closer != nil {
return r.closer.Close()
}
return nil
}
func (r *reader[T]) readFile(ctx context.Context, index int) error {
if index >= len(r.fileIndex.Files()) {
return io.EOF
}
if r.closer != nil {
_ = r.closer.Close()
}
file := r.fileIndex.At(index)
rdr, err := file.Open(ctx, r.fs)
if err != nil {
return err
}
var decmprRdr = io.NopCloser(rdr)
if r.options.NewDecompressor != nil {
decmprRdr = r.options.NewDecompressor(decmprRdr)
}
r.closer = &funcCloser{
CloseFunc: func() error {
if err := decmprRdr.Close(); err != nil {
_ = rdr.Close()
return err
}
return rdr.Close()
},
}
r.decoder = r.options.NewDecoder(decmprRdr)
r.currFileIndex = index
return nil
}
func (r *reader[T]) readNextFile(ctx context.Context) error {
defer r.prefetchNextFile(ctx)
return r.readFile(ctx, r.currFileIndex+1)
}
func (r *reader[T]) prefetchNextFile(ctx context.Context) {
if r.currFileIndex+1 < len(r.fileIndex.Files()) {
go r.prefetchFile(ctx, r.fileIndex.At(r.currFileIndex+1))
}
}
func (r *reader[T]) prefetchFile(ctx context.Context, file *File) {
pCtx, cancel := context.WithTimeout(ctx, r.options.FilePrefetchTimeout)
defer cancel()
_ = file.Prefetch(pCtx, r.fs)
}
func (r *reader[T]) isBlockWithin(block Block[T]) bool {
return r.fileIndex.Files()[r.currFileIndex].FirstBlockNum <= block.Number &&
block.Number <= r.fileIndex.Files()[r.currFileIndex].LastBlockNum
}