|
15 | 15 | */
|
16 | 16 | package com.google.cloud.bigtable.admin.v2;
|
17 | 17 |
|
| 18 | +import com.google.api.core.ApiAsyncFunction; |
18 | 19 | import com.google.api.core.ApiFunction;
|
19 | 20 | import com.google.api.core.ApiFuture;
|
20 | 21 | import com.google.api.core.ApiFutures;
|
|
27 | 28 | import com.google.bigtable.admin.v2.InstanceName;
|
28 | 29 | import com.google.bigtable.admin.v2.ListTablesRequest;
|
29 | 30 | import com.google.bigtable.admin.v2.TableName;
|
| 31 | +import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage; |
30 | 32 | import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse;
|
31 | 33 | import com.google.cloud.bigtable.admin.v2.models.ConsistencyToken;
|
32 | 34 | import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
|
@@ -433,22 +435,65 @@ public ApiFuture<List<TableName>> listTablesAsync() {
|
433 | 435 | ListTablesRequest request = ListTablesRequest.newBuilder().setParent(instanceName.toString())
|
434 | 436 | .build();
|
435 | 437 |
|
436 |
| - ApiFuture<ListTablesPagedResponse> listResp = |
437 |
| - this.stub.listTablesPagedCallable().futureCall(request); |
| 438 | + // TODO(igorbernstein2): try to upstream pagination spooling or figure out a way to expose the |
| 439 | + // paginated responses while maintaining the wrapper facade. |
438 | 440 |
|
439 |
| - return ApiFutures.transform( |
440 |
| - listResp, |
441 |
| - new ApiFunction<ListTablesPagedResponse, List<TableName>>() { |
| 441 | + // Fetch the first page. |
| 442 | + ApiFuture<ListTablesPage> firstPageFuture = ApiFutures.transform( |
| 443 | + stub.listTablesPagedCallable().futureCall(request), |
| 444 | + new ApiFunction<ListTablesPagedResponse, ListTablesPage>() { |
442 | 445 | @Override
|
443 |
| - public List<TableName> apply(ListTablesPagedResponse response) { |
444 |
| - List<TableName> results = Lists.newArrayList(); |
445 |
| - for (com.google.bigtable.admin.v2.Table proto : response.iterateAll()) { |
| 446 | + public ListTablesPage apply(ListTablesPagedResponse response) { |
| 447 | + return response.getPage(); |
| 448 | + } |
| 449 | + }, |
| 450 | + MoreExecutors.directExecutor() |
| 451 | + ); |
| 452 | + |
| 453 | + // Fetch the rest of the pages by chaining the futures. |
| 454 | + ApiFuture<List<com.google.bigtable.admin.v2.Table>> allProtos = ApiFutures |
| 455 | + .transformAsync( |
| 456 | + firstPageFuture, |
| 457 | + new ApiAsyncFunction<ListTablesPage, List<com.google.bigtable.admin.v2.Table>>() { |
| 458 | + List<com.google.bigtable.admin.v2.Table> responseAccumulator = Lists |
| 459 | + .newArrayList(); |
| 460 | + |
| 461 | + @Override |
| 462 | + public ApiFuture<List<com.google.bigtable.admin.v2.Table>> apply( |
| 463 | + ListTablesPage page) { |
| 464 | + // Add all entries from the page |
| 465 | + responseAccumulator.addAll(Lists.newArrayList(page.getValues())); |
| 466 | + |
| 467 | + // If this is the last page, just return the accumulated responses. |
| 468 | + if (!page.hasNextPage()) { |
| 469 | + return ApiFutures.immediateFuture(responseAccumulator); |
| 470 | + } |
| 471 | + |
| 472 | + // Otherwise fetch the next page. |
| 473 | + return ApiFutures.transformAsync( |
| 474 | + page.getNextPageAsync(), |
| 475 | + this, |
| 476 | + MoreExecutors.directExecutor() |
| 477 | + ); |
| 478 | + } |
| 479 | + }, |
| 480 | + MoreExecutors.directExecutor() |
| 481 | + ); |
| 482 | + |
| 483 | + // Wrap all of the accumulated protos. |
| 484 | + return ApiFutures.transform(allProtos, |
| 485 | + new ApiFunction<List<com.google.bigtable.admin.v2.Table>, List<TableName>>() { |
| 486 | + @Override |
| 487 | + public List<TableName> apply(List<com.google.bigtable.admin.v2.Table> protos) { |
| 488 | + List<TableName> results = Lists.newArrayListWithCapacity(protos.size()); |
| 489 | + for (com.google.bigtable.admin.v2.Table proto : protos) { |
446 | 490 | results.add(TableName.parse(proto.getName()));
|
447 | 491 | }
|
448 | 492 | return results;
|
449 | 493 | }
|
450 | 494 | },
|
451 |
| - MoreExecutors.directExecutor()); |
| 495 | + MoreExecutors.directExecutor() |
| 496 | + ); |
452 | 497 | }
|
453 | 498 |
|
454 | 499 | /**
|
|
0 commit comments