Skip to content

Commit

Permalink
db: add NewIterWithContext to Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeerbhola committed Oct 19, 2023
1 parent f6cde3f commit 593a72b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
8 changes: 4 additions & 4 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -1088,16 +1088,16 @@ func (b *Batch) SetRepr(data []byte) error {
// later mutations. Its view can be refreshed via RefreshBatchSnapshot or
// SetOptions().
func (b *Batch) NewIter(o *IterOptions) (*Iterator, error) {
return b.NewIterWithContext(context.Background(), o), nil
return b.NewIterWithContext(context.Background(), o)
}

// NewIterWithContext is like NewIter, and additionally accepts a context for
// tracing.
func (b *Batch) NewIterWithContext(ctx context.Context, o *IterOptions) *Iterator {
func (b *Batch) NewIterWithContext(ctx context.Context, o *IterOptions) (*Iterator, error) {
if b.index == nil {
return &Iterator{err: ErrNotIndexed}
return &Iterator{err: ErrNotIndexed}, nil
}
return b.db.newIter(ctx, b, snapshotIterOpts{}, o)
return b.db.newIter(ctx, b, snapshotIterOpts{}, o), nil
}

// newInternalIter creates a new internalIterator that iterates over the
Expand Down
4 changes: 4 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ type Reader interface {
// SeekLT, First or Last.
NewIter(o *IterOptions) (*Iterator, error)

// NewIterWithContext is like NewIter, and additionally accepts a context
// for tracing.
NewIterWithContext(ctx context.Context, o *IterOptions) (*Iterator, error)

// Close closes the Reader. It may or may not close any underlying io.Reader
// or io.Writer, depending on how the DB was created.
//
Expand Down
3 changes: 2 additions & 1 deletion db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,8 @@ func TestTracing(t *testing.T) {

tracer.buf.Reset()
b := d.NewIndexedBatch()
iter = b.NewIterWithContext(ctx, nil)
iter, err = b.NewIterWithContext(ctx, nil)
require.NoError(t, err)
iter.SeekGE([]byte("hello"))
iter.Close()
require.Equal(t, iterTraceString, tracer.buf.String())
Expand Down

0 comments on commit 593a72b

Please sign in to comment.