|
| 1 | +/* |
| 2 | + * Copyright 2016 Basho Technologies, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.basho.riak.client.api; |
| 18 | + |
| 19 | +import com.basho.riak.client.api.commands.ChunkedResponseIterator; |
| 20 | +import com.basho.riak.client.api.commands.ImmediateCoreFutureAdapter; |
| 21 | +import com.basho.riak.client.core.*; |
| 22 | + |
| 23 | +import java.util.Iterator; |
| 24 | + |
| 25 | +/* |
| 26 | + * The base class for all Streamable Riak Commands. |
| 27 | + * Allows the user to either use {@link RiakCommand#executeAsync} and return a "batch-mode" result |
| 28 | + * that is only available after the command is complete, or |
| 29 | + * use {@link StreamableRiakCommand#executeAsyncStreaming} and return a "immediate" or "stream-mode" result |
| 30 | + * that data will flow into. |
| 31 | + * @param <S> The response type returned by "streaming mode" {@link executeAsyncStreaming} |
| 32 | + * @param <R> The response type returned by the "batch mode" @{link executeAsync} |
| 33 | + * @param <I> The query info type |
| 34 | + * @author Alex Moore <amoore at basho.com> |
| 35 | + * @author Sergey Galkin <srggal at gmail dot com> |
| 36 | + * @since 2.0 |
| 37 | + */ |
| 38 | +public abstract class StreamableRiakCommand<R extends StreamableRiakCommand.StreamableResponse, I, CoreR, CoreI> extends GenericRiakCommand<R, I, CoreR, CoreI> |
| 39 | +{ |
| 40 | + public static abstract class StreamableRiakCommandWithSameInfo<R extends StreamableResponse, I, CoreR> extends StreamableRiakCommand<R,I, CoreR, I> |
| 41 | + { |
| 42 | + @Override |
| 43 | + protected I convertInfo(I coreInfo) { |
| 44 | + return coreInfo; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static abstract class StreamableResponse<T, S> implements Iterable<T> |
| 49 | + { |
| 50 | + protected ChunkedResponseIterator<T, ?, S> chunkedResponseIterator; |
| 51 | + |
| 52 | + /** |
| 53 | + * Constructor for streamable response |
| 54 | + * @param chunkedResponseIterator |
| 55 | + */ |
| 56 | + protected StreamableResponse(ChunkedResponseIterator<T, ?, S> chunkedResponseIterator) { |
| 57 | + this.chunkedResponseIterator = chunkedResponseIterator; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Constructor for not streamable response. |
| 62 | + */ |
| 63 | + protected StreamableResponse() |
| 64 | + { |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Whether the results are to be streamed back. |
| 69 | + * If true, results will appear in this class's iterator. |
| 70 | + * If false, they will appear in the original result collection. |
| 71 | + * @return true if the results are to be streamed. |
| 72 | + */ |
| 73 | + public boolean isStreaming() |
| 74 | + { |
| 75 | + return chunkedResponseIterator != null; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Get an iterator over the result data. |
| 80 | + * |
| 81 | + * If using the streaming API, this method will block |
| 82 | + * and wait for more data if none is immediately available. |
| 83 | + * It is also advisable to check {@link Thread#isInterrupted()} |
| 84 | + * in environments where thread interrupts must be obeyed. |
| 85 | + * |
| 86 | + * @return an iterator over the result data. |
| 87 | + */ |
| 88 | + @Override |
| 89 | + public Iterator<T> iterator() { |
| 90 | + if (isStreaming()) { |
| 91 | + assert chunkedResponseIterator != null; |
| 92 | + return chunkedResponseIterator; |
| 93 | + } |
| 94 | + |
| 95 | + throw new UnsupportedOperationException("Iterating is only supported for streamable response"); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + protected abstract R createResponse(int timeout, StreamingRiakFuture<CoreR, CoreI> coreFuture); |
| 100 | + |
| 101 | + protected abstract PBStreamingFutureOperation<CoreR, ?, CoreI> buildCoreOperation(boolean streamResults); |
| 102 | + |
| 103 | + @Override |
| 104 | + protected final FutureOperation<CoreR, ?, CoreI> buildCoreOperation() { |
| 105 | + return buildCoreOperation(false); |
| 106 | + } |
| 107 | + |
| 108 | + protected final RiakFuture<R, I> executeAsyncStreaming(RiakCluster cluster, int timeout) |
| 109 | + { |
| 110 | + final PBStreamingFutureOperation<CoreR, ?, CoreI> coreOperation = buildCoreOperation(true); |
| 111 | + final StreamingRiakFuture<CoreR, CoreI> coreFuture = cluster.execute(coreOperation); |
| 112 | + |
| 113 | + final R r = createResponse(timeout, coreFuture); |
| 114 | + |
| 115 | + final ImmediateCoreFutureAdapter<R,I, CoreR, CoreI> future = new ImmediateCoreFutureAdapter<R,I,CoreR,CoreI>(coreFuture, r) |
| 116 | + { |
| 117 | + @Override |
| 118 | + protected R convertResponse(CoreR response) |
| 119 | + { |
| 120 | + return StreamableRiakCommand.this.convertResponse(coreOperation, response); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + protected I convertQueryInfo(CoreI coreQueryInfo) |
| 125 | + { |
| 126 | + return StreamableRiakCommand.this.convertInfo(coreQueryInfo); |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + coreFuture.addListener(future); |
| 131 | + return future; |
| 132 | + } |
| 133 | +} |
0 commit comments