Interface ResultCursor

All Known Subinterfaces:
AsyncResultCursor
All Known Implementing Classes:
AsyncResultCursorImpl, DisposableAsyncResultCursor

public interface ResultCursor
The result of asynchronous execution of a Cypher query, conceptually an asynchronous stream of 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().

Important note on semantics

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).

Since:
1.5
  • Method Details

    • keys

      List<String> keys()
      Retrieve the keys of the records this result cursor contains.
      Returns:
      list of all keys.
    • consumeAsync

      Asynchronously retrieve 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 Result.list() to buffer all records into memory before summary.

      Returns:
      a CompletionStage completed with a summary for the whole query result. Stage can also be completed exceptionally if query execution fails.
    • nextAsync

      CompletionStage<Record> nextAsync()
      Asynchronously navigate to and retrieve the next Record in this result. Returned stage can contain null if end of records stream has been reached.
      Returns:
      a CompletionStage completed with a record or null. Stage can also be completed exceptionally if query execution fails.
    • peekAsync

      CompletionStage<Record> peekAsync()
      Asynchronously investigate the next upcoming Record without moving forward in the result. Returned stage can contain null if end of records stream has been reached.
      Returns:
      a CompletionStage completed with a record or null. Stage can also be completed exceptionally if query execution fails.
    • singleAsync

      CompletionStage<Record> singleAsync()
      Asynchronously return the first record in the result, failing if there is not exactly one record left in the stream.
      Returns:
      a 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.
    • forEachAsync

      CompletionStage<ResultSummary> forEachAsync(Consumer<Record> action)
      Asynchronously apply the given action to every record in the result, yielding a summary of it.
      Parameters:
      action - the function to be applied to every record in the result. Provided function should not block.
      Returns:
      a CompletionStage completed with a summary for the whole query result. Stage can also be completed exceptionally if query execution or provided function fails.
    • listAsync

      CompletionStage<List<Record>> listAsync()
      Asynchronously 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:
      a CompletionStage completed with a list of all remaining immutable records. Stage can also be completed exceptionally if query execution fails.
    • listAsync

      <T> CompletionStage<List<T>> listAsync(Function<Record,T> mapFunction)
      Asynchronously 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. See Records for some predefined functions.
      Returns:
      a CompletionStage completed with a list of all remaining immutable records. Stage can also be completed exceptionally if query execution or provided function fails.
    • isOpenAsync

      CompletionStage<Boolean> isOpenAsync()
      Determine if result is open.

      Result is considered to be open if it has not been consumed (consumeAsync()) 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:
      a CompletionStage completed with true if result is open and false otherwise.