Class LazyCursor<T>

  • Type Parameters:
    T - the type of elements of the cursor
    All Implemented Interfaces:
    RecordCursor<T>, AutoCloseable, Iterator<T>

    @API(MAINTAINED)
    public class LazyCursor<T>
    extends Object
    implements RecordCursor<T>
    Wraps a future that supplies a record cursor. A typical use case for this cursor will be when the type of cursor that is to be produced by depend on the result of another future. For example:
        return new LazyCursor(isTheThingReady().thenCompose( isThingReady -> {
            if (isThingReady) {
                return new KeyValueCursor(...);
            } else {
                return RecordCursor.empty();
            }
        } ));
     
    Due to the lazy nature of the cursor, it is impermissable to call any synchronous methods that return information about the state of the cursor until onHasNext() has been called.
    • Constructor Detail

      • LazyCursor

        public LazyCursor​(@Nonnull
                          CompletableFuture<RecordCursor<T>> futureCursor,
                          @Nullable
                          Executor executor)
        Creates a new lazy cursor.
        Parameters:
        futureCursor - the future that will ultimately supply the actual underlying cursor
        executor - if not null, then this executor will be returned from getExecutor(), otherwise getExecutor() will throw an exception if called before the underlying cursor has been materialized. It is advisable to provide this value when chaining cursors in the event that another wrapping cursor may depend on the executor from this cursor before the cursor is used.
    • Method Detail

      • onNext

        @Nonnull
        public CompletableFuture<RecordCursorResult<T>> onNext()
        Description copied from interface: RecordCursor
        Asynchronously return the next result from this cursor. When complete, the future will contain a RecordCursorResult, which represents exactly one of the following:
        1. The next object of type T produced by the cursor. In addition to the next record, this result includes a RecordCursorContinuation that can be used to continue the cursor after the last record returned. The returned continuation is guaranteed not to be an "end continuation" representing the end of the cursor: specifically, RecordCursorContinuation.isEnd() is always false on the returned continuation.
        2. The fact that the cursor is stopped and cannot produce another record and a RecordCursor.NoNextReason that explains why no record could be produced. The result include a continuation that can be used to continue the cursor after the last record returned. If the result's NoNextReason is anything other than RecordCursor.NoNextReason.SOURCE_EXHAUSTED, the returned continuation must not be an end continuation. Conversely, if the result's NoNextReason is SOURCE_EXHAUSTED, then the returned continuation must be an an "end continuation".
        In either case, the returned RecordCursorContinuation can be serialized to an opaque byte array using RecordCursorContinuation.toBytes(). This can be passed back into a new cursor of the same type, with all other parameters remaining the same.
        Specified by:
        onNext in interface RecordCursor<T>
        Returns:
        a future for the next result from this cursor representing either the next record or an indication of why the cursor stopped
        See Also:
        RecordCursorResult, RecordCursorContinuation
      • getContinuation

        @Nullable
        @Deprecated
        public byte[] getContinuation()
        Deprecated.
        Description copied from interface: RecordCursor
        Get a byte string that can be used to continue a query after the last record returned.
        Specified by:
        getContinuation in interface RecordCursor<T>
        Returns:
        opaque byte array denoting where the cursor should pick up. This can be passed back into a new cursor of the same type, with all other parameters remaining the same. Returns null if the underlying source is completely exhausted, independent of any limit passed to the cursor creator. Since such creators generally accept null to mean no continuation, that is, start from the beginning, one must check for null from getContinuation to keep from starting over. Result is not always defined if called before onHasNext or before next after onHasNext has returned true. That is, a continuation is only guaranteed when called "between" records from a while (hasNext) next loop or after its end.
      • accept

        public boolean accept​(@Nonnull
                              RecordCursorVisitor visitor)
        Description copied from interface: RecordCursor
        Accept a visit from hierarchical visitor, which implements RecordCursorVisitor. By contract, implementations of this method must return the value of visitor.visitLeave(this), which determines whether or not subsequent siblings of this cursor should be visited.
        Specified by:
        accept in interface RecordCursor<T>
        Parameters:
        visitor - a hierarchical visitor
        Returns:
        true if the subsequent siblings of the cursor should be visited, and false otherwise