From 9218e7ce5a4e3d23ffdb7815b83cb9fcb0d98329 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 16:37:43 +0000 Subject: [PATCH 01/27] feat(bigtable): Add schema bundle support --- .../admin/v2/BigtableTableAdminClient.java | 357 ++++++++++++++++++ .../bigtable/admin/v2/internal/NameUtil.java | 17 + .../v2/models/CreateSchemaBundleRequest.java | 98 +++++ .../admin/v2/models/SchemaBundle.java | 111 ++++++ .../v2/models/UpdateSchemaBundleRequest.java | 131 +++++++ .../admin/v2/internal/NameUtilTest.java | 21 ++ 6 files changed, 735 insertions(+) create mode 100644 google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java create mode 100644 google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java create mode 100644 google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java index 889598020a73..2e04494f9cc5 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java @@ -23,13 +23,16 @@ import com.google.api.gax.rpc.ApiExceptions; import com.google.api.gax.rpc.NotFoundException; import com.google.bigtable.admin.v2.DeleteAuthorizedViewRequest; +import com.google.bigtable.admin.v2.DeleteSchemaBundleRequest; import com.google.bigtable.admin.v2.DeleteBackupRequest; import com.google.bigtable.admin.v2.DeleteTableRequest; import com.google.bigtable.admin.v2.DropRowRangeRequest; import com.google.bigtable.admin.v2.GetAuthorizedViewRequest; +import com.google.bigtable.admin.v2.GetSchemaBundleRequest; import com.google.bigtable.admin.v2.GetBackupRequest; import com.google.bigtable.admin.v2.GetTableRequest; import com.google.bigtable.admin.v2.ListAuthorizedViewsRequest; +import com.google.bigtable.admin.v2.ListSchemaBundlesRequest; import com.google.bigtable.admin.v2.ListBackupsRequest; import com.google.bigtable.admin.v2.ListTablesRequest; import com.google.bigtable.admin.v2.RestoreTableMetadata; @@ -38,17 +41,21 @@ import com.google.cloud.Policy; import com.google.cloud.Policy.DefaultMarshaller; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListAuthorizedViewsPage; +import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListSchemaBundlesPage; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListAuthorizedViewsPagedResponse; +import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListSchemaBundlesPagedResponse; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListBackupsPage; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListBackupsPagedResponse; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse; import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.cloud.bigtable.admin.v2.models.AuthorizedView; +import com.google.cloud.bigtable.admin.v2.models.SchemaBundle; import com.google.cloud.bigtable.admin.v2.models.Backup; import com.google.cloud.bigtable.admin.v2.models.ConsistencyRequest; import com.google.cloud.bigtable.admin.v2.models.CopyBackupRequest; import com.google.cloud.bigtable.admin.v2.models.CreateAuthorizedViewRequest; +import com.google.cloud.bigtable.admin.v2.models.CreateSchemaBundleRequest; import com.google.cloud.bigtable.admin.v2.models.CreateBackupRequest; import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; import com.google.cloud.bigtable.admin.v2.models.EncryptionInfo; @@ -59,6 +66,7 @@ import com.google.cloud.bigtable.admin.v2.models.RestoredTableResult; import com.google.cloud.bigtable.admin.v2.models.Table; import com.google.cloud.bigtable.admin.v2.models.UpdateAuthorizedViewRequest; +import com.google.cloud.bigtable.admin.v2.models.UpdateSchemaBundleRequest; import com.google.cloud.bigtable.admin.v2.models.UpdateBackupRequest; import com.google.cloud.bigtable.admin.v2.models.UpdateTableRequest; import com.google.cloud.bigtable.admin.v2.stub.EnhancedBigtableTableAdminStub; @@ -1797,6 +1805,355 @@ public ApiFuture deleteAuthorizedViewAsync(String tableId, String authoriz return transformToVoid(this.stub.deleteAuthorizedViewCallable().futureCall(request)); } + /** + * Creates a new schema bundle with the specified configuration. + * + *

+ * Sample code: + * + *

{@code
+   * CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of("my-table", "my-new-schema-bundle")
+   *     .setDeletionProtection(true)
+   *     .setSchemaBundleType(
+   *         SubsetView.create()
+   *             .addRowPrefix("row#")
+   *             .addFamilySubsets(
+   *                 "my-family", FamilySubsets.create().addQualifier("column")));
+   *
+   * SchemaBundle response = client.createSchemaBundle(request);
+   * }
+ * + * @see CreateSchemaBundleRequest for available options. + */ + public SchemaBundle createSchemaBundle(CreateSchemaBundleRequest request) { + return ApiExceptions.callAndTranslateApiException(createSchemaBundleAsync(request)); + } + + /** + * Asynchronously creates a new schema bundle with the specified configuration. + * + *

+ * Sample code: + * + *

{@code
+   * CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of("my-table", "my-new-schema-bundle")
+   *     .setDeletionProtection(true)
+   *     .setSchemaBundleType(
+   *         SubsetView.create()
+   *             .addRowPrefix("row#")
+   *             .addFamilySubsets(
+   *                 "my-family", FamilySubsets.create().addQualifier("column")));
+   *
+   * ApiFuture future = client.createSchemaBundleAsync(request);
+   *
+   * ApiFutures.addCallback(
+   *     future,
+   *     new ApiFutureCallback() {
+   *       public void onSuccess(SchemaBundle schemaBundle) {
+   *         System.out.println("Successfully created the schema bundle: " + schemaBundle.getId());
+   *       }
+   *
+   *       public void onFailure(Throwable t) {
+   *         t.printStackTrace();
+   *       }
+   *     },
+   *     MoreExecutors.directExecutor());
+   * }
+ * + * @see CreateSchemaBundleRequest for available options. + */ + public ApiFuture createSchemaBundleAsync(CreateSchemaBundleRequest request) { + return ApiFutures.transform( + stub.createSchemaBundleOperationCallable() + .futureCall(request.toProto(projectId, instanceId)), + new ApiFunction() { + @Override + public SchemaBundle apply( + com.google.bigtable.admin.v2.SchemaBundle schemaBundleProto) { + return SchemaBundle.fromProto(schemaBundleProto); + } + }, + MoreExecutors.directExecutor()); + } + + /** + * Updates an existing schema bundle with the specified configuration. + * + *

+ * Sample code: + * + *

{@code
+   * SchemaBundle existingSchemaBundle = client.getSchemaBundle("my-table", "my-schema-bundle");
+   *
+   * UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(existingSchemaBundle).setDeletionProtection(true);
+   *
+   * SchemaBundle response = client.updateSchemaBundle(request);
+   * }
+ * + * @see UpdateSchemaBundleRequest for available options. + */ + public SchemaBundle updateSchemaBundle(UpdateSchemaBundleRequest request) { + return ApiExceptions.callAndTranslateApiException(updateSchemaBundleAsync(request)); + } + + /** + * Asynchronously updates an existing schema bundle with the specified + * configuration. + * + *

+ * Sample code: + * + *

{@code
+   * SchemaBundle existingSchemaBundle = client.getSchemaBundle("my-table", "my-schema-bundle");
+   *
+   * UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(existingSchemaBundle).setDeletionProtection(true);
+   *
+   * ApiFuture future = client.updateSchemaBundleAsync(request);
+   *
+   * ApiFutures.addCallback(
+   *     future,
+   *     new ApiFutureCallback() {
+   *       public void onSuccess(SchemaBundle schemaBundle) {
+   *         System.out.println("Successfully updated the schema bundle: " + schemaBundle.getId());
+   *       }
+   *
+   *       public void onFailure(Throwable t) {
+   *         t.printStackTrace();
+   *       }
+   *     },
+   *     MoreExecutors.directExecutor());
+   * }
+ * + * @see UpdateSchemaBundleRequest for available options. + */ + public ApiFuture updateSchemaBundleAsync(UpdateSchemaBundleRequest request) { + return ApiFutures.transform( + stub.updateSchemaBundleOperationCallable() + .futureCall(request.toProto(projectId, instanceId)), + new ApiFunction() { + @Override + public SchemaBundle apply( + com.google.bigtable.admin.v2.SchemaBundle schemaBundleProto) { + return SchemaBundle.fromProto(schemaBundleProto); + } + }, + MoreExecutors.directExecutor()); + } + + /** + * Gets an schema bundle with the specified schema bundle ID in the specified + * table. + * + *

+ * Sample code: + * + *

{@code
+   * SchemaBundle schemaBundle = client.getSchemaBundle("my-table", "my-schema-bundle");
+   * }
+ */ + public SchemaBundle getSchemaBundle(String tableId, String schemaBundleId) { + return ApiExceptions.callAndTranslateApiException( + getSchemaBundleAsync(tableId, schemaBundleId)); + } + + /** + * Asynchronously gets an schema bundle with the specified schema bundle ID in + * the specified + * table. + * + *

+ * Sample code: + * + *

{@code
+   * ApiFuture future = client.getSchemaBundleAsync("my-table", "my-schema-bundle");
+   *
+   * ApiFutures.addCallback(
+   *     future,
+   *     new ApiFutureCallback() {
+   *       public void onSuccess(SchemaBundle schemaBundle) {
+   *         System.out.println("Successfully get the schema bundle: " + schemaBundle.getId());
+   *       }
+   *
+   *       public void onFailure(Throwable t) {
+   *         t.printStackTrace();
+   *       }
+   *     },
+   *     MoreExecutors.directExecutor());
+   * }
+ */ + public ApiFuture getSchemaBundleAsync(String tableId, String schemaBundleId) { + GetSchemaBundleRequest request = GetSchemaBundleRequest.newBuilder() + .setName( + NameUtil.formatSchemaBundleName(projectId, instanceId, tableId, schemaBundleId)) + .build(); + return ApiFutures.transform( + stub.getSchemaBundleCallable().futureCall(request), + new ApiFunction() { + @Override + public SchemaBundle apply( + com.google.bigtable.admin.v2.SchemaBundle schemaBundleProto) { + return SchemaBundle.fromProto(schemaBundleProto); + } + }, + MoreExecutors.directExecutor()); + } + + /** + * Lists all schema bundle IDs in the specified table. + * + *

+ * Sample code: + * + *

{@code
+   * List schemaBundles = client.listSchemaBundles("my-table");
+   * }
+ */ + public List listSchemaBundles(String tableId) { + return ApiExceptions.callAndTranslateApiException(listSchemaBundlesAsync(tableId)); + } + + /** + * Asynchronously lists all schema bundle IDs in the specified table. + * + *

+ * Sample code: + * + *

{@code
+   * ApiFuture> future = client.listSchemaBundlesAsync("my-table");
+   *
+   * ApiFutures.addCallback(
+   *     future,
+   *     new ApiFutureCallback>() {
+   *       public void onSuccess(List schemaBundleIds) {
+   *         System.out.println("Successfully get list of schema bundles:");
+   *         for (SchemaBundle schemaBundleId : schemaBundleIds) {
+   *           System.out.println(schemaBundleId);
+   *         }
+   *       }
+   *
+   *       public void onFailure(Throwable t) {
+   *         t.printStackTrace();
+   *       }
+   *     },
+   *     MoreExecutors.directExecutor());
+   * }
+ */ + public ApiFuture> listSchemaBundlesAsync(String tableId) { + ListSchemaBundlesRequest request = ListSchemaBundlesRequest.newBuilder() + .setParent(NameUtil.formatTableName(projectId, instanceId, tableId)) + .build(); + + // TODO(igorbernstein2): try to upstream pagination spooling or figure out a way + // to expose the + // paginated responses while maintaining the wrapper facade. + + // Fetches the first page. + ApiFuture firstPageFuture = ApiFutures.transform( + stub.listSchemaBundlesPagedCallable().futureCall(request), + new ApiFunction() { + @Override + public ListSchemaBundlesPage apply(ListSchemaBundlesPagedResponse response) { + return response.getPage(); + } + }, + MoreExecutors.directExecutor()); + + // Fetches the rest of the pages by chaining the futures. + ApiFuture> allProtos = ApiFutures.transformAsync( + firstPageFuture, + new ApiAsyncFunction>() { + List responseAccumulator = Lists.newArrayList(); + + @Override + public ApiFuture> apply( + ListSchemaBundlesPage page) { + // Add all entries from the page + responseAccumulator.addAll(Lists.newArrayList(page.getValues())); + + // If this is the last page, just return the accumulated responses. + if (!page.hasNextPage()) { + return ApiFutures.immediateFuture(responseAccumulator); + } + + // Otherwise fetch the next page. + return ApiFutures.transformAsync( + page.getNextPageAsync(), this, MoreExecutors.directExecutor()); + } + }, + MoreExecutors.directExecutor()); + + // Wraps all of the accumulated protos. + return ApiFutures.transform( + allProtos, + new ApiFunction, List>() { + @Override + public List apply(List protos) { + List results = Lists.newArrayListWithCapacity(protos.size()); + for (com.google.bigtable.admin.v2.SchemaBundle proto : protos) { + results.add(NameUtil.extractSchemaBundleIdFromSchemaBundleName(proto.getName())); + } + return results; + } + }, + MoreExecutors.directExecutor()); + } + + /** + * Deletes an schema bundle with the specified schema bundle ID in the specified + * table. Note + * that the deletion is prohibited if the schema bundle has deletion_protection + * field set to + * true. + * + *

+ * Sample code: + * + *

{@code
+   * client.deleteSchemaBundle("my-table", "my-schema-bundle");
+   * }
+ */ + public void deleteSchemaBundle(String tableId, String schemaBundleId) { + ApiExceptions.callAndTranslateApiException( + deleteSchemaBundleAsync(tableId, schemaBundleId)); + } + + /** + * Asynchronously deletes an schema bundle with the specified schema bundle ID + * in the + * specified table. Note that the deletion is prohibited if the schema bundle + * has + * deletion_protection field set to true. + * + *

+ * Sample code: + * + *

{@code
+   * ApiFuture future = client.deleteSchemaBundleAsync("my-table", "my-schema-bundle");
+   *
+   * ApiFutures.addCallback(
+   *   future,
+   *   new ApiFutureCallback() {
+   *     public void onSuccess(Void ignored) {
+   *       System.out.println("Successfully deleted the schema bundle");
+   *     }
+   *
+   *     public void onFailure(Throwable t) {
+   *       t.printStackTrace();
+   *     }
+   *   },
+   *   MoreExecutors.directExecutor()
+   * );
+   * }
+ */ + public ApiFuture deleteSchemaBundleAsync(String tableId, String schemaBundleId) { + DeleteSchemaBundleRequest request = DeleteSchemaBundleRequest.newBuilder() + .setName( + NameUtil.formatSchemaBundleName(projectId, instanceId, tableId, schemaBundleId)) + .build(); + + return transformToVoid(this.stub.deleteSchemaBundleCallable().futureCall(request)); + } + /** * Helper method to construct the table name in format: * projects/{project}/instances/{instance}/tables/{tableId} diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/internal/NameUtil.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/internal/NameUtil.java index ec2c3a0b585b..a1cdf187d5f8 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/internal/NameUtil.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/internal/NameUtil.java @@ -38,6 +38,9 @@ public class NameUtil { private static final Pattern AUTHORIZED_VIEW_PATTERN = Pattern.compile("projects/([^/]+)/instances/([^/]+)/tables/([^/]+)/authorizedViews/([^/]+)"); + private static final Pattern SCHEMA_BUNDLE_PATTERN = Pattern + .compile("projects/([^/]+)/instances/([^/]+)/tables/([^/]+)/schemaBundles/([^/]+)"); + public static String formatProjectName(String projectId) { return "projects/" + projectId; } @@ -74,6 +77,11 @@ public static String formatAuthorizedViewName( return formatTableName(projectId, instanceId, tableId) + "/authorizedViews/" + viewId; } +public static String formatSchemaBundleName( + String projectId, String instanceId, String tableId, String bundleId) { + return formatTableName(projectId, instanceId, tableId) + "/schemaBundles/" + bundleId; +} + public static String extractTableIdFromTableName(String fullTableName) { Matcher matcher = TABLE_PATTERN.matcher(fullTableName); if (!matcher.matches()) { @@ -99,6 +107,15 @@ public static String extractAuthorizedViewIdFromAuthorizedViewName( return matcher.group(4); } + public static String extractSchemaBundleIdFromSchemaBundleName( + String fullSchemaBundleName) { + Matcher matcher = SCHEMA_BUNDLE_PATTERN.matcher(fullSchemaBundleName); + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid schema bundle name: " + fullSchemaBundleName); + } + return matcher.group(4); + } + public static String extractZoneIdFromLocationName(String fullLocationName) { Matcher matcher = LOCATION_PATTERN.matcher(fullLocationName); if (!matcher.matches()) { diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java new file mode 100644 index 000000000000..d1071f39592f --- /dev/null +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java @@ -0,0 +1,98 @@ +/* + * Copyright 2024 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.cloud.bigtable.admin.v2.internal.NameUtil; +import com.google.common.base.Objects; +import com.google.common.base.Preconditions; +import javax.annotation.Nonnull; + +/** + * Parameters for creating a new Cloud Bigtable {@link SchemaBundle}, which represents subsets of + * a particular table. + * + *

Sample code: + * + *

{@code
+ * CreateSchemaBundleRequest request =
+ *     CreateSchemaBundleRequest.of("my-table", "my-new-schema-bundle")
+ *         .setProtoSchema("proto_file.pb");
+ * }
+ * + * @see SchemaBundle for more details. + */ +public final class CreateSchemaBundleRequest { + private final String tableId; + private final com.google.bigtable.admin.v2.CreateSchemaBundleRequest.Builder requestBuilder = + com.google.bigtable.admin.v2.CreateSchemaBundleRequest.newBuilder(); + + public static CreateSchemaBundleRequest of( + @Nonnull String tableId, @Nonnull String schemaBundleId) { + return new CreateSchemaBundleRequest(tableId, schemaBundleId); + } + + private CreateSchemaBundleRequest(@Nonnull String tableId, @Nonnull String schemaBundleId) { + Preconditions.checkNotNull(tableId, "tableId must be set"); + Preconditions.checkNotNull(schemaBundleId, "schemaBundleId must be set"); + + this.tableId = tableId; + requestBuilder.setSchemaBundleId(schemaBundleId); + } + + /** + * Sets the proto schema for this schema bundle. + * + * @see SchemaBundleType for details. + */ + public CreateSchemaBundleRequest setProtoSchema( + @Nonnull String protoFile) { + Preconditions.checkNotNull(protoFile, "protoSchema must be set"); + + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + CreateSchemaBundleRequest that = (CreateSchemaBundleRequest) o; + return Objects.equal(requestBuilder.build(), that.requestBuilder.build()) + && Objects.equal(tableId, that.tableId); + } + + @Override + public int hashCode() { + return Objects.hashCode(requestBuilder.build(), tableId); + } + + /** + * Creates the request protobuf. This method is considered an internal implementation detail and + * not meant to be used by applications. + */ + @InternalApi + public com.google.bigtable.admin.v2.CreateSchemaBundleRequest toProto( + @Nonnull String projectId, @Nonnull String instanceId) { + return requestBuilder + .setParent(NameUtil.formatTableName(projectId, instanceId, tableId)) + .build(); + } +} diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java new file mode 100644 index 000000000000..186ab7997c6c --- /dev/null +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java @@ -0,0 +1,111 @@ +/* + * Copyright 2024 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.api.core.InternalExtensionOnly; +import com.google.bigtable.admin.v2.SchemaBundleName; +import com.google.common.base.Objects; +import com.google.common.base.Preconditions; +import javax.annotation.Nonnull; + +/** + * A class that wraps the {@link com.google.bigtable.admin.v2.SchemaBundle} protocol buffer + * object. + * + *

An AuthorizedView represents subsets of a particular table based on rules. The access to each + * AuthorizedView can be configured separately from the Table. + * + *

Users can perform read/write operation on an AuthorizedView by providing an authorizedView id + * besides a table id, in which case the semantics remain identical as reading/writing on a Table + * except that visibility is restricted to the subset of the Table that the AuthorizedView + * represents. + */ +public final class SchemaBundle { + private final com.google.bigtable.admin.v2.SchemaBundle proto; + + /** + * Wraps the protobuf. This method is considered an internal implementation detail and not meant + * to be used by applications. + */ + @InternalApi + public static SchemaBundle fromProto( + @Nonnull com.google.bigtable.admin.v2.SchemaBundle proto) { + return new SchemaBundle(proto); + } + + private SchemaBundle(@Nonnull com.google.bigtable.admin.v2.SchemaBundle proto) { + Preconditions.checkNotNull(proto); + Preconditions.checkArgument(!proto.getName().isEmpty(), "SchemaBundle must have a name"); + Preconditions.checkArgument( + proto.hasProtoSchema(), "Schemabundle must have a proto_schema field"); + this.proto = proto; + } + + /** Gets the schema bundle's id. */ + public String getId() { +// Constructor ensures that name is not null. + SchemaBundleName fullName = SchemaBundleName.parse(proto.getName()); + + //noinspection ConstantConditions + return fullName.getSchemaBundle(); + } + + /** Gets the id of the table that owns this schema bundle. */ + public String getTableId() { + // Constructor ensures that name is not null. + SchemaBundleName fullName = SchemaBundleName.parse(proto.getName()); + + //noinspection ConstantConditions + return fullName.getTable(); + } + + /** Gets the proto schema of this schema bundle. */ + public com.google.protobuf.ByteString getProtoSchema() { + if (proto.hasProtoSchema()){ + return proto.getProtoSchema().getProtoDescriptors(); + } + throw new IllegalStateException("This SchemaBundle doesn't have a valid type specified"); + + } + + /** + * Creates the request protobuf. This method is considered an internal implementation detail and + * not meant to be used by applications. + */ + @InternalApi + public com.google.bigtable.admin.v2.SchemaBundle toProto() { + return proto; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + SchemaBundle that = (SchemaBundle) o; + return Objects.equal(proto, that.proto); + } + + @Override + public int hashCode() { + return Objects.hashCode(proto); + } +} diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java new file mode 100644 index 000000000000..b1b3c970b216 --- /dev/null +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java @@ -0,0 +1,131 @@ +/* + * Copyright 2024 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.cloud.bigtable.admin.v2.internal.NameUtil; +import com.google.common.base.Objects; +import com.google.common.base.Preconditions; +import com.google.protobuf.FieldMask; +import com.google.protobuf.util.FieldMaskUtil; +import javax.annotation.Nonnull; + +/** + * Parameters for updating an existing Cloud Bigtable {@link SchemaBundle}. + * + *

Sample code: + * + *

{@code
+ * SchemaBundle existingSchemaBundle = client.getSchemaBundle("my-table", "my-schema-bundle");
+ * UpdateSchemaBundleRequest request =
+ *     UpdateSchemaBundleRequest.of(existingSchemaBundle).setProtoSchema("file.pb");
+ * }
+ * + * @see SchemaBundle for more details. + */ +public final class UpdateSchemaBundleRequest { + private final com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.Builder requestBuilder; + private final String tableId; + private final String schemaBundleId; + + /** Builds a new update request using an existing schema bundle. */ + public static UpdateSchemaBundleRequest of(@Nonnull SchemaBundle schemaBundle) { + return new UpdateSchemaBundleRequest( + schemaBundle.getTableId(), + schemaBundle.getId(), + com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.newBuilder() + .setSchemaBundle(schemaBundle.toProto())); + } + + /** Builds a new update schema bundle request. */ + public static UpdateSchemaBundleRequest of( + @Nonnull String tableId, @Nonnull String schemaBundleId) { + return new UpdateSchemaBundleRequest( + tableId, + schemaBundleId, + com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.newBuilder()); + } + + private UpdateSchemaBundleRequest( + @Nonnull String tableId, + @Nonnull String schemaBundleId, + @Nonnull com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.Builder requestBuilder) { + Preconditions.checkNotNull(tableId, "tableId must be set"); + Preconditions.checkNotNull(schemaBundleId, "schemaBundleId must be set"); + Preconditions.checkNotNull(requestBuilder, "proto builder must be set"); + + this.tableId = tableId; + this.schemaBundleId = schemaBundleId; + this.requestBuilder = requestBuilder; + } + + /** Changes the deletion protection of an existing schema bundle. */ + public UpdateSchemaBundleRequest setProtoSchema(String protoSchemaFile) { + //requestBuilder.getSchemaBundleBuilder().setDeletionProtection(deletionProtection); + updateFieldMask(com.google.bigtable.admin.v2.SchemaBundle.PROTO_SCHEMA_FIELD_NUMBER); + return this; + } + + /** + * Configures if safety warnings should be disabled. If set, then non backwards compatible changes are allowed. + */ + @SuppressWarnings("WeakerAccess") + public UpdateSchemaBundleRequest setIgnoreWarnings(boolean value) { + requestBuilder.setIgnoreWarnings(value); + return this; + } + + private void updateFieldMask(int fieldNumber) { + FieldMask newMask = + FieldMaskUtil.fromFieldNumbers( + com.google.bigtable.admin.v2.SchemaBundle.class, fieldNumber); + requestBuilder.setUpdateMask(FieldMaskUtil.union(requestBuilder.getUpdateMask(), newMask)); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + UpdateSchemaBundleRequest that = (UpdateSchemaBundleRequest) o; + return Objects.equal(requestBuilder.build(), that.requestBuilder.build()) + && Objects.equal(tableId, that.tableId) + && Objects.equal(schemaBundleId, that.schemaBundleId); + } + + @Override + public int hashCode() { + return Objects.hashCode(requestBuilder.build(), tableId, schemaBundleId); + } + + /** + * Creates the request protobuf. This method is considered an internal implementation detail and + * not meant to be used by applications. + */ + @InternalApi + public com.google.bigtable.admin.v2.UpdateSchemaBundleRequest toProto( + @Nonnull String projectId, @Nonnull String instanceId) { + requestBuilder + .getSchemaBundleBuilder() + .setName( + NameUtil.formatSchemaBundleName(projectId, instanceId, tableId, schemaBundleId)); + return requestBuilder.build(); + } +} diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java index 530077721db5..bb5c6b4a0b19 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java @@ -60,6 +60,16 @@ public void formatAuthorizedViewNameTest() { .isEqualTo(testAuthorizedViewName); } + @Test + public void formatSchemabundleNameTest() { + String testSchemaBundleName = "projects/my-project/instances/my-instance/tables/my-table/schemaBundles/my-schema-bundle"; + + assertThat( + NameUtil.formatSchemaBundleName( + "my-project", "my-instance", "my-table", "my-schema-bundle")) + .isEqualTo(testSchemaBundleName); + } + @Test public void formatMaterializedViewNameTest() { String testMaterializedViewName = @@ -92,6 +102,17 @@ public void extractAuthorizedViewIdFromAuthorizedViewNameTest() { NameUtil.extractAuthorizedViewIdFromAuthorizedViewName("bad-format"); } + @Test + public void extractSchemaBundleIdFromSchemaBundleNameTest() { + String testAuthorizedViewName = "projects/my-project/instances/my-instance/tables/my-table/schemaBundles/my-schema-bundle"; + + assertThat(NameUtil.extractSchemaBundleIdFromSchemaBundleName(testAuthorizedViewName)) + .isEqualTo("my-schema-bundle"); + + exception.expect(IllegalArgumentException.class); + NameUtil.extractSchemaBundleIdFromSchemaBundleName("bad-format"); + } + @Test public void extractTableIdFromAuthorizedViewNameTest() { String testAuthorizedViewName = From 4b6b41252c2ed19083d61d71b9a296890c3c6829 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 16:43:55 +0000 Subject: [PATCH 02/27] minor fix --- .../admin/v2/BigtableTableAdminClient.java | 16 ++++++++-------- .../bigtable/admin/v2/internal/NameUtilTest.java | 5 +++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java index 2e04494f9cc5..039ac51f97e9 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java @@ -23,17 +23,17 @@ import com.google.api.gax.rpc.ApiExceptions; import com.google.api.gax.rpc.NotFoundException; import com.google.bigtable.admin.v2.DeleteAuthorizedViewRequest; -import com.google.bigtable.admin.v2.DeleteSchemaBundleRequest; import com.google.bigtable.admin.v2.DeleteBackupRequest; import com.google.bigtable.admin.v2.DeleteTableRequest; import com.google.bigtable.admin.v2.DropRowRangeRequest; +import com.google.bigtable.admin.v2.DeleteSchemaBundleRequest; import com.google.bigtable.admin.v2.GetAuthorizedViewRequest; -import com.google.bigtable.admin.v2.GetSchemaBundleRequest; import com.google.bigtable.admin.v2.GetBackupRequest; +import com.google.bigtable.admin.v2.GetSchemaBundleRequest; import com.google.bigtable.admin.v2.GetTableRequest; import com.google.bigtable.admin.v2.ListAuthorizedViewsRequest; -import com.google.bigtable.admin.v2.ListSchemaBundlesRequest; import com.google.bigtable.admin.v2.ListBackupsRequest; +import com.google.bigtable.admin.v2.ListSchemaBundlesRequest; import com.google.bigtable.admin.v2.ListTablesRequest; import com.google.bigtable.admin.v2.RestoreTableMetadata; import com.google.bigtable.admin.v2.Table.ClusterState; @@ -41,22 +41,21 @@ import com.google.cloud.Policy; import com.google.cloud.Policy.DefaultMarshaller; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListAuthorizedViewsPage; -import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListSchemaBundlesPage; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListAuthorizedViewsPagedResponse; -import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListSchemaBundlesPagedResponse; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListBackupsPage; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListBackupsPagedResponse; +import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListSchemaBundlesPage; +import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListSchemaBundlesPagedResponse; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse; import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.cloud.bigtable.admin.v2.models.AuthorizedView; -import com.google.cloud.bigtable.admin.v2.models.SchemaBundle; import com.google.cloud.bigtable.admin.v2.models.Backup; import com.google.cloud.bigtable.admin.v2.models.ConsistencyRequest; import com.google.cloud.bigtable.admin.v2.models.CopyBackupRequest; import com.google.cloud.bigtable.admin.v2.models.CreateAuthorizedViewRequest; -import com.google.cloud.bigtable.admin.v2.models.CreateSchemaBundleRequest; import com.google.cloud.bigtable.admin.v2.models.CreateBackupRequest; +import com.google.cloud.bigtable.admin.v2.models.CreateSchemaBundleRequest; import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; import com.google.cloud.bigtable.admin.v2.models.EncryptionInfo; import com.google.cloud.bigtable.admin.v2.models.GCRules; @@ -64,10 +63,11 @@ import com.google.cloud.bigtable.admin.v2.models.OptimizeRestoredTableOperationToken; import com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest; import com.google.cloud.bigtable.admin.v2.models.RestoredTableResult; +import com.google.cloud.bigtable.admin.v2.models.SchemaBundle; import com.google.cloud.bigtable.admin.v2.models.Table; import com.google.cloud.bigtable.admin.v2.models.UpdateAuthorizedViewRequest; -import com.google.cloud.bigtable.admin.v2.models.UpdateSchemaBundleRequest; import com.google.cloud.bigtable.admin.v2.models.UpdateBackupRequest; +import com.google.cloud.bigtable.admin.v2.models.UpdateSchemaBundleRequest; import com.google.cloud.bigtable.admin.v2.models.UpdateTableRequest; import com.google.cloud.bigtable.admin.v2.stub.EnhancedBigtableTableAdminStub; import com.google.cloud.bigtable.data.v2.internal.TableAdminRequestContext; diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java index bb5c6b4a0b19..da70572f561f 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java @@ -104,9 +104,10 @@ public void extractAuthorizedViewIdFromAuthorizedViewNameTest() { @Test public void extractSchemaBundleIdFromSchemaBundleNameTest() { - String testAuthorizedViewName = "projects/my-project/instances/my-instance/tables/my-table/schemaBundles/my-schema-bundle"; + String testSchemaBundleName = "projects/my-project/instances/my-instance/tables/my-table/schemaBundles/my-schema-bundle"; - assertThat(NameUtil.extractSchemaBundleIdFromSchemaBundleName(testAuthorizedViewName)) + assertThat(NameUtil.extractSchemaBundleIdFromSchemaBundleName( + testSchemaBundleName)) .isEqualTo("my-schema-bundle"); exception.expect(IllegalArgumentException.class); From e032e575eaeb694e1029617f023fb8420268b106 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 16:46:41 +0000 Subject: [PATCH 03/27] fix format --- .../admin/v2/BigtableTableAdminClient.java | 155 ++++++++---------- .../bigtable/admin/v2/internal/NameUtil.java | 15 +- .../v2/models/CreateSchemaBundleRequest.java | 7 +- .../admin/v2/models/SchemaBundle.java | 14 +- .../v2/models/UpdateSchemaBundleRequest.java | 8 +- .../admin/v2/internal/NameUtilTest.java | 23 +-- 6 files changed, 101 insertions(+), 121 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java index 039ac51f97e9..136fc8f3abbf 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java @@ -24,9 +24,9 @@ import com.google.api.gax.rpc.NotFoundException; import com.google.bigtable.admin.v2.DeleteAuthorizedViewRequest; import com.google.bigtable.admin.v2.DeleteBackupRequest; +import com.google.bigtable.admin.v2.DeleteSchemaBundleRequest; import com.google.bigtable.admin.v2.DeleteTableRequest; import com.google.bigtable.admin.v2.DropRowRangeRequest; -import com.google.bigtable.admin.v2.DeleteSchemaBundleRequest; import com.google.bigtable.admin.v2.GetAuthorizedViewRequest; import com.google.bigtable.admin.v2.GetBackupRequest; import com.google.bigtable.admin.v2.GetSchemaBundleRequest; @@ -1808,8 +1808,7 @@ public ApiFuture deleteAuthorizedViewAsync(String tableId, String authoriz /** * Creates a new schema bundle with the specified configuration. * - *

- * Sample code: + *

Sample code: * *

{@code
    * CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of("my-table", "my-new-schema-bundle")
@@ -1832,8 +1831,7 @@ public SchemaBundle createSchemaBundle(CreateSchemaBundleRequest request) {
   /**
    * Asynchronously creates a new schema bundle with the specified configuration.
    *
-   * 

- * Sample code: + *

Sample code: * *

{@code
    * CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of("my-table", "my-new-schema-bundle")
@@ -1868,8 +1866,7 @@ public ApiFuture createSchemaBundleAsync(CreateSchemaBundleRequest
             .futureCall(request.toProto(projectId, instanceId)),
         new ApiFunction() {
           @Override
-          public SchemaBundle apply(
-              com.google.bigtable.admin.v2.SchemaBundle schemaBundleProto) {
+          public SchemaBundle apply(com.google.bigtable.admin.v2.SchemaBundle schemaBundleProto) {
             return SchemaBundle.fromProto(schemaBundleProto);
           }
         },
@@ -1879,8 +1876,7 @@ public SchemaBundle apply(
   /**
    * Updates an existing schema bundle with the specified configuration.
    *
-   * 

- * Sample code: + *

Sample code: * *

{@code
    * SchemaBundle existingSchemaBundle = client.getSchemaBundle("my-table", "my-schema-bundle");
@@ -1897,11 +1893,9 @@ public SchemaBundle updateSchemaBundle(UpdateSchemaBundleRequest request) {
   }
 
   /**
-   * Asynchronously updates an existing schema bundle with the specified
-   * configuration.
+   * Asynchronously updates an existing schema bundle with the specified configuration.
    *
-   * 

- * Sample code: + *

Sample code: * *

{@code
    * SchemaBundle existingSchemaBundle = client.getSchemaBundle("my-table", "my-schema-bundle");
@@ -1932,8 +1926,7 @@ public ApiFuture updateSchemaBundleAsync(UpdateSchemaBundleRequest
             .futureCall(request.toProto(projectId, instanceId)),
         new ApiFunction() {
           @Override
-          public SchemaBundle apply(
-              com.google.bigtable.admin.v2.SchemaBundle schemaBundleProto) {
+          public SchemaBundle apply(com.google.bigtable.admin.v2.SchemaBundle schemaBundleProto) {
             return SchemaBundle.fromProto(schemaBundleProto);
           }
         },
@@ -1941,11 +1934,9 @@ public SchemaBundle apply(
   }
 
   /**
-   * Gets an schema bundle with the specified schema bundle ID in the specified
-   * table.
+   * Gets an schema bundle with the specified schema bundle ID in the specified table.
    *
-   * 

- * Sample code: + *

Sample code: * *

{@code
    * SchemaBundle schemaBundle = client.getSchemaBundle("my-table", "my-schema-bundle");
@@ -1957,12 +1948,10 @@ public SchemaBundle getSchemaBundle(String tableId, String schemaBundleId) {
   }
 
   /**
-   * Asynchronously gets an schema bundle with the specified schema bundle ID in
-   * the specified
+   * Asynchronously gets an schema bundle with the specified schema bundle ID in the specified
    * table.
    *
-   * 

- * Sample code: + *

Sample code: * *

{@code
    * ApiFuture future = client.getSchemaBundleAsync("my-table", "my-schema-bundle");
@@ -1982,16 +1971,16 @@ public SchemaBundle getSchemaBundle(String tableId, String schemaBundleId) {
    * }
*/ public ApiFuture getSchemaBundleAsync(String tableId, String schemaBundleId) { - GetSchemaBundleRequest request = GetSchemaBundleRequest.newBuilder() - .setName( - NameUtil.formatSchemaBundleName(projectId, instanceId, tableId, schemaBundleId)) - .build(); + GetSchemaBundleRequest request = + GetSchemaBundleRequest.newBuilder() + .setName( + NameUtil.formatSchemaBundleName(projectId, instanceId, tableId, schemaBundleId)) + .build(); return ApiFutures.transform( stub.getSchemaBundleCallable().futureCall(request), new ApiFunction() { @Override - public SchemaBundle apply( - com.google.bigtable.admin.v2.SchemaBundle schemaBundleProto) { + public SchemaBundle apply(com.google.bigtable.admin.v2.SchemaBundle schemaBundleProto) { return SchemaBundle.fromProto(schemaBundleProto); } }, @@ -2001,8 +1990,7 @@ public SchemaBundle apply( /** * Lists all schema bundle IDs in the specified table. * - *

- * Sample code: + *

Sample code: * *

{@code
    * List schemaBundles = client.listSchemaBundles("my-table");
@@ -2015,8 +2003,7 @@ public List listSchemaBundles(String tableId) {
   /**
    * Asynchronously lists all schema bundle IDs in the specified table.
    *
-   * 

- * Sample code: + *

Sample code: * *

{@code
    * ApiFuture> future = client.listSchemaBundlesAsync("my-table");
@@ -2039,48 +2026,53 @@ public List listSchemaBundles(String tableId) {
    * }
*/ public ApiFuture> listSchemaBundlesAsync(String tableId) { - ListSchemaBundlesRequest request = ListSchemaBundlesRequest.newBuilder() - .setParent(NameUtil.formatTableName(projectId, instanceId, tableId)) - .build(); + ListSchemaBundlesRequest request = + ListSchemaBundlesRequest.newBuilder() + .setParent(NameUtil.formatTableName(projectId, instanceId, tableId)) + .build(); // TODO(igorbernstein2): try to upstream pagination spooling or figure out a way // to expose the // paginated responses while maintaining the wrapper facade. // Fetches the first page. - ApiFuture firstPageFuture = ApiFutures.transform( - stub.listSchemaBundlesPagedCallable().futureCall(request), - new ApiFunction() { - @Override - public ListSchemaBundlesPage apply(ListSchemaBundlesPagedResponse response) { - return response.getPage(); - } - }, - MoreExecutors.directExecutor()); + ApiFuture firstPageFuture = + ApiFutures.transform( + stub.listSchemaBundlesPagedCallable().futureCall(request), + new ApiFunction() { + @Override + public ListSchemaBundlesPage apply(ListSchemaBundlesPagedResponse response) { + return response.getPage(); + } + }, + MoreExecutors.directExecutor()); // Fetches the rest of the pages by chaining the futures. - ApiFuture> allProtos = ApiFutures.transformAsync( - firstPageFuture, - new ApiAsyncFunction>() { - List responseAccumulator = Lists.newArrayList(); + ApiFuture> allProtos = + ApiFutures.transformAsync( + firstPageFuture, + new ApiAsyncFunction< + ListSchemaBundlesPage, List>() { + List responseAccumulator = + Lists.newArrayList(); - @Override - public ApiFuture> apply( - ListSchemaBundlesPage page) { - // Add all entries from the page - responseAccumulator.addAll(Lists.newArrayList(page.getValues())); - - // If this is the last page, just return the accumulated responses. - if (!page.hasNextPage()) { - return ApiFutures.immediateFuture(responseAccumulator); - } + @Override + public ApiFuture> apply( + ListSchemaBundlesPage page) { + // Add all entries from the page + responseAccumulator.addAll(Lists.newArrayList(page.getValues())); - // Otherwise fetch the next page. - return ApiFutures.transformAsync( - page.getNextPageAsync(), this, MoreExecutors.directExecutor()); - } - }, - MoreExecutors.directExecutor()); + // If this is the last page, just return the accumulated responses. + if (!page.hasNextPage()) { + return ApiFutures.immediateFuture(responseAccumulator); + } + + // Otherwise fetch the next page. + return ApiFutures.transformAsync( + page.getNextPageAsync(), this, MoreExecutors.directExecutor()); + } + }, + MoreExecutors.directExecutor()); // Wraps all of the accumulated protos. return ApiFutures.transform( @@ -2099,33 +2091,25 @@ public List apply(List protos } /** - * Deletes an schema bundle with the specified schema bundle ID in the specified - * table. Note - * that the deletion is prohibited if the schema bundle has deletion_protection - * field set to - * true. + * Deletes an schema bundle with the specified schema bundle ID in the specified table. Note that + * the deletion is prohibited if the schema bundle has deletion_protection field set to true. * - *

- * Sample code: + *

Sample code: * *

{@code
    * client.deleteSchemaBundle("my-table", "my-schema-bundle");
    * }
*/ public void deleteSchemaBundle(String tableId, String schemaBundleId) { - ApiExceptions.callAndTranslateApiException( - deleteSchemaBundleAsync(tableId, schemaBundleId)); + ApiExceptions.callAndTranslateApiException(deleteSchemaBundleAsync(tableId, schemaBundleId)); } /** - * Asynchronously deletes an schema bundle with the specified schema bundle ID - * in the - * specified table. Note that the deletion is prohibited if the schema bundle - * has - * deletion_protection field set to true. + * Asynchronously deletes an schema bundle with the specified schema bundle ID in the specified + * table. Note that the deletion is prohibited if the schema bundle has deletion_protection field + * set to true. * - *

- * Sample code: + *

Sample code: * *

{@code
    * ApiFuture future = client.deleteSchemaBundleAsync("my-table", "my-schema-bundle");
@@ -2146,10 +2130,11 @@ public void deleteSchemaBundle(String tableId, String schemaBundleId) {
    * }
*/ public ApiFuture deleteSchemaBundleAsync(String tableId, String schemaBundleId) { - DeleteSchemaBundleRequest request = DeleteSchemaBundleRequest.newBuilder() - .setName( - NameUtil.formatSchemaBundleName(projectId, instanceId, tableId, schemaBundleId)) - .build(); + DeleteSchemaBundleRequest request = + DeleteSchemaBundleRequest.newBuilder() + .setName( + NameUtil.formatSchemaBundleName(projectId, instanceId, tableId, schemaBundleId)) + .build(); return transformToVoid(this.stub.deleteSchemaBundleCallable().futureCall(request)); } diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/internal/NameUtil.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/internal/NameUtil.java index a1cdf187d5f8..3918b40dd00d 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/internal/NameUtil.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/internal/NameUtil.java @@ -38,8 +38,8 @@ public class NameUtil { private static final Pattern AUTHORIZED_VIEW_PATTERN = Pattern.compile("projects/([^/]+)/instances/([^/]+)/tables/([^/]+)/authorizedViews/([^/]+)"); - private static final Pattern SCHEMA_BUNDLE_PATTERN = Pattern - .compile("projects/([^/]+)/instances/([^/]+)/tables/([^/]+)/schemaBundles/([^/]+)"); + private static final Pattern SCHEMA_BUNDLE_PATTERN = + Pattern.compile("projects/([^/]+)/instances/([^/]+)/tables/([^/]+)/schemaBundles/([^/]+)"); public static String formatProjectName(String projectId) { return "projects/" + projectId; @@ -77,10 +77,10 @@ public static String formatAuthorizedViewName( return formatTableName(projectId, instanceId, tableId) + "/authorizedViews/" + viewId; } -public static String formatSchemaBundleName( - String projectId, String instanceId, String tableId, String bundleId) { - return formatTableName(projectId, instanceId, tableId) + "/schemaBundles/" + bundleId; -} + public static String formatSchemaBundleName( + String projectId, String instanceId, String tableId, String bundleId) { + return formatTableName(projectId, instanceId, tableId) + "/schemaBundles/" + bundleId; + } public static String extractTableIdFromTableName(String fullTableName) { Matcher matcher = TABLE_PATTERN.matcher(fullTableName); @@ -107,8 +107,7 @@ public static String extractAuthorizedViewIdFromAuthorizedViewName( return matcher.group(4); } - public static String extractSchemaBundleIdFromSchemaBundleName( - String fullSchemaBundleName) { + public static String extractSchemaBundleIdFromSchemaBundleName(String fullSchemaBundleName) { Matcher matcher = SCHEMA_BUNDLE_PATTERN.matcher(fullSchemaBundleName); if (!matcher.matches()) { throw new IllegalArgumentException("Invalid schema bundle name: " + fullSchemaBundleName); diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java index d1071f39592f..50ba884fa6e8 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java @@ -23,8 +23,8 @@ import javax.annotation.Nonnull; /** - * Parameters for creating a new Cloud Bigtable {@link SchemaBundle}, which represents subsets of - * a particular table. + * Parameters for creating a new Cloud Bigtable {@link SchemaBundle}, which represents subsets of a + * particular table. * *

Sample code: * @@ -59,8 +59,7 @@ private CreateSchemaBundleRequest(@Nonnull String tableId, @Nonnull String schem * * @see SchemaBundleType for details. */ - public CreateSchemaBundleRequest setProtoSchema( - @Nonnull String protoFile) { + public CreateSchemaBundleRequest setProtoSchema(@Nonnull String protoFile) { Preconditions.checkNotNull(protoFile, "protoSchema must be set"); return this; diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java index 186ab7997c6c..5dc6e8e97dbd 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java @@ -17,15 +17,13 @@ package com.google.cloud.bigtable.admin.v2.models; import com.google.api.core.InternalApi; -import com.google.api.core.InternalExtensionOnly; import com.google.bigtable.admin.v2.SchemaBundleName; import com.google.common.base.Objects; import com.google.common.base.Preconditions; import javax.annotation.Nonnull; /** - * A class that wraps the {@link com.google.bigtable.admin.v2.SchemaBundle} protocol buffer - * object. + * A class that wraps the {@link com.google.bigtable.admin.v2.SchemaBundle} protocol buffer object. * *

An AuthorizedView represents subsets of a particular table based on rules. The access to each * AuthorizedView can be configured separately from the Table. @@ -43,8 +41,7 @@ public final class SchemaBundle { * to be used by applications. */ @InternalApi - public static SchemaBundle fromProto( - @Nonnull com.google.bigtable.admin.v2.SchemaBundle proto) { + public static SchemaBundle fromProto(@Nonnull com.google.bigtable.admin.v2.SchemaBundle proto) { return new SchemaBundle(proto); } @@ -58,7 +55,7 @@ private SchemaBundle(@Nonnull com.google.bigtable.admin.v2.SchemaBundle proto) { /** Gets the schema bundle's id. */ public String getId() { -// Constructor ensures that name is not null. + // Constructor ensures that name is not null. SchemaBundleName fullName = SchemaBundleName.parse(proto.getName()); //noinspection ConstantConditions @@ -76,11 +73,10 @@ public String getTableId() { /** Gets the proto schema of this schema bundle. */ public com.google.protobuf.ByteString getProtoSchema() { - if (proto.hasProtoSchema()){ - return proto.getProtoSchema().getProtoDescriptors(); + if (proto.hasProtoSchema()) { + return proto.getProtoSchema().getProtoDescriptors(); } throw new IllegalStateException("This SchemaBundle doesn't have a valid type specified"); - } /** diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java index b1b3c970b216..7b81a3d14a78 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java @@ -75,13 +75,14 @@ private UpdateSchemaBundleRequest( /** Changes the deletion protection of an existing schema bundle. */ public UpdateSchemaBundleRequest setProtoSchema(String protoSchemaFile) { - //requestBuilder.getSchemaBundleBuilder().setDeletionProtection(deletionProtection); + // requestBuilder.getSchemaBundleBuilder().setDeletionProtection(deletionProtection); updateFieldMask(com.google.bigtable.admin.v2.SchemaBundle.PROTO_SCHEMA_FIELD_NUMBER); return this; } /** - * Configures if safety warnings should be disabled. If set, then non backwards compatible changes are allowed. + * Configures if safety warnings should be disabled. If set, then non backwards compatible changes + * are allowed. */ @SuppressWarnings("WeakerAccess") public UpdateSchemaBundleRequest setIgnoreWarnings(boolean value) { @@ -124,8 +125,7 @@ public com.google.bigtable.admin.v2.UpdateSchemaBundleRequest toProto( @Nonnull String projectId, @Nonnull String instanceId) { requestBuilder .getSchemaBundleBuilder() - .setName( - NameUtil.formatSchemaBundleName(projectId, instanceId, tableId, schemaBundleId)); + .setName(NameUtil.formatSchemaBundleName(projectId, instanceId, tableId, schemaBundleId)); return requestBuilder.build(); } } diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java index da70572f561f..68f0b590d896 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java @@ -62,12 +62,13 @@ public void formatAuthorizedViewNameTest() { @Test public void formatSchemabundleNameTest() { - String testSchemaBundleName = "projects/my-project/instances/my-instance/tables/my-table/schemaBundles/my-schema-bundle"; + String testSchemaBundleName = + "projects/my-project/instances/my-instance/tables/my-table/schemaBundles/my-schema-bundle"; - assertThat( - NameUtil.formatSchemaBundleName( - "my-project", "my-instance", "my-table", "my-schema-bundle")) - .isEqualTo(testSchemaBundleName); + assertThat( + NameUtil.formatSchemaBundleName( + "my-project", "my-instance", "my-table", "my-schema-bundle")) + .isEqualTo(testSchemaBundleName); } @Test @@ -104,14 +105,14 @@ public void extractAuthorizedViewIdFromAuthorizedViewNameTest() { @Test public void extractSchemaBundleIdFromSchemaBundleNameTest() { - String testSchemaBundleName = "projects/my-project/instances/my-instance/tables/my-table/schemaBundles/my-schema-bundle"; + String testSchemaBundleName = + "projects/my-project/instances/my-instance/tables/my-table/schemaBundles/my-schema-bundle"; - assertThat(NameUtil.extractSchemaBundleIdFromSchemaBundleName( - testSchemaBundleName)) - .isEqualTo("my-schema-bundle"); + assertThat(NameUtil.extractSchemaBundleIdFromSchemaBundleName(testSchemaBundleName)) + .isEqualTo("my-schema-bundle"); - exception.expect(IllegalArgumentException.class); - NameUtil.extractSchemaBundleIdFromSchemaBundleName("bad-format"); + exception.expect(IllegalArgumentException.class); + NameUtil.extractSchemaBundleIdFromSchemaBundleName("bad-format"); } @Test From 4783b5e99d63623290b5d185310c554d7dd63904 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 17:48:24 +0000 Subject: [PATCH 04/27] add tests --- .../models/CreateSchemaBundleRequestTest.java | 86 ++++++++++ .../admin/v2/models/SchemaBundleTest.java | 148 ++++++++++++++++++ .../models/UpdateSchemaBundleRequestTest.java | 117 ++++++++++++++ 3 files changed, 351 insertions(+) create mode 100644 google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java create mode 100644 google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundleTest.java create mode 100644 google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java new file mode 100644 index 000000000000..c39abd2469f6 --- /dev/null +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2024 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; + +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; +import com.google.protobuf.ByteString; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreateSchemaBundleRequestTest { + private static final String PROJECT_ID = "my-project"; + private static final String INSTANCE_ID = "my-instance"; + private static final String TABLE_ID = "my-table"; + private static final String SCHEMA_BUNDLE_ID = "my-schema-bundle"; + + @Test + public void testToProto() { + CreateSchemaBundleRequest request = + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); + + com.google.bigtable.admin.v2.CreateSchemaBundleRequest requestProto = + com.google.bigtable.admin.v2.CreateSchemaBundleRequest.newBuilder() + .setParent(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID)) + .setSchemaBundleId(SCHEMA_BUNDLE_ID) + .setSchemaBundle( + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema")) + .build()) + .build()) + .build(); + assertThat(request.toProto(PROJECT_ID, INSTANCE_ID)).isEqualTo(requestProto); + } + + @Test + public void testEquality() { + CreateSchemaBundleRequest request = + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); + + assertThat(request) + .isEqualTo( + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb")); + + assertThat(request) + .isNotEqualTo( + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema("updated_file.pb")); + } + + @Test + public void testHashCode() { + CreateSchemaBundleRequest request = + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); + + assertThat(request.hashCode()) + .isEqualTo( + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema("file.pb") + .hashCode()); + + assertThat(request.hashCode()) + .isNotEqualTo( + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema("updated_file.pb") + .hashCode()); + } +} diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundleTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundleTest.java new file mode 100644 index 000000000000..09f675aa63af --- /dev/null +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundleTest.java @@ -0,0 +1,148 @@ +/* + * Copyright 2024 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; + +import com.google.bigtable.admin.v2.SchemaBundleName; +import com.google.protobuf.ByteString; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class SchemaBundleTest { + private static final String PROJECT_ID = "my-project"; + private static final String INSTANCE_ID = "my-instance"; + private static final String TABLE_ID = "my-table"; + private static final String SCHEMA_BUNDLE_ID = "my-schema-bundle"; + + @Test + public void testFromProto() { + SchemaBundleName schemaBundleName = + SchemaBundleName.of(PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID); + + com.google.bigtable.admin.v2.SchemaBundle schemaBundleProto = + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName(schemaBundleName.toString()) + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema")) + .build()) + .build(); + + SchemaBundle result = SchemaBundle.fromProto(schemaBundleProto); + + assertThat(result.getId()).isEqualTo(SCHEMA_BUNDLE_ID); + assertThat(result.getTableId()).isEqualTo(TABLE_ID); + assertThat(result.getProtoSchema()).isEqualTo(ByteString.copyFromUtf8("schema")); + } + + @Test + public void testRequiresName() { + com.google.bigtable.admin.v2.SchemaBundle proto = + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema")) + .build()) + .build(); + Exception actualException = null; + + try { + SchemaBundle.fromProto(proto); + } catch (Exception e) { + actualException = e; + } + + assertThat(actualException).isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void testRequiresSchemaBundleType() { + SchemaBundleName schemaBundleName = + SchemaBundleName.of(PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID); + com.google.bigtable.admin.v2.SchemaBundle proto = + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName(schemaBundleName.toString()) + .build(); + Exception actualException = null; + + try { + SchemaBundle.fromProto(proto); + } catch (Exception e) { + actualException = e; + } + + assertThat(actualException).isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void testEquality() { + SchemaBundleName schemaBundleName = + SchemaBundleName.of(PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID); + com.google.bigtable.admin.v2.SchemaBundle proto = + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName(schemaBundleName.toString()) + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema")) + .build()) + .build(); + SchemaBundle schemaBundle = SchemaBundle.fromProto(proto); + + assertThat(schemaBundle).isEqualTo(SchemaBundle.fromProto(proto)); + + assertThat(schemaBundle) + .isNotEqualTo( + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName(schemaBundleName.toString()) + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema")) + .build()) + .build()); + } + + @Test + public void testHashCode() { + SchemaBundleName schemaBundleName = + SchemaBundleName.of(PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID); + com.google.bigtable.admin.v2.SchemaBundle proto = + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName(schemaBundleName.toString()) + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema")) + .build()) + .build(); + SchemaBundle schemaBundle = SchemaBundle.fromProto(proto); + + assertThat(schemaBundle.hashCode()).isEqualTo(SchemaBundle.fromProto(proto).hashCode()); + + assertThat(schemaBundle.hashCode()) + .isNotEqualTo( + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName(schemaBundleName.toString()) + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema")) + .build()) + .build() + .hashCode()); + } +} diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java new file mode 100644 index 000000000000..4a29f849af62 --- /dev/null +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java @@ -0,0 +1,117 @@ +/* + * Copyright 2024 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; + +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; +import com.google.protobuf.ByteString; +import com.google.protobuf.FieldMask; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class UpdateSchemaBundleRequestTest { + private static final String PROJECT_ID = "my-project"; + private static final String INSTANCE_ID = "my-instance"; + private static final String TABLE_ID = "my-table"; + private static final String SCHEMA_BUNDLE_ID = "my-schema-bundle"; + + @Test + public void testToProto() { + UpdateSchemaBundleRequest request = + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema("file.pb") + .setIgnoreWarnings(true); + + com.google.bigtable.admin.v2.UpdateSchemaBundleRequest requestProto = + com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.newBuilder() + .setSchemaBundle( + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName( + NameUtil.formatSchemaBundleName( + PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema")) + .build()) + .build()) + .setUpdateMask(FieldMask.newBuilder().addPaths("proto_schema")) + .setIgnoreWarnings(true) + .build(); + assertThat(request.toProto(PROJECT_ID, INSTANCE_ID)).isEqualTo(requestProto); + } + + @Test + public void testUpdateProtoSchema() { + com.google.bigtable.admin.v2.SchemaBundle existingSchemaBundle = + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName( + NameUtil.formatSchemaBundleName( + PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema")) + .build()) + .build(); + + UpdateSchemaBundleRequest request = + UpdateSchemaBundleRequest.of(SchemaBundle.fromProto(existingSchemaBundle)) + .setProtoSchema("updated_file.pb"); + + com.google.bigtable.admin.v2.UpdateSchemaBundleRequest requestProto = + com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.newBuilder() + .setSchemaBundle(existingSchemaBundle.toBuilder()) + .setUpdateMask(FieldMask.newBuilder().addPaths("proto_schema")) + .build(); + assertThat(request.toProto(PROJECT_ID, INSTANCE_ID)).isEqualTo(requestProto); + } + + @Test + public void testEquality() { + UpdateSchemaBundleRequest request = + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); + + assertThat(request) + .isEqualTo( + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb")); + + assertThat(request) + .isNotEqualTo( + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema("updated_file.pb")); + } + + @Test + public void testHashCode() { + UpdateSchemaBundleRequest request = + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); + + assertThat(request.hashCode()) + .isEqualTo( + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema("file.pb") + .hashCode()); + + assertThat(request.hashCode()) + .isNotEqualTo( + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema("updated_file.pb") + .hashCode()); + } +} From 7cf812ac560b1a0540728f5d7398cba86a4bfc42 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 18:04:00 +0000 Subject: [PATCH 05/27] add tests --- .../v2/BigtableTableAdminClientTests.java | 245 ++++++++++++++++++ 1 file changed, 245 insertions(+) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java index 15713b17f193..e07e50443330 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java @@ -35,6 +35,7 @@ import com.google.bigtable.admin.v2.CopyBackupMetadata; import com.google.bigtable.admin.v2.CreateAuthorizedViewMetadata; import com.google.bigtable.admin.v2.CreateBackupMetadata; +import com.google.bigtable.admin.v2.CreateSchemaBundleMetadata; import com.google.bigtable.admin.v2.DeleteBackupRequest; import com.google.bigtable.admin.v2.DeleteTableRequest; import com.google.bigtable.admin.v2.DropRowRangeRequest; @@ -46,10 +47,12 @@ import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; import com.google.bigtable.admin.v2.RestoreSourceType; import com.google.bigtable.admin.v2.RestoreTableMetadata; +import com.google.bigtable.admin.v2.SchemaBundleName; import com.google.bigtable.admin.v2.Table.ClusterState; import com.google.bigtable.admin.v2.Table.View; import com.google.bigtable.admin.v2.TableName; import com.google.bigtable.admin.v2.UpdateAuthorizedViewMetadata; +import com.google.bigtable.admin.v2.UpdateSchemaBundleMetadata; import com.google.bigtable.admin.v2.UpdateTableMetadata; import com.google.cloud.Identity; import com.google.cloud.Policy; @@ -58,6 +61,8 @@ import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListAuthorizedViewsPagedResponse; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListBackupsPage; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListBackupsPagedResponse; +import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListSchemaBundlesPage; +import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListSchemaBundlesPagedResponse; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse; import com.google.cloud.bigtable.admin.v2.internal.NameUtil; @@ -67,16 +72,19 @@ import com.google.cloud.bigtable.admin.v2.models.CopyBackupRequest; import com.google.cloud.bigtable.admin.v2.models.CreateAuthorizedViewRequest; import com.google.cloud.bigtable.admin.v2.models.CreateBackupRequest; +import com.google.cloud.bigtable.admin.v2.models.CreateSchemaBundleRequest; import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; import com.google.cloud.bigtable.admin.v2.models.EncryptionInfo; import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest; import com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest; import com.google.cloud.bigtable.admin.v2.models.RestoredTableResult; +import com.google.cloud.bigtable.admin.v2.models.SchemaBundle; import com.google.cloud.bigtable.admin.v2.models.SubsetView; import com.google.cloud.bigtable.admin.v2.models.Table; import com.google.cloud.bigtable.admin.v2.models.Type; import com.google.cloud.bigtable.admin.v2.models.UpdateAuthorizedViewRequest; import com.google.cloud.bigtable.admin.v2.models.UpdateBackupRequest; +import com.google.cloud.bigtable.admin.v2.models.UpdateSchemaBundleRequest; import com.google.cloud.bigtable.admin.v2.stub.EnhancedBigtableTableAdminStub; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; @@ -122,6 +130,7 @@ public class BigtableTableAdminClientTests { private static final String CLUSTER_ID = "my-cluster"; private static final String BACKUP_ID = "my-backup"; private static final String AUTHORIZED_VIEW_ID = "my-authorized-view"; + private static final String SCHEMA_BUNDLE_ID = "my-schema-bundle"; private static final String INSTANCE_NAME = NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID); private static final String TABLE_NAME = @@ -225,6 +234,35 @@ public class BigtableTableAdminClientTests { private UnaryCallable mockDeleteAuthorizedViewCallable; + @Mock + private OperationCallable< + com.google.bigtable.admin.v2.CreateSchemaBundleRequest, + com.google.bigtable.admin.v2.SchemaBundle, + CreateSchemaBundleMetadata> + mockCreateSchemaBundleOperationCallable; + + @Mock + private OperationCallable< + com.google.bigtable.admin.v2.UpdateSchemaBundleRequest, + com.google.bigtable.admin.v2.SchemaBundle, + UpdateSchemaBundleMetadata> + mockUpdateSchemaBundleOperationCallable; + + @Mock + private UnaryCallable< + com.google.bigtable.admin.v2.GetSchemaBundleRequest, + com.google.bigtable.admin.v2.SchemaBundle> + mockGetSchemaBundleCallable; + + @Mock + private UnaryCallable< + com.google.bigtable.admin.v2.ListSchemaBundlesRequest, ListSchemaBundlesPagedResponse> + mockListSchemaBundlesCallable; + + @Mock + private UnaryCallable + mockDeleteSchemaBundleCallable; + @Mock private UnaryCallable mockGetIamPolicyCallable; @@ -1291,6 +1329,213 @@ public void testDeleteAuthorizedView() { assertThat(wasCalled.get()).isTrue(); } + @Test + public void testCreateSchemaBundle() { + // Setup + Mockito.when(mockStub.createSchemaBundleOperationCallable()) + .thenReturn(mockCreateSchemaBundleOperationCallable); + + com.google.bigtable.admin.v2.CreateSchemaBundleRequest expectedRequest = + com.google.bigtable.admin.v2.CreateSchemaBundleRequest.newBuilder() + .setParent(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID)) + .setSchemaBundleId(SCHEMA_BUNDLE_ID) + .setSchemaBundle( + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema")))) + .build(); + + com.google.bigtable.admin.v2.SchemaBundle expectedResponse = + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName( + NameUtil.formatSchemaBundleName( + PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema"))) + .build(); + + mockOperationResult( + mockCreateSchemaBundleOperationCallable, + expectedRequest, + expectedResponse, + CreateSchemaBundleMetadata.newBuilder() + .setName(expectedRequest.getSchemaBundle().getName()) + .build()); + + CreateSchemaBundleRequest req = + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); + + // Execute + SchemaBundle actualResult = adminClient.createSchemaBundle(req); + + // Verify + assertThat(actualResult).isEqualTo(SchemaBundle.fromProto(expectedResponse)); + } + + @Test + public void testUpdateSchemaBundle() { + // Setup + Mockito.when(mockStub.updateSchemaBundleOperationCallable()) + .thenReturn(mockUpdateSchemaBundleOperationCallable); + + com.google.bigtable.admin.v2.UpdateSchemaBundleRequest expectedRequest = + com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.newBuilder() + .setSchemaBundle( + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName( + NameUtil.formatSchemaBundleName( + PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema"))) + .build()) + .setUpdateMask(FieldMask.newBuilder().addPaths("proto_schema")) + .build(); + + com.google.bigtable.admin.v2.SchemaBundle expectedResponse = + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName( + NameUtil.formatSchemaBundleName( + PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema"))) + .build(); + + mockOperationResult( + mockUpdateSchemaBundleOperationCallable, + expectedRequest, + expectedResponse, + UpdateSchemaBundleMetadata.newBuilder() + .setName(expectedRequest.getSchemaBundle().getName()) + .build()); + + UpdateSchemaBundleRequest req = + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("updated_file.pb"); + + // Execute + SchemaBundle actualResult = adminClient.updateSchemaBundle(req); + + // Verify + assertThat(actualResult).isEqualTo(SchemaBundle.fromProto(expectedResponse)); + } + + @Test + public void testGetSchemaBundle() { + // Setup + Mockito.when(mockStub.getSchemaBundleCallable()).thenReturn(mockGetSchemaBundleCallable); + + com.google.bigtable.admin.v2.GetSchemaBundleRequest expectedRequest = + com.google.bigtable.admin.v2.GetSchemaBundleRequest.newBuilder() + .setName( + NameUtil.formatSchemaBundleName( + PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) + .build(); + + com.google.bigtable.admin.v2.SchemaBundle expectedResponse = + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName( + NameUtil.formatSchemaBundleName( + PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) + .setProtoSchema( + com.google.bigtable.admin.v2.ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFromUtf8("schema"))) + .build(); + + Mockito.when(mockGetSchemaBundleCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(expectedResponse)); + + // Execute + SchemaBundle actualResult = adminClient.getSchemaBundle(TABLE_ID, SCHEMA_BUNDLE_ID); + + // Verify + assertThat(actualResult).isEqualTo(SchemaBundle.fromProto(expectedResponse)); + } + + @Test + public void testListSchemaBundles() { + // Setup + Mockito.when(mockStub.listSchemaBundlesPagedCallable()) + .thenReturn(mockListSchemaBundlesCallable); + + com.google.bigtable.admin.v2.ListSchemaBundlesRequest expectedRequest = + com.google.bigtable.admin.v2.ListSchemaBundlesRequest.newBuilder() + .setParent(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID)) + .build(); + + // 3 SchemaBundles spread across 2 pages + List expectedProtos = Lists.newArrayList(); + for (int i = 0; i < 3; i++) { + expectedProtos.add( + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setName( + NameUtil.formatSchemaBundleName( + PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID + i)) + .build()); + } + + // 2 on the first page + ListSchemaBundlesPage page0 = Mockito.mock(ListSchemaBundlesPage.class); + Mockito.when(page0.getValues()).thenReturn(expectedProtos.subList(0, 2)); + Mockito.when(page0.hasNextPage()).thenReturn(true); + + // 1 on the last page + ListSchemaBundlesPage page1 = Mockito.mock(ListSchemaBundlesPage.class); + Mockito.when(page1.getValues()).thenReturn(expectedProtos.subList(2, 3)); + + // Link page0 to page1 + Mockito.when(page0.getNextPageAsync()).thenReturn(ApiFutures.immediateFuture(page1)); + + // Link page to the response + ListSchemaBundlesPagedResponse response0 = Mockito.mock(ListSchemaBundlesPagedResponse.class); + Mockito.when(response0.getPage()).thenReturn(page0); + + Mockito.when(mockListSchemaBundlesCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(response0)); + + // Execute + List actualResults = adminClient.listSchemaBundles(TABLE_ID); + + // Verify + List expectedResults = Lists.newArrayList(); + for (com.google.bigtable.admin.v2.SchemaBundle expectedProto : expectedProtos) { + expectedResults.add(SchemaBundleName.parse(expectedProto.getName()).getSchemaBundle()); + } + + assertThat(actualResults).containsExactlyElementsIn(expectedResults); + } + + @Test + public void testDeleteSchemaBundle() { + // Setup + Mockito.when(mockStub.deleteSchemaBundleCallable()).thenReturn(mockDeleteSchemaBundleCallable); + + com.google.bigtable.admin.v2.DeleteSchemaBundleRequest expectedRequest = + com.google.bigtable.admin.v2.DeleteSchemaBundleRequest.newBuilder() + .setName( + NameUtil.formatSchemaBundleName( + PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) + .build(); + + final AtomicBoolean wasCalled = new AtomicBoolean(false); + + Mockito.when(mockDeleteSchemaBundleCallable.futureCall(expectedRequest)) + .thenAnswer( + (Answer>) + invocationOnMock -> { + wasCalled.set(true); + return ApiFutures.immediateFuture(Empty.getDefaultInstance()); + }); + + // Execute + adminClient.deleteSchemaBundle(TABLE_ID, SCHEMA_BUNDLE_ID); + + // Verify + assertThat(wasCalled.get()).isTrue(); + } + @Test public void testGetBackupIamPolicy() { // Setup From fa1376d4d3f459eaa460557545d6c3a19eaf56ff Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 18:35:55 +0000 Subject: [PATCH 06/27] handled file --- .../v2/models/CreateSchemaBundleRequest.java | 18 +++++++++++++++--- .../v2/models/UpdateSchemaBundleRequest.java | 15 +++++++++++++-- .../v2/BigtableTableAdminClientTests.java | 6 ++++-- .../models/CreateSchemaBundleRequestTest.java | 8 +++++--- .../models/UpdateSchemaBundleRequestTest.java | 10 ++++++---- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java index 50ba884fa6e8..fa1d3455832f 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java @@ -17,9 +17,16 @@ package com.google.cloud.bigtable.admin.v2.models; import com.google.api.core.InternalApi; +import com.google.bigtable.admin.v2.ProtoSchema; import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.common.base.Objects; import com.google.common.base.Preconditions; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + import javax.annotation.Nonnull; /** @@ -59,9 +66,14 @@ private CreateSchemaBundleRequest(@Nonnull String tableId, @Nonnull String schem * * @see SchemaBundleType for details. */ - public CreateSchemaBundleRequest setProtoSchema(@Nonnull String protoFile) { - Preconditions.checkNotNull(protoFile, "protoSchema must be set"); - + public CreateSchemaBundleRequest setProtoSchema(@Nonnull String protoSchemaFile) + throws IOException { + Preconditions.checkNotNull(protoSchemaFile, "protoSchema must be set"); + byte[] content = Files.readAllBytes(Paths.get(protoSchemaFile)); + requestBuilder.setSchemaBundle( + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setProtoSchema( + ProtoSchema.newBuilder().setProtoDescriptors(ByteString.copyFrom(content)))); return this; } diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java index 7b81a3d14a78..1e3ca0572c9f 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java @@ -17,11 +17,18 @@ package com.google.cloud.bigtable.admin.v2.models; import com.google.api.core.InternalApi; +import com.google.bigtable.admin.v2.ProtoSchema; import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.common.base.Objects; import com.google.common.base.Preconditions; +import com.google.protobuf.ByteString; import com.google.protobuf.FieldMask; import com.google.protobuf.util.FieldMaskUtil; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + import javax.annotation.Nonnull; /** @@ -74,8 +81,12 @@ private UpdateSchemaBundleRequest( } /** Changes the deletion protection of an existing schema bundle. */ - public UpdateSchemaBundleRequest setProtoSchema(String protoSchemaFile) { - // requestBuilder.getSchemaBundleBuilder().setDeletionProtection(deletionProtection); + public UpdateSchemaBundleRequest setProtoSchema(String protoSchemaFile) throws IOException { + byte[] content = Files.readAllBytes(Paths.get(protoSchemaFile)); + requestBuilder.setSchemaBundle( + com.google.bigtable.admin.v2.SchemaBundle.newBuilder() + .setProtoSchema( + ProtoSchema.newBuilder().setProtoDescriptors(ByteString.copyFrom(content)))); updateFieldMask(com.google.bigtable.admin.v2.SchemaBundle.PROTO_SCHEMA_FIELD_NUMBER); return this; } diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java index e07e50443330..16ae6e9cf48c 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java @@ -98,6 +98,8 @@ import com.google.protobuf.util.Timestamps; import io.grpc.Status; import io.grpc.Status.Code; + +import java.io.IOException; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; @@ -1330,7 +1332,7 @@ public void testDeleteAuthorizedView() { } @Test - public void testCreateSchemaBundle() { + public void testCreateSchemaBundle() throws IOException { // Setup Mockito.when(mockStub.createSchemaBundleOperationCallable()) .thenReturn(mockCreateSchemaBundleOperationCallable); @@ -1375,7 +1377,7 @@ public void testCreateSchemaBundle() { } @Test - public void testUpdateSchemaBundle() { + public void testUpdateSchemaBundle() throws IOException { // Setup Mockito.when(mockStub.updateSchemaBundleOperationCallable()) .thenReturn(mockUpdateSchemaBundleOperationCallable); diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java index c39abd2469f6..000b45858c93 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java @@ -18,6 +18,8 @@ import static com.google.common.truth.Truth.assertThat; +import java.io.IOException; + import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.protobuf.ByteString; import org.junit.Test; @@ -32,7 +34,7 @@ public class CreateSchemaBundleRequestTest { private static final String SCHEMA_BUNDLE_ID = "my-schema-bundle"; @Test - public void testToProto() { + public void testToProto() throws IOException{ CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); @@ -52,7 +54,7 @@ public void testToProto() { } @Test - public void testEquality() { + public void testEquality() throws IOException{ CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); @@ -67,7 +69,7 @@ public void testEquality() { } @Test - public void testHashCode() { + public void testHashCode() throws IOException{ CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java index 4a29f849af62..2031ec34cb5c 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java @@ -18,6 +18,8 @@ import static com.google.common.truth.Truth.assertThat; +import java.io.IOException; + import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.protobuf.ByteString; import com.google.protobuf.FieldMask; @@ -33,7 +35,7 @@ public class UpdateSchemaBundleRequestTest { private static final String SCHEMA_BUNDLE_ID = "my-schema-bundle"; @Test - public void testToProto() { + public void testToProto() throws IOException{ UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) .setProtoSchema("file.pb") @@ -58,7 +60,7 @@ public void testToProto() { } @Test - public void testUpdateProtoSchema() { + public void testUpdateProtoSchema() throws IOException{ com.google.bigtable.admin.v2.SchemaBundle existingSchemaBundle = com.google.bigtable.admin.v2.SchemaBundle.newBuilder() .setName( @@ -83,7 +85,7 @@ public void testUpdateProtoSchema() { } @Test - public void testEquality() { + public void testEquality() throws IOException{ UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); @@ -98,7 +100,7 @@ public void testEquality() { } @Test - public void testHashCode() { + public void testHashCode() throws IOException{ UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); From 9d83ed773de0f19ccdf518d3c9e01a823a559cf3 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 18:48:27 +0000 Subject: [PATCH 07/27] added files --- .../v2/models/CreateSchemaBundleRequest.java | 2 -- .../v2/models/UpdateSchemaBundleRequest.java | 2 -- .../v2/BigtableTableAdminClientTests.java | 8 +++-- .../models/CreateSchemaBundleRequestTest.java | 31 ++++++++++------- .../models/UpdateSchemaBundleRequestTest.java | 34 +++++++++++-------- .../src/test/resources/proto_schema_bundle.pb | 6 ++++ .../test/resources/proto_schema_bundle.proto | 22 ++++++++++++ .../resources/updated_proto_schema_bundle.pb | 7 ++++ .../updated_proto_schema_bundle.proto | 23 +++++++++++++ 9 files changed, 103 insertions(+), 32 deletions(-) create mode 100644 google-cloud-bigtable/src/test/resources/proto_schema_bundle.pb create mode 100644 google-cloud-bigtable/src/test/resources/proto_schema_bundle.proto create mode 100644 google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.pb create mode 100644 google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.proto diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java index fa1d3455832f..7ae70a70eb5b 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java @@ -24,9 +24,7 @@ import com.google.protobuf.ByteString; import java.io.IOException; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; - import javax.annotation.Nonnull; /** diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java index 1e3ca0572c9f..8d09e563b72d 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java @@ -26,9 +26,7 @@ import com.google.protobuf.util.FieldMaskUtil; import java.io.IOException; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; - import javax.annotation.Nonnull; /** diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java index 16ae6e9cf48c..99404ebbce26 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java @@ -133,6 +133,10 @@ public class BigtableTableAdminClientTests { private static final String BACKUP_ID = "my-backup"; private static final String AUTHORIZED_VIEW_ID = "my-authorized-view"; private static final String SCHEMA_BUNDLE_ID = "my-schema-bundle"; + // Location: `google-cloud-bigtable/src/test/resources/proto_schema_bundle.pb` + private static final String TEST_PROTO_SCHEMA_BUNDLE = "proto_schema_bundle.pb"; + // Location: `google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.pb` + private static final String TEST_UPDATED_PROTO_SCHEMA_BUNDLE = "updated_proto_schema_bundle.pb"; private static final String INSTANCE_NAME = NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID); private static final String TABLE_NAME = @@ -1367,7 +1371,7 @@ public void testCreateSchemaBundle() throws IOException { .build()); CreateSchemaBundleRequest req = - CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); // Execute SchemaBundle actualResult = adminClient.createSchemaBundle(req); @@ -1415,7 +1419,7 @@ public void testUpdateSchemaBundle() throws IOException { .build()); UpdateSchemaBundleRequest req = - UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("updated_file.pb"); + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE); // Execute SchemaBundle actualResult = adminClient.updateSchemaBundle(req); diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java index 000b45858c93..dbb00c00fb3f 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java @@ -18,10 +18,9 @@ import static com.google.common.truth.Truth.assertThat; -import java.io.IOException; - import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.protobuf.ByteString; +import java.io.IOException; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -32,11 +31,16 @@ public class CreateSchemaBundleRequestTest { private static final String INSTANCE_ID = "my-instance"; private static final String TABLE_ID = "my-table"; private static final String SCHEMA_BUNDLE_ID = "my-schema-bundle"; + // Location: `google-cloud-bigtable/src/test/resources/proto_schema_bundle.pb` + private static final String TEST_PROTO_SCHEMA_BUNDLE = "proto_schema_bundle.pb"; + // Location: `google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.pb` + private static final String TEST_UPDATED_PROTO_SCHEMA_BUNDLE = "updated_proto_schema_bundle.pb"; @Test - public void testToProto() throws IOException{ + public void testToProto() throws IOException { CreateSchemaBundleRequest request = - CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); com.google.bigtable.admin.v2.CreateSchemaBundleRequest requestProto = com.google.bigtable.admin.v2.CreateSchemaBundleRequest.newBuilder() @@ -54,35 +58,38 @@ public void testToProto() throws IOException{ } @Test - public void testEquality() throws IOException{ + public void testEquality() throws IOException { CreateSchemaBundleRequest request = - CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); assertThat(request) .isEqualTo( - CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb")); + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE)); assertThat(request) .isNotEqualTo( CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema("updated_file.pb")); + .setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); } @Test - public void testHashCode() throws IOException{ + public void testHashCode() throws IOException { CreateSchemaBundleRequest request = - CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); assertThat(request.hashCode()) .isEqualTo( CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema("file.pb") + .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE) .hashCode()); assertThat(request.hashCode()) .isNotEqualTo( CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema("updated_file.pb") + .setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE) .hashCode()); } } diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java index 2031ec34cb5c..0a5c427e775e 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java @@ -18,11 +18,10 @@ import static com.google.common.truth.Truth.assertThat; -import java.io.IOException; - import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.protobuf.ByteString; import com.google.protobuf.FieldMask; +import java.io.IOException; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -33,12 +32,16 @@ public class UpdateSchemaBundleRequestTest { private static final String INSTANCE_ID = "my-instance"; private static final String TABLE_ID = "my-table"; private static final String SCHEMA_BUNDLE_ID = "my-schema-bundle"; + // Location: `google-cloud-bigtable/src/test/resources/proto_schema_bundle.pb` + private static final String TEST_PROTO_SCHEMA_BUNDLE = "proto_schema_bundle.pb"; + // Location: `google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.pb` + private static final String TEST_UPDATED_PROTO_SCHEMA_BUNDLE = "updated_proto_schema_bundle.pb"; @Test - public void testToProto() throws IOException{ + public void testToProto() throws IOException { UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema("file.pb") + .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE) .setIgnoreWarnings(true); com.google.bigtable.admin.v2.UpdateSchemaBundleRequest requestProto = @@ -60,7 +63,7 @@ public void testToProto() throws IOException{ } @Test - public void testUpdateProtoSchema() throws IOException{ + public void testUpdateProtoSchema() throws IOException { com.google.bigtable.admin.v2.SchemaBundle existingSchemaBundle = com.google.bigtable.admin.v2.SchemaBundle.newBuilder() .setName( @@ -74,7 +77,7 @@ public void testUpdateProtoSchema() throws IOException{ UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(SchemaBundle.fromProto(existingSchemaBundle)) - .setProtoSchema("updated_file.pb"); + .setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE); com.google.bigtable.admin.v2.UpdateSchemaBundleRequest requestProto = com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.newBuilder() @@ -85,35 +88,38 @@ public void testUpdateProtoSchema() throws IOException{ } @Test - public void testEquality() throws IOException{ + public void testEquality() throws IOException { UpdateSchemaBundleRequest request = - UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); assertThat(request) .isEqualTo( - UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb")); + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE)); assertThat(request) .isNotEqualTo( UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema("updated_file.pb")); + .setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); } @Test - public void testHashCode() throws IOException{ + public void testHashCode() throws IOException { UpdateSchemaBundleRequest request = - UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema("file.pb"); + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); assertThat(request.hashCode()) .isEqualTo( UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema("file.pb") + .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE) .hashCode()); assertThat(request.hashCode()) .isNotEqualTo( UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema("updated_file.pb") + .setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE) .hashCode()); } } diff --git a/google-cloud-bigtable/src/test/resources/proto_schema_bundle.pb b/google-cloud-bigtable/src/test/resources/proto_schema_bundle.pb new file mode 100644 index 000000000000..c9ac4086f5d7 --- /dev/null +++ b/google-cloud-bigtable/src/test/resources/proto_schema_bundle.pb @@ -0,0 +1,6 @@ + +q +proto_schema_bundle.proto#gcloud.bigtable.schema_bundles.test"' +Author + +first_name ( R firstNamebproto3 \ No newline at end of file diff --git a/google-cloud-bigtable/src/test/resources/proto_schema_bundle.proto b/google-cloud-bigtable/src/test/resources/proto_schema_bundle.proto new file mode 100644 index 000000000000..e03f0ccccbef --- /dev/null +++ b/google-cloud-bigtable/src/test/resources/proto_schema_bundle.proto @@ -0,0 +1,22 @@ +/* +Copyright 2025 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 + http://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. +*/ +// The `proto_schema_bundle.pb` binary is generated from this source file, via command: +// protoc --include_imports --descriptor_set_out=proto_schema_bundle.pb proto_schema_bundle.proto + +syntax = "proto3"; + +package gcloud.bigtable.schema_bundles.test; + +message Author { + string first_name = 1; +} diff --git a/google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.pb b/google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.pb new file mode 100644 index 000000000000..21f877a2fe11 --- /dev/null +++ b/google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.pb @@ -0,0 +1,7 @@ + +– +!updated_proto_schema_bundle.proto#gcloud.bigtable.schema_bundles.test"D +Author + +first_name ( R firstName + last_name ( RlastNamebproto3 \ No newline at end of file diff --git a/google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.proto b/google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.proto new file mode 100644 index 000000000000..e9894cabd20a --- /dev/null +++ b/google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.proto @@ -0,0 +1,23 @@ +/* +Copyright 2025 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 + http://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. +*/ +// The `updated_proto_schema_bundle.pb` binary is generated from this source file, via command: +// protoc --include_imports --descriptor_set_out=updated_proto_schema_bundle.pb updated_proto_schema_bundle.proto + +syntax = "proto3"; + +package gcloud.bigtable.schema_bundles.test; + +message Author { + string first_name = 1; + string last_name = 2; +} From 39098e4361b350a19883eab3485c62d9c92eca6f Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 18:49:28 +0000 Subject: [PATCH 08/27] fix format --- .../bigtable/admin/v2/BigtableTableAdminClientTests.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java index 99404ebbce26..dabba632e8fa 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java @@ -98,7 +98,6 @@ import com.google.protobuf.util.Timestamps; import io.grpc.Status; import io.grpc.Status.Code; - import java.io.IOException; import java.util.List; import java.util.Map; @@ -1371,7 +1370,8 @@ public void testCreateSchemaBundle() throws IOException { .build()); CreateSchemaBundleRequest req = - CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); + CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); // Execute SchemaBundle actualResult = adminClient.createSchemaBundle(req); @@ -1419,7 +1419,8 @@ public void testUpdateSchemaBundle() throws IOException { .build()); UpdateSchemaBundleRequest req = - UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID).setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE); + UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) + .setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE); // Execute SchemaBundle actualResult = adminClient.updateSchemaBundle(req); From be4412b36f78c16deaba412b6b2b8d5b39ca2d00 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 18:59:36 +0000 Subject: [PATCH 09/27] fix files --- .../bigtable/admin/v2/models/CreateSchemaBundleRequest.java | 4 ++-- .../bigtable/admin/v2/models/UpdateSchemaBundleRequest.java | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java index 7ae70a70eb5b..29d6c8356e7d 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java @@ -24,7 +24,7 @@ import com.google.protobuf.ByteString; import java.io.IOException; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import javax.annotation.Nonnull; /** @@ -67,7 +67,7 @@ private CreateSchemaBundleRequest(@Nonnull String tableId, @Nonnull String schem public CreateSchemaBundleRequest setProtoSchema(@Nonnull String protoSchemaFile) throws IOException { Preconditions.checkNotNull(protoSchemaFile, "protoSchema must be set"); - byte[] content = Files.readAllBytes(Paths.get(protoSchemaFile)); + byte[] content = Files.readAllBytes(Path.of(protoSchemaFile)); requestBuilder.setSchemaBundle( com.google.bigtable.admin.v2.SchemaBundle.newBuilder() .setProtoSchema( diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java index 8d09e563b72d..2f5f567d24dd 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java @@ -26,6 +26,7 @@ import com.google.protobuf.util.FieldMaskUtil; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import javax.annotation.Nonnull; @@ -80,7 +81,7 @@ private UpdateSchemaBundleRequest( /** Changes the deletion protection of an existing schema bundle. */ public UpdateSchemaBundleRequest setProtoSchema(String protoSchemaFile) throws IOException { - byte[] content = Files.readAllBytes(Paths.get(protoSchemaFile)); + byte[] content = Files.readAllBytes(Path.of(protoSchemaFile)); requestBuilder.setSchemaBundle( com.google.bigtable.admin.v2.SchemaBundle.newBuilder() .setProtoSchema( From 53daf124ea5b541b0b4bd775daaab30251953084 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 19:00:41 +0000 Subject: [PATCH 10/27] fix comment --- .../cloud/bigtable/admin/v2/models/SchemaBundle.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java index 5dc6e8e97dbd..0fcc43989fc4 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java @@ -24,14 +24,6 @@ /** * A class that wraps the {@link com.google.bigtable.admin.v2.SchemaBundle} protocol buffer object. - * - *

An AuthorizedView represents subsets of a particular table based on rules. The access to each - * AuthorizedView can be configured separately from the Table. - * - *

Users can perform read/write operation on an AuthorizedView by providing an authorizedView id - * besides a table id, in which case the semantics remain identical as reading/writing on a Table - * except that visibility is restricted to the subset of the Table that the AuthorizedView - * represents. */ public final class SchemaBundle { private final com.google.bigtable.admin.v2.SchemaBundle proto; From a8e0bd8630e17a09df00a84427b9c0950ce8eb41 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 19:03:55 +0000 Subject: [PATCH 11/27] fix --- .../bigtable/admin/v2/models/CreateSchemaBundleRequest.java | 4 ++-- .../bigtable/admin/v2/models/UpdateSchemaBundleRequest.java | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java index 29d6c8356e7d..7ae70a70eb5b 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java @@ -24,7 +24,7 @@ import com.google.protobuf.ByteString; import java.io.IOException; import java.nio.file.Files; -import java.nio.file.Path; +import java.nio.file.Paths; import javax.annotation.Nonnull; /** @@ -67,7 +67,7 @@ private CreateSchemaBundleRequest(@Nonnull String tableId, @Nonnull String schem public CreateSchemaBundleRequest setProtoSchema(@Nonnull String protoSchemaFile) throws IOException { Preconditions.checkNotNull(protoSchemaFile, "protoSchema must be set"); - byte[] content = Files.readAllBytes(Path.of(protoSchemaFile)); + byte[] content = Files.readAllBytes(Paths.get(protoSchemaFile)); requestBuilder.setSchemaBundle( com.google.bigtable.admin.v2.SchemaBundle.newBuilder() .setProtoSchema( diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java index 2f5f567d24dd..8d09e563b72d 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java @@ -26,7 +26,6 @@ import com.google.protobuf.util.FieldMaskUtil; import java.io.IOException; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; import javax.annotation.Nonnull; @@ -81,7 +80,7 @@ private UpdateSchemaBundleRequest( /** Changes the deletion protection of an existing schema bundle. */ public UpdateSchemaBundleRequest setProtoSchema(String protoSchemaFile) throws IOException { - byte[] content = Files.readAllBytes(Path.of(protoSchemaFile)); + byte[] content = Files.readAllBytes(Paths.get(protoSchemaFile)); requestBuilder.setSchemaBundle( com.google.bigtable.admin.v2.SchemaBundle.newBuilder() .setProtoSchema( From 322d7c9356109f82b8df1df083f72b9840e922c4 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 19:12:55 +0000 Subject: [PATCH 12/27] fix test --- .../admin/v2/models/CreateSchemaBundleRequestTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java index dbb00c00fb3f..d4664fd1fd88 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java @@ -21,6 +21,9 @@ import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.protobuf.ByteString; import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Paths; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -37,10 +40,13 @@ public class CreateSchemaBundleRequestTest { private static final String TEST_UPDATED_PROTO_SCHEMA_BUNDLE = "updated_proto_schema_bundle.pb"; @Test - public void testToProto() throws IOException { + public void testToProto() throws IOException, URISyntaxException { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + URL protoSchema = cl.getResource(TEST_PROTO_SCHEMA_BUNDLE); + CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); + .setProtoSchema(Paths.get(protoSchema.toURI()).toAbsolutePath().toString()); com.google.bigtable.admin.v2.CreateSchemaBundleRequest requestProto = com.google.bigtable.admin.v2.CreateSchemaBundleRequest.newBuilder() From 65ebcbb6e8cbfdb345058f6f381b8ddf05dfe53f Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 19:21:07 +0000 Subject: [PATCH 13/27] fix test --- .../models/CreateSchemaBundleRequestTest.java | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java index d4664fd1fd88..1dbf88b1df53 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Files; import java.nio.file.Paths; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,12 +42,11 @@ public class CreateSchemaBundleRequestTest { @Test public void testToProto() throws IOException, URISyntaxException { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - URL protoSchema = cl.getResource(TEST_PROTO_SCHEMA_BUNDLE); - CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(Paths.get(protoSchema.toURI()).toAbsolutePath().toString()); + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); + + byte[] content = Files.readAllBytes(Paths.get(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); com.google.bigtable.admin.v2.CreateSchemaBundleRequest requestProto = com.google.bigtable.admin.v2.CreateSchemaBundleRequest.newBuilder() @@ -56,7 +56,7 @@ public void testToProto() throws IOException, URISyntaxException { com.google.bigtable.admin.v2.SchemaBundle.newBuilder() .setProtoSchema( com.google.bigtable.admin.v2.ProtoSchema.newBuilder() - .setProtoDescriptors(ByteString.copyFromUtf8("schema")) + .setProtoDescriptors(ByteString.copyFrom(content)) .build()) .build()) .build(); @@ -64,38 +64,44 @@ public void testToProto() throws IOException, URISyntaxException { } @Test - public void testEquality() throws IOException { + public void testEquality() throws IOException, URISyntaxException { CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); assertThat(request) .isEqualTo( CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE)); + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); assertThat(request) .isNotEqualTo( CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); + .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE))); } @Test - public void testHashCode() throws IOException { + public void testHashCode() throws IOException, URISyntaxException { CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); assertThat(request.hashCode()) .isEqualTo( CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE) + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)) .hashCode()); assertThat(request.hashCode()) .isNotEqualTo( CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE) + .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)) .hashCode()); } + + private String getResourceFilePath(String filePath) throws URISyntaxException { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + URL protoSchema = cl.getResource(filePath); + return Paths.get(protoSchema.toURI()).toAbsolutePath().toString(); + } } From ed1cb53cd8769e3cf331a43c9f04c0da72cf93df Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 19:23:53 +0000 Subject: [PATCH 14/27] fix test --- .../models/UpdateSchemaBundleRequestTest.java | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java index 0a5c427e775e..1d71fe292693 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java @@ -22,6 +22,10 @@ import com.google.protobuf.ByteString; import com.google.protobuf.FieldMask; import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -38,11 +42,12 @@ public class UpdateSchemaBundleRequestTest { private static final String TEST_UPDATED_PROTO_SCHEMA_BUNDLE = "updated_proto_schema_bundle.pb"; @Test - public void testToProto() throws IOException { + public void testToProto() throws IOException, URISyntaxException { UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE) + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)) .setIgnoreWarnings(true); + byte[] content = Files.readAllBytes(Paths.get(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); com.google.bigtable.admin.v2.UpdateSchemaBundleRequest requestProto = com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.newBuilder() @@ -53,7 +58,7 @@ public void testToProto() throws IOException { PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) .setProtoSchema( com.google.bigtable.admin.v2.ProtoSchema.newBuilder() - .setProtoDescriptors(ByteString.copyFromUtf8("schema")) + .setProtoDescriptors(ByteString.copyFrom(content)) .build()) .build()) .setUpdateMask(FieldMask.newBuilder().addPaths("proto_schema")) @@ -63,7 +68,9 @@ public void testToProto() throws IOException { } @Test - public void testUpdateProtoSchema() throws IOException { + public void testUpdateProtoSchema() throws IOException, URISyntaxException { + byte[] content = Files.readAllBytes(Paths.get(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); + com.google.bigtable.admin.v2.SchemaBundle existingSchemaBundle = com.google.bigtable.admin.v2.SchemaBundle.newBuilder() .setName( @@ -71,13 +78,13 @@ public void testUpdateProtoSchema() throws IOException { PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) .setProtoSchema( com.google.bigtable.admin.v2.ProtoSchema.newBuilder() - .setProtoDescriptors(ByteString.copyFromUtf8("schema")) + .setProtoDescriptors(ByteString.copyFrom(content)) .build()) .build(); UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(SchemaBundle.fromProto(existingSchemaBundle)) - .setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE); + .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); com.google.bigtable.admin.v2.UpdateSchemaBundleRequest requestProto = com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.newBuilder() @@ -88,38 +95,44 @@ public void testUpdateProtoSchema() throws IOException { } @Test - public void testEquality() throws IOException { + public void testEquality() throws IOException, URISyntaxException { UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); assertThat(request) .isEqualTo( UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE)); + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); assertThat(request) .isNotEqualTo( UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); + .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE))); } @Test - public void testHashCode() throws IOException { + public void testHashCode() throws IOException, URISyntaxException { UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); assertThat(request.hashCode()) .isEqualTo( UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE) + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)) .hashCode()); assertThat(request.hashCode()) .isNotEqualTo( UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE) + .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)) .hashCode()); } + + private String getResourceFilePath(String filePath) throws URISyntaxException { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + URL protoSchema = cl.getResource(filePath); + return Paths.get(protoSchema.toURI()).toAbsolutePath().toString(); + } } From 3c551e1698487188f8c8fa769052e193c22d6cf8 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 19:27:51 +0000 Subject: [PATCH 15/27] fix more tests --- .../v2/BigtableTableAdminClientTests.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java index dabba632e8fa..336365137b29 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java @@ -99,6 +99,10 @@ import io.grpc.Status; import io.grpc.Status.Code; import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; @@ -1339,6 +1343,7 @@ public void testCreateSchemaBundle() throws IOException { // Setup Mockito.when(mockStub.createSchemaBundleOperationCallable()) .thenReturn(mockCreateSchemaBundleOperationCallable); + byte[] content = Files.readAllBytes(Paths.get(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); com.google.bigtable.admin.v2.CreateSchemaBundleRequest expectedRequest = com.google.bigtable.admin.v2.CreateSchemaBundleRequest.newBuilder() @@ -1348,7 +1353,7 @@ public void testCreateSchemaBundle() throws IOException { com.google.bigtable.admin.v2.SchemaBundle.newBuilder() .setProtoSchema( com.google.bigtable.admin.v2.ProtoSchema.newBuilder() - .setProtoDescriptors(ByteString.copyFromUtf8("schema")))) + .setProtoDescriptors(ByteString.copyFrom(content)))) .build(); com.google.bigtable.admin.v2.SchemaBundle expectedResponse = @@ -1358,7 +1363,7 @@ public void testCreateSchemaBundle() throws IOException { PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) .setProtoSchema( com.google.bigtable.admin.v2.ProtoSchema.newBuilder() - .setProtoDescriptors(ByteString.copyFromUtf8("schema"))) + .setProtoDescriptors(ByteString.copyFrom(content))) .build(); mockOperationResult( @@ -1371,7 +1376,7 @@ public void testCreateSchemaBundle() throws IOException { CreateSchemaBundleRequest req = CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_PROTO_SCHEMA_BUNDLE); + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); // Execute SchemaBundle actualResult = adminClient.createSchemaBundle(req); @@ -1385,6 +1390,8 @@ public void testUpdateSchemaBundle() throws IOException { // Setup Mockito.when(mockStub.updateSchemaBundleOperationCallable()) .thenReturn(mockUpdateSchemaBundleOperationCallable); + byte[] content = + Files.readAllBytes(Paths.get(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE))); com.google.bigtable.admin.v2.UpdateSchemaBundleRequest expectedRequest = com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.newBuilder() @@ -1395,7 +1402,7 @@ public void testUpdateSchemaBundle() throws IOException { PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) .setProtoSchema( com.google.bigtable.admin.v2.ProtoSchema.newBuilder() - .setProtoDescriptors(ByteString.copyFromUtf8("schema"))) + .setProtoDescriptors(ByteString.copyFrom(content))) .build()) .setUpdateMask(FieldMask.newBuilder().addPaths("proto_schema")) .build(); @@ -1407,7 +1414,7 @@ public void testUpdateSchemaBundle() throws IOException { PROJECT_ID, INSTANCE_ID, TABLE_ID, SCHEMA_BUNDLE_ID)) .setProtoSchema( com.google.bigtable.admin.v2.ProtoSchema.newBuilder() - .setProtoDescriptors(ByteString.copyFromUtf8("schema"))) + .setProtoDescriptors(ByteString.copyFrom(content))) .build(); mockOperationResult( @@ -1420,7 +1427,7 @@ public void testUpdateSchemaBundle() throws IOException { UpdateSchemaBundleRequest req = UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(TEST_UPDATED_PROTO_SCHEMA_BUNDLE); + .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); // Execute SchemaBundle actualResult = adminClient.updateSchemaBundle(req); @@ -1667,4 +1674,10 @@ private void mockOperationResult( OperationFutures.immediateOperationFuture(operationSnapshot); Mockito.when(callable.futureCall(request)).thenReturn(operationFuture); } + + private String getResourceFilePath(String filePath) throws URISyntaxException { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + URL protoSchema = cl.getResource(filePath); + return Paths.get(protoSchema.toURI()).toAbsolutePath().toString(); + } } From e0384024ac2fd1cf5690ab636ad0c14d06da965d Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 19:31:18 +0000 Subject: [PATCH 16/27] fix more tests --- .../admin/v2/models/UpdateSchemaBundleRequestTest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java index 1d71fe292693..62bf94084566 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import com.google.bigtable.admin.v2.ProtoSchema; import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.protobuf.ByteString; import com.google.protobuf.FieldMask; @@ -88,7 +89,10 @@ public void testUpdateProtoSchema() throws IOException, URISyntaxException { com.google.bigtable.admin.v2.UpdateSchemaBundleRequest requestProto = com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.newBuilder() - .setSchemaBundle(existingSchemaBundle.toBuilder()) + .setSchemaBundle( + existingSchemaBundle.toBuilder() + .setProtoSchema( + ProtoSchema.newBuilder().setProtoDescriptors(ByteString.copyFrom(content)))) .setUpdateMask(FieldMask.newBuilder().addPaths("proto_schema")) .build(); assertThat(request.toProto(PROJECT_ID, INSTANCE_ID)).isEqualTo(requestProto); From e97801bea709eedfdb3d90396103ac38f4095dc8 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 19:32:10 +0000 Subject: [PATCH 17/27] fix more tests --- .../bigtable/admin/v2/BigtableTableAdminClientTests.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java index 336365137b29..f1b832e9b4f6 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java @@ -1339,7 +1339,7 @@ public void testDeleteAuthorizedView() { } @Test - public void testCreateSchemaBundle() throws IOException { + public void testCreateSchemaBundle() throws IOException, URISyntaxException { // Setup Mockito.when(mockStub.createSchemaBundleOperationCallable()) .thenReturn(mockCreateSchemaBundleOperationCallable); @@ -1386,7 +1386,7 @@ public void testCreateSchemaBundle() throws IOException { } @Test - public void testUpdateSchemaBundle() throws IOException { + public void testUpdateSchemaBundle() throws IOException, URISyntaxException { // Setup Mockito.when(mockStub.updateSchemaBundleOperationCallable()) .thenReturn(mockUpdateSchemaBundleOperationCallable); From 07f06886a8c141caa6a42d5cd00e9920e3fc55e0 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 19:37:08 +0000 Subject: [PATCH 18/27] fix more tests --- .../admin/v2/models/UpdateSchemaBundleRequestTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java index 62bf94084566..350d3cb612ac 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java @@ -71,6 +71,8 @@ public void testToProto() throws IOException, URISyntaxException { @Test public void testUpdateProtoSchema() throws IOException, URISyntaxException { byte[] content = Files.readAllBytes(Paths.get(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); + byte[] updated_content = + Files.readAllBytes(Paths.get(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE))); com.google.bigtable.admin.v2.SchemaBundle existingSchemaBundle = com.google.bigtable.admin.v2.SchemaBundle.newBuilder() @@ -92,7 +94,8 @@ public void testUpdateProtoSchema() throws IOException, URISyntaxException { .setSchemaBundle( existingSchemaBundle.toBuilder() .setProtoSchema( - ProtoSchema.newBuilder().setProtoDescriptors(ByteString.copyFrom(content)))) + ProtoSchema.newBuilder() + .setProtoDescriptors(ByteString.copyFrom(updated_content)))) .setUpdateMask(FieldMask.newBuilder().addPaths("proto_schema")) .build(); assertThat(request.toProto(PROJECT_ID, INSTANCE_ID)).isEqualTo(requestProto); From 3b4e6b4d5d35944fe3f0a01902b00909e83d8f05 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Wed, 2 Jul 2025 19:47:26 +0000 Subject: [PATCH 19/27] fix more tests --- .../admin/v2/it/BigtableSchemaBundleIT.java | 220 ++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java new file mode 100644 index 000000000000..eb215c0d2d48 --- /dev/null +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java @@ -0,0 +1,220 @@ +/* + * Copyright 2024 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.common.truth.Truth.assertWithMessage; +import static com.google.common.truth.TruthJUnit.assume; +import static org.junit.Assert.fail; + +import com.google.api.gax.batching.Batcher; +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import com.google.cloud.bigtable.admin.v2.models.CreateSchemaBundleRequest; +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; +import com.google.cloud.bigtable.admin.v2.models.SchemaBundle; +import com.google.cloud.bigtable.admin.v2.models.Table; +import com.google.cloud.bigtable.admin.v2.models.UpdateSchemaBundleRequest; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.models.RowMutationEntry; +import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv; +import com.google.cloud.bigtable.test_helpers.env.PrefixGenerator; +import com.google.cloud.bigtable.test_helpers.env.TestEnvRule; +import com.google.protobuf.ByteString; +import io.grpc.StatusRuntimeException; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; +import java.util.Random; +import java.util.logging.Logger; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class BigtableSchemaBundleIT { + @ClassRule public static final TestEnvRule testEnvRule = new TestEnvRule(); + @Rule public final PrefixGenerator prefixGenerator = new PrefixGenerator(); + private static final Logger LOGGER = Logger.getLogger(BigtableSchemaBundleIT.class.getName()); + private static final int[] BACKOFF_DURATION = {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}; + // Location: `google-cloud-bigtable/src/test/resources/proto_schema_bundle.pb` + private static final String TEST_PROTO_SCHEMA_BUNDLE = "proto_schema_bundle.pb"; + // Location: + // `google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.pb` + private static final String TEST_UPDATED_PROTO_SCHEMA_BUNDLE = "updated_proto_schema_bundle.pb"; + + private static BigtableTableAdminClient tableAdmin; + private static BigtableDataClient dataClient; + private static Table testTable; + + @BeforeClass + public static void setUpClass() throws InterruptedException { + assume() + .withMessage("BigtableInstanceAdminClient is not supported on Emulator") + .that(testEnvRule.env()) + .isNotInstanceOf(EmulatorEnv.class); + + tableAdmin = testEnvRule.env().getTableAdminClient(); + dataClient = testEnvRule.env().getDataClient(); + + testTable = createAndPopulateTestTable(tableAdmin, dataClient); + } + + @AfterClass + public static void tearDownClass() { + if (testTable != null) { + try { + tableAdmin.deleteTable(testTable.getId()); + } catch (Exception e) { + // Ignore. + } + } + } + + @Test + public void createSchemaBundleAndGetSchemaBundleTest() throws IOException, URISyntaxException { + String SchemaBundleId = prefixGenerator.newPrefix(); + byte[] content = Files.readAllBytes(Paths.get(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); + + CreateSchemaBundleRequest request = + CreateSchemaBundleRequest.of(testTable.getId(), SchemaBundleId) + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); + try { + SchemaBundle response = tableAdmin.createSchemaBundle(request); + assertWithMessage("Got wrong schema bundle Id in createSchemaBundle") + .that(response.getId()) + .isEqualTo(SchemaBundleId); + assertWithMessage("Got wrong proto schema in createSchemaBundle") + .that(response.getProtoSchema()) + .isEqualTo(ByteString.copyFrom(content)); + + response = tableAdmin.getSchemaBundle(testTable.getId(), SchemaBundleId); + assertWithMessage("Got wrong schema bundle Id in getSchemaBundle") + .that(response.getId()) + .isEqualTo(SchemaBundleId); + assertWithMessage("Got wrong proto schema in getSchemaBundle") + .that(response.getProtoSchema()) + .isEqualTo(ByteString.copyFrom(content)); + } finally { + tableAdmin.deleteSchemaBundle(testTable.getId(), SchemaBundleId); + } + } + + @Test + public void listSchemaBundlesTest() throws IOException, URISyntaxException { + String SchemaBundleId1 = prefixGenerator.newPrefix(); + String SchemaBundleId2 = prefixGenerator.newPrefix(); + + try { + tableAdmin.createSchemaBundle(createSchemaBundleRequest(SchemaBundleId1)); + tableAdmin.createSchemaBundle(createSchemaBundleRequest(SchemaBundleId2)); + + List response = tableAdmin.listSchemaBundles(testTable.getId()); + // Concurrent tests running may cause flakiness. Use containsAtLeast instead of + // containsExactly. + assertWithMessage("Got wrong schema bundle Ids in listSchemaBundles") + .that(response) + .containsAtLeast( + tableAdmin.getSchemaBundle(testTable.getId(), SchemaBundleId1).getId(), + tableAdmin.getSchemaBundle(testTable.getId(), SchemaBundleId2).getId()); + } finally { + tableAdmin.deleteSchemaBundle(testTable.getId(), SchemaBundleId1); + tableAdmin.deleteSchemaBundle(testTable.getId(), SchemaBundleId2); + } + } + + @Test + public void updateSchemaBundleAndDeleteSchemaBundleTest() + throws InterruptedException, IOException, URISyntaxException { + String SchemaBundleId = prefixGenerator.newPrefix(); + + // Create a schema bundle. + CreateSchemaBundleRequest request = createSchemaBundleRequest(SchemaBundleId); + + SchemaBundle response = tableAdmin.createSchemaBundle(request); + + // Update the schema bundle. + byte[] content = + Files.readAllBytes(Paths.get(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE))); + UpdateSchemaBundleRequest updateRequest = + UpdateSchemaBundleRequest.of(testTable.getId(), SchemaBundleId) + .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); + response = tableAdmin.updateSchemaBundle(updateRequest); + assertWithMessage("Got wrong deletion protection in UpdateSchemaBundle") + .that(response.getProtoSchema()) + .isEqualTo(ByteString.copyFrom(content)); + + // Now we should be able to successfully delete the SchemaBundle. + tableAdmin.deleteSchemaBundle(testTable.getId(), SchemaBundleId); + try { + for (int i = 0; i < BACKOFF_DURATION.length; i++) { + tableAdmin.getSchemaBundle(testTable.getId(), SchemaBundleId); + + LOGGER.info( + "Wait for " + + BACKOFF_DURATION[i] + + " seconds for deleting schema bundle " + + SchemaBundleId); + Thread.sleep(BACKOFF_DURATION[i] * 1000); + } + fail("SchemaBundle was not deleted."); + } catch (NotFoundException e) { + assertWithMessage("Incorrect exception type") + .that(e.getCause()) + .isInstanceOf(StatusRuntimeException.class); + } + } + + private CreateSchemaBundleRequest createSchemaBundleRequest(String SchemaBundleId) + throws IOException, URISyntaxException { + return CreateSchemaBundleRequest.of(testTable.getId(), SchemaBundleId) + .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); + } + + private static Table createAndPopulateTestTable( + BigtableTableAdminClient tableAdmin, BigtableDataClient dataClient) + throws InterruptedException { + String tableId = PrefixGenerator.newPrefix("BigtableSchemaBundleIT#createAndPopulateTestTable"); + Table testTable = tableAdmin.createTable(CreateTableRequest.of(tableId).addFamily("cf1")); + + // Populate test data. + byte[] rowBytes = new byte[1024]; + Random random = new Random(); + random.nextBytes(rowBytes); + + try (Batcher batcher = dataClient.newBulkMutationBatcher(tableId)) { + for (int i = 0; i < 10; i++) { + batcher.add( + RowMutationEntry.create("test-row-" + i) + .setCell("cf1", ByteString.EMPTY, ByteString.copyFrom(rowBytes))); + } + } + return testTable; + } + + private String getResourceFilePath(String filePath) throws URISyntaxException { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + URL protoSchema = cl.getResource(filePath); + return Paths.get(protoSchema.toURI()).toAbsolutePath().toString(); + } +} From c8a53f965c23e72fd9b57b3cfa9f4976f5b0468c Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Mon, 7 Jul 2025 17:00:05 +0000 Subject: [PATCH 20/27] use separate tables for tests --- .../bigtable/admin/v2/it/BigtableSchemaBundleIT.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java index eb215c0d2d48..2d82cd52de6e 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java @@ -43,7 +43,6 @@ import java.util.List; import java.util.Random; import java.util.logging.Logger; -import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Rule; @@ -65,7 +64,7 @@ public class BigtableSchemaBundleIT { private static BigtableTableAdminClient tableAdmin; private static BigtableDataClient dataClient; - private static Table testTable; + private Table testTable; @BeforeClass public static void setUpClass() throws InterruptedException { @@ -76,12 +75,15 @@ public static void setUpClass() throws InterruptedException { tableAdmin = testEnvRule.env().getTableAdminClient(); dataClient = testEnvRule.env().getDataClient(); + } + @Before + public void setUp() throws InterruptedException { testTable = createAndPopulateTestTable(tableAdmin, dataClient); } - @AfterClass - public static void tearDownClass() { + @After + public void tearDown() { if (testTable != null) { try { tableAdmin.deleteTable(testTable.getId()); From 0370978166369e7faf40f9b079e8870315d20abb Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Mon, 7 Jul 2025 17:03:57 +0000 Subject: [PATCH 21/27] use separate tables for tests --- .../cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java index 2d82cd52de6e..c400582eb8e9 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java @@ -43,6 +43,8 @@ import java.util.List; import java.util.Random; import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Rule; From d71624753875115fbf7637a7ed94ef5eb82648e3 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Mon, 7 Jul 2025 17:52:49 +0000 Subject: [PATCH 22/27] use separate tables for tests --- .../admin/v2/it/BigtableSchemaBundleIT.java | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java index c400582eb8e9..28b8a6e5c01d 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java @@ -129,22 +129,17 @@ public void listSchemaBundlesTest() throws IOException, URISyntaxException { String SchemaBundleId1 = prefixGenerator.newPrefix(); String SchemaBundleId2 = prefixGenerator.newPrefix(); - try { - tableAdmin.createSchemaBundle(createSchemaBundleRequest(SchemaBundleId1)); - tableAdmin.createSchemaBundle(createSchemaBundleRequest(SchemaBundleId2)); - - List response = tableAdmin.listSchemaBundles(testTable.getId()); - // Concurrent tests running may cause flakiness. Use containsAtLeast instead of - // containsExactly. - assertWithMessage("Got wrong schema bundle Ids in listSchemaBundles") - .that(response) - .containsAtLeast( - tableAdmin.getSchemaBundle(testTable.getId(), SchemaBundleId1).getId(), - tableAdmin.getSchemaBundle(testTable.getId(), SchemaBundleId2).getId()); - } finally { - tableAdmin.deleteSchemaBundle(testTable.getId(), SchemaBundleId1); - tableAdmin.deleteSchemaBundle(testTable.getId(), SchemaBundleId2); - } + tableAdmin.createSchemaBundle(createSchemaBundleRequest(SchemaBundleId1)); + tableAdmin.createSchemaBundle(createSchemaBundleRequest(SchemaBundleId2)); + + List response = tableAdmin.listSchemaBundles(testTable.getId()); + // Concurrent tests running may cause flakiness. Use containsAtLeast instead of + // containsExactly. + assertWithMessage("Got wrong schema bundle Ids in listSchemaBundles") + .that(response) + .containsAtLeast( + tableAdmin.getSchemaBundle(testTable.getId(), SchemaBundleId1).getId(), + tableAdmin.getSchemaBundle(testTable.getId(), SchemaBundleId2).getId()); } @Test From 2c21c56e2f18d2c0a8bd94ecd1f31dba98c50e91 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Tue, 8 Jul 2025 19:42:34 +0000 Subject: [PATCH 23/27] update according to PR --- .../v2/models/CreateSchemaBundleRequest.java | 22 ++++++++++--------- .../admin/v2/models/SchemaBundle.java | 8 ++----- .../v2/models/UpdateSchemaBundleRequest.java | 18 ++++++++++----- .../admin/v2/it/BigtableSchemaBundleIT.java | 6 ++--- .../models/CreateSchemaBundleRequestTest.java | 14 ++++++------ .../models/UpdateSchemaBundleRequestTest.java | 16 +++++++------- 6 files changed, 45 insertions(+), 39 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java index 7ae70a70eb5b..ea966d81c4bc 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequest.java @@ -36,7 +36,7 @@ *

{@code
  * CreateSchemaBundleRequest request =
  *     CreateSchemaBundleRequest.of("my-table", "my-new-schema-bundle")
- *         .setProtoSchema("proto_file.pb");
+ *         .setProtoSchemaFile("proto_file.pb");
  * }
* * @see SchemaBundle for more details. @@ -59,19 +59,21 @@ private CreateSchemaBundleRequest(@Nonnull String tableId, @Nonnull String schem requestBuilder.setSchemaBundleId(schemaBundleId); } - /** - * Sets the proto schema for this schema bundle. - * - * @see SchemaBundleType for details. - */ - public CreateSchemaBundleRequest setProtoSchema(@Nonnull String protoSchemaFile) + /** Sets the proto schema for this schema bundle. */ + public CreateSchemaBundleRequest setProtoSchemaFile(@Nonnull String protoSchemaFile) throws IOException { - Preconditions.checkNotNull(protoSchemaFile, "protoSchema must be set"); + Preconditions.checkNotNull(protoSchemaFile, "protoSchemaFile must be set"); byte[] content = Files.readAllBytes(Paths.get(protoSchemaFile)); + return setProtoSchema(ByteString.copyFrom(content)); + } + + /** Sets the proto schema for this schema bundle. */ + public CreateSchemaBundleRequest setProtoSchema(@Nonnull ByteString protoSchema) + throws IOException { + Preconditions.checkNotNull(protoSchema, "protoSchema must be set"); requestBuilder.setSchemaBundle( com.google.bigtable.admin.v2.SchemaBundle.newBuilder() - .setProtoSchema( - ProtoSchema.newBuilder().setProtoDescriptors(ByteString.copyFrom(content)))); + .setProtoSchema(ProtoSchema.newBuilder().setProtoDescriptors(protoSchema))); return this; } diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java index 0fcc43989fc4..82d75cb9b0e0 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java @@ -27,6 +27,7 @@ */ public final class SchemaBundle { private final com.google.bigtable.admin.v2.SchemaBundle proto; + private final SchemaBundleName schemaBundleName; /** * Wraps the protobuf. This method is considered an internal implementation detail and not meant @@ -43,22 +44,17 @@ private SchemaBundle(@Nonnull com.google.bigtable.admin.v2.SchemaBundle proto) { Preconditions.checkArgument( proto.hasProtoSchema(), "Schemabundle must have a proto_schema field"); this.proto = proto; + this.schemaBundleName = SchemaBundleName.parse(proto.getName()); } /** Gets the schema bundle's id. */ public String getId() { - // Constructor ensures that name is not null. - SchemaBundleName fullName = SchemaBundleName.parse(proto.getName()); - //noinspection ConstantConditions return fullName.getSchemaBundle(); } /** Gets the id of the table that owns this schema bundle. */ public String getTableId() { - // Constructor ensures that name is not null. - SchemaBundleName fullName = SchemaBundleName.parse(proto.getName()); - //noinspection ConstantConditions return fullName.getTable(); } diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java index 8d09e563b72d..e40fd5ee9cd7 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java @@ -37,7 +37,7 @@ *
{@code
  * SchemaBundle existingSchemaBundle = client.getSchemaBundle("my-table", "my-schema-bundle");
  * UpdateSchemaBundleRequest request =
- *     UpdateSchemaBundleRequest.of(existingSchemaBundle).setProtoSchema("file.pb");
+ *     UpdateSchemaBundleRequest.of(existingSchemaBundle).setProtoSchemaFile("file.pb");
  * }
* * @see SchemaBundle for more details. @@ -78,13 +78,21 @@ private UpdateSchemaBundleRequest( this.requestBuilder = requestBuilder; } - /** Changes the deletion protection of an existing schema bundle. */ - public UpdateSchemaBundleRequest setProtoSchema(String protoSchemaFile) throws IOException { + /** Sets the proto schema for this schema bundle. */ + public CreateSchemaBundleRequest setProtoSchemaFile(@Nonnull String protoSchemaFile) + throws IOException { + Preconditions.checkNotNull(protoSchemaFile, "protoSchemaFile must be set"); byte[] content = Files.readAllBytes(Paths.get(protoSchemaFile)); + return setProtoSchema(ByteString.copyFrom(content)); + } + + /** Sets the proto schema for this schema bundle. */ + public CreateSchemaBundleRequest setProtoSchema(@Nonnull ByteString protoSchema) + throws IOException { + Preconditions.checkNotNull(protoSchema, "protoSchema must be set"); requestBuilder.setSchemaBundle( com.google.bigtable.admin.v2.SchemaBundle.newBuilder() - .setProtoSchema( - ProtoSchema.newBuilder().setProtoDescriptors(ByteString.copyFrom(content)))); + .setProtoSchema(ProtoSchema.newBuilder().setProtoDescriptors(protoSchema))); updateFieldMask(com.google.bigtable.admin.v2.SchemaBundle.PROTO_SCHEMA_FIELD_NUMBER); return this; } diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java index 28b8a6e5c01d..62001f8230d9 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableSchemaBundleIT.java @@ -102,7 +102,7 @@ public void createSchemaBundleAndGetSchemaBundleTest() throws IOException, URISy CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of(testTable.getId(), SchemaBundleId) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); try { SchemaBundle response = tableAdmin.createSchemaBundle(request); assertWithMessage("Got wrong schema bundle Id in createSchemaBundle") @@ -157,7 +157,7 @@ public void updateSchemaBundleAndDeleteSchemaBundleTest() Files.readAllBytes(Paths.get(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE))); UpdateSchemaBundleRequest updateRequest = UpdateSchemaBundleRequest.of(testTable.getId(), SchemaBundleId) - .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); + .setProtoSchemaFile(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); response = tableAdmin.updateSchemaBundle(updateRequest); assertWithMessage("Got wrong deletion protection in UpdateSchemaBundle") .that(response.getProtoSchema()) @@ -187,7 +187,7 @@ public void updateSchemaBundleAndDeleteSchemaBundleTest() private CreateSchemaBundleRequest createSchemaBundleRequest(String SchemaBundleId) throws IOException, URISyntaxException { return CreateSchemaBundleRequest.of(testTable.getId(), SchemaBundleId) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); } private static Table createAndPopulateTestTable( diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java index 1dbf88b1df53..2d37eccff5b0 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateSchemaBundleRequestTest.java @@ -44,7 +44,7 @@ public class CreateSchemaBundleRequestTest { public void testToProto() throws IOException, URISyntaxException { CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); byte[] content = Files.readAllBytes(Paths.get(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); @@ -67,35 +67,35 @@ public void testToProto() throws IOException, URISyntaxException { public void testEquality() throws IOException, URISyntaxException { CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); assertThat(request) .isEqualTo( CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); assertThat(request) .isNotEqualTo( CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE))); + .setProtoSchemaFile(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE))); } @Test public void testHashCode() throws IOException, URISyntaxException { CreateSchemaBundleRequest request = CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); assertThat(request.hashCode()) .isEqualTo( CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)) + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)) .hashCode()); assertThat(request.hashCode()) .isNotEqualTo( CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)) + .setProtoSchemaFile(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)) .hashCode()); } diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java index 350d3cb612ac..994d56068aad 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequestTest.java @@ -46,7 +46,7 @@ public class UpdateSchemaBundleRequestTest { public void testToProto() throws IOException, URISyntaxException { UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)) + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)) .setIgnoreWarnings(true); byte[] content = Files.readAllBytes(Paths.get(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); @@ -87,7 +87,7 @@ public void testUpdateProtoSchema() throws IOException, URISyntaxException { UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(SchemaBundle.fromProto(existingSchemaBundle)) - .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); + .setProtoSchemaFile(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); com.google.bigtable.admin.v2.UpdateSchemaBundleRequest requestProto = com.google.bigtable.admin.v2.UpdateSchemaBundleRequest.newBuilder() @@ -105,35 +105,35 @@ public void testUpdateProtoSchema() throws IOException, URISyntaxException { public void testEquality() throws IOException, URISyntaxException { UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); assertThat(request) .isEqualTo( UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE))); assertThat(request) .isNotEqualTo( UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE))); + .setProtoSchemaFile(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE))); } @Test public void testHashCode() throws IOException, URISyntaxException { UpdateSchemaBundleRequest request = UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); assertThat(request.hashCode()) .isEqualTo( UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)) + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)) .hashCode()); assertThat(request.hashCode()) .isNotEqualTo( UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)) + .setProtoSchemaFile(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)) .hashCode()); } From dff78e375ed8f659c205c12b6a5d350098af37ce Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Tue, 8 Jul 2025 19:47:34 +0000 Subject: [PATCH 24/27] update according to PR --- .../admin/v2/models/SchemaBundle.java | 95 ------------------- 1 file changed, 95 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java index 82d75cb9b0e0..e69de29bb2d1 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java @@ -1,95 +0,0 @@ -/* - * Copyright 2024 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.SchemaBundleName; -import com.google.common.base.Objects; -import com.google.common.base.Preconditions; -import javax.annotation.Nonnull; - -/** - * A class that wraps the {@link com.google.bigtable.admin.v2.SchemaBundle} protocol buffer object. - */ -public final class SchemaBundle { - private final com.google.bigtable.admin.v2.SchemaBundle proto; - private final SchemaBundleName schemaBundleName; - - /** - * Wraps the protobuf. This method is considered an internal implementation detail and not meant - * to be used by applications. - */ - @InternalApi - public static SchemaBundle fromProto(@Nonnull com.google.bigtable.admin.v2.SchemaBundle proto) { - return new SchemaBundle(proto); - } - - private SchemaBundle(@Nonnull com.google.bigtable.admin.v2.SchemaBundle proto) { - Preconditions.checkNotNull(proto); - Preconditions.checkArgument(!proto.getName().isEmpty(), "SchemaBundle must have a name"); - Preconditions.checkArgument( - proto.hasProtoSchema(), "Schemabundle must have a proto_schema field"); - this.proto = proto; - this.schemaBundleName = SchemaBundleName.parse(proto.getName()); - } - - /** Gets the schema bundle's id. */ - public String getId() { - //noinspection ConstantConditions - return fullName.getSchemaBundle(); - } - - /** Gets the id of the table that owns this schema bundle. */ - public String getTableId() { - //noinspection ConstantConditions - return fullName.getTable(); - } - - /** Gets the proto schema of this schema bundle. */ - public com.google.protobuf.ByteString getProtoSchema() { - if (proto.hasProtoSchema()) { - return proto.getProtoSchema().getProtoDescriptors(); - } - throw new IllegalStateException("This SchemaBundle doesn't have a valid type specified"); - } - - /** - * Creates the request protobuf. This method is considered an internal implementation detail and - * not meant to be used by applications. - */ - @InternalApi - public com.google.bigtable.admin.v2.SchemaBundle toProto() { - return proto; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - SchemaBundle that = (SchemaBundle) o; - return Objects.equal(proto, that.proto); - } - - @Override - public int hashCode() { - return Objects.hashCode(proto); - } -} From 4a7af0057fa31c2d4ae6c0d19c4e6e6ebedbd1ac Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Tue, 8 Jul 2025 19:51:11 +0000 Subject: [PATCH 25/27] update according to PR --- .../admin/v2/models/SchemaBundle.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java index e69de29bb2d1..7782c335a2ef 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/SchemaBundle.java @@ -0,0 +1,95 @@ +/* + * Copyright 2024 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.SchemaBundleName; +import com.google.common.base.Objects; +import com.google.common.base.Preconditions; +import javax.annotation.Nonnull; + +/** + * A class that wraps the {@link com.google.bigtable.admin.v2.SchemaBundle} protocol buffer object. + */ +public final class SchemaBundle { + private final com.google.bigtable.admin.v2.SchemaBundle proto; + private final SchemaBundleName schemaBundleName; + + /** + * Wraps the protobuf. This method is considered an internal implementation detail and not meant + * to be used by applications. + */ + @InternalApi + public static SchemaBundle fromProto(@Nonnull com.google.bigtable.admin.v2.SchemaBundle proto) { + return new SchemaBundle(proto); + } + + private SchemaBundle(@Nonnull com.google.bigtable.admin.v2.SchemaBundle proto) { + Preconditions.checkNotNull(proto); + Preconditions.checkArgument(!proto.getName().isEmpty(), "SchemaBundle must have a name"); + Preconditions.checkArgument( + proto.hasProtoSchema(), "Schemabundle must have a proto_schema field"); + this.proto = proto; + this.schemaBundleName = SchemaBundleName.parse(proto.getName()); + } + + /** Gets the schema bundle's id. */ + public String getId() { + //noinspection ConstantConditions + return schemaBundleName.getSchemaBundle(); + } + + /** Gets the id of the table that owns this schema bundle. */ + public String getTableId() { + //noinspection ConstantConditions + return schemaBundleName.getTable(); + } + + /** Gets the proto schema of this schema bundle. */ + public com.google.protobuf.ByteString getProtoSchema() { + if (proto.hasProtoSchema()) { + return proto.getProtoSchema().getProtoDescriptors(); + } + throw new IllegalStateException("This SchemaBundle doesn't have a valid type specified"); + } + + /** + * Creates the request protobuf. This method is considered an internal implementation detail and + * not meant to be used by applications. + */ + @InternalApi + public com.google.bigtable.admin.v2.SchemaBundle toProto() { + return proto; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + SchemaBundle that = (SchemaBundle) o; + return Objects.equal(proto, that.proto); + } + + @Override + public int hashCode() { + return Objects.hashCode(proto); + } +} From 8994d78cb9b4c71e52b218a4182486a379148ba1 Mon Sep 17 00:00:00 2001 From: Ron Gal <125445217+ron-gal@users.noreply.github.com> Date: Tue, 8 Jul 2025 15:57:23 -0400 Subject: [PATCH 26/27] Update UpdateSchemaBundleRequest.java --- .../bigtable/admin/v2/models/UpdateSchemaBundleRequest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java index e40fd5ee9cd7..904c4d0097f4 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateSchemaBundleRequest.java @@ -79,7 +79,7 @@ private UpdateSchemaBundleRequest( } /** Sets the proto schema for this schema bundle. */ - public CreateSchemaBundleRequest setProtoSchemaFile(@Nonnull String protoSchemaFile) + public UpdateSchemaBundleRequest setProtoSchemaFile(@Nonnull String protoSchemaFile) throws IOException { Preconditions.checkNotNull(protoSchemaFile, "protoSchemaFile must be set"); byte[] content = Files.readAllBytes(Paths.get(protoSchemaFile)); @@ -87,7 +87,7 @@ public CreateSchemaBundleRequest setProtoSchemaFile(@Nonnull String protoSchemaF } /** Sets the proto schema for this schema bundle. */ - public CreateSchemaBundleRequest setProtoSchema(@Nonnull ByteString protoSchema) + public UpdateSchemaBundleRequest setProtoSchema(@Nonnull ByteString protoSchema) throws IOException { Preconditions.checkNotNull(protoSchema, "protoSchema must be set"); requestBuilder.setSchemaBundle( From caa24a2cc8e969d99bfe695a0be0abb33eda2818 Mon Sep 17 00:00:00 2001 From: Ron Gal Date: Tue, 8 Jul 2025 20:01:28 +0000 Subject: [PATCH 27/27] fix build --- .../bigtable/admin/v2/BigtableTableAdminClientTests.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java index f1b832e9b4f6..0bf373116185 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java @@ -1376,7 +1376,7 @@ public void testCreateSchemaBundle() throws IOException, URISyntaxException { CreateSchemaBundleRequest req = CreateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); + .setProtoSchemaFile(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)); // Execute SchemaBundle actualResult = adminClient.createSchemaBundle(req); @@ -1427,7 +1427,7 @@ public void testUpdateSchemaBundle() throws IOException, URISyntaxException { UpdateSchemaBundleRequest req = UpdateSchemaBundleRequest.of(TABLE_ID, SCHEMA_BUNDLE_ID) - .setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); + .setProtoSchemaFile(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)); // Execute SchemaBundle actualResult = adminClient.updateSchemaBundle(req);