From 8879b6675ddfed68d1310286995c23b6b3775252 Mon Sep 17 00:00:00 2001 From: spollapally Date: Mon, 4 Jun 2018 14:33:18 -0400 Subject: [PATCH 01/10] TableAdmin with Unit tests --- .../bigtable/admin/v2/TableAdminClient.java | 237 ++++++++++++++++ .../bigtable/admin/v2/models/GCRules.java | 126 +++++++++ .../admin/v2/models/TableAdminRequests.java | 137 ++++++++++ .../admin/v2/TableAdminClientTest.java | 257 ++++++++++++++++++ .../admin/v2/it/TableAdminClientIT.java | 167 ++++++++++++ .../bigtable/admin/v2/models/GCRulesTest.java | 226 +++++++++++++++ .../v2/models/TableAdminRequestsTest.java | 81 ++++++ 7 files changed, 1231 insertions(+) create mode 100644 google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java create mode 100644 google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java create mode 100644 google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java create mode 100644 google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java create mode 100644 google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java create mode 100644 google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java create mode 100644 google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java new file mode 100644 index 000000000000..ab87cb0c4e79 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java @@ -0,0 +1,237 @@ +package com.google.cloud.bigtable.admin.v2; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import com.google.api.core.ApiFunction; +import com.google.api.core.ApiFuture; +import com.google.api.core.ApiFutures; +import com.google.bigtable.admin.v2.CheckConsistencyRequest; +import com.google.bigtable.admin.v2.CheckConsistencyResponse; +import com.google.bigtable.admin.v2.DeleteTableRequest; +import com.google.bigtable.admin.v2.DropRowRangeRequest; +import com.google.bigtable.admin.v2.DropRowRangeRequest.Builder; +import com.google.bigtable.admin.v2.GenerateConsistencyTokenRequest; +import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; +import com.google.bigtable.admin.v2.GetTableRequest; +import com.google.bigtable.admin.v2.InstanceName; +import com.google.bigtable.admin.v2.ListTablesRequest; +import com.google.bigtable.admin.v2.ListTablesResponse; +import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; +import com.google.bigtable.admin.v2.Table; +import com.google.bigtable.admin.v2.TableName; +import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.CreateTable; +import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.ModifyFamilies; +import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStub; +import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStubSettings; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import com.google.protobuf.ByteString; +import com.google.protobuf.Empty; + +public class TableAdminClient implements AutoCloseable { + private final BigtableTableAdminStub stub; + private final InstanceName instanceName; + + public static TableAdminClient create(InstanceName instanceName) throws IOException { + return new TableAdminClient(instanceName, BigtableTableAdminSettings.newBuilder().build()); + } + + public static TableAdminClient create(InstanceName instanceName, + BigtableTableAdminSettings adminSettings) throws IOException { + return new TableAdminClient(instanceName, adminSettings); + } + + public static TableAdminClient create(InstanceName instanceName, BigtableTableAdminStub stub) + throws IOException { + return new TableAdminClient(instanceName, stub); + } + + public TableAdminClient(InstanceName instanceName, BigtableTableAdminSettings adminSettings) + throws IOException { + this(instanceName, + ((BigtableTableAdminStubSettings) adminSettings.getStubSettings()).createStub()); + } + + public TableAdminClient(InstanceName instanceName, BigtableTableAdminStub stub) + throws IOException { + Preconditions.checkNotNull(instanceName); + Preconditions.checkNotNull(stub); + this.instanceName = instanceName; + this.stub = stub; + } + + @SuppressWarnings("unused") + private BigtableTableAdminStub getTableAdminStub() { + return stub; + } + + public InstanceName getInstanceName() { + return instanceName; + } + + @Override + public void close() throws Exception { + stub.close(); + } + + public Table createTable(CreateTable createTable) { + return this.stub.createTableCallable().call(createTable.toProto(instanceName.toString())); + } + + public ApiFuture createTableAsync(CreateTable createTable) { + return this.stub.createTableCallable().futureCall(createTable.toProto(instanceName.toString())); + } + + public Table modifyFamilies(ModifyFamilies modifyFamily) { + ModifyColumnFamiliesRequest modReq = + modifyFamily.toProto(getUniqueTableName(modifyFamily.getTableId())); + return this.stub.modifyColumnFamiliesCallable().call(modReq); + } + + public ApiFuture
modifyFamiliesAsync(ModifyFamilies modifyFamily) { + ModifyColumnFamiliesRequest modReq = + modifyFamily.toProto(getUniqueTableName(modifyFamily.getTableId())); + return this.stub.modifyColumnFamiliesCallable().futureCall(modReq); + } + + public void deleteTable(String tableId) { + this.stub.deleteTableCallable().call(getDeleteTableRequest(tableId)); + } + + public ApiFuture deleteTableAsync(String tableId) { + return this.stub.deleteTableCallable().futureCall(getDeleteTableRequest(tableId)); + } + + public Table getTable(String tableId) { + return this.stub.getTableCallable().call(getGetTableRequest(tableId)); + } + + public ApiFuture
getTableAsync(String tableId) { + return this.stub.getTableCallable().futureCall(getGetTableRequest(tableId)); + } + + public List listTables() { + ListTablesResponse listResp = this.stub.listTablesCallable().call(getListTableRequest()); + return convert(listResp); + } + + public ApiFuture> listTablesAsync() { + ApiFuture listResp = + this.stub.listTablesCallable().futureCall(getListTableRequest()); + + return ApiFutures.transform(listResp, new ApiFunction>() { + @Override + public List apply(ListTablesResponse input) { + return convert(input); + } + }); + } + + /** + * Drops rows by prefix. If prefix is "*" drops all data + * + * @param tableId + * @param rowKeyPrefix + */ + public void dropRowRange(String tableId, String rowKeyPrefix) { + this.stub.dropRowRangeCallable().call(getDropRowRangeRequest(tableId, rowKeyPrefix)); + } + + public ApiFuture dropRowRangeAsync(String tableId, String rowKeyPrefix) { + return this.stub.dropRowRangeCallable() + .futureCall(getDropRowRangeRequest(tableId, rowKeyPrefix)); + } + + public String GenerateConsistencyToken(String tableId) { + return this.stub.generateConsistencyTokenCallable().call(getGenConsistencyToken(tableId)) + .getConsistencyToken(); + } + + public ApiFuture generateConsistencyTokenAsync(String tableId) { + ApiFuture tokenResp = + this.stub.generateConsistencyTokenCallable().futureCall(getGenConsistencyToken(tableId)); + + return ApiFutures.transform(tokenResp, + new ApiFunction() { + @Override + public String apply(GenerateConsistencyTokenResponse input) { + return input.getConsistencyToken(); + } + }); + } + + public boolean isConsistent(String tableId, String token) { + return stub.checkConsistencyCallable().call(getCheckConsistencyRequest(tableId, token)) + .getConsistent(); + } + + public ApiFuture isConsistentAsync(String tableId, String token) { + ApiFuture CheckConsResp = + stub.checkConsistencyCallable().futureCall(getCheckConsistencyRequest(tableId, token)); + + return ApiFutures.transform(CheckConsResp, + new ApiFunction() { + @Override + public Boolean apply(CheckConsistencyResponse input) { + return input.getConsistent(); + } + }); + } + + @VisibleForTesting + String getUniqueTableName(String tableId) { + return TableName.of(instanceName.getProject(), instanceName.getInstance(), tableId).toString(); + } + + @VisibleForTesting + ListTablesRequest getListTableRequest() { + return ListTablesRequest.newBuilder().setParent(instanceName.toString()).build(); + } + + @VisibleForTesting + GetTableRequest getGetTableRequest(String tableId) { + return GetTableRequest.newBuilder().setName(getUniqueTableName(tableId)).build(); + } + + @VisibleForTesting + DeleteTableRequest getDeleteTableRequest(String tableId) { + return DeleteTableRequest.newBuilder().setName(getUniqueTableName(tableId)).build(); + } + + @VisibleForTesting + DropRowRangeRequest getDropRowRangeRequest(String tableId, String rowKeyPrefix) { + Builder dropRowReq = DropRowRangeRequest.newBuilder().setName(getUniqueTableName(tableId)); + + // TODO: verify if Utf8String and ByteString are equivalent + // and if using "*" prefix as special case to invoke deletAll approach is reasonable + if ("*".equals(rowKeyPrefix)) { + dropRowReq.setDeleteAllDataFromTable(true); + } else { + dropRowReq.setRowKeyPrefix(ByteString.copyFromUtf8(rowKeyPrefix)); + } + return dropRowReq.build(); + } + + @VisibleForTesting + GenerateConsistencyTokenRequest getGenConsistencyToken(String tableId) { + return GenerateConsistencyTokenRequest.newBuilder().setName(getUniqueTableName(tableId)) + .build(); + } + + @VisibleForTesting + CheckConsistencyRequest getCheckConsistencyRequest(String tableId, String token) { + return CheckConsistencyRequest.newBuilder().setName(getUniqueTableName(tableId)) + .setConsistencyToken(token).build(); + } + + @VisibleForTesting + static List convert(ListTablesResponse listTablesResponse) { + List tableNames = new ArrayList<>(); + + for (Table table : listTablesResponse.getTablesList()) { + tableNames.add(TableName.parse(table.getName())); + } + return tableNames; + } +} diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java new file mode 100644 index 000000000000..7cfc963e811d --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java @@ -0,0 +1,126 @@ +package com.google.cloud.bigtable.admin.v2.models; + +import javax.annotation.Nonnull; +import com.google.bigtable.admin.v2.GcRule; +import com.google.common.base.Preconditions; +import com.google.protobuf.Duration; + +public final class GCRules { + public static final GCRules GCRULES = new GCRules(); + + private GCRules() {} + + public IntersectionRule intersection() { + return new IntersectionRule(); + } + + public UnionRule union() { + return new UnionRule(); + } + + public VersionRule versions() { + return new VersionRule(); + } + + public DurationRule age() { + return new DurationRule(); + } + + public static final class IntersectionRule implements GCRule { + private GcRule.Intersection.Builder builder; + + private IntersectionRule() { + this.builder = GcRule.Intersection.newBuilder(); + } + + public IntersectionRule rule(@Nonnull GCRule rule) { + builder.addRules(rule.toProto()); + return this; + } + + @Override + public GcRule toProto() { + switch (builder.getRulesCount()) { + case 0: + return GcRule.newBuilder().build(); + case 1: + return builder.getRules(0); + default: + return GcRule.newBuilder().setIntersection(builder.build()).build(); + } + } + } + + public static final class UnionRule implements GCRule { + private GcRule.Union.Builder builder; + + private UnionRule() { + this.builder = GcRule.Union.newBuilder(); + } + + public UnionRule rule(@Nonnull GCRule rule) { + builder.addRules(rule.toProto()); + return this; + } + + @Override + public GcRule toProto() { + switch (builder.getRulesCount()) { + case 0: + return GcRule.newBuilder().build(); + case 1: + return builder.getRules(0); + default: + return GcRule.newBuilder().setUnion(builder.build()).build(); + } + } + } + + public static final class VersionRule implements GCRule { + private GcRule.Builder builder ; + + private VersionRule() { + this.builder = GcRule.newBuilder(); + } + + public VersionRule maxNum(int value) { + Preconditions.checkNotNull(value); + builder.setMaxNumVersions(value); + return this; + } + + @Override + public GcRule toProto() { + return builder.build(); + } + } + + public static final class DurationRule implements GCRule { + private Duration.Builder builder; + + private DurationRule() { + this.builder = Duration.newBuilder(); + } + + public DurationRule seconds(long value) { + Preconditions.checkNotNull(value); + builder.setSeconds(value); + return this; + } + + public DurationRule nanos(int value) { + Preconditions.checkNotNull(value); + builder.setNanos(value); + return this; + } + + @Override + public GcRule toProto() { + return GcRule.newBuilder().setMaxAge(builder.build()).build(); + } + } + + interface GCRule extends Cloneable { + GcRule toProto(); + } +} diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java new file mode 100644 index 000000000000..b33eaa4f08c1 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java @@ -0,0 +1,137 @@ +package com.google.cloud.bigtable.admin.v2.models; + +import com.google.bigtable.admin.v2.ColumnFamily; +import com.google.bigtable.admin.v2.CreateTableRequest; +import com.google.bigtable.admin.v2.Table; +import com.google.bigtable.admin.v2.Table.TimestampGranularity; +import com.google.bigtable.admin.v2.CreateTableRequest.Split; +import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; +import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; +import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; +import com.google.common.base.Preconditions; +import com.google.protobuf.ByteString; + +/** + * Idiomatic models to build Bigtable admin requests + * + */ +public final class TableAdminRequests { + private TableAdminRequests() {} + + public static CreateTable createTable(String tableId) { + return new CreateTable(tableId); + } + + public static ModifyFamilies modifyFamilies(String tableId) { + return new ModifyFamilies(tableId); + } + + public static final class CreateTable { + private final CreateTableRequest.Builder createTableRequest = CreateTableRequest.newBuilder(); + private final Table.Builder tableRequest = Table.newBuilder(); + + private CreateTable(String tableId) { + createTableRequest.setTableId(tableId); + } + + public CreateTable withGranularity(TimestampGranularity granularity) { + Preconditions.checkNotNull(granularity); + tableRequest.setGranularity(granularity); + return this; + } + + public CreateTable addColumnFamily(String familyId) { + Preconditions.checkNotNull(familyId); + tableRequest.putColumnFamilies(familyId, ColumnFamily.newBuilder().build()); + return this; + } + + public CreateTable addColumnFamily(String familyId, GCRule gcRule) { + Preconditions.checkNotNull(familyId); + tableRequest.putColumnFamilies(familyId, + ColumnFamily.newBuilder().setGcRule(gcRule.toProto()).build()); + return this; + } + + public CreateTable addSplit(ByteString key) { + Preconditions.checkNotNull(key); + createTableRequest.addInitialSplits(Split.newBuilder().setKey(key).build()); + return this; + } + + public CreateTable adduniformSplits(ByteString key) { + Preconditions.checkNotNull(key); + // TODO: add implementation + throw new UnsupportedOperationException(); + } + + public CreateTableRequest toProto(String parent) { + Preconditions.checkNotNull(parent); + return createTableRequest.setParent(parent).setTable(tableRequest.build()).build(); + } + } + + public static class ModifyFamilies { + final ModifyColumnFamiliesRequest.Builder modFamilyRequest = + ModifyColumnFamiliesRequest.newBuilder(); + String tableId; + + private ModifyFamilies(String tableId) { + Preconditions.checkNotNull(tableId); + this.tableId = tableId; + } + + public String getTableId() { + return this.tableId; + } + + private Modification.Builder verifyAndCreate(String familyId) { + Preconditions.checkNotNull(familyId); + Modification.Builder modification = Modification.newBuilder(); + modification.setId(familyId); + return modification; + } + + public ModifyFamilies create(String familyId) { + Modification.Builder modification = verifyAndCreate(familyId); + modification.setCreate(ColumnFamily.newBuilder()); + modFamilyRequest.addModifications(modification.build()); + return this; + } + + public ModifyFamilies createWithGCRule(String familyId, GCRule gcRule) { + Modification.Builder modification = verifyAndCreate(familyId); + Preconditions.checkNotNull(gcRule); + modification.setCreate(ColumnFamily.newBuilder().setGcRule(gcRule.toProto())); + modFamilyRequest.addModifications(modification.build()); + return this; + } + + public ModifyFamilies update(String familyId) { + Modification.Builder modification = verifyAndCreate(familyId); + modification.setUpdate(ColumnFamily.newBuilder()); + modFamilyRequest.addModifications(modification.build()); + return this; + } + + public ModifyFamilies updateWithGCRule(String familyId, GCRule gcRule) { + Modification.Builder modification = verifyAndCreate(familyId); + Preconditions.checkNotNull(gcRule); + modification.setUpdate(ColumnFamily.newBuilder().setGcRule(gcRule.toProto())); + modFamilyRequest.addModifications(modification.build()); + return this; + } + + public ModifyFamilies drop(String familyId) { + Modification.Builder modification = verifyAndCreate(familyId); + modification.setId(familyId).setDrop(true); + modFamilyRequest.addModifications(modification.build()); + return this; + } + + public ModifyColumnFamiliesRequest toProto(String uniqueTableName) { + Preconditions.checkNotNull(uniqueTableName); + return modFamilyRequest.setName(uniqueTableName).build(); + } + } +} diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java new file mode 100644 index 000000000000..7c9f498038f6 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java @@ -0,0 +1,257 @@ +package com.google.cloud.bigtable.admin.v2; + +import org.junit.Before; +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.*; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import com.google.api.core.ApiFuture; +import com.google.api.core.ApiFutures; +import com.google.api.gax.rpc.UnaryCallable; +import com.google.bigtable.admin.v2.CheckConsistencyRequest; +import com.google.bigtable.admin.v2.CheckConsistencyResponse; +import com.google.bigtable.admin.v2.CreateTableRequest; +import com.google.bigtable.admin.v2.DeleteTableRequest; +import com.google.bigtable.admin.v2.DropRowRangeRequest; +import com.google.bigtable.admin.v2.GenerateConsistencyTokenRequest; +import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; +import com.google.bigtable.admin.v2.GetTableRequest; +import com.google.bigtable.admin.v2.InstanceName; +import com.google.bigtable.admin.v2.ListTablesRequest; +import com.google.bigtable.admin.v2.ListTablesResponse; +import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; +import com.google.bigtable.admin.v2.Table; +import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests; +import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.CreateTable; +import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.ModifyFamilies; +import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStub; +import com.google.protobuf.ByteString; +import com.google.protobuf.Empty; + +@RunWith(MockitoJUnitRunner.class) +public class TableAdminClientTest { + private TableAdminClient adminClient; + @Mock + private BigtableTableAdminStub mockStub; + + @Mock + private UnaryCallable mockCreateTableCallable; + @Mock + private UnaryCallable mockModifyTableCallable; + @Mock + private UnaryCallable mockDeleteTableCallable; + @Mock + private UnaryCallable mockGetTableCallable; + @Mock + private UnaryCallable mockListTableCallable; + @Mock + private UnaryCallable mockDropRowRangeCallable; + @Mock + private UnaryCallable mockGenerateConsistencyTokenCallable; + @Mock + private UnaryCallable mockCheckConsistencyCallable; + + @Before + public void setUp() throws Exception { + adminClient = new TableAdminClient(InstanceName.of("[PROJECT]", "[INSTANCE]"), mockStub); + + Mockito.when(mockStub.createTableCallable()).thenReturn(mockCreateTableCallable); + Mockito.when(mockStub.modifyColumnFamiliesCallable()).thenReturn(mockModifyTableCallable); + Mockito.when(mockStub.deleteTableCallable()).thenReturn(mockDeleteTableCallable); + Mockito.when(mockStub.getTableCallable()).thenReturn(mockGetTableCallable); + Mockito.when(mockStub.listTablesCallable()).thenReturn(mockListTableCallable); + Mockito.when(mockStub.dropRowRangeCallable()).thenReturn(mockDropRowRangeCallable); + Mockito.when(mockStub.generateConsistencyTokenCallable()) + .thenReturn(mockGenerateConsistencyTokenCallable); + Mockito.when(mockStub.checkConsistencyCallable()).thenReturn(mockCheckConsistencyCallable); + } + + @Test + public void close() throws Exception { + adminClient.close(); + Mockito.verify(mockStub).close(); + } + + @Test + public void createTable() { + CreateTable createTableReq = TableAdminRequests.createTable("tableId"); + adminClient.createTable(createTableReq); + Mockito.verify(mockCreateTableCallable) + .call(createTableReq.toProto(adminClient.getInstanceName().toString())); + } + + @Test + public void createTableAsync() { + CreateTable createTableReq = TableAdminRequests.createTable("tableId"); + adminClient.createTableAsync(createTableReq); + Mockito.verify(mockCreateTableCallable) + .futureCall(createTableReq.toProto(adminClient.getInstanceName().toString())); + } + + @Test + public void modifyFamilies() { + ModifyFamilies modifyFamReq = TableAdminRequests.modifyFamilies("tableId"); + adminClient.modifyFamilies(modifyFamReq); + Mockito.verify(mockModifyTableCallable) + .call(modifyFamReq.toProto(adminClient.getUniqueTableName("tableId"))); + } + + @Test + public void modifyFamiliesAsync() { + ModifyFamilies modifyFamReq = TableAdminRequests.modifyFamilies("tableId"); + adminClient.modifyFamiliesAsync(modifyFamReq); + Mockito.verify(mockModifyTableCallable) + .futureCall(modifyFamReq.toProto(adminClient.getUniqueTableName("tableId"))); + } + + @Test + public void deleteTable() { + adminClient.deleteTable("tableId"); + Mockito.verify(mockDeleteTableCallable).call(adminClient.getDeleteTableRequest("tableId")); + } + + @Test + public void deleteTableAsync() { + adminClient.deleteTableAsync("tableId"); + Mockito.verify(mockDeleteTableCallable) + .futureCall(adminClient.getDeleteTableRequest("tableId")); + } + + @Test + public void getTable() { + adminClient.getTable("tableId"); + Mockito.verify(mockGetTableCallable).call(adminClient.getGetTableRequest("tableId")); + } + + @Test + public void getTableAsync() { + adminClient.getTableAsync("tableId"); + Mockito.verify(mockGetTableCallable).futureCall(adminClient.getGetTableRequest("tableId")); + } + + @Test + public void listTables() { + ListTablesResponse listTablesResponse = ListTablesResponse.newBuilder().build(); + Mockito.when(mockListTableCallable.call(adminClient.getListTableRequest())) + .thenReturn(listTablesResponse); + + adminClient.listTables(); + Mockito.verify(mockListTableCallable).call(adminClient.getListTableRequest()); + } + + @Test + public void listTablesAsync() { + ApiFuture listTablesResponse = + ApiFutures.immediateFuture(ListTablesResponse.newBuilder().build()); + Mockito.when(mockListTableCallable.futureCall(adminClient.getListTableRequest())) + .thenReturn(listTablesResponse); + + adminClient.listTablesAsync(); + Mockito.verify(mockListTableCallable).futureCall(adminClient.getListTableRequest()); + } + + @Test + public void dropRowRange() { + adminClient.dropRowRange("tableId", "rowKeyPrefix"); + Mockito.verify(mockDropRowRangeCallable) + .call(adminClient.getDropRowRangeRequest("tableId", "rowKeyPrefix")); + } + + @Test + public void getDropRowRangeRequest() { + DropRowRangeRequest actual = adminClient.getDropRowRangeRequest("tableId", "rowKeyPrefix"); + + DropRowRangeRequest expected = DropRowRangeRequest.newBuilder() + .setName(adminClient.getUniqueTableName("tableId")) + .setRowKeyPrefix(ByteString.copyFromUtf8("rowKeyPrefix")) + .build(); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void getDropRowRangeRequest_dropAllData() { + DropRowRangeRequest actual = adminClient.getDropRowRangeRequest("tableId", "*"); + + DropRowRangeRequest expected = DropRowRangeRequest.newBuilder() + .setName(adminClient.getUniqueTableName("tableId")) + .setDeleteAllDataFromTable(true) + .build(); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void dropRowRangeAsync() { + adminClient.dropRowRangeAsync("tableId", "rowKeyPrefix"); + Mockito.verify(mockDropRowRangeCallable) + .futureCall(adminClient.getDropRowRangeRequest("tableId", "rowKeyPrefix")); + } + + @Test + public void GenerateConsistencyToken() { + GenerateConsistencyTokenResponse genResp = + GenerateConsistencyTokenResponse.newBuilder().build(); + Mockito.when( + mockGenerateConsistencyTokenCallable.call(adminClient.getGenConsistencyToken("tableId"))) + .thenReturn(genResp); + + adminClient.GenerateConsistencyToken("tableId"); + Mockito.verify(mockGenerateConsistencyTokenCallable) + .call(adminClient.getGenConsistencyToken("tableId")); + } + + @Test + public void GenerateConsistencyTokenAsync() { + ApiFuture genResp = + ApiFutures.immediateFuture(GenerateConsistencyTokenResponse.newBuilder().build()); + Mockito.when(mockGenerateConsistencyTokenCallable.futureCall( + adminClient.getGenConsistencyToken("tableId"))).thenReturn(genResp); + + adminClient.generateConsistencyTokenAsync("tableId"); + Mockito.verify(mockGenerateConsistencyTokenCallable) + .futureCall(adminClient.getGenConsistencyToken("tableId")); + + } + + @Test + public void isConsistent() { + CheckConsistencyResponse consistencyResp = CheckConsistencyResponse.newBuilder().build(); + Mockito.when(mockCheckConsistencyCallable.call( + adminClient.getCheckConsistencyRequest("tableId", "token"))).thenReturn(consistencyResp); + + adminClient.isConsistent("tableId", "token"); + Mockito.verify(mockCheckConsistencyCallable) + .call(adminClient.getCheckConsistencyRequest("tableId", "token")); + } + + @Test + public void isConsistentAsync() { + ApiFuture consistencyResp = + ApiFutures.immediateFuture(CheckConsistencyResponse.newBuilder().build()); + Mockito.when(mockCheckConsistencyCallable.futureCall( + adminClient.getCheckConsistencyRequest("tableId", "token"))).thenReturn(consistencyResp); + + adminClient.isConsistentAsync("tableId", "token"); + Mockito.verify(mockCheckConsistencyCallable) + .futureCall(adminClient.getCheckConsistencyRequest("tableId", "token")); + } + + @Test + public void convert() { + ListTablesResponse listTablesResponse = + ListTablesResponse.newBuilder() + .addTables(Table.newBuilder().setName("projects/p/instances/i/tables/t1")) + .addTables(Table.newBuilder().setName("projects/p/instances/i/tables/t2")) + .build(); + + assertEquals(2, TableAdminClient.convert(listTablesResponse).size()); + + listTablesResponse = ListTablesResponse.newBuilder().build(); + assertEquals(0, TableAdminClient.convert(listTablesResponse).size()); + } +} + diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java new file mode 100644 index 000000000000..948bdec44063 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java @@ -0,0 +1,167 @@ +package com.google.cloud.bigtable.admin.v2.it; + +import static com.google.cloud.bigtable.admin.v2.models.GCRules.GCRULES; +import static org.junit.Assert.*; +import java.io.IOException; +import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import com.google.bigtable.admin.v2.InstanceName; +import com.google.bigtable.admin.v2.Table; +import com.google.bigtable.admin.v2.Table.TimestampGranularity; +import com.google.bigtable.admin.v2.TableName; +import com.google.cloud.bigtable.admin.v2.TableAdminClient; +import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests; +import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.CreateTable; +import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.ModifyFamilies; +import com.google.protobuf.ByteString; + +public class TableAdminClientIT { + static TableAdminClient tableAdmin; + + @BeforeClass + public static void setup() throws IOException { + tableAdmin = TableAdminClient.create(InstanceName.of("sduskis-hello-shakespear", "beam-test")); + } + + @AfterClass + public static void cleanup() throws Exception { + tableAdmin.close(); + } + + @Test + public void createTable() throws Exception { + String tableId = "adminCreateTest"; + CreateTable createTableReq = + TableAdminRequests.createTable(tableId).addColumnFamily("cf1") + .addColumnFamily("cf2", GCRULES.versions().maxNum(10)) + .withGranularity(TimestampGranularity.MILLIS) + .addSplit(ByteString.copyFromUtf8("b")) + .addSplit(ByteString.copyFromUtf8("q")); + + try { + Table table = tableAdmin.createTable(createTableReq); + assertNotNull(table); + assertTrue(table.getName().endsWith(tableId)); + assertEquals(2, table.getColumnFamiliesCount()); + assertFalse(table.getColumnFamiliesMap().get("cf1").hasGcRule()); + assertTrue(table.getColumnFamiliesMap().get("cf2").hasGcRule()); + assertEquals(10, table.getColumnFamiliesMap().get("cf2").getGcRule().getMaxNumVersions()); + assertEquals(TimestampGranularity.MILLIS, table.getGranularity()); + // TODO: is there a way to test splits here? + } finally { + tableAdmin.deleteTable(tableId); + } + } + + @Test + public void modifyFamilies() { + String tableId = "adminModifyFamTest"; + ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId); + modifyFamiliesReq.create("mf1").createWithGCRule("mf2", GCRULES.age().seconds(1000).nanos(20000)) + .updateWithGCRule("mf1", + GCRULES.union().rule(GCRULES.age().seconds(100)) + .rule(GCRULES.versions().maxNum(1))) + .createWithGCRule("mf3", + GCRULES.intersection().rule(GCRULES.age().seconds(2000)) + .rule(GCRULES.versions().maxNum(10))) + .createWithGCRule("mf4", + GCRULES.intersection().rule(GCRULES.age().seconds(360))) + .create("mf5").create("mf6").drop("mf5").drop("mf6").create("mf7"); + + try { + tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + Table table = tableAdmin.modifyFamilies(modifyFamiliesReq); + assertEquals(5, table.getColumnFamiliesCount()); + assertNotNull(table.getColumnFamiliesOrThrow("mf1")); + assertNotNull(table.getColumnFamiliesOrThrow("mf2")); + assertTrue(table.getColumnFamiliesOrThrow("mf1").getGcRule().hasUnion()); + assertEquals(1000, + table.getColumnFamiliesOrThrow("mf2").getGcRule().getMaxAge().getSeconds()); + assertEquals(20000, table.getColumnFamiliesOrThrow("mf2").getGcRule().getMaxAge().getNanos()); + assertTrue(table.getColumnFamiliesOrThrow("mf3").getGcRule().hasIntersection()); + assertFalse(table.getColumnFamiliesOrThrow("mf4").getGcRule().hasIntersection()); + assertTrue(table.getColumnFamiliesOrThrow("mf4").getGcRule().hasMaxAge()); + assertNotNull(table.getColumnFamiliesOrThrow("mf7")); + } finally { + tableAdmin.deleteTable(tableId); + } + } + + @Test + public void deleteTable() { + String tableId = "adminDeleteTest"; + tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + tableAdmin.deleteTable(tableId); + } + + @Test + public void getTable() { + String tableId = "adminGetTest"; + + try { + tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + Table table = tableAdmin.getTable(tableId); + assertNotNull(table); + assertTrue(table.getName().endsWith(tableId)); + } finally { + tableAdmin.deleteTable(tableId); + } + } + + @Test + public void listTables() { + String tableId = "adminListTest"; + + try { + tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + List tables = tableAdmin.listTables(); + assertNotNull(tables); + assertEquals(1, tables.size()); + } finally { + tableAdmin.deleteTable(tableId); + } + } + + @Test + public void listTablesAsync() throws Exception { + String tableId = "adminListTest"; + + try { + tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + List tables = tableAdmin.listTablesAsync().get(); + assertNotNull(tables); + assertEquals(1, tables.size()); + } finally { + tableAdmin.deleteTable(tableId); + } + } + + @Test + public void dropRowRange() { + String tableId = "adminDropRowrangeTest"; + + try { + tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + tableAdmin.dropRowRange(tableId, "*"); + } finally { + tableAdmin.deleteTable(tableId); + } + } + + @Test + public void CheckConsistency() { + String tableId = "adminConsistencyTest"; + + try { + tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + String token = tableAdmin.GenerateConsistencyToken(tableId); + boolean consistent = tableAdmin.isConsistent(tableId, token); + assertNotNull(token); + assertTrue(consistent); + } finally { + tableAdmin.deleteTable(tableId); + } + } +} diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java new file mode 100644 index 000000000000..a61ffeaa41d7 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java @@ -0,0 +1,226 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.cloud.bigtable.admin.v2.models; + +import static com.google.cloud.bigtable.admin.v2.models.GCRules.GCRULES; +import static com.google.common.truth.Truth.assertThat; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import com.google.bigtable.admin.v2.GcRule; +import com.google.bigtable.admin.v2.GcRule.Intersection; +import com.google.bigtable.admin.v2.GcRule.Union; +import com.google.protobuf.Duration; + +@RunWith(JUnit4.class) +public class GCRulesTest { + + @Test + public void duration() { + GcRule actual = GCRULES.age().seconds(61).nanos(9).toProto(); + + GcRule expected = buildAgeRule(61, 9); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void durationSeconds() { + GcRule actual = GCRULES.age().seconds(1).toProto(); + + GcRule expected = buildAgeRule(1, -1); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void durationNanos() { + GcRule actual = GCRULES.age().nanos(11).toProto(); + + GcRule expected = buildAgeRule(-1, 11); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void durationOverwrite() { + GcRule actual = GCRULES.age().seconds(61).nanos(9).seconds(1).nanos(5).seconds(4).toProto(); + + GcRule expected = buildAgeRule(4, 5); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void versions() { + GcRule actual = GCRULES.versions().maxNum(10).toProto(); + + GcRule expected = buildVersionsRule(10); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void unionEmpty() { + GcRule actual = GCRULES.union().toProto(); + + GcRule expected = GcRule.newBuilder().build(); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void unionOne() { + GcRule actual = GCRULES.union().rule(GCRULES.versions().maxNum(1)).toProto(); + + GcRule expected = buildVersionsRule(1);; + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void unionTwo() { + GcRule actual = + GCRULES.union().rule(GCRULES.versions().maxNum(1)).rule(GCRULES.age().seconds(1)).toProto(); + + GcRule expected = + GcRule.newBuilder() + .setUnion( + Union.newBuilder().addRules(buildVersionsRule(1)).addRules(buildAgeRule(1, -1))) + .build(); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void unionThree() { + GcRule actual = GCRULES.union().rule(GCRULES.versions().maxNum(1)) + .rule(GCRULES.age().seconds(1)).rule(GCRULES.age().nanos(1)).toProto(); + + GcRule expected = + GcRule.newBuilder() + .setUnion(Union.newBuilder().addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, -1)).addRules(buildAgeRule(-1, 1))) + .build(); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void intersectionEmpty() { + GcRule actual = GCRULES.intersection().toProto(); + + GcRule expected = GcRule.newBuilder().build(); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void intersectionOne() { + GcRule actual = GCRULES.intersection().rule(GCRULES.versions().maxNum(1)).toProto(); + + GcRule expected = buildVersionsRule(1);; + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void intersectionTwo() { + GcRule actual = GCRULES.intersection().rule(GCRULES.versions().maxNum(1)) + .rule(GCRULES.age().seconds(1)).toProto(); + + GcRule expected = GcRule.newBuilder().setIntersection( + Intersection.newBuilder().addRules(buildVersionsRule(1)).addRules(buildAgeRule(1, -1))) + .build(); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void intersectionThree() { + GcRule actual = GCRULES.intersection().rule(GCRULES.versions().maxNum(1)) + .rule(GCRULES.age().seconds(1)).rule(GCRULES.age().nanos(1)).toProto(); + + GcRule expected = + GcRule.newBuilder() + .setIntersection( + Intersection.newBuilder().addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, -1)).addRules(buildAgeRule(-1, 1))) + .build(); + + assertThat(actual).isEqualTo(expected); + } + + @Test + public void UnionOfIntersections() { + GcRule actual = GCRULES.union() + .rule(GCRULES.intersection().rule(GCRULES.versions().maxNum(1)) + .rule(GCRULES.age().seconds(1))) + .rule(GCRULES.intersection().rule(GCRULES.versions().maxNum(1)) + .rule(GCRULES.age().seconds(1))) + .toProto(); + + GcRule expected = + GcRule.newBuilder() + .setUnion(Union.newBuilder().addRules( + GcRule.newBuilder().setIntersection( + Intersection.newBuilder().addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, -1)))) + .addRules(GcRule.newBuilder().setIntersection( + Intersection.newBuilder().addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, -1))))) + .build(); + assertThat(actual).isEqualTo(expected); + } + + @Test + public void IntersectionOfUnion() { + GcRule actual = GCRULES.intersection() + .rule(GCRULES.union().rule(GCRULES.versions().maxNum(1)) + .rule(GCRULES.age().seconds(1))) + .rule(GCRULES.union().rule(GCRULES.versions().maxNum(1)) + .rule(GCRULES.age().seconds(1))) + .toProto(); + + GcRule expected = GcRule.newBuilder().setIntersection( + Intersection.newBuilder() + .addRules( + GcRule.newBuilder() + .setUnion(Union.newBuilder().addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, -1)))) + .addRules(GcRule.newBuilder() + .setUnion(Union.newBuilder().addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, -1))))) + .build(); + assertThat(actual).isEqualTo(expected); + } + + private GcRule buildAgeRule(int seconds, int nanos) { + Duration.Builder duartionBuilder = Duration.newBuilder(); + + if (seconds > -1) { + duartionBuilder.setSeconds(seconds); + } + + if (nanos > -1) { + duartionBuilder.setNanos(nanos); + } + + return GcRule.newBuilder().setMaxAge(duartionBuilder).build(); + } + + private GcRule buildVersionsRule(int maxVer) { + return GcRule.newBuilder().setMaxNumVersions(maxVer).build(); + } +} diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java new file mode 100644 index 000000000000..787f53dc0915 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java @@ -0,0 +1,81 @@ +package com.google.cloud.bigtable.admin.v2.models; + +import static com.google.common.truth.Truth.assertThat; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import com.google.bigtable.admin.v2.ColumnFamily; +import com.google.bigtable.admin.v2.CreateTableRequest; +import com.google.bigtable.admin.v2.Table; +import com.google.bigtable.admin.v2.CreateTableRequest.Split; +import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; +import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; +import com.google.bigtable.admin.v2.Table.TimestampGranularity; +import com.google.protobuf.ByteString; + +@RunWith(JUnit4.class) +public class TableAdminRequestsTest { + + @Test + public void createTable() { + CreateTableRequest actual = TableAdminRequests.createTable("tableId") + .withGranularity(TimestampGranularity.MILLIS) + .addColumnFamily("cf1") + .addColumnFamily("cf2", GCRules.GCRULES.versions().maxNum(1)) + .addSplit(ByteString.copyFromUtf8("c")).toProto("parent"); + + CreateTableRequest expected = + CreateTableRequest.newBuilder().setTableId("tableId").setParent("parent") + .addInitialSplits(Split.newBuilder().setKey(ByteString.copyFromUtf8("c"))) + .setTable( + Table.newBuilder().setGranularity(TimestampGranularity.MILLIS) + .putColumnFamilies("cf1", ColumnFamily.newBuilder().build()) + .putColumnFamilies("cf2", ColumnFamily.newBuilder().setGcRule(GCRules.GCRULES.versions().maxNum(1).toProto()).build())) + .build(); + + assertThat(actual).isEqualTo(expected); + } + + @Test (expected = NullPointerException.class) + public void createTable_RequiredTableId() { + TableAdminRequests.createTable(null).toProto("parent"); + } + + @Test (expected = NullPointerException.class) + public void createTable_RequiredParent() { + TableAdminRequests.createTable("tableId").toProto(null); + } + + @Test + public void modifyFamilies() { + ModifyColumnFamiliesRequest actual = TableAdminRequests.modifyFamilies("tableId") + .create("cf1") + .createWithGCRule("cf2", GCRules.GCRULES.versions().maxNum(1)) + .create("cf3") + .update("cf2") + .updateWithGCRule("cf1", GCRules.GCRULES.versions().maxNum(5)) + .drop("cf3") + .toProto("uniqueTableName"); + + ModifyColumnFamiliesRequest expected = ModifyColumnFamiliesRequest.newBuilder() + .setName("uniqueTableName") + .addModifications(Modification.newBuilder().setId("cf1").setCreate(ColumnFamily.newBuilder())) + .addModifications(Modification.newBuilder().setId("cf2").setCreate(ColumnFamily.newBuilder().setGcRule(GCRules.GCRULES.versions().maxNum(1).toProto()))) + .addModifications(Modification.newBuilder().setId("cf3").setCreate(ColumnFamily.newBuilder())) + .addModifications(Modification.newBuilder().setId("cf2").setUpdate(ColumnFamily.newBuilder())) + .addModifications(Modification.newBuilder().setId("cf1").setUpdate(ColumnFamily.newBuilder().setGcRule(GCRules.GCRULES.versions().maxNum(5).toProto()))) + .addModifications(Modification.newBuilder().setId("cf3").setDrop(true)) + .build(); + assertThat(actual).isEqualTo(expected); + } + + @Test (expected = NullPointerException.class) + public void modifyFamilies_RequiredTableId() { + TableAdminRequests.modifyFamilies(null).toProto("uniqueTableName"); + } + + @Test (expected = NullPointerException.class) + public void modifyFamilies_RequiredParent() { + TableAdminRequests.modifyFamilies("tableId").toProto(null); + } +} From c0ddeffb3623ffccfae7c2661a3514b97addf56d Mon Sep 17 00:00:00 2001 From: spollapally Date: Fri, 8 Jun 2018 00:39:40 -0400 Subject: [PATCH 02/10] Add table admin response wrappers, and implement review comments. --- .../bigtable/admin/v2/TableAdminClient.java | 222 ++++++++++------ .../bigtable/admin/v2/models/GCRules.java | 100 +++++--- .../admin/v2/models/TableAdminRequests.java | 76 +++--- .../admin/v2/models/TableAdminResponses.java | 236 ++++++++++++++++++ .../admin/v2/TableAdminClientTest.java | 183 ++++++++------ .../admin/v2/it/TableAdminClientIT.java | 133 ++++++---- .../bigtable/admin/v2/models/GCRulesTest.java | 227 +++++++++-------- .../v2/models/TableAdminRequestsTest.java | 119 ++++++--- .../v2/models/TableAdminResponsesTest.java | 197 +++++++++++++++ 9 files changed, 1077 insertions(+), 416 deletions(-) create mode 100644 google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java create mode 100644 google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java index ab87cb0c4e79..87c51bb138a7 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java @@ -1,3 +1,16 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ package com.google.cloud.bigtable.admin.v2; import java.io.IOException; @@ -22,6 +35,8 @@ import com.google.bigtable.admin.v2.TableName; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.CreateTable; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.ModifyFamilies; +import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses; +import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.TableResponse; import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStub; import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStubSettings; import com.google.common.annotations.VisibleForTesting; @@ -29,6 +44,7 @@ import com.google.protobuf.ByteString; import com.google.protobuf.Empty; +/** Table admin client for creating and configuring Bigtable tables. */ public class TableAdminClient implements AutoCloseable { private final BigtableTableAdminStub stub; private final InstanceName instanceName; @@ -37,8 +53,8 @@ public static TableAdminClient create(InstanceName instanceName) throws IOExcept return new TableAdminClient(instanceName, BigtableTableAdminSettings.newBuilder().build()); } - public static TableAdminClient create(InstanceName instanceName, - BigtableTableAdminSettings adminSettings) throws IOException { + public static TableAdminClient create( + InstanceName instanceName, BigtableTableAdminSettings adminSettings) throws IOException { return new TableAdminClient(instanceName, adminSettings); } @@ -47,13 +63,14 @@ public static TableAdminClient create(InstanceName instanceName, BigtableTableAd return new TableAdminClient(instanceName, stub); } - public TableAdminClient(InstanceName instanceName, BigtableTableAdminSettings adminSettings) + private TableAdminClient(InstanceName instanceName, BigtableTableAdminSettings adminSettings) throws IOException { - this(instanceName, - ((BigtableTableAdminStubSettings) adminSettings.getStubSettings()).createStub()); + this( + instanceName, + ((BigtableTableAdminStubSettings) adminSettings.getStubSettings()).createStub()); } - public TableAdminClient(InstanceName instanceName, BigtableTableAdminStub stub) + private TableAdminClient(InstanceName instanceName, BigtableTableAdminStub stub) throws IOException { Preconditions.checkNotNull(instanceName); Preconditions.checkNotNull(stub); @@ -61,11 +78,6 @@ public TableAdminClient(InstanceName instanceName, BigtableTableAdminStub stub) this.stub = stub; } - @SuppressWarnings("unused") - private BigtableTableAdminStub getTableAdminStub() { - return stub; - } - public InstanceName getInstanceName() { return instanceName; } @@ -75,84 +87,112 @@ public void close() throws Exception { stub.close(); } - public Table createTable(CreateTable createTable) { - return this.stub.createTableCallable().call(createTable.toProto(instanceName.toString())); + public TableResponse createTable(CreateTable createTable) { + Table table = this.stub.createTableCallable().call(createTable.toProto(instanceName)); + return TableAdminResponses.convertTable(table); } - public ApiFuture
createTableAsync(CreateTable createTable) { - return this.stub.createTableCallable().futureCall(createTable.toProto(instanceName.toString())); + public ApiFuture createTableAsync(CreateTable createTable) { + return transformToTableResponse( + this.stub.createTableCallable().futureCall(createTable.toProto(instanceName))); } - public Table modifyFamilies(ModifyFamilies modifyFamily) { + public TableResponse modifyFamilies(ModifyFamilies modifyFamily) { ModifyColumnFamiliesRequest modReq = - modifyFamily.toProto(getUniqueTableName(modifyFamily.getTableId())); - return this.stub.modifyColumnFamiliesCallable().call(modReq); + modifyFamily.toProto(getTableName(modifyFamily.getTableId())); + Table table = this.stub.modifyColumnFamiliesCallable().call(modReq); + return TableAdminResponses.convertTable(table); } - public ApiFuture
modifyFamiliesAsync(ModifyFamilies modifyFamily) { + public ApiFuture modifyFamiliesAsync(ModifyFamilies modifyFamily) { ModifyColumnFamiliesRequest modReq = - modifyFamily.toProto(getUniqueTableName(modifyFamily.getTableId())); - return this.stub.modifyColumnFamiliesCallable().futureCall(modReq); + modifyFamily.toProto(getTableName(modifyFamily.getTableId())); + return transformToTableResponse(this.stub.modifyColumnFamiliesCallable().futureCall(modReq)); } public void deleteTable(String tableId) { - this.stub.deleteTableCallable().call(getDeleteTableRequest(tableId)); + this.stub.deleteTableCallable().call(composeDeleteTableRequest(tableId)); } - public ApiFuture deleteTableAsync(String tableId) { - return this.stub.deleteTableCallable().futureCall(getDeleteTableRequest(tableId)); + public ApiFuture deleteTableAsync(String tableId) { + return transformToVoid( + this.stub.deleteTableCallable().futureCall(composeDeleteTableRequest(tableId))); } - public Table getTable(String tableId) { - return this.stub.getTableCallable().call(getGetTableRequest(tableId)); + public TableResponse getTable(String tableId) { + Table table = this.stub.getTableCallable().call(composeGetTableRequest(tableId)); + return TableAdminResponses.convertTable(table); } - public ApiFuture
getTableAsync(String tableId) { - return this.stub.getTableCallable().futureCall(getGetTableRequest(tableId)); + public ApiFuture getTableAsync(String tableId) { + return transformToTableResponse( + this.stub.getTableCallable().futureCall(composeGetTableRequest(tableId))); } public List listTables() { - ListTablesResponse listResp = this.stub.listTablesCallable().call(getListTableRequest()); - return convert(listResp); + ListTablesResponse listResp = this.stub.listTablesCallable().call(composeListTableRequest()); + return convertToTableNames(listResp); } public ApiFuture> listTablesAsync() { ApiFuture listResp = - this.stub.listTablesCallable().futureCall(getListTableRequest()); - - return ApiFutures.transform(listResp, new ApiFunction>() { - @Override - public List apply(ListTablesResponse input) { - return convert(input); - } - }); - } - - /** - * Drops rows by prefix. If prefix is "*" drops all data - * - * @param tableId - * @param rowKeyPrefix - */ + this.stub.listTablesCallable().futureCall(composeListTableRequest()); + + return ApiFutures.transform( + listResp, + new ApiFunction>() { + @Override + public List apply(ListTablesResponse input) { + return convertToTableNames(input); + } + }); + } + public void dropRowRange(String tableId, String rowKeyPrefix) { - this.stub.dropRowRangeCallable().call(getDropRowRangeRequest(tableId, rowKeyPrefix)); + dropRowRange(tableId, ByteString.copyFromUtf8(rowKeyPrefix)); + } + + public ApiFuture dropRowRangeAsync(String tableId, String rowKeyPrefix) { + return dropRowRangeAsync(tableId, ByteString.copyFromUtf8(rowKeyPrefix)); + } + + public void dropAllData(String tableId) { + this.stub.dropRowRangeCallable().call(composeDropRowRangeRequest(tableId, null, true)); + } + + public ApiFuture dropAllDataAsync(String tableId) { + return transformToVoid( + this.stub + .dropRowRangeCallable() + .futureCall(composeDropRowRangeRequest(tableId, null, true))); } - public ApiFuture dropRowRangeAsync(String tableId, String rowKeyPrefix) { - return this.stub.dropRowRangeCallable() - .futureCall(getDropRowRangeRequest(tableId, rowKeyPrefix)); + public void dropRowRange(String tableId, ByteString rowKeyPrefix) { + this.stub.dropRowRangeCallable().call(composeDropRowRangeRequest(tableId, rowKeyPrefix, false)); } - public String GenerateConsistencyToken(String tableId) { - return this.stub.generateConsistencyTokenCallable().call(getGenConsistencyToken(tableId)) - .getConsistencyToken(); + public ApiFuture dropRowRangeAsync(String tableId, ByteString rowKeyPrefix) { + return transformToVoid( + this.stub + .dropRowRangeCallable() + .futureCall(composeDropRowRangeRequest(tableId, rowKeyPrefix, false))); + } + + public String generateConsistencyToken(String tableId) { + return this.stub + .generateConsistencyTokenCallable() + .call(composeGenerateConsistencyTokenRequest(tableId)) + .getConsistencyToken(); } public ApiFuture generateConsistencyTokenAsync(String tableId) { ApiFuture tokenResp = - this.stub.generateConsistencyTokenCallable().futureCall(getGenConsistencyToken(tableId)); + this.stub + .generateConsistencyTokenCallable() + .futureCall(composeGenerateConsistencyTokenRequest(tableId)); - return ApiFutures.transform(tokenResp, + return ApiFutures.transform( + tokenResp, new ApiFunction() { @Override public String apply(GenerateConsistencyTokenResponse input) { @@ -162,15 +202,17 @@ public String apply(GenerateConsistencyTokenResponse input) { } public boolean isConsistent(String tableId, String token) { - return stub.checkConsistencyCallable().call(getCheckConsistencyRequest(tableId, token)) - .getConsistent(); + return stub.checkConsistencyCallable() + .call(composeCheckConsistencyRequest(tableId, token)) + .getConsistent(); } public ApiFuture isConsistentAsync(String tableId, String token) { ApiFuture CheckConsResp = - stub.checkConsistencyCallable().futureCall(getCheckConsistencyRequest(tableId, token)); + stub.checkConsistencyCallable().futureCall(composeCheckConsistencyRequest(tableId, token)); - return ApiFutures.transform(CheckConsResp, + return ApiFutures.transform( + CheckConsResp, new ApiFunction() { @Override public Boolean apply(CheckConsistencyResponse input) { @@ -180,53 +222,53 @@ public Boolean apply(CheckConsistencyResponse input) { } @VisibleForTesting - String getUniqueTableName(String tableId) { + String getTableName(String tableId) { return TableName.of(instanceName.getProject(), instanceName.getInstance(), tableId).toString(); } @VisibleForTesting - ListTablesRequest getListTableRequest() { + ListTablesRequest composeListTableRequest() { return ListTablesRequest.newBuilder().setParent(instanceName.toString()).build(); } @VisibleForTesting - GetTableRequest getGetTableRequest(String tableId) { - return GetTableRequest.newBuilder().setName(getUniqueTableName(tableId)).build(); + GetTableRequest composeGetTableRequest(String tableId) { + return GetTableRequest.newBuilder().setName(getTableName(tableId)).build(); } @VisibleForTesting - DeleteTableRequest getDeleteTableRequest(String tableId) { - return DeleteTableRequest.newBuilder().setName(getUniqueTableName(tableId)).build(); + DeleteTableRequest composeDeleteTableRequest(String tableId) { + return DeleteTableRequest.newBuilder().setName(getTableName(tableId)).build(); } @VisibleForTesting - DropRowRangeRequest getDropRowRangeRequest(String tableId, String rowKeyPrefix) { - Builder dropRowReq = DropRowRangeRequest.newBuilder().setName(getUniqueTableName(tableId)); + DropRowRangeRequest composeDropRowRangeRequest( + String tableId, ByteString rowKeyPrefix, boolean dropAll) { + Builder dropRowReq = DropRowRangeRequest.newBuilder().setName(getTableName(tableId)); - // TODO: verify if Utf8String and ByteString are equivalent - // and if using "*" prefix as special case to invoke deletAll approach is reasonable - if ("*".equals(rowKeyPrefix)) { + if (dropAll) { dropRowReq.setDeleteAllDataFromTable(true); } else { - dropRowReq.setRowKeyPrefix(ByteString.copyFromUtf8(rowKeyPrefix)); + dropRowReq.setRowKeyPrefix(rowKeyPrefix); } return dropRowReq.build(); } @VisibleForTesting - GenerateConsistencyTokenRequest getGenConsistencyToken(String tableId) { - return GenerateConsistencyTokenRequest.newBuilder().setName(getUniqueTableName(tableId)) - .build(); + GenerateConsistencyTokenRequest composeGenerateConsistencyTokenRequest(String tableId) { + return GenerateConsistencyTokenRequest.newBuilder().setName(getTableName(tableId)).build(); } @VisibleForTesting - CheckConsistencyRequest getCheckConsistencyRequest(String tableId, String token) { - return CheckConsistencyRequest.newBuilder().setName(getUniqueTableName(tableId)) - .setConsistencyToken(token).build(); + CheckConsistencyRequest composeCheckConsistencyRequest(String tableId, String token) { + return CheckConsistencyRequest.newBuilder() + .setName(getTableName(tableId)) + .setConsistencyToken(token) + .build(); } @VisibleForTesting - static List convert(ListTablesResponse listTablesResponse) { + static List convertToTableNames(ListTablesResponse listTablesResponse) { List tableNames = new ArrayList<>(); for (Table table : listTablesResponse.getTablesList()) { @@ -234,4 +276,28 @@ static List convert(ListTablesResponse listTablesResponse) { } return tableNames; } + + @VisibleForTesting + static ApiFuture transformToTableResponse(ApiFuture
future) { + return ApiFutures.transform( + future, + new ApiFunction() { + @Override + public TableResponse apply(Table table) { + return TableAdminResponses.convertTable(table); + } + }); + } + + @VisibleForTesting + static ApiFuture transformToVoid(ApiFuture future) { + return ApiFutures.transform( + future, + new ApiFunction() { + @Override + public Void apply(Empty empty) { + return null; + } + }); + } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java index 7cfc963e811d..f7a146dd9772 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java @@ -1,15 +1,28 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ package com.google.cloud.bigtable.admin.v2.models; +import java.util.concurrent.TimeUnit; import javax.annotation.Nonnull; +import org.threeten.bp.Duration; import com.google.bigtable.admin.v2.GcRule; -import com.google.common.base.Preconditions; -import com.google.protobuf.Duration; public final class GCRules { public static final GCRules GCRULES = new GCRules(); - + private GCRules() {} - + public IntersectionRule intersection() { return new IntersectionRule(); } @@ -18,26 +31,36 @@ public UnionRule union() { return new UnionRule(); } - public VersionRule versions() { - return new VersionRule(); + public VersionRule maxVersions(int maxVersion) { + return new VersionRule(maxVersion); + } + + public DurationRule maxAge(long maxAge, TimeUnit timeUnit) { + Duration duration = Duration.ZERO; + TimeUnit.SECONDS.convert(maxAge, timeUnit); + return maxAge(duration); } - - public DurationRule age() { - return new DurationRule(); + + public DurationRule maxAge(Duration duration) { + return new DurationRule(duration); + } + + public DefaultRule defaulRule() { + return new DefaultRule(); } public static final class IntersectionRule implements GCRule { private GcRule.Intersection.Builder builder; - + private IntersectionRule() { this.builder = GcRule.Intersection.newBuilder(); } - + public IntersectionRule rule(@Nonnull GCRule rule) { builder.addRules(rule.toProto()); return this; } - + @Override public GcRule toProto() { switch (builder.getRulesCount()) { @@ -50,14 +73,14 @@ public GcRule toProto() { } } } - + public static final class UnionRule implements GCRule { private GcRule.Union.Builder builder; - + private UnionRule() { this.builder = GcRule.Union.newBuilder(); } - + public UnionRule rule(@Nonnull GCRule rule) { builder.addRules(rule.toProto()); return this; @@ -77,16 +100,11 @@ public GcRule toProto() { } public static final class VersionRule implements GCRule { - private GcRule.Builder builder ; - - private VersionRule() { - this.builder = GcRule.newBuilder(); - } - - public VersionRule maxNum(int value) { - Preconditions.checkNotNull(value); - builder.setMaxNumVersions(value); - return this; + private GcRule.Builder builder; + + private VersionRule(int maxVersion) { + this.builder = GcRule.newBuilder(); + builder.setMaxNumVersions(maxVersion); } @Override @@ -96,22 +114,13 @@ public GcRule toProto() { } public static final class DurationRule implements GCRule { - private Duration.Builder builder; - - private DurationRule() { - this.builder = Duration.newBuilder(); - } - - public DurationRule seconds(long value) { - Preconditions.checkNotNull(value); - builder.setSeconds(value); - return this; - } + private com.google.protobuf.Duration.Builder builder; - public DurationRule nanos(int value) { - Preconditions.checkNotNull(value); - builder.setNanos(value); - return this; + private DurationRule(Duration duration) { + this.builder = + com.google.protobuf.Duration.newBuilder() + .setSeconds(duration.getSeconds()) + .setNanos(duration.getNano()); } @Override @@ -120,7 +129,16 @@ public GcRule toProto() { } } - interface GCRule extends Cloneable { + public static final class DefaultRule implements GCRule { + private DefaultRule() {} + + @Override + public GcRule toProto() { + return GcRule.getDefaultInstance(); + } + } + + interface GCRule { GcRule toProto(); } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java index b33eaa4f08c1..5d5ace05c31e 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java @@ -1,3 +1,16 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ package com.google.cloud.bigtable.admin.v2.models; import com.google.bigtable.admin.v2.ColumnFamily; @@ -5,16 +18,15 @@ import com.google.bigtable.admin.v2.Table; import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.bigtable.admin.v2.CreateTableRequest.Split; +import com.google.bigtable.admin.v2.GcRule; +import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; -/** - * Idiomatic models to build Bigtable admin requests - * - */ +/** Idiomatic models to build Bigtable admin requests */ public final class TableAdminRequests { private TableAdminRequests() {} @@ -48,8 +60,8 @@ public CreateTable addColumnFamily(String familyId) { public CreateTable addColumnFamily(String familyId, GCRule gcRule) { Preconditions.checkNotNull(familyId); - tableRequest.putColumnFamilies(familyId, - ColumnFamily.newBuilder().setGcRule(gcRule.toProto()).build()); + tableRequest.putColumnFamilies( + familyId, ColumnFamily.newBuilder().setGcRule(gcRule.toProto()).build()); return this; } @@ -59,22 +71,25 @@ public CreateTable addSplit(ByteString key) { return this; } - public CreateTable adduniformSplits(ByteString key) { + public CreateTable addUniformSplits(ByteString key, int numSplits) { Preconditions.checkNotNull(key); // TODO: add implementation throw new UnsupportedOperationException(); } - public CreateTableRequest toProto(String parent) { - Preconditions.checkNotNull(parent); - return createTableRequest.setParent(parent).setTable(tableRequest.build()).build(); + public CreateTableRequest toProto(InstanceName instanceName) { + Preconditions.checkNotNull(instanceName); + return createTableRequest + .setParent(instanceName.toString()) + .setTable(tableRequest.build()) + .build(); } } - public static class ModifyFamilies { - final ModifyColumnFamiliesRequest.Builder modFamilyRequest = + public static final class ModifyFamilies { + private final ModifyColumnFamiliesRequest.Builder modFamilyRequest = ModifyColumnFamiliesRequest.newBuilder(); - String tableId; + private String tableId; private ModifyFamilies(String tableId) { Preconditions.checkNotNull(tableId); @@ -85,37 +100,20 @@ public String getTableId() { return this.tableId; } - private Modification.Builder verifyAndCreate(String familyId) { - Preconditions.checkNotNull(familyId); - Modification.Builder modification = Modification.newBuilder(); - modification.setId(familyId); - return modification; - } - public ModifyFamilies create(String familyId) { - Modification.Builder modification = verifyAndCreate(familyId); - modification.setCreate(ColumnFamily.newBuilder()); - modFamilyRequest.addModifications(modification.build()); - return this; + return createWithGCRule(familyId, null); } public ModifyFamilies createWithGCRule(String familyId, GCRule gcRule) { - Modification.Builder modification = verifyAndCreate(familyId); - Preconditions.checkNotNull(gcRule); - modification.setCreate(ColumnFamily.newBuilder().setGcRule(gcRule.toProto())); - modFamilyRequest.addModifications(modification.build()); - return this; - } - - public ModifyFamilies update(String familyId) { - Modification.Builder modification = verifyAndCreate(familyId); - modification.setUpdate(ColumnFamily.newBuilder()); + Modification.Builder modification = Modification.newBuilder().setId(familyId); + GcRule grule = (gcRule == null) ? GcRule.getDefaultInstance() : gcRule.toProto(); + modification.setCreate(ColumnFamily.newBuilder().setGcRule(grule)); modFamilyRequest.addModifications(modification.build()); return this; } public ModifyFamilies updateWithGCRule(String familyId, GCRule gcRule) { - Modification.Builder modification = verifyAndCreate(familyId); + Modification.Builder modification = Modification.newBuilder().setId(familyId); Preconditions.checkNotNull(gcRule); modification.setUpdate(ColumnFamily.newBuilder().setGcRule(gcRule.toProto())); modFamilyRequest.addModifications(modification.build()); @@ -123,15 +121,15 @@ public ModifyFamilies updateWithGCRule(String familyId, GCRule gcRule) { } public ModifyFamilies drop(String familyId) { - Modification.Builder modification = verifyAndCreate(familyId); + Modification.Builder modification = Modification.newBuilder().setId(familyId); modification.setId(familyId).setDrop(true); modFamilyRequest.addModifications(modification.build()); return this; } - public ModifyColumnFamiliesRequest toProto(String uniqueTableName) { - Preconditions.checkNotNull(uniqueTableName); - return modFamilyRequest.setName(uniqueTableName).build(); + public ModifyColumnFamiliesRequest toProto(String tableName) { + Preconditions.checkNotNull(tableName); + return modFamilyRequest.setName(tableName).build(); } } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java new file mode 100644 index 000000000000..f26f1b26370a --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java @@ -0,0 +1,236 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.cloud.bigtable.admin.v2.models; + +import static com.google.cloud.bigtable.admin.v2.models.GCRules.GCRULES; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import org.threeten.bp.Duration; +import com.google.bigtable.admin.v2.Table.ClusterState; +import com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState; +import com.google.bigtable.admin.v2.Table.TimestampGranularity; +import com.google.bigtable.admin.v2.ColumnFamily; +import com.google.bigtable.admin.v2.GcRule; +import com.google.bigtable.admin.v2.GcRule.RuleCase; +import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; +import com.google.bigtable.admin.v2.Table; +import com.google.bigtable.admin.v2.TableName; +import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.IntersectionRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.UnionRule; +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; + +/** + * Table admin response wrapper objects to limit exposure to the underlying protocol buffer objects + */ +public class TableAdminResponses { + private TableAdminResponses() {} + + public static TableResponse convertTable(Table table) { + return new TableResponse(table); + } + + public static ConsistencyTokenResponse convertTokenResponse( + GenerateConsistencyTokenResponse tokenResponse) { + return new ConsistencyTokenResponse(tokenResponse); + } + + /** Wrapper on {@link Table} protocol buffer object */ + public static final class TableResponse { + private TableName tableName; + private TimestampGranularity timestampGranularity; + private Map clusterStates = new HashMap<>(); + // TODO - fix spelling + private Map columnFamilies = new HashMap<>(); + + private TableResponse(Table table) { + Preconditions.checkNotNull(table); + this.tableName = TableName.parse(table.getName()); + this.timestampGranularity = table.getGranularity(); + + Map clusterStatesMap = table.getClusterStatesMap(); + for (Entry entry : clusterStatesMap.entrySet()) { + clusterStates.put( + entry.getKey(), new ClusterStateResponse(entry.getKey(), entry.getValue())); + } + + Map columnFamiliesMap = table.getColumnFamiliesMap(); + for (Entry entry : columnFamiliesMap.entrySet()) { + columnFamilies.put( + entry.getKey(), new ColumnFamilyResponse(entry.getKey(), entry.getValue())); + } + } + + public TableName getTableName() { + return tableName; + } + + public TimestampGranularity getTimestampGranularity() { + return timestampGranularity; + } + + public Map getClusterStatesMap() { + return clusterStates; + } + + public Map getColumnFamiliesMap() { + return columnFamilies; + } + + public Collection getClusterStates() { + return clusterStates.values(); + } + + public Collection getColumnFamiles() { + return columnFamilies.values(); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("tableName", tableName) + .add("timestampGranularity", timestampGranularity) + .add("clusterStates", getClusterStates()) + .add("columnFamiles", getColumnFamiles()) + .toString(); + } + } + + /** Wrapper on {@link ClusterStateResponse} protocol buffer object */ + public static final class ClusterStateResponse { + private String id; + private ReplicationState replicationState; + + private ClusterStateResponse(String id, ClusterState clusterState) { + this.id = id; + replicationState = clusterState.getReplicationState(); + } + + public String getId() { + return id; + } + + public ReplicationState getReplicationState() { + return replicationState; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("is", id) + .add("replicationState", replicationState) + .toString(); + } + } + + /** Wrapper on {@link ColumnFamily} protocol buffer object */ + public static final class ColumnFamilyResponse { + private String id; + private GCRule gCRule = GCRULES.defaulRule(); + + private ColumnFamilyResponse(String id, ColumnFamily cf) { + Preconditions.checkNotNull(id); + Preconditions.checkNotNull(cf); + this.id = id; + + if (cf.getGcRule() != null) { + this.gCRule = convertGcRule(cf.getGcRule()); + } + } + + public String getId() { + return id; + } + + public GcRule getGcRule() { + // TODO: Exposing the protobuf to let users traverse the rule hierarchy as a quick hack + // If hierarchy is actually limited in real use, it might have more options to consider + return gCRule.toProto(); + } + + public boolean hasGcRule() { + return !RuleCase.RULE_NOT_SET.equals(gCRule.toProto().getRuleCase()); + } + + public boolean hasUnion() { + return gCRule.toProto().hasUnion(); + } + + public boolean hasIntersection() { + return gCRule.toProto().hasIntersection(); + } + + public boolean hasMaxAge() { + return gCRule.toProto().hasMaxAge(); + } + + @Override + public String toString() { + // TODO: fix inconsistent json format + return MoreObjects.toStringHelper(this) + .add("id", id) + .add("gCRule", gCRule.toProto()) + .toString(); + } + + private GCRule convertGcRule(GcRule source) { + switch (source.getRuleCase()) { + case MAX_AGE: + return GCRULES.maxAge( + Duration.ofSeconds(source.getMaxAge().getSeconds(), source.getMaxAge().getNanos())); + + case MAX_NUM_VERSIONS: + return GCRULES.maxVersions(source.getMaxNumVersions()); + + case INTERSECTION: + IntersectionRule intersection = GCRules.GCRULES.intersection(); + for (GcRule rule : source.getIntersection().getRulesList()) { + intersection.rule(convertGcRule(rule)); + } + return intersection; + + case UNION: + UnionRule union = GCRules.GCRULES.union(); + for (GcRule rule : source.getUnion().getRulesList()) { + union.rule(convertGcRule(rule)); + } + return union; + + default: + return GCRULES.defaulRule(); + } + } + } + + /** Wrapper on {@link GenerateConsistencyTokenResponse#getConsistencyToken()} */ + public static final class ConsistencyTokenResponse { + private String token; + + private ConsistencyTokenResponse(GenerateConsistencyTokenResponse resp) { + this.token = resp.getConsistencyToken(); + } + + public String getToken() { + return token; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this).add("token", token).toString(); + } + } +} diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java index 7c9f498038f6..673178e43182 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java @@ -1,8 +1,10 @@ package com.google.cloud.bigtable.admin.v2; -import org.junit.Before; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import java.util.List; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -24,6 +26,7 @@ import com.google.bigtable.admin.v2.ListTablesResponse; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; import com.google.bigtable.admin.v2.Table; +import com.google.bigtable.admin.v2.TableName; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.CreateTable; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.ModifyFamilies; @@ -34,29 +37,26 @@ @RunWith(MockitoJUnitRunner.class) public class TableAdminClientTest { private TableAdminClient adminClient; - @Mock - private BigtableTableAdminStub mockStub; + @Mock private BigtableTableAdminStub mockStub; + + @Mock private UnaryCallable mockCreateTableCallable; + @Mock private UnaryCallable mockModifyTableCallable; + @Mock private UnaryCallable mockDeleteTableCallable; + @Mock private UnaryCallable mockGetTableCallable; + @Mock private UnaryCallable mockListTableCallable; + @Mock private UnaryCallable mockDropRowRangeCallable; @Mock - private UnaryCallable mockCreateTableCallable; - @Mock - private UnaryCallable mockModifyTableCallable; - @Mock - private UnaryCallable mockDeleteTableCallable; - @Mock - private UnaryCallable mockGetTableCallable; - @Mock - private UnaryCallable mockListTableCallable; - @Mock - private UnaryCallable mockDropRowRangeCallable; - @Mock - private UnaryCallable mockGenerateConsistencyTokenCallable; + private UnaryCallable + mockGenerateConsistencyTokenCallable; + @Mock - private UnaryCallable mockCheckConsistencyCallable; + private UnaryCallable + mockCheckConsistencyCallable; @Before public void setUp() throws Exception { - adminClient = new TableAdminClient(InstanceName.of("[PROJECT]", "[INSTANCE]"), mockStub); + adminClient = TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), mockStub); Mockito.when(mockStub.createTableCallable()).thenReturn(mockCreateTableCallable); Mockito.when(mockStub.modifyColumnFamiliesCallable()).thenReturn(mockModifyTableCallable); @@ -65,8 +65,21 @@ public void setUp() throws Exception { Mockito.when(mockStub.listTablesCallable()).thenReturn(mockListTableCallable); Mockito.when(mockStub.dropRowRangeCallable()).thenReturn(mockDropRowRangeCallable); Mockito.when(mockStub.generateConsistencyTokenCallable()) - .thenReturn(mockGenerateConsistencyTokenCallable); + .thenReturn(mockGenerateConsistencyTokenCallable); Mockito.when(mockStub.checkConsistencyCallable()).thenReturn(mockCheckConsistencyCallable); + + Table table = Table.newBuilder().build(); + ApiFuture
futureTable = ApiFutures.immediateFuture(table); + Mockito.when(mockCreateTableCallable.call(any(CreateTableRequest.class))).thenReturn(table); + Mockito.when(mockCreateTableCallable.futureCall(any(CreateTableRequest.class))) + .thenReturn(futureTable); + Mockito.when(mockModifyTableCallable.call(any(ModifyColumnFamiliesRequest.class))) + .thenReturn(table); + Mockito.when(mockModifyTableCallable.futureCall(any(ModifyColumnFamiliesRequest.class))) + .thenReturn(futureTable); + Mockito.when(mockGetTableCallable.call(any(GetTableRequest.class))).thenReturn(table); + Mockito.when(mockGetTableCallable.futureCall(any(GetTableRequest.class))) + .thenReturn(futureTable); } @Test @@ -80,7 +93,7 @@ public void createTable() { CreateTable createTableReq = TableAdminRequests.createTable("tableId"); adminClient.createTable(createTableReq); Mockito.verify(mockCreateTableCallable) - .call(createTableReq.toProto(adminClient.getInstanceName().toString())); + .call(createTableReq.toProto(adminClient.getInstanceName())); } @Test @@ -88,7 +101,7 @@ public void createTableAsync() { CreateTable createTableReq = TableAdminRequests.createTable("tableId"); adminClient.createTableAsync(createTableReq); Mockito.verify(mockCreateTableCallable) - .futureCall(createTableReq.toProto(adminClient.getInstanceName().toString())); + .futureCall(createTableReq.toProto(adminClient.getInstanceName())); } @Test @@ -96,7 +109,7 @@ public void modifyFamilies() { ModifyFamilies modifyFamReq = TableAdminRequests.modifyFamilies("tableId"); adminClient.modifyFamilies(modifyFamReq); Mockito.verify(mockModifyTableCallable) - .call(modifyFamReq.toProto(adminClient.getUniqueTableName("tableId"))); + .call(modifyFamReq.toProto(adminClient.getTableName("tableId"))); } @Test @@ -104,154 +117,178 @@ public void modifyFamiliesAsync() { ModifyFamilies modifyFamReq = TableAdminRequests.modifyFamilies("tableId"); adminClient.modifyFamiliesAsync(modifyFamReq); Mockito.verify(mockModifyTableCallable) - .futureCall(modifyFamReq.toProto(adminClient.getUniqueTableName("tableId"))); + .futureCall(modifyFamReq.toProto(adminClient.getTableName("tableId"))); } @Test public void deleteTable() { adminClient.deleteTable("tableId"); - Mockito.verify(mockDeleteTableCallable).call(adminClient.getDeleteTableRequest("tableId")); + Mockito.verify(mockDeleteTableCallable).call(adminClient.composeDeleteTableRequest("tableId")); } @Test public void deleteTableAsync() { + ApiFuture empty = ApiFutures.immediateFuture(Empty.newBuilder().build()); + Mockito.when(mockDeleteTableCallable.futureCall(any(DeleteTableRequest.class))) + .thenReturn(empty); + adminClient.deleteTableAsync("tableId"); Mockito.verify(mockDeleteTableCallable) - .futureCall(adminClient.getDeleteTableRequest("tableId")); + .futureCall(adminClient.composeDeleteTableRequest("tableId")); } @Test public void getTable() { adminClient.getTable("tableId"); - Mockito.verify(mockGetTableCallable).call(adminClient.getGetTableRequest("tableId")); + Mockito.verify(mockGetTableCallable).call(adminClient.composeGetTableRequest("tableId")); } @Test public void getTableAsync() { adminClient.getTableAsync("tableId"); - Mockito.verify(mockGetTableCallable).futureCall(adminClient.getGetTableRequest("tableId")); + Mockito.verify(mockGetTableCallable).futureCall(adminClient.composeGetTableRequest("tableId")); } @Test public void listTables() { ListTablesResponse listTablesResponse = ListTablesResponse.newBuilder().build(); - Mockito.when(mockListTableCallable.call(adminClient.getListTableRequest())) - .thenReturn(listTablesResponse); + Mockito.when(mockListTableCallable.call(adminClient.composeListTableRequest())) + .thenReturn(listTablesResponse); adminClient.listTables(); - Mockito.verify(mockListTableCallable).call(adminClient.getListTableRequest()); + Mockito.verify(mockListTableCallable).call(adminClient.composeListTableRequest()); } @Test public void listTablesAsync() { ApiFuture listTablesResponse = ApiFutures.immediateFuture(ListTablesResponse.newBuilder().build()); - Mockito.when(mockListTableCallable.futureCall(adminClient.getListTableRequest())) - .thenReturn(listTablesResponse); + Mockito.when(mockListTableCallable.futureCall(adminClient.composeListTableRequest())) + .thenReturn(listTablesResponse); adminClient.listTablesAsync(); - Mockito.verify(mockListTableCallable).futureCall(adminClient.getListTableRequest()); + Mockito.verify(mockListTableCallable).futureCall(adminClient.composeListTableRequest()); } @Test public void dropRowRange() { adminClient.dropRowRange("tableId", "rowKeyPrefix"); Mockito.verify(mockDropRowRangeCallable) - .call(adminClient.getDropRowRangeRequest("tableId", "rowKeyPrefix")); + .call( + adminClient.composeDropRowRangeRequest( + "tableId", ByteString.copyFromUtf8("rowKeyPrefix"), false)); } @Test public void getDropRowRangeRequest() { - DropRowRangeRequest actual = adminClient.getDropRowRangeRequest("tableId", "rowKeyPrefix"); - - DropRowRangeRequest expected = DropRowRangeRequest.newBuilder() - .setName(adminClient.getUniqueTableName("tableId")) - .setRowKeyPrefix(ByteString.copyFromUtf8("rowKeyPrefix")) - .build(); - + DropRowRangeRequest actual = + adminClient.composeDropRowRangeRequest( + "tableId", ByteString.copyFromUtf8("rowKeyPrefix"), false); + + DropRowRangeRequest expected = + DropRowRangeRequest.newBuilder() + .setName(adminClient.getTableName("tableId")) + .setRowKeyPrefix(ByteString.copyFromUtf8("rowKeyPrefix")) + .build(); + assertThat(actual).isEqualTo(expected); } @Test public void getDropRowRangeRequest_dropAllData() { - DropRowRangeRequest actual = adminClient.getDropRowRangeRequest("tableId", "*"); - - DropRowRangeRequest expected = DropRowRangeRequest.newBuilder() - .setName(adminClient.getUniqueTableName("tableId")) - .setDeleteAllDataFromTable(true) - .build(); - + DropRowRangeRequest actual = adminClient.composeDropRowRangeRequest("tableId", null, true); + + DropRowRangeRequest expected = + DropRowRangeRequest.newBuilder() + .setName(adminClient.getTableName("tableId")) + .setDeleteAllDataFromTable(true) + .build(); + assertThat(actual).isEqualTo(expected); } @Test public void dropRowRangeAsync() { + ApiFuture empty = ApiFutures.immediateFuture(Empty.newBuilder().build()); + Mockito.when(mockDropRowRangeCallable.futureCall(any(DropRowRangeRequest.class))) + .thenReturn(empty); + adminClient.dropRowRangeAsync("tableId", "rowKeyPrefix"); Mockito.verify(mockDropRowRangeCallable) - .futureCall(adminClient.getDropRowRangeRequest("tableId", "rowKeyPrefix")); + .futureCall( + adminClient.composeDropRowRangeRequest( + "tableId", ByteString.copyFromUtf8("rowKeyPrefix"), false)); } @Test - public void GenerateConsistencyToken() { + public void generateConsistencyToken() { GenerateConsistencyTokenResponse genResp = GenerateConsistencyTokenResponse.newBuilder().build(); Mockito.when( - mockGenerateConsistencyTokenCallable.call(adminClient.getGenConsistencyToken("tableId"))) - .thenReturn(genResp); + mockGenerateConsistencyTokenCallable.call( + adminClient.composeGenerateConsistencyTokenRequest("tableId"))) + .thenReturn(genResp); - adminClient.GenerateConsistencyToken("tableId"); + adminClient.generateConsistencyToken("tableId"); Mockito.verify(mockGenerateConsistencyTokenCallable) - .call(adminClient.getGenConsistencyToken("tableId")); + .call(adminClient.composeGenerateConsistencyTokenRequest("tableId")); } @Test - public void GenerateConsistencyTokenAsync() { + public void generateConsistencyTokenAsync() { ApiFuture genResp = ApiFutures.immediateFuture(GenerateConsistencyTokenResponse.newBuilder().build()); - Mockito.when(mockGenerateConsistencyTokenCallable.futureCall( - adminClient.getGenConsistencyToken("tableId"))).thenReturn(genResp); + Mockito.when( + mockGenerateConsistencyTokenCallable.futureCall( + adminClient.composeGenerateConsistencyTokenRequest("tableId"))) + .thenReturn(genResp); adminClient.generateConsistencyTokenAsync("tableId"); Mockito.verify(mockGenerateConsistencyTokenCallable) - .futureCall(adminClient.getGenConsistencyToken("tableId")); - + .futureCall(adminClient.composeGenerateConsistencyTokenRequest("tableId")); } @Test public void isConsistent() { CheckConsistencyResponse consistencyResp = CheckConsistencyResponse.newBuilder().build(); - Mockito.when(mockCheckConsistencyCallable.call( - adminClient.getCheckConsistencyRequest("tableId", "token"))).thenReturn(consistencyResp); + Mockito.when( + mockCheckConsistencyCallable.call( + adminClient.composeCheckConsistencyRequest("tableId", "token"))) + .thenReturn(consistencyResp); adminClient.isConsistent("tableId", "token"); Mockito.verify(mockCheckConsistencyCallable) - .call(adminClient.getCheckConsistencyRequest("tableId", "token")); + .call(adminClient.composeCheckConsistencyRequest("tableId", "token")); } @Test public void isConsistentAsync() { ApiFuture consistencyResp = ApiFutures.immediateFuture(CheckConsistencyResponse.newBuilder().build()); - Mockito.when(mockCheckConsistencyCallable.futureCall( - adminClient.getCheckConsistencyRequest("tableId", "token"))).thenReturn(consistencyResp); + Mockito.when( + mockCheckConsistencyCallable.futureCall( + adminClient.composeCheckConsistencyRequest("tableId", "token"))) + .thenReturn(consistencyResp); adminClient.isConsistentAsync("tableId", "token"); Mockito.verify(mockCheckConsistencyCallable) - .futureCall(adminClient.getCheckConsistencyRequest("tableId", "token")); + .futureCall(adminClient.composeCheckConsistencyRequest("tableId", "token")); } @Test - public void convert() { + public void convertToTableNames() { ListTablesResponse listTablesResponse = ListTablesResponse.newBuilder() - .addTables(Table.newBuilder().setName("projects/p/instances/i/tables/t1")) - .addTables(Table.newBuilder().setName("projects/p/instances/i/tables/t2")) - .build(); + .addTables(Table.newBuilder().setName("projects/p/instances/i/tables/t1")) + .addTables(Table.newBuilder().setName("projects/p/instances/i/tables/t2")) + .build(); - assertEquals(2, TableAdminClient.convert(listTablesResponse).size()); + List tableNames = TableAdminClient.convertToTableNames(listTablesResponse); + assertEquals(2, tableNames.size()); + assertEquals("projects/p/instances/i/tables/t1", tableNames.get(0).toString()); + assertEquals("projects/p/instances/i/tables/t2", tableNames.get(1).toString()); listTablesResponse = ListTablesResponse.newBuilder().build(); - assertEquals(0, TableAdminClient.convert(listTablesResponse).size()); + assertEquals(0, TableAdminClient.convertToTableNames(listTablesResponse).size()); } } - diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java index 948bdec44063..3308d6466722 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java @@ -1,20 +1,39 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.google.cloud.bigtable.admin.v2.it; import static com.google.cloud.bigtable.admin.v2.models.GCRules.GCRULES; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.io.IOException; import java.util.List; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import org.threeten.bp.Duration; import com.google.bigtable.admin.v2.InstanceName; -import com.google.bigtable.admin.v2.Table; import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.bigtable.admin.v2.TableName; import com.google.cloud.bigtable.admin.v2.TableAdminClient; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.CreateTable; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.ModifyFamilies; +import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.TableResponse; import com.google.protobuf.ByteString; public class TableAdminClientIT { @@ -34,21 +53,23 @@ public static void cleanup() throws Exception { public void createTable() throws Exception { String tableId = "adminCreateTest"; CreateTable createTableReq = - TableAdminRequests.createTable(tableId).addColumnFamily("cf1") - .addColumnFamily("cf2", GCRULES.versions().maxNum(10)) - .withGranularity(TimestampGranularity.MILLIS) - .addSplit(ByteString.copyFromUtf8("b")) - .addSplit(ByteString.copyFromUtf8("q")); + TableAdminRequests.createTable(tableId) + .addColumnFamily("cf1") + .addColumnFamily("cf2", GCRULES.maxVersions(10)) + .withGranularity(TimestampGranularity.MILLIS) + .addSplit(ByteString.copyFromUtf8("b")) + .addSplit(ByteString.copyFromUtf8("q")); try { - Table table = tableAdmin.createTable(createTableReq); - assertNotNull(table); - assertTrue(table.getName().endsWith(tableId)); - assertEquals(2, table.getColumnFamiliesCount()); - assertFalse(table.getColumnFamiliesMap().get("cf1").hasGcRule()); - assertTrue(table.getColumnFamiliesMap().get("cf2").hasGcRule()); - assertEquals(10, table.getColumnFamiliesMap().get("cf2").getGcRule().getMaxNumVersions()); - assertEquals(TimestampGranularity.MILLIS, table.getGranularity()); + TableResponse tableResponse = tableAdmin.createTable(createTableReq); + assertNotNull(tableResponse); + assertEquals(tableId, tableResponse.getTableName().getTable()); + assertEquals(2, tableResponse.getColumnFamiles().size()); + assertFalse(tableResponse.getColumnFamiliesMap().get("cf1").hasGcRule()); + assertTrue(tableResponse.getColumnFamiliesMap().get("cf2").hasGcRule()); + assertEquals( + 10, tableResponse.getColumnFamiliesMap().get("cf2").getGcRule().getMaxNumVersions()); + assertEquals(TimestampGranularity.MILLIS, tableResponse.getTimestampGranularity()); // TODO: is there a way to test splits here? } finally { tableAdmin.deleteTable(tableId); @@ -59,31 +80,48 @@ public void createTable() throws Exception { public void modifyFamilies() { String tableId = "adminModifyFamTest"; ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId); - modifyFamiliesReq.create("mf1").createWithGCRule("mf2", GCRULES.age().seconds(1000).nanos(20000)) - .updateWithGCRule("mf1", - GCRULES.union().rule(GCRULES.age().seconds(100)) - .rule(GCRULES.versions().maxNum(1))) - .createWithGCRule("mf3", - GCRULES.intersection().rule(GCRULES.age().seconds(2000)) - .rule(GCRULES.versions().maxNum(10))) - .createWithGCRule("mf4", - GCRULES.intersection().rule(GCRULES.age().seconds(360))) - .create("mf5").create("mf6").drop("mf5").drop("mf6").create("mf7"); + Duration.ofSeconds(1000); + modifyFamiliesReq + .create("mf1") + .createWithGCRule( + "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000))) // .seconds(1000).nanos(20000) + .updateWithGCRule( + "mf1", + GCRULES + .union() + .rule(GCRULES.maxAge(Duration.ofSeconds(100))) + .rule(GCRULES.maxVersions(1))) + .createWithGCRule( + "mf3", + GCRULES + .intersection() + .rule(GCRULES.maxAge(Duration.ofSeconds(2000))) + .rule(GCRULES.maxVersions(10))) + .createWithGCRule( + "mf4", GCRULES.intersection().rule(GCRULES.maxAge(Duration.ofSeconds(360)))) + .create("mf5") + .create("mf6") + .drop("mf5") + .drop("mf6") + .create("mf7"); try { tableAdmin.createTable(TableAdminRequests.createTable(tableId)); - Table table = tableAdmin.modifyFamilies(modifyFamiliesReq); - assertEquals(5, table.getColumnFamiliesCount()); - assertNotNull(table.getColumnFamiliesOrThrow("mf1")); - assertNotNull(table.getColumnFamiliesOrThrow("mf2")); - assertTrue(table.getColumnFamiliesOrThrow("mf1").getGcRule().hasUnion()); - assertEquals(1000, - table.getColumnFamiliesOrThrow("mf2").getGcRule().getMaxAge().getSeconds()); - assertEquals(20000, table.getColumnFamiliesOrThrow("mf2").getGcRule().getMaxAge().getNanos()); - assertTrue(table.getColumnFamiliesOrThrow("mf3").getGcRule().hasIntersection()); - assertFalse(table.getColumnFamiliesOrThrow("mf4").getGcRule().hasIntersection()); - assertTrue(table.getColumnFamiliesOrThrow("mf4").getGcRule().hasMaxAge()); - assertNotNull(table.getColumnFamiliesOrThrow("mf7")); + TableResponse tableResponse = tableAdmin.modifyFamilies(modifyFamiliesReq); + assertEquals(5, tableResponse.getColumnFamiles().size()); + assertNotNull(tableResponse.getColumnFamiliesMap().get("mf1")); + assertNotNull(tableResponse.getColumnFamiliesMap().get("mf2")); + assertTrue(tableResponse.getColumnFamiliesMap().get("mf1").getGcRule().hasUnion()); + assertEquals( + 1000, + tableResponse.getColumnFamiliesMap().get("mf2").getGcRule().getMaxAge().getSeconds()); + assertEquals( + 20000, + tableResponse.getColumnFamiliesMap().get("mf2").getGcRule().getMaxAge().getNanos()); + assertTrue(tableResponse.getColumnFamiliesMap().get("mf3").getGcRule().hasIntersection()); + assertFalse(tableResponse.getColumnFamiliesMap().get("mf4").getGcRule().hasIntersection()); + assertTrue(tableResponse.getColumnFamiliesMap().get("mf4").getGcRule().hasMaxAge()); + assertNotNull(tableResponse.getColumnFamiliesMap().get("mf7")); } finally { tableAdmin.deleteTable(tableId); } @@ -99,12 +137,12 @@ public void deleteTable() { @Test public void getTable() { String tableId = "adminGetTest"; - + try { tableAdmin.createTable(TableAdminRequests.createTable(tableId)); - Table table = tableAdmin.getTable(tableId); - assertNotNull(table); - assertTrue(table.getName().endsWith(tableId)); + TableResponse tableResponse = tableAdmin.getTable(tableId); + assertNotNull(tableResponse); + assertEquals(tableId, tableResponse.getTableName().getTable()); } finally { tableAdmin.deleteTable(tableId); } @@ -123,7 +161,7 @@ public void listTables() { tableAdmin.deleteTable(tableId); } } - + @Test public void listTablesAsync() throws Exception { String tableId = "adminListTest"; @@ -137,26 +175,27 @@ public void listTablesAsync() throws Exception { tableAdmin.deleteTable(tableId); } } - + @Test public void dropRowRange() { String tableId = "adminDropRowrangeTest"; try { tableAdmin.createTable(TableAdminRequests.createTable(tableId)); - tableAdmin.dropRowRange(tableId, "*"); + tableAdmin.dropRowRange(tableId, "rowPrefix"); + tableAdmin.dropAllData(tableId); } finally { tableAdmin.deleteTable(tableId); } } - + @Test - public void CheckConsistency() { + public void checkConsistency() { String tableId = "adminConsistencyTest"; try { tableAdmin.createTable(TableAdminRequests.createTable(tableId)); - String token = tableAdmin.GenerateConsistencyToken(tableId); + String token = tableAdmin.generateConsistencyToken(tableId); boolean consistent = tableAdmin.isConsistent(tableId, token); assertNotNull(token); assertTrue(consistent); diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java index a61ffeaa41d7..9c800efe2028 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java @@ -18,101 +18,93 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import org.threeten.bp.Duration; import com.google.bigtable.admin.v2.GcRule; import com.google.bigtable.admin.v2.GcRule.Intersection; import com.google.bigtable.admin.v2.GcRule.Union; -import com.google.protobuf.Duration; @RunWith(JUnit4.class) public class GCRulesTest { @Test public void duration() { - GcRule actual = GCRULES.age().seconds(61).nanos(9).toProto(); - + GcRule actual = GCRULES.maxAge(Duration.ofSeconds(61, 9)).toProto(); GcRule expected = buildAgeRule(61, 9); - assertThat(actual).isEqualTo(expected); } @Test public void durationSeconds() { - GcRule actual = GCRULES.age().seconds(1).toProto(); - - GcRule expected = buildAgeRule(1, -1); - + GcRule actual = GCRULES.maxAge(Duration.ofSeconds(1)).toProto(); + GcRule expected = buildAgeRule(1, 0); assertThat(actual).isEqualTo(expected); } @Test public void durationNanos() { - GcRule actual = GCRULES.age().nanos(11).toProto(); - - GcRule expected = buildAgeRule(-1, 11); - - assertThat(actual).isEqualTo(expected); - } - - @Test - public void durationOverwrite() { - GcRule actual = GCRULES.age().seconds(61).nanos(9).seconds(1).nanos(5).seconds(4).toProto(); - - GcRule expected = buildAgeRule(4, 5); - + GcRule actual = GCRULES.maxAge(Duration.ofNanos(11)).toProto(); + GcRule expected = buildAgeRule(0, 11); assertThat(actual).isEqualTo(expected); } @Test public void versions() { - GcRule actual = GCRULES.versions().maxNum(10).toProto(); - + GcRule actual = GCRULES.maxVersions(10).toProto(); GcRule expected = buildVersionsRule(10); - assertThat(actual).isEqualTo(expected); } @Test public void unionEmpty() { GcRule actual = GCRULES.union().toProto(); - GcRule expected = GcRule.newBuilder().build(); - assertThat(actual).isEqualTo(expected); } @Test public void unionOne() { - GcRule actual = GCRULES.union().rule(GCRULES.versions().maxNum(1)).toProto(); - - GcRule expected = buildVersionsRule(1);; - + GcRule actual = GCRULES.union().rule(GCRULES.maxVersions(1)).toProto(); + GcRule expected = buildVersionsRule(1); + ; assertThat(actual).isEqualTo(expected); } @Test public void unionTwo() { GcRule actual = - GCRULES.union().rule(GCRULES.versions().maxNum(1)).rule(GCRULES.age().seconds(1)).toProto(); + GCRULES + .union() + .rule(GCRULES.maxVersions(1)) + .rule(GCRULES.maxAge(Duration.ofSeconds(1))) + .toProto(); GcRule expected = GcRule.newBuilder() - .setUnion( - Union.newBuilder().addRules(buildVersionsRule(1)).addRules(buildAgeRule(1, -1))) - .build(); + .setUnion( + Union.newBuilder().addRules(buildVersionsRule(1)).addRules(buildAgeRule(1, 0))) + .build(); assertThat(actual).isEqualTo(expected); } @Test public void unionThree() { - GcRule actual = GCRULES.union().rule(GCRULES.versions().maxNum(1)) - .rule(GCRULES.age().seconds(1)).rule(GCRULES.age().nanos(1)).toProto(); + GcRule actual = + GCRULES + .union() + .rule(GCRULES.maxVersions(1)) + .rule(GCRULES.maxAge(Duration.ofSeconds(1))) + .rule(GCRULES.maxAge(Duration.ofNanos(1))) + .toProto(); GcRule expected = GcRule.newBuilder() - .setUnion(Union.newBuilder().addRules(buildVersionsRule(1)) - .addRules(buildAgeRule(1, -1)).addRules(buildAgeRule(-1, 1))) - .build(); + .setUnion( + Union.newBuilder() + .addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, 0)) + .addRules(buildAgeRule(0, 1))) + .build(); assertThat(actual).isEqualTo(expected); } @@ -120,107 +112,146 @@ public void unionThree() { @Test public void intersectionEmpty() { GcRule actual = GCRULES.intersection().toProto(); - GcRule expected = GcRule.newBuilder().build(); - assertThat(actual).isEqualTo(expected); } @Test public void intersectionOne() { - GcRule actual = GCRULES.intersection().rule(GCRULES.versions().maxNum(1)).toProto(); - - GcRule expected = buildVersionsRule(1);; - + GcRule actual = GCRULES.intersection().rule(GCRULES.maxVersions(1)).toProto(); + GcRule expected = buildVersionsRule(1); + ; assertThat(actual).isEqualTo(expected); } @Test public void intersectionTwo() { - GcRule actual = GCRULES.intersection().rule(GCRULES.versions().maxNum(1)) - .rule(GCRULES.age().seconds(1)).toProto(); + GcRule actual = + GCRULES + .intersection() + .rule(GCRULES.maxVersions(1)) + .rule(GCRULES.maxAge(Duration.ofSeconds(1))) + .toProto(); - GcRule expected = GcRule.newBuilder().setIntersection( - Intersection.newBuilder().addRules(buildVersionsRule(1)).addRules(buildAgeRule(1, -1))) - .build(); + GcRule expected = + GcRule.newBuilder() + .setIntersection( + Intersection.newBuilder() + .addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, 0))) + .build(); assertThat(actual).isEqualTo(expected); } @Test public void intersectionThree() { - GcRule actual = GCRULES.intersection().rule(GCRULES.versions().maxNum(1)) - .rule(GCRULES.age().seconds(1)).rule(GCRULES.age().nanos(1)).toProto(); + GcRule actual = + GCRULES + .intersection() + .rule(GCRULES.maxVersions(1)) + .rule(GCRULES.maxAge(Duration.ofSeconds(1))) + .rule(GCRULES.maxAge(Duration.ofNanos(1))) + .toProto(); GcRule expected = GcRule.newBuilder() - .setIntersection( - Intersection.newBuilder().addRules(buildVersionsRule(1)) - .addRules(buildAgeRule(1, -1)).addRules(buildAgeRule(-1, 1))) - .build(); + .setIntersection( + Intersection.newBuilder() + .addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, 0)) + .addRules(buildAgeRule(0, 1))) + .build(); assertThat(actual).isEqualTo(expected); } @Test - public void UnionOfIntersections() { - GcRule actual = GCRULES.union() - .rule(GCRULES.intersection().rule(GCRULES.versions().maxNum(1)) - .rule(GCRULES.age().seconds(1))) - .rule(GCRULES.intersection().rule(GCRULES.versions().maxNum(1)) - .rule(GCRULES.age().seconds(1))) - .toProto(); + public void unionOfIntersections() { + GcRule actual = + GCRULES + .union() + .rule( + GCRULES + .intersection() + .rule(GCRULES.maxVersions(1)) + .rule(GCRULES.maxAge(Duration.ofSeconds(1)))) + .rule( + GCRULES + .intersection() + .rule(GCRULES.maxVersions(1)) + .rule(GCRULES.maxAge(Duration.ofSeconds(1)))) + .toProto(); GcRule expected = GcRule.newBuilder() - .setUnion(Union.newBuilder().addRules( - GcRule.newBuilder().setIntersection( - Intersection.newBuilder().addRules(buildVersionsRule(1)) - .addRules(buildAgeRule(1, -1)))) - .addRules(GcRule.newBuilder().setIntersection( - Intersection.newBuilder().addRules(buildVersionsRule(1)) - .addRules(buildAgeRule(1, -1))))) - .build(); + .setUnion( + Union.newBuilder() + .addRules( + GcRule.newBuilder() + .setIntersection( + Intersection.newBuilder() + .addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, 0)))) + .addRules( + GcRule.newBuilder() + .setIntersection( + Intersection.newBuilder() + .addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, 0))))) + .build(); + assertThat(actual).isEqualTo(expected); } @Test - public void IntersectionOfUnion() { - GcRule actual = GCRULES.intersection() - .rule(GCRULES.union().rule(GCRULES.versions().maxNum(1)) - .rule(GCRULES.age().seconds(1))) - .rule(GCRULES.union().rule(GCRULES.versions().maxNum(1)) - .rule(GCRULES.age().seconds(1))) - .toProto(); - - GcRule expected = GcRule.newBuilder().setIntersection( - Intersection.newBuilder() + public void intersectionOfUnions() { + GcRule actual = + GCRULES + .intersection() + .rule( + GCRULES + .union() + .rule(GCRULES.maxVersions(1)) + .rule(GCRULES.maxAge(Duration.ofSeconds(1)))) + .rule( + GCRULES + .union() + .rule(GCRULES.maxVersions(1)) + .rule(GCRULES.maxAge(Duration.ofSeconds(1)))) + .toProto(); + + GcRule expected = + GcRule.newBuilder() + .setIntersection( + Intersection.newBuilder() .addRules( GcRule.newBuilder() - .setUnion(Union.newBuilder().addRules(buildVersionsRule(1)) - .addRules(buildAgeRule(1, -1)))) - .addRules(GcRule.newBuilder() - .setUnion(Union.newBuilder().addRules(buildVersionsRule(1)) - .addRules(buildAgeRule(1, -1))))) - .build(); + .setUnion( + Union.newBuilder() + .addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, 0)))) + .addRules( + GcRule.newBuilder() + .setUnion( + Union.newBuilder() + .addRules(buildVersionsRule(1)) + .addRules(buildAgeRule(1, 0))))) + .build(); + assertThat(actual).isEqualTo(expected); } - private GcRule buildAgeRule(int seconds, int nanos) { - Duration.Builder duartionBuilder = Duration.newBuilder(); - - if (seconds > -1) { - duartionBuilder.setSeconds(seconds); - } - - if (nanos > -1) { - duartionBuilder.setNanos(nanos); - } + public static GcRule buildAgeRule(long seconds, int nanos) { + com.google.protobuf.Duration.Builder duartionBuilder = + com.google.protobuf.Duration.newBuilder(); + duartionBuilder.setSeconds(seconds); + duartionBuilder.setNanos(nanos); return GcRule.newBuilder().setMaxAge(duartionBuilder).build(); } - private GcRule buildVersionsRule(int maxVer) { + public static GcRule buildVersionsRule(int maxVer) { return GcRule.newBuilder().setMaxNumVersions(maxVer).build(); } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java index 787f53dc0915..40fdf5bbb826 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java @@ -1,3 +1,16 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ package com.google.cloud.bigtable.admin.v2.models; import static com.google.common.truth.Truth.assertThat; @@ -8,6 +21,7 @@ import com.google.bigtable.admin.v2.CreateTableRequest; import com.google.bigtable.admin.v2.Table; import com.google.bigtable.admin.v2.CreateTableRequest.Split; +import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; import com.google.bigtable.admin.v2.Table.TimestampGranularity; @@ -18,63 +32,88 @@ public class TableAdminRequestsTest { @Test public void createTable() { - CreateTableRequest actual = TableAdminRequests.createTable("tableId") - .withGranularity(TimestampGranularity.MILLIS) - .addColumnFamily("cf1") - .addColumnFamily("cf2", GCRules.GCRULES.versions().maxNum(1)) - .addSplit(ByteString.copyFromUtf8("c")).toProto("parent"); - + CreateTableRequest actual = + TableAdminRequests.createTable("tableId") + .withGranularity(TimestampGranularity.MILLIS) + .addColumnFamily("cf1") + .addColumnFamily("cf2", GCRules.GCRULES.maxVersions(1)) + .addSplit(ByteString.copyFromUtf8("c")) + .toProto(InstanceName.of("project", "instance")); + CreateTableRequest expected = - CreateTableRequest.newBuilder().setTableId("tableId").setParent("parent") - .addInitialSplits(Split.newBuilder().setKey(ByteString.copyFromUtf8("c"))) - .setTable( - Table.newBuilder().setGranularity(TimestampGranularity.MILLIS) - .putColumnFamilies("cf1", ColumnFamily.newBuilder().build()) - .putColumnFamilies("cf2", ColumnFamily.newBuilder().setGcRule(GCRules.GCRULES.versions().maxNum(1).toProto()).build())) + CreateTableRequest.newBuilder() + .setTableId("tableId") + .setParent(InstanceName.of("project", "instance").getInstance()) + .addInitialSplits(Split.newBuilder().setKey(ByteString.copyFromUtf8("c"))) + .setTable( + Table.newBuilder() + .setGranularity(TimestampGranularity.MILLIS) + .putColumnFamilies("cf1", ColumnFamily.newBuilder().build()) + .putColumnFamilies( + "cf2", + ColumnFamily.newBuilder() + .setGcRule(GCRules.GCRULES.maxVersions(1).toProto()) + .build())) .build(); - + assertThat(actual).isEqualTo(expected); } - - @Test (expected = NullPointerException.class) + + @Test(expected = NullPointerException.class) public void createTable_RequiredTableId() { - TableAdminRequests.createTable(null).toProto("parent"); + TableAdminRequests.createTable(null).toProto(InstanceName.of("project", "instance")); } - @Test (expected = NullPointerException.class) + @Test(expected = NullPointerException.class) public void createTable_RequiredParent() { TableAdminRequests.createTable("tableId").toProto(null); } - - @Test + + @Test public void modifyFamilies() { - ModifyColumnFamiliesRequest actual = TableAdminRequests.modifyFamilies("tableId") - .create("cf1") - .createWithGCRule("cf2", GCRules.GCRULES.versions().maxNum(1)) - .create("cf3") - .update("cf2") - .updateWithGCRule("cf1", GCRules.GCRULES.versions().maxNum(5)) - .drop("cf3") - .toProto("uniqueTableName"); - - ModifyColumnFamiliesRequest expected = ModifyColumnFamiliesRequest.newBuilder() - .setName("uniqueTableName") - .addModifications(Modification.newBuilder().setId("cf1").setCreate(ColumnFamily.newBuilder())) - .addModifications(Modification.newBuilder().setId("cf2").setCreate(ColumnFamily.newBuilder().setGcRule(GCRules.GCRULES.versions().maxNum(1).toProto()))) - .addModifications(Modification.newBuilder().setId("cf3").setCreate(ColumnFamily.newBuilder())) - .addModifications(Modification.newBuilder().setId("cf2").setUpdate(ColumnFamily.newBuilder())) - .addModifications(Modification.newBuilder().setId("cf1").setUpdate(ColumnFamily.newBuilder().setGcRule(GCRules.GCRULES.versions().maxNum(5).toProto()))) - .addModifications(Modification.newBuilder().setId("cf3").setDrop(true)) - .build(); + ModifyColumnFamiliesRequest actual = + TableAdminRequests.modifyFamilies("tableId") + .create("cf1") + .createWithGCRule("cf2", GCRules.GCRULES.maxVersions(1)) + .create("cf3") + .updateWithGCRule("cf1", GCRules.GCRULES.maxVersions(5)) + .drop("cf3") + .toProto("uniqueTableName"); + + ModifyColumnFamiliesRequest expected = + ModifyColumnFamiliesRequest.newBuilder() + .setName("uniqueTableName") + .addModifications( + Modification.newBuilder().setId("cf1").setCreate(ColumnFamily.newBuilder())) + .addModifications( + Modification.newBuilder() + .setId("cf2") + .setCreate( + ColumnFamily.newBuilder() + .setGcRule(GCRules.GCRULES.maxVersions(1).toProto()))) + .addModifications( + Modification.newBuilder().setId("cf3").setCreate(ColumnFamily.newBuilder())) + /* + * .addModifications( Modification.newBuilder().setId("cf2") + * .setUpdate(ColumnFamily.newBuilder())) + */ + .addModifications( + Modification.newBuilder() + .setId("cf1") + .setUpdate( + ColumnFamily.newBuilder() + .setGcRule(GCRules.GCRULES.maxVersions(5).toProto()))) + .addModifications(Modification.newBuilder().setId("cf3").setDrop(true)) + .build(); assertThat(actual).isEqualTo(expected); } - - @Test (expected = NullPointerException.class) + + @Test(expected = NullPointerException.class) public void modifyFamilies_RequiredTableId() { TableAdminRequests.modifyFamilies(null).toProto("uniqueTableName"); } - @Test (expected = NullPointerException.class) + @Test(expected = NullPointerException.class) public void modifyFamilies_RequiredParent() { TableAdminRequests.modifyFamilies("tableId").toProto(null); } diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java new file mode 100644 index 000000000000..6a67b491e88b --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java @@ -0,0 +1,197 @@ +package com.google.cloud.bigtable.admin.v2.models; + +import static org.junit.Assert.*; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.threeten.bp.Duration; +import com.google.bigtable.admin.v2.ColumnFamily; +import com.google.bigtable.admin.v2.GcRule; +import com.google.bigtable.admin.v2.Table; +import com.google.bigtable.admin.v2.Table.ClusterState; +import com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState; +import com.google.bigtable.admin.v2.Table.TimestampGranularity; +import com.google.bigtable.admin.v2.TableName; +import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.ConsistencyTokenResponse; +import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.TableResponse; +import com.google.bigtable.admin.v2.GcRule.Intersection; +import com.google.bigtable.admin.v2.GcRule.Union; +import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; +import static com.google.cloud.bigtable.admin.v2.models.GCRules.GCRULES; +import static com.google.common.truth.Truth.assertThat; + +@RunWith(JUnit4.class) +public class TableAdminResponsesTest { + + @Test + public void convertTable() { + TableName testName = TableName.of("p", "i", "testTable"); + Table table = + Table.newBuilder() + .setName(testName.toString()) + .setGranularity(TimestampGranularity.MILLIS) + .putClusterStates( + "test", + ClusterState.newBuilder().setReplicationState(ReplicationState.READY).build()) + .putClusterStates( + "prod", + ClusterState.newBuilder() + .setReplicationState(ReplicationState.INITIALIZING) + .build()) + .putColumnFamilies("cf1", ColumnFamily.newBuilder().build()) + .putColumnFamilies( + "cf2", + ColumnFamily.newBuilder() + .setGcRule(GcRule.newBuilder().setMaxNumVersions(1)) + .build()) + .putColumnFamilies( + "cf3", + ColumnFamily.newBuilder() + .setGcRule( + GcRule.newBuilder() + .setMaxAge( + com.google.protobuf.Duration.newBuilder() + .setSeconds(1) + .setNanos(99))) + .build()) + .build(); + + TableResponse tableResponse = TableAdminResponses.convertTable(table); + assertNotNull(tableResponse); + assertEquals(testName, tableResponse.getTableName()); + assertEquals(TimestampGranularity.MILLIS, tableResponse.getTimestampGranularity()); + assertEquals(2, tableResponse.getClusterStates().size()); + assertEquals( + ReplicationState.READY, + tableResponse.getClusterStatesMap().get("test").getReplicationState()); + assertEquals( + ReplicationState.INITIALIZING, + tableResponse.getClusterStatesMap().get("prod").getReplicationState()); + assertEquals(3, tableResponse.getColumnFamiles().size()); + assertNotNull("cf1", tableResponse.getColumnFamiliesMap().get("cf1").getId()); + assertFalse(tableResponse.getColumnFamiliesMap().get("cf1").hasGcRule()); + + assertThat(GCRULES.defaulRule().toProto()) + .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf1").getGcRule()); + assertThat(GCRULES.maxVersions(1).toProto()) + .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf2").getGcRule()); + assertThat(GCRULES.maxAge(Duration.ofSeconds(1, 99)).toProto()) + .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGcRule()); + } + + @Test + public void convertTable_unionOfIntersections() { + GcRule expected = + GcRule.newBuilder() + .setUnion( + Union.newBuilder() + .addRules( + GcRule.newBuilder() + .setIntersection( + Intersection.newBuilder() + .addRules(GCRulesTest.buildVersionsRule(1)) + .addRules(GCRulesTest.buildAgeRule(1, 0)))) + .addRules( + GcRule.newBuilder() + .setIntersection( + Intersection.newBuilder() + .addRules(GCRulesTest.buildVersionsRule(1)) + .addRules(GCRulesTest.buildAgeRule(1, 0))))) + .build(); + + GcRule actual = + GCRULES + .union() + .rule( + GCRULES + .intersection() + .rule(GCRULES.maxVersions(1)) + .rule(GCRULES.maxAge(Duration.ofSeconds(1)))) + .rule( + GCRULES + .intersection() + .rule(GCRULES.maxVersions(1)) + .rule(GCRULES.maxAge(Duration.ofSeconds(1)))) + .toProto(); + + Table table = + Table.newBuilder() + .putColumnFamilies("cf3", ColumnFamily.newBuilder().setGcRule(expected).build()) + .build(); + + TableResponse tableResponse = TableAdminResponses.convertTable(table); + assertThat(actual).isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGcRule()); + assertTrue(tableResponse.getColumnFamiliesMap().get("cf3").hasGcRule()); + assertTrue(tableResponse.getColumnFamiliesMap().get("cf3").hasUnion()); + assertFalse(tableResponse.getColumnFamiliesMap().get("cf3").hasIntersection()); + assertFalse(tableResponse.getColumnFamiliesMap().get("cf3").hasMaxAge()); + } + + @Test + public void convertTable_intersectionOfUnions() { + GcRule actual = + GcRule.newBuilder() + .setIntersection( + Intersection.newBuilder() + .addRules( + GcRule.newBuilder() + .setUnion( + Union.newBuilder() + .addRules(GCRulesTest.buildVersionsRule(1)) + .addRules(GCRulesTest.buildAgeRule(1, 0)))) + .addRules( + GcRule.newBuilder() + .setUnion( + Union.newBuilder() + .addRules(GCRulesTest.buildVersionsRule(1)) + .addRules(GCRulesTest.buildAgeRule(1, 0))))) + .build(); + + GcRule expected = + GCRULES + .intersection() + .rule( + GCRULES + .union() + .rule(GCRULES.maxVersions(1)) + .rule(GCRULES.maxAge(Duration.ofSeconds(1)))) + .rule( + GCRULES + .union() + .rule(GCRULES.maxVersions(1)) + .rule(GCRULES.maxAge(Duration.ofSeconds(1)))) + .toProto(); + + Table table = + Table.newBuilder() + .putColumnFamilies("cf3", ColumnFamily.newBuilder().setGcRule(expected).build()) + .build(); + + TableResponse tableResponse = TableAdminResponses.convertTable(table); + assertThat(actual).isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGcRule()); + } + + @Test + public void convertTable_Empty() { + TableResponse tableResponse = TableAdminResponses.convertTable(Table.newBuilder().build()); + + assertNotNull(tableResponse); + assertEquals( + TimestampGranularity.TIMESTAMP_GRANULARITY_UNSPECIFIED, + tableResponse.getTimestampGranularity()); + assertEquals(0, tableResponse.getClusterStates().size()); + assertEquals(0, tableResponse.getColumnFamiles().size()); + } + + @Test + public void convertTokenResponse() { + ConsistencyTokenResponse tokenResponse = + TableAdminResponses.convertTokenResponse( + GenerateConsistencyTokenResponse.newBuilder() + .setConsistencyToken("87282hgwd8yg") + .build()); + + assertNotNull(tokenResponse); + assertEquals("87282hgwd8yg", tokenResponse.getToken()); + } +} From cb17050eadba741972363da69c0072efacf44a72 Mon Sep 17 00:00:00 2001 From: spollapally Date: Tue, 19 Jun 2018 15:32:45 -0400 Subject: [PATCH 03/10] Added documentation --- .../bigtable/admin/v2/TableAdminClient.java | 608 +++++++++++++++--- .../bigtable/admin/v2/models/GCRules.java | 220 ++++++- .../admin/v2/models/TableAdminRequests.java | 115 +++- .../admin/v2/models/TableAdminResponses.java | 177 +++-- .../admin/v2/TableAdminClientTest.java | 50 +- .../admin/v2/it/TableAdminClientIT.java | 30 +- .../v2/models/TableAdminRequestsTest.java | 31 +- .../v2/models/TableAdminResponsesTest.java | 68 +- 8 files changed, 1056 insertions(+), 243 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java index 87c51bb138a7..a812b63202c1 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java @@ -19,7 +19,7 @@ import com.google.api.core.ApiFunction; import com.google.api.core.ApiFuture; import com.google.api.core.ApiFutures; -import com.google.bigtable.admin.v2.CheckConsistencyRequest; +import com.google.api.core.BetaApi; import com.google.bigtable.admin.v2.CheckConsistencyResponse; import com.google.bigtable.admin.v2.DeleteTableRequest; import com.google.bigtable.admin.v2.DropRowRangeRequest; @@ -31,12 +31,13 @@ import com.google.bigtable.admin.v2.ListTablesRequest; import com.google.bigtable.admin.v2.ListTablesResponse; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; -import com.google.bigtable.admin.v2.Table; import com.google.bigtable.admin.v2.TableName; +import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.CreateTable; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.ModifyFamilies; import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses; -import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.TableResponse; +import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.ConsistencyToken; +import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.Table; import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStub; import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStubSettings; import com.google.common.annotations.VisibleForTesting; @@ -44,20 +45,86 @@ import com.google.protobuf.ByteString; import com.google.protobuf.Empty; -/** Table admin client for creating and configuring Bigtable tables. */ +/** + * Client for creating, configuring, and deleting Cloud Bigtable tables + * + *

Provides access to the table schemas only, not the data stored within the tables. + * + *

See the individual methods for example code. + * + *

Sample code to get started: + *


+ *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+ *     CreateTable createTableReq =
+ *         TableAdminRequests.createTable("tableId")
+ *             .addColumnFamily("cf1")
+ *             .addColumnFamily("cf2", GCRULES.maxVersions(10))
+ *             .withGranularity(TimestampGranularity.MILLIS)
+ *             .addSplit(ByteString.copyFromUtf8("b"))
+ *             .addSplit(ByteString.copyFromUtf8("q"));
+ *     client.createTable(createTableReq);
+ *   }
+ * 
+ * + *

Note: close() needs to be called on the bigtableTableAdminClient object to clean up resources + * such as threads. In the example above, try-with-resources is used, which automatically calls + * close(). + * + *

This class can be customized by passing in a custom instance of BigtableTableAdminSettings to + * create(). For example: + * + *

To customize credentials: + *


+ * BigtableTableAdminSettings bigtableTableAdminSettings =
+ *     BigtableTableAdminSettings.newBuilder()
+ *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
+ *         .build();
+ * TableAdminClient client = 
+ * TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings);
+ * 
+ * + * To customize the endpoint: + *

+ * BigtableTableAdminSettings bigtableTableAdminSettings =
+ *     BigtableTableAdminSettings.newBuilder().setEndpoint(myEndpoint).build();
+ * TableAdminClient client = 
+ * TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings);
+ * 
+ **/ +@BetaApi public class TableAdminClient implements AutoCloseable { private final BigtableTableAdminStub stub; private final InstanceName instanceName; + /** + * Constructs an instance of TableAdminClient with the given instanceName + * + * @param instanceName + * @throws IOException + */ public static TableAdminClient create(InstanceName instanceName) throws IOException { return new TableAdminClient(instanceName, BigtableTableAdminSettings.newBuilder().build()); } - public static TableAdminClient create( - InstanceName instanceName, BigtableTableAdminSettings adminSettings) throws IOException { + /** + * Constructs an instance of TableAdminClient with the given instanceName and bigtableTableAdminSettings + * + * @param instanceName + * @param adminSettings + * @throws IOException + */ + public static TableAdminClient create(InstanceName instanceName, + BigtableTableAdminSettings adminSettings) throws IOException { return new TableAdminClient(instanceName, adminSettings); } + /** + * Constructs an instance of TableAdminClient with the given instanceName and bigtableTableAdminStub + * + * @param instanceName + * @param stub + * @throws IOException + */ public static TableAdminClient create(InstanceName instanceName, BigtableTableAdminStub stub) throws IOException { return new TableAdminClient(instanceName, stub); @@ -65,9 +132,8 @@ public static TableAdminClient create(InstanceName instanceName, BigtableTableAd private TableAdminClient(InstanceName instanceName, BigtableTableAdminSettings adminSettings) throws IOException { - this( - instanceName, - ((BigtableTableAdminStubSettings) adminSettings.getStubSettings()).createStub()); + this(instanceName, + ((BigtableTableAdminStubSettings) adminSettings.getStubSettings()).createStub()); } private TableAdminClient(InstanceName instanceName, BigtableTableAdminStub stub) @@ -78,141 +144,445 @@ private TableAdminClient(InstanceName instanceName, BigtableTableAdminStub stub) this.stub = stub; } + /** + * Gets the instanceName this client is associated to + * @return InstanceName + */ public InstanceName getInstanceName() { return instanceName; } @Override - public void close() throws Exception { + public void close() { stub.close(); } - public TableResponse createTable(CreateTable createTable) { - Table table = this.stub.createTableCallable().call(createTable.toProto(instanceName)); + /** + * Creates a new table with the specified configuration + * See {@link TableAdminRequests.createTable} for possible configurations + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *     CreateTable createTableReq =
+   *         TableAdminRequests.createTable("tableId")
+   *             .addColumnFamily("cf2", GCRULES.maxVersions(10))
+   *             .withGranularity(TimestampGranularity.MILLIS);
+   *             
+   *     client.createTable(createTableReq);
+   *   }
+   * 
+ * + * @param createTable + * @return Table - the newly created table + */ + public Table createTable(CreateTable createTable) { + com.google.bigtable.admin.v2.Table table = + this.stub.createTableCallable().call(createTable.toProto(instanceName)); return TableAdminResponses.convertTable(table); } - public ApiFuture createTableAsync(CreateTable createTable) { + /** + * Creates a new table with the specified configuration asynchronously + * See {@link TableAdminRequests#createTable(String)} + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *     CreateTable createTableReq =
+   *         TableAdminRequests.createTable("tableId")
+   *             .addColumnFamily("cf2", GCRULES.maxVersions(10))
+   *             .withGranularity(TimestampGranularity.MILLIS);
+   *             
+   *     client.createTableAsync(createTableReq);
+   *   }
+   * 
+ * + * @param createTable + * @return ApiFuture
- the newly created table + */ + public ApiFuture
createTableAsync(CreateTable createTable) { return transformToTableResponse( this.stub.createTableCallable().futureCall(createTable.toProto(instanceName))); } - public TableResponse modifyFamilies(ModifyFamilies modifyFamily) { + /** + * Creates, Updates and drops ColumnFamilies as per the request + * See {@link TableAdminRequests#modifyFamilies(String)} + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *     ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId)
+   *             .create("mf1")
+   *       .createWithGCRule(
+   *         "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000)))
+   *     .updateWithGCRule(
+   *         "mf1",
+   *         GCRULES
+   *             .union()
+   *             .rule(GCRULES.maxAge(Duration.ofSeconds(100)))
+   *             .rule(GCRULES.maxVersions(1)))
+   *             .createWithGCRule(
+   *         "mf3",
+   *         GCRULES
+   *             .intersection()
+   *             .rule(GCRULES.maxAge(Duration.ofSeconds(2000)))
+   *             .rule(GCRULES.maxVersions(10)))
+   *             .drop("mf1")
+   *     client.modifyFamilies(modifyFamiliesReq);
+   *   }
+   * 
+ * + * @param modifyFamily + * @return Table - Modified table + */ + public Table modifyFamilies(ModifyFamilies modifyFamily) { ModifyColumnFamiliesRequest modReq = - modifyFamily.toProto(getTableName(modifyFamily.getTableId())); - Table table = this.stub.modifyColumnFamiliesCallable().call(modReq); + modifyFamily.toProto(instanceName); + com.google.bigtable.admin.v2.Table table = + this.stub.modifyColumnFamiliesCallable().call(modReq); return TableAdminResponses.convertTable(table); } - public ApiFuture modifyFamiliesAsync(ModifyFamilies modifyFamily) { + /** + * Creates, Updates and drops ColumnFamilies as per the request asynchronously + * See {@link TableAdminRequests#modifyFamilies(String)} + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *     ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId)
+   *             .create("mf1")
+   *       .createWithGCRule(
+   *         "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000)))
+   *     .updateWithGCRule(
+   *         "mf1",
+   *         GCRULES
+   *             .union()
+   *             .rule(GCRULES.maxAge(Duration.ofSeconds(100)))
+   *             .rule(GCRULES.maxVersions(1)))
+   *             .createWithGCRule(
+   *         "mf3",
+   *         GCRULES
+   *             .intersection()
+   *             .rule(GCRULES.maxAge(Duration.ofSeconds(2000)))
+   *             .rule(GCRULES.maxVersions(10)))
+   *             .drop("mf1")
+   *     client.modifyFamiliesAsync(modifyFamiliesReq);
+   *   }
+   * 
+ * + * @param modifyFamily + * @return Table - Modified table + */ + public ApiFuture
modifyFamiliesAsync(ModifyFamilies modifyFamily) { ModifyColumnFamiliesRequest modReq = - modifyFamily.toProto(getTableName(modifyFamily.getTableId())); + modifyFamily.toProto(instanceName); return transformToTableResponse(this.stub.modifyColumnFamiliesCallable().futureCall(modReq)); } + /** + * Deletes the specified tableId + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.deleteTable("tableId");
+   *   }
+   * 
+ * + * @param tableId + */ public void deleteTable(String tableId) { this.stub.deleteTableCallable().call(composeDeleteTableRequest(tableId)); } + /** + * Deletes the specified tableId asynchronously + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.deleteTableAsync("tableId");
+   *   }
+   * 
+ * + * @param tableId + */ public ApiFuture deleteTableAsync(String tableId) { return transformToVoid( this.stub.deleteTableCallable().futureCall(composeDeleteTableRequest(tableId))); } - public TableResponse getTable(String tableId) { - Table table = this.stub.getTableCallable().call(composeGetTableRequest(tableId)); + /** + * Gets the Table by tableId + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.getTable("tableId");
+   *   }
+   * 
+ * + * @param tableId + * @return Table + */ + public Table getTable(String tableId) { + com.google.bigtable.admin.v2.Table table = + this.stub.getTableCallable().call(composeGetTableRequest(tableId)); return TableAdminResponses.convertTable(table); } - public ApiFuture getTableAsync(String tableId) { + /** + * Gets the Table by tableId + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.getTableAsync("tableId");
+   *   }
+   * 
+ * + * @param tableId + * @return ApiFuture
+ */ + public ApiFuture
getTableAsync(String tableId) { return transformToTableResponse( this.stub.getTableCallable().futureCall(composeGetTableRequest(tableId))); } + /** + * Lists all TableNames in the instance. + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.listTables();
+   *   }
+   * 
+ * + * @param tableId + * @return List + */ public List listTables() { ListTablesResponse listResp = this.stub.listTablesCallable().call(composeListTableRequest()); return convertToTableNames(listResp); } + /** + * Lists all TableNames in the instance asynchronously + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.listTablesAsync();
+   *   }
+   * 
+ * + * @param tableId + * @return List + */ public ApiFuture> listTablesAsync() { ApiFuture listResp = this.stub.listTablesCallable().futureCall(composeListTableRequest()); - return ApiFutures.transform( - listResp, - new ApiFunction>() { - @Override - public List apply(ListTablesResponse input) { - return convertToTableNames(input); - } - }); - } - + return ApiFutures.transform(listResp, new ApiFunction>() { + @Override + public List apply(ListTablesResponse input) { + return convertToTableNames(input); + } + }); + } + + /** + * Drops rows by the specified key prefix and tableId + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.dropRowRange("tableId");
+   *   }
+   * 
+ * + * @param tableId + * @param rowKeyPrefix + */ public void dropRowRange(String tableId, String rowKeyPrefix) { dropRowRange(tableId, ByteString.copyFromUtf8(rowKeyPrefix)); } + /** + * Drops rows by the specified key prefix and tableId asynchronously + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.dropRowRangeAsync("tableId");
+   *   }
+   * 
+ * + * @param tableId + * @param rowKeyPrefix + */ public ApiFuture dropRowRangeAsync(String tableId, String rowKeyPrefix) { return dropRowRangeAsync(tableId, ByteString.copyFromUtf8(rowKeyPrefix)); } - public void dropAllData(String tableId) { - this.stub.dropRowRangeCallable().call(composeDropRowRangeRequest(tableId, null, true)); - } - - public ApiFuture dropAllDataAsync(String tableId) { - return transformToVoid( - this.stub - .dropRowRangeCallable() - .futureCall(composeDropRowRangeRequest(tableId, null, true))); - } - + /** + * Drops rows by the specified key prefix and tableId + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.dropRowRange("tableId");
+   *   }
+   * 
+ * + * @param tableId + * @param rowKeyPrefix + */ public void dropRowRange(String tableId, ByteString rowKeyPrefix) { this.stub.dropRowRangeCallable().call(composeDropRowRangeRequest(tableId, rowKeyPrefix, false)); } + /** + * Drops rows by the specified key prefix and tableId + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.dropRowRangeAsync("tableId");
+   *   }
+   * 
+ * + * @param tableId + * @param rowKeyPrefix + */ public ApiFuture dropRowRangeAsync(String tableId, ByteString rowKeyPrefix) { - return transformToVoid( - this.stub - .dropRowRangeCallable() - .futureCall(composeDropRowRangeRequest(tableId, rowKeyPrefix, false))); - } - - public String generateConsistencyToken(String tableId) { - return this.stub - .generateConsistencyTokenCallable() - .call(composeGenerateConsistencyTokenRequest(tableId)) - .getConsistencyToken(); + return transformToVoid(this.stub.dropRowRangeCallable().futureCall( + composeDropRowRangeRequest(tableId, rowKeyPrefix, false))); + } + + /** + * Drops all data in the table + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.dropAllData("tableId");
+   *   }
+   * 
+ * + * @param tableId + */ + public void dropAllData(String tableId) { + this.stub.dropRowRangeCallable().call(composeDropRowRangeRequest(tableId, null, true)); } - public ApiFuture generateConsistencyTokenAsync(String tableId) { + /** + * Drops all data in the table asynchronously + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.dropAllData("tableId");
+   *   }
+   * 
+ * + * @param tableId + * @return ApiFuture + */ + public ApiFuture dropAllDataAsync(String tableId) { + return transformToVoid(this.stub.dropRowRangeCallable() + .futureCall(composeDropRowRangeRequest(tableId, null, true))); + } + + /** + * Generates a token to verify the replication status of table mutations invoked before this call. + * Token expires in 90 days + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.generateConsistencyToken("tableId");
+   *   }
+   * 
+ * + * @param tableId + * @return ConsistencyToken + */ + public ConsistencyToken generateConsistencyToken(String tableId) { + return TableAdminResponses.convertTokenResponse(this.stub.generateConsistencyTokenCallable() + .call(composeGenerateConsistencyTokenRequest(tableId))); + } + + /** + * Generates a token to verify the replication status of table mutations invoked before this call asynchronously + * Token expires in 90 days + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.generateConsistencyToken("tableId");
+   *   }
+   * 
+ * + * @param tableId + * @return ApiFuture + */ + public ApiFuture generateConsistencyTokenAsync(String tableId) { ApiFuture tokenResp = - this.stub - .generateConsistencyTokenCallable() - .futureCall(composeGenerateConsistencyTokenRequest(tableId)); + this.stub.generateConsistencyTokenCallable() + .futureCall(composeGenerateConsistencyTokenRequest(tableId)); - return ApiFutures.transform( - tokenResp, - new ApiFunction() { + return ApiFutures.transform(tokenResp, + new ApiFunction() { @Override - public String apply(GenerateConsistencyTokenResponse input) { - return input.getConsistencyToken(); + public ConsistencyToken apply(GenerateConsistencyTokenResponse input) { + return TableAdminResponses.convertTokenResponse(input); } }); } - public boolean isConsistent(String tableId, String token) { - return stub.checkConsistencyCallable() - .call(composeCheckConsistencyRequest(tableId, token)) - .getConsistent(); - } - - public ApiFuture isConsistentAsync(String tableId, String token) { + /** + * Checks replication consistency for the specified token consistency token + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.isConsistent("tableId", token);
+   *   }
+   * 
+ * + * @param tableId + * @param token + * @return boolean + */ + public boolean isConsistent(String tableId, ConsistencyToken token) { + return stub.checkConsistencyCallable().call(token.toProto(getTableName(tableId))) + .getConsistent(); + } + + /** + * Checks replication consistency for the specified token consistency token asynchronously + * + *

Sample code: + *

 
+   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *  client.isConsistentAsync("tableId", token);
+   *   }
+   * 
+ * + * @param tableId + * @param token + * @return ApiFuture + */ + public ApiFuture isConsistentAsync(String tableId, ConsistencyToken token) { ApiFuture CheckConsResp = - stub.checkConsistencyCallable().futureCall(composeCheckConsistencyRequest(tableId, token)); + stub.checkConsistencyCallable().futureCall(token.toProto(getTableName(tableId))); - return ApiFutures.transform( - CheckConsResp, + return ApiFutures.transform(CheckConsResp, new ApiFunction() { @Override public Boolean apply(CheckConsistencyResponse input) { @@ -221,29 +591,61 @@ public Boolean apply(CheckConsistencyResponse input) { }); } + /** + * Helper method to construct the table name in format: + * projects/{project}/instances/{instance}/tables/{tableId} + * + * @param tableId + * @return String - unique table name + */ @VisibleForTesting String getTableName(String tableId) { return TableName.of(instanceName.getProject(), instanceName.getInstance(), tableId).toString(); } + /** + * Helper method to build an instance of ListTablesRequest + * + * @return ListTablesRequest + */ @VisibleForTesting ListTablesRequest composeListTableRequest() { return ListTablesRequest.newBuilder().setParent(instanceName.toString()).build(); } + /** + * Helper method to build an instance of GetTableRequest + * + * @param tableId + * @return GetTableRequest + */ @VisibleForTesting GetTableRequest composeGetTableRequest(String tableId) { return GetTableRequest.newBuilder().setName(getTableName(tableId)).build(); } + /** + * Helper method to build an instance of DeleteTableRequest + * + * @param tableId + * @return DeleteTableRequest + */ @VisibleForTesting DeleteTableRequest composeDeleteTableRequest(String tableId) { return DeleteTableRequest.newBuilder().setName(getTableName(tableId)).build(); } + /** + * Helper method to build an instance of DropRowRangeRequest + * + * @param tableId + * @param rowKeyPrefix + * @param boolean dropAll + * @return DropRowRangeRequest + */ @VisibleForTesting - DropRowRangeRequest composeDropRowRangeRequest( - String tableId, ByteString rowKeyPrefix, boolean dropAll) { + DropRowRangeRequest composeDropRowRangeRequest(String tableId, ByteString rowKeyPrefix, + boolean dropAll) { Builder dropRowReq = DropRowRangeRequest.newBuilder().setName(getTableName(tableId)); if (dropAll) { @@ -254,50 +656,64 @@ DropRowRangeRequest composeDropRowRangeRequest( return dropRowReq.build(); } + /** + * Helper method to build an instance of GenerateConsistencyTokenRequest + * + * @param tableId + * @return GenerateConsistencyTokenRequest + */ @VisibleForTesting GenerateConsistencyTokenRequest composeGenerateConsistencyTokenRequest(String tableId) { return GenerateConsistencyTokenRequest.newBuilder().setName(getTableName(tableId)).build(); } - @VisibleForTesting - CheckConsistencyRequest composeCheckConsistencyRequest(String tableId, String token) { - return CheckConsistencyRequest.newBuilder() - .setName(getTableName(tableId)) - .setConsistencyToken(token) - .build(); - } - + /** + * Helper method to convert ListTablesResponse to List + * + * @param listTablesResponse + * @return List + */ @VisibleForTesting static List convertToTableNames(ListTablesResponse listTablesResponse) { List tableNames = new ArrayList<>(); - for (Table table : listTablesResponse.getTablesList()) { + for (com.google.bigtable.admin.v2.Table table : listTablesResponse.getTablesList()) { tableNames.add(TableName.parse(table.getName())); } return tableNames; } + /** + * Helper method to transform ApiFuture to ApiFuture
+ * + * @param future + * @return ApiFuture
+ */ @VisibleForTesting - static ApiFuture transformToTableResponse(ApiFuture
future) { - return ApiFutures.transform( - future, - new ApiFunction() { + static ApiFuture
transformToTableResponse( + ApiFuture future) { + return ApiFutures.transform(future, + new ApiFunction() { @Override - public TableResponse apply(Table table) { + public Table apply(com.google.bigtable.admin.v2.Table table) { return TableAdminResponses.convertTable(table); } }); } + /** + * Helper method to transform ApiFuture to ApiFuture + * + * @param future + * @return ApiFuture + */ @VisibleForTesting static ApiFuture transformToVoid(ApiFuture future) { - return ApiFutures.transform( - future, - new ApiFunction() { - @Override - public Void apply(Empty empty) { - return null; - } - }); + return ApiFutures.transform(future, new ApiFunction() { + @Override + public Void apply(Empty empty) { + return null; + } + }); } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java index f7a146dd9772..d9158ee3f6b9 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java @@ -13,54 +13,129 @@ */ package com.google.cloud.bigtable.admin.v2.models; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.TimeUnit; import javax.annotation.Nonnull; import org.threeten.bp.Duration; +import com.google.api.core.BetaApi; +import com.google.api.core.InternalApi; import com.google.bigtable.admin.v2.GcRule; +import com.google.bigtable.admin.v2.GcRule.Intersection; +import com.google.bigtable.admin.v2.GcRule.Union; +import com.google.common.base.MoreObjects; +/** + * Wraps {@link GcRule} protocol buffer object and exposes a simpler Fluent DSL model + */ +@BetaApi public final class GCRules { + /** Factory method to create GCRules - entry point into the DSL. */ public static final GCRules GCRULES = new GCRules(); private GCRules() {} + /** + * Creates a new instance of the IntersectionRule + * + * @return IntersectionRule + */ public IntersectionRule intersection() { return new IntersectionRule(); } + /** + * Creates a new instance of the IntersectionRule + * + * @return UnionRule + */ public UnionRule union() { return new UnionRule(); } + /** + * Creates a new instance of the VersionRule + * + * @param maxVersion - maximum number of cell versions to keep + * @return VersionRule + */ public VersionRule maxVersions(int maxVersion) { return new VersionRule(maxVersion); } + /** + * Creates a new instance of the DurationRule + * + * @param maxAge - maximum age of the cell to keep + * @param timeUnit - timeunit for the age + * @return DurationRule + */ public DurationRule maxAge(long maxAge, TimeUnit timeUnit) { Duration duration = Duration.ZERO; TimeUnit.SECONDS.convert(maxAge, timeUnit); return maxAge(duration); } + /** + * Creates a new instance of the DurationRule + * + * @param duration - age expressed as duration + * @return DurationRule + */ public DurationRule maxAge(Duration duration) { return new DurationRule(duration); } + /** + * Creates an empty default rule + * + * @return DefaultRule + */ public DefaultRule defaulRule() { return new DefaultRule(); } - public static final class IntersectionRule implements GCRule { + /** + * Fluent wrapper for {@link Intersection} rule. + * Allows far adding an hierarchy of rules with intersection as the root + */ + public static final class IntersectionRule extends BaseRule { private GcRule.Intersection.Builder builder; - + private List rulesList = new ArrayList<>(); + private IntersectionRule() { this.builder = GcRule.Intersection.newBuilder(); } + /** + * Adds a new GCRule + * + * @param rule + * @return IntersectionRule + */ public IntersectionRule rule(@Nonnull GCRule rule) { + rulesList.add(rule); builder.addRules(rule.toProto()); return this; } + /** + * Gets the list of child rules + * + * @return List + */ + public List getRulesList() { + return rulesList; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("rulesList", rulesList) + .toString(); + } + + @InternalApi @Override public GcRule toProto() { switch (builder.getRulesCount()) { @@ -74,18 +149,47 @@ public GcRule toProto() { } } - public static final class UnionRule implements GCRule { + /** + * Fluent wrapper for {@link Union} rule. + * Allows far adding an hierarchy of rules with union as the root + */ + public static final class UnionRule extends BaseRule { private GcRule.Union.Builder builder; - + private List rulesList = new ArrayList<>(); + private UnionRule() { this.builder = GcRule.Union.newBuilder(); } + /** + * Adds a new GCRule + * + * @param rule + * @return UnionRule + */ public UnionRule rule(@Nonnull GCRule rule) { + rulesList.add(rule); builder.addRules(rule.toProto()); return this; } + /** + * Gets the list of child rules + * + * @return List + */ + public List getRulesList() { + return rulesList; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("rulesList", rulesList) + .toString(); + } + + @InternalApi @Override public GcRule toProto() { switch (builder.getRulesCount()) { @@ -99,7 +203,10 @@ public GcRule toProto() { } } - public static final class VersionRule implements GCRule { + /** + * Wrapper for building max versions rule + */ + public static final class VersionRule extends BaseRule { private GcRule.Builder builder; private VersionRule(int maxVersion) { @@ -107,13 +214,31 @@ private VersionRule(int maxVersion) { builder.setMaxNumVersions(maxVersion); } + /** + * Gets the configured maximum versions + */ + public int getMaxVersions() { + return toProto().getMaxNumVersions(); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("maxNumVersions", getMaxVersions()) + .toString(); + } + + @InternalApi @Override public GcRule toProto() { return builder.build(); } } - public static final class DurationRule implements GCRule { + /** + * Wrapper for building max duration rule + */ + public static final class DurationRule extends BaseRule { private com.google.protobuf.Duration.Builder builder; private DurationRule(Duration duration) { @@ -122,23 +247,102 @@ private DurationRule(Duration duration) { .setSeconds(duration.getSeconds()) .setNanos(duration.getNano()); } + + /** + * Gets the configured maximum age + * + * @return Duration + */ + public Duration getMaxAge() { + long seconds = toProto().getMaxAge().getSeconds(); + int nanos = toProto().getMaxAge().getNanos(); + return Duration.ofSeconds(seconds, nanos); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("maxAge", getMaxAge()) + .toString(); + } + @InternalApi @Override public GcRule toProto() { return GcRule.newBuilder().setMaxAge(builder.build()).build(); } } - public static final class DefaultRule implements GCRule { + /** + * Wrapper for building a empty rule + * + */ + public static final class DefaultRule extends BaseRule { private DefaultRule() {} + @InternalApi @Override public GcRule toProto() { return GcRule.getDefaultInstance(); } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .toString(); + } } - interface GCRule { + /** + * Helpers to support casting rules the Type, when the Type is known + */ + public static abstract class BaseRule implements GCRule { + + /** + * Casts the rule to DurationRule. + * On failure throws a ClassCastException + */ + public DurationRule getDurationOrThow() { + return (DurationRule) this; + } + + /** + * Casts the rule to VersionRule. + * On failure throws a ClassCastException + */ + public VersionRule getVersionOrThow() { + return (VersionRule) this; + } + + /** + * Casts the rule to UnionRule. + * On failure throws a ClassCastException + */ + public UnionRule getUnionOrThow() { + return (UnionRule) this; + } + + /** + * Casts the rule to IntersectionRule. + * On failure throws a ClassCastException + */ + public IntersectionRule getIntersectionOrThow() { + return (IntersectionRule) this; + } + } + + /** + * + * interface for fluent GcRule wrappers + * + */ + public interface GCRule { + DurationRule getDurationOrThow(); + VersionRule getVersionOrThow(); + UnionRule getUnionOrThow(); + IntersectionRule getIntersectionOrThow(); + + @InternalApi GcRule toProto(); } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java index 5d5ace05c31e..7515ed361386 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java @@ -13,9 +13,12 @@ */ package com.google.cloud.bigtable.admin.v2.models; +import com.google.api.core.BetaApi; +import com.google.api.core.InternalApi; import com.google.bigtable.admin.v2.ColumnFamily; import com.google.bigtable.admin.v2.CreateTableRequest; import com.google.bigtable.admin.v2.Table; +import com.google.bigtable.admin.v2.TableName; import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.bigtable.admin.v2.CreateTableRequest.Split; import com.google.bigtable.admin.v2.GcRule; @@ -26,38 +29,86 @@ import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; -/** Idiomatic models to build Bigtable admin requests */ +/** + * + * Fluent DSL models to build Bigtable Table admin requests + * + */ +@BetaApi public final class TableAdminRequests { private TableAdminRequests() {} + /** + * Factory method to get an instance of CreateTable + * + * @param tableId - the id of the table to create + * @return CreateTable + */ public static CreateTable createTable(String tableId) { return new CreateTable(tableId); } + /** + * Factory method to get an instance of ModifyFamilies + * + * @param tableId - the id of the table to create + * @return ModifyFamilies + */ public static ModifyFamilies modifyFamilies(String tableId) { return new ModifyFamilies(tableId); } + /** + * Fluent wrapper for {@link CreateTableRequest} + * + *
 
+   * Allows for creating table with 
+   *    - optional columnFamilies, including optional {@link GCRule} 
+   *    - optional granularity
+   *    - and optional split points
+   * 
+ */ public static final class CreateTable { private final CreateTableRequest.Builder createTableRequest = CreateTableRequest.newBuilder(); private final Table.Builder tableRequest = Table.newBuilder(); + /** + * Configures table with the specified id + * + * @param tableId + */ private CreateTable(String tableId) { createTableRequest.setTableId(tableId); } + /** + * Configures table with the specified granularity + * + * @param granularity + */ public CreateTable withGranularity(TimestampGranularity granularity) { Preconditions.checkNotNull(granularity); tableRequest.setGranularity(granularity); return this; } + /** + * Adds a new columnFamily to the configuration + * + * @param familyId + */ public CreateTable addColumnFamily(String familyId) { Preconditions.checkNotNull(familyId); tableRequest.putColumnFamilies(familyId, ColumnFamily.newBuilder().build()); return this; } + /** + * Adds a new columnFamily with {@link GCRule} to the configuration + * + * @param familyId + * @param gcRule + */ public CreateTable addColumnFamily(String familyId, GCRule gcRule) { Preconditions.checkNotNull(familyId); tableRequest.putColumnFamilies( @@ -65,18 +116,30 @@ public CreateTable addColumnFamily(String familyId, GCRule gcRule) { return this; } + /** + * Adds split at the specified key to the configuration + * + * @param key + */ public CreateTable addSplit(ByteString key) { Preconditions.checkNotNull(key); createTableRequest.addInitialSplits(Split.newBuilder().setKey(key).build()); return this; } + /** + * Adds the specified number of uniform splits to the configuration + * + * @param key + * @param numSplits + */ public CreateTable addUniformSplits(ByteString key, int numSplits) { Preconditions.checkNotNull(key); // TODO: add implementation throw new UnsupportedOperationException(); } + @InternalApi public CreateTableRequest toProto(InstanceName instanceName) { Preconditions.checkNotNull(instanceName); return createTableRequest @@ -86,24 +149,47 @@ public CreateTableRequest toProto(InstanceName instanceName) { } } + /** + * Fluent wrapper for {@link ModifyColumnFamiliesRequest} + * + *
 
+   * Allows the following ColumnFamily modifications 
+   *    - create family, optionally with {@link GCRule} 
+   *    - update existing family {@link GCRule}
+   *    - drop an existing family
+   * 
+ */ public static final class ModifyFamilies { private final ModifyColumnFamiliesRequest.Builder modFamilyRequest = ModifyColumnFamiliesRequest.newBuilder(); private String tableId; + /** + * Configures the tableId to execute the modifications + * + * @param tableId + */ private ModifyFamilies(String tableId) { Preconditions.checkNotNull(tableId); this.tableId = tableId; } - public String getTableId() { - return this.tableId; - } - + /** + * Configures the name of the new ColumnFamily to be created + * @param familyId + * @return + */ public ModifyFamilies create(String familyId) { return createWithGCRule(familyId, null); } + /** + * Configures the name and GcRule of the new ColumnFamily to be created + * + * @param familyId + * @param gcRule + * @return + */ public ModifyFamilies createWithGCRule(String familyId, GCRule gcRule) { Modification.Builder modification = Modification.newBuilder().setId(familyId); GcRule grule = (gcRule == null) ? GcRule.getDefaultInstance() : gcRule.toProto(); @@ -112,6 +198,13 @@ public ModifyFamilies createWithGCRule(String familyId, GCRule gcRule) { return this; } + /** + * Updates the GCRule of existing ColumnFamily + * + * @param familyId + * @param gcRule + * @return + */ public ModifyFamilies updateWithGCRule(String familyId, GCRule gcRule) { Modification.Builder modification = Modification.newBuilder().setId(familyId); Preconditions.checkNotNull(gcRule); @@ -120,6 +213,12 @@ public ModifyFamilies updateWithGCRule(String familyId, GCRule gcRule) { return this; } + /** + * Drops the specified ColumnFamily + * + * @param familyId + * @return + */ public ModifyFamilies drop(String familyId) { Modification.Builder modification = Modification.newBuilder().setId(familyId); modification.setId(familyId).setDrop(true); @@ -127,8 +226,10 @@ public ModifyFamilies drop(String familyId) { return this; } - public ModifyColumnFamiliesRequest toProto(String tableName) { - Preconditions.checkNotNull(tableName); + @InternalApi + public ModifyColumnFamiliesRequest toProto(InstanceName instanceName) { + Preconditions.checkNotNull(instanceName); + String tableName = TableName.of(instanceName.getProject(), instanceName.getInstance(), tableId).toString(); return modFamilyRequest.setName(tableName).build(); } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java index f26f1b26370a..dcfeb3127ca0 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java @@ -19,83 +19,134 @@ import java.util.Map; import java.util.Map.Entry; import org.threeten.bp.Duration; -import com.google.bigtable.admin.v2.Table.ClusterState; import com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState; import com.google.bigtable.admin.v2.Table.TimestampGranularity; -import com.google.bigtable.admin.v2.ColumnFamily; +import com.google.api.core.BetaApi; +import com.google.api.core.InternalApi; +import com.google.bigtable.admin.v2.CheckConsistencyRequest; import com.google.bigtable.admin.v2.GcRule; import com.google.bigtable.admin.v2.GcRule.RuleCase; import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; -import com.google.bigtable.admin.v2.Table; import com.google.bigtable.admin.v2.TableName; +import com.google.cloud.bigtable.admin.v2.TableAdminClient; import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; import com.google.cloud.bigtable.admin.v2.models.GCRules.IntersectionRule; import com.google.cloud.bigtable.admin.v2.models.GCRules.UnionRule; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; -/** - * Table admin response wrapper objects to limit exposure to the underlying protocol buffer objects +/** + * + * Bigtable Table admin response wrappers + * */ +@BetaApi public class TableAdminResponses { private TableAdminResponses() {} - public static TableResponse convertTable(Table table) { - return new TableResponse(table); + /** + * Converts the protocol buffer table to a simpler Table model with only the required elements + * + * @param com.google.bigtable.admin.v2.Table - Protobuf table + * @return Table - Table response wrapper + */ + @InternalApi + public static Table convertTable(com.google.bigtable.admin.v2.Table table) { + return new Table(table); } - public static ConsistencyTokenResponse convertTokenResponse( + /** + * Converts the protocol buffer response to a simpler ConsistencyToken which can be passed back as is. + * + * @param GenerateConsistencyTokenResponse - Protobuf ConsistencyTokenResponse + * @return ConsistencyToken - ConsistencyToken response wrapper + */ + public static ConsistencyToken convertTokenResponse( GenerateConsistencyTokenResponse tokenResponse) { - return new ConsistencyTokenResponse(tokenResponse); + return new ConsistencyToken(tokenResponse); } - /** Wrapper on {@link Table} protocol buffer object */ - public static final class TableResponse { + /** + * Wrapper for {@link Table} protocol buffer object + * + */ + public static final class Table { private TableName tableName; private TimestampGranularity timestampGranularity; - private Map clusterStates = new HashMap<>(); - // TODO - fix spelling - private Map columnFamilies = new HashMap<>(); + private Map clusterStates = new HashMap<>(); + private Map columnFamilies = new HashMap<>(); - private TableResponse(Table table) { + private Table(com.google.bigtable.admin.v2.Table table) { Preconditions.checkNotNull(table); this.tableName = TableName.parse(table.getName()); this.timestampGranularity = table.getGranularity(); - Map clusterStatesMap = table.getClusterStatesMap(); - for (Entry entry : clusterStatesMap.entrySet()) { + Map clusterStatesMap = table.getClusterStatesMap(); + for (Entry entry : clusterStatesMap.entrySet()) { clusterStates.put( - entry.getKey(), new ClusterStateResponse(entry.getKey(), entry.getValue())); + entry.getKey(), new ClusterState(entry.getKey(), entry.getValue())); } - Map columnFamiliesMap = table.getColumnFamiliesMap(); - for (Entry entry : columnFamiliesMap.entrySet()) { + Map columnFamiliesMap = table.getColumnFamiliesMap(); + for (Entry entry : columnFamiliesMap.entrySet()) { columnFamilies.put( - entry.getKey(), new ColumnFamilyResponse(entry.getKey(), entry.getValue())); + entry.getKey(), new ColumnFamily(entry.getKey(), entry.getValue())); } } + /** + * Gets the unique name of the table in the format: + * projects/{project}/instances/{instance}/tables/{tableId} + * + * @return TableName + */ public TableName getTableName() { return tableName; } + /** + * Gets the timestampGranularity of the table + * + * @return TimestampGranularity + */ public TimestampGranularity getTimestampGranularity() { return timestampGranularity; } - public Map getClusterStatesMap() { + /** + * Returns state of the table by clusters in the instance as map of clusterId and {@link ClusterState} + * + * @return Map + */ + public Map getClusterStatesMap() { return clusterStates; } - public Map getColumnFamiliesMap() { + /** + * Returns a map of columfamilies in the table keyed by columnfamily and name + * + * @return Map + */ + public Map getColumnFamiliesMap() { return columnFamilies; } - public Collection getClusterStates() { + /** + * Returns state of the table by clusters in the instance as a Collection + * + * @return Collection + */ + public Collection getClusterStates() { return clusterStates.values(); } - public Collection getColumnFamiles() { + /** + * Returns all columnfamilies in the table as a Collection + * + * @return + */ + public Collection getColumnFamiles() { return columnFamilies.values(); } @@ -110,20 +161,32 @@ public String toString() { } } - /** Wrapper on {@link ClusterStateResponse} protocol buffer object */ - public static final class ClusterStateResponse { + /** + * Wrapper for {@link ClusterState} protocol buffer object + */ + public static final class ClusterState { private String id; private ReplicationState replicationState; - private ClusterStateResponse(String id, ClusterState clusterState) { + private ClusterState(String id, com.google.bigtable.admin.v2.Table.ClusterState clusterState) { this.id = id; replicationState = clusterState.getReplicationState(); } + /** + * Gets the cluster Id + * + * @return String + */ public String getId() { return id; } + /** + * Gets the ReplicationState of the table for this cluster + * + * @return ReplicationState + */ public ReplicationState getReplicationState() { return replicationState; } @@ -137,12 +200,12 @@ public String toString() { } } - /** Wrapper on {@link ColumnFamily} protocol buffer object */ - public static final class ColumnFamilyResponse { + /** Wrapper for {@link ColumnFamily} protocol buffer object */ + public static final class ColumnFamily { private String id; private GCRule gCRule = GCRULES.defaulRule(); - private ColumnFamilyResponse(String id, ColumnFamily cf) { + private ColumnFamily(String id, com.google.bigtable.admin.v2.ColumnFamily cf) { Preconditions.checkNotNull(id); Preconditions.checkNotNull(cf); this.id = id; @@ -152,38 +215,36 @@ private ColumnFamilyResponse(String id, ColumnFamily cf) { } } + /** + * Gets the columnfamily name + * @return String + */ public String getId() { return id; } - public GcRule getGcRule() { - // TODO: Exposing the protobuf to let users traverse the rule hierarchy as a quick hack - // If hierarchy is actually limited in real use, it might have more options to consider - return gCRule.toProto(); + /** + * Get's the GCRule configured for the columnfamily + * + * @return GCRule + */ + public GCRule getGCRule() { + return gCRule; } + /** + * Returns true if a GCRule has been configured for the family + * @return + */ public boolean hasGcRule() { return !RuleCase.RULE_NOT_SET.equals(gCRule.toProto().getRuleCase()); } - public boolean hasUnion() { - return gCRule.toProto().hasUnion(); - } - - public boolean hasIntersection() { - return gCRule.toProto().hasIntersection(); - } - - public boolean hasMaxAge() { - return gCRule.toProto().hasMaxAge(); - } - @Override public String toString() { - // TODO: fix inconsistent json format return MoreObjects.toStringHelper(this) .add("id", id) - .add("gCRule", gCRule.toProto()) + .add("gCRule", gCRule) .toString(); } @@ -216,15 +277,25 @@ private GCRule convertGcRule(GcRule source) { } } - /** Wrapper on {@link GenerateConsistencyTokenResponse#getConsistencyToken()} */ - public static final class ConsistencyTokenResponse { + /** + * Wrapper for {@link GenerateConsistencyTokenResponse#getConsistencyToken()} + * Cannot be created, they are obtained by invoking {@link TableAdminClient#generateConsistencyToken(String)} + */ + public static final class ConsistencyToken { private String token; - private ConsistencyTokenResponse(GenerateConsistencyTokenResponse resp) { + private ConsistencyToken(GenerateConsistencyTokenResponse resp) { this.token = resp.getConsistencyToken(); } - public String getToken() { + @InternalApi + public CheckConsistencyRequest toProto(String tableName) { + return CheckConsistencyRequest.newBuilder().setName(tableName).setConsistencyToken(token) + .build(); + } + + @VisibleForTesting + String getToken() { return token; } diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java index 673178e43182..259a15a8a69a 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java @@ -7,6 +7,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; @@ -30,6 +31,7 @@ import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.CreateTable; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.ModifyFamilies; +import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.ConsistencyToken; import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStub; import com.google.protobuf.ByteString; import com.google.protobuf.Empty; @@ -109,7 +111,7 @@ public void modifyFamilies() { ModifyFamilies modifyFamReq = TableAdminRequests.modifyFamilies("tableId"); adminClient.modifyFamilies(modifyFamReq); Mockito.verify(mockModifyTableCallable) - .call(modifyFamReq.toProto(adminClient.getTableName("tableId"))); + .call(modifyFamReq.toProto(adminClient.getInstanceName())); } @Test @@ -117,7 +119,7 @@ public void modifyFamiliesAsync() { ModifyFamilies modifyFamReq = TableAdminRequests.modifyFamilies("tableId"); adminClient.modifyFamiliesAsync(modifyFamReq); Mockito.verify(mockModifyTableCallable) - .futureCall(modifyFamReq.toProto(adminClient.getTableName("tableId"))); + .futureCall(modifyFamReq.toProto(adminClient.getInstanceName())); } @Test @@ -221,7 +223,7 @@ public void dropRowRangeAsync() { } @Test - public void generateConsistencyToken() { + public void generateAndCheckConsistency() { GenerateConsistencyTokenResponse genResp = GenerateConsistencyTokenResponse.newBuilder().build(); Mockito.when( @@ -229,13 +231,24 @@ public void generateConsistencyToken() { adminClient.composeGenerateConsistencyTokenRequest("tableId"))) .thenReturn(genResp); - adminClient.generateConsistencyToken("tableId"); + ConsistencyToken consistencyToken = adminClient.generateConsistencyToken("tableId"); Mockito.verify(mockGenerateConsistencyTokenCallable) .call(adminClient.composeGenerateConsistencyTokenRequest("tableId")); + + ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(CheckConsistencyRequest.class); + CheckConsistencyResponse consistencyResp = CheckConsistencyResponse.newBuilder().build(); + Mockito.when( + mockCheckConsistencyCallable.call( + any(CheckConsistencyRequest.class))) + .thenReturn(consistencyResp); + + adminClient.isConsistent("tableId", consistencyToken); + Mockito.verify(mockCheckConsistencyCallable) + .call(requestCaptor.capture()); } @Test - public void generateConsistencyTokenAsync() { + public void generateAndCheckConsistencyAsync() throws Exception { ApiFuture genResp = ApiFutures.immediateFuture(GenerateConsistencyTokenResponse.newBuilder().build()); Mockito.when( @@ -243,36 +256,21 @@ public void generateConsistencyTokenAsync() { adminClient.composeGenerateConsistencyTokenRequest("tableId"))) .thenReturn(genResp); - adminClient.generateConsistencyTokenAsync("tableId"); + ApiFuture consistencyTokenFuture = adminClient.generateConsistencyTokenAsync("tableId"); Mockito.verify(mockGenerateConsistencyTokenCallable) .futureCall(adminClient.composeGenerateConsistencyTokenRequest("tableId")); - } - - @Test - public void isConsistent() { - CheckConsistencyResponse consistencyResp = CheckConsistencyResponse.newBuilder().build(); - Mockito.when( - mockCheckConsistencyCallable.call( - adminClient.composeCheckConsistencyRequest("tableId", "token"))) - .thenReturn(consistencyResp); - - adminClient.isConsistent("tableId", "token"); - Mockito.verify(mockCheckConsistencyCallable) - .call(adminClient.composeCheckConsistencyRequest("tableId", "token")); - } - - @Test - public void isConsistentAsync() { + + ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(CheckConsistencyRequest.class); ApiFuture consistencyResp = ApiFutures.immediateFuture(CheckConsistencyResponse.newBuilder().build()); Mockito.when( mockCheckConsistencyCallable.futureCall( - adminClient.composeCheckConsistencyRequest("tableId", "token"))) + any(CheckConsistencyRequest.class))) .thenReturn(consistencyResp); - adminClient.isConsistentAsync("tableId", "token"); + adminClient.isConsistentAsync("tableId", consistencyTokenFuture.get()); Mockito.verify(mockCheckConsistencyCallable) - .futureCall(adminClient.composeCheckConsistencyRequest("tableId", "token")); + .futureCall(requestCaptor.capture()); } @Test diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java index 3308d6466722..5635bc4af11a 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java @@ -33,7 +33,8 @@ import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.CreateTable; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.ModifyFamilies; -import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.TableResponse; +import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.ConsistencyToken; +import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.Table; import com.google.protobuf.ByteString; public class TableAdminClientIT { @@ -61,14 +62,14 @@ public void createTable() throws Exception { .addSplit(ByteString.copyFromUtf8("q")); try { - TableResponse tableResponse = tableAdmin.createTable(createTableReq); + Table tableResponse = tableAdmin.createTable(createTableReq); assertNotNull(tableResponse); assertEquals(tableId, tableResponse.getTableName().getTable()); assertEquals(2, tableResponse.getColumnFamiles().size()); assertFalse(tableResponse.getColumnFamiliesMap().get("cf1").hasGcRule()); assertTrue(tableResponse.getColumnFamiliesMap().get("cf2").hasGcRule()); assertEquals( - 10, tableResponse.getColumnFamiliesMap().get("cf2").getGcRule().getMaxNumVersions()); + 10, tableResponse.getColumnFamiliesMap().get("cf2").getGCRule().getVersionOrThow().getMaxVersions()); assertEquals(TimestampGranularity.MILLIS, tableResponse.getTimestampGranularity()); // TODO: is there a way to test splits here? } finally { @@ -84,7 +85,7 @@ public void modifyFamilies() { modifyFamiliesReq .create("mf1") .createWithGCRule( - "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000))) // .seconds(1000).nanos(20000) + "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000))) .updateWithGCRule( "mf1", GCRULES @@ -107,20 +108,19 @@ public void modifyFamilies() { try { tableAdmin.createTable(TableAdminRequests.createTable(tableId)); - TableResponse tableResponse = tableAdmin.modifyFamilies(modifyFamiliesReq); + Table tableResponse = tableAdmin.modifyFamilies(modifyFamiliesReq); assertEquals(5, tableResponse.getColumnFamiles().size()); assertNotNull(tableResponse.getColumnFamiliesMap().get("mf1")); assertNotNull(tableResponse.getColumnFamiliesMap().get("mf2")); - assertTrue(tableResponse.getColumnFamiliesMap().get("mf1").getGcRule().hasUnion()); + assertEquals(2, tableResponse.getColumnFamiliesMap().get("mf1").getGCRule().getUnionOrThow().getRulesList().size()); assertEquals( 1000, - tableResponse.getColumnFamiliesMap().get("mf2").getGcRule().getMaxAge().getSeconds()); + tableResponse.getColumnFamiliesMap().get("mf2").getGCRule().getDurationOrThow().getMaxAge().getSeconds()); assertEquals( 20000, - tableResponse.getColumnFamiliesMap().get("mf2").getGcRule().getMaxAge().getNanos()); - assertTrue(tableResponse.getColumnFamiliesMap().get("mf3").getGcRule().hasIntersection()); - assertFalse(tableResponse.getColumnFamiliesMap().get("mf4").getGcRule().hasIntersection()); - assertTrue(tableResponse.getColumnFamiliesMap().get("mf4").getGcRule().hasMaxAge()); + tableResponse.getColumnFamiliesMap().get("mf2").getGCRule().getDurationOrThow().getMaxAge().getNano()); + assertEquals(2, tableResponse.getColumnFamiliesMap().get("mf3").getGCRule().getIntersectionOrThow().getRulesList().size()); + assertEquals(360, tableResponse.getColumnFamiliesMap().get("mf4").getGCRule().getDurationOrThow().getMaxAge().getSeconds()); assertNotNull(tableResponse.getColumnFamiliesMap().get("mf7")); } finally { tableAdmin.deleteTable(tableId); @@ -140,7 +140,7 @@ public void getTable() { try { tableAdmin.createTable(TableAdminRequests.createTable(tableId)); - TableResponse tableResponse = tableAdmin.getTable(tableId); + Table tableResponse = tableAdmin.getTable(tableId); assertNotNull(tableResponse); assertEquals(tableId, tableResponse.getTableName().getTable()); } finally { @@ -195,9 +195,9 @@ public void checkConsistency() { try { tableAdmin.createTable(TableAdminRequests.createTable(tableId)); - String token = tableAdmin.generateConsistencyToken(tableId); - boolean consistent = tableAdmin.isConsistent(tableId, token); - assertNotNull(token); + ConsistencyToken consistencyToken = tableAdmin.generateConsistencyToken(tableId); + assertNotNull(consistencyToken); + boolean consistent = tableAdmin.isConsistent(tableId, consistencyToken); assertTrue(consistent); } finally { tableAdmin.deleteTable(tableId); diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java index 40fdf5bbb826..6bce75f0ea6e 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java @@ -19,7 +19,9 @@ import org.junit.runners.JUnit4; import com.google.bigtable.admin.v2.ColumnFamily; import com.google.bigtable.admin.v2.CreateTableRequest; +import com.google.bigtable.admin.v2.GcRule; import com.google.bigtable.admin.v2.Table; +import com.google.bigtable.admin.v2.TableName; import com.google.bigtable.admin.v2.CreateTableRequest.Split; import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; @@ -29,23 +31,24 @@ @RunWith(JUnit4.class) public class TableAdminRequestsTest { + private final InstanceName instanceName = InstanceName.of("project", "instance"); @Test public void createTable() { CreateTableRequest actual = TableAdminRequests.createTable("tableId") - .withGranularity(TimestampGranularity.MILLIS) + .withGranularity(TimestampGranularity.MILLIS) .addColumnFamily("cf1") .addColumnFamily("cf2", GCRules.GCRULES.maxVersions(1)) .addSplit(ByteString.copyFromUtf8("c")) - .toProto(InstanceName.of("project", "instance")); + .toProto(instanceName); CreateTableRequest expected = CreateTableRequest.newBuilder() .setTableId("tableId") - .setParent(InstanceName.of("project", "instance").getInstance()) + .setParent(InstanceName.of("project", "instance").toString()) .addInitialSplits(Split.newBuilder().setKey(ByteString.copyFromUtf8("c"))) - .setTable( + .setTable( Table.newBuilder() .setGranularity(TimestampGranularity.MILLIS) .putColumnFamilies("cf1", ColumnFamily.newBuilder().build()) @@ -61,7 +64,7 @@ public void createTable() { @Test(expected = NullPointerException.class) public void createTable_RequiredTableId() { - TableAdminRequests.createTable(null).toProto(InstanceName.of("project", "instance")); + TableAdminRequests.createTable(null).toProto(instanceName); } @Test(expected = NullPointerException.class) @@ -77,14 +80,14 @@ public void modifyFamilies() { .createWithGCRule("cf2", GCRules.GCRULES.maxVersions(1)) .create("cf3") .updateWithGCRule("cf1", GCRules.GCRULES.maxVersions(5)) - .drop("cf3") - .toProto("uniqueTableName"); + .drop("cf3") + .toProto(instanceName); ModifyColumnFamiliesRequest expected = ModifyColumnFamiliesRequest.newBuilder() - .setName("uniqueTableName") + .setName(TableName.of(instanceName.getProject(), instanceName.getInstance(), "tableId").toString()) .addModifications( - Modification.newBuilder().setId("cf1").setCreate(ColumnFamily.newBuilder())) + Modification.newBuilder().setId("cf1").setCreate(ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()))) .addModifications( Modification.newBuilder() .setId("cf2") @@ -92,25 +95,21 @@ public void modifyFamilies() { ColumnFamily.newBuilder() .setGcRule(GCRules.GCRULES.maxVersions(1).toProto()))) .addModifications( - Modification.newBuilder().setId("cf3").setCreate(ColumnFamily.newBuilder())) - /* - * .addModifications( Modification.newBuilder().setId("cf2") - * .setUpdate(ColumnFamily.newBuilder())) - */ + Modification.newBuilder().setId("cf3").setCreate(ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()))) .addModifications( Modification.newBuilder() .setId("cf1") .setUpdate( ColumnFamily.newBuilder() .setGcRule(GCRules.GCRULES.maxVersions(5).toProto()))) - .addModifications(Modification.newBuilder().setId("cf3").setDrop(true)) + .addModifications(Modification.newBuilder().setId("cf3").setDrop(true)) .build(); assertThat(actual).isEqualTo(expected); } @Test(expected = NullPointerException.class) public void modifyFamilies_RequiredTableId() { - TableAdminRequests.modifyFamilies(null).toProto("uniqueTableName"); + TableAdminRequests.modifyFamilies(null).toProto(instanceName); } @Test(expected = NullPointerException.class) diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java index 6a67b491e88b..0e1a2fb52cd4 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java @@ -1,3 +1,16 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ package com.google.cloud.bigtable.admin.v2.models; import static org.junit.Assert.*; @@ -7,13 +20,14 @@ import org.threeten.bp.Duration; import com.google.bigtable.admin.v2.ColumnFamily; import com.google.bigtable.admin.v2.GcRule; -import com.google.bigtable.admin.v2.Table; import com.google.bigtable.admin.v2.Table.ClusterState; import com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState; import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.bigtable.admin.v2.TableName; -import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.ConsistencyTokenResponse; -import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.TableResponse; +import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.IntersectionRule; +import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.ConsistencyToken; +import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.Table; import com.google.bigtable.admin.v2.GcRule.Intersection; import com.google.bigtable.admin.v2.GcRule.Union; import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; @@ -26,8 +40,8 @@ public class TableAdminResponsesTest { @Test public void convertTable() { TableName testName = TableName.of("p", "i", "testTable"); - Table table = - Table.newBuilder() + com.google.bigtable.admin.v2.Table table = + com.google.bigtable.admin.v2.Table.newBuilder() .setName(testName.toString()) .setGranularity(TimestampGranularity.MILLIS) .putClusterStates( @@ -56,7 +70,7 @@ public void convertTable() { .build()) .build(); - TableResponse tableResponse = TableAdminResponses.convertTable(table); + Table tableResponse = TableAdminResponses.convertTable(table); assertNotNull(tableResponse); assertEquals(testName, tableResponse.getTableName()); assertEquals(TimestampGranularity.MILLIS, tableResponse.getTimestampGranularity()); @@ -72,11 +86,11 @@ public void convertTable() { assertFalse(tableResponse.getColumnFamiliesMap().get("cf1").hasGcRule()); assertThat(GCRULES.defaulRule().toProto()) - .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf1").getGcRule()); + .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf1").getGCRule().toProto()); assertThat(GCRULES.maxVersions(1).toProto()) - .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf2").getGcRule()); + .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf2").getGCRule().toProto()); assertThat(GCRULES.maxAge(Duration.ofSeconds(1, 99)).toProto()) - .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGcRule()); + .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGCRule().toProto()); } @Test @@ -114,17 +128,27 @@ public void convertTable_unionOfIntersections() { .rule(GCRULES.maxAge(Duration.ofSeconds(1)))) .toProto(); - Table table = - Table.newBuilder() + com.google.bigtable.admin.v2.Table table = + com.google.bigtable.admin.v2.Table.newBuilder() .putColumnFamilies("cf3", ColumnFamily.newBuilder().setGcRule(expected).build()) .build(); - TableResponse tableResponse = TableAdminResponses.convertTable(table); - assertThat(actual).isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGcRule()); + Table tableResponse = TableAdminResponses.convertTable(table); + assertThat(actual).isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGCRule().toProto()); assertTrue(tableResponse.getColumnFamiliesMap().get("cf3").hasGcRule()); - assertTrue(tableResponse.getColumnFamiliesMap().get("cf3").hasUnion()); - assertFalse(tableResponse.getColumnFamiliesMap().get("cf3").hasIntersection()); - assertFalse(tableResponse.getColumnFamiliesMap().get("cf3").hasMaxAge()); + + GCRule parentUnion = tableResponse.getColumnFamiliesMap().get("cf3").getGCRule(); + assertEquals(2, parentUnion.getUnionOrThow().getRulesList().size()); + + IntersectionRule intersectionRule = parentUnion.getUnionOrThow().getRulesList().get(0).getIntersectionOrThow(); + assertEquals(2, intersectionRule.getIntersectionOrThow().getRulesList().size()); + assertEquals(1, intersectionRule.getIntersectionOrThow().getRulesList().get(0).getVersionOrThow().getMaxVersions()); + assertEquals(Duration.ofSeconds(1, 0), intersectionRule.getIntersectionOrThow().getRulesList().get(1).getDurationOrThow().getMaxAge()); + + intersectionRule = parentUnion.getUnionOrThow().getRulesList().get(1).getIntersectionOrThow(); + assertEquals(2, intersectionRule.getIntersectionOrThow().getRulesList().size()); + assertEquals(1, intersectionRule.getIntersectionOrThow().getRulesList().get(0).getVersionOrThow().getMaxVersions()); + assertEquals(Duration.ofSeconds(1, 0), intersectionRule.getIntersectionOrThow().getRulesList().get(1).getDurationOrThow().getMaxAge()); } @Test @@ -162,18 +186,18 @@ public void convertTable_intersectionOfUnions() { .rule(GCRULES.maxAge(Duration.ofSeconds(1)))) .toProto(); - Table table = - Table.newBuilder() + com.google.bigtable.admin.v2.Table table = + com.google.bigtable.admin.v2.Table.newBuilder() .putColumnFamilies("cf3", ColumnFamily.newBuilder().setGcRule(expected).build()) .build(); - TableResponse tableResponse = TableAdminResponses.convertTable(table); - assertThat(actual).isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGcRule()); + Table tableResponse = TableAdminResponses.convertTable(table); + assertThat(actual).isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGCRule().toProto()); } @Test public void convertTable_Empty() { - TableResponse tableResponse = TableAdminResponses.convertTable(Table.newBuilder().build()); + Table tableResponse = TableAdminResponses.convertTable(com.google.bigtable.admin.v2.Table.newBuilder().build()); assertNotNull(tableResponse); assertEquals( @@ -185,7 +209,7 @@ public void convertTable_Empty() { @Test public void convertTokenResponse() { - ConsistencyTokenResponse tokenResponse = + ConsistencyToken tokenResponse = TableAdminResponses.convertTokenResponse( GenerateConsistencyTokenResponse.newBuilder() .setConsistencyToken("87282hgwd8yg") From 4e049093199679d31763bfa614576f5a33a83a9a Mon Sep 17 00:00:00 2001 From: spollapally Date: Tue, 19 Jun 2018 16:03:33 -0400 Subject: [PATCH 04/10] Re-formating with googleformatter --- .../handwritten_files.lst | 8 + .../bigtable/admin/v2/TableAdminClient.java | 338 ++++++++++-------- .../bigtable/admin/v2/models/GCRules.java | 145 +++----- .../admin/v2/models/TableAdminRequests.java | 70 ++-- .../admin/v2/models/TableAdminResponses.java | 102 +++--- .../admin/v2/TableAdminClientTest.java | 42 ++- .../admin/v2/it/TableAdminClientIT.java | 57 ++- .../bigtable/admin/v2/models/GCRulesTest.java | 16 +- .../v2/models/TableAdminRequestsTest.java | 38 +- .../v2/models/TableAdminResponsesTest.java | 68 +++- 10 files changed, 498 insertions(+), 386 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable/handwritten_files.lst b/google-cloud-clients/google-cloud-bigtable/handwritten_files.lst index acf082236813..7d385c98eb25 100644 --- a/google-cloud-clients/google-cloud-bigtable/handwritten_files.lst +++ b/google-cloud-clients/google-cloud-bigtable/handwritten_files.lst @@ -74,3 +74,11 @@ src/main/java/com/google/cloud/bigtable/data/v2/internal/ByteStringComparator.ja src/main/java/com/google/cloud/bigtable/data/v2/internal/DummyBatchingDescriptor.java src/main/java/com/google/cloud/bigtable/data/v2/internal/RequestContext.java src/main/java/com/google/cloud/bigtable/data/v2/internal/RegexUtil.java +src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java +src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java +src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java +src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java +src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java +src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java +src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java +src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java index a812b63202c1..e923555ab52b 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java @@ -1,15 +1,17 @@ /* * Copyright 2018 Google LLC * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.google.cloud.bigtable.admin.v2; @@ -45,14 +47,15 @@ import com.google.protobuf.ByteString; import com.google.protobuf.Empty; -/** - * Client for creating, configuring, and deleting Cloud Bigtable tables - * +/** + * Client for creating, configuring, and deleting Cloud Bigtable tables + * *

Provides access to the table schemas only, not the data stored within the tables. - * + * *

See the individual methods for example code. - * - *

Sample code to get started: + * + *

Sample code to get started: + * *


  *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
  *     CreateTable createTableReq =
@@ -65,7 +68,7 @@
  *     client.createTable(createTableReq);
  *   }
  * 
- * + * *

Note: close() needs to be called on the bigtableTableAdminClient object to clean up resources * such as threads. In the example above, try-with-resources is used, which automatically calls * close(). @@ -74,23 +77,25 @@ * create(). For example: * *

To customize credentials: + * *


  * BigtableTableAdminSettings bigtableTableAdminSettings =
  *     BigtableTableAdminSettings.newBuilder()
  *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
  *         .build();
- * TableAdminClient client = 
+ * TableAdminClient client =
  * TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings);
  * 
* * To customize the endpoint: + * *

  * BigtableTableAdminSettings bigtableTableAdminSettings =
  *     BigtableTableAdminSettings.newBuilder().setEndpoint(myEndpoint).build();
- * TableAdminClient client = 
+ * TableAdminClient client =
  * TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings);
  * 
- **/ + */ @BetaApi public class TableAdminClient implements AutoCloseable { private final BigtableTableAdminStub stub; @@ -98,7 +103,7 @@ public class TableAdminClient implements AutoCloseable { /** * Constructs an instance of TableAdminClient with the given instanceName - * + * * @param instanceName * @throws IOException */ @@ -107,20 +112,22 @@ public static TableAdminClient create(InstanceName instanceName) throws IOExcept } /** - * Constructs an instance of TableAdminClient with the given instanceName and bigtableTableAdminSettings - * + * Constructs an instance of TableAdminClient with the given instanceName and + * bigtableTableAdminSettings + * * @param instanceName * @param adminSettings * @throws IOException */ - public static TableAdminClient create(InstanceName instanceName, - BigtableTableAdminSettings adminSettings) throws IOException { + public static TableAdminClient create( + InstanceName instanceName, BigtableTableAdminSettings adminSettings) throws IOException { return new TableAdminClient(instanceName, adminSettings); } /** - * Constructs an instance of TableAdminClient with the given instanceName and bigtableTableAdminStub - * + * Constructs an instance of TableAdminClient with the given instanceName and + * bigtableTableAdminStub + * * @param instanceName * @param stub * @throws IOException @@ -132,8 +139,9 @@ public static TableAdminClient create(InstanceName instanceName, BigtableTableAd private TableAdminClient(InstanceName instanceName, BigtableTableAdminSettings adminSettings) throws IOException { - this(instanceName, - ((BigtableTableAdminStubSettings) adminSettings.getStubSettings()).createStub()); + this( + instanceName, + ((BigtableTableAdminStubSettings) adminSettings.getStubSettings()).createStub()); } private TableAdminClient(InstanceName instanceName, BigtableTableAdminStub stub) @@ -146,6 +154,7 @@ private TableAdminClient(InstanceName instanceName, BigtableTableAdminStub stub) /** * Gets the instanceName this client is associated to + * * @return InstanceName */ public InstanceName getInstanceName() { @@ -158,21 +167,22 @@ public void close() { } /** - * Creates a new table with the specified configuration - * See {@link TableAdminRequests.createTable} for possible configurations - * + * Creates a new table with the specified configuration See {@link TableAdminRequests.createTable} + * for possible configurations + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *     CreateTable createTableReq =
    *         TableAdminRequests.createTable("tableId")
    *             .addColumnFamily("cf2", GCRULES.maxVersions(10))
    *             .withGranularity(TimestampGranularity.MILLIS);
-   *             
+   *
    *     client.createTable(createTableReq);
    *   }
    * 
- * + * * @param createTable * @return Table - the newly created table */ @@ -185,15 +195,15 @@ public Table createTable(CreateTable createTable) { /** * Creates a new table with the specified configuration asynchronously * See {@link TableAdminRequests#createTable(String)} - * + * *

Sample code: - *

 
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *     CreateTable createTableReq =
    *         TableAdminRequests.createTable("tableId")
    *             .addColumnFamily("cf2", GCRULES.maxVersions(10))
    *             .withGranularity(TimestampGranularity.MILLIS);
-   *             
+   *
    *     client.createTableAsync(createTableReq);
    *   }
    * 
@@ -207,11 +217,12 @@ public ApiFuture
createTableAsync(CreateTable createTable) { } /** - * Creates, Updates and drops ColumnFamilies as per the request - * See {@link TableAdminRequests#modifyFamilies(String)} - * + * Creates, Updates and drops ColumnFamilies as per the request See {@link + * TableAdminRequests#modifyFamilies(String)} + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *     ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId)
    *             .create("mf1")
@@ -233,24 +244,24 @@ public ApiFuture
createTableAsync(CreateTable createTable) { * client.modifyFamilies(modifyFamiliesReq); * } * - * + * * @param modifyFamily * @return Table - Modified table */ public Table modifyFamilies(ModifyFamilies modifyFamily) { - ModifyColumnFamiliesRequest modReq = - modifyFamily.toProto(instanceName); + ModifyColumnFamiliesRequest modReq = modifyFamily.toProto(instanceName); com.google.bigtable.admin.v2.Table table = this.stub.modifyColumnFamiliesCallable().call(modReq); return TableAdminResponses.convertTable(table); } /** - * Creates, Updates and drops ColumnFamilies as per the request asynchronously - * See {@link TableAdminRequests#modifyFamilies(String)} - * + * Creates, Updates and drops ColumnFamilies as per the request asynchronously See {@link + * TableAdminRequests#modifyFamilies(String)} + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *     ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId)
    *             .create("mf1")
@@ -272,26 +283,26 @@ public Table modifyFamilies(ModifyFamilies modifyFamily) {
    *     client.modifyFamiliesAsync(modifyFamiliesReq);
    *   }
    * 
- * + * * @param modifyFamily * @return Table - Modified table */ public ApiFuture
modifyFamiliesAsync(ModifyFamilies modifyFamily) { - ModifyColumnFamiliesRequest modReq = - modifyFamily.toProto(instanceName); + ModifyColumnFamiliesRequest modReq = modifyFamily.toProto(instanceName); return transformToTableResponse(this.stub.modifyColumnFamiliesCallable().futureCall(modReq)); } /** * Deletes the specified tableId - * + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.deleteTable("tableId");
+   *        client.deleteTable("tableId");
    *   }
    * 
- * + * * @param tableId */ public void deleteTable(String tableId) { @@ -299,15 +310,16 @@ public void deleteTable(String tableId) { } /** - * Deletes the specified tableId asynchronously - * + * Deletes the specified tableId asynchronously + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.deleteTableAsync("tableId");
+   *        client.deleteTableAsync("tableId");
    *   }
    * 
- * + * * @param tableId */ public ApiFuture deleteTableAsync(String tableId) { @@ -317,14 +329,15 @@ public ApiFuture deleteTableAsync(String tableId) { /** * Gets the Table by tableId - * + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.getTable("tableId");
+   *        client.getTable("tableId");
    *   }
    * 
- * + * * @param tableId * @return Table */ @@ -336,14 +349,14 @@ public Table getTable(String tableId) { /** * Gets the Table by tableId - * + * *

Sample code: - *

 
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.getTableAsync("tableId");
+   *        client.getTableAsync("tableId");
    *   }
    * 
- * + * * @param tableId * @return ApiFuture
*/ @@ -353,15 +366,16 @@ public ApiFuture
getTableAsync(String tableId) { } /** - * Lists all TableNames in the instance. - * + * Lists all TableNames in the instance. + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.listTables();
+   *        client.listTables();
    *   }
    * 
- * + * * @param tableId * @return List */ @@ -372,14 +386,15 @@ public List listTables() { /** * Lists all TableNames in the instance asynchronously - * + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.listTablesAsync();
+   *        client.listTablesAsync();
    *   }
    * 
- * + * * @param tableId * @return List */ @@ -387,21 +402,24 @@ public ApiFuture> listTablesAsync() { ApiFuture listResp = this.stub.listTablesCallable().futureCall(composeListTableRequest()); - return ApiFutures.transform(listResp, new ApiFunction>() { - @Override - public List apply(ListTablesResponse input) { - return convertToTableNames(input); - } - }); + return ApiFutures.transform( + listResp, + new ApiFunction>() { + @Override + public List apply(ListTablesResponse input) { + return convertToTableNames(input); + } + }); } /** * Drops rows by the specified key prefix and tableId - * + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.dropRowRange("tableId");
+   *        client.dropRowRange("tableId");
    *   }
    * 
* @@ -414,11 +432,12 @@ public void dropRowRange(String tableId, String rowKeyPrefix) { /** * Drops rows by the specified key prefix and tableId asynchronously - * + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.dropRowRangeAsync("tableId");
+   *        client.dropRowRangeAsync("tableId");
    *   }
    * 
* @@ -431,11 +450,12 @@ public ApiFuture dropRowRangeAsync(String tableId, String rowKeyPrefix) { /** * Drops rows by the specified key prefix and tableId - * + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.dropRowRange("tableId");
+   *        client.dropRowRange("tableId");
    *   }
    * 
* @@ -448,11 +468,12 @@ public void dropRowRange(String tableId, ByteString rowKeyPrefix) { /** * Drops rows by the specified key prefix and tableId - * + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.dropRowRangeAsync("tableId");
+   *        client.dropRowRangeAsync("tableId");
    *   }
    * 
* @@ -460,17 +481,20 @@ public void dropRowRange(String tableId, ByteString rowKeyPrefix) { * @param rowKeyPrefix */ public ApiFuture dropRowRangeAsync(String tableId, ByteString rowKeyPrefix) { - return transformToVoid(this.stub.dropRowRangeCallable().futureCall( - composeDropRowRangeRequest(tableId, rowKeyPrefix, false))); + return transformToVoid( + this.stub + .dropRowRangeCallable() + .futureCall(composeDropRowRangeRequest(tableId, rowKeyPrefix, false))); } /** * Drops all data in the table - * + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.dropAllData("tableId");
+   *        client.dropAllData("tableId");
    *   }
    * 
* @@ -482,61 +506,70 @@ public void dropAllData(String tableId) { /** * Drops all data in the table asynchronously - * + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.dropAllData("tableId");
+   *        client.dropAllData("tableId");
    *   }
    * 
- * + * * @param tableId * @return ApiFuture */ public ApiFuture dropAllDataAsync(String tableId) { - return transformToVoid(this.stub.dropRowRangeCallable() - .futureCall(composeDropRowRangeRequest(tableId, null, true))); + return transformToVoid( + this.stub + .dropRowRangeCallable() + .futureCall(composeDropRowRangeRequest(tableId, null, true))); } /** * Generates a token to verify the replication status of table mutations invoked before this call. - * Token expires in 90 days - * + * Token expires in 90 days + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.generateConsistencyToken("tableId");
+   *        client.generateConsistencyToken("tableId");
    *   }
    * 
- * + * * @param tableId * @return ConsistencyToken */ public ConsistencyToken generateConsistencyToken(String tableId) { - return TableAdminResponses.convertTokenResponse(this.stub.generateConsistencyTokenCallable() - .call(composeGenerateConsistencyTokenRequest(tableId))); + return TableAdminResponses.convertTokenResponse( + this.stub + .generateConsistencyTokenCallable() + .call(composeGenerateConsistencyTokenRequest(tableId))); } /** - * Generates a token to verify the replication status of table mutations invoked before this call asynchronously - * Token expires in 90 days - * + * Generates a token to verify the replication status of table mutations invoked before this call + * asynchronously Token expires in 90 days + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.generateConsistencyToken("tableId");
+   *        client.generateConsistencyToken("tableId");
    *   }
    * 
- * + * * @param tableId * @return ApiFuture */ public ApiFuture generateConsistencyTokenAsync(String tableId) { ApiFuture tokenResp = - this.stub.generateConsistencyTokenCallable() - .futureCall(composeGenerateConsistencyTokenRequest(tableId)); + this.stub + .generateConsistencyTokenCallable() + .futureCall(composeGenerateConsistencyTokenRequest(tableId)); - return ApiFutures.transform(tokenResp, + return ApiFutures.transform( + tokenResp, new ApiFunction() { @Override public ConsistencyToken apply(GenerateConsistencyTokenResponse input) { @@ -547,33 +580,36 @@ public ConsistencyToken apply(GenerateConsistencyTokenResponse input) { /** * Checks replication consistency for the specified token consistency token - * + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.isConsistent("tableId", token);
+   *        client.isConsistent("tableId", token);
    *   }
    * 
- * + * * @param tableId * @param token * @return boolean */ public boolean isConsistent(String tableId, ConsistencyToken token) { - return stub.checkConsistencyCallable().call(token.toProto(getTableName(tableId))) - .getConsistent(); + return stub.checkConsistencyCallable() + .call(token.toProto(getTableName(tableId))) + .getConsistent(); } /** * Checks replication consistency for the specified token consistency token asynchronously - * + * *

Sample code: - *

 
+   *
+   * 

    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *  client.isConsistentAsync("tableId", token);
+   *        client.isConsistentAsync("tableId", token);
    *   }
    * 
- * + * * @param tableId * @param token * @return ApiFuture @@ -582,7 +618,8 @@ public ApiFuture isConsistentAsync(String tableId, ConsistencyToken tok ApiFuture CheckConsResp = stub.checkConsistencyCallable().futureCall(token.toProto(getTableName(tableId))); - return ApiFutures.transform(CheckConsResp, + return ApiFutures.transform( + CheckConsResp, new ApiFunction() { @Override public Boolean apply(CheckConsistencyResponse input) { @@ -594,7 +631,7 @@ public Boolean apply(CheckConsistencyResponse input) { /** * Helper method to construct the table name in format: * projects/{project}/instances/{instance}/tables/{tableId} - * + * * @param tableId * @return String - unique table name */ @@ -605,7 +642,7 @@ String getTableName(String tableId) { /** * Helper method to build an instance of ListTablesRequest - * + * * @return ListTablesRequest */ @VisibleForTesting @@ -615,7 +652,7 @@ ListTablesRequest composeListTableRequest() { /** * Helper method to build an instance of GetTableRequest - * + * * @param tableId * @return GetTableRequest */ @@ -626,7 +663,7 @@ GetTableRequest composeGetTableRequest(String tableId) { /** * Helper method to build an instance of DeleteTableRequest - * + * * @param tableId * @return DeleteTableRequest */ @@ -637,15 +674,15 @@ DeleteTableRequest composeDeleteTableRequest(String tableId) { /** * Helper method to build an instance of DropRowRangeRequest - * + * * @param tableId * @param rowKeyPrefix * @param boolean dropAll * @return DropRowRangeRequest */ @VisibleForTesting - DropRowRangeRequest composeDropRowRangeRequest(String tableId, ByteString rowKeyPrefix, - boolean dropAll) { + DropRowRangeRequest composeDropRowRangeRequest( + String tableId, ByteString rowKeyPrefix, boolean dropAll) { Builder dropRowReq = DropRowRangeRequest.newBuilder().setName(getTableName(tableId)); if (dropAll) { @@ -658,7 +695,7 @@ DropRowRangeRequest composeDropRowRangeRequest(String tableId, ByteString rowKey /** * Helper method to build an instance of GenerateConsistencyTokenRequest - * + * * @param tableId * @return GenerateConsistencyTokenRequest */ @@ -669,7 +706,7 @@ GenerateConsistencyTokenRequest composeGenerateConsistencyTokenRequest(String ta /** * Helper method to convert ListTablesResponse to List - * + * * @param listTablesResponse * @return List */ @@ -685,14 +722,15 @@ static List convertToTableNames(ListTablesResponse listTablesResponse /** * Helper method to transform ApiFuture to ApiFuture
- * + * * @param future * @return ApiFuture
*/ @VisibleForTesting static ApiFuture
transformToTableResponse( ApiFuture future) { - return ApiFutures.transform(future, + return ApiFutures.transform( + future, new ApiFunction() { @Override public Table apply(com.google.bigtable.admin.v2.Table table) { @@ -703,17 +741,19 @@ public Table apply(com.google.bigtable.admin.v2.Table table) { /** * Helper method to transform ApiFuture to ApiFuture - * + * * @param future * @return ApiFuture */ @VisibleForTesting static ApiFuture transformToVoid(ApiFuture future) { - return ApiFutures.transform(future, new ApiFunction() { - @Override - public Void apply(Empty empty) { - return null; - } - }); + return ApiFutures.transform( + future, + new ApiFunction() { + @Override + public Void apply(Empty empty) { + return null; + } + }); } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java index d9158ee3f6b9..793d0868788f 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java @@ -1,15 +1,17 @@ /* * Copyright 2018 Google LLC * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.google.cloud.bigtable.admin.v2.models; @@ -25,9 +27,7 @@ import com.google.bigtable.admin.v2.GcRule.Union; import com.google.common.base.MoreObjects; -/** - * Wraps {@link GcRule} protocol buffer object and exposes a simpler Fluent DSL model - */ +/** Wraps {@link GcRule} protocol buffer object and exposes a simpler Fluent DSL model */ @BetaApi public final class GCRules { /** Factory method to create GCRules - entry point into the DSL. */ @@ -37,7 +37,7 @@ private GCRules() {} /** * Creates a new instance of the IntersectionRule - * + * * @return IntersectionRule */ public IntersectionRule intersection() { @@ -46,7 +46,7 @@ public IntersectionRule intersection() { /** * Creates a new instance of the IntersectionRule - * + * * @return UnionRule */ public UnionRule union() { @@ -55,7 +55,7 @@ public UnionRule union() { /** * Creates a new instance of the VersionRule - * + * * @param maxVersion - maximum number of cell versions to keep * @return VersionRule */ @@ -65,7 +65,7 @@ public VersionRule maxVersions(int maxVersion) { /** * Creates a new instance of the DurationRule - * + * * @param maxAge - maximum age of the cell to keep * @param timeUnit - timeunit for the age * @return DurationRule @@ -78,7 +78,7 @@ public DurationRule maxAge(long maxAge, TimeUnit timeUnit) { /** * Creates a new instance of the DurationRule - * + * * @param duration - age expressed as duration * @return DurationRule */ @@ -88,7 +88,7 @@ public DurationRule maxAge(Duration duration) { /** * Creates an empty default rule - * + * * @return DefaultRule */ public DefaultRule defaulRule() { @@ -96,20 +96,20 @@ public DefaultRule defaulRule() { } /** - * Fluent wrapper for {@link Intersection} rule. - * Allows far adding an hierarchy of rules with intersection as the root + * Fluent wrapper for {@link Intersection} rule. Allows far adding an hierarchy of rules with + * intersection as the root */ public static final class IntersectionRule extends BaseRule { private GcRule.Intersection.Builder builder; private List rulesList = new ArrayList<>(); - + private IntersectionRule() { this.builder = GcRule.Intersection.newBuilder(); } /** * Adds a new GCRule - * + * * @param rule * @return IntersectionRule */ @@ -121,20 +121,18 @@ public IntersectionRule rule(@Nonnull GCRule rule) { /** * Gets the list of child rules - * + * * @return List */ public List getRulesList() { return rulesList; } - + @Override public String toString() { - return MoreObjects.toStringHelper(this) - .add("rulesList", rulesList) - .toString(); + return MoreObjects.toStringHelper(this).add("rulesList", rulesList).toString(); } - + @InternalApi @Override public GcRule toProto() { @@ -150,20 +148,20 @@ public GcRule toProto() { } /** - * Fluent wrapper for {@link Union} rule. - * Allows far adding an hierarchy of rules with union as the root + * Fluent wrapper for {@link Union} rule. Allows far adding an hierarchy of rules with union as + * the root */ public static final class UnionRule extends BaseRule { private GcRule.Union.Builder builder; private List rulesList = new ArrayList<>(); - + private UnionRule() { this.builder = GcRule.Union.newBuilder(); } /** * Adds a new GCRule - * + * * @param rule * @return UnionRule */ @@ -175,18 +173,16 @@ public UnionRule rule(@Nonnull GCRule rule) { /** * Gets the list of child rules - * + * * @return List */ public List getRulesList() { return rulesList; } - + @Override public String toString() { - return MoreObjects.toStringHelper(this) - .add("rulesList", rulesList) - .toString(); + return MoreObjects.toStringHelper(this).add("rulesList", rulesList).toString(); } @InternalApi @@ -203,9 +199,7 @@ public GcRule toProto() { } } - /** - * Wrapper for building max versions rule - */ + /** Wrapper for building max versions rule */ public static final class VersionRule extends BaseRule { private GcRule.Builder builder; @@ -214,18 +208,14 @@ private VersionRule(int maxVersion) { builder.setMaxNumVersions(maxVersion); } - /** - * Gets the configured maximum versions - */ + /** Gets the configured maximum versions */ public int getMaxVersions() { return toProto().getMaxNumVersions(); } - + @Override public String toString() { - return MoreObjects.toStringHelper(this) - .add("maxNumVersions", getMaxVersions()) - .toString(); + return MoreObjects.toStringHelper(this).add("maxNumVersions", getMaxVersions()).toString(); } @InternalApi @@ -235,9 +225,7 @@ public GcRule toProto() { } } - /** - * Wrapper for building max duration rule - */ + /** Wrapper for building max duration rule */ public static final class DurationRule extends BaseRule { private com.google.protobuf.Duration.Builder builder; @@ -247,23 +235,21 @@ private DurationRule(Duration duration) { .setSeconds(duration.getSeconds()) .setNanos(duration.getNano()); } - + /** * Gets the configured maximum age - * + * * @return Duration */ public Duration getMaxAge() { long seconds = toProto().getMaxAge().getSeconds(); int nanos = toProto().getMaxAge().getNanos(); - return Duration.ofSeconds(seconds, nanos); + return Duration.ofSeconds(seconds, nanos); } @Override public String toString() { - return MoreObjects.toStringHelper(this) - .add("maxAge", getMaxAge()) - .toString(); + return MoreObjects.toStringHelper(this).add("maxAge", getMaxAge()).toString(); } @InternalApi @@ -273,10 +259,7 @@ public GcRule toProto() { } } - /** - * Wrapper for building a empty rule - * - */ + /** Wrapper for building a empty rule */ public static final class DefaultRule extends BaseRule { private DefaultRule() {} @@ -285,63 +268,47 @@ private DefaultRule() {} public GcRule toProto() { return GcRule.getDefaultInstance(); } - + @Override public String toString() { - return MoreObjects.toStringHelper(this) - .toString(); + return MoreObjects.toStringHelper(this).toString(); } } - /** - * Helpers to support casting rules the Type, when the Type is known - */ - public static abstract class BaseRule implements GCRule { - - /** - * Casts the rule to DurationRule. - * On failure throws a ClassCastException - */ + /** Helpers to support casting rules the Type, when the Type is known */ + public abstract static class BaseRule implements GCRule { + + /** Casts the rule to DurationRule. On failure throws a ClassCastException */ public DurationRule getDurationOrThow() { return (DurationRule) this; } - /** - * Casts the rule to VersionRule. - * On failure throws a ClassCastException - */ + /** Casts the rule to VersionRule. On failure throws a ClassCastException */ public VersionRule getVersionOrThow() { return (VersionRule) this; } - /** - * Casts the rule to UnionRule. - * On failure throws a ClassCastException - */ + /** Casts the rule to UnionRule. On failure throws a ClassCastException */ public UnionRule getUnionOrThow() { return (UnionRule) this; } - /** - * Casts the rule to IntersectionRule. - * On failure throws a ClassCastException - */ + /** Casts the rule to IntersectionRule. On failure throws a ClassCastException */ public IntersectionRule getIntersectionOrThow() { return (IntersectionRule) this; } } - - /** - * - * interface for fluent GcRule wrappers - * - */ + + /** interface for fluent GcRule wrappers */ public interface GCRule { DurationRule getDurationOrThow(); + VersionRule getVersionOrThow(); + UnionRule getUnionOrThow(); + IntersectionRule getIntersectionOrThow(); - + @InternalApi GcRule toProto(); } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java index 7515ed361386..9ee75cae30d4 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java @@ -1,15 +1,17 @@ /* * Copyright 2018 Google LLC * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.google.cloud.bigtable.admin.v2.models; @@ -29,18 +31,14 @@ import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; -/** - * - * Fluent DSL models to build Bigtable Table admin requests - * - */ +/** Fluent DSL models to build Bigtable Table admin requests */ @BetaApi public final class TableAdminRequests { private TableAdminRequests() {} /** * Factory method to get an instance of CreateTable - * + * * @param tableId - the id of the table to create * @return CreateTable */ @@ -50,7 +48,7 @@ public static CreateTable createTable(String tableId) { /** * Factory method to get an instance of ModifyFamilies - * + * * @param tableId - the id of the table to create * @return ModifyFamilies */ @@ -60,13 +58,13 @@ public static ModifyFamilies modifyFamilies(String tableId) { /** * Fluent wrapper for {@link CreateTableRequest} - * - *
 
-   * Allows for creating table with 
-   *    - optional columnFamilies, including optional {@link GCRule} 
+   *
+   * 
+   * Allows for creating table with
+   *    - optional columnFamilies, including optional {@link GCRule}
    *    - optional granularity
    *    - and optional split points
-   * 
+ *
*/ public static final class CreateTable { private final CreateTableRequest.Builder createTableRequest = CreateTableRequest.newBuilder(); @@ -74,7 +72,7 @@ public static final class CreateTable { /** * Configures table with the specified id - * + * * @param tableId */ private CreateTable(String tableId) { @@ -83,7 +81,7 @@ private CreateTable(String tableId) { /** * Configures table with the specified granularity - * + * * @param granularity */ public CreateTable withGranularity(TimestampGranularity granularity) { @@ -94,7 +92,7 @@ public CreateTable withGranularity(TimestampGranularity granularity) { /** * Adds a new columnFamily to the configuration - * + * * @param familyId */ public CreateTable addColumnFamily(String familyId) { @@ -105,7 +103,7 @@ public CreateTable addColumnFamily(String familyId) { /** * Adds a new columnFamily with {@link GCRule} to the configuration - * + * * @param familyId * @param gcRule */ @@ -118,7 +116,7 @@ public CreateTable addColumnFamily(String familyId, GCRule gcRule) { /** * Adds split at the specified key to the configuration - * + * * @param key */ public CreateTable addSplit(ByteString key) { @@ -129,7 +127,7 @@ public CreateTable addSplit(ByteString key) { /** * Adds the specified number of uniform splits to the configuration - * + * * @param key * @param numSplits */ @@ -151,13 +149,13 @@ public CreateTableRequest toProto(InstanceName instanceName) { /** * Fluent wrapper for {@link ModifyColumnFamiliesRequest} - * - *
 
-   * Allows the following ColumnFamily modifications 
-   *    - create family, optionally with {@link GCRule} 
+   *
+   * 
+   * Allows the following ColumnFamily modifications
+   *    - create family, optionally with {@link GCRule}
    *    - update existing family {@link GCRule}
    *    - drop an existing family
-   * 
+ *
*/ public static final class ModifyFamilies { private final ModifyColumnFamiliesRequest.Builder modFamilyRequest = @@ -166,7 +164,7 @@ public static final class ModifyFamilies { /** * Configures the tableId to execute the modifications - * + * * @param tableId */ private ModifyFamilies(String tableId) { @@ -176,6 +174,7 @@ private ModifyFamilies(String tableId) { /** * Configures the name of the new ColumnFamily to be created + * * @param familyId * @return */ @@ -185,7 +184,7 @@ public ModifyFamilies create(String familyId) { /** * Configures the name and GcRule of the new ColumnFamily to be created - * + * * @param familyId * @param gcRule * @return @@ -200,7 +199,7 @@ public ModifyFamilies createWithGCRule(String familyId, GCRule gcRule) { /** * Updates the GCRule of existing ColumnFamily - * + * * @param familyId * @param gcRule * @return @@ -215,7 +214,7 @@ public ModifyFamilies updateWithGCRule(String familyId, GCRule gcRule) { /** * Drops the specified ColumnFamily - * + * * @param familyId * @return */ @@ -229,7 +228,8 @@ public ModifyFamilies drop(String familyId) { @InternalApi public ModifyColumnFamiliesRequest toProto(InstanceName instanceName) { Preconditions.checkNotNull(instanceName); - String tableName = TableName.of(instanceName.getProject(), instanceName.getInstance(), tableId).toString(); + String tableName = + TableName.of(instanceName.getProject(), instanceName.getInstance(), tableId).toString(); return modFamilyRequest.setName(tableName).build(); } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java index dcfeb3127ca0..c2305dfa7007 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java @@ -1,15 +1,17 @@ /* * Copyright 2018 Google LLC * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.google.cloud.bigtable.admin.v2.models; @@ -36,18 +38,14 @@ import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; -/** - * - * Bigtable Table admin response wrappers - * - */ +/** Bigtable Table admin response wrappers */ @BetaApi public class TableAdminResponses { private TableAdminResponses() {} /** * Converts the protocol buffer table to a simpler Table model with only the required elements - * + * * @param com.google.bigtable.admin.v2.Table - Protobuf table * @return Table - Table response wrapper */ @@ -57,20 +55,18 @@ public static Table convertTable(com.google.bigtable.admin.v2.Table table) { } /** - * Converts the protocol buffer response to a simpler ConsistencyToken which can be passed back as is. - * + * Converts the protocol buffer response to a simpler ConsistencyToken which can be passed back as + * is. + * * @param GenerateConsistencyTokenResponse - Protobuf ConsistencyTokenResponse - * @return ConsistencyToken - ConsistencyToken response wrapper + * @return ConsistencyToken - ConsistencyToken response wrapper */ public static ConsistencyToken convertTokenResponse( GenerateConsistencyTokenResponse tokenResponse) { return new ConsistencyToken(tokenResponse); } - /** - * Wrapper for {@link Table} protocol buffer object - * - */ + /** Wrapper for {@link Table} protocol buffer object */ public static final class Table { private TableName tableName; private TimestampGranularity timestampGranularity; @@ -82,24 +78,26 @@ private Table(com.google.bigtable.admin.v2.Table table) { this.tableName = TableName.parse(table.getName()); this.timestampGranularity = table.getGranularity(); - Map clusterStatesMap = table.getClusterStatesMap(); - for (Entry entry : clusterStatesMap.entrySet()) { - clusterStates.put( - entry.getKey(), new ClusterState(entry.getKey(), entry.getValue())); + Map clusterStatesMap = + table.getClusterStatesMap(); + for (Entry entry : + clusterStatesMap.entrySet()) { + clusterStates.put(entry.getKey(), new ClusterState(entry.getKey(), entry.getValue())); } - Map columnFamiliesMap = table.getColumnFamiliesMap(); - for (Entry entry : columnFamiliesMap.entrySet()) { - columnFamilies.put( - entry.getKey(), new ColumnFamily(entry.getKey(), entry.getValue())); + Map columnFamiliesMap = + table.getColumnFamiliesMap(); + for (Entry entry : + columnFamiliesMap.entrySet()) { + columnFamilies.put(entry.getKey(), new ColumnFamily(entry.getKey(), entry.getValue())); } } /** * Gets the unique name of the table in the format: * projects/{project}/instances/{instance}/tables/{tableId} - * - * @return TableName + * + * @return TableName */ public TableName getTableName() { return tableName; @@ -107,7 +105,7 @@ public TableName getTableName() { /** * Gets the timestampGranularity of the table - * + * * @return TimestampGranularity */ public TimestampGranularity getTimestampGranularity() { @@ -115,8 +113,9 @@ public TimestampGranularity getTimestampGranularity() { } /** - * Returns state of the table by clusters in the instance as map of clusterId and {@link ClusterState} - * + * Returns state of the table by clusters in the instance as map of clusterId and {@link + * ClusterState} + * * @return Map */ public Map getClusterStatesMap() { @@ -125,7 +124,7 @@ public Map getClusterStatesMap() { /** * Returns a map of columfamilies in the table keyed by columnfamily and name - * + * * @return Map */ public Map getColumnFamiliesMap() { @@ -134,7 +133,7 @@ public Map getColumnFamiliesMap() { /** * Returns state of the table by clusters in the instance as a Collection - * + * * @return Collection */ public Collection getClusterStates() { @@ -143,7 +142,7 @@ public Collection getClusterStates() { /** * Returns all columnfamilies in the table as a Collection - * + * * @return */ public Collection getColumnFamiles() { @@ -161,9 +160,7 @@ public String toString() { } } - /** - * Wrapper for {@link ClusterState} protocol buffer object - */ + /** Wrapper for {@link ClusterState} protocol buffer object */ public static final class ClusterState { private String id; private ReplicationState replicationState; @@ -175,7 +172,7 @@ private ClusterState(String id, com.google.bigtable.admin.v2.Table.ClusterState /** * Gets the cluster Id - * + * * @return String */ public String getId() { @@ -184,7 +181,7 @@ public String getId() { /** * Gets the ReplicationState of the table for this cluster - * + * * @return ReplicationState */ public ReplicationState getReplicationState() { @@ -217,6 +214,7 @@ private ColumnFamily(String id, com.google.bigtable.admin.v2.ColumnFamily cf) { /** * Gets the columnfamily name + * * @return String */ public String getId() { @@ -225,7 +223,7 @@ public String getId() { /** * Get's the GCRule configured for the columnfamily - * + * * @return GCRule */ public GCRule getGCRule() { @@ -234,6 +232,7 @@ public GCRule getGCRule() { /** * Returns true if a GCRule has been configured for the family + * * @return */ public boolean hasGcRule() { @@ -242,10 +241,7 @@ public boolean hasGcRule() { @Override public String toString() { - return MoreObjects.toStringHelper(this) - .add("id", id) - .add("gCRule", gCRule) - .toString(); + return MoreObjects.toStringHelper(this).add("id", id).add("gCRule", gCRule).toString(); } private GCRule convertGcRule(GcRule source) { @@ -277,9 +273,11 @@ private GCRule convertGcRule(GcRule source) { } } - /** + /** * Wrapper for {@link GenerateConsistencyTokenResponse#getConsistencyToken()} - * Cannot be created, they are obtained by invoking {@link TableAdminClient#generateConsistencyToken(String)} + * + *

Cannot be created. + * They are obtained by invoking {@link TableAdminClient#generateConsistencyToken(String)} */ public static final class ConsistencyToken { private String token; @@ -290,10 +288,12 @@ private ConsistencyToken(GenerateConsistencyTokenResponse resp) { @InternalApi public CheckConsistencyRequest toProto(String tableName) { - return CheckConsistencyRequest.newBuilder().setName(tableName).setConsistencyToken(token) - .build(); + return CheckConsistencyRequest.newBuilder() + .setName(tableName) + .setConsistencyToken(token) + .build(); } - + @VisibleForTesting String getToken() { return token; diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java index 259a15a8a69a..9b73d0aa6af4 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.google.cloud.bigtable.admin.v2; import static com.google.common.truth.Truth.assertThat; @@ -234,17 +249,15 @@ public void generateAndCheckConsistency() { ConsistencyToken consistencyToken = adminClient.generateConsistencyToken("tableId"); Mockito.verify(mockGenerateConsistencyTokenCallable) .call(adminClient.composeGenerateConsistencyTokenRequest("tableId")); - - ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(CheckConsistencyRequest.class); + + ArgumentCaptor requestCaptor = + ArgumentCaptor.forClass(CheckConsistencyRequest.class); CheckConsistencyResponse consistencyResp = CheckConsistencyResponse.newBuilder().build(); - Mockito.when( - mockCheckConsistencyCallable.call( - any(CheckConsistencyRequest.class))) + Mockito.when(mockCheckConsistencyCallable.call(any(CheckConsistencyRequest.class))) .thenReturn(consistencyResp); adminClient.isConsistent("tableId", consistencyToken); - Mockito.verify(mockCheckConsistencyCallable) - .call(requestCaptor.capture()); + Mockito.verify(mockCheckConsistencyCallable).call(requestCaptor.capture()); } @Test @@ -256,21 +269,20 @@ public void generateAndCheckConsistencyAsync() throws Exception { adminClient.composeGenerateConsistencyTokenRequest("tableId"))) .thenReturn(genResp); - ApiFuture consistencyTokenFuture = adminClient.generateConsistencyTokenAsync("tableId"); + ApiFuture consistencyTokenFuture = + adminClient.generateConsistencyTokenAsync("tableId"); Mockito.verify(mockGenerateConsistencyTokenCallable) .futureCall(adminClient.composeGenerateConsistencyTokenRequest("tableId")); - - ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(CheckConsistencyRequest.class); + + ArgumentCaptor requestCaptor = + ArgumentCaptor.forClass(CheckConsistencyRequest.class); ApiFuture consistencyResp = ApiFutures.immediateFuture(CheckConsistencyResponse.newBuilder().build()); - Mockito.when( - mockCheckConsistencyCallable.futureCall( - any(CheckConsistencyRequest.class))) + Mockito.when(mockCheckConsistencyCallable.futureCall(any(CheckConsistencyRequest.class))) .thenReturn(consistencyResp); adminClient.isConsistentAsync("tableId", consistencyTokenFuture.get()); - Mockito.verify(mockCheckConsistencyCallable) - .futureCall(requestCaptor.capture()); + Mockito.verify(mockCheckConsistencyCallable).futureCall(requestCaptor.capture()); } @Test diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java index 5635bc4af11a..d5f539b2ab4e 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java @@ -69,7 +69,13 @@ public void createTable() throws Exception { assertFalse(tableResponse.getColumnFamiliesMap().get("cf1").hasGcRule()); assertTrue(tableResponse.getColumnFamiliesMap().get("cf2").hasGcRule()); assertEquals( - 10, tableResponse.getColumnFamiliesMap().get("cf2").getGCRule().getVersionOrThow().getMaxVersions()); + 10, + tableResponse + .getColumnFamiliesMap() + .get("cf2") + .getGCRule() + .getVersionOrThow() + .getMaxVersions()); assertEquals(TimestampGranularity.MILLIS, tableResponse.getTimestampGranularity()); // TODO: is there a way to test splits here? } finally { @@ -84,8 +90,7 @@ public void modifyFamilies() { Duration.ofSeconds(1000); modifyFamiliesReq .create("mf1") - .createWithGCRule( - "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000))) + .createWithGCRule("mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000))) .updateWithGCRule( "mf1", GCRULES @@ -112,15 +117,51 @@ public void modifyFamilies() { assertEquals(5, tableResponse.getColumnFamiles().size()); assertNotNull(tableResponse.getColumnFamiliesMap().get("mf1")); assertNotNull(tableResponse.getColumnFamiliesMap().get("mf2")); - assertEquals(2, tableResponse.getColumnFamiliesMap().get("mf1").getGCRule().getUnionOrThow().getRulesList().size()); + assertEquals( + 2, + tableResponse + .getColumnFamiliesMap() + .get("mf1") + .getGCRule() + .getUnionOrThow() + .getRulesList() + .size()); assertEquals( 1000, - tableResponse.getColumnFamiliesMap().get("mf2").getGCRule().getDurationOrThow().getMaxAge().getSeconds()); + tableResponse + .getColumnFamiliesMap() + .get("mf2") + .getGCRule() + .getDurationOrThow() + .getMaxAge() + .getSeconds()); assertEquals( 20000, - tableResponse.getColumnFamiliesMap().get("mf2").getGCRule().getDurationOrThow().getMaxAge().getNano()); - assertEquals(2, tableResponse.getColumnFamiliesMap().get("mf3").getGCRule().getIntersectionOrThow().getRulesList().size()); - assertEquals(360, tableResponse.getColumnFamiliesMap().get("mf4").getGCRule().getDurationOrThow().getMaxAge().getSeconds()); + tableResponse + .getColumnFamiliesMap() + .get("mf2") + .getGCRule() + .getDurationOrThow() + .getMaxAge() + .getNano()); + assertEquals( + 2, + tableResponse + .getColumnFamiliesMap() + .get("mf3") + .getGCRule() + .getIntersectionOrThow() + .getRulesList() + .size()); + assertEquals( + 360, + tableResponse + .getColumnFamiliesMap() + .get("mf4") + .getGCRule() + .getDurationOrThow() + .getMaxAge() + .getSeconds()); assertNotNull(tableResponse.getColumnFamiliesMap().get("mf7")); } finally { tableAdmin.deleteTable(tableId); diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java index 9c800efe2028..e3dee2577159 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java @@ -1,15 +1,17 @@ /* * Copyright 2018 Google LLC * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.google.cloud.bigtable.admin.v2.models; diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java index 6bce75f0ea6e..0b5aa9562b6a 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java @@ -1,15 +1,17 @@ /* * Copyright 2018 Google LLC * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.google.cloud.bigtable.admin.v2.models; @@ -31,13 +33,13 @@ @RunWith(JUnit4.class) public class TableAdminRequestsTest { - private final InstanceName instanceName = InstanceName.of("project", "instance"); + private final InstanceName instanceName = InstanceName.of("project", "instance"); @Test public void createTable() { CreateTableRequest actual = TableAdminRequests.createTable("tableId") - .withGranularity(TimestampGranularity.MILLIS) + .withGranularity(TimestampGranularity.MILLIS) .addColumnFamily("cf1") .addColumnFamily("cf2", GCRules.GCRULES.maxVersions(1)) .addSplit(ByteString.copyFromUtf8("c")) @@ -48,7 +50,7 @@ public void createTable() { .setTableId("tableId") .setParent(InstanceName.of("project", "instance").toString()) .addInitialSplits(Split.newBuilder().setKey(ByteString.copyFromUtf8("c"))) - .setTable( + .setTable( Table.newBuilder() .setGranularity(TimestampGranularity.MILLIS) .putColumnFamilies("cf1", ColumnFamily.newBuilder().build()) @@ -80,14 +82,18 @@ public void modifyFamilies() { .createWithGCRule("cf2", GCRules.GCRULES.maxVersions(1)) .create("cf3") .updateWithGCRule("cf1", GCRules.GCRULES.maxVersions(5)) - .drop("cf3") + .drop("cf3") .toProto(instanceName); ModifyColumnFamiliesRequest expected = ModifyColumnFamiliesRequest.newBuilder() - .setName(TableName.of(instanceName.getProject(), instanceName.getInstance(), "tableId").toString()) + .setName( + TableName.of(instanceName.getProject(), instanceName.getInstance(), "tableId") + .toString()) .addModifications( - Modification.newBuilder().setId("cf1").setCreate(ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()))) + Modification.newBuilder() + .setId("cf1") + .setCreate(ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()))) .addModifications( Modification.newBuilder() .setId("cf2") @@ -95,14 +101,16 @@ public void modifyFamilies() { ColumnFamily.newBuilder() .setGcRule(GCRules.GCRULES.maxVersions(1).toProto()))) .addModifications( - Modification.newBuilder().setId("cf3").setCreate(ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()))) + Modification.newBuilder() + .setId("cf3") + .setCreate(ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()))) .addModifications( Modification.newBuilder() .setId("cf1") .setUpdate( ColumnFamily.newBuilder() .setGcRule(GCRules.GCRULES.maxVersions(5).toProto()))) - .addModifications(Modification.newBuilder().setId("cf3").setDrop(true)) + .addModifications(Modification.newBuilder().setId("cf3").setDrop(true)) .build(); assertThat(actual).isEqualTo(expected); } diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java index 0e1a2fb52cd4..76985f3a735e 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java @@ -1,15 +1,17 @@ /* * Copyright 2018 Google LLC * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.google.cloud.bigtable.admin.v2.models; @@ -134,21 +136,51 @@ public void convertTable_unionOfIntersections() { .build(); Table tableResponse = TableAdminResponses.convertTable(table); - assertThat(actual).isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGCRule().toProto()); + assertThat(actual) + .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGCRule().toProto()); assertTrue(tableResponse.getColumnFamiliesMap().get("cf3").hasGcRule()); - + GCRule parentUnion = tableResponse.getColumnFamiliesMap().get("cf3").getGCRule(); assertEquals(2, parentUnion.getUnionOrThow().getRulesList().size()); - IntersectionRule intersectionRule = parentUnion.getUnionOrThow().getRulesList().get(0).getIntersectionOrThow(); + IntersectionRule intersectionRule = + parentUnion.getUnionOrThow().getRulesList().get(0).getIntersectionOrThow(); assertEquals(2, intersectionRule.getIntersectionOrThow().getRulesList().size()); - assertEquals(1, intersectionRule.getIntersectionOrThow().getRulesList().get(0).getVersionOrThow().getMaxVersions()); - assertEquals(Duration.ofSeconds(1, 0), intersectionRule.getIntersectionOrThow().getRulesList().get(1).getDurationOrThow().getMaxAge()); - + assertEquals( + 1, + intersectionRule + .getIntersectionOrThow() + .getRulesList() + .get(0) + .getVersionOrThow() + .getMaxVersions()); + assertEquals( + Duration.ofSeconds(1, 0), + intersectionRule + .getIntersectionOrThow() + .getRulesList() + .get(1) + .getDurationOrThow() + .getMaxAge()); + intersectionRule = parentUnion.getUnionOrThow().getRulesList().get(1).getIntersectionOrThow(); assertEquals(2, intersectionRule.getIntersectionOrThow().getRulesList().size()); - assertEquals(1, intersectionRule.getIntersectionOrThow().getRulesList().get(0).getVersionOrThow().getMaxVersions()); - assertEquals(Duration.ofSeconds(1, 0), intersectionRule.getIntersectionOrThow().getRulesList().get(1).getDurationOrThow().getMaxAge()); + assertEquals( + 1, + intersectionRule + .getIntersectionOrThow() + .getRulesList() + .get(0) + .getVersionOrThow() + .getMaxVersions()); + assertEquals( + Duration.ofSeconds(1, 0), + intersectionRule + .getIntersectionOrThow() + .getRulesList() + .get(1) + .getDurationOrThow() + .getMaxAge()); } @Test @@ -192,12 +224,14 @@ public void convertTable_intersectionOfUnions() { .build(); Table tableResponse = TableAdminResponses.convertTable(table); - assertThat(actual).isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGCRule().toProto()); + assertThat(actual) + .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGCRule().toProto()); } @Test public void convertTable_Empty() { - Table tableResponse = TableAdminResponses.convertTable(com.google.bigtable.admin.v2.Table.newBuilder().build()); + Table tableResponse = + TableAdminResponses.convertTable(com.google.bigtable.admin.v2.Table.newBuilder().build()); assertNotNull(tableResponse); assertEquals( From 122f037396a9fe28e5e3a086b5028a90a71e57d5 Mon Sep 17 00:00:00 2001 From: spollapally Date: Wed, 20 Jun 2018 03:06:59 -0400 Subject: [PATCH 05/10] code review updates. --- .../bigtable/admin/v2/TableAdminClient.java | 192 +++++++++--------- .../bigtable/admin/v2/models/GCRules.java | 47 +++-- .../admin/v2/models/TableAdminRequests.java | 43 ++-- .../admin/v2/models/TableAdminResponses.java | 69 ++++--- .../admin/v2/TableAdminClientTest.java | 2 +- .../admin/v2/it/TableAdminClientIT.java | 14 +- .../bigtable/admin/v2/models/GCRulesTest.java | 78 +++++-- .../v2/models/TableAdminRequestsTest.java | 12 +- .../v2/models/TableAdminResponsesTest.java | 6 +- 9 files changed, 272 insertions(+), 191 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java index e923555ab52b..707e3e6a8bb4 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java @@ -34,7 +34,6 @@ import com.google.bigtable.admin.v2.ListTablesResponse; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; import com.google.bigtable.admin.v2.TableName; -import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.CreateTable; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.ModifyFamilies; import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses; @@ -56,18 +55,16 @@ * *

Sample code to get started: * - *


+ * 
{@code
  *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
  *     CreateTable createTableReq =
  *         TableAdminRequests.createTable("tableId")
  *             .addColumnFamily("cf1")
  *             .addColumnFamily("cf2", GCRULES.maxVersions(10))
- *             .withGranularity(TimestampGranularity.MILLIS)
  *             .addSplit(ByteString.copyFromUtf8("b"))
  *             .addSplit(ByteString.copyFromUtf8("q"));
  *     client.createTable(createTableReq);
- *   }
- * 
+ * }
* *

Note: close() needs to be called on the bigtableTableAdminClient object to clean up resources * such as threads. In the example above, try-with-resources is used, which automatically calls @@ -78,23 +75,23 @@ * *

To customize credentials: * - *


+ * 
{@code
  * BigtableTableAdminSettings bigtableTableAdminSettings =
  *     BigtableTableAdminSettings.newBuilder()
  *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
  *         .build();
  * TableAdminClient client =
  * TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings);
- * 
+ * }
* * To customize the endpoint: * - *

+ * 
{@code
  * BigtableTableAdminSettings bigtableTableAdminSettings =
  *     BigtableTableAdminSettings.newBuilder().setEndpoint(myEndpoint).build();
  * TableAdminClient client =
  * TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings);
- * 
+ * }
*/ @BetaApi public class TableAdminClient implements AutoCloseable { @@ -167,12 +164,11 @@ public void close() { } /** - * Creates a new table with the specified configuration See {@link TableAdminRequests.createTable} - * for possible configurations + * Creates a new table with the specified configuration * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *     CreateTable createTableReq =
    *         TableAdminRequests.createTable("tableId")
@@ -181,10 +177,11 @@ public void close() {
    *
    *     client.createTable(createTableReq);
    *   }
-   * 
+ * }
* * @param createTable * @return Table - the newly created table + * @see CreateTable for createTable configurations */ public Table createTable(CreateTable createTable) { com.google.bigtable.admin.v2.Table table = @@ -194,10 +191,8 @@ public Table createTable(CreateTable createTable) { /** * Creates a new table with the specified configuration asynchronously - * See {@link TableAdminRequests#createTable(String)} - * *

Sample code: - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *     CreateTable createTableReq =
    *         TableAdminRequests.createTable("tableId")
@@ -206,10 +201,11 @@ public Table createTable(CreateTable createTable) {
    *
    *     client.createTableAsync(createTableReq);
    *   }
-   * 
+ * }
* * @param createTable * @return ApiFuture
- the newly created table + * @see CreateTable for createTable configurations */ public ApiFuture
createTableAsync(CreateTable createTable) { return transformToTableResponse( @@ -217,36 +213,37 @@ public ApiFuture
createTableAsync(CreateTable createTable) { } /** - * Creates, Updates and drops ColumnFamilies as per the request See {@link - * TableAdminRequests#modifyFamilies(String)} + * Creates, Updates and drops ColumnFamilies as per the request. * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *     ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId)
-   *             .create("mf1")
-   *       .createWithGCRule(
-   *         "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000)))
-   *     .updateWithGCRule(
-   *         "mf1",
-   *         GCRULES
-   *             .union()
-   *             .rule(GCRULES.maxAge(Duration.ofSeconds(100)))
-   *             .rule(GCRULES.maxVersions(1)))
-   *             .createWithGCRule(
-   *         "mf3",
-   *         GCRULES
-   *             .intersection()
-   *             .rule(GCRULES.maxAge(Duration.ofSeconds(2000)))
-   *             .rule(GCRULES.maxVersions(10)))
-   *             .drop("mf1")
+   *        .create("mf1")
+   *        .createWithGCRule(
+   *            "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000)))
+   *        .updateWithGCRule(
+   *            "mf1",
+   *            GCRULES
+   *                .union()
+   *                .rule(GCRULES.maxAge(Duration.ofSeconds(100)))
+   *                .rule(GCRULES.maxVersions(1)))
+   *                .createWithGCRule(
+   *            "mf3",
+   *            GCRULES
+   *                .intersection()
+   *                .rule(GCRULES.maxAge(Duration.ofSeconds(2000)))
+   *                .rule(GCRULES.maxVersions(10)))
+   *                .drop("mf1")
+   *                
    *     client.modifyFamilies(modifyFamiliesReq);
    *   }
-   * 
+ * }
* * @param modifyFamily * @return Table - Modified table + * @see ModifyFamilies for modifyFamily options. */ public Table modifyFamilies(ModifyFamilies modifyFamily) { ModifyColumnFamiliesRequest modReq = modifyFamily.toProto(instanceName); @@ -256,36 +253,37 @@ public Table modifyFamilies(ModifyFamilies modifyFamily) { } /** - * Creates, Updates and drops ColumnFamilies as per the request asynchronously See {@link - * TableAdminRequests#modifyFamilies(String)} + * Creates, Updates and drops ColumnFamilies as per the request asynchronously * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *     ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId)
-   *             .create("mf1")
-   *       .createWithGCRule(
-   *         "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000)))
-   *     .updateWithGCRule(
-   *         "mf1",
-   *         GCRULES
-   *             .union()
-   *             .rule(GCRULES.maxAge(Duration.ofSeconds(100)))
-   *             .rule(GCRULES.maxVersions(1)))
-   *             .createWithGCRule(
-   *         "mf3",
-   *         GCRULES
-   *             .intersection()
-   *             .rule(GCRULES.maxAge(Duration.ofSeconds(2000)))
-   *             .rule(GCRULES.maxVersions(10)))
-   *             .drop("mf1")
+   *        .create("mf1")
+   *        .createWithGCRule(
+   *            "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000)))
+   *        .updateWithGCRule(
+   *            "mf1",
+   *            GCRULES
+   *                 .union()
+   *                .rule(GCRULES.maxAge(Duration.ofSeconds(100)))
+   *                .rule(GCRULES.maxVersions(1)))
+   *                .createWithGCRule(
+   *            "mf3",
+   *            GCRULES
+   *                 .intersection()
+   *                .rule(GCRULES.maxAge(Duration.ofSeconds(2000)))
+   *                .rule(GCRULES.maxVersions(10)))
+   *                .drop("mf1")
+   *                
    *     client.modifyFamiliesAsync(modifyFamiliesReq);
    *   }
-   * 
+ * }
* * @param modifyFamily * @return Table - Modified table + * @see ModifyFamilies for modifyFamily options. */ public ApiFuture
modifyFamiliesAsync(ModifyFamilies modifyFamily) { ModifyColumnFamiliesRequest modReq = modifyFamily.toProto(instanceName); @@ -297,11 +295,11 @@ public ApiFuture
modifyFamiliesAsync(ModifyFamilies modifyFamily) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *        client.deleteTable("tableId");
    *   }
-   * 
+ * }
* * @param tableId */ @@ -314,11 +312,11 @@ public void deleteTable(String tableId) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *        client.deleteTableAsync("tableId");
    *   }
-   * 
+ * }
* * @param tableId */ @@ -332,11 +330,11 @@ public ApiFuture deleteTableAsync(String tableId) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *        client.getTable("tableId");
    *   }
-   * 
+ * }
* * @param tableId * @return Table @@ -351,11 +349,11 @@ public Table getTable(String tableId) { * Gets the Table by tableId * *

Sample code: - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *        client.getTableAsync("tableId");
    *   }
-   * 
+ * }
* * @param tableId * @return ApiFuture
@@ -370,11 +368,11 @@ public ApiFuture
getTableAsync(String tableId) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *        client.listTables();
    *   }
-   * 
+ * }
* * @param tableId * @return List @@ -389,11 +387,11 @@ public List listTables() { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *        client.listTablesAsync();
    *   }
-   * 
+ * }
* * @param tableId * @return List @@ -417,11 +415,11 @@ public List apply(ListTablesResponse input) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *        client.dropRowRange("tableId");
    *   }
-   * 
+ * }
* * @param tableId * @param rowKeyPrefix @@ -435,11 +433,11 @@ public void dropRowRange(String tableId, String rowKeyPrefix) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *        client.dropRowRangeAsync("tableId");
    *   }
-   * 
+ * }
* * @param tableId * @param rowKeyPrefix @@ -453,11 +451,11 @@ public ApiFuture dropRowRangeAsync(String tableId, String rowKeyPrefix) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *        client.dropRowRange("tableId");
    *   }
-   * 
+ * }
* * @param tableId * @param rowKeyPrefix @@ -471,11 +469,11 @@ public void dropRowRange(String tableId, ByteString rowKeyPrefix) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
    *        client.dropRowRangeAsync("tableId");
    *   }
-   * 
+ * }
* * @param tableId * @param rowKeyPrefix @@ -492,15 +490,15 @@ public ApiFuture dropRowRangeAsync(String tableId, ByteString rowKeyPrefix * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.dropAllData("tableId");
+   *        client.dropAllRows("tableId");
    *   }
-   * 
+ * }
* * @param tableId */ - public void dropAllData(String tableId) { + public void dropAllRows(String tableId) { this.stub.dropRowRangeCallable().call(composeDropRowRangeRequest(tableId, null, true)); } @@ -509,16 +507,16 @@ public void dropAllData(String tableId) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.dropAllData("tableId");
+   *        client.dropAllRowsAsync("tableId");
    *   }
-   * 
+ * }
* * @param tableId * @return ApiFuture */ - public ApiFuture dropAllDataAsync(String tableId) { + public ApiFuture dropAllRowsAsync(String tableId) { return transformToVoid( this.stub .dropRowRangeCallable() @@ -531,11 +529,11 @@ public ApiFuture dropAllDataAsync(String tableId) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.generateConsistencyToken("tableId");
+   *        ConsistencyToken consistencyToken = client.generateConsistencyToken("tableId");
    *   }
-   * 
+ * }
* * @param tableId * @return ConsistencyToken @@ -553,11 +551,11 @@ public ConsistencyToken generateConsistencyToken(String tableId) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.generateConsistencyToken("tableId");
+   *        ConsistencyToken consistencyToken = client.generateConsistencyToken("tableId");
    *   }
-   * 
+ * }
* * @param tableId * @return ApiFuture @@ -583,11 +581,11 @@ public ConsistencyToken apply(GenerateConsistencyTokenResponse input) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.isConsistent("tableId", token);
+   *        boolean consistent = client.isConsistent("tableId", token);
    *   }
-   * 
+ * }
* * @param tableId * @param token @@ -604,11 +602,11 @@ public boolean isConsistent(String tableId, ConsistencyToken token) { * *

Sample code: * - *


+   * 
{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.isConsistentAsync("tableId", token);
+   *        boolean consistent = client.isConsistentAsync("tableId", token);
    *   }
-   * 
+ * }
* * @param tableId * @param token diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java index 793d0868788f..af463659a607 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java @@ -71,9 +71,7 @@ public VersionRule maxVersions(int maxVersion) { * @return DurationRule */ public DurationRule maxAge(long maxAge, TimeUnit timeUnit) { - Duration duration = Duration.ZERO; - TimeUnit.SECONDS.convert(maxAge, timeUnit); - return maxAge(duration); + return maxAge(Duration.ofNanos(TimeUnit.NANOSECONDS.convert(maxAge, timeUnit))); } /** @@ -100,11 +98,10 @@ public DefaultRule defaulRule() { * intersection as the root */ public static final class IntersectionRule extends BaseRule { - private GcRule.Intersection.Builder builder; - private List rulesList = new ArrayList<>(); + private final List rulesList; private IntersectionRule() { - this.builder = GcRule.Intersection.newBuilder(); + rulesList = new ArrayList<>(); } /** @@ -115,7 +112,6 @@ private IntersectionRule() { */ public IntersectionRule rule(@Nonnull GCRule rule) { rulesList.add(rule); - builder.addRules(rule.toProto()); return this; } @@ -136,13 +132,16 @@ public String toString() { @InternalApi @Override public GcRule toProto() { - switch (builder.getRulesCount()) { + switch (rulesList.size()) { case 0: return GcRule.newBuilder().build(); case 1: - return builder.getRules(0); + return rulesList.get(0).toProto(); default: - return GcRule.newBuilder().setIntersection(builder.build()).build(); + return GcRule.newBuilder() + .setIntersection( + Intersection.newBuilder().addAllRules(convertToGcRules(rulesList))) + .build(); } } } @@ -152,11 +151,10 @@ public GcRule toProto() { * the root */ public static final class UnionRule extends BaseRule { - private GcRule.Union.Builder builder; - private List rulesList = new ArrayList<>(); + private final List rulesList; private UnionRule() { - this.builder = GcRule.Union.newBuilder(); + rulesList = new ArrayList<>(); } /** @@ -167,7 +165,6 @@ private UnionRule() { */ public UnionRule rule(@Nonnull GCRule rule) { rulesList.add(rule); - builder.addRules(rule.toProto()); return this; } @@ -188,20 +185,23 @@ public String toString() { @InternalApi @Override public GcRule toProto() { - switch (builder.getRulesCount()) { + switch (rulesList.size()) { case 0: return GcRule.newBuilder().build(); case 1: - return builder.getRules(0); + return rulesList.get(0).toProto(); default: - return GcRule.newBuilder().setUnion(builder.build()).build(); + return GcRule.newBuilder() + .setUnion( + Union.newBuilder().addAllRules(convertToGcRules(rulesList))) + .build(); } } } /** Wrapper for building max versions rule */ public static final class VersionRule extends BaseRule { - private GcRule.Builder builder; + private final GcRule.Builder builder; private VersionRule(int maxVersion) { this.builder = GcRule.newBuilder(); @@ -227,7 +227,7 @@ public GcRule toProto() { /** Wrapper for building max duration rule */ public static final class DurationRule extends BaseRule { - private com.google.protobuf.Duration.Builder builder; + private final com.google.protobuf.Duration.Builder builder; private DurationRule(Duration duration) { this.builder = @@ -312,4 +312,13 @@ public interface GCRule { @InternalApi GcRule toProto(); } + + private static List convertToGcRules(List rules) { + List gcRules = new ArrayList<>(rules.size()); + + for (GCRule rule : rules) { + gcRules.add(rule.toProto()); + } + return gcRules; + } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java index 9ee75cae30d4..67141f213ca5 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java @@ -19,14 +19,13 @@ import com.google.api.core.InternalApi; import com.google.bigtable.admin.v2.ColumnFamily; import com.google.bigtable.admin.v2.CreateTableRequest; -import com.google.bigtable.admin.v2.Table; -import com.google.bigtable.admin.v2.TableName; -import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.bigtable.admin.v2.CreateTableRequest.Split; -import com.google.bigtable.admin.v2.GcRule; import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; +import com.google.bigtable.admin.v2.Table; +import com.google.bigtable.admin.v2.Table.TimestampGranularity; +import com.google.bigtable.admin.v2.TableName; import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; @@ -59,12 +58,12 @@ public static ModifyFamilies modifyFamilies(String tableId) { /** * Fluent wrapper for {@link CreateTableRequest} * - *
-   * Allows for creating table with
-   *    - optional columnFamilies, including optional {@link GCRule}
-   *    - optional granularity
-   *    - and optional split points
-   * 
+ *

Allows for creating table with: + *

    + *
  • optional columnFamilies, including optional {@link GCRule} + *
  • optional granularity + *
  • and optional split points + *
*/ public static final class CreateTable { private final CreateTableRequest.Builder createTableRequest = CreateTableRequest.newBuilder(); @@ -150,17 +149,17 @@ public CreateTableRequest toProto(InstanceName instanceName) { /** * Fluent wrapper for {@link ModifyColumnFamiliesRequest} * - *
-   * Allows the following ColumnFamily modifications
-   *    - create family, optionally with {@link GCRule}
-   *    - update existing family {@link GCRule}
-   *    - drop an existing family
-   * 
+ *

Allows for the following ColumnFamily modifications: + *

    + *
  • create family, optionally with {@link GCRule} + *
  • update existing family {@link GCRule} + *
  • drop an existing family + *
*/ public static final class ModifyFamilies { private final ModifyColumnFamiliesRequest.Builder modFamilyRequest = ModifyColumnFamiliesRequest.newBuilder(); - private String tableId; + private final String tableId; /** * Configures the tableId to execute the modifications @@ -179,7 +178,7 @@ private ModifyFamilies(String tableId) { * @return */ public ModifyFamilies create(String familyId) { - return createWithGCRule(familyId, null); + return create(familyId, GCRules.GCRULES.defaulRule()); } /** @@ -189,10 +188,10 @@ public ModifyFamilies create(String familyId) { * @param gcRule * @return */ - public ModifyFamilies createWithGCRule(String familyId, GCRule gcRule) { + public ModifyFamilies create(String familyId, GCRule gcRule) { Modification.Builder modification = Modification.newBuilder().setId(familyId); - GcRule grule = (gcRule == null) ? GcRule.getDefaultInstance() : gcRule.toProto(); - modification.setCreate(ColumnFamily.newBuilder().setGcRule(grule)); + Preconditions.checkNotNull(gcRule); + modification.setCreate(ColumnFamily.newBuilder().setGcRule(gcRule.toProto())); modFamilyRequest.addModifications(modification.build()); return this; } @@ -204,7 +203,7 @@ public ModifyFamilies createWithGCRule(String familyId, GCRule gcRule) { * @param gcRule * @return */ - public ModifyFamilies updateWithGCRule(String familyId, GCRule gcRule) { + public ModifyFamilies update(String familyId, GCRule gcRule) { Modification.Builder modification = Modification.newBuilder().setId(familyId); Preconditions.checkNotNull(gcRule); modification.setUpdate(ColumnFamily.newBuilder().setGcRule(gcRule.toProto())); diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java index c2305dfa7007..715cbfe35660 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java @@ -46,7 +46,7 @@ private TableAdminResponses() {} /** * Converts the protocol buffer table to a simpler Table model with only the required elements * - * @param com.google.bigtable.admin.v2.Table - Protobuf table + * @param table - Protobuf table * @return Table - Table response wrapper */ @InternalApi @@ -58,39 +58,62 @@ public static Table convertTable(com.google.bigtable.admin.v2.Table table) { * Converts the protocol buffer response to a simpler ConsistencyToken which can be passed back as * is. * - * @param GenerateConsistencyTokenResponse - Protobuf ConsistencyTokenResponse + * @param tokenResponse - Protobuf ConsistencyTokenResponse * @return ConsistencyToken - ConsistencyToken response wrapper */ + @InternalApi public static ConsistencyToken convertTokenResponse( GenerateConsistencyTokenResponse tokenResponse) { return new ConsistencyToken(tokenResponse); } + /** + * Converts the protocol buffer response to a simpler ClusterState model with only required elements + * + * @param clusterStatesMap - Protobuf clusterStatesMap + * @return Map + */ + @InternalApi + public static Map convertClusterStates( + Map clusterStatesMap) { + Map clusterStates = new HashMap<>(); + + for (Entry entry : clusterStatesMap.entrySet()) { + clusterStates.put(entry.getKey(), new ClusterState(entry.getKey(), entry.getValue())); + } + return clusterStates; + } + + /** + * Converts the protocol buffer response to a simpler ColumnFamily model with only required elements + * + * @param columnFamiliesMap - Protobuf columnFamiliesMap + * @return Map + */ + @InternalApi + public static Map convertColumnFamilies( + Map columnFamiliesMap) { + Map columnFamilies = new HashMap<>(); + + for (Entry entry : columnFamiliesMap.entrySet()) { + columnFamilies.put(entry.getKey(), new ColumnFamily(entry.getKey(), entry.getValue())); + } + return columnFamilies; + } + /** Wrapper for {@link Table} protocol buffer object */ public static final class Table { - private TableName tableName; - private TimestampGranularity timestampGranularity; - private Map clusterStates = new HashMap<>(); - private Map columnFamilies = new HashMap<>(); + private final TableName tableName; + private final TimestampGranularity timestampGranularity; + private final Map clusterStates; + private final Map columnFamilies; private Table(com.google.bigtable.admin.v2.Table table) { Preconditions.checkNotNull(table); this.tableName = TableName.parse(table.getName()); this.timestampGranularity = table.getGranularity(); - - Map clusterStatesMap = - table.getClusterStatesMap(); - for (Entry entry : - clusterStatesMap.entrySet()) { - clusterStates.put(entry.getKey(), new ClusterState(entry.getKey(), entry.getValue())); - } - - Map columnFamiliesMap = - table.getColumnFamiliesMap(); - for (Entry entry : - columnFamiliesMap.entrySet()) { - columnFamilies.put(entry.getKey(), new ColumnFamily(entry.getKey(), entry.getValue())); - } + this.clusterStates = convertClusterStates(table.getClusterStatesMap()); + this.columnFamilies = convertColumnFamilies(table.getColumnFamiliesMap()); } /** @@ -162,8 +185,8 @@ public String toString() { /** Wrapper for {@link ClusterState} protocol buffer object */ public static final class ClusterState { - private String id; - private ReplicationState replicationState; + private final String id; + private final ReplicationState replicationState; private ClusterState(String id, com.google.bigtable.admin.v2.Table.ClusterState clusterState) { this.id = id; @@ -280,7 +303,7 @@ private GCRule convertGcRule(GcRule source) { * They are obtained by invoking {@link TableAdminClient#generateConsistencyToken(String)} */ public static final class ConsistencyToken { - private String token; + private final String token; private ConsistencyToken(GenerateConsistencyTokenResponse resp) { this.token = resp.getConsistencyToken(); diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java index 9b73d0aa6af4..0c0b60b8f2dd 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java @@ -212,7 +212,7 @@ public void getDropRowRangeRequest() { } @Test - public void getDropRowRangeRequest_dropAllData() { + public void getDropRowRangeRequestDropAllData() { DropRowRangeRequest actual = adminClient.composeDropRowRangeRequest("tableId", null, true); DropRowRangeRequest expected = diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java index d5f539b2ab4e..b6aedbe6d930 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java @@ -41,12 +41,12 @@ public class TableAdminClientIT { static TableAdminClient tableAdmin; @BeforeClass - public static void setup() throws IOException { + public static void setUp() throws IOException { tableAdmin = TableAdminClient.create(InstanceName.of("sduskis-hello-shakespear", "beam-test")); } @AfterClass - public static void cleanup() throws Exception { + public static void cleanUp() throws Exception { tableAdmin.close(); } @@ -90,20 +90,20 @@ public void modifyFamilies() { Duration.ofSeconds(1000); modifyFamiliesReq .create("mf1") - .createWithGCRule("mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000))) - .updateWithGCRule( + .create("mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000))) + .update( "mf1", GCRULES .union() .rule(GCRULES.maxAge(Duration.ofSeconds(100))) .rule(GCRULES.maxVersions(1))) - .createWithGCRule( + .create( "mf3", GCRULES .intersection() .rule(GCRULES.maxAge(Duration.ofSeconds(2000))) .rule(GCRULES.maxVersions(10))) - .createWithGCRule( + .create( "mf4", GCRULES.intersection().rule(GCRULES.maxAge(Duration.ofSeconds(360)))) .create("mf5") .create("mf6") @@ -224,7 +224,7 @@ public void dropRowRange() { try { tableAdmin.createTable(TableAdminRequests.createTable(tableId)); tableAdmin.dropRowRange(tableId, "rowPrefix"); - tableAdmin.dropAllData(tableId); + tableAdmin.dropAllRows(tableId); } finally { tableAdmin.deleteTable(tableId); } diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java index e3dee2577159..daf58014dbd6 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java @@ -15,8 +15,10 @@ */ package com.google.cloud.bigtable.admin.v2.models; +import static org.junit.Assert.*; import static com.google.cloud.bigtable.admin.v2.models.GCRules.GCRULES; import static com.google.common.truth.Truth.assertThat; +import java.util.concurrent.TimeUnit; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -24,15 +26,17 @@ import com.google.bigtable.admin.v2.GcRule; import com.google.bigtable.admin.v2.GcRule.Intersection; import com.google.bigtable.admin.v2.GcRule.Union; +import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; @RunWith(JUnit4.class) public class GCRulesTest { @Test public void duration() { - GcRule actual = GCRULES.maxAge(Duration.ofSeconds(61, 9)).toProto(); + GCRule actual = GCRULES.maxAge(Duration.ofSeconds(61, 9)); GcRule expected = buildAgeRule(61, 9); - assertThat(actual).isEqualTo(expected); + assertNotNull(actual.getDurationOrThow().getMaxAge()); + assertThat(actual.toProto()).isEqualTo(expected); } @Test @@ -49,11 +53,40 @@ public void durationNanos() { assertThat(actual).isEqualTo(expected); } + @Test + public void durationTimeUnitSeconds() { + GcRule actual = GCRULES.maxAge(1, TimeUnit.DAYS).toProto(); + GcRule expected = buildAgeRule(3600*24, 0); + assertThat(actual).isEqualTo(expected); + } + + @Test + public void durationTimeUnitMinutes() { + GcRule actual = GCRULES.maxAge(1, TimeUnit.MINUTES).toProto(); + GcRule expected = buildAgeRule(60, 0); + assertThat(actual).isEqualTo(expected); + } + + @Test + public void durationTimeUnitNanos() { + GcRule actual = GCRULES.maxAge(1, TimeUnit.NANOSECONDS).toProto(); + GcRule expected = buildAgeRule(0, 1); + assertThat(actual).isEqualTo(expected); + } + + @Test + public void durationTimeUnitNegative() { + GcRule actual = GCRULES.maxAge(-1, TimeUnit.MINUTES).toProto(); + GcRule expected = buildAgeRule(-60, 0); + assertThat(actual).isEqualTo(expected); + } + @Test public void versions() { - GcRule actual = GCRULES.maxVersions(10).toProto(); + GCRule actual = GCRULES.maxVersions(10); GcRule expected = buildVersionsRule(10); - assertThat(actual).isEqualTo(expected); + assertNotNull(actual.getVersionOrThow().getMaxVersions()); + assertThat(actual.toProto()).isEqualTo(expected); } @Test @@ -67,7 +100,6 @@ public void unionEmpty() { public void unionOne() { GcRule actual = GCRULES.union().rule(GCRULES.maxVersions(1)).toProto(); GcRule expected = buildVersionsRule(1); - ; assertThat(actual).isEqualTo(expected); } @@ -170,7 +202,7 @@ public void intersectionThree() { @Test public void unionOfIntersections() { - GcRule actual = + GCRule actual = GCRULES .union() .rule( @@ -182,8 +214,7 @@ public void unionOfIntersections() { GCRULES .intersection() .rule(GCRULES.maxVersions(1)) - .rule(GCRULES.maxAge(Duration.ofSeconds(1)))) - .toProto(); + .rule(GCRULES.maxAge(Duration.ofSeconds(1)))); GcRule expected = GcRule.newBuilder() @@ -203,12 +234,13 @@ public void unionOfIntersections() { .addRules(buildAgeRule(1, 0))))) .build(); - assertThat(actual).isEqualTo(expected); + assertEquals(2, actual.getUnionOrThow().getRulesList().size()); + assertThat(actual.toProto()).isEqualTo(expected); } @Test public void intersectionOfUnions() { - GcRule actual = + GCRule actual = GCRULES .intersection() .rule( @@ -220,8 +252,7 @@ public void intersectionOfUnions() { GCRULES .union() .rule(GCRULES.maxVersions(1)) - .rule(GCRULES.maxAge(Duration.ofSeconds(1)))) - .toProto(); + .rule(GCRULES.maxAge(Duration.ofSeconds(1)))); GcRule expected = GcRule.newBuilder() @@ -241,7 +272,28 @@ public void intersectionOfUnions() { .addRules(buildAgeRule(1, 0))))) .build(); - assertThat(actual).isEqualTo(expected); + assertEquals(2, actual.getIntersectionOrThow().getRulesList().size()); + assertThat(actual.toProto()).isEqualTo(expected); + } + + @Test(expected=ClassCastException.class) + public void getDurationOrThow() { + GCRULES.defaulRule().getDurationOrThow(); + } + + @Test(expected=ClassCastException.class) + public void getVersionOrThow() { + GCRULES.intersection().getVersionOrThow(); + } + + @Test(expected=ClassCastException.class) + public void getIntersectionOrThow() { + GCRULES.union().getIntersectionOrThow(); + } + + @Test(expected=ClassCastException.class) + public void getUnionOrThow() { + GCRULES.defaulRule().getUnionOrThow(); } public static GcRule buildAgeRule(long seconds, int nanos) { diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java index 0b5aa9562b6a..165cb0c6f7bd 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java @@ -65,12 +65,12 @@ public void createTable() { } @Test(expected = NullPointerException.class) - public void createTable_RequiredTableId() { + public void createTableRequiredTableId() { TableAdminRequests.createTable(null).toProto(instanceName); } @Test(expected = NullPointerException.class) - public void createTable_RequiredParent() { + public void createTableRequiredParent() { TableAdminRequests.createTable("tableId").toProto(null); } @@ -79,9 +79,9 @@ public void modifyFamilies() { ModifyColumnFamiliesRequest actual = TableAdminRequests.modifyFamilies("tableId") .create("cf1") - .createWithGCRule("cf2", GCRules.GCRULES.maxVersions(1)) + .create("cf2", GCRules.GCRULES.maxVersions(1)) .create("cf3") - .updateWithGCRule("cf1", GCRules.GCRULES.maxVersions(5)) + .update("cf1", GCRules.GCRULES.maxVersions(5)) .drop("cf3") .toProto(instanceName); @@ -116,12 +116,12 @@ public void modifyFamilies() { } @Test(expected = NullPointerException.class) - public void modifyFamilies_RequiredTableId() { + public void modifyFamiliesRequiredTableId() { TableAdminRequests.modifyFamilies(null).toProto(instanceName); } @Test(expected = NullPointerException.class) - public void modifyFamilies_RequiredParent() { + public void modifyFamiliesRequiredParent() { TableAdminRequests.modifyFamilies("tableId").toProto(null); } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java index 76985f3a735e..52b3306ea7d3 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java @@ -96,7 +96,7 @@ public void convertTable() { } @Test - public void convertTable_unionOfIntersections() { + public void convertTableUnionOfIntersections() { GcRule expected = GcRule.newBuilder() .setUnion( @@ -184,7 +184,7 @@ public void convertTable_unionOfIntersections() { } @Test - public void convertTable_intersectionOfUnions() { + public void convertTableIntersectionOfUnions() { GcRule actual = GcRule.newBuilder() .setIntersection( @@ -229,7 +229,7 @@ public void convertTable_intersectionOfUnions() { } @Test - public void convertTable_Empty() { + public void convertTableEmpty() { Table tableResponse = TableAdminResponses.convertTable(com.google.bigtable.admin.v2.Table.newBuilder().build()); From 7263049bf30caff10d9da33832bf6fd04f2e9897 Mon Sep 17 00:00:00 2001 From: spollapally Date: Wed, 20 Jun 2018 11:02:02 -0400 Subject: [PATCH 06/10] More codacy-bot review cleanup --- .../cloud/bigtable/admin/v2/models/TableAdminRequests.java | 7 +++---- .../bigtable/admin/v2/models/TableAdminResponses.java | 2 +- .../cloud/bigtable/admin/v2/it/TableAdminClientIT.java | 4 ++-- .../google/cloud/bigtable/admin/v2/models/GCRulesTest.java | 1 - 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java index 67141f213ca5..a28bf636d438 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java @@ -129,13 +129,12 @@ public CreateTable addSplit(ByteString key) { * * @param key * @param numSplits - */ - public CreateTable addUniformSplits(ByteString key, int numSplits) { - Preconditions.checkNotNull(key); + public CreateTable addUniformSplits(int numSplits) { // TODO: add implementation throw new UnsupportedOperationException(); } - + */ + @InternalApi public CreateTableRequest toProto(InstanceName instanceName) { Preconditions.checkNotNull(instanceName); diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java index 715cbfe35660..dbbb914eee1e 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java @@ -222,7 +222,7 @@ public String toString() { /** Wrapper for {@link ColumnFamily} protocol buffer object */ public static final class ColumnFamily { - private String id; + private final String id; private GCRule gCRule = GCRULES.defaulRule(); private ColumnFamily(String id, com.google.bigtable.admin.v2.ColumnFamily cf) { diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java index b6aedbe6d930..2e14ca4d65b0 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java @@ -41,12 +41,12 @@ public class TableAdminClientIT { static TableAdminClient tableAdmin; @BeforeClass - public static void setUp() throws IOException { + public static void createClient() throws IOException { tableAdmin = TableAdminClient.create(InstanceName.of("sduskis-hello-shakespear", "beam-test")); } @AfterClass - public static void cleanUp() throws Exception { + public static void closeClient() throws Exception { tableAdmin.close(); } diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java index daf58014dbd6..9b695904f95b 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java @@ -154,7 +154,6 @@ public void intersectionEmpty() { public void intersectionOne() { GcRule actual = GCRULES.intersection().rule(GCRULES.maxVersions(1)).toProto(); GcRule expected = buildVersionsRule(1); - ; assertThat(actual).isEqualTo(expected); } From a011712d7994ebb55b91d4a6f1d57338da107c4c Mon Sep 17 00:00:00 2001 From: spollapally Date: Wed, 20 Jun 2018 13:50:01 -0400 Subject: [PATCH 07/10] Refactor method names, remove throwOr methods, and fixed javadocs formatting. --- .../bigtable/admin/v2/TableAdminClient.java | 239 +++++++++--------- .../bigtable/admin/v2/models/GCRules.java | 52 +--- .../admin/v2/models/TableAdminRequests.java | 53 ++-- .../admin/v2/models/TableAdminResponses.java | 41 ++- .../admin/v2/it/TableAdminClientIT.java | 65 ++--- .../bigtable/admin/v2/models/GCRulesTest.java | 48 ++-- .../v2/models/TableAdminRequestsTest.java | 11 +- .../v2/models/TableAdminResponsesTest.java | 56 ++-- 8 files changed, 216 insertions(+), 349 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java index 707e3e6a8bb4..5fe82277c3d6 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java @@ -56,15 +56,16 @@ *

Sample code to get started: * *

{@code
- *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
- *     CreateTable createTableReq =
- *         TableAdminRequests.createTable("tableId")
- *             .addColumnFamily("cf1")
- *             .addColumnFamily("cf2", GCRULES.maxVersions(10))
- *             .addSplit(ByteString.copyFromUtf8("b"))
- *             .addSplit(ByteString.copyFromUtf8("q"));
- *     client.createTable(createTableReq);
- *   }
+ * try(TableAdminClient client = TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) { + * CreateTable createTableReq = + * TableAdminRequests.createTable("tableId") + * .addColumnFamily("cf1") + * .addColumnFamily("cf2", GCRULES.maxVersions(10)) + * .addSplit(ByteString.copyFromUtf8("b")) + * .addSplit(ByteString.copyFromUtf8("q")); + * client.createTable(createTableReq); + * } + * } * *

Note: close() needs to be called on the bigtableTableAdminClient object to clean up resources * such as threads. In the example above, try-with-resources is used, which automatically calls @@ -77,9 +78,9 @@ * *

{@code
  * BigtableTableAdminSettings bigtableTableAdminSettings =
- *     BigtableTableAdminSettings.newBuilder()
- *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
- *         .build();
+ *   BigtableTableAdminSettings.newBuilder()
+ *   .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
+ *     .build();
  * TableAdminClient client =
  * TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings);
  * }
@@ -169,14 +170,12 @@ public void close() { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *     CreateTable createTableReq =
-   *         TableAdminRequests.createTable("tableId")
-   *             .addColumnFamily("cf2", GCRULES.maxVersions(10))
-   *             .withGranularity(TimestampGranularity.MILLIS);
-   *
-   *     client.createTable(createTableReq);
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   CreateTable createTableReq =
+   *     TableAdminRequests.createTable("tableId")
+   *       .addColumnFamily("cf2", GCRULES.maxVersions(10))
+   *   client.createTable(createTableReq);
+   * }
    * }
* * @param createTable @@ -191,16 +190,17 @@ public Table createTable(CreateTable createTable) { /** * Creates a new table with the specified configuration asynchronously + * *

Sample code: + * *

{@code
    *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *     CreateTable createTableReq =
-   *         TableAdminRequests.createTable("tableId")
-   *             .addColumnFamily("cf2", GCRULES.maxVersions(10))
-   *             .withGranularity(TimestampGranularity.MILLIS);
+   *    CreateTable createTableReq =
+   *      TableAdminRequests.createTable("tableId")
+   *        .addColumnFamily("cf2", GCRULES.maxVersions(10))
    *
-   *     client.createTableAsync(createTableReq);
-   *   }
+   *    client.createTableAsync(createTableReq);
+   *  }
    *  }
* * @param createTable @@ -213,37 +213,36 @@ public ApiFuture
createTableAsync(CreateTable createTable) { } /** - * Creates, Updates and drops ColumnFamilies as per the request. + * Creates, Updates and drops ColumnFamilies as per the request. * *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *     ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId)
-   *        .create("mf1")
-   *        .createWithGCRule(
-   *            "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000)))
-   *        .updateWithGCRule(
-   *            "mf1",
-   *            GCRULES
-   *                .union()
-   *                .rule(GCRULES.maxAge(Duration.ofSeconds(100)))
-   *                .rule(GCRULES.maxVersions(1)))
-   *                .createWithGCRule(
-   *            "mf3",
-   *            GCRULES
-   *                .intersection()
-   *                .rule(GCRULES.maxAge(Duration.ofSeconds(2000)))
-   *                .rule(GCRULES.maxVersions(10)))
-   *                .drop("mf1")
-   *                
-   *     client.modifyFamilies(modifyFamiliesReq);
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId)
+   *     .addFamily("mf1")
+   *     .addFamily(
+   *       "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000)))
+   *     .updateFamily(
+   *       "mf1",
+   *          GCRULES
+   *            .union()
+   *              .rule(GCRULES.maxAge(Duration.ofSeconds(100)))
+   *              .rule(GCRULES.maxVersions(1)))
+   *     .addFamily(
+   *       "mf3",
+   *         GCRULES
+   *           .intersection()
+   *           .rule(GCRULES.maxAge(Duration.ofSeconds(2000)))
+   *           .rule(GCRULES.maxVersions(10)))
+   *     .dropFamily("mf1")
+   *    client.modifyFamilies(modifyFamiliesReq);
+   * }
    * }
* * @param modifyFamily * @return Table - Modified table - * @see ModifyFamilies for modifyFamily options. + * @see ModifyFamilies for modifyFamily options */ public Table modifyFamilies(ModifyFamilies modifyFamily) { ModifyColumnFamiliesRequest modReq = modifyFamily.toProto(instanceName); @@ -258,32 +257,31 @@ public Table modifyFamilies(ModifyFamilies modifyFamily) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *     ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId)
-   *        .create("mf1")
-   *        .createWithGCRule(
-   *            "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000)))
-   *        .updateWithGCRule(
-   *            "mf1",
-   *            GCRULES
-   *                 .union()
-   *                .rule(GCRULES.maxAge(Duration.ofSeconds(100)))
-   *                .rule(GCRULES.maxVersions(1)))
-   *                .createWithGCRule(
-   *            "mf3",
-   *            GCRULES
-   *                 .intersection()
-   *                .rule(GCRULES.maxAge(Duration.ofSeconds(2000)))
-   *                .rule(GCRULES.maxVersions(10)))
-   *                .drop("mf1")
-   *                
-   *     client.modifyFamiliesAsync(modifyFamiliesReq);
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId)
+   *     .addFamily("mf1")
+   *     .addFamily(
+   *       "mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000)))
+   *     .updateFamily(
+   *       "mf1",
+   *          GCRULES
+   *            .union()
+   *              .rule(GCRULES.maxAge(Duration.ofSeconds(100)))
+   *              .rule(GCRULES.maxVersions(1)))
+   *     .addFamily(
+   *       "mf3",
+   *         GCRULES
+   *           .intersection()
+   *           .rule(GCRULES.maxAge(Duration.ofSeconds(2000)))
+   *           .rule(GCRULES.maxVersions(10)))
+   *     .dropFamily("mf1")
+   *    client.modifyFamilies(modifyFamiliesReq);
+   * }
    * }
* * @param modifyFamily - * @return Table - Modified table - * @see ModifyFamilies for modifyFamily options. + * @return ApiFuture
- Modified table + * @see ModifyFamilies for modifyFamily options */ public ApiFuture
modifyFamiliesAsync(ModifyFamilies modifyFamily) { ModifyColumnFamiliesRequest modReq = modifyFamily.toProto(instanceName); @@ -296,9 +294,9 @@ public ApiFuture
modifyFamiliesAsync(ModifyFamilies modifyFamily) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.deleteTable("tableId");
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   client.deleteTable("tableId");
+   * }
    * }
* * @param tableId @@ -313,9 +311,9 @@ public void deleteTable(String tableId) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.deleteTableAsync("tableId");
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   client.deleteTableAsync("tableId");
+   * }
    * }
* * @param tableId @@ -331,9 +329,9 @@ public ApiFuture deleteTableAsync(String tableId) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.getTable("tableId");
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   client.getTable("tableId");
+   * }
    * }
* * @param tableId @@ -349,10 +347,11 @@ public Table getTable(String tableId) { * Gets the Table by tableId * *

Sample code: + * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.getTableAsync("tableId");
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   client.getTableAsync("tableId");
+   * }
    * }
* * @param tableId @@ -369,9 +368,9 @@ public ApiFuture
getTableAsync(String tableId) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.listTables();
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   client.listTables();
+   * }
    * }
* * @param tableId @@ -388,9 +387,9 @@ public List listTables() { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.listTablesAsync();
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   client.listTablesAsync();
+   * }
    * }
* * @param tableId @@ -416,9 +415,9 @@ public List apply(ListTablesResponse input) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.dropRowRange("tableId");
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   client.dropRowRange("tableId");
+   * }
    * }
* * @param tableId @@ -434,9 +433,9 @@ public void dropRowRange(String tableId, String rowKeyPrefix) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.dropRowRangeAsync("tableId");
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   client.dropRowRangeAsync("tableId");
+   * }
    * }
* * @param tableId @@ -452,9 +451,9 @@ public ApiFuture dropRowRangeAsync(String tableId, String rowKeyPrefix) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.dropRowRange("tableId");
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   client.dropRowRange("tableId");
+   * }
    * }
* * @param tableId @@ -470,9 +469,9 @@ public void dropRowRange(String tableId, ByteString rowKeyPrefix) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.dropRowRangeAsync("tableId");
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   client.dropRowRangeAsync("tableId");
+   * }
    * }
* * @param tableId @@ -491,9 +490,9 @@ public ApiFuture dropRowRangeAsync(String tableId, ByteString rowKeyPrefix *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.dropAllRows("tableId");
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   client.dropAllRows("tableId");
+   * }
    * }
* * @param tableId @@ -508,9 +507,9 @@ public void dropAllRows(String tableId) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        client.dropAllRowsAsync("tableId");
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   client.dropAllRowsAsync("tableId");
+   * }
    * }
* * @param tableId @@ -530,9 +529,9 @@ public ApiFuture dropAllRowsAsync(String tableId) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        ConsistencyToken consistencyToken = client.generateConsistencyToken("tableId");
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   ConsistencyToken consistencyToken = client.generateConsistencyToken("tableId");
+   * }
    * }
* * @param tableId @@ -552,9 +551,9 @@ public ConsistencyToken generateConsistencyToken(String tableId) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        ConsistencyToken consistencyToken = client.generateConsistencyToken("tableId");
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   ConsistencyToken consistencyToken = client.generateConsistencyToken("tableId");
+   * }
    * }
* * @param tableId @@ -582,9 +581,9 @@ public ConsistencyToken apply(GenerateConsistencyTokenResponse input) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        boolean consistent = client.isConsistent("tableId", token);
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   boolean consistent = client.isConsistent("tableId", token);
+   * }
    * }
* * @param tableId @@ -603,9 +602,9 @@ public boolean isConsistent(String tableId, ConsistencyToken token) { *

Sample code: * *

{@code
-   *  try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
-   *        boolean consistent = client.isConsistentAsync("tableId", token);
-   *   }
+   * try(TableAdminClient client =  TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
+   *   boolean consistent = client.isConsistentAsync("tableId", token);
+   * }
    * }
* * @param tableId diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java index af463659a607..70537e5d995c 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java @@ -97,7 +97,7 @@ public DefaultRule defaulRule() { * Fluent wrapper for {@link Intersection} rule. Allows far adding an hierarchy of rules with * intersection as the root */ - public static final class IntersectionRule extends BaseRule { + public static final class IntersectionRule implements GCRule { private final List rulesList; private IntersectionRule() { @@ -139,9 +139,8 @@ public GcRule toProto() { return rulesList.get(0).toProto(); default: return GcRule.newBuilder() - .setIntersection( - Intersection.newBuilder().addAllRules(convertToGcRules(rulesList))) - .build(); + .setIntersection(Intersection.newBuilder().addAllRules(convertToGcRules(rulesList))) + .build(); } } } @@ -150,7 +149,7 @@ public GcRule toProto() { * Fluent wrapper for {@link Union} rule. Allows far adding an hierarchy of rules with union as * the root */ - public static final class UnionRule extends BaseRule { + public static final class UnionRule implements GCRule { private final List rulesList; private UnionRule() { @@ -192,15 +191,14 @@ public GcRule toProto() { return rulesList.get(0).toProto(); default: return GcRule.newBuilder() - .setUnion( - Union.newBuilder().addAllRules(convertToGcRules(rulesList))) + .setUnion(Union.newBuilder().addAllRules(convertToGcRules(rulesList))) .build(); } } } /** Wrapper for building max versions rule */ - public static final class VersionRule extends BaseRule { + public static final class VersionRule implements GCRule { private final GcRule.Builder builder; private VersionRule(int maxVersion) { @@ -226,7 +224,7 @@ public GcRule toProto() { } /** Wrapper for building max duration rule */ - public static final class DurationRule extends BaseRule { + public static final class DurationRule implements GCRule { private final com.google.protobuf.Duration.Builder builder; private DurationRule(Duration duration) { @@ -260,7 +258,7 @@ public GcRule toProto() { } /** Wrapper for building a empty rule */ - public static final class DefaultRule extends BaseRule { + public static final class DefaultRule implements GCRule { private DefaultRule() {} @InternalApi @@ -275,44 +273,12 @@ public String toString() { } } - /** Helpers to support casting rules the Type, when the Type is known */ - public abstract static class BaseRule implements GCRule { - - /** Casts the rule to DurationRule. On failure throws a ClassCastException */ - public DurationRule getDurationOrThow() { - return (DurationRule) this; - } - - /** Casts the rule to VersionRule. On failure throws a ClassCastException */ - public VersionRule getVersionOrThow() { - return (VersionRule) this; - } - - /** Casts the rule to UnionRule. On failure throws a ClassCastException */ - public UnionRule getUnionOrThow() { - return (UnionRule) this; - } - - /** Casts the rule to IntersectionRule. On failure throws a ClassCastException */ - public IntersectionRule getIntersectionOrThow() { - return (IntersectionRule) this; - } - } - /** interface for fluent GcRule wrappers */ public interface GCRule { - DurationRule getDurationOrThow(); - - VersionRule getVersionOrThow(); - - UnionRule getUnionOrThow(); - - IntersectionRule getIntersectionOrThow(); - @InternalApi GcRule toProto(); } - + private static List convertToGcRules(List rules) { List gcRules = new ArrayList<>(rules.size()); diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java index a28bf636d438..3c7c653028ee 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java @@ -24,7 +24,6 @@ import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; import com.google.bigtable.admin.v2.Table; -import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.bigtable.admin.v2.TableName; import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; import com.google.common.base.Preconditions; @@ -58,11 +57,12 @@ public static ModifyFamilies modifyFamilies(String tableId) { /** * Fluent wrapper for {@link CreateTableRequest} * - *

Allows for creating table with: + *

Allows for creating table with: + * *

    - *
  • optional columnFamilies, including optional {@link GCRule} - *
  • optional granularity - *
  • and optional split points + *
  • optional columnFamilies, including optional {@link GCRule} + *
  • optional granularity + *
  • and optional split points *
*/ public static final class CreateTable { @@ -78,17 +78,6 @@ private CreateTable(String tableId) { createTableRequest.setTableId(tableId); } - /** - * Configures table with the specified granularity - * - * @param granularity - */ - public CreateTable withGranularity(TimestampGranularity granularity) { - Preconditions.checkNotNull(granularity); - tableRequest.setGranularity(granularity); - return this; - } - /** * Adds a new columnFamily to the configuration * @@ -124,17 +113,6 @@ public CreateTable addSplit(ByteString key) { return this; } - /** - * Adds the specified number of uniform splits to the configuration - * - * @param key - * @param numSplits - public CreateTable addUniformSplits(int numSplits) { - // TODO: add implementation - throw new UnsupportedOperationException(); - } - */ - @InternalApi public CreateTableRequest toProto(InstanceName instanceName) { Preconditions.checkNotNull(instanceName); @@ -148,11 +126,12 @@ public CreateTableRequest toProto(InstanceName instanceName) { /** * Fluent wrapper for {@link ModifyColumnFamiliesRequest} * - *

Allows for the following ColumnFamily modifications: - *

    - *
  • create family, optionally with {@link GCRule} - *
  • update existing family {@link GCRule} - *
  • drop an existing family + *

    Allows for the following ColumnFamily modifications: + * + *

      + *
    • create family, optionally with {@link GCRule} + *
    • update existing family {@link GCRule} + *
    • drop an existing family *
    */ public static final class ModifyFamilies { @@ -176,8 +155,8 @@ private ModifyFamilies(String tableId) { * @param familyId * @return */ - public ModifyFamilies create(String familyId) { - return create(familyId, GCRules.GCRULES.defaulRule()); + public ModifyFamilies addFamily(String familyId) { + return addFamily(familyId, GCRules.GCRULES.defaulRule()); } /** @@ -187,7 +166,7 @@ public ModifyFamilies create(String familyId) { * @param gcRule * @return */ - public ModifyFamilies create(String familyId, GCRule gcRule) { + public ModifyFamilies addFamily(String familyId, GCRule gcRule) { Modification.Builder modification = Modification.newBuilder().setId(familyId); Preconditions.checkNotNull(gcRule); modification.setCreate(ColumnFamily.newBuilder().setGcRule(gcRule.toProto())); @@ -202,7 +181,7 @@ public ModifyFamilies create(String familyId, GCRule gcRule) { * @param gcRule * @return */ - public ModifyFamilies update(String familyId, GCRule gcRule) { + public ModifyFamilies updateFamily(String familyId, GCRule gcRule) { Modification.Builder modification = Modification.newBuilder().setId(familyId); Preconditions.checkNotNull(gcRule); modification.setUpdate(ColumnFamily.newBuilder().setGcRule(gcRule.toProto())); @@ -216,7 +195,7 @@ public ModifyFamilies update(String familyId, GCRule gcRule) { * @param familyId * @return */ - public ModifyFamilies drop(String familyId) { + public ModifyFamilies dropFamily(String familyId) { Modification.Builder modification = Modification.newBuilder().setId(familyId); modification.setId(familyId).setDrop(true); modFamilyRequest.addModifications(modification.build()); diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java index dbbb914eee1e..41d391e7bdec 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java @@ -21,14 +21,13 @@ import java.util.Map; import java.util.Map.Entry; import org.threeten.bp.Duration; -import com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState; -import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.api.core.BetaApi; import com.google.api.core.InternalApi; import com.google.bigtable.admin.v2.CheckConsistencyRequest; import com.google.bigtable.admin.v2.GcRule; import com.google.bigtable.admin.v2.GcRule.RuleCase; import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; +import com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState; import com.google.bigtable.admin.v2.TableName; import com.google.cloud.bigtable.admin.v2.TableAdminClient; import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; @@ -68,25 +67,28 @@ public static ConsistencyToken convertTokenResponse( } /** - * Converts the protocol buffer response to a simpler ClusterState model with only required elements - * + * Converts the protocol buffer response to a simpler ClusterState model with only required + * elements + * * @param clusterStatesMap - Protobuf clusterStatesMap - * @return Map + * @return Map */ @InternalApi public static Map convertClusterStates( Map clusterStatesMap) { Map clusterStates = new HashMap<>(); - for (Entry entry : clusterStatesMap.entrySet()) { + for (Entry entry : + clusterStatesMap.entrySet()) { clusterStates.put(entry.getKey(), new ClusterState(entry.getKey(), entry.getValue())); } return clusterStates; } /** - * Converts the protocol buffer response to a simpler ColumnFamily model with only required elements - * + * Converts the protocol buffer response to a simpler ColumnFamily model with only required + * elements + * * @param columnFamiliesMap - Protobuf columnFamiliesMap * @return Map */ @@ -95,7 +97,8 @@ public static Map convertColumnFamilies( Map columnFamiliesMap) { Map columnFamilies = new HashMap<>(); - for (Entry entry : columnFamiliesMap.entrySet()) { + for (Entry entry : + columnFamiliesMap.entrySet()) { columnFamilies.put(entry.getKey(), new ColumnFamily(entry.getKey(), entry.getValue())); } return columnFamilies; @@ -104,14 +107,12 @@ public static Map convertColumnFamilies( /** Wrapper for {@link Table} protocol buffer object */ public static final class Table { private final TableName tableName; - private final TimestampGranularity timestampGranularity; private final Map clusterStates; private final Map columnFamilies; private Table(com.google.bigtable.admin.v2.Table table) { Preconditions.checkNotNull(table); this.tableName = TableName.parse(table.getName()); - this.timestampGranularity = table.getGranularity(); this.clusterStates = convertClusterStates(table.getClusterStatesMap()); this.columnFamilies = convertColumnFamilies(table.getColumnFamiliesMap()); } @@ -126,15 +127,6 @@ public TableName getTableName() { return tableName; } - /** - * Gets the timestampGranularity of the table - * - * @return TimestampGranularity - */ - public TimestampGranularity getTimestampGranularity() { - return timestampGranularity; - } - /** * Returns state of the table by clusters in the instance as map of clusterId and {@link * ClusterState} @@ -176,7 +168,6 @@ public Collection getColumnFamiles() { public String toString() { return MoreObjects.toStringHelper(this) .add("tableName", tableName) - .add("timestampGranularity", timestampGranularity) .add("clusterStates", getClusterStates()) .add("columnFamiles", getColumnFamiles()) .toString(); @@ -297,10 +288,10 @@ private GCRule convertGcRule(GcRule source) { } /** - * Wrapper for {@link GenerateConsistencyTokenResponse#getConsistencyToken()} - * - *

    Cannot be created. - * They are obtained by invoking {@link TableAdminClient#generateConsistencyToken(String)} + * Wrapper for {@link GenerateConsistencyTokenResponse#getConsistencyToken()} + * + *

    Cannot be created. They are obtained by invoking {@link + * TableAdminClient#generateConsistencyToken(String)} */ public static final class ConsistencyToken { private final String token; diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java index 2e14ca4d65b0..37878597fd5a 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java @@ -27,9 +27,12 @@ import org.junit.Test; import org.threeten.bp.Duration; import com.google.bigtable.admin.v2.InstanceName; -import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.bigtable.admin.v2.TableName; import com.google.cloud.bigtable.admin.v2.TableAdminClient; +import com.google.cloud.bigtable.admin.v2.models.GCRules.DurationRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.IntersectionRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.UnionRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.VersionRule; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.CreateTable; import com.google.cloud.bigtable.admin.v2.models.TableAdminRequests.ModifyFamilies; @@ -57,7 +60,6 @@ public void createTable() throws Exception { TableAdminRequests.createTable(tableId) .addColumnFamily("cf1") .addColumnFamily("cf2", GCRULES.maxVersions(10)) - .withGranularity(TimestampGranularity.MILLIS) .addSplit(ByteString.copyFromUtf8("b")) .addSplit(ByteString.copyFromUtf8("q")); @@ -70,14 +72,8 @@ public void createTable() throws Exception { assertTrue(tableResponse.getColumnFamiliesMap().get("cf2").hasGcRule()); assertEquals( 10, - tableResponse - .getColumnFamiliesMap() - .get("cf2") - .getGCRule() - .getVersionOrThow() + ((VersionRule) tableResponse.getColumnFamiliesMap().get("cf2").getGCRule()) .getMaxVersions()); - assertEquals(TimestampGranularity.MILLIS, tableResponse.getTimestampGranularity()); - // TODO: is there a way to test splits here? } finally { tableAdmin.deleteTable(tableId); } @@ -89,27 +85,26 @@ public void modifyFamilies() { ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId); Duration.ofSeconds(1000); modifyFamiliesReq - .create("mf1") - .create("mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000))) - .update( + .addFamily("mf1") + .addFamily("mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000))) + .updateFamily( "mf1", GCRULES .union() .rule(GCRULES.maxAge(Duration.ofSeconds(100))) .rule(GCRULES.maxVersions(1))) - .create( + .addFamily( "mf3", GCRULES .intersection() .rule(GCRULES.maxAge(Duration.ofSeconds(2000))) .rule(GCRULES.maxVersions(10))) - .create( - "mf4", GCRULES.intersection().rule(GCRULES.maxAge(Duration.ofSeconds(360)))) - .create("mf5") - .create("mf6") - .drop("mf5") - .drop("mf6") - .create("mf7"); + .addFamily("mf4", GCRULES.intersection().rule(GCRULES.maxAge(Duration.ofSeconds(360)))) + .addFamily("mf5") + .addFamily("mf6") + .dropFamily("mf5") + .dropFamily("mf6") + .addFamily("mf7"); try { tableAdmin.createTable(TableAdminRequests.createTable(tableId)); @@ -119,47 +114,27 @@ public void modifyFamilies() { assertNotNull(tableResponse.getColumnFamiliesMap().get("mf2")); assertEquals( 2, - tableResponse - .getColumnFamiliesMap() - .get("mf1") - .getGCRule() - .getUnionOrThow() + ((UnionRule) tableResponse.getColumnFamiliesMap().get("mf1").getGCRule()) .getRulesList() .size()); assertEquals( 1000, - tableResponse - .getColumnFamiliesMap() - .get("mf2") - .getGCRule() - .getDurationOrThow() + ((DurationRule) tableResponse.getColumnFamiliesMap().get("mf2").getGCRule()) .getMaxAge() .getSeconds()); assertEquals( 20000, - tableResponse - .getColumnFamiliesMap() - .get("mf2") - .getGCRule() - .getDurationOrThow() + ((DurationRule) tableResponse.getColumnFamiliesMap().get("mf2").getGCRule()) .getMaxAge() .getNano()); assertEquals( 2, - tableResponse - .getColumnFamiliesMap() - .get("mf3") - .getGCRule() - .getIntersectionOrThow() + ((IntersectionRule) tableResponse.getColumnFamiliesMap().get("mf3").getGCRule()) .getRulesList() .size()); assertEquals( 360, - tableResponse - .getColumnFamiliesMap() - .get("mf4") - .getGCRule() - .getDurationOrThow() + ((DurationRule) tableResponse.getColumnFamiliesMap().get("mf4").getGCRule()) .getMaxAge() .getSeconds()); assertNotNull(tableResponse.getColumnFamiliesMap().get("mf7")); diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java index 9b695904f95b..690128dfd816 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/GCRulesTest.java @@ -15,9 +15,10 @@ */ package com.google.cloud.bigtable.admin.v2.models; -import static org.junit.Assert.*; import static com.google.cloud.bigtable.admin.v2.models.GCRules.GCRULES; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import java.util.concurrent.TimeUnit; import org.junit.Test; import org.junit.runner.RunWith; @@ -26,16 +27,19 @@ import com.google.bigtable.admin.v2.GcRule; import com.google.bigtable.admin.v2.GcRule.Intersection; import com.google.bigtable.admin.v2.GcRule.Union; -import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.DurationRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.IntersectionRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.UnionRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.VersionRule; @RunWith(JUnit4.class) public class GCRulesTest { @Test public void duration() { - GCRule actual = GCRULES.maxAge(Duration.ofSeconds(61, 9)); + DurationRule actual = GCRULES.maxAge(Duration.ofSeconds(61, 9)); GcRule expected = buildAgeRule(61, 9); - assertNotNull(actual.getDurationOrThow().getMaxAge()); + assertNotNull(actual.getMaxAge()); assertThat(actual.toProto()).isEqualTo(expected); } @@ -56,10 +60,10 @@ public void durationNanos() { @Test public void durationTimeUnitSeconds() { GcRule actual = GCRULES.maxAge(1, TimeUnit.DAYS).toProto(); - GcRule expected = buildAgeRule(3600*24, 0); + GcRule expected = buildAgeRule(3600 * 24, 0); assertThat(actual).isEqualTo(expected); } - + @Test public void durationTimeUnitMinutes() { GcRule actual = GCRULES.maxAge(1, TimeUnit.MINUTES).toProto(); @@ -83,9 +87,9 @@ public void durationTimeUnitNegative() { @Test public void versions() { - GCRule actual = GCRULES.maxVersions(10); + VersionRule actual = GCRULES.maxVersions(10); GcRule expected = buildVersionsRule(10); - assertNotNull(actual.getVersionOrThow().getMaxVersions()); + assertNotNull(actual.getMaxVersions()); assertThat(actual.toProto()).isEqualTo(expected); } @@ -201,7 +205,7 @@ public void intersectionThree() { @Test public void unionOfIntersections() { - GCRule actual = + UnionRule actual = GCRULES .union() .rule( @@ -233,13 +237,13 @@ public void unionOfIntersections() { .addRules(buildAgeRule(1, 0))))) .build(); - assertEquals(2, actual.getUnionOrThow().getRulesList().size()); + assertEquals(2, actual.getRulesList().size()); assertThat(actual.toProto()).isEqualTo(expected); } @Test public void intersectionOfUnions() { - GCRule actual = + IntersectionRule actual = GCRULES .intersection() .rule( @@ -271,29 +275,9 @@ public void intersectionOfUnions() { .addRules(buildAgeRule(1, 0))))) .build(); - assertEquals(2, actual.getIntersectionOrThow().getRulesList().size()); + assertEquals(2, actual.getRulesList().size()); assertThat(actual.toProto()).isEqualTo(expected); } - - @Test(expected=ClassCastException.class) - public void getDurationOrThow() { - GCRULES.defaulRule().getDurationOrThow(); - } - - @Test(expected=ClassCastException.class) - public void getVersionOrThow() { - GCRULES.intersection().getVersionOrThow(); - } - - @Test(expected=ClassCastException.class) - public void getIntersectionOrThow() { - GCRULES.union().getIntersectionOrThow(); - } - - @Test(expected=ClassCastException.class) - public void getUnionOrThow() { - GCRULES.defaulRule().getUnionOrThow(); - } public static GcRule buildAgeRule(long seconds, int nanos) { com.google.protobuf.Duration.Builder duartionBuilder = diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java index 165cb0c6f7bd..19efd4ed7514 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java @@ -39,7 +39,6 @@ public class TableAdminRequestsTest { public void createTable() { CreateTableRequest actual = TableAdminRequests.createTable("tableId") - .withGranularity(TimestampGranularity.MILLIS) .addColumnFamily("cf1") .addColumnFamily("cf2", GCRules.GCRULES.maxVersions(1)) .addSplit(ByteString.copyFromUtf8("c")) @@ -78,11 +77,11 @@ public void createTableRequiredParent() { public void modifyFamilies() { ModifyColumnFamiliesRequest actual = TableAdminRequests.modifyFamilies("tableId") - .create("cf1") - .create("cf2", GCRules.GCRULES.maxVersions(1)) - .create("cf3") - .update("cf1", GCRules.GCRULES.maxVersions(5)) - .drop("cf3") + .addFamily("cf1") + .addFamily("cf2", GCRules.GCRULES.maxVersions(1)) + .addFamily("cf3") + .updateFamily("cf1", GCRules.GCRULES.maxVersions(5)) + .dropFamily("cf3") .toProto(instanceName); ModifyColumnFamiliesRequest expected = diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java index 52b3306ea7d3..80fc5fb92bf3 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java @@ -26,8 +26,10 @@ import com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState; import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.bigtable.admin.v2.TableName; -import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.DurationRule; import com.google.cloud.bigtable.admin.v2.models.GCRules.IntersectionRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.UnionRule; +import com.google.cloud.bigtable.admin.v2.models.GCRules.VersionRule; import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.ConsistencyToken; import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.Table; import com.google.bigtable.admin.v2.GcRule.Intersection; @@ -75,7 +77,6 @@ public void convertTable() { Table tableResponse = TableAdminResponses.convertTable(table); assertNotNull(tableResponse); assertEquals(testName, tableResponse.getTableName()); - assertEquals(TimestampGranularity.MILLIS, tableResponse.getTimestampGranularity()); assertEquals(2, tableResponse.getClusterStates().size()); assertEquals( ReplicationState.READY, @@ -140,47 +141,23 @@ public void convertTableUnionOfIntersections() { .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGCRule().toProto()); assertTrue(tableResponse.getColumnFamiliesMap().get("cf3").hasGcRule()); - GCRule parentUnion = tableResponse.getColumnFamiliesMap().get("cf3").getGCRule(); - assertEquals(2, parentUnion.getUnionOrThow().getRulesList().size()); + UnionRule parentUnion = + ((UnionRule) tableResponse.getColumnFamiliesMap().get("cf3").getGCRule()); + assertEquals(2, parentUnion.getRulesList().size()); - IntersectionRule intersectionRule = - parentUnion.getUnionOrThow().getRulesList().get(0).getIntersectionOrThow(); - assertEquals(2, intersectionRule.getIntersectionOrThow().getRulesList().size()); - assertEquals( - 1, - intersectionRule - .getIntersectionOrThow() - .getRulesList() - .get(0) - .getVersionOrThow() - .getMaxVersions()); + IntersectionRule intersectionRule = ((IntersectionRule) parentUnion.getRulesList().get(0)); + assertEquals(2, intersectionRule.getRulesList().size()); + assertEquals(1, ((VersionRule) intersectionRule.getRulesList().get(0)).getMaxVersions()); assertEquals( Duration.ofSeconds(1, 0), - intersectionRule - .getIntersectionOrThow() - .getRulesList() - .get(1) - .getDurationOrThow() - .getMaxAge()); - - intersectionRule = parentUnion.getUnionOrThow().getRulesList().get(1).getIntersectionOrThow(); - assertEquals(2, intersectionRule.getIntersectionOrThow().getRulesList().size()); - assertEquals( - 1, - intersectionRule - .getIntersectionOrThow() - .getRulesList() - .get(0) - .getVersionOrThow() - .getMaxVersions()); + ((DurationRule) intersectionRule.getRulesList().get(1)).getMaxAge()); + + intersectionRule = ((IntersectionRule) parentUnion.getRulesList().get(1)); + assertEquals(2, intersectionRule.getRulesList().size()); + assertEquals(1, ((VersionRule) intersectionRule.getRulesList().get(0)).getMaxVersions()); assertEquals( Duration.ofSeconds(1, 0), - intersectionRule - .getIntersectionOrThow() - .getRulesList() - .get(1) - .getDurationOrThow() - .getMaxAge()); + ((DurationRule) intersectionRule.getRulesList().get(1)).getMaxAge()); } @Test @@ -234,9 +211,6 @@ public void convertTableEmpty() { TableAdminResponses.convertTable(com.google.bigtable.admin.v2.Table.newBuilder().build()); assertNotNull(tableResponse); - assertEquals( - TimestampGranularity.TIMESTAMP_GRANULARITY_UNSPECIFIED, - tableResponse.getTimestampGranularity()); assertEquals(0, tableResponse.getClusterStates().size()); assertEquals(0, tableResponse.getColumnFamiles().size()); } From 6a011b18a85854bbe016241630f26a075f8dc03b Mon Sep 17 00:00:00 2001 From: spollapally Date: Wed, 20 Jun 2018 14:12:24 -0400 Subject: [PATCH 08/10] Fix failing test case --- .../cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java index 19efd4ed7514..cb8bee2013cd 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java @@ -28,7 +28,6 @@ import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; -import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.protobuf.ByteString; @RunWith(JUnit4.class) @@ -51,7 +50,6 @@ public void createTable() { .addInitialSplits(Split.newBuilder().setKey(ByteString.copyFromUtf8("c"))) .setTable( Table.newBuilder() - .setGranularity(TimestampGranularity.MILLIS) .putColumnFamilies("cf1", ColumnFamily.newBuilder().build()) .putColumnFamilies( "cf2", From d530655761caba449d8c9aeee330309c7b31376b Mon Sep 17 00:00:00 2001 From: spollapally Date: Thu, 21 Jun 2018 10:39:39 -0400 Subject: [PATCH 09/10] javadoc indentation fixes. --- .../bigtable/admin/v2/TableAdminClient.java | 19 +++++++++---------- .../bigtable/admin/v2/models/GCRules.java | 6 ++---- .../admin/v2/models/TableAdminRequests.java | 4 ++-- .../admin/v2/it/TableAdminClientIT.java | 4 ++-- .../v2/models/TableAdminRequestsTest.java | 4 ++-- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java index 5fe82277c3d6..3351fc09bce4 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java @@ -59,8 +59,8 @@ * try(TableAdminClient client = TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) { * CreateTable createTableReq = * TableAdminRequests.createTable("tableId") - * .addColumnFamily("cf1") - * .addColumnFamily("cf2", GCRULES.maxVersions(10)) + * .addFamily("cf1") + * .addFamily("cf2", GCRULES.maxVersions(10)) * .addSplit(ByteString.copyFromUtf8("b")) * .addSplit(ByteString.copyFromUtf8("q")); * client.createTable(createTableReq); @@ -80,9 +80,9 @@ * BigtableTableAdminSettings bigtableTableAdminSettings = * BigtableTableAdminSettings.newBuilder() * .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) - * .build(); + * .build(); * TableAdminClient client = - * TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings); + * TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings); * } * * To customize the endpoint: @@ -91,7 +91,7 @@ * BigtableTableAdminSettings bigtableTableAdminSettings = * BigtableTableAdminSettings.newBuilder().setEndpoint(myEndpoint).build(); * TableAdminClient client = - * TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings); + * TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings); * } */ @BetaApi @@ -173,7 +173,7 @@ public void close() { * try(TableAdminClient client = TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) { * CreateTable createTableReq = * TableAdminRequests.createTable("tableId") - * .addColumnFamily("cf2", GCRULES.maxVersions(10)) + * .addFamily("cf2", GCRULES.maxVersions(10)) * client.createTable(createTableReq); * } * } @@ -197,8 +197,7 @@ public Table createTable(CreateTable createTable) { * try(TableAdminClient client = TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) { * CreateTable createTableReq = * TableAdminRequests.createTable("tableId") - * .addColumnFamily("cf2", GCRULES.maxVersions(10)) - * + * .addFamily("cf2", GCRULES.maxVersions(10)) * client.createTableAsync(createTableReq); * } * } @@ -612,11 +611,11 @@ public boolean isConsistent(String tableId, ConsistencyToken token) { * @return ApiFuture */ public ApiFuture isConsistentAsync(String tableId, ConsistencyToken token) { - ApiFuture CheckConsResp = + ApiFuture checkConsResp = stub.checkConsistencyCallable().futureCall(token.toProto(getTableName(tableId))); return ApiFutures.transform( - CheckConsResp, + checkConsResp, new ApiFunction() { @Override public Boolean apply(CheckConsistencyResponse input) { diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java index 70537e5d995c..6817d305e783 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java @@ -208,7 +208,7 @@ private VersionRule(int maxVersion) { /** Gets the configured maximum versions */ public int getMaxVersions() { - return toProto().getMaxNumVersions(); + return builder.getMaxNumVersions(); } @Override @@ -240,9 +240,7 @@ private DurationRule(Duration duration) { * @return Duration */ public Duration getMaxAge() { - long seconds = toProto().getMaxAge().getSeconds(); - int nanos = toProto().getMaxAge().getNanos(); - return Duration.ofSeconds(seconds, nanos); + return Duration.ofSeconds(builder.getSeconds(), builder.getNanos()); } @Override diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java index 3c7c653028ee..59c3cb3fb30a 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java @@ -83,7 +83,7 @@ private CreateTable(String tableId) { * * @param familyId */ - public CreateTable addColumnFamily(String familyId) { + public CreateTable addFamily(String familyId) { Preconditions.checkNotNull(familyId); tableRequest.putColumnFamilies(familyId, ColumnFamily.newBuilder().build()); return this; @@ -95,7 +95,7 @@ public CreateTable addColumnFamily(String familyId) { * @param familyId * @param gcRule */ - public CreateTable addColumnFamily(String familyId, GCRule gcRule) { + public CreateTable addFamily(String familyId, GCRule gcRule) { Preconditions.checkNotNull(familyId); tableRequest.putColumnFamilies( familyId, ColumnFamily.newBuilder().setGcRule(gcRule.toProto()).build()); diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java index 37878597fd5a..7cd8bff9854e 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java @@ -58,8 +58,8 @@ public void createTable() throws Exception { String tableId = "adminCreateTest"; CreateTable createTableReq = TableAdminRequests.createTable(tableId) - .addColumnFamily("cf1") - .addColumnFamily("cf2", GCRULES.maxVersions(10)) + .addFamily("cf1") + .addFamily("cf2", GCRULES.maxVersions(10)) .addSplit(ByteString.copyFromUtf8("b")) .addSplit(ByteString.copyFromUtf8("q")); diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java index cb8bee2013cd..d33940e18f0a 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java @@ -38,8 +38,8 @@ public class TableAdminRequestsTest { public void createTable() { CreateTableRequest actual = TableAdminRequests.createTable("tableId") - .addColumnFamily("cf1") - .addColumnFamily("cf2", GCRules.GCRULES.maxVersions(1)) + .addFamily("cf1") + .addFamily("cf2", GCRules.GCRULES.maxVersions(1)) .addSplit(ByteString.copyFromUtf8("c")) .toProto(instanceName); From c6a974e15aabf83990eb9539b8017c343f27a1c2 Mon Sep 17 00:00:00 2001 From: spollapally Date: Mon, 25 Jun 2018 20:18:56 -0400 Subject: [PATCH 10/10] defaultRule method name typo fix. --- .../com/google/cloud/bigtable/admin/v2/models/GCRules.java | 2 +- .../cloud/bigtable/admin/v2/models/TableAdminRequests.java | 2 +- .../cloud/bigtable/admin/v2/models/TableAdminResponses.java | 4 ++-- .../bigtable/admin/v2/models/TableAdminResponsesTest.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java index 6817d305e783..624f91781a78 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java @@ -89,7 +89,7 @@ public DurationRule maxAge(Duration duration) { * * @return DefaultRule */ - public DefaultRule defaulRule() { + public DefaultRule defaultRule() { return new DefaultRule(); } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java index 59c3cb3fb30a..ed4197069099 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java @@ -156,7 +156,7 @@ private ModifyFamilies(String tableId) { * @return */ public ModifyFamilies addFamily(String familyId) { - return addFamily(familyId, GCRules.GCRULES.defaulRule()); + return addFamily(familyId, GCRules.GCRULES.defaultRule()); } /** diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java index 41d391e7bdec..82edc5184419 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java @@ -214,7 +214,7 @@ public String toString() { /** Wrapper for {@link ColumnFamily} protocol buffer object */ public static final class ColumnFamily { private final String id; - private GCRule gCRule = GCRULES.defaulRule(); + private GCRule gCRule = GCRULES.defaultRule(); private ColumnFamily(String id, com.google.bigtable.admin.v2.ColumnFamily cf) { Preconditions.checkNotNull(id); @@ -282,7 +282,7 @@ private GCRule convertGcRule(GcRule source) { return union; default: - return GCRULES.defaulRule(); + return GCRULES.defaultRule(); } } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java index 80fc5fb92bf3..3b759e2f2954 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java @@ -88,7 +88,7 @@ public void convertTable() { assertNotNull("cf1", tableResponse.getColumnFamiliesMap().get("cf1").getId()); assertFalse(tableResponse.getColumnFamiliesMap().get("cf1").hasGcRule()); - assertThat(GCRULES.defaulRule().toProto()) + assertThat(GCRULES.defaultRule().toProto()) .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf1").getGCRule().toProto()); assertThat(GCRULES.maxVersions(1).toProto()) .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf2").getGCRule().toProto());