Interface AsyncResultSet

All Superinterfaces:
AutoCloseable, ResultSet, StructReader
All Known Implementing Classes:
ForwardingAsyncResultSet

public interface AsyncResultSet extends ResultSet
Interface for result sets returned by async query methods.
  • Method Details

    • tryNext

      Non-blocking call that attempts to step the cursor to the next position in the stream. The cursor may be inspected only if the cursor returns CursorState.OK.

      A caller will typically call tryNext in a loop inside the ReadyCallback, consuming all results available. For more information see setCallback(Executor, ReadyCallback).

      Currently this method may only be called if a ReadyCallback has been registered. This is for safety purposes only, and may be relaxed in future.

      Returns:
      current cursor readiness state
      Throws:
      SpannerException - When an unrecoverable problem downstream occurs. Once this occurs you will get no further callbacks. You should return CallbackResponse.DONE back from callback.
    • setCallback

      com.google.api.core.ApiFuture<Void> setCallback(Executor exec, AsyncResultSet.ReadyCallback cb)
      Register a callback with the ResultSet to be made aware when more data is available, changing the usage pattern from sync to async. Details:

      Flow Control

      If no flow control is needed (say because result sizes are known in advance to be finite in size) then async processing is simple. The following is a code example that transfers work from the cursor to an upstream sink:
      
       @Override
       public CallbackResponse cursorReady(ResultSet cursor) {
         try {
           while (true) {
             switch (cursor.tryNext()) {
               case OK:    upstream.emit(cursor.getRow()); break;
               case DONE:  upstream.done(); return CallbackResponse.DONE;
               case NOT_READY:  return CallbackResponse.CONTINUE;
             }
           }
         } catch (SpannerException e) {
           upstream.doneWithError(e);
           return CallbackResponse.DONE;
         }
       }
       
      Flow control may be needed if for example the upstream system may not always be ready to handle more data. In this case the app developer has two main options:
      • Semi-async: make upstream.emit() a blocking call. This will block the callback thread until progress is possible. When coding in this way the threads in the Executor provided to setCallback must be blockable without causing harm to progress in your system.
      • Full-async: call cursor.pause() and return from the callback with data still in the Cursor. Once in this state cursor waits until resume() is called before calling callback again.
      Parameters:
      exec - executor on which to run all callbacks. Typically use a threadpool. If the executor is one that runs the work on the submitting thread, you must be very careful not to throw RuntimeException up the stack, lest you do damage to calling components. For example, it may cause an event dispatcher thread to crash.
      cb - ready callback
      Returns:
      An ApiFuture that returns null when the consumption of the AsyncResultSet has finished successfully. No more calls to the AsyncResultSet.ReadyCallback will follow and all resources used by the AsyncResultSet have been cleaned up. The ApiFuture throws an ExecutionException if the consumption of the AsyncResultSet finished with an error.
    • cancel

      void cancel()
      Attempt to cancel this operation and free all resources. Non-blocking. This is a no-op for child row cursors and does not cancel the parent cursor.
    • resume

      void resume()
      Resume callbacks from the cursor. If there is more data available, a callback will be dispatched immediately. This can be called from any thread.
    • toListAsync

      <T> com.google.api.core.ApiFuture<List<T>> toListAsync(com.google.common.base.Function<StructReader,T> transformer, Executor executor)
      Transforms the row cursor into an immutable list using the given transformer function. transformer will be called once per row, thus the returned list will contain one entry per row. The returned future will throw a SpannerException if the row cursor encountered any error or if the transformer threw an exception on any row.

      The transformer will be run on the supplied executor. The implementation may batch multiple transformer invocations together into a single Runnable when possible to increase efficiency. At any point in time, there will be at most one invocation of the transformer in progress.

      WARNING: This will result in materializing the entire list so this should be used judiciously after considering the memory requirements of the returned list.

      WARNING: The RowBase object passed to transformer function is not immutable and is not guaranteed to remain valid after the transformer function returns. The same RowBase object might be passed multiple times to the transformer with different underlying data each time. So *NEVER* keep a reference to the RowBase outside of the transformer. Specifically do not use Functions.identity() function.

      Parameters:
      transformer - function which will be used to transform the row. It should not return null.
      executor - executor on which the transformer will be run. This should ideally not be an inline executor such as MoreExecutors.directExecutor(); using such an executor may degrade the performance of the Spanner library.
    • toList

      <T> List<T> toList(com.google.common.base.Function<StructReader,T> transformer) throws SpannerException
      Transforms the row cursor into an immutable list using the given transformer function. transformer will be called once per row, thus the returned list will contain one entry per row. This method will block until all the rows have been yielded by the cursor.

      WARNING: This will result in consuming the entire list so this should be used judiciously after considering the memory requirements of the returned list.

      WARNING: The RowBase object passed to transformer function is not immutable and is not guaranteed to remain valid after the transformer function returns. The same RowBase object might be passed multiple times to the transformer with different underlying data each time. So *NEVER* keep a reference to the RowBase outside of the transformer. Specifically do not use Functions.identity() function.

      Parameters:
      transformer - function which will be used to transform the row. It should not return null.
      Throws:
      SpannerException