Search in sources :

Example 1 with ListBackupsPage

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

the class BigtableTableAdminClientTest method testListBackups.

@Test
public void testListBackups() {
    // Setup
    Mockito.when(mockStub.listBackupsPagedCallable()).thenReturn(mockListBackupCallable);
    com.google.bigtable.admin.v2.ListBackupsRequest testRequest = com.google.bigtable.admin.v2.ListBackupsRequest.newBuilder().setParent(NameUtil.formatClusterName(PROJECT_ID, INSTANCE_ID, CLUSTER_ID)).build();
    // 3 Backups spread across 2 pages
    List<com.google.bigtable.admin.v2.Backup> expectedProtos = Lists.newArrayList();
    for (int i = 0; i < 3; i++) {
        expectedProtos.add(com.google.bigtable.admin.v2.Backup.newBuilder().setName(NameUtil.formatBackupName(PROJECT_ID, INSTANCE_ID, CLUSTER_ID, BACKUP_ID + i)).build());
    }
    // 2 on the first page
    ListBackupsPage page0 = Mockito.mock(ListBackupsPage.class);
    Mockito.when(page0.getValues()).thenReturn(expectedProtos.subList(0, 2));
    Mockito.when(page0.hasNextPage()).thenReturn(true);
    // 1 on the last page
    ListBackupsPage page1 = Mockito.mock(ListBackupsPage.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
    ListBackupsPagedResponse response0 = Mockito.mock(ListBackupsPagedResponse.class);
    Mockito.when(response0.getPage()).thenReturn(page0);
    Mockito.when(mockListBackupCallable.futureCall(testRequest)).thenReturn(ApiFutures.immediateFuture(response0));
    // Execute
    List<String> actualResults = adminClient.listBackups(CLUSTER_ID);
    // Verify
    List<String> expectedResults = Lists.newArrayList();
    for (com.google.bigtable.admin.v2.Backup expectedProto : expectedProtos) {
        expectedResults.add(NameUtil.extractBackupIdFromBackupName(expectedProto.getName()));
    }
    assertThat(actualResults).containsExactlyElementsIn(expectedResults);
}
Also used : ListBackupsPage(com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListBackupsPage) Backup(com.google.cloud.bigtable.admin.v2.models.Backup) ByteString(com.google.protobuf.ByteString) ListBackupsRequest(com.google.bigtable.admin.v2.ListBackupsRequest) ListBackupsPagedResponse(com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListBackupsPagedResponse) Test(org.junit.Test)

Example 2 with ListBackupsPage

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

the class BigtableTableAdminClient method listBackupsAsync.

/**
 * Lists backups in the specified cluster asynchronously.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * ApiFuture<List<String>> listFuture = client.listBackupsAsync(clusterId);
 *
 * ApiFutures.addCallback(
 *   listFuture,
 *   new ApiFutureCallback<List<String>>() {
 *     public void onSuccess(List<String> backupIds) {
 *       System.out.println("Got list of backups:");
 *       for (String backupId : backupIds) {
 *         System.out.println(backupId);
 *       }
 *     }
 *
 *     public void onFailure(Throwable t) {
 *       t.printStackTrace();
 *     }
 *   },
 *   MoreExecutors.directExecutor()
 * );
 * }</pre>
 */
public ApiFuture<List<String>> listBackupsAsync(String clusterId) {
    ListBackupsRequest request = ListBackupsRequest.newBuilder().setParent(NameUtil.formatClusterName(projectId, instanceId, clusterId)).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<ListBackupsPage> firstPageFuture = ApiFutures.transform(stub.listBackupsPagedCallable().futureCall(request), new ApiFunction<ListBackupsPagedResponse, ListBackupsPage>() {

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

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

        @Override
        public ApiFuture<List<com.google.bigtable.admin.v2.Backup>> apply(ListBackupsPage 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.Backup>, List<String>>() {

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

Aggregations

ListBackupsRequest (com.google.bigtable.admin.v2.ListBackupsRequest)2 ListBackupsPage (com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListBackupsPage)2 ListBackupsPagedResponse (com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListBackupsPagedResponse)2 Backup (com.google.cloud.bigtable.admin.v2.models.Backup)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