Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding utility to transform protobuf into model object #299

Merged
merged 5 commits into from May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.cloud.bigtable.data.v2.models;

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.bigtable.v2.MutateRowsRequest;
import com.google.cloud.bigtable.data.v2.internal.NameUtil;
Expand Down Expand Up @@ -114,6 +115,21 @@ public MutateRowsRequest toProto(RequestContext requestContext) {
.build();
}

/**
* Wraps the protobuf {@link MutateRowsRequest}.
*
* <p>WARNING: Please note that the table name will be overwritten by the configuration in the
* BigtableDataClient. The mutation must always be idempotent because it would be retried.
rahulKQL marked this conversation as resolved.
Show resolved Hide resolved
*/
@BetaApi
public static BulkMutation fromProtobuf(@Nonnull MutateRowsRequest request) {
BulkMutation bulkMutation =
BulkMutation.create(NameUtil.extractTableIdFromTableName(request.getTableName()));
bulkMutation.builder = request.toBuilder();

return bulkMutation;
}

/** Creates a copy of {@link BulkMutation}. */
@Override
public BulkMutation clone() {
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.cloud.bigtable.data.v2.models;

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.bigtable.v2.CheckAndMutateRowRequest;
import com.google.cloud.bigtable.data.v2.internal.NameUtil;
Expand Down Expand Up @@ -136,4 +137,20 @@ public CheckAndMutateRowRequest toProto(RequestContext requestContext) {
.setAppProfileId(requestContext.getAppProfileId())
.build();
}

/**
* Wraps the protobuf {@link CheckAndMutateRowRequest}.
*
* <p>WARNING: Please note that the table name will be overwritten by the configuration in the
* BigtableDataClient. The mutation must always be idempotent because it would be retried.
*/
rahulKQL marked this conversation as resolved.
Show resolved Hide resolved
@BetaApi
public static ConditionalRowMutation fromProtobuf(@Nonnull CheckAndMutateRowRequest request) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and everywhere else, please stay consistent with existing fromProto methods

String tableId = NameUtil.extractTableIdFromTableName(request.getTableName());
ConditionalRowMutation rowMutation =
ConditionalRowMutation.create(tableId, request.getRowKey());
rowMutation.builder = request.toBuilder();

return rowMutation;
}
}
Expand Up @@ -87,6 +87,17 @@ public static Mutation fromProtoUnsafe(List<com.google.bigtable.v2.Mutation> pro
return mutation;
}

/**
* Wraps the List of protobuf {@link com.google.bigtable.v2.Mutation}. This method creates a
* mutation instance that is idempotent in nature, which could safely be retried.
igorbernstein2 marked this conversation as resolved.
Show resolved Hide resolved
*/
@BetaApi
public static Mutation fromProtobuf(List<com.google.bigtable.v2.Mutation> protos) {
Mutation mutation = new Mutation(false);
mutation.mutations.addAll(protos);
return mutation;
}

private Mutation(boolean allowServersideTimestamp) {
this.allowServersideTimestamp = allowServersideTimestamp;
}
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.cloud.bigtable.data.v2.models;

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.bigtable.v2.ReadModifyWriteRowRequest;
import com.google.bigtable.v2.ReadModifyWriteRule;
Expand Down Expand Up @@ -137,4 +138,20 @@ public ReadModifyWriteRowRequest toProto(RequestContext requestContext) {
.setAppProfileId(requestContext.getAppProfileId())
.build();
}

/**
* Wraps the protobuf {@link ReadModifyWriteRowRequest}.
*
* <p>WARNING: Please note that the table name will be overwritten by the configuration in the
* BigtableDataClient.
*/
rahulKQL marked this conversation as resolved.
Show resolved Hide resolved
@BetaApi
public static ReadModifyWriteRow fromProtobuf(ReadModifyWriteRowRequest request) {
String tableId = NameUtil.extractTableIdFromTableName(request.getTableName());

ReadModifyWriteRow row = ReadModifyWriteRow.create(tableId, request.getRowKey());
row.builder = request.toBuilder();

return row;
}
}
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.cloud.bigtable.data.v2.models;

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.bigtable.v2.MutateRowRequest;
import com.google.bigtable.v2.MutateRowsRequest;
Expand Down Expand Up @@ -214,4 +215,18 @@ public MutateRowsRequest toBulkProto(RequestContext requestContext) {
Entry.newBuilder().setRowKey(key).addAllMutations(mutation.getMutations()).build())
.build();
}

/**
* Wraps the protobuf {@link MutateRowRequest}.
*
* <p>WARNING: Please note that the table name will be overwritten by the configuration in the
* BigtableDataClient. The mutation must always be idempotent because it would be retried.
rahulKQL marked this conversation as resolved.
Show resolved Hide resolved
*/
@BetaApi
public static RowMutation fromProtobuf(@Nonnull MutateRowRequest request) {
String tableId = NameUtil.extractTableIdFromTableName(request.getTableName());

return RowMutation.create(
tableId, request.getRowKey(), Mutation.fromProtobuf(request.getMutationsList()));
}
}
Expand Up @@ -147,4 +147,18 @@ public void addRowMutationEntry() {
bulkMutation.add(entry);
assertThat(bulkMutation.toProto(REQUEST_CONTEXT).getEntriesList()).contains(entry.toProto());
}

@Test
public void fromProtobufTest() {
BulkMutation expected =
BulkMutation.create(TABLE_ID)
.add(
"key",
Mutation.create().setCell("fake-family", "fake-qualifier", 10_000L, "fake-value"));

MutateRowsRequest protoRequest = expected.toProto(REQUEST_CONTEXT);
BulkMutation actualBulkMutation = BulkMutation.fromProtobuf(protoRequest);

assertThat(actualBulkMutation.toProto(REQUEST_CONTEXT)).isEqualTo(protoRequest);
}
}
Expand Up @@ -162,4 +162,18 @@ public void serializationTest() throws IOException, ClassNotFoundException {
ConditionalRowMutation actual = (ConditionalRowMutation) ois.readObject();
assertThat(actual.toProto(REQUEST_CONTEXT)).isEqualTo(expected.toProto(REQUEST_CONTEXT));
}

@Test
public void fromProtobufTest() {
ConditionalRowMutation mutation =
ConditionalRowMutation.create(TABLE_ID, TEST_KEY)
.condition(Filters.FILTERS.key().regex("test"))
.then(Mutation.create().setCell("family1", "qualifier1", 10_000L, "value"))
.otherwise(Mutation.create().deleteFamily("family"));

CheckAndMutateRowRequest protoRequest = mutation.toProto(REQUEST_CONTEXT);
ConditionalRowMutation actualRequest = ConditionalRowMutation.fromProtobuf(protoRequest);

assertThat(actualRequest.toProto(REQUEST_CONTEXT)).isEqualTo(protoRequest);
}
}
Expand Up @@ -254,4 +254,20 @@ public void testWithLongValue() {
.setValue(ByteString.copyFrom(Longs.toByteArray(20_000L)))
.build());
}

@Test
public void fromProtobufTest() {
mutation
.setCell(
"fake-family",
ByteString.copyFromUtf8("fake-qualifier"),
1_000,
ByteString.copyFromUtf8("fake-value"))
.deleteCells("fake-family", ByteString.copyFromUtf8("fake-qualifier"))
.deleteFamily("fake-family2");

List<com.google.bigtable.v2.Mutation> protoMutation = mutation.getMutations();

assertThat(Mutation.fromProtobuf(protoMutation).getMutations()).isEqualTo(protoMutation);
}
}
Expand Up @@ -113,4 +113,17 @@ public void serializationTest() throws IOException, ClassNotFoundException {
ReadModifyWriteRow actual = (ReadModifyWriteRow) ois.readObject();
assertThat(actual.toProto(REQUEST_CONTEXT)).isEqualTo(expected.toProto(REQUEST_CONTEXT));
}

@Test
public void fromProtobufTest() {
ReadModifyWriteRow expected =
ReadModifyWriteRow.create(TABLE_ID, "row-key")
.increment("fake-family", ByteString.copyFromUtf8("fake-qualifier"), 1)
.append("fake-family", "fake-qualifier", "fake-value");

ReadModifyWriteRowRequest protoRequest = expected.toProto(REQUEST_CONTEXT);
ReadModifyWriteRow actualRequest = ReadModifyWriteRow.fromProtobuf(protoRequest);

assertThat(actualRequest.toProto(REQUEST_CONTEXT)).isEqualTo(protoRequest);
}
}
Expand Up @@ -139,4 +139,17 @@ public void testWithLongValue() {
.setValue(ByteString.copyFrom(Longs.toByteArray(100_000L)))
.build());
}

@Test
public void fromProtobufTest() {
RowMutation rowMutation =
RowMutation.create("fake-table", "fake-key")
.setCell("fake-family", "fake-qualifier-1", "fake-value")
.setCell("fake-family", "fake-qualifier-2", 30_000L, "fake-value-2");

MutateRowRequest protoRequest = rowMutation.toProto(REQUEST_CONTEXT);
RowMutation actualRequest = RowMutation.fromProtobuf(protoRequest);

assertThat(actualRequest.toProto(REQUEST_CONTEXT)).isEqualTo(protoRequest);
}
}