public abstract static class OffsetBasedSource.OffsetBasedReader<T> extends BoundedSource.BoundedReader<T>
Source.Reader that implements code common to readers of all
OffsetBasedSources.
Subclasses have to implement:
startImpl() and advanceImpl() for reading the
first or subsequent records.
Source.Reader.getCurrent(), getCurrentOffset(), and optionally
isAtSplitPoint() and BoundedSource.BoundedReader.getCurrentTimestamp() to access properties of
the last record successfully read by startImpl() or advanceImpl().
| Constructor and Description |
|---|
OffsetBasedReader(OffsetBasedSource<T> source) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
advance()
Advances the reader to the next valid record.
|
protected abstract boolean |
advanceImpl()
Same as
BoundedReader#advance, except OffsetBasedSource.OffsetBasedReader base class
takes care of coordinating against concurrent calls to splitAtFraction(double). |
protected abstract long |
getCurrentOffset()
Returns the starting offset of the
current record,
which has been read by the last successful Source.Reader.start() or
Source.Reader.advance() call. |
OffsetBasedSource<T> |
getCurrentSource()
Returns a
Source describing the same input that this Reader reads
(including items already read). |
Double |
getFractionConsumed()
Returns a value in [0, 1] representing approximately what fraction of the source
(
BoundedSource.BoundedReader.getCurrentSource()) this reader has read so far. |
protected boolean |
isAtSplitPoint()
Returns whether the current record is at a split point (i.e., whether the current record
would be the first record to be read by a source with a specified start offset of
getCurrentOffset()). |
OffsetBasedSource<T> |
splitAtFraction(double fraction)
Tells the reader to narrow the range of the input it's going to read and give up
the remainder, so that the new range would contain approximately the given
fraction of the amount of data in the current range.
|
boolean |
start()
Initializes the reader and advances the reader to the first record.
|
protected abstract boolean |
startImpl()
Same as
BoundedReader#start, except OffsetBasedSource.OffsetBasedReader base class
takes care of coordinating against concurrent calls to splitAtFraction(double). |
getCurrentTimestampclose, getCurrentpublic OffsetBasedReader(OffsetBasedSource<T> source)
source - the OffsetBasedSource to be read by the current reader.protected abstract long getCurrentOffset()
throws NoSuchElementException
current record,
which has been read by the last successful Source.Reader.start() or
Source.Reader.advance() call.
If no such call has been made yet, the return value is unspecified.
See RangeTracker for description of offset semantics.
NoSuchElementExceptionprotected boolean isAtSplitPoint()
throws NoSuchElementException
getCurrentOffset()).
See detailed documentation about split points in RangeTracker.
NoSuchElementExceptionpublic final boolean start()
throws IOException
Source.ReaderThis method should be called exactly once. The invocation should occur prior to calling
Source.Reader.advance() or Source.Reader.getCurrent(). This method may perform expensive operations that
are needed to initialize the reader.
start in class Source.Reader<T>true if a record was read, false if there is no more input available.IOExceptionpublic final boolean advance()
throws IOException
Source.Readeradvance in class Source.Reader<T>true if a record was read, false if there is no more input available.IOExceptionprotected abstract boolean startImpl()
throws IOException
BoundedReader#start, except OffsetBasedSource.OffsetBasedReader base class
takes care of coordinating against concurrent calls to splitAtFraction(double).IOExceptionprotected abstract boolean advanceImpl()
throws IOException
BoundedReader#advance, except OffsetBasedSource.OffsetBasedReader base class
takes care of coordinating against concurrent calls to splitAtFraction(double).IOExceptionpublic OffsetBasedSource<T> getCurrentSource()
Source.ReaderSource describing the same input that this Reader reads
(including items already read).
A reader created from the result of getCurrentSource, if consumed, MUST
return the same data items as the current reader.
getCurrentSource in class BoundedSource.BoundedReader<T>public Double getFractionConsumed()
BoundedSource.BoundedReaderBoundedSource.BoundedReader.getCurrentSource()) this reader has read so far.
It is recommended that this method should satisfy the following properties:
Source.Reader.start() call.
Source.Reader.start() or Source.Reader.advance() call that returns false.
By default, returns null to indicate that this cannot be estimated.
BoundedSource.BoundedReader.splitAtFraction(double) is implemented, this method can be called concurrently to other
methods (including itself), and it is therefore critical for it to be implemented
in a thread-safe way.getFractionConsumed in class BoundedSource.BoundedReader<T>null if such an estimate is not available.public final OffsetBasedSource<T> splitAtFraction(double fraction)
BoundedSource.BoundedReaderReturns a BoundedSource representing the remainder.
BoundedSource<T> initial = reader.getCurrentSource();
BoundedSource<T> residual = reader.splitAtFraction(fraction);
BoundedSource<T> primary = reader.getCurrentSource();
This method should return null if the split cannot be performed for this fraction
while satisfying the semantics above. E.g., a reader that reads a range of offsets
in a file should return null if it is already past the position in its range
corresponding to the given fraction. In this case, the method MUST have no effect
(the reader must behave as if the method hadn't been called at all).
It is also very important that this method always completes quickly, in particular, it should not perform or wait on any blocking operations such as I/O, RPCs etc. Violating this requirement may stall completion of the work item or even cause it to fail.
E.g. it is incorrect to make both this method and Source.Reader.start()/Source.Reader.advance()
synchronized, because those methods can perform blocking operations, and then
this method would have to wait for those calls to complete.
RangeTracker makes it easy to implement
this method safely and correctly.
By default, returns null to indicate that splitting is not possible.
splitAtFraction in class BoundedSource.BoundedReader<T>