public interface ResultCursor
records
.
Result can be eagerly fetched in a list using listAsync()
or navigated lazily using
forEachAsync(Consumer)
or nextAsync()
.
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 listAsync()
.
In order to handle very large results, and to minimize memory overhead and maximize
performance, results are retrieved lazily. Please see AsyncQueryRunner
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 AsyncTransaction.commitAsync()
on the transaction
or AsyncSession.closeAsync()
on the session
that created this result, or you need to use
the result.
Note: Every returned CompletionStage
can be completed by an IO thread which should never block.
Otherwise IO operations on this and potentially other network connections might deadlock. Please do not chain
blocking operations like CompletableFuture.get()
on the returned stages. Consider using asynchronous calls
throughout the chain or offloading blocking operation to a different Executor
. This can be done using
methods with "Async" suffix like CompletionStage.thenApplyAsync(java.util.function.Function)
or
CompletionStage.thenApplyAsync(java.util.function.Function, Executor)
.
Modifier and Type | Method and Description |
---|---|
CompletionStage<ResultSummary> |
consumeAsync()
Asynchronously retrieve the result summary.
|
CompletionStage<ResultSummary> |
forEachAsync(Consumer<Record> action)
Asynchronously apply the given
action to every record in the result, yielding a summary of it. |
List<String> |
keys()
Retrieve the keys of the records this result cursor contains.
|
CompletionStage<List<Record>> |
listAsync()
Asynchronously retrieve and store the entire result stream.
|
<T> CompletionStage<List<T>> |
listAsync(Function<Record,T> mapFunction)
Asynchronously retrieve and store a projection of the entire result.
|
CompletionStage<Record> |
nextAsync()
Asynchronously navigate to and retrieve the next
Record in this result. |
CompletionStage<Record> |
peekAsync()
Asynchronously investigate the next upcoming
Record without moving forward in the result. |
CompletionStage<Record> |
singleAsync()
Asynchronously return the first record in the result, failing if there is not exactly
one record left in the stream.
|
List<String> keys()
CompletionStage<ResultSummary> consumeAsync()
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 Result.list()
to buffer all records into memory before summary.
CompletionStage
completed with a summary for the whole query result. Stage can also be
completed exceptionally if query execution fails.CompletionStage<Record> nextAsync()
Record
in this result. Returned stage can contain
null
if end of records stream has been reached.CompletionStage
completed with a record or null
. Stage can also be
completed exceptionally if query execution fails.CompletionStage<Record> peekAsync()
Record
without moving forward in the result. Returned
stage can contain null
if end of records stream has been reached.CompletionStage
completed with a record or null
. Stage can also be
completed exceptionally if query execution fails.CompletionStage<Record> singleAsync()
CompletionStage
completed with the first and only record in the stream. Stage will be
completed exceptionally with NoSuchRecordException
if there is not exactly one record left in the
stream. It can also be completed exceptionally if query execution fails.CompletionStage<ResultSummary> forEachAsync(Consumer<Record> action)
action
to every record in the result, yielding a summary of it.action
- the function to be applied to every record in the result. Provided function should not block.CompletionStage
completed with a summary for the whole query result. Stage can also be
completed exceptionally if query execution or provided function fails.CompletionStage<List<Record>> listAsync()
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.
CompletionStage
completed with a list of all remaining immutable records. Stage can also be
completed exceptionally if query execution fails.<T> CompletionStage<List<T>> listAsync(Function<Record,T> mapFunction)
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.
T
- the type of result list elementsmapFunction
- a function to map from Record to T. See Records
for some predefined functions.CompletionStage
completed with a list of all remaining immutable records. Stage can also be
completed exceptionally if query execution or provided function fails.