Skip to content

Commit 0e1bc68

Browse files
elisheva-qlogicigorbernstein2
authored andcommitted
Exists method added (#3769)
1 parent 827230e commit 0e1bc68

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.api.core.ApiFuture;
2121
import com.google.api.core.ApiFutures;
2222
import com.google.api.gax.rpc.ApiExceptions;
23+
import com.google.api.gax.rpc.NotFoundException;
2324
import com.google.bigtable.admin.v2.DeleteTableRequest;
2425
import com.google.bigtable.admin.v2.DropRowRangeRequest;
2526
import com.google.bigtable.admin.v2.GetTableRequest;
@@ -323,6 +324,65 @@ public ApiFuture<Void> deleteTableAsync(String tableId) {
323324
return transformToVoid(this.stub.deleteTableCallable().futureCall(request));
324325
}
325326

327+
/**
328+
* Checks if the table specified by the tableId exists
329+
*
330+
* <p>Sample code:
331+
*
332+
* <pre>{@code
333+
* if(client.exists("my-table")) {
334+
* System.out.println("Table exists");
335+
* }
336+
* }</pre>
337+
*/
338+
public boolean exists(String tableId) {
339+
return ApiExceptions.callAndTranslateApiException(existsAsync(tableId));
340+
}
341+
342+
/**
343+
* Asynchronously checks if the table specified by the tableId exists
344+
*
345+
* <p>Sample code:
346+
*
347+
* <pre>{@code
348+
* ApiFuture<Boolean> found = client.existsAsync("my-table");
349+
*
350+
* ApiFutures.addCallback(
351+
* found,
352+
* new ApiFutureCallback<Boolean>() {
353+
* public void onSuccess(Boolean found) {
354+
* if (found) {
355+
* System.out.println("Table exists");
356+
* } else {
357+
* System.out.println("Table not found");
358+
* }
359+
* }
360+
*
361+
* public void onFailure(Throwable t) {
362+
* t.printStackTrace();
363+
* }
364+
* },
365+
* MoreExecutors.directExecutor()
366+
* );
367+
* }</pre>
368+
*/
369+
public ApiFuture<Boolean> existsAsync(String tableId) {
370+
371+
ApiFuture<Table> protoFuture = getTableAsync(tableId, com.google.bigtable.admin.v2.Table.View.NAME_ONLY);
372+
373+
ApiFuture<Boolean> existsFuture = ApiFutures.transform(protoFuture, new ApiFunction<Table, Boolean>() {
374+
@Override public Boolean apply(Table ignored) {
375+
return true;
376+
}
377+
}, MoreExecutors.directExecutor());
378+
379+
return ApiFutures.catching(existsFuture, NotFoundException.class, new ApiFunction<NotFoundException, Boolean>() {
380+
@Override public Boolean apply(NotFoundException ignored) {
381+
return false;
382+
}
383+
}, MoreExecutors.directExecutor());
384+
}
385+
326386
/**
327387
* Gets the table metadata by tableId.
328388
*
@@ -373,8 +433,13 @@ public Table getTable(String tableId) {
373433
*/
374434
@SuppressWarnings("WeakerAccess")
375435
public ApiFuture<Table> getTableAsync(String tableId) {
436+
return getTableAsync(tableId, com.google.bigtable.admin.v2.Table.View.SCHEMA_VIEW);
437+
}
438+
439+
private ApiFuture<Table> getTableAsync(String tableId, com.google.bigtable.admin.v2.Table.View view) {
376440
GetTableRequest request = GetTableRequest.newBuilder()
377441
.setName(getTableName(tableId))
442+
.setView(view)
378443
.build();
379444

380445
return transformToTableResponse(

google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import com.google.api.core.ApiFuture;
2121
import com.google.api.core.ApiFutures;
22+
import com.google.api.gax.grpc.GrpcStatusCode;
23+
import com.google.api.gax.rpc.NotFoundException;
2224
import com.google.api.gax.rpc.UnaryCallable;
2325
import com.google.bigtable.admin.v2.ColumnFamily;
2426
import com.google.bigtable.admin.v2.DeleteTableRequest;
@@ -28,6 +30,7 @@
2830
import com.google.bigtable.admin.v2.InstanceName;
2931
import com.google.bigtable.admin.v2.ListTablesRequest;
3032
import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification;
33+
import com.google.bigtable.admin.v2.Table.View;
3134
import com.google.bigtable.admin.v2.TableName;
3235
import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage;
3336
import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse;
@@ -40,9 +43,12 @@
4043
import com.google.protobuf.Empty;
4144
import java.util.List;
4245
import java.util.concurrent.atomic.AtomicBoolean;
46+
47+
import io.grpc.Status;
4348
import org.junit.Before;
4449
import org.junit.Test;
4550
import org.junit.runner.RunWith;
51+
import org.mockito.Matchers;
4652
import org.mockito.Mock;
4753
import org.mockito.Mockito;
4854
import org.mockito.invocation.InvocationOnMock;
@@ -184,6 +190,7 @@ public void testGetTable() {
184190
// Setup
185191
GetTableRequest expectedRequest = GetTableRequest.newBuilder()
186192
.setName(TABLE_NAME.toString())
193+
.setView(View.SCHEMA_VIEW)
187194
.build();
188195

189196
com.google.bigtable.admin.v2.Table expectedResponse = com.google.bigtable.admin.v2.Table
@@ -303,4 +310,38 @@ public ApiFuture<Void> answer(InvocationOnMock invocationOnMock) throws Throwabl
303310
// Verify
304311
assertThat(wasCalled.get()).isTrue();
305312
}
313+
314+
@Test
315+
public void testExistsTrue() {
316+
// Setup
317+
com.google.bigtable.admin.v2.Table expectedResponse =
318+
com.google.bigtable.admin.v2.Table.newBuilder()
319+
.setName(TABLE_NAME.toString())
320+
.build();
321+
322+
Mockito.when(mockGetTableCallable.futureCall(Matchers.any(GetTableRequest.class)))
323+
.thenReturn(ApiFutures.immediateFuture(expectedResponse));
324+
325+
// Execute
326+
boolean found = adminClient.exists(TABLE_NAME.getTable());
327+
328+
// Verify
329+
assertThat(found).isTrue();
330+
}
331+
332+
@Test
333+
public void testExistsFalse() {
334+
// Setup
335+
NotFoundException exception =
336+
new NotFoundException("fake error", null, GrpcStatusCode.of(Status.Code.NOT_FOUND), false);
337+
338+
Mockito.when(mockGetTableCallable.futureCall(Matchers.any(GetTableRequest.class)))
339+
.thenReturn(ApiFutures.<com.google.bigtable.admin.v2.Table>immediateFailedFuture(exception));
340+
341+
// Execute
342+
boolean found = adminClient.exists(TABLE_NAME.getTable());
343+
344+
// Verify
345+
assertThat(found).isFalse();
346+
}
306347
}

0 commit comments

Comments
 (0)