From 0086fe861887690887486e506785545a6c1da224 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Fri, 27 Jul 2018 18:18:22 -0400 Subject: [PATCH] WIP: Bigtable Admin: Promote models to top level classes --- .../bigtable/admin/v2/TableAdminClient.java | 43 ++- .../admin/v2/models/ClusterState.java | 56 ++++ .../admin/v2/models/ColumnFamily.java | 71 +++++ .../admin/v2/models/ConsistencyToken.java | 60 ++++ .../admin/v2/models/CreateTableRequest.java | 99 ++++++ .../bigtable/admin/v2/models/GCRules.java | 33 ++ .../models/ModifyColumnFamiliesRequest.java | 117 +++++++ .../cloud/bigtable/admin/v2/models/Table.java | 108 +++++++ .../admin/v2/models/TableAdminRequests.java | 213 ------------- .../admin/v2/models/TableAdminResponses.java | 299 ------------------ .../admin/v2/TableAdminClientTest.java | 34 +- .../admin/v2/it/TableAdminClientIT.java | 33 +- .../admin/v2/models/ConsistencyTokenTest.java | 39 +++ .../v2/models/TableAdminRequestsTest.java | 52 ++- ...AdminResponsesTest.java => TableTest.java} | 28 +- 15 files changed, 667 insertions(+), 618 deletions(-) create mode 100644 google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ClusterState.java create mode 100644 google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ColumnFamily.java create mode 100644 google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyToken.java create mode 100644 google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java create mode 100644 google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ModifyColumnFamiliesRequest.java create mode 100644 google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java delete mode 100644 google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java delete mode 100644 google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java create mode 100644 google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyTokenTest.java rename google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/{TableAdminResponsesTest.java => TableTest.java} (90%) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java index 89f3ee6ccdb1..88b01e0c5ed1 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java @@ -32,13 +32,11 @@ 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.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.ConsistencyToken; -import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.Table; +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; +import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest; +import com.google.cloud.bigtable.admin.v2.models.ConsistencyToken; +import com.google.cloud.bigtable.admin.v2.models.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; @@ -178,12 +176,12 @@ public void close() { * * @param createTable * @return the newly created table - * @see CreateTable for createTable configurations + * @see CreateTableRequest for createTable configurations */ - public Table createTable(CreateTable createTable) { + public Table createTable(CreateTableRequest createTable) { com.google.bigtable.admin.v2.Table table = this.stub.createTableCallable().call(createTable.toProto(instanceName)); - return TableAdminResponses.convertTable(table); + return Table.fromProto(table); } /** @@ -202,9 +200,9 @@ public Table createTable(CreateTable createTable) { * * @param createTable * @return the newly created table - * @see CreateTable for createTable configurations + * @see CreateTableRequest for createTable configurations */ - public ApiFuture createTableAsync(CreateTable createTable) { + public ApiFuture
createTableAsync(CreateTableRequest createTable) { return transformToTableResponse( this.stub.createTableCallable().futureCall(createTable.toProto(instanceName))); } @@ -239,13 +237,13 @@ public ApiFuture
createTableAsync(CreateTable createTable) { * * @param modifyFamily * @return the modified table - * @see ModifyFamilies for modifyFamily options + * @see ModifyColumnFamiliesRequest for modifyFamily options */ - public Table modifyFamilies(ModifyFamilies modifyFamily) { - ModifyColumnFamiliesRequest modReq = modifyFamily.toProto(instanceName); + public Table modifyFamilies(ModifyColumnFamiliesRequest modifyFamily) { + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest modReq = modifyFamily.toProto(instanceName); com.google.bigtable.admin.v2.Table table = this.stub.modifyColumnFamiliesCallable().call(modReq); - return TableAdminResponses.convertTable(table); + return Table.fromProto(table); } /** @@ -278,10 +276,10 @@ public Table modifyFamilies(ModifyFamilies modifyFamily) { * * @param modifyFamily * @return Modified table - * @see ModifyFamilies for modifyFamily options + * @see ModifyColumnFamiliesRequest for modifyFamily options */ - public ApiFuture
modifyFamiliesAsync(ModifyFamilies modifyFamily) { - ModifyColumnFamiliesRequest modReq = modifyFamily.toProto(instanceName); + public ApiFuture
modifyFamiliesAsync(ModifyColumnFamiliesRequest modifyFamily) { + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest modReq = modifyFamily.toProto(instanceName); return transformToTableResponse(this.stub.modifyColumnFamiliesCallable().futureCall(modReq)); } @@ -336,7 +334,7 @@ public ApiFuture deleteTableAsync(String tableId) { public Table getTable(String tableId) { com.google.bigtable.admin.v2.Table table = this.stub.getTableCallable().call(composeGetTableRequest(tableId)); - return TableAdminResponses.convertTable(table); + return Table.fromProto(table); } /** @@ -525,7 +523,7 @@ public ApiFuture dropAllRowsAsync(String tableId) { * @param tableId */ public ConsistencyToken generateConsistencyToken(String tableId) { - return TableAdminResponses.convertTokenResponse( + return ConsistencyToken.fromProto( this.stub .generateConsistencyTokenCallable() .call(composeGenerateConsistencyTokenRequest(tableId))); @@ -556,7 +554,7 @@ public ApiFuture generateConsistencyTokenAsync(String tableId) new ApiFunction() { @Override public ConsistencyToken apply(GenerateConsistencyTokenResponse input) { - return TableAdminResponses.convertTokenResponse(input); + return ConsistencyToken.fromProto(input); } }); } @@ -654,7 +652,6 @@ DeleteTableRequest composeDeleteTableRequest(String tableId) { * * @param tableId * @param rowKeyPrefix - * @param boolean dropAll */ @VisibleForTesting DropRowRangeRequest composeDropRowRangeRequest( @@ -707,7 +704,7 @@ static ApiFuture
transformToTableResponse( new ApiFunction() { @Override public Table apply(com.google.bigtable.admin.v2.Table table) { - return TableAdminResponses.convertTable(table); + return Table.fromProto(table); } }); } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ClusterState.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ClusterState.java new file mode 100644 index 000000000000..14e70062ae1c --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ClusterState.java @@ -0,0 +1,56 @@ +/* + * 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.Table.ClusterState.ReplicationState; +import com.google.common.base.MoreObjects; + +/** Wrapper for {@link ClusterState} protocol buffer object */ +public final class ClusterState { + private final String id; + private final ReplicationState replicationState; + + public static ClusterState fromProto(String clusterId, com.google.bigtable.admin.v2.Table.ClusterState proto) { + return new ClusterState(clusterId, proto.getReplicationState()); + } + + private ClusterState(String id, ReplicationState replicationState) { + this.id = id; + this.replicationState = replicationState; + } + + /** + * Gets the cluster Id + */ + public String getId() { + return id; + } + + /** + * Gets the ReplicationState of the table for this cluster + */ + public ReplicationState getReplicationState() { + return replicationState; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("id", id) + .add("replicationState", replicationState) + .toString(); + } +} diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ColumnFamily.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ColumnFamily.java new file mode 100644 index 000000000000..e8802f675ae3 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ColumnFamily.java @@ -0,0 +1,71 @@ +/* + * 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 com.google.api.core.InternalApi; +import com.google.bigtable.admin.v2.GcRule; +import com.google.bigtable.admin.v2.GcRule.RuleCase; +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 org.threeten.bp.Duration; + +/** Wrapper for {@link ColumnFamily} protocol buffer object */ +public final class ColumnFamily { + private final String id; + private final GCRule rule; + + @InternalApi + public static ColumnFamily fromProto(String id, com.google.bigtable.admin.v2.ColumnFamily proto) { + GcRule ruleProto = MoreObjects.firstNonNull(proto.getGcRule(), GcRule.getDefaultInstance()); + + return new ColumnFamily(id, GCRULES.fromProto(ruleProto)); + } + + private ColumnFamily(String id, GCRule rule) { + this.id = id; + this.rule = rule; + } + + /** + * Gets the columnfamily name + */ + public String getId() { + return id; + } + + /** + * Get's the GCRule configured for the columnfamily + */ + public GCRule getGCRule() { + return rule; + } + + /** + * Returns true if a GCRule has been configured for the family + */ + public boolean hasGCRule() { + return !RuleCase.RULE_NOT_SET.equals(rule.toProto().getRuleCase()); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this).add("id", id).add("GCRule", rule).toString(); + } +} diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyToken.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyToken.java new file mode 100644 index 000000000000..cf01403d9957 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyToken.java @@ -0,0 +1,60 @@ +/* + * 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.api.core.InternalApi; +import com.google.bigtable.admin.v2.CheckConsistencyRequest; +import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; +import com.google.cloud.bigtable.admin.v2.TableAdminClient; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.MoreObjects; + +/** + * Wrapper for {@link GenerateConsistencyTokenResponse#getConsistencyToken()} + * + *

Cannot be created. They are obtained by invoking {@link + * TableAdminClient#generateConsistencyToken(String)} + */ +public final class ConsistencyToken { + private final String token; + + @InternalApi + public static ConsistencyToken fromProto(GenerateConsistencyTokenResponse proto) { + return new ConsistencyToken(proto.getConsistencyToken()); + } + + private ConsistencyToken(String token) { + this.token = token; + } + + @InternalApi + public CheckConsistencyRequest toProto(String tableName) { + return CheckConsistencyRequest.newBuilder() + .setName(tableName) + .setConsistencyToken(token) + .build(); + } + + @VisibleForTesting + 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-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java new file mode 100644 index 000000000000..819dad080d90 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java @@ -0,0 +1,99 @@ +/* + * 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.api.core.InternalApi; +import com.google.bigtable.admin.v2.ColumnFamily; +import com.google.bigtable.admin.v2.CreateTableRequest.Split; +import com.google.bigtable.admin.v2.InstanceName; +import com.google.bigtable.admin.v2.Table; +import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; +import com.google.common.base.Preconditions; +import com.google.protobuf.ByteString; + +/** + * Fluent wrapper for {@link com.google.bigtable.admin.v2.CreateTableRequest} + * + *

Allows for creating table with: + * + *

+ */ +public final class CreateTableRequest { + private final com.google.bigtable.admin.v2.CreateTableRequest.Builder createTableRequest = com.google.bigtable.admin.v2.CreateTableRequest + .newBuilder(); + private final com.google.bigtable.admin.v2.Table.Builder tableRequest = Table.newBuilder(); + + public static CreateTableRequest of(String tableId) { + return new CreateTableRequest(tableId); + } + + /** + * Configures table with the specified id + * + * @param tableId + */ + CreateTableRequest(String tableId) { + createTableRequest.setTableId(tableId); + } + + /** + * Adds a new columnFamily to the configuration + * + * @param familyId + */ + public CreateTableRequest addFamily(String familyId) { + Preconditions.checkNotNull(familyId); + tableRequest.putColumnFamilies(familyId, com.google.bigtable.admin.v2.ColumnFamily.newBuilder().build()); + return this; + } + + /** + * Adds a new columnFamily with {@link GCRule} to the configuration + * + * @param familyId + * @param gcRule + */ + public CreateTableRequest addFamily(String familyId, GCRule gcRule) { + Preconditions.checkNotNull(familyId); + tableRequest.putColumnFamilies( + familyId, ColumnFamily.newBuilder().setGcRule(gcRule.toProto()).build()); + return this; + } + + /** + * Adds split at the specified key to the configuration + * + * @param key + */ + public CreateTableRequest addSplit(ByteString key) { + Preconditions.checkNotNull(key); + createTableRequest.addInitialSplits(Split.newBuilder().setKey(key).build()); + return this; + } + + @InternalApi + public com.google.bigtable.admin.v2.CreateTableRequest toProto(InstanceName instanceName) { + Preconditions.checkNotNull(instanceName); + return createTableRequest + .setParent(instanceName.toString()) + .setTable(tableRequest.build()) + .build(); + } +} diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java index 1ee6263030fe..2147ad037f62 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java @@ -84,6 +84,39 @@ public DefaultRule defaultRule() { return new DefaultRule(); } + + @InternalApi + public GCRule fromProto(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(fromProto(rule)); + } + return intersection; + + case UNION: + UnionRule union = GCRules.GCRULES.union(); + for (GcRule rule : source.getUnion().getRulesList()) { + union.rule(fromProto(rule)); + } + return union; + + case RULE_NOT_SET: + return defaultRule(); + + default: + throw new IllegalArgumentException("Unknown GcRule case: " + source.getRuleCase()); + } + } + /** * Fluent wrapper for {@link Intersection} rule. Allows far adding an hierarchy of rules with * intersection as the root diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ModifyColumnFamiliesRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ModifyColumnFamiliesRequest.java new file mode 100644 index 000000000000..f64ae1964b22 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ModifyColumnFamiliesRequest.java @@ -0,0 +1,117 @@ +/* + * 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.api.core.InternalApi; +import com.google.bigtable.admin.v2.ColumnFamily; +import com.google.bigtable.admin.v2.InstanceName; +import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; +import com.google.bigtable.admin.v2.TableName; +import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; +import com.google.common.base.Preconditions; + +/** + * Fluent wrapper for {@link com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest} + * + *

Allows for the following ColumnFamily modifications: + * + *

+ */ +public final class ModifyColumnFamiliesRequest { + private final com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Builder modFamilyRequest = + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.newBuilder(); + private final String tableId; + + + public static ModifyColumnFamiliesRequest of(String tableId) { + return new ModifyColumnFamiliesRequest(tableId); + } + + /** + * Configures the tableId to execute the modifications + * + * @param tableId + */ + ModifyColumnFamiliesRequest(String tableId) { + Preconditions.checkNotNull(tableId); + this.tableId = tableId; + } + + /** + * Configures the name of the new ColumnFamily to be created + * + * @param familyId + * @return + */ + public ModifyColumnFamiliesRequest addFamily(String familyId) { + return addFamily(familyId, GCRules.GCRULES.defaultRule()); + } + + /** + * Configures the name and GcRule of the new ColumnFamily to be created + * + * @param familyId + * @param gcRule + * @return + */ + public ModifyColumnFamiliesRequest addFamily(String familyId, GCRule gcRule) { + Modification.Builder modification = Modification.newBuilder().setId(familyId); + Preconditions.checkNotNull(gcRule); + modification.setCreate(com.google.bigtable.admin.v2.ColumnFamily.newBuilder().setGcRule(gcRule.toProto())); + modFamilyRequest.addModifications(modification.build()); + return this; + } + + /** + * Updates the GCRule of existing ColumnFamily + * + * @param familyId + * @param gcRule + * @return + */ + public ModifyColumnFamiliesRequest updateFamily(String familyId, GCRule gcRule) { + Modification.Builder modification = Modification.newBuilder().setId(familyId); + Preconditions.checkNotNull(gcRule); + modification.setUpdate(ColumnFamily.newBuilder().setGcRule(gcRule.toProto())); + modFamilyRequest.addModifications(modification.build()); + return this; + } + + /** + * Drops the specified ColumnFamily + * + * @param familyId + * @return + */ + public ModifyColumnFamiliesRequest dropFamily(String familyId) { + Modification.Builder modification = Modification.newBuilder().setId(familyId); + modification.setId(familyId).setDrop(true); + modFamilyRequest.addModifications(modification.build()); + return this; + } + + @InternalApi + public com.google.bigtable.admin.v2.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-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java new file mode 100644 index 000000000000..4b010ce2b42e --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java @@ -0,0 +1,108 @@ +/* + * 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.api.core.InternalApi; +import com.google.bigtable.admin.v2.TableName; +import com.google.common.base.MoreObjects; +import com.google.common.collect.ImmutableMap; +import java.util.Collection; +import java.util.Map; +import java.util.Map.Entry; +import javax.annotation.Nonnull; + +/** Wrapper for {@link Table} protocol buffer object */ +public final class Table { + private final TableName tableName; + private final Map clusterStates; + private final Map columnFamilies; + + @InternalApi + public static Table fromProto(@Nonnull com.google.bigtable.admin.v2.Table proto) { + ImmutableMap.Builder clusterStates = ImmutableMap.builder(); + + for (Entry entry : proto.getClusterStatesMap().entrySet()) { + clusterStates.put(entry.getKey(), ClusterState.fromProto(entry.getKey(), entry.getValue())); + } + + ImmutableMap.Builder families = ImmutableMap.builder(); + + for (Entry entry : proto.getColumnFamiliesMap().entrySet()) { + families.put(entry.getKey(), ColumnFamily.fromProto(entry.getKey(), entry.getValue())); + } + + return new Table( + TableName.parse(proto.getName()), + clusterStates.build(), + families.build()); + } + + private Table(TableName tableName, + Map clusterStates, + Map columnFamilies) { + this.tableName = tableName; + this.clusterStates = clusterStates; + this.columnFamilies = columnFamilies; + } + + /** + * Gets the unique name of the table in the format: + * projects/{project}/instances/{instance}/tables/{tableId} + */ + public TableName getTableName() { + return tableName; + } + + /** + * Returns state of the table by clusters in the instance as map of clusterId and {@link + * ClusterState} + */ + public Map getClusterStatesMap() { + return clusterStates; + } + + /** + * Returns a map of columfamilies in the table keyed by columnfamily and name + */ + public Map getColumnFamiliesMap() { + return columnFamilies; + } + + /** + * Returns state of the table by clusters in the instance as a Collection + */ + public Collection getClusterStates() { + return clusterStates.values(); + } + + /** + * Returns all columnfamilies in the table as a Collection + */ + public Collection getColumnFamiles() { + return columnFamilies.values(); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("tableName", tableName) + .add("clusterStates", getClusterStates()) + .add("columnFamiles", getColumnFamiles()) + .toString(); + } + + +} diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java deleted file mode 100644 index ed4197069099..000000000000 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequests.java +++ /dev/null @@ -1,213 +0,0 @@ -/* - * 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.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.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; -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; - -/** 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); - } - - /** - * Adds a new columnFamily to the configuration - * - * @param familyId - */ - public CreateTable addFamily(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 addFamily(String familyId, GCRule gcRule) { - Preconditions.checkNotNull(familyId); - tableRequest.putColumnFamilies( - familyId, ColumnFamily.newBuilder().setGcRule(gcRule.toProto()).build()); - 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; - } - - @InternalApi - public CreateTableRequest toProto(InstanceName instanceName) { - Preconditions.checkNotNull(instanceName); - return createTableRequest - .setParent(instanceName.toString()) - .setTable(tableRequest.build()) - .build(); - } - } - - /** - * 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 - *
- */ - public static final class ModifyFamilies { - private final ModifyColumnFamiliesRequest.Builder modFamilyRequest = - ModifyColumnFamiliesRequest.newBuilder(); - private final String tableId; - - /** - * Configures the tableId to execute the modifications - * - * @param tableId - */ - private ModifyFamilies(String tableId) { - Preconditions.checkNotNull(tableId); - this.tableId = tableId; - } - - /** - * Configures the name of the new ColumnFamily to be created - * - * @param familyId - * @return - */ - public ModifyFamilies addFamily(String familyId) { - return addFamily(familyId, GCRules.GCRULES.defaultRule()); - } - - /** - * Configures the name and GcRule of the new ColumnFamily to be created - * - * @param familyId - * @param gcRule - * @return - */ - public ModifyFamilies addFamily(String familyId, GCRule gcRule) { - Modification.Builder modification = Modification.newBuilder().setId(familyId); - Preconditions.checkNotNull(gcRule); - modification.setCreate(ColumnFamily.newBuilder().setGcRule(gcRule.toProto())); - modFamilyRequest.addModifications(modification.build()); - return this; - } - - /** - * Updates the GCRule of existing ColumnFamily - * - * @param familyId - * @param gcRule - * @return - */ - public ModifyFamilies updateFamily(String familyId, GCRule gcRule) { - Modification.Builder modification = Modification.newBuilder().setId(familyId); - Preconditions.checkNotNull(gcRule); - modification.setUpdate(ColumnFamily.newBuilder().setGcRule(gcRule.toProto())); - modFamilyRequest.addModifications(modification.build()); - return this; - } - - /** - * Drops the specified ColumnFamily - * - * @param familyId - * @return - */ - public ModifyFamilies dropFamily(String familyId) { - Modification.Builder modification = Modification.newBuilder().setId(familyId); - modification.setId(familyId).setDrop(true); - modFamilyRequest.addModifications(modification.build()); - return this; - } - - @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-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java deleted file mode 100644 index 42b9f22a8ecb..000000000000 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponses.java +++ /dev/null @@ -1,299 +0,0 @@ -/* - * 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.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; -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; - -/** 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 table - Protobuf table - * @return Table response wrapper - */ - @InternalApi - public static Table convertTable(com.google.bigtable.admin.v2.Table table) { - return new Table(table); - } - - /** - * Converts the protocol buffer response to a simpler ConsistencyToken which can be passed back as - * is. - * - * @param tokenResponse - Protobuf ConsistencyTokenResponse - * @return 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 - */ - @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 - */ - @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 final TableName tableName; - 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.clusterStates = convertClusterStates(table.getClusterStatesMap()); - this.columnFamilies = convertColumnFamilies(table.getColumnFamiliesMap()); - } - - /** - * Gets the unique name of the table in the format: - * projects/{project}/instances/{instance}/tables/{tableId} - */ - public TableName getTableName() { - return tableName; - } - - /** - * Returns state of the table by clusters in the instance as map of clusterId and {@link - * ClusterState} - */ - public Map getClusterStatesMap() { - return clusterStates; - } - - /** - * Returns a map of columfamilies in the table keyed by columnfamily and name - */ - public Map getColumnFamiliesMap() { - return columnFamilies; - } - - /** - * Returns state of the table by clusters in the instance as a Collection - */ - public Collection getClusterStates() { - return clusterStates.values(); - } - - /** - * Returns all columnfamilies in the table as a Collection - */ - public Collection getColumnFamiles() { - return columnFamilies.values(); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("tableName", tableName) - .add("clusterStates", getClusterStates()) - .add("columnFamiles", getColumnFamiles()) - .toString(); - } - } - - /** Wrapper for {@link ClusterState} protocol buffer object */ - public static final class ClusterState { - private final String id; - private final ReplicationState replicationState; - - private ClusterState(String id, com.google.bigtable.admin.v2.Table.ClusterState clusterState) { - this.id = id; - replicationState = clusterState.getReplicationState(); - } - - /** - * Gets the cluster Id - */ - public String getId() { - return id; - } - - /** - * Gets the ReplicationState of the table for this cluster - */ - public ReplicationState getReplicationState() { - return replicationState; - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("is", id) - .add("replicationState", replicationState) - .toString(); - } - } - - /** Wrapper for {@link ColumnFamily} protocol buffer object */ - public static final class ColumnFamily { - private final String id; - private GCRule gCRule = GCRULES.defaultRule(); - - private ColumnFamily(String id, com.google.bigtable.admin.v2.ColumnFamily cf) { - Preconditions.checkNotNull(id); - Preconditions.checkNotNull(cf); - this.id = id; - - if (cf.getGcRule() != null) { - this.gCRule = convertGcRule(cf.getGcRule()); - } - } - - /** - * Gets the columnfamily name - */ - public String getId() { - return id; - } - - /** - * Get's the GCRule configured for the columnfamily - */ - public GCRule getGCRule() { - return gCRule; - } - - /** - * Returns true if a GCRule has been configured for the family - */ - public boolean hasGcRule() { - return !RuleCase.RULE_NOT_SET.equals(gCRule.toProto().getRuleCase()); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this).add("id", id).add("gCRule", gCRule).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.defaultRule(); - } - } - } - - /** - * 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; - - private ConsistencyToken(GenerateConsistencyTokenResponse resp) { - this.token = resp.getConsistencyToken(); - } - - @InternalApi - public CheckConsistencyRequest toProto(String tableName) { - return CheckConsistencyRequest.newBuilder() - .setName(tableName) - .setConsistencyToken(token) - .build(); - } - - @VisibleForTesting - 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-admin/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java index 0c0b60b8f2dd..f91d2d3bb75c 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/TableAdminClientTest.java @@ -31,7 +31,6 @@ 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; @@ -40,13 +39,11 @@ 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; -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.models.CreateTableRequest; +import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest; +import com.google.cloud.bigtable.admin.v2.models.ConsistencyToken; import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStub; import com.google.protobuf.ByteString; import com.google.protobuf.Empty; @@ -56,8 +53,8 @@ public class TableAdminClientTest { private TableAdminClient adminClient; @Mock private BigtableTableAdminStub mockStub; - @Mock private UnaryCallable mockCreateTableCallable; - @Mock private UnaryCallable mockModifyTableCallable; + @Mock private UnaryCallable mockCreateTableCallable; + @Mock private UnaryCallable mockModifyTableCallable; @Mock private UnaryCallable mockDeleteTableCallable; @Mock private UnaryCallable mockGetTableCallable; @Mock private UnaryCallable mockListTableCallable; @@ -87,12 +84,15 @@ public void setUp() throws Exception { 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))) + Mockito.when(mockCreateTableCallable.call(any(com.google.bigtable.admin.v2.CreateTableRequest.class))).thenReturn(table); + Mockito.when(mockCreateTableCallable.futureCall(any( + com.google.bigtable.admin.v2.CreateTableRequest.class))) .thenReturn(futureTable); - Mockito.when(mockModifyTableCallable.call(any(ModifyColumnFamiliesRequest.class))) + Mockito.when(mockModifyTableCallable.call(any( + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.class))) .thenReturn(table); - Mockito.when(mockModifyTableCallable.futureCall(any(ModifyColumnFamiliesRequest.class))) + Mockito.when(mockModifyTableCallable.futureCall(any( + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.class))) .thenReturn(futureTable); Mockito.when(mockGetTableCallable.call(any(GetTableRequest.class))).thenReturn(table); Mockito.when(mockGetTableCallable.futureCall(any(GetTableRequest.class))) @@ -100,14 +100,14 @@ public void setUp() throws Exception { } @Test - public void close() throws Exception { + public void close() { adminClient.close(); Mockito.verify(mockStub).close(); } @Test public void createTable() { - CreateTable createTableReq = TableAdminRequests.createTable("tableId"); + CreateTableRequest createTableReq = CreateTableRequest.of("tableId"); adminClient.createTable(createTableReq); Mockito.verify(mockCreateTableCallable) .call(createTableReq.toProto(adminClient.getInstanceName())); @@ -115,7 +115,7 @@ public void createTable() { @Test public void createTableAsync() { - CreateTable createTableReq = TableAdminRequests.createTable("tableId"); + CreateTableRequest createTableReq = CreateTableRequest.of("tableId"); adminClient.createTableAsync(createTableReq); Mockito.verify(mockCreateTableCallable) .futureCall(createTableReq.toProto(adminClient.getInstanceName())); @@ -123,7 +123,7 @@ public void createTableAsync() { @Test public void modifyFamilies() { - ModifyFamilies modifyFamReq = TableAdminRequests.modifyFamilies("tableId"); + ModifyColumnFamiliesRequest modifyFamReq = ModifyColumnFamiliesRequest.of("tableId"); adminClient.modifyFamilies(modifyFamReq); Mockito.verify(mockModifyTableCallable) .call(modifyFamReq.toProto(adminClient.getInstanceName())); @@ -131,7 +131,7 @@ public void modifyFamilies() { @Test public void modifyFamiliesAsync() { - ModifyFamilies modifyFamReq = TableAdminRequests.modifyFamilies("tableId"); + ModifyColumnFamiliesRequest modifyFamReq = ModifyColumnFamiliesRequest.of("tableId"); adminClient.modifyFamiliesAsync(modifyFamReq); Mockito.verify(mockModifyTableCallable) .futureCall(modifyFamReq.toProto(adminClient.getInstanceName())); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java index 33195f282062..ddf525e0385f 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/it/TableAdminClientIT.java @@ -28,11 +28,10 @@ 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; -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.models.CreateTableRequest; +import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest; +import com.google.cloud.bigtable.admin.v2.models.ConsistencyToken; +import com.google.cloud.bigtable.admin.v2.models.Table; import com.google.protobuf.ByteString; import java.io.IOException; import java.util.List; @@ -79,8 +78,8 @@ public void setup() { @Test public void createTable() { String tableId = "adminCreateTest"; - CreateTable createTableReq = - TableAdminRequests.createTable(tableId) + CreateTableRequest createTableReq = + CreateTableRequest.of(tableId) .addFamily("cf1") .addFamily("cf2", GCRULES.maxVersions(10)) .addSplit(ByteString.copyFromUtf8("b")) @@ -91,8 +90,8 @@ public void createTable() { assertNotNull(tableResponse); assertEquals(tableId, tableResponse.getTableName().getTable()); assertEquals(2, tableResponse.getColumnFamiles().size()); - assertFalse(tableResponse.getColumnFamiliesMap().get("cf1").hasGcRule()); - assertTrue(tableResponse.getColumnFamiliesMap().get("cf2").hasGcRule()); + assertFalse(tableResponse.getColumnFamiliesMap().get("cf1").hasGCRule()); + assertTrue(tableResponse.getColumnFamiliesMap().get("cf2").hasGCRule()); assertEquals( 10, ((VersionRule) tableResponse.getColumnFamiliesMap().get("cf2").getGCRule()) @@ -105,7 +104,7 @@ public void createTable() { @Test public void modifyFamilies() { String tableId = "adminModifyFamTest"; - ModifyFamilies modifyFamiliesReq = TableAdminRequests.modifyFamilies(tableId); + ModifyColumnFamiliesRequest modifyFamiliesReq = ModifyColumnFamiliesRequest.of(tableId); modifyFamiliesReq .addFamily("mf1") @@ -130,7 +129,7 @@ public void modifyFamilies() { .addFamily("mf7"); try { - tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + tableAdmin.createTable(CreateTableRequest.of(tableId)); Table tableResponse = tableAdmin.modifyFamilies(modifyFamiliesReq); assertEquals(5, tableResponse.getColumnFamiles().size()); assertNotNull(tableResponse.getColumnFamiliesMap().get("mf1")); @@ -169,7 +168,7 @@ public void modifyFamilies() { @Test public void deleteTable() { String tableId = "adminDeleteTest"; - tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + tableAdmin.createTable(CreateTableRequest.of(tableId)); tableAdmin.deleteTable(tableId); } @@ -178,7 +177,7 @@ public void getTable() { String tableId = "adminGetTest"; try { - tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + tableAdmin.createTable(CreateTableRequest.of(tableId)); Table tableResponse = tableAdmin.getTable(tableId); assertNotNull(tableResponse); assertEquals(tableId, tableResponse.getTableName().getTable()); @@ -192,7 +191,7 @@ public void listTables() { String tableId = "adminListTest"; try { - tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + tableAdmin.createTable(CreateTableRequest.of(tableId)); List tables = tableAdmin.listTables(); assertNotNull(tables); assertFalse("List tables did not return any tables", tables.isEmpty()); @@ -206,7 +205,7 @@ public void listTablesAsync() throws Exception { String tableId = "adminListTest"; try { - tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + tableAdmin.createTable(CreateTableRequest.of(tableId)); List tables = tableAdmin.listTablesAsync().get(); assertNotNull(tables); assertFalse("List tables did not return any tables", tables.isEmpty()); @@ -220,7 +219,7 @@ public void dropRowRange() { String tableId = "adminDropRowrangeTest"; try { - tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + tableAdmin.createTable(CreateTableRequest.of(tableId)); tableAdmin.dropRowRange(tableId, "rowPrefix"); tableAdmin.dropAllRows(tableId); } finally { @@ -233,7 +232,7 @@ public void checkConsistency() { String tableId = "adminConsistencyTest"; try { - tableAdmin.createTable(TableAdminRequests.createTable(tableId)); + tableAdmin.createTable(CreateTableRequest.of(tableId)); ConsistencyToken consistencyToken = tableAdmin.generateConsistencyToken(tableId); assertNotNull(consistencyToken); boolean consistent = tableAdmin.isConsistent(tableId, consistencyToken); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyTokenTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyTokenTest.java new file mode 100644 index 000000000000..833a9eadf234 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyTokenTest.java @@ -0,0 +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.models; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ConsistencyTokenTest { + @Test + public void fromJsonTest() { + ConsistencyToken tokenResponse = + ConsistencyToken.fromProto( + GenerateConsistencyTokenResponse.newBuilder() + .setConsistencyToken("87282hgwd8yg") + .build()); + + assertNotNull(tokenResponse); + assertEquals("87282hgwd8yg", tokenResponse.getToken()); + } +} diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java index d33940e18f0a..d0538634a2e6 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java @@ -16,18 +16,15 @@ package com.google.cloud.bigtable.admin.v2.models; import static com.google.common.truth.Truth.assertThat; + +import com.google.bigtable.admin.v2.CreateTableRequest.Split; 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.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; -import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; import com.google.protobuf.ByteString; @RunWith(JUnit4.class) @@ -36,24 +33,23 @@ public class TableAdminRequestsTest { @Test public void createTable() { - CreateTableRequest actual = - TableAdminRequests.createTable("tableId") + com.google.bigtable.admin.v2.CreateTableRequest actual = CreateTableRequest.of("tableId") .addFamily("cf1") .addFamily("cf2", GCRules.GCRULES.maxVersions(1)) .addSplit(ByteString.copyFromUtf8("c")) .toProto(instanceName); - CreateTableRequest expected = - CreateTableRequest.newBuilder() + com.google.bigtable.admin.v2.CreateTableRequest expected = + com.google.bigtable.admin.v2.CreateTableRequest.newBuilder() .setTableId("tableId") .setParent(InstanceName.of("project", "instance").toString()) .addInitialSplits(Split.newBuilder().setKey(ByteString.copyFromUtf8("c"))) .setTable( Table.newBuilder() - .putColumnFamilies("cf1", ColumnFamily.newBuilder().build()) + .putColumnFamilies("cf1", com.google.bigtable.admin.v2.ColumnFamily.newBuilder().build()) .putColumnFamilies( "cf2", - ColumnFamily.newBuilder() + com.google.bigtable.admin.v2.ColumnFamily.newBuilder() .setGcRule(GCRules.GCRULES.maxVersions(1).toProto()) .build())) .build(); @@ -63,18 +59,18 @@ public void createTable() { @Test(expected = NullPointerException.class) public void createTableRequiredTableId() { - TableAdminRequests.createTable(null).toProto(instanceName); + CreateTableRequest.of(null); } @Test(expected = NullPointerException.class) public void createTableRequiredParent() { - TableAdminRequests.createTable("tableId").toProto(null); + CreateTableRequest.of("tableId").toProto(null); } @Test public void modifyFamilies() { - ModifyColumnFamiliesRequest actual = - TableAdminRequests.modifyFamilies("tableId") + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest actual = + ModifyColumnFamiliesRequest.of("tableId") .addFamily("cf1") .addFamily("cf2", GCRules.GCRULES.maxVersions(1)) .addFamily("cf3") @@ -82,43 +78,43 @@ public void modifyFamilies() { .dropFamily("cf3") .toProto(instanceName); - ModifyColumnFamiliesRequest expected = - ModifyColumnFamiliesRequest.newBuilder() + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest expected = + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.newBuilder() .setName( TableName.of(instanceName.getProject(), instanceName.getInstance(), "tableId") .toString()) .addModifications( - Modification.newBuilder() + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder() .setId("cf1") - .setCreate(ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()))) + .setCreate(com.google.bigtable.admin.v2.ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()))) .addModifications( - Modification.newBuilder() + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder() .setId("cf2") .setCreate( - ColumnFamily.newBuilder() + com.google.bigtable.admin.v2.ColumnFamily.newBuilder() .setGcRule(GCRules.GCRULES.maxVersions(1).toProto()))) .addModifications( - Modification.newBuilder() + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder() .setId("cf3") - .setCreate(ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()))) + .setCreate(com.google.bigtable.admin.v2.ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()))) .addModifications( - Modification.newBuilder() + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder() .setId("cf1") .setUpdate( - ColumnFamily.newBuilder() + com.google.bigtable.admin.v2.ColumnFamily.newBuilder() .setGcRule(GCRules.GCRULES.maxVersions(5).toProto()))) - .addModifications(Modification.newBuilder().setId("cf3").setDrop(true)) + .addModifications(com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder().setId("cf3").setDrop(true)) .build(); assertThat(actual).isEqualTo(expected); } @Test(expected = NullPointerException.class) public void modifyFamiliesRequiredTableId() { - TableAdminRequests.modifyFamilies(null).toProto(instanceName); + ModifyColumnFamiliesRequest.of(null).toProto(instanceName); } @Test(expected = NullPointerException.class) public void modifyFamiliesRequiredParent() { - TableAdminRequests.modifyFamilies("tableId").toProto(null); + ModifyColumnFamiliesRequest.of("tableId").toProto(null); } } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java similarity index 90% rename from google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java rename to google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java index 3b759e2f2954..b2f33b073a1c 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminResponsesTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java @@ -30,8 +30,6 @@ 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; import com.google.bigtable.admin.v2.GcRule.Union; import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; @@ -39,7 +37,7 @@ import static com.google.common.truth.Truth.assertThat; @RunWith(JUnit4.class) -public class TableAdminResponsesTest { +public class TableTest { @Test public void convertTable() { @@ -74,7 +72,7 @@ public void convertTable() { .build()) .build(); - Table tableResponse = TableAdminResponses.convertTable(table); + Table tableResponse = Table.fromProto(table); assertNotNull(tableResponse); assertEquals(testName, tableResponse.getTableName()); assertEquals(2, tableResponse.getClusterStates().size()); @@ -86,7 +84,7 @@ public void convertTable() { tableResponse.getClusterStatesMap().get("prod").getReplicationState()); assertEquals(3, tableResponse.getColumnFamiles().size()); assertNotNull("cf1", tableResponse.getColumnFamiliesMap().get("cf1").getId()); - assertFalse(tableResponse.getColumnFamiliesMap().get("cf1").hasGcRule()); + assertFalse(tableResponse.getColumnFamiliesMap().get("cf1").hasGCRule()); assertThat(GCRULES.defaultRule().toProto()) .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf1").getGCRule().toProto()); @@ -136,10 +134,10 @@ public void convertTableUnionOfIntersections() { .putColumnFamilies("cf3", ColumnFamily.newBuilder().setGcRule(expected).build()) .build(); - Table tableResponse = TableAdminResponses.convertTable(table); + Table tableResponse = Table.fromProto(table); assertThat(actual) .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGCRule().toProto()); - assertTrue(tableResponse.getColumnFamiliesMap().get("cf3").hasGcRule()); + assertTrue(tableResponse.getColumnFamiliesMap().get("cf3").hasGCRule()); UnionRule parentUnion = ((UnionRule) tableResponse.getColumnFamiliesMap().get("cf3").getGCRule()); @@ -200,7 +198,7 @@ public void convertTableIntersectionOfUnions() { .putColumnFamilies("cf3", ColumnFamily.newBuilder().setGcRule(expected).build()) .build(); - Table tableResponse = TableAdminResponses.convertTable(table); + Table tableResponse = Table.fromProto(table); assertThat(actual) .isEqualTo(tableResponse.getColumnFamiliesMap().get("cf3").getGCRule().toProto()); } @@ -208,22 +206,10 @@ public void convertTableIntersectionOfUnions() { @Test public void convertTableEmpty() { Table tableResponse = - TableAdminResponses.convertTable(com.google.bigtable.admin.v2.Table.newBuilder().build()); + Table.fromProto(com.google.bigtable.admin.v2.Table.newBuilder().build()); assertNotNull(tableResponse); assertEquals(0, tableResponse.getClusterStates().size()); assertEquals(0, tableResponse.getColumnFamiles().size()); } - - @Test - public void convertTokenResponse() { - ConsistencyToken tokenResponse = - TableAdminResponses.convertTokenResponse( - GenerateConsistencyTokenResponse.newBuilder() - .setConsistencyToken("87282hgwd8yg") - .build()); - - assertNotNull(tokenResponse); - assertEquals("87282hgwd8yg", tokenResponse.getToken()); - } }