github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/table/read_ahead_table_reader.go (about)

     1  // Copyright 2020 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package table
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/dolthub/dolt/go/libraries/doltcore/row"
    21  	"github.com/dolthub/dolt/go/libraries/doltcore/schema"
    22  	"github.com/dolthub/dolt/go/libraries/utils/async"
    23  )
    24  
    25  var _ TableReadCloser = (*AsyncReadAheadTableReader)(nil)
    26  
    27  // AsyncReadAheadTableReader is a TableReadCloser implementation that spins up a go routine to keep reading data into
    28  // a buffered channel so that it is ready when the caller wants it.
    29  type AsyncReadAheadTableReader struct {
    30  	backingReader TableReadCloser
    31  	reader        *async.AsyncReader
    32  }
    33  
    34  // NewAsyncReadAheadTableReader creates a new AsyncReadAheadTableReader
    35  func NewAsyncReadAheadTableReader(tr TableReadCloser, bufferSize int) *AsyncReadAheadTableReader {
    36  	read := func(ctx context.Context) (interface{}, error) {
    37  		return tr.ReadRow(ctx)
    38  	}
    39  
    40  	reader := async.NewAsyncReader(read, bufferSize)
    41  	return &AsyncReadAheadTableReader{tr, reader}
    42  }
    43  
    44  // Start the worker routine reading rows to the channel
    45  func (tr *AsyncReadAheadTableReader) Start(ctx context.Context) error {
    46  	return tr.reader.Start(ctx)
    47  }
    48  
    49  // GetSchema gets the schema of the rows that this reader will return
    50  func (tr *AsyncReadAheadTableReader) GetSchema() schema.Schema {
    51  	return tr.backingReader.GetSchema()
    52  }
    53  
    54  // ReadRow reads a row from a table.  If there is a bad row the returned error will be non nil, and calling
    55  // IsBadRow(err) will be return true. This is a potentially non-fatal error and callers can decide if they want to
    56  // continue on a bad row, or fail.
    57  func (tr *AsyncReadAheadTableReader) ReadRow(ctx context.Context) (row.Row, error) {
    58  	obj, err := tr.reader.Read()
    59  
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	return obj.(row.Row), err
    65  }
    66  
    67  // Close releases resources being held
    68  func (tr *AsyncReadAheadTableReader) Close(ctx context.Context) error {
    69  	_ = tr.reader.Close()
    70  	return tr.backingReader.Close(ctx)
    71  }