Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()) {
Expand All @@ -99,6 +107,14 @@ 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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.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.Paths;
import javax.annotation.Nonnull;

/**
* Parameters for creating a new Cloud Bigtable {@link SchemaBundle}, which represents subsets of a
* particular table.
*
* <p>Sample code:
*
* <pre>{@code
* CreateSchemaBundleRequest request =
* CreateSchemaBundleRequest.of("my-table", "my-new-schema-bundle")
* .setProtoSchemaFile("proto_file.pb");
* }</pre>
*
* @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. */
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(protoSchema)));
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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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 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.Paths;
import javax.annotation.Nonnull;

/**
* Parameters for updating an existing Cloud Bigtable {@link SchemaBundle}.
*
* <p>Sample code:
*
* <pre>{@code
* SchemaBundle existingSchemaBundle = client.getSchemaBundle("my-table", "my-schema-bundle");
* UpdateSchemaBundleRequest request =
* UpdateSchemaBundleRequest.of(existingSchemaBundle).setProtoSchemaFile("file.pb");
* }</pre>
*
* @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;
}

/** Sets the proto schema for this schema bundle. */
public UpdateSchemaBundleRequest 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 UpdateSchemaBundleRequest 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(protoSchema)));
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();
}
}
Loading
Loading