Skip to content

Commit bed08ff

Browse files
Bigtable: cleanup of futures + extras (googleapis#3571)
1 parent fc3c81f commit bed08ff

File tree

14 files changed

+678
-215
lines changed

14 files changed

+678
-215
lines changed

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

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.google.cloud.bigtable.admin.v2;
1717

18+
import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse;
19+
import com.google.common.util.concurrent.MoreExecutors;
1820
import java.io.IOException;
1921
import java.util.ArrayList;
2022
import java.util.List;
@@ -144,9 +146,7 @@ public void close() {
144146
* @see CreateTableRequest for createTable configurations
145147
*/
146148
public Table createTable(CreateTableRequest request) {
147-
com.google.bigtable.admin.v2.Table table =
148-
this.stub.createTableCallable().call(request.toProto(instanceName));
149-
return Table.fromProto(table);
149+
return awaitFuture(createTableAsync(request));
150150
}
151151

152152
/**
@@ -200,9 +200,7 @@ public ApiFuture<Table> createTableAsync(CreateTableRequest request) {
200200
* @see ModifyColumnFamiliesRequest for modifyFamily options
201201
*/
202202
public Table modifyFamilies(ModifyColumnFamiliesRequest request) {
203-
com.google.bigtable.admin.v2.Table table =
204-
this.stub.modifyColumnFamiliesCallable().call(request.toProto(instanceName));
205-
return Table.fromProto(table);
203+
return awaitFuture(modifyFamiliesAsync(request));
206204
}
207205

208206
/**
@@ -252,7 +250,7 @@ public ApiFuture<Table> modifyFamiliesAsync(ModifyColumnFamiliesRequest request)
252250
* }</pre>
253251
*/
254252
public void deleteTable(String tableId) {
255-
this.stub.deleteTableCallable().call(composeDeleteTableRequest(tableId));
253+
awaitFuture(deleteTableAsync(tableId));
256254
}
257255

258256
/**
@@ -283,9 +281,7 @@ public ApiFuture<Void> deleteTableAsync(String tableId) {
283281
* }</pre>
284282
*/
285283
public Table getTable(String tableId) {
286-
com.google.bigtable.admin.v2.Table table =
287-
this.stub.getTableCallable().call(composeGetTableRequest(tableId));
288-
return Table.fromProto(table);
284+
return awaitFuture(getTableAsync(tableId));
289285
}
290286

291287
/**
@@ -316,8 +312,7 @@ public ApiFuture<Table> getTableAsync(String tableId) {
316312
* }</pre>
317313
*/
318314
public List<TableName> listTables() {
319-
ListTablesResponse listResp = this.stub.listTablesCallable().call(composeListTableRequest());
320-
return convertToTableNames(listResp);
315+
return awaitFuture(listTablesAsync());
321316
}
322317

323318
/**
@@ -332,17 +327,18 @@ public List<TableName> listTables() {
332327
* }</pre>
333328
*/
334329
public ApiFuture<List<TableName>> listTablesAsync() {
335-
ApiFuture<ListTablesResponse> listResp =
336-
this.stub.listTablesCallable().futureCall(composeListTableRequest());
330+
ApiFuture<ListTablesPagedResponse> listResp =
331+
this.stub.listTablesPagedCallable().futureCall(composeListTableRequest());
337332

338333
return ApiFutures.transform(
339334
listResp,
340-
new ApiFunction<ListTablesResponse, List<TableName>>() {
335+
new ApiFunction<ListTablesPagedResponse, List<TableName>>() {
341336
@Override
342-
public List<TableName> apply(ListTablesResponse input) {
343-
return convertToTableNames(input);
337+
public List<TableName> apply(ListTablesPagedResponse input) {
338+
return convertToTableNames(input.iterateAll());
344339
}
345-
});
340+
},
341+
MoreExecutors.directExecutor());
346342
}
347343

348344
/**
@@ -357,7 +353,7 @@ public List<TableName> apply(ListTablesResponse input) {
357353
* }</pre>
358354
*/
359355
public void dropRowRange(String tableId, String rowKeyPrefix) {
360-
dropRowRange(tableId, ByteString.copyFromUtf8(rowKeyPrefix));
356+
awaitFuture(dropRowRangeAsync(tableId, rowKeyPrefix));
361357
}
362358

363359
/**
@@ -387,7 +383,7 @@ public ApiFuture<Void> dropRowRangeAsync(String tableId, String rowKeyPrefix) {
387383
* }</pre>
388384
*/
389385
public void dropRowRange(String tableId, ByteString rowKeyPrefix) {
390-
this.stub.dropRowRangeCallable().call(composeDropRowRangeRequest(tableId, rowKeyPrefix, false));
386+
awaitFuture(dropRowRangeAsync(tableId, rowKeyPrefix));
391387
}
392388

393389
/**
@@ -420,7 +416,7 @@ public ApiFuture<Void> dropRowRangeAsync(String tableId, ByteString rowKeyPrefix
420416
* }</pre>
421417
*/
422418
public void dropAllRows(String tableId) {
423-
this.stub.dropRowRangeCallable().call(composeDropRowRangeRequest(tableId, null, true));
419+
awaitFuture(dropAllRowsAsync(tableId));
424420
}
425421

426422
/**
@@ -454,10 +450,7 @@ public ApiFuture<Void> dropAllRowsAsync(String tableId) {
454450
* }</pre>
455451
*/
456452
public ConsistencyToken generateConsistencyToken(String tableId) {
457-
return ConsistencyToken.fromProto(
458-
this.stub
459-
.generateConsistencyTokenCallable()
460-
.call(composeGenerateConsistencyTokenRequest(tableId)));
453+
return awaitFuture(generateConsistencyTokenAsync(tableId));
461454
}
462455

463456
/**
@@ -485,7 +478,8 @@ public ApiFuture<ConsistencyToken> generateConsistencyTokenAsync(String tableId)
485478
public ConsistencyToken apply(GenerateConsistencyTokenResponse input) {
486479
return ConsistencyToken.fromProto(input);
487480
}
488-
});
481+
},
482+
MoreExecutors.directExecutor());
489483
}
490484

491485
/**
@@ -500,9 +494,7 @@ public ConsistencyToken apply(GenerateConsistencyTokenResponse input) {
500494
* }</pre>
501495
*/
502496
public boolean isConsistent(String tableId, ConsistencyToken token) {
503-
return stub.checkConsistencyCallable()
504-
.call(token.toProto(getTableName(tableId)))
505-
.getConsistent();
497+
return awaitFuture(isConsistentAsync(tableId, token));
506498
}
507499

508500
/**
@@ -527,7 +519,8 @@ public ApiFuture<Boolean> isConsistentAsync(String tableId, ConsistencyToken tok
527519
public Boolean apply(CheckConsistencyResponse input) {
528520
return input.getConsistent();
529521
}
530-
});
522+
},
523+
MoreExecutors.directExecutor());
531524
}
532525

533526
/**
@@ -591,10 +584,10 @@ GenerateConsistencyTokenRequest composeGenerateConsistencyTokenRequest(String ta
591584
* Helper method to convert ListTablesResponse to List<TableName>
592585
*/
593586
@VisibleForTesting
594-
static List<TableName> convertToTableNames(ListTablesResponse listTablesResponse) {
587+
static List<TableName> convertToTableNames(Iterable<com.google.bigtable.admin.v2.Table> listTablesResponse) {
595588
List<TableName> tableNames = new ArrayList<>();
596589

597-
for (com.google.bigtable.admin.v2.Table table : listTablesResponse.getTablesList()) {
590+
for (com.google.bigtable.admin.v2.Table table : listTablesResponse) {
598591
tableNames.add(TableName.parse(table.getName()));
599592
}
600593
return tableNames;
@@ -604,8 +597,7 @@ static List<TableName> convertToTableNames(ListTablesResponse listTablesResponse
604597
/**
605598
* Helper method to transform ApiFuture<com.google.bigtable.admin.v2.Table> to ApiFuture<Table>
606599
*/
607-
@VisibleForTesting
608-
static ApiFuture<Table> transformToTableResponse(
600+
private static ApiFuture<Table> transformToTableResponse(
609601
ApiFuture<com.google.bigtable.admin.v2.Table> future) {
610602
return ApiFutures.transform(
611603
future,
@@ -614,21 +606,31 @@ static ApiFuture<Table> transformToTableResponse(
614606
public Table apply(com.google.bigtable.admin.v2.Table table) {
615607
return Table.fromProto(table);
616608
}
617-
});
609+
},
610+
MoreExecutors.directExecutor());
618611
}
619612

620613
/**
621614
* Helper method to transform ApiFuture<Empty> to ApiFuture<Void>
622615
*/
623-
@VisibleForTesting
624-
static ApiFuture<Void> transformToVoid(ApiFuture<Empty> future) {
616+
private static ApiFuture<Void> transformToVoid(ApiFuture<Empty> future) {
625617
return ApiFutures.transform(
626618
future,
627619
new ApiFunction<Empty, Void>() {
628620
@Override
629621
public Void apply(Empty empty) {
630622
return null;
631623
}
632-
});
624+
},
625+
MoreExecutors.directExecutor());
626+
}
627+
628+
private <T> T awaitFuture(ApiFuture<T> future) {
629+
try {
630+
return future.get();
631+
} catch(Throwable t) {
632+
// TODO(igorbernstein2): figure out a better wrapper exception.
633+
throw new RuntimeException(t);
634+
}
633635
}
634636
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState;
1919
import com.google.common.base.MoreObjects;
20+
import com.google.common.base.Objects;
2021

2122
// TODO(igorbernstein2): remove this class and promote Replication State to Table.
2223
/** Wrapper for {@link ClusterState} protocol buffer object */
@@ -47,6 +48,24 @@ public ReplicationState getReplicationState() {
4748
return replicationState;
4849
}
4950

51+
@Override
52+
public boolean equals(Object o) {
53+
if (this == o) {
54+
return true;
55+
}
56+
if (o == null || getClass() != o.getClass()) {
57+
return false;
58+
}
59+
ClusterState that = (ClusterState) o;
60+
return Objects.equal(id, that.id) &&
61+
replicationState == that.replicationState;
62+
}
63+
64+
@Override
65+
public int hashCode() {
66+
return Objects.hashCode(id, replicationState);
67+
}
68+
5069
@Override
5170
public String toString() {
5271
return MoreObjects.toStringHelper(this)

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.bigtable.admin.v2.GcRule.RuleCase;
2323
import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule;
2424
import com.google.common.base.MoreObjects;
25+
import com.google.common.base.Objects;
2526

2627
/** Wrapper for {@link ColumnFamily} protocol buffer object */
2728
public final class ColumnFamily {
@@ -63,6 +64,24 @@ public boolean hasGCRule() {
6364
return !RuleCase.RULE_NOT_SET.equals(rule.toProto().getRuleCase());
6465
}
6566

67+
@Override
68+
public boolean equals(Object o) {
69+
if (this == o) {
70+
return true;
71+
}
72+
if (o == null || getClass() != o.getClass()) {
73+
return false;
74+
}
75+
ColumnFamily that = (ColumnFamily) o;
76+
return Objects.equal(id, that.id) &&
77+
Objects.equal(rule, that.rule);
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
return Objects.hashCode(id, rule);
83+
}
84+
6685
@Override
6786
public String toString() {
6887
return MoreObjects.toStringHelper(this).add("id", id).add("GCRule", rule).toString();

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse;
2121
import com.google.common.annotations.VisibleForTesting;
2222
import com.google.common.base.MoreObjects;
23+
import com.google.common.base.Objects;
2324

2425
/**
2526
* Wrapper for {@link GenerateConsistencyTokenResponse#getConsistencyToken()}
@@ -53,6 +54,23 @@ String getToken() {
5354
return token;
5455
}
5556

57+
@Override
58+
public boolean equals(Object o) {
59+
if (this == o) {
60+
return true;
61+
}
62+
if (o == null || getClass() != o.getClass()) {
63+
return false;
64+
}
65+
ConsistencyToken that = (ConsistencyToken) o;
66+
return Objects.equal(token, that.token);
67+
}
68+
69+
@Override
70+
public int hashCode() {
71+
return Objects.hashCode(token);
72+
}
73+
5674
@Override
5775
public String toString() {
5876
return MoreObjects.toStringHelper(this).add("token", token).toString();

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.bigtable.admin.v2.InstanceName;
2121
import com.google.bigtable.admin.v2.Table;
2222
import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule;
23+
import com.google.common.base.Objects;
2324
import com.google.common.base.Preconditions;
2425
import com.google.protobuf.ByteString;
2526

@@ -90,6 +91,24 @@ public CreateTableRequest addSplit(ByteString key) {
9091
return this;
9192
}
9293

94+
@Override
95+
public boolean equals(Object o) {
96+
if (this == o) {
97+
return true;
98+
}
99+
if (o == null || getClass() != o.getClass()) {
100+
return false;
101+
}
102+
CreateTableRequest that = (CreateTableRequest) o;
103+
return Objects.equal(createTableRequest, that.createTableRequest) &&
104+
Objects.equal(tableRequest, that.tableRequest);
105+
}
106+
107+
@Override
108+
public int hashCode() {
109+
return Objects.hashCode(createTableRequest, tableRequest);
110+
}
111+
93112
@InternalApi
94113
public com.google.bigtable.admin.v2.CreateTableRequest toProto(InstanceName instanceName) {
95114
Preconditions.checkNotNull(instanceName);

0 commit comments

Comments
 (0)