|
25 | 25 |
|
26 | 26 | import com.google.api.client.util.BackOff; |
27 | 27 | import com.google.api.client.util.ExponentialBackOff; |
| 28 | +import com.google.api.gax.paging.Page; |
28 | 29 | import com.google.api.pathtemplate.PathTemplate; |
29 | 30 | import com.google.cloud.BaseService; |
| 31 | +import com.google.cloud.PageImpl; |
| 32 | +import com.google.cloud.PageImpl.NextPageFetcher; |
30 | 33 | import com.google.cloud.Timestamp; |
31 | 34 | import com.google.cloud.spanner.AbstractReadContext.MultiUseReadOnlyTransaction; |
32 | 35 | import com.google.cloud.spanner.AbstractReadContext.SingleReadContext; |
33 | 36 | import com.google.cloud.spanner.AbstractReadContext.SingleUseReadOnlyTransaction; |
34 | 37 | import com.google.cloud.spanner.spi.v1.SpannerRpc; |
| 38 | +import com.google.cloud.spanner.spi.v1.SpannerRpc.Paginated; |
35 | 39 | import com.google.common.annotations.VisibleForTesting; |
36 | 40 | import com.google.common.base.Preconditions; |
37 | 41 | import com.google.common.base.Throwables; |
@@ -374,6 +378,36 @@ private static <T extends Message> T unpack(Any response, Class<T> clazz) |
374 | 378 | } |
375 | 379 | } |
376 | 380 |
|
| 381 | + abstract static class PageFetcher<S, T> implements NextPageFetcher<S> { |
| 382 | + private String nextPageToken; |
| 383 | + |
| 384 | + @Override |
| 385 | + public Page<S> getNextPage() { |
| 386 | + Paginated<T> nextPage = |
| 387 | + runWithRetries( |
| 388 | + new Callable<Paginated<T>>() { |
| 389 | + @Override |
| 390 | + public Paginated<T> call() { |
| 391 | + return getNextPage(nextPageToken); |
| 392 | + } |
| 393 | + }); |
| 394 | + this.nextPageToken = nextPage.getNextPageToken(); |
| 395 | + List<S> results = new ArrayList<>(); |
| 396 | + for (T proto : nextPage.getResults()) { |
| 397 | + results.add(fromProto(proto)); |
| 398 | + } |
| 399 | + return new PageImpl<S>(this, nextPageToken, results); |
| 400 | + } |
| 401 | + |
| 402 | + void setNextPageToken(String nextPageToken) { |
| 403 | + this.nextPageToken = nextPageToken; |
| 404 | + } |
| 405 | + |
| 406 | + abstract Paginated<T> getNextPage(@Nullable String nextPageToken); |
| 407 | + |
| 408 | + abstract S fromProto(T proto); |
| 409 | + } |
| 410 | + |
377 | 411 | class SessionImpl implements Session { |
378 | 412 | private final String name; |
379 | 413 | private SessionTransaction activeTransaction; |
|
0 commit comments