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 1 commit
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 @@ -114,6 +114,20 @@ public MutateRowsRequest toProto(RequestContext requestContext) {
.build();
}

/**
* Wraps the protobuf {@link MutateRowsRequest}.
*
* <p>WARNING: Please note that the project id & instance id in the table name will be overwritten
rahulKQL marked this conversation as resolved.
Show resolved Hide resolved
* by the configuration in the BigtableDataClient.
*/
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 @@ -136,4 +136,19 @@ public CheckAndMutateRowRequest toProto(RequestContext requestContext) {
.setAppProfileId(requestContext.getAppProfileId())
.build();
}

/**
* Wraps the protobuf {@link CheckAndMutateRowRequest}.
*
* <p>WARNING: Please note that the project id & instance id in the table name will be overwritten
* by the configuration in the BigtableDataClient.
*/
rahulKQL marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -137,4 +137,19 @@ public ReadModifyWriteRowRequest toProto(RequestContext requestContext) {
.setAppProfileId(requestContext.getAppProfileId())
.build();
}

/**
* Wraps the protobuf {@link ReadModifyWriteRowRequest}.
*
* <p>WARNING: Please note that the project id & instance id in the table name will be overwritten
* by the configuration in the BigtableDataClient.
*/
rahulKQL marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -214,4 +214,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 project id & instance id in the table name will be overwritten
* by the configuration in the BigtableDataClient.
rahulKQL marked this conversation as resolved.
Show resolved Hide resolved
*/
public static RowMutation fromProtobuf(@Nonnull MutateRowRequest request) {
String tableId = NameUtil.extractTableIdFromTableName(request.getTableName());

// TODO: Should we give an option for Mutation.fromProto(List<com.google.bigtable.v2.Mutation>)?
return RowMutation.create(
tableId, request.getRowKey(), Mutation.fromProtoUnsafe(request.getMutationsList()));
}
}
Expand Up @@ -16,6 +16,7 @@
package com.google.cloud.bigtable.data.v2.models;

import com.google.api.core.InternalApi;
import com.google.bigtable.v2.MutateRowRequest;
import com.google.bigtable.v2.MutateRowsRequest;
import com.google.common.base.Preconditions;
import com.google.protobuf.ByteString;
Expand Down Expand Up @@ -177,4 +178,11 @@ public MutateRowsRequest.Entry toProto() {
.addAllMutations(mutation.getMutations())
.build();
}

/** Wraps the protobuf {@link MutateRowRequest}. */
public static RowMutationEntry fromProtobuf(MutateRowsRequest.Entry request) {
// TODO: Should we have an option for Mutation.fromProto(List<com.google.bigtable.v2.Mutation>)?
return new RowMutationEntry(
request.getRowKey(), Mutation.fromProtoUnsafe(request.getMutationsList()));
}
igorbernstein2 marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -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 @@ -165,4 +165,17 @@ public void testWithLongValue() {
.setValue(ByteString.copyFrom(Longs.toByteArray(100_000L)))
.build());
}

@Test
public void fromProtobufTest() {
RowMutationEntry expected =
RowMutationEntry.create("row-key")
.setCell("fake-family", "fake-qualifier", 10_000L, "fake-value")
.deleteFamily("fake-family");

MutateRowsRequest.Entry protoRequest = expected.toProto();
RowMutationEntry actualRequest = RowMutationEntry.fromProtobuf(protoRequest);

assertThat(actualRequest.toProto()).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);
}
}