Skip to content

Commit

Permalink
Exists method added (#3769)
Browse files Browse the repository at this point in the history
  • Loading branch information
elisheva-qlogic authored and igorbernstein2 committed Oct 23, 2018
1 parent 827230e commit 0e1bc68
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.gax.rpc.ApiExceptions;
import com.google.api.gax.rpc.NotFoundException;
import com.google.bigtable.admin.v2.DeleteTableRequest;
import com.google.bigtable.admin.v2.DropRowRangeRequest;
import com.google.bigtable.admin.v2.GetTableRequest;
Expand Down Expand Up @@ -323,6 +324,65 @@ public ApiFuture<Void> deleteTableAsync(String tableId) {
return transformToVoid(this.stub.deleteTableCallable().futureCall(request));
}

/**
* Checks if the table specified by the tableId exists
*
* <p>Sample code:
*
* <pre>{@code
* if(client.exists("my-table")) {
* System.out.println("Table exists");
* }
* }</pre>
*/
public boolean exists(String tableId) {
return ApiExceptions.callAndTranslateApiException(existsAsync(tableId));
}

/**
* Asynchronously checks if the table specified by the tableId exists
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Boolean> found = client.existsAsync("my-table");
*
* ApiFutures.addCallback(
* found,
* new ApiFutureCallback<Boolean>() {
* public void onSuccess(Boolean found) {
* if (found) {
* System.out.println("Table exists");
* } else {
* System.out.println("Table not found");
* }
* }
*
* public void onFailure(Throwable t) {
* t.printStackTrace();
* }
* },
* MoreExecutors.directExecutor()
* );
* }</pre>
*/
public ApiFuture<Boolean> existsAsync(String tableId) {

ApiFuture<Table> protoFuture = getTableAsync(tableId, com.google.bigtable.admin.v2.Table.View.NAME_ONLY);

ApiFuture<Boolean> existsFuture = ApiFutures.transform(protoFuture, new ApiFunction<Table, Boolean>() {
@Override public Boolean apply(Table ignored) {
return true;
}
}, MoreExecutors.directExecutor());

return ApiFutures.catching(existsFuture, NotFoundException.class, new ApiFunction<NotFoundException, Boolean>() {
@Override public Boolean apply(NotFoundException ignored) {
return false;
}
}, MoreExecutors.directExecutor());
}

/**
* Gets the table metadata by tableId.
*
Expand Down Expand Up @@ -373,8 +433,13 @@ public Table getTable(String tableId) {
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<Table> getTableAsync(String tableId) {
return getTableAsync(tableId, com.google.bigtable.admin.v2.Table.View.SCHEMA_VIEW);
}

private ApiFuture<Table> getTableAsync(String tableId, com.google.bigtable.admin.v2.Table.View view) {
GetTableRequest request = GetTableRequest.newBuilder()
.setName(getTableName(tableId))
.setView(view)
.build();

return transformToTableResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.gax.grpc.GrpcStatusCode;
import com.google.api.gax.rpc.NotFoundException;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.bigtable.admin.v2.ColumnFamily;
import com.google.bigtable.admin.v2.DeleteTableRequest;
Expand All @@ -28,6 +30,7 @@
import com.google.bigtable.admin.v2.InstanceName;
import com.google.bigtable.admin.v2.ListTablesRequest;
import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification;
import com.google.bigtable.admin.v2.Table.View;
import com.google.bigtable.admin.v2.TableName;
import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage;
import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse;
Expand All @@ -40,9 +43,12 @@
import com.google.protobuf.Empty;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import io.grpc.Status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
Expand Down Expand Up @@ -184,6 +190,7 @@ public void testGetTable() {
// Setup
GetTableRequest expectedRequest = GetTableRequest.newBuilder()
.setName(TABLE_NAME.toString())
.setView(View.SCHEMA_VIEW)
.build();

com.google.bigtable.admin.v2.Table expectedResponse = com.google.bigtable.admin.v2.Table
Expand Down Expand Up @@ -303,4 +310,38 @@ public ApiFuture<Void> answer(InvocationOnMock invocationOnMock) throws Throwabl
// Verify
assertThat(wasCalled.get()).isTrue();
}

@Test
public void testExistsTrue() {
// Setup
com.google.bigtable.admin.v2.Table expectedResponse =
com.google.bigtable.admin.v2.Table.newBuilder()
.setName(TABLE_NAME.toString())
.build();

Mockito.when(mockGetTableCallable.futureCall(Matchers.any(GetTableRequest.class)))
.thenReturn(ApiFutures.immediateFuture(expectedResponse));

// Execute
boolean found = adminClient.exists(TABLE_NAME.getTable());

// Verify
assertThat(found).isTrue();
}

@Test
public void testExistsFalse() {
// Setup
NotFoundException exception =
new NotFoundException("fake error", null, GrpcStatusCode.of(Status.Code.NOT_FOUND), false);

Mockito.when(mockGetTableCallable.futureCall(Matchers.any(GetTableRequest.class)))
.thenReturn(ApiFutures.<com.google.bigtable.admin.v2.Table>immediateFailedFuture(exception));

// Execute
boolean found = adminClient.exists(TABLE_NAME.getTable());

// Verify
assertThat(found).isFalse();
}
}

0 comments on commit 0e1bc68

Please sign in to comment.