Elffuss BraidHub

kikocis/java-driver public

Merge pull request #411 from datastax/java836

Make ResultSet#fetchMoreResult return a ListenableFuture<ResultSet> (JAVA-836)
Changesetf1f8f4e3bbe95d416a112b33f1c1383eb3dab35455721510128841489535bd9d
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-07-24 22:00 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-07-24 22:00 +02:00
Parents382208d6a566, 7bef602fb122

Browse the files at this changeset

5 files changed

modified changelog/README.md +2 −0
@@ -6,6 +6,8 @@
66 - [bug] DateCodec does not format values correctly (JAVA-816)
77 - [bug] TimeCodec does not format values correctly (JAVA-817)
88 - [bug] TypeCodec.getDataTypeFor() does not handle LocalDate instances (JAVA-818)
9+- [improvement] Make ResultSet#fetchMoreResult return a
10+ ListenableFuture<ResultSet> (JAVA-836)
911
1012 Merged from 2.1 branch:
1113
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +11 −10
@@ -21,6 +21,7 @@
2121 import java.util.concurrent.ExecutionException;
2222 import java.util.concurrent.LinkedBlockingDeque;
2323
24+import com.google.common.util.concurrent.Futures;
2425 import com.google.common.util.concurrent.ListenableFuture;
2526 import com.google.common.util.concurrent.SettableFuture;
2627 import com.google.common.util.concurrent.Uninterruptibles;
@@ -187,8 +188,8 @@
187188 return true;
188189 }
189190
190- public ListenableFuture<Void> fetchMoreResults() {
191- return MoreFutures.VOID_SUCCESS;
191+ public ListenableFuture<ResultSet> fetchMoreResults() {
192+ return Futures.<ResultSet>immediateFuture(this);
192193 }
193194
194195 public ExecutionInfo getExecutionInfo() {
@@ -292,25 +293,25 @@
292293 }
293294 }
294295
295- public ListenableFuture<Void> fetchMoreResults() {
296+ public ListenableFuture<ResultSet> fetchMoreResults() {
296297 return fetchMoreResults(this.fetchState);
297298 }
298299
299- private ListenableFuture<Void> fetchMoreResults(FetchingState fetchState) {
300+ private ListenableFuture<ResultSet> fetchMoreResults(FetchingState fetchState) {
300301 if (fetchState == null)
301- return MoreFutures.VOID_SUCCESS;
302+ return Futures.<ResultSet>immediateFuture(this);
302303
303304 if (fetchState.inProgress != null)
304305 return fetchState.inProgress;
305306
306307 assert fetchState.nextStart != null;
307308 ByteBuffer state = fetchState.nextStart;
308- SettableFuture<Void> future = SettableFuture.create();
309+ SettableFuture<ResultSet> future = SettableFuture.create();
309310 this.fetchState = new FetchingState(null, future);
310311 return queryNextPage(state, future);
311312 }
312313
313- private ListenableFuture<Void> queryNextPage(ByteBuffer nextStart, final SettableFuture<Void> future) {
314+ private ListenableFuture<ResultSet> queryNextPage(ByteBuffer nextStart, final SettableFuture<ResultSet> future) {
314315
315316 assert !(statement instanceof BatchStatement);
316317
@@ -351,7 +352,7 @@
351352 }
352353
353354 MultiPage.this.infos.offer(info);
354- future.set(null);
355+ future.set(MultiPage.this);
355356 break;
356357 case ERROR:
357358 future.setException(((Responses.Error)response).asException(connection.address));
@@ -406,9 +407,9 @@
406407
407408 private static class FetchingState {
408409 public final ByteBuffer nextStart;
409- public final ListenableFuture<Void> inProgress;
410+ public final ListenableFuture<ResultSet> inProgress;
410411
411- FetchingState(ByteBuffer nextStart, ListenableFuture<Void> inProgress) {
412+ FetchingState(ByteBuffer nextStart, ListenableFuture<ResultSet> inProgress) {
412413 assert (nextStart == null) != (inProgress == null);
413414 this.nextStart = nextStart;
414415 this.inProgress = inProgress;
modified driver-core/src/main/java/com/datastax/driver/core/ResultSet.java +1 −1
@@ -156,7 +156,7 @@
156156 * thrown (you should thus call {@code isFullyFetched() to know if calling this
157157 * method can be of any use}).
158158 */
159- public ListenableFuture<Void> fetchMoreResults();
159+ public ListenableFuture<ResultSet> fetchMoreResults();
160160
161161 /**
162162 * 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 @@
125125 exception hierarchy, it now has new child classes that are not
126126 related to timeouts).
127127
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+
128134 ### 2.1.7
129135
130136 This version brings a few changes in the driver's behavior; none of them break