Search in sources :

Example 1 with ListAppProfilesPage

use of com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPage in project java-bigtable by googleapis.

the class BigtableInstanceAdminClient method listAppProfilesAsync.

/**
 * Asynchronously lists all app profiles of the specified instance.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * ApiFuture<List<AppProfile>> appProfilesFuture = client.listAppProfilesAsync("my-instance");
 *
 * List<AppProfile> appProfiles = appProfileFuture.get();
 * }</pre>
 *
 * @see AppProfile
 */
@SuppressWarnings("WeakerAccess")
public ApiFuture<List<AppProfile>> listAppProfilesAsync(String instanceId) {
    String instanceName = NameUtil.formatInstanceName(projectId, instanceId);
    ListAppProfilesRequest request = ListAppProfilesRequest.newBuilder().setParent(instanceName).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<ListAppProfilesPage> firstPageFuture = ApiFutures.transform(stub.listAppProfilesPagedCallable().futureCall(request), new ApiFunction<ListAppProfilesPagedResponse, ListAppProfilesPage>() {

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

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

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

        @Override
        public List<AppProfile> apply(List<com.google.bigtable.admin.v2.AppProfile> input) {
            List<AppProfile> results = Lists.newArrayListWithCapacity(input.size());
            for (com.google.bigtable.admin.v2.AppProfile appProfile : input) {
                results.add(AppProfile.fromProto(appProfile));
            }
            return results;
        }
    }, MoreExecutors.directExecutor());
}
Also used : AppProfile(com.google.cloud.bigtable.admin.v2.models.AppProfile) ListAppProfilesRequest(com.google.bigtable.admin.v2.ListAppProfilesRequest) ApiFuture(com.google.api.core.ApiFuture) ListAppProfilesPage(com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPage) ListAppProfilesPagedResponse(com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPagedResponse) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 2 with ListAppProfilesPage

use of com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPage in project java-bigtable by googleapis.

the class BigtableInstanceAdminClientTest method testListAppProfiles.

@Test
public void testListAppProfiles() {
    // Setup
    Mockito.when(mockStub.listAppProfilesPagedCallable()).thenReturn(mockListAppProfilesCallable);
    com.google.bigtable.admin.v2.ListAppProfilesRequest expectedRequest = com.google.bigtable.admin.v2.ListAppProfilesRequest.newBuilder().setParent(NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID)).build();
    // 3 AppProfiles spread across 2 pages
    List<com.google.bigtable.admin.v2.AppProfile> expectedProtos = Lists.newArrayList();
    for (int i = 0; i < 3; i++) {
        expectedProtos.add(com.google.bigtable.admin.v2.AppProfile.newBuilder().setName(APP_PROFILE_NAME + i).setDescription("profile" + i).setMultiClusterRoutingUseAny(com.google.bigtable.admin.v2.AppProfile.MultiClusterRoutingUseAny.getDefaultInstance()).build());
    }
    // 2 on the first page
    ListAppProfilesPage page0 = Mockito.mock(ListAppProfilesPage.class);
    Mockito.when(page0.getValues()).thenReturn(expectedProtos.subList(0, 2));
    Mockito.when(page0.hasNextPage()).thenReturn(true);
    // 1 on the last page
    ListAppProfilesPage page1 = Mockito.mock(ListAppProfilesPage.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
    ListAppProfilesPagedResponse response0 = Mockito.mock(ListAppProfilesPagedResponse.class);
    Mockito.when(response0.getPage()).thenReturn(page0);
    Mockito.when(mockListAppProfilesCallable.futureCall(expectedRequest)).thenReturn(ApiFutures.immediateFuture(response0));
    // Execute
    List<AppProfile> actualResults = adminClient.listAppProfiles(INSTANCE_ID);
    // Verify
    List<AppProfile> expectedResults = Lists.newArrayList();
    for (com.google.bigtable.admin.v2.AppProfile expectedProto : expectedProtos) {
        expectedResults.add(AppProfile.fromProto(expectedProto));
    }
    assertThat(actualResults).containsExactlyElementsIn(expectedResults);
}
Also used : AppProfile(com.google.cloud.bigtable.admin.v2.models.AppProfile) ListAppProfilesPage(com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPage) ListAppProfilesPagedResponse(com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPagedResponse) Test(org.junit.Test)

Aggregations

ListAppProfilesPage (com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPage)2 ListAppProfilesPagedResponse (com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPagedResponse)2 AppProfile (com.google.cloud.bigtable.admin.v2.models.AppProfile)2 ApiFuture (com.google.api.core.ApiFuture)1 ListAppProfilesRequest (com.google.bigtable.admin.v2.ListAppProfilesRequest)1 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 Test (org.junit.Test)1