Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose file index in reader #13

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"time"

"github.com/0xsequence/ethwal/storage/stub"
"github.com/fatih/structs"

"github.com/0xsequence/ethwal/storage"
Expand All @@ -19,7 +20,8 @@ const firstFileIndex = 0
const loadIndexFileTimeout = 30 * time.Second

type Reader[T any] interface {
FilesNum() int
FileNum() int
FileIndex() *FileIndex
Read(ctx context.Context) (Block[T], error)
Seek(ctx context.Context, blockNum uint64) error
BlockNum() uint64
Expand Down Expand Up @@ -102,13 +104,27 @@ func NewReader[T any](opt Options) (Reader[T], error) {
}, nil
}

func (r *reader[T]) FilesNum() int {
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()
Expand Down
26 changes: 24 additions & 2 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestReader_Read(t *testing.T) {
}
}

func TestReader_NumWALFiles(t *testing.T) {
func TestReader_FileNum(t *testing.T) {
testSetup(t, NewCBOREncoder, nil)
defer testTeardown(t)

Expand All @@ -238,7 +238,29 @@ func TestReader_NumWALFiles(t *testing.T) {
})
require.NoError(t, err)

assert.Equal(t, 3, rdr.FilesNum())
assert.Equal(t, 3, rdr.FileNum())

require.NoError(t, rdr.Close())
}

func TestReader_FileIndex(t *testing.T) {
testSetup(t, NewCBOREncoder, nil)
defer testTeardown(t)

rdr, err := NewReader[int](Options{
Dataset: Dataset{
Name: "int-wal",
Path: testPath,
Version: defaultDatasetVersion,
},
NewEncoder: NewCBOREncoder,
NewDecoder: NewCBORDecoder,
})
require.NoError(t, err)

fileIndex := rdr.FileIndex()
require.NotNil(t, fileIndex)
assert.Equal(t, 3, len(fileIndex.Files()))

require.NoError(t, rdr.Close())
}
Expand Down
8 changes: 6 additions & 2 deletions reader_with_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ func NewReaderWithFilter[T any](reader Reader[T], filter Filter) (Reader[T], err
}, nil
}

func (c *readerWithFilter[T]) FilesNum() int {
return c.reader.FilesNum()
func (c *readerWithFilter[T]) FileNum() int {
return c.reader.FileNum()
}

func (c *readerWithFilter[T]) FileIndex() *FileIndex {
return c.reader.FileIndex()
}

func (c *readerWithFilter[T]) Seek(ctx context.Context, blockNum uint64) error {
Expand Down
38 changes: 38 additions & 0 deletions storage/stub/stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package stub

import (
"context"
"fmt"
"io"

"github.com/Shopify/go-storage"
)

type Stub struct {
}

func (s Stub) Walk(ctx context.Context, path string, fn storage.WalkFn) error {
return nil
}

func (s Stub) Open(ctx context.Context, path string, options *storage.ReaderOptions) (*storage.File, error) {
return nil, fmt.Errorf("not implemented")
}

func (s Stub) Attributes(ctx context.Context, path string, options *storage.ReaderOptions) (*storage.Attributes, error) {
return nil, fmt.Errorf("not implemented")
}

func (s Stub) Create(ctx context.Context, path string, options *storage.WriterOptions) (io.WriteCloser, error) {
return nil, fmt.Errorf("not implemented")
}

func (s Stub) Delete(ctx context.Context, path string) error {
return nil
}

func (s Stub) URL(ctx context.Context, path string, options *storage.SignedURLOptions) (string, error) {
return "", nil
}

var _ storage.FS = (*Stub)(nil)
Loading