Skip to content

Commit

Permalink
Bigtable: [Breaking change] listTables() now returns relative table n…
Browse files Browse the repository at this point in the history
…ames (#3696)

* Bigtable: be consistent and don't expose absolute table names

* fix merge

* fix merge
  • Loading branch information
igorbernstein2 authored and chingor13 committed Nov 12, 2018
1 parent a7bd8a5 commit 5fa529e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,38 +447,37 @@ private ApiFuture<Table> getTableAsync(String tableId, com.google.bigtable.admin
}

/**
* Lists all TableNames in the instance.
* Lists all table ids in the instance.
*
* <p>Sample code:
*
* <pre>{@code
* List<TableName> tableNames = client.listTables();
* for(TableName name : tableNames) {
* List<String> tableIds = client.listTables();
* for(String tableId: tableIds) {
* System.out.println(name.getTable());
* }
* }</pre>
*/
// TODO(igorbernstein2): consider changing this method to use relative table ids.
@SuppressWarnings("WeakerAccess")
public List<TableName> listTables() {
public List<String> listTables() {
return ApiExceptions.callAndTranslateApiException(listTablesAsync());
}

/**
* Asynchronously lists all TableNames in the instance.
* Asynchronously lists all table ids in the instance.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<List<TableName>> listFuture = client.listTablesAsync();
* ApiFuture<List<String>> listFuture = client.listTables();
*
* ApiFutures.addCallback(
* listFuture,
* new ApiFutureCallback<List<TableName>>() {
* public void onSuccess(List<TableName> tableNames) {
* new ApiFutureCallback<List<String>>() {
* public void onSuccess(List<String> tableIds) {
* System.out.println("Got list of tables:");
* for (TableName name : tableNames) {
* System.out.println(name.getTable());
* for (String tableId : tableIds) {
* System.out.println(tableId);
* }
* }
*
Expand All @@ -490,9 +489,8 @@ public List<TableName> listTables() {
* );
* }</pre>
*/
// TODO(igorbernstein2): consider changing this method to use relative table ids.
@SuppressWarnings("WeakerAccess")
public ApiFuture<List<TableName>> listTablesAsync() {
public ApiFuture<List<String>> listTablesAsync() {
ListTablesRequest request = ListTablesRequest.newBuilder().setParent(instanceName.toString())
.build();

Expand Down Expand Up @@ -543,12 +541,12 @@ public ApiFuture<List<com.google.bigtable.admin.v2.Table>> apply(

// Wrap all of the accumulated protos.
return ApiFutures.transform(allProtos,
new ApiFunction<List<com.google.bigtable.admin.v2.Table>, List<TableName>>() {
new ApiFunction<List<com.google.bigtable.admin.v2.Table>, List<String>>() {
@Override
public List<TableName> apply(List<com.google.bigtable.admin.v2.Table> protos) {
List<TableName> results = Lists.newArrayListWithCapacity(protos.size());
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(TableName.parse(proto.getName()));
results.add(TableName.parse(proto.getName()).getTable());
}
return results;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ public void testListTables() {
);

// Execute
List<TableName> actualResults = adminClient.listTables();
List<String> actualResults = adminClient.listTables();

// Verify
List<TableName> expectedResults = Lists.newArrayList();
List<String> expectedResults = Lists.newArrayList();
for (com.google.bigtable.admin.v2.Table expectedProto : expectedProtos) {
expectedResults.add(TableName.parse(expectedProto.getName()));
expectedResults.add(TableName.parse(expectedProto.getName()).getTable());
}

assertThat(actualResults).containsExactlyElementsIn(expectedResults);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.junit.Assert.assertTrue;

import com.google.bigtable.admin.v2.InstanceName;
import com.google.bigtable.admin.v2.TableName;
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
import com.google.cloud.bigtable.admin.v2.models.ColumnFamily;
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
Expand Down Expand Up @@ -68,9 +67,9 @@ public static void createClient() throws IOException {

// Cleanup old tables, under normal circumstances this will do nothing
String stalePrefix = String.format("020%d", System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1));
for (TableName tableName : tableAdmin.listTables()) {
if (stalePrefix.compareTo(tableName.getTable()) > 0) {
tableAdmin.deleteTable(tableName.getTable());
for (String tableId: tableAdmin.listTables()) {
if (stalePrefix.compareTo(tableId) > 0) {
tableAdmin.deleteTable(tableId);
}
}
}
Expand Down Expand Up @@ -217,7 +216,7 @@ public void listTables() {

try {
tableAdmin.createTable(CreateTableRequest.of(tableId));
List<TableName> tables = tableAdmin.listTables();
List<String> tables = tableAdmin.listTables();
assertNotNull(tables);
assertFalse("List tables did not return any tables", tables.isEmpty());
} finally {
Expand All @@ -231,7 +230,7 @@ public void listTablesAsync() throws Exception {

try {
tableAdmin.createTable(CreateTableRequest.of(tableId));
List<TableName> tables = tableAdmin.listTablesAsync().get();
List<String> tables = tableAdmin.listTablesAsync().get();
assertNotNull(tables);
assertFalse("List tables did not return any tables", tables.isEmpty());
} finally {
Expand Down

0 comments on commit 5fa529e

Please sign in to comment.