records
.
The standard way of navigating through the result returned by the database is to
iterate
over it.
Results are valid until the next query is run or until the end of the current transaction,
whichever comes first. To keep a result around while further queries are run, or to use a result outside the scope
of the current transaction, see list()
.
Important note on semantics
In order to handle very large results, and to minimize memory overhead and maximize performance, results are retrieved lazily. Please seeQueryRunner
for
important details on the effects of this.
The short version is that, if you want a hard guarantee that the underlying query
has completed, you need to either call Resource.close()
on the Transaction
or Session
that created this result, or you need to use the result.
Calling any method on this interface will guarantee that any write operation has completed on the remote database.
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionconsume()
Return the result summary.boolean
hasNext()
Test if there is another record we can navigate to in this result.boolean
isOpen()
Determine if result is open.keys()
Retrieve the keys of the records this result contains.list()
Retrieve and store the entire result stream.<T> List<T>
Retrieve and store a projection of the entire result.next()
Navigate to and retrieve the nextRecord
in this result.peek()
Investigate the next upcoming record without moving forward in the result.single()
Return the first record in the result, failing if there is not exactly one record left in the streamstream()
Convert this result to a sequentialStream
of records.Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Method Details
-
keys
Retrieve the keys of the records this result contains.- Returns:
- all keys
-
hasNext
boolean hasNext()Test if there is another record we can navigate to in this result. -
next
Record next()Navigate to and retrieve the nextRecord
in this result.- Specified by:
next
in interfaceIterator<Record>
- Returns:
- the next record
- Throws:
NoSuchRecordException
- if there is no record left in the stream
-
single
Return the first record in the result, failing if there is not exactly one record left in the streamCalling this method always exhausts the result, even when
NoSuchRecordException
is thrown.- Returns:
- the first and only record in the stream
- Throws:
NoSuchRecordException
- if there is not exactly one record left in the stream
-
peek
Record peek()Investigate the next upcoming record without moving forward in the result.- Returns:
- the next record
- Throws:
NoSuchRecordException
- if there is no record left in the stream
-
stream
Convert this result to a sequentialStream
of records.Result is exhausted when a terminal operation on the returned stream is executed.
- Returns:
- sequential
Stream
of records. Empty stream if this result has already been consumed or is empty.
-
list
Retrieve and store the entire result stream. This can be used if you want to iterate over the stream multiple times or to store the whole result for later use.Note that this method can only be used if you know that the query that yielded this result returns a finite stream. Some queries can yield infinite results, in which case calling this method will lead to running out of memory.
Calling this method exhausts the result.
- Returns:
- list of all remaining immutable records
-
list
Retrieve and store a projection of the entire result. This can be used if you want to iterate over the stream multiple times or to store the whole result for later use.Note that this method can only be used if you know that the query that yielded this result returns a finite stream. Some queries can yield infinite results, in which case calling this method will lead to running out of memory.
Calling this method exhausts the result.
- Type Parameters:
T
- the type of result list elements- Parameters:
mapFunction
- a function to map from Record to T. SeeRecords
for some predefined functions.- Returns:
- list of all mapped remaining immutable records
-
consume
ResultSummary consume()Return the result summary.If the records in the result is not fully consumed, then calling this method will exhausts the result.
If you want to access unconsumed records after summary, you shall use
list()
to buffer all records into memory before summary.- Returns:
- a summary for the whole query result.
-
isOpen
boolean isOpen()Determine if result is open.Result is considered to be open if it has not been consumed (
consume()
) and its creator object (e.g. session or transaction) has not been closed (including committed or rolled back).Attempts to access data on closed result will produce
ResultConsumedException
.- Returns:
true
if result is open andfalse
otherwise.
-