Search in sources :

Example 1 with ListTablesPage

use of com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage in project java-bigtable by googleapis.

the class BigtableTableAdminClient method listTablesAsync.

/**
 * Asynchronously lists all table IDs in the instance.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * ApiFuture<List<String>> listFuture = client.listTables();
 *
 * ApiFutures.addCallback(
 *   listFuture,
 *   new ApiFutureCallback<List<String>>() {
 *     public void onSuccess(List<String> tableIds) {
 *       System.out.println("Got list of tables:");
 *       for (String tableId : tableIds) {
 *         System.out.println(tableId);
 *       }
 *     }
 *
 *     public void onFailure(Throwable t) {
 *       t.printStackTrace();
 *     }
 *   },
 *   MoreExecutors.directExecutor()
 * );
 * }</pre>
 */
public ApiFuture<List<String>> listTablesAsync() {
    ListTablesRequest request = ListTablesRequest.newBuilder().setParent(NameUtil.formatInstanceName(projectId, instanceId)).build();
    // TODO(igorbernstein2): try to upstream pagination spooling or figure out a way to expose the
    // paginated responses while maintaining the wrapper facade.
    // Fetches the first page.
    ApiFuture<ListTablesPage> firstPageFuture = ApiFutures.transform(stub.listTablesPagedCallable().futureCall(request), new ApiFunction<ListTablesPagedResponse, ListTablesPage>() {

        @Override
        public ListTablesPage apply(ListTablesPagedResponse response) {
            return response.getPage();
        }
    }, MoreExecutors.directExecutor());
    // Fetches the rest of the pages by chaining the futures.
    ApiFuture<List<com.google.bigtable.admin.v2.Table>> allProtos = ApiFutures.transformAsync(firstPageFuture, new ApiAsyncFunction<ListTablesPage, List<com.google.bigtable.admin.v2.Table>>() {

        List<com.google.bigtable.admin.v2.Table> responseAccumulator = Lists.newArrayList();

        @Override
        public ApiFuture<List<com.google.bigtable.admin.v2.Table>> apply(ListTablesPage page) {
            // Add all entries from the page
            responseAccumulator.addAll(Lists.newArrayList(page.getValues()));
            // If this is the last page, just return the accumulated responses.
            if (!page.hasNextPage()) {
                return ApiFutures.immediateFuture(responseAccumulator);
            }
            // Otherwise fetch the next page.
            return ApiFutures.transformAsync(page.getNextPageAsync(), this, MoreExecutors.directExecutor());
        }
    }, MoreExecutors.directExecutor());
    // Wraps all of the accumulated protos.
    return ApiFutures.transform(allProtos, new ApiFunction<List<com.google.bigtable.admin.v2.Table>, List<String>>() {

        @Override
        public List<String> apply(List<com.google.bigtable.admin.v2.Table> protos) {
            List<String> results = Lists.newArrayListWithCapacity(protos.size());
            for (com.google.bigtable.admin.v2.Table proto : protos) {
                results.add(NameUtil.extractTableIdFromTableName(proto.getName()));
            }
            return results;
        }
    }, MoreExecutors.directExecutor());
}
Also used : ListTablesPage(com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage) Table(com.google.cloud.bigtable.admin.v2.models.Table) ListTablesPagedResponse(com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse) ApiFuture(com.google.api.core.ApiFuture) ListTablesRequest(com.google.bigtable.admin.v2.ListTablesRequest) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 2 with ListTablesPage

use of com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage in project java-bigtable by googleapis.

the class BigtableTableAdminClientTest method testListTables.

@Test
public void testListTables() {
    // Setup
    Mockito.when(mockStub.listTablesPagedCallable()).thenReturn(mockListTableCallable);
    com.google.bigtable.admin.v2.ListTablesRequest expectedRequest = com.google.bigtable.admin.v2.ListTablesRequest.newBuilder().setParent(INSTANCE_NAME).build();
    // 3 Tables spread across 2 pages
    List<com.google.bigtable.admin.v2.Table> expectedProtos = Lists.newArrayList();
    for (int i = 0; i < 3; i++) {
        expectedProtos.add(com.google.bigtable.admin.v2.Table.newBuilder().setName(TABLE_NAME + i).build());
    }
    // 2 on the first page
    ListTablesPage page0 = Mockito.mock(ListTablesPage.class);
    Mockito.when(page0.getValues()).thenReturn(expectedProtos.subList(0, 2));
    Mockito.when(page0.hasNextPage()).thenReturn(true);
    // 1 on the last page
    ListTablesPage page1 = Mockito.mock(ListTablesPage.class);
    Mockito.when(page1.getValues()).thenReturn(expectedProtos.subList(2, 3));
    // Link page0 to page1
    Mockito.when(page0.getNextPageAsync()).thenReturn(ApiFutures.immediateFuture(page1));
    // Link page to the response
    ListTablesPagedResponse response0 = Mockito.mock(ListTablesPagedResponse.class);
    Mockito.when(response0.getPage()).thenReturn(page0);
    Mockito.when(mockListTableCallable.futureCall(expectedRequest)).thenReturn(ApiFutures.immediateFuture(response0));
    // Execute
    List<String> actualResults = adminClient.listTables();
    // Verify
    List<String> expectedResults = Lists.newArrayList();
    for (com.google.bigtable.admin.v2.Table expectedProto : expectedProtos) {
        expectedResults.add(TableName.parse(expectedProto.getName()).getTable());
    }
    assertThat(actualResults).containsExactlyElementsIn(expectedResults);
}
Also used : ListTablesPage(com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage) Table(com.google.cloud.bigtable.admin.v2.models.Table) ByteString(com.google.protobuf.ByteString) ListTablesRequest(com.google.bigtable.admin.v2.ListTablesRequest) ListTablesPagedResponse(com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse) Test(org.junit.Test)

Aggregations

ListTablesRequest (com.google.bigtable.admin.v2.ListTablesRequest)2 ListTablesPage (com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage)2 ListTablesPagedResponse (com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse)2 Table (com.google.cloud.bigtable.admin.v2.models.Table)2 ApiFuture (com.google.api.core.ApiFuture)1 ImmutableList (com.google.common.collect.ImmutableList)1 ByteString (com.google.protobuf.ByteString)1 List (java.util.List)1 Test (org.junit.Test)1