Elffuss BraidHub

kikocis/java-driver public

Document async paging.
Changeset0c0587f9a355c508940089f5821e190049a9e7ff939487ac80ff1c31fc32c662
Authorolim7t <omichallat+github@gmail.com> · 2015-12-07 17:44 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-12-07 18:09 +01:00
Parents5c562f23d881

Browse the files at this changeset

2 files changed

modified features/async/README.md +60 −0
@@ -42,6 +42,66 @@
4242 });
4343 ```
4444
45+### Async paging
46+
47+If you consume a `ResultSet` in a callback, be aware that iterating the
48+rows will trigger synchronous queries as you page through the results.
49+To avoid this, use [getAvailableWithoutFetching] to limit the iteration
50+to the current page, and [fetchMoreResults] to get a future to the next
51+page (see also the section on [paging](../paging/)).
52+Here is a full example:
53+
54+[getAvailableWithoutFetching]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/ResultSet.html#getAvailableWithoutFetching--
55+[fetchMoreResults]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/ResultSet.html#fetchMoreResults--
56+
57+```java
58+Statement statement = new SimpleStatement("select * from foo").setFetchSize(20);
59+ListenableFuture<Void> future = Futures.transform(
60+ session.executeAsync(statement),
61+ iterateFirst());
62+
63+// Unfortunately we have to special-case for the first page because the signatures of the
64+// futures differ.
65+// In 3.0, fetchMoreResults() will return a Future<ResultSet> to avoid this.
66+private static AsyncFunction<ResultSet, Void> iterateFirst() {
67+ return new AsyncFunction<ResultSet, Void>() {
68+ @Override
69+ public ListenableFuture<Void> apply(ResultSet rs) throws Exception {
70+ return iterate(rs, 1).apply(null);
71+ }
72+ };
73+}
74+
75+private static AsyncFunction<Void, Void> iterate(final ResultSet rs, final int page) {
76+ return new AsyncFunction<Void, Void>() {
77+ @Override
78+ public ListenableFuture<Void> apply(Void v) throws Exception {
79+
80+ // How far we can go without triggering the blocking fetch:
81+ int remainingInPage = rs.getAvailableWithoutFetching();
82+
83+ System.out.printf("Starting page %d (%d rows)%n", page, remainingInPage);
84+
85+ for (Row row : rs) {
86+ System.out.printf("[page %d - %d] row = %s%n", page, remainingInPage, row);
87+ if (--remainingInPage == 0)
88+ break;
89+ }
90+ System.out.printf("Done page %d%n", page);
91+
92+ boolean wasLastPage = rs.getExecutionInfo().getPagingState() == null;
93+ if (wasLastPage) {
94+ System.out.println("Done iterating");
95+ return Futures.immediateFuture(null);
96+ } else {
97+ ListenableFuture<Void> future = rs.fetchMoreResults();
98+ return Futures.transform(future, iterate(rs, page + 1));
99+ }
100+ }
101+ };
102+}
103+```
104+
45105 ### Good practices
46106
47107 If your callback is slow, consider providing a separate executor.
modified features/paging/README.md +5 −0
@@ -93,6 +93,11 @@
9393 }
9494 ```
9595
96+If you use paging with the async API, you'll also want to use those
97+methods to avoid triggering synchronous fetches unintentionally; see
98+[async paging](../async/#async-paging).
99+
100+
96101 ### Saving and reusing the paging state
97102
98103 Sometimes it is convenient to save the paging state in order to restore