|
| 1 | +/* |
| 2 | + * Copyright (C) 2012-2015 DataStax 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 | +package com.datastax.driver.core; |
| 17 | + |
| 18 | +import com.google.common.util.concurrent.ListenableFuture; |
| 19 | + |
| 20 | +import java.util.Iterator; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +/** |
| 24 | + * Defines an iterable whose elements can be remotely fetched and paged, |
| 25 | + * possibly asynchronously. |
| 26 | + */ |
| 27 | +public interface PagingIterable<S extends PagingIterable<S, T>, T> extends Iterable<T> { |
| 28 | + |
| 29 | + /** |
| 30 | + * Returns whether this result set has more results. |
| 31 | + * |
| 32 | + * @return whether this result set has more results. |
| 33 | + */ |
| 34 | + boolean isExhausted(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Whether all results from this result set have been fetched from the |
| 38 | + * database. |
| 39 | + * <p/> |
| 40 | + * Note that if {@code isFullyFetched()}, then {@link #getAvailableWithoutFetching} |
| 41 | + * will return how many rows remain in the result set before exhaustion. But |
| 42 | + * please note that {@code !isFullyFetched()} never guarantees that the result set |
| 43 | + * is not exhausted (you should call {@link #isExhausted()} to verify it). |
| 44 | + * |
| 45 | + * @return whether all results have been fetched. |
| 46 | + */ |
| 47 | + boolean isFullyFetched(); |
| 48 | + |
| 49 | + /** |
| 50 | + * The number of rows that can be retrieved from this result set without |
| 51 | + * blocking to fetch. |
| 52 | + * |
| 53 | + * @return the number of rows readily available in this result set. If |
| 54 | + * {@link #isFullyFetched()}, this is the total number of rows remaining |
| 55 | + * in this result set (after which the result set will be exhausted). |
| 56 | + */ |
| 57 | + int getAvailableWithoutFetching(); |
| 58 | + |
| 59 | + /** |
| 60 | + * Force fetching the next page of results for this result set, if any. |
| 61 | + * <p/> |
| 62 | + * This method is entirely optional. It will be called automatically while |
| 63 | + * the result set is consumed (through {@link #one}, {@link #all} or iteration) |
| 64 | + * when needed (i.e. when {@code getAvailableWithoutFetching() == 0} and |
| 65 | + * {@code isFullyFetched() == false}). |
| 66 | + * <p/> |
| 67 | + * You can however call this method manually to force the fetching of the |
| 68 | + * next page of results. This can allow to prefetch results before they are |
| 69 | + * strictly needed. For instance, if you want to prefetch the next page of |
| 70 | + * results as soon as there is less than 100 rows readily available in this |
| 71 | + * result set, you can do: |
| 72 | + * <pre> |
| 73 | + * ResultSet rs = session.execute(...); |
| 74 | + * Iterator<Row> iter = rs.iterator(); |
| 75 | + * while (iter.hasNext()) { |
| 76 | + * if (rs.getAvailableWithoutFetching() == 100 && !rs.isFullyFetched()) |
| 77 | + * rs.fetchMoreResults(); |
| 78 | + * Row row = iter.next() |
| 79 | + * ... process the row ... |
| 80 | + * } |
| 81 | + * </pre> |
| 82 | + * This method is not blocking, so in the example above, the call to {@code |
| 83 | + * fetchMoreResults} will not block the processing of the 100 currently available |
| 84 | + * rows (but {@code iter.hasNext()} will block once those rows have been processed |
| 85 | + * until the fetch query returns, if it hasn't yet). |
| 86 | + * <p/> |
| 87 | + * Only one page of results (for a given result set) can be |
| 88 | + * fetched at any given time. If this method is called twice and the query |
| 89 | + * triggered by the first call has not returned yet when the second one is |
| 90 | + * performed, then the 2nd call will simply return a future on the currently |
| 91 | + * in progress query. |
| 92 | + * |
| 93 | + * @return a future on the completion of fetching the next page of results. |
| 94 | + * If the result set is already fully retrieved ({@code isFullyFetched() == true}), |
| 95 | + * then the returned future will return immediately but not particular error will be |
| 96 | + * thrown (you should thus call {@link #isFullyFetched()} to know if calling this |
| 97 | + * method can be of any use}). |
| 98 | + */ |
| 99 | + ListenableFuture<S> fetchMoreResults(); |
| 100 | + |
| 101 | + /** |
| 102 | + * Returns the next result from this result set. |
| 103 | + * |
| 104 | + * @return the next row in this result set or null if this result set is |
| 105 | + * exhausted. |
| 106 | + */ |
| 107 | + T one(); |
| 108 | + |
| 109 | + /** |
| 110 | + * Returns all the remaining rows in this result set as a list. |
| 111 | + * <p/> |
| 112 | + * Note that, contrary to {@link #iterator()} or successive calls to |
| 113 | + * {@link #one()}, this method forces fetching the full content of the result set |
| 114 | + * at once, holding it all in memory in particular. It is thus recommended |
| 115 | + * to prefer iterations through {@link #iterator()} when possible, especially |
| 116 | + * if the result set can be big. |
| 117 | + * |
| 118 | + * @return a list containing the remaining results of this result set. The |
| 119 | + * returned list is empty if and only the result set is exhausted. The result set |
| 120 | + * will be exhausted after a call to this method. |
| 121 | + */ |
| 122 | + List<T> all(); |
| 123 | + |
| 124 | + /** |
| 125 | + * Returns an iterator over the rows contained in this result set. |
| 126 | + * <p/> |
| 127 | + * The {@link Iterator#next} method is equivalent to calling {@link #one}. |
| 128 | + * So this iterator will consume results from this result set and after a |
| 129 | + * full iteration, the result set will be empty. |
| 130 | + * <p/> |
| 131 | + * The returned iterator does not support the {@link Iterator#remove} method. |
| 132 | + * |
| 133 | + * @return an iterator that will consume and return the remaining rows of |
| 134 | + * this result set. |
| 135 | + */ |
| 136 | + Iterator<T> iterator(); |
| 137 | + |
| 138 | + /** |
| 139 | + * Returns information on the execution of the last query made for this result set. |
| 140 | + * <p/> |
| 141 | + * Note that in most cases, a result set is fetched with only one query, but large |
| 142 | + * result sets can be paged and thus be retrieved by multiple queries. In that |
| 143 | + * case this method return the {@link ExecutionInfo} for the last query |
| 144 | + * performed. To retrieve the information for all queries, use {@link #getAllExecutionInfo}. |
| 145 | + * <p/> |
| 146 | + * The returned object includes basic information such as the queried hosts, |
| 147 | + * but also the Cassandra query trace if tracing was enabled for the query. |
| 148 | + * |
| 149 | + * @return the execution info for the last query made for this result set. |
| 150 | + */ |
| 151 | + ExecutionInfo getExecutionInfo(); |
| 152 | + |
| 153 | + /** |
| 154 | + * Return the execution information for all queries made to retrieve this |
| 155 | + * result set. |
| 156 | + * <p/> |
| 157 | + * Unless the result set is large enough to get paged underneath, the returned |
| 158 | + * list will be singleton. If paging has been used however, the returned list |
| 159 | + * contains the {@link ExecutionInfo} objects for all the queries done to obtain this |
| 160 | + * result set (at the time of the call) in the order those queries were made. |
| 161 | + * |
| 162 | + * @return a list of the execution info for all the queries made for this result set. |
| 163 | + */ |
| 164 | + List<ExecutionInfo> getAllExecutionInfo(); |
| 165 | + |
| 166 | +} |
0 commit comments