kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | f1f8f4e3bbe95d416a112b33f1c1383eb3dab35455721510128841489535bd9d |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-07-24 22:00 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-07-24 22:00 +02:00 |
| Parents | 382208d6a566, 7bef602fb122 |
5 files changed
modified changelog/README.md +2 −0
| @@ -6,6 +6,8 @@ | ||
| 6 | 6 | - [bug] DateCodec does not format values correctly (JAVA-816) |
| 7 | 7 | - [bug] TimeCodec does not format values correctly (JAVA-817) |
| 8 | 8 | - [bug] TypeCodec.getDataTypeFor() does not handle LocalDate instances (JAVA-818) |
| 9 | +- [improvement] Make ResultSet#fetchMoreResult return a | |
| 10 | + ListenableFuture<ResultSet> (JAVA-836) | |
| 9 | 11 | |
| 10 | 12 | Merged from 2.1 branch: |
| 11 | 13 |
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +11 −10
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | import java.util.concurrent.ExecutionException; |
| 22 | 22 | import java.util.concurrent.LinkedBlockingDeque; |
| 23 | 23 | |
| 24 | +import com.google.common.util.concurrent.Futures; | |
| 24 | 25 | import com.google.common.util.concurrent.ListenableFuture; |
| 25 | 26 | import com.google.common.util.concurrent.SettableFuture; |
| 26 | 27 | import com.google.common.util.concurrent.Uninterruptibles; |
| @@ -187,8 +188,8 @@ | ||
| 187 | 188 | return true; |
| 188 | 189 | } |
| 189 | 190 | |
| 190 | - public ListenableFuture<Void> fetchMoreResults() { | |
| 191 | - return MoreFutures.VOID_SUCCESS; | |
| 191 | + public ListenableFuture<ResultSet> fetchMoreResults() { | |
| 192 | + return Futures.<ResultSet>immediateFuture(this); | |
| 192 | 193 | } |
| 193 | 194 | |
| 194 | 195 | public ExecutionInfo getExecutionInfo() { |
| @@ -292,25 +293,25 @@ | ||
| 292 | 293 | } |
| 293 | 294 | } |
| 294 | 295 | |
| 295 | - public ListenableFuture<Void> fetchMoreResults() { | |
| 296 | + public ListenableFuture<ResultSet> fetchMoreResults() { | |
| 296 | 297 | return fetchMoreResults(this.fetchState); |
| 297 | 298 | } |
| 298 | 299 | |
| 299 | - private ListenableFuture<Void> fetchMoreResults(FetchingState fetchState) { | |
| 300 | + private ListenableFuture<ResultSet> fetchMoreResults(FetchingState fetchState) { | |
| 300 | 301 | if (fetchState == null) |
| 301 | - return MoreFutures.VOID_SUCCESS; | |
| 302 | + return Futures.<ResultSet>immediateFuture(this); | |
| 302 | 303 | |
| 303 | 304 | if (fetchState.inProgress != null) |
| 304 | 305 | return fetchState.inProgress; |
| 305 | 306 | |
| 306 | 307 | assert fetchState.nextStart != null; |
| 307 | 308 | ByteBuffer state = fetchState.nextStart; |
| 308 | - SettableFuture<Void> future = SettableFuture.create(); | |
| 309 | + SettableFuture<ResultSet> future = SettableFuture.create(); | |
| 309 | 310 | this.fetchState = new FetchingState(null, future); |
| 310 | 311 | return queryNextPage(state, future); |
| 311 | 312 | } |
| 312 | 313 | |
| 313 | - private ListenableFuture<Void> queryNextPage(ByteBuffer nextStart, final SettableFuture<Void> future) { | |
| 314 | + private ListenableFuture<ResultSet> queryNextPage(ByteBuffer nextStart, final SettableFuture<ResultSet> future) { | |
| 314 | 315 | |
| 315 | 316 | assert !(statement instanceof BatchStatement); |
| 316 | 317 | |
| @@ -351,7 +352,7 @@ | ||
| 351 | 352 | } |
| 352 | 353 | |
| 353 | 354 | MultiPage.this.infos.offer(info); |
| 354 | - future.set(null); | |
| 355 | + future.set(MultiPage.this); | |
| 355 | 356 | break; |
| 356 | 357 | case ERROR: |
| 357 | 358 | future.setException(((Responses.Error)response).asException(connection.address)); |
| @@ -406,9 +407,9 @@ | ||
| 406 | 407 | |
| 407 | 408 | private static class FetchingState { |
| 408 | 409 | public final ByteBuffer nextStart; |
| 409 | - public final ListenableFuture<Void> inProgress; | |
| 410 | + public final ListenableFuture<ResultSet> inProgress; | |
| 410 | 411 | |
| 411 | - FetchingState(ByteBuffer nextStart, ListenableFuture<Void> inProgress) { | |
| 412 | + FetchingState(ByteBuffer nextStart, ListenableFuture<ResultSet> inProgress) { | |
| 412 | 413 | assert (nextStart == null) != (inProgress == null); |
| 413 | 414 | this.nextStart = nextStart; |
| 414 | 415 | this.inProgress = inProgress; |
modified driver-core/src/main/java/com/datastax/driver/core/ResultSet.java +1 −1
| @@ -156,7 +156,7 @@ | ||
| 156 | 156 | * thrown (you should thus call {@code isFullyFetched() to know if calling this |
| 157 | 157 | * method can be of any use}). |
| 158 | 158 | */ |
| 159 | - public ListenableFuture<Void> fetchMoreResults(); | |
| 159 | + public ListenableFuture<ResultSet> fetchMoreResults(); | |
| 160 | 160 | |
| 161 | 161 | /** |
| 162 | 162 | * Returns information on the execution of the last query made for this ResultSet. |
added driver-core/src/test/java/com/datastax/driver/core/AsyncResultSetTest.java +91 −0
| @@ -0,0 +1,91 @@ | ||
| 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 java.util.Collection; | |
| 19 | +import java.util.Set; | |
| 20 | +import java.util.concurrent.ConcurrentSkipListSet; | |
| 21 | + | |
| 22 | +import com.google.common.collect.Lists; | |
| 23 | +import com.google.common.util.concurrent.AsyncFunction; | |
| 24 | +import com.google.common.util.concurrent.Futures; | |
| 25 | +import com.google.common.util.concurrent.ListenableFuture; | |
| 26 | +import org.testng.annotations.BeforeMethod; | |
| 27 | +import org.testng.annotations.Test; | |
| 28 | + | |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 30 | + | |
| 31 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 32 | + | |
| 33 | +@CassandraVersion(major = 2.0, description = "uses paging") | |
| 34 | +public class AsyncResultSetTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 35 | + @Override | |
| 36 | + protected Collection<String> getTableDefinitions() { | |
| 37 | + return Lists.newArrayList( | |
| 38 | + "create table ints (i int primary key)"); | |
| 39 | + } | |
| 40 | + | |
| 41 | + @BeforeMethod(groups = "short") | |
| 42 | + public void cleanup() { | |
| 43 | + session.execute("truncate ints"); | |
| 44 | + } | |
| 45 | + | |
| 46 | + @Test(groups = "short") | |
| 47 | + public void should_iterate_single_page_result_set_asynchronously() { | |
| 48 | + should_iterate_result_set_asynchronously(100, 500); | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Test(groups = "short") | |
| 52 | + public void should_iterate_multi_page_result_set_asynchronously() { | |
| 53 | + should_iterate_result_set_asynchronously(1000, 20); | |
| 54 | + } | |
| 55 | + | |
| 56 | + private void should_iterate_result_set_asynchronously(int totalCount, int fetchSize) { | |
| 57 | + for (int i = 0; i < totalCount; i++) | |
| 58 | + session.execute(String.format("insert into ints (i) values (%d)", i)); | |
| 59 | + | |
| 60 | + Statement statement = new SimpleStatement("select * from ints").setFetchSize(fetchSize); | |
| 61 | + ResultsAccumulator results = new ResultsAccumulator(); | |
| 62 | + | |
| 63 | + ListenableFuture<ResultSet> future = Futures.transform( | |
| 64 | + session.executeAsync(statement), | |
| 65 | + results); | |
| 66 | + | |
| 67 | + Futures.getUnchecked(future); | |
| 68 | + | |
| 69 | + assertThat(results.all.size()).isEqualTo(totalCount); | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** Dummy tranformation that accumulates all traversed results */ | |
| 73 | + static class ResultsAccumulator implements AsyncFunction<ResultSet, ResultSet> { | |
| 74 | + final Set<Integer> all = new ConcurrentSkipListSet<Integer>(); | |
| 75 | + | |
| 76 | + @Override | |
| 77 | + public ListenableFuture<ResultSet> apply(ResultSet rs) throws Exception { | |
| 78 | + int remainingInPage = rs.getAvailableWithoutFetching(); | |
| 79 | + for (Row row : rs) { | |
| 80 | + all.add(row.getInt(0)); | |
| 81 | + if (--remainingInPage == 0) | |
| 82 | + break; | |
| 83 | + } | |
| 84 | + boolean wasLastPage = rs.getExecutionInfo().getPagingState() == null; | |
| 85 | + if (wasLastPage) | |
| 86 | + return Futures.immediateFuture(rs); | |
| 87 | + else | |
| 88 | + return Futures.transform(rs.fetchMoreResults(), this); | |
| 89 | + } | |
| 90 | + } | |
| 91 | +} |
modified upgrade_guide/README.md +6 −0
| @@ -125,6 +125,12 @@ | ||
| 125 | 125 | exception hierarchy, it now has new child classes that are not |
| 126 | 126 | related to timeouts). |
| 127 | 127 | |
| 128 | +13. `ResultSet#fetchMoreResults()` now returns a `ListenableFuture<ResultSet>`. | |
| 129 | + This makes the API more friendly if you chain transformations on an async | |
| 130 | + query to process all pages (see `AsyncResultSetTest` in the sources for an | |
| 131 | + example). | |
| 132 | + | |
| 133 | + | |
| 128 | 134 | ### 2.1.7 |
| 129 | 135 | |
| 130 | 136 | This version brings a few changes in the driver's behavior; none of them break |