kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | a9bf8f24e394dd8ded9cee0f0289913e4e61b685b869e222b0f6272dcfcd4090 |
|---|---|
| Author | Alexandre Dutra <adutra@users.noreply.github.com> · 2016-07-15 19:28 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-07-15 10:28 -07:00 |
| Parents | bc95e11c0ebe |
6 files changed
modified changelog/README.md +1 −0
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | - [improvement] JAVA-1233: Update HdrHistogram to 2.1.9. |
| 8 | 8 | - [improvement] JAVA-1233: Update Snappy to 1.1.2.6. |
| 9 | 9 | - [bug] JAVA-1161: Preserve full time zone info in ZonedDateTimeCodec and DateTimeCodec. |
| 10 | +- [new feature] JAVA-1157: Allow asynchronous paging of Mapper Result. | |
| 10 | 11 | |
| 11 | 12 | Merged from 3.0.x branch: |
| 12 | 13 |
added driver-core/src/main/java/com/datastax/driver/core/PagingIterable.java +166 −0
| @@ -0,0 +1,166 @@ | ||
| 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 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/ResultSet.java +5 −144
| @@ -16,11 +16,6 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | |
| 19 | -import com.google.common.util.concurrent.ListenableFuture; | |
| 20 | - | |
| 21 | -import java.util.Iterator; | |
| 22 | -import java.util.List; | |
| 23 | - | |
| 24 | 19 | /** |
| 25 | 20 | * The result of a query. |
| 26 | 21 | * <p/> |
| @@ -39,7 +34,11 @@ | ||
| 39 | 34 | * <p/> |
| 40 | 35 | * Note that this class is not thread-safe. |
| 41 | 36 | */ |
| 42 | -public interface ResultSet extends Iterable<Row> { | |
| 37 | +public interface ResultSet extends PagingIterable<ResultSet, Row> { | |
| 38 | + | |
| 39 | + // redeclared only to make clirr happy | |
| 40 | + @Override | |
| 41 | + Row one(); | |
| 43 | 42 | |
| 44 | 43 | /** |
| 45 | 44 | * Returns the columns returned in this ResultSet. |
| @@ -48,144 +47,6 @@ | ||
| 48 | 47 | */ |
| 49 | 48 | public ColumnDefinitions getColumnDefinitions(); |
| 50 | 49 | |
| 51 | - /** | |
| 52 | - * Returns whether this ResultSet has more results. | |
| 53 | - * | |
| 54 | - * @return whether this ResultSet has more results. | |
| 55 | - */ | |
| 56 | - public boolean isExhausted(); | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * Returns the next result from this ResultSet. | |
| 60 | - * | |
| 61 | - * @return the next row in this resultSet or null if this ResultSet is | |
| 62 | - * exhausted. | |
| 63 | - */ | |
| 64 | - public Row one(); | |
| 65 | - | |
| 66 | - /** | |
| 67 | - * Returns all the remaining rows in this ResultSet as a list. | |
| 68 | - * <p/> | |
| 69 | - * Note that, contrary to {@code iterator()} or successive calls to | |
| 70 | - * {@code one()}, this method forces fetching the full content of the ResultSet | |
| 71 | - * at once, holding it all in memory in particular. It is thus recommended | |
| 72 | - * to prefer iterations through {@code iterator()} when possible, especially | |
| 73 | - * if the ResultSet can be big. | |
| 74 | - * | |
| 75 | - * @return a list containing the remaining results of this ResultSet. The | |
| 76 | - * returned list is empty if and only the ResultSet is exhausted. The ResultSet | |
| 77 | - * will be exhausted after a call to this method. | |
| 78 | - */ | |
| 79 | - public List<Row> all(); | |
| 80 | - | |
| 81 | - /** | |
| 82 | - * Returns an iterator over the rows contained in this ResultSet. | |
| 83 | - * <p/> | |
| 84 | - * The {@link Iterator#next} method is equivalent to calling {@link #one}. | |
| 85 | - * So this iterator will consume results from this ResultSet and after a | |
| 86 | - * full iteration, the ResultSet will be empty. | |
| 87 | - * <p/> | |
| 88 | - * The returned iterator does not support the {@link Iterator#remove} method. | |
| 89 | - * | |
| 90 | - * @return an iterator that will consume and return the remaining rows of | |
| 91 | - * this ResultSet. | |
| 92 | - */ | |
| 93 | - @Override | |
| 94 | - public Iterator<Row> iterator(); | |
| 95 | - | |
| 96 | - /** | |
| 97 | - * The number of rows that can be retrieved from this result set without | |
| 98 | - * blocking to fetch. | |
| 99 | - * | |
| 100 | - * @return the number of rows readily available in this result set. If | |
| 101 | - * {@link #isFullyFetched()}, this is the total number of rows remaining | |
| 102 | - * in this result set (after which the result set will be exhausted). | |
| 103 | - */ | |
| 104 | - public int getAvailableWithoutFetching(); | |
| 105 | - | |
| 106 | - /** | |
| 107 | - * Whether all results from this result set have been fetched from the | |
| 108 | - * database. | |
| 109 | - * <p/> | |
| 110 | - * Note that if {@code isFullyFetched()}, then {@link #getAvailableWithoutFetching} | |
| 111 | - * will return how many rows remain in the result set before exhaustion. But | |
| 112 | - * please note that {@code !isFullyFetched()} never guarantees that the result set | |
| 113 | - * is not exhausted (you should call {@code isExhausted()} to verify it). | |
| 114 | - * | |
| 115 | - * @return whether all results have been fetched. | |
| 116 | - */ | |
| 117 | - public boolean isFullyFetched(); | |
| 118 | - | |
| 119 | - /** | |
| 120 | - * Force fetching the next page of results for this result set, if any. | |
| 121 | - * <p/> | |
| 122 | - * This method is entirely optional. It will be called automatically while | |
| 123 | - * the result set is consumed (through {@link #one}, {@link #all} or iteration) | |
| 124 | - * when needed (i.e. when {@code getAvailableWithoutFetching() == 0} and | |
| 125 | - * {@code isFullyFetched() == false}). | |
| 126 | - * <p/> | |
| 127 | - * You can however call this method manually to force the fetching of the | |
| 128 | - * next page of results. This can allow to prefetch results before they are | |
| 129 | - * strictly needed. For instance, if you want to prefetch the next page of | |
| 130 | - * results as soon as there is less than 100 rows readily available in this | |
| 131 | - * result set, you can do: | |
| 132 | - * <pre> | |
| 133 | - * ResultSet rs = session.execute(...); | |
| 134 | - * Iterator<Row> iter = rs.iterator(); | |
| 135 | - * while (iter.hasNext()) { | |
| 136 | - * if (rs.getAvailableWithoutFetching() == 100 && !rs.isFullyFetched()) | |
| 137 | - * rs.fetchMoreResults(); | |
| 138 | - * Row row = iter.next() | |
| 139 | - * ... process the row ... | |
| 140 | - * } | |
| 141 | - * </pre> | |
| 142 | - * This method is not blocking, so in the example above, the call to {@code | |
| 143 | - * fetchMoreResults} will not block the processing of the 100 currently available | |
| 144 | - * rows (but {@code iter.hasNext()} will block once those rows have been processed | |
| 145 | - * until the fetch query returns, if it hasn't yet). | |
| 146 | - * <p/> | |
| 147 | - * Only one page of results (for a given result set) can be | |
| 148 | - * fetched at any given time. If this method is called twice and the query | |
| 149 | - * triggered by the first call has not returned yet when the second one is | |
| 150 | - * performed, then the 2nd call will simply return a future on the currently | |
| 151 | - * in progress query. | |
| 152 | - * | |
| 153 | - * @return a future on the completion of fetching the next page of results. | |
| 154 | - * If the result set is already fully retrieved ({@code isFullyFetched() == true}), | |
| 155 | - * then the returned future will return immediately but not particular error will be | |
| 156 | - * thrown (you should thus call {@code isFullyFetched() to know if calling this | |
| 157 | - * method can be of any use}). | |
| 158 | - */ | |
| 159 | - public ListenableFuture<ResultSet> fetchMoreResults(); | |
| 160 | - | |
| 161 | - /** | |
| 162 | - * Returns information on the execution of the last query made for this ResultSet. | |
| 163 | - * <p/> | |
| 164 | - * Note that in most cases, a ResultSet is fetched with only one query, but large | |
| 165 | - * result sets can be paged and thus be retrieved by multiple queries. In that | |
| 166 | - * case this method return the {@code ExecutionInfo} for the last query | |
| 167 | - * performed. To retrieve the information for all queries, use {@link #getAllExecutionInfo}. | |
| 168 | - * <p/> | |
| 169 | - * The returned object includes basic information such as the queried hosts, | |
| 170 | - * but also the Cassandra query trace if tracing was enabled for the query. | |
| 171 | - * | |
| 172 | - * @return the execution info for the last query made for this ResultSet. | |
| 173 | - */ | |
| 174 | - public ExecutionInfo getExecutionInfo(); | |
| 175 | - | |
| 176 | - /** | |
| 177 | - * Return the execution information for all queries made to retrieve this | |
| 178 | - * ResultSet. | |
| 179 | - * <p/> | |
| 180 | - * Unless the ResultSet is large enough to get paged underneath, the returned | |
| 181 | - * list will be singleton. If paging has been used however, the returned list | |
| 182 | - * contains the {@code ExecutionInfo} for all the queries done to obtain this | |
| 183 | - * ResultSet (at the time of the call) in the order those queries were made. | |
| 184 | - * | |
| 185 | - * @return a list of the execution info for all the queries made for this ResultSet. | |
| 186 | - */ | |
| 187 | - public List<ExecutionInfo> getAllExecutionInfo(); | |
| 188 | - | |
| 189 | 50 | /** |
| 190 | 51 | * If the query that produced this ResultSet was a conditional update, |
| 191 | 52 | * return whether it was successfully applied. |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Mapper.java +28 −6
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | import com.datastax.driver.core.querybuilder.Insert; |
| 21 | 21 | import com.datastax.driver.core.querybuilder.QueryBuilder; |
| 22 | 22 | import com.datastax.driver.mapping.Mapper.Option.SaveNullFields; |
| 23 | +import com.datastax.driver.mapping.annotations.Accessor; | |
| 23 | 24 | import com.datastax.driver.mapping.annotations.Computed; |
| 24 | 25 | import com.google.common.base.Function; |
| 25 | 26 | import com.google.common.base.Functions; |
| @@ -705,10 +706,6 @@ | ||
| 705 | 706 | } |
| 706 | 707 | |
| 707 | 708 | /** |
| 708 | - * Creates a query to fetch entity given its PRIMARY KEY. | |
| 709 | - * <p/> | |
| 710 | - * <p/> | |
| 711 | - * /** | |
| 712 | 709 | * Maps the rows from a {@code ResultSet} into the class this is a mapper of. |
| 713 | 710 | * <p/> |
| 714 | 711 | * If the query was user-generated (that is, passed directly to {@code session.execute()} or configured with |
| @@ -728,7 +725,30 @@ | ||
| 728 | 725 | */ |
| 729 | 726 | public Result<T> map(ResultSet resultSet) { |
| 730 | 727 | boolean useAlias = !manager.isCassandraV1 && isFromMapperQuery(resultSet); |
| 731 | - return new Result<T>(resultSet, mapper, protocolVersion, useAlias); | |
| 728 | + return new Result<T>(resultSet, mapper, useAlias); | |
| 729 | + } | |
| 730 | + | |
| 731 | + /** | |
| 732 | + * Asynchronously maps the rows from a {@link ResultSetFuture} into the class this is a mapper of. | |
| 733 | + * <p/> | |
| 734 | + * Use this method to map a {@link ResultSetFuture} that was not generated by the mapper | |
| 735 | + * (e.g. a {@link ResultSetFuture} coming from a manual query or an {@link Accessor} method). | |
| 736 | + * It expects that the result set contains all column mapped in the target class, | |
| 737 | + * and that they are not aliased. {@link Computed} fields will not be filled in mapped objects. | |
| 738 | + * | |
| 739 | + * @param resultSetFuture the {@link ResultSetFuture} to map. | |
| 740 | + * @return the mapped result set future. Note that the returned mapped result set | |
| 741 | + * will encapsulate the result set returned by {@code resultSetFuture} and so consuming results from this | |
| 742 | + * returned mapped result set will consume results from that result set | |
| 743 | + * and vice-versa. | |
| 744 | + */ | |
| 745 | + public ListenableFuture<Result<T>> mapAsync(ResultSetFuture resultSetFuture) { | |
| 746 | + return Futures.transform(resultSetFuture, new Function<ResultSet, Result<T>>() { | |
| 747 | + @Override | |
| 748 | + public Result<T> apply(ResultSet rs) { | |
| 749 | + return map(rs); | |
| 750 | + } | |
| 751 | + }); | |
| 732 | 752 | } |
| 733 | 753 | |
| 734 | 754 | private boolean isFromMapperQuery(ResultSet resultSet) { |
| @@ -741,7 +761,9 @@ | ||
| 741 | 761 | */ |
| 742 | 762 | @Deprecated |
| 743 | 763 | public Result<T> mapAliased(ResultSet resultSet) { |
| 744 | - return map(resultSet); | |
| 764 | + return (manager.isCassandraV1) | |
| 765 | + ? map(resultSet) // no aliases | |
| 766 | + : new Result<T>(resultSet, mapper, true); | |
| 745 | 767 | } |
| 746 | 768 | |
| 747 | 769 | /** |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Result.java +40 −43
| @@ -16,23 +16,28 @@ | ||
| 16 | 16 | package com.datastax.driver.mapping; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.*; |
| 19 | +import com.google.common.base.Function; | |
| 20 | +import com.google.common.util.concurrent.Futures; | |
| 21 | +import com.google.common.util.concurrent.ListenableFuture; | |
| 19 | 22 | |
| 20 | 23 | import java.util.*; |
| 21 | 24 | |
| 22 | 25 | /** |
| 23 | - * A {@code ResultSet} mapped to an entity class. | |
| 26 | + * A result set whose rows are mapped to an entity class. | |
| 24 | 27 | */ |
| 25 | -public class Result<T> implements Iterable<T> { | |
| 28 | +public class Result<T> implements PagingIterable<Result<T>, T> { | |
| 26 | 29 | |
| 27 | 30 | private final ResultSet rs; |
| 28 | 31 | private final EntityMapper<T> mapper; |
| 29 | - private final ProtocolVersion protocolVersion; | |
| 30 | 32 | private final boolean useAlias; |
| 31 | 33 | |
| 32 | - Result(ResultSet rs, EntityMapper<T> mapper, ProtocolVersion protocolVersion, boolean useAlias) { | |
| 34 | + Result(ResultSet rs, EntityMapper<T> mapper) { | |
| 35 | + this(rs, mapper, false); | |
| 36 | + } | |
| 37 | + | |
| 38 | + Result(ResultSet rs, EntityMapper<T> mapper, boolean useAlias) { | |
| 33 | 39 | this.rs = rs; |
| 34 | 40 | this.mapper = mapper; |
| 35 | - this.protocolVersion = protocolVersion; | |
| 36 | 41 | this.useAlias = useAlias; |
| 37 | 42 | } |
| 38 | 43 | |
| @@ -67,33 +72,18 @@ | ||
| 67 | 72 | return true; |
| 68 | 73 | } |
| 69 | 74 | |
| 70 | - /** | |
| 71 | - * Test whether this mapped result set has more results. | |
| 72 | - * | |
| 73 | - * @return whether this mapped result set has more results. | |
| 74 | - */ | |
| 75 | + @Override | |
| 75 | 76 | public boolean isExhausted() { |
| 76 | 77 | return rs.isExhausted(); |
| 77 | 78 | } |
| 78 | 79 | |
| 79 | - /** | |
| 80 | - * Returns the next result (i.e. the entity corresponding to the next row | |
| 81 | - * in the result set). | |
| 82 | - * | |
| 83 | - * @return the next result in this mapped result set or null if it is exhausted. | |
| 84 | - */ | |
| 80 | + @Override | |
| 85 | 81 | public T one() { |
| 86 | 82 | Row row = rs.one(); |
| 87 | 83 | return row == null ? null : map(row); |
| 88 | 84 | } |
| 89 | 85 | |
| 90 | - /** | |
| 91 | - * Returns all the remaining results (entities) in this mapped result set | |
| 92 | - * as a list. | |
| 93 | - * | |
| 94 | - * @return a list containing the remaining results of this mapped result | |
| 95 | - * set. The returned list is empty if and only the result set is exhausted. | |
| 96 | - */ | |
| 86 | + @Override | |
| 97 | 87 | public List<T> all() { |
| 98 | 88 | List<Row> rows = rs.all(); |
| 99 | 89 | List<T> entities = new ArrayList<T>(rows.size()); |
| @@ -103,18 +93,6 @@ | ||
| 103 | 93 | return entities; |
| 104 | 94 | } |
| 105 | 95 | |
| 106 | - /** | |
| 107 | - * An iterator over the entities of this mapped result set. | |
| 108 | - * <p/> | |
| 109 | - * The {@link Iterator#next} method is equivalent to calling {@link #one}. | |
| 110 | - * So this iterator will consume results and after a full iteration, the | |
| 111 | - * mapped result set (and underlying {@code ResultSet}) will be empty. | |
| 112 | - * <p/> | |
| 113 | - * The returned iterator does not support the {@link Iterator#remove} method. | |
| 114 | - * | |
| 115 | - * @return an iterator that will consume and return the remaining rows of | |
| 116 | - * this mapped result set. | |
| 117 | - */ | |
| 118 | 96 | @Override |
| 119 | 97 | public Iterator<T> iterator() { |
| 120 | 98 | return new Iterator<T>() { |
| @@ -137,15 +115,34 @@ | ||
| 137 | 115 | }; |
| 138 | 116 | } |
| 139 | 117 | |
| 140 | - /** | |
| 141 | - * Returns information on the execution of this query. | |
| 142 | - * <p/> | |
| 143 | - * The returned object includes basic information such as the queried hosts, | |
| 144 | - * but also the Cassandra query trace if tracing was enabled for the query. | |
| 145 | - * | |
| 146 | - * @return the execution info for this query. | |
| 147 | - */ | |
| 118 | + @Override | |
| 148 | 119 | public ExecutionInfo getExecutionInfo() { |
| 149 | 120 | return rs.getExecutionInfo(); |
| 150 | 121 | } |
| 122 | + | |
| 123 | + @Override | |
| 124 | + public List<ExecutionInfo> getAllExecutionInfo() { | |
| 125 | + return rs.getAllExecutionInfo(); | |
| 126 | + } | |
| 127 | + | |
| 128 | + @Override | |
| 129 | + public ListenableFuture<Result<T>> fetchMoreResults() { | |
| 130 | + return Futures.transform(rs.fetchMoreResults(), new Function<ResultSet, Result<T>>() { | |
| 131 | + @Override | |
| 132 | + public Result<T> apply(ResultSet rs) { | |
| 133 | + return Result.this; | |
| 134 | + } | |
| 135 | + }); | |
| 136 | + } | |
| 137 | + | |
| 138 | + @Override | |
| 139 | + public boolean isFullyFetched() { | |
| 140 | + return rs.isFullyFetched(); | |
| 141 | + } | |
| 142 | + | |
| 143 | + @Override | |
| 144 | + public int getAvailableWithoutFetching() { | |
| 145 | + return rs.getAvailableWithoutFetching(); | |
| 146 | + } | |
| 147 | + | |
| 151 | 148 | } |
added driver-mapping/src/test/java/com/datastax/driver/mapping/MapperAsyncResultTest.java +126 −0
| @@ -0,0 +1,126 @@ | ||
| 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.mapping; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.CCMTestsSupport; | |
| 19 | +import com.datastax.driver.core.SimpleStatement; | |
| 20 | +import com.datastax.driver.core.Statement; | |
| 21 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 22 | +import com.datastax.driver.mapping.annotations.PartitionKey; | |
| 23 | +import com.datastax.driver.mapping.annotations.Table; | |
| 24 | +import com.google.common.util.concurrent.AsyncFunction; | |
| 25 | +import com.google.common.util.concurrent.Futures; | |
| 26 | +import com.google.common.util.concurrent.ListenableFuture; | |
| 27 | +import org.testng.annotations.BeforeMethod; | |
| 28 | +import org.testng.annotations.Test; | |
| 29 | + | |
| 30 | +import java.util.Set; | |
| 31 | +import java.util.concurrent.ConcurrentSkipListSet; | |
| 32 | + | |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 34 | + | |
| 35 | +/** | |
| 36 | + * Tests that a Result can be paged asynchronously. | |
| 37 | + * | |
| 38 | + * @jira_ticket JAVA-1157 | |
| 39 | + */ | |
| 40 | +@SuppressWarnings("unused") | |
| 41 | +@CassandraVersion(major = 2.0, description = "uses paging") | |
| 42 | +public class MapperAsyncResultTest extends CCMTestsSupport { | |
| 43 | + | |
| 44 | + @Override | |
| 45 | + public void onTestContextInitialized() { | |
| 46 | + execute("CREATE TABLE users (id int PRIMARY KEY, name text)"); | |
| 47 | + } | |
| 48 | + | |
| 49 | + @Table(name = "users") | |
| 50 | + static class User { | |
| 51 | + | |
| 52 | + @PartitionKey | |
| 53 | + private int id; | |
| 54 | + | |
| 55 | + private String name; | |
| 56 | + | |
| 57 | + public int getId() { | |
| 58 | + return id; | |
| 59 | + } | |
| 60 | + | |
| 61 | + public void setId(int id) { | |
| 62 | + this.id = id; | |
| 63 | + } | |
| 64 | + | |
| 65 | + public String getName() { | |
| 66 | + return name; | |
| 67 | + } | |
| 68 | + | |
| 69 | + public void setName(String name) { | |
| 70 | + this.name = name; | |
| 71 | + } | |
| 72 | + | |
| 73 | + } | |
| 74 | + | |
| 75 | + @BeforeMethod(groups = "short") | |
| 76 | + public void cleanup() { | |
| 77 | + session().execute("TRUNCATE users"); | |
| 78 | + } | |
| 79 | + | |
| 80 | + @Test(groups = "short") | |
| 81 | + public void should_iterate_single_page_result_set_asynchronously() { | |
| 82 | + should_iterate_result_set_asynchronously(100, 500); | |
| 83 | + } | |
| 84 | + | |
| 85 | + @Test(groups = "short") | |
| 86 | + public void should_iterate_multi_page_result_set_asynchronously() { | |
| 87 | + should_iterate_result_set_asynchronously(1000, 20); | |
| 88 | + } | |
| 89 | + | |
| 90 | + private void should_iterate_result_set_asynchronously(int totalCount, int fetchSize) { | |
| 91 | + for (int i = 0; i < totalCount; i++) | |
| 92 | + session().execute(String.format("INSERT INTO users (id, name) values (%d, '%s')", i, "user" + i)); | |
| 93 | + Statement statement = new SimpleStatement("SELECT * FROM users").setFetchSize(fetchSize); | |
| 94 | + Mapper<User> mapper = new MappingManager(session()).mapper(User.class); | |
| 95 | + ResultsAccumulator accumulator = new ResultsAccumulator(); | |
| 96 | + ListenableFuture<Result<User>> results = mapper.mapAsync(session().executeAsync(statement)); | |
| 97 | + ListenableFuture<Result<User>> future = Futures.transform( | |
| 98 | + results, | |
| 99 | + accumulator); | |
| 100 | + Futures.getUnchecked(future); | |
| 101 | + assertThat(accumulator.all.size()).isEqualTo(totalCount); | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Dummy transformation that accumulates all traversed results | |
| 106 | + */ | |
| 107 | + static class ResultsAccumulator implements AsyncFunction<Result<User>, Result<User>> { | |
| 108 | + | |
| 109 | + final Set<Integer> all = new ConcurrentSkipListSet<Integer>(); | |
| 110 | + | |
| 111 | + @Override | |
| 112 | + public ListenableFuture<Result<User>> apply(Result<User> users) throws Exception { | |
| 113 | + int remainingInPage = users.getAvailableWithoutFetching(); | |
| 114 | + for (User user : users) { | |
| 115 | + all.add(user.getId()); | |
| 116 | + if (--remainingInPage == 0) | |
| 117 | + break; | |
| 118 | + } | |
| 119 | + boolean wasLastPage = users.getExecutionInfo().getPagingState() == null; | |
| 120 | + if (wasLastPage) | |
| 121 | + return Futures.immediateFuture(users); | |
| 122 | + else | |
| 123 | + return Futures.transform(users.fetchMoreResults(), this); | |
| 124 | + } | |
| 125 | + } | |
| 126 | +} |