Class Source.Reader<T>
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
BoundedSource.BoundedReader
,MicrobatchSource.Reader
,UnboundedSource.UnboundedReader
This interface is deliberately distinct from Iterator
because the current
model tends to be easier to program and more efficient in practice for iterating over sources
such as files, databases etc. (rather than pure collections).
Reading data from the Source.Reader
must obey the following access pattern:
- One call to
start()
- If
start()
returned true, any number of calls togetCurrent
* methods
- If
- Repeatedly, a call to
advance()
. This may be called regardless of what the previousstart()
/advance()
returned.- If
advance()
returned true, any number of calls togetCurrent
* methods
- If
For example, if the reader is reading a fixed set of data:
try { for (boolean available = reader.start(); available; available = reader.advance()) { T item = reader.getCurrent(); Instant timestamp = reader.getCurrentTimestamp(); ... } } finally { reader.close(); }
If the set of data being read is continually growing:
try { boolean available = reader.start(); while (true) { if (available) { T item = reader.getCurrent(); Instant timestamp = reader.getCurrentTimestamp(); ... resetExponentialBackoff(); } else { exponentialBackoff(); } available = reader.advance(); } } finally { reader.close(); }
Note: this interface is a work-in-progress and may change.
All Reader
functions except getCurrentSource()
do not need to be thread-safe;
they may only be accessed by a single thread at once. However, getCurrentSource()
needs
to be thread-safe, and other functions should assume that its returned value can change
asynchronously.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract boolean
advance()
Advances the reader to the next valid record.abstract void
close()
Closes the reader.abstract T
Returns aSource
describing the same input that thisReader
currently reads (including items already read).abstract Instant
Returns the timestamp associated with the current data item.abstract boolean
start()
Initializes the reader and advances the reader to the first record.
-
Constructor Details
-
Reader
public Reader()
-
-
Method Details
-
start
Initializes the reader and advances the reader to the first record.This method should be called exactly once. The invocation should occur prior to calling
advance()
orgetCurrent()
. This method may perform expensive operations that are needed to initialize the reader.- Returns:
true
if a record was read,false
if there is no more input available.- Throws:
IOException
-
advance
Advances the reader to the next valid record.It is an error to call this without having called
start()
first.- Returns:
true
if a record was read,false
if there is no more input available.- Throws:
IOException
-
getCurrent
Returns the value of the data item that was read by the laststart()
oradvance()
call. The returned value must be effectively immutable and remain valid indefinitely.Multiple calls to this method without an intervening call to
advance()
should return the same result.- Throws:
NoSuchElementException
- ifstart()
was never called, or if the laststart()
oradvance()
returnedfalse
.
-
getCurrentTimestamp
Returns the timestamp associated with the current data item.If the source does not support timestamps, this should return
BoundedWindow.TIMESTAMP_MIN_VALUE
.Multiple calls to this method without an intervening call to
advance()
should return the same result.- Throws:
NoSuchElementException
- if the reader is at the beginning of the input andstart()
oradvance()
wasn't called, or if the laststart()
oradvance()
returnedfalse
.
-
close
Closes the reader. The reader cannot be used after this method is called.- Specified by:
close
in interfaceAutoCloseable
- Throws:
IOException
-
getCurrentSource
Returns aSource
describing the same input that thisReader
currently reads (including items already read).Usually, an implementation will simply return the immutable
Source
object from which the currentSource.Reader
was constructed, or delegate to the base class. However, when using or implementing this method on aBoundedSource.BoundedReader
, special considerations apply, see documentation forBoundedSource.BoundedReader.getCurrentSource()
.
-