diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/DefaultRowAdapter.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/DefaultRowAdapter.java index efe56dc4188f..18943265ce60 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/DefaultRowAdapter.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/DefaultRowAdapter.java @@ -15,6 +15,10 @@ */ package com.google.cloud.bigtable.data.v2.models; +import com.google.api.core.InternalApi; +import com.google.bigtable.v2.Cell; +import com.google.bigtable.v2.Column; +import com.google.bigtable.v2.Family; import com.google.common.collect.ImmutableList; import com.google.protobuf.ByteString; import java.util.List; @@ -43,6 +47,34 @@ public ByteString getKey(Row row) { return row.getKey(); } + /** + * Helper to convert a proto Row to a model Row. + * + *

For internal use only. + */ + @InternalApi + public Row createRowFromProto(com.google.bigtable.v2.Row row) { + RowBuilder builder = createRowBuilder(); + builder.startRow(row.getKey()); + + for (Family family : row.getFamiliesList()) { + for (Column column : family.getColumnsList()) { + for (Cell cell : column.getCellsList()) { + builder.startCell( + family.getName(), + column.getQualifier(), + cell.getTimestampMicros(), + cell.getLabelsList(), + cell.getValue().size()); + builder.cellValue(cell.getValue()); + builder.finishCell(); + } + } + } + + return builder.finishRow(); + } + /** {@inheritDoc} */ public class DefaultRowBuilder implements RowBuilder { private ByteString currentKey; diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/ReadModifyWriteRowCallable.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/ReadModifyWriteRowCallable.java index f456f0202bfa..09e133678e6a 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/ReadModifyWriteRowCallable.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/ReadModifyWriteRowCallable.java @@ -20,24 +20,19 @@ import com.google.api.core.ApiFutures; import com.google.api.gax.rpc.ApiCallContext; import com.google.api.gax.rpc.UnaryCallable; -import com.google.bigtable.v2.Cell; -import com.google.bigtable.v2.Column; -import com.google.bigtable.v2.Family; import com.google.bigtable.v2.ReadModifyWriteRowRequest; import com.google.bigtable.v2.ReadModifyWriteRowResponse; import com.google.cloud.bigtable.data.v2.internal.RequestContext; import com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter; import com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow; import com.google.cloud.bigtable.data.v2.models.Row; -import com.google.cloud.bigtable.data.v2.models.RowAdapter; -import com.google.cloud.bigtable.data.v2.models.RowAdapter.RowBuilder; import com.google.common.util.concurrent.MoreExecutors; /** Simple wrapper for ReadModifyWriteRow to wrap the request and response protobufs. */ class ReadModifyWriteRowCallable extends UnaryCallable { private final UnaryCallable inner; private final RequestContext requestContext; - private final RowAdapter rowAdapter; + private final DefaultRowAdapter rowAdapter; ReadModifyWriteRowCallable( UnaryCallable inner, @@ -64,25 +59,6 @@ public Row apply(ReadModifyWriteRowResponse readModifyWriteRowResponse) { } private Row convertResponse(ReadModifyWriteRowResponse response) { - RowBuilder rowBuilder = rowAdapter.createRowBuilder(); - rowBuilder.startRow(response.getRow().getKey()); - - for (Family family : response.getRow().getFamiliesList()) { - for (Column column : family.getColumnsList()) { - for (Cell cell : column.getCellsList()) { - rowBuilder.startCell( - family.getName(), - column.getQualifier(), - cell.getTimestampMicros(), - cell.getLabelsList(), - cell.getValue().size()); - - rowBuilder.cellValue(cell.getValue()); - - rowBuilder.finishCell(); - } - } - } - return rowBuilder.finishRow(); + return rowAdapter.createRowFromProto(response.getRow()); } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/DefaultRowAdapterTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/DefaultRowAdapterTest.java index 5adbfc40c158..21afab9edffc 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/DefaultRowAdapterTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/DefaultRowAdapterTest.java @@ -171,4 +171,69 @@ public void sortFamiliesAreSorted() { RowCell.create("family2", col2, 1000, labels, value2))) .inOrder(); } + + @Test + public void protoTest() { + ByteString key = ByteString.copyFromUtf8("key"); + + String family1 = "family1"; + ByteString col1 = ByteString.copyFromUtf8("col1"); + ByteString value1 = ByteString.copyFromUtf8("value1"); + + String family2 = "family2"; + ByteString col2 = ByteString.copyFromUtf8("col2"); + ByteString value2 = ByteString.copyFromUtf8("value2"); + + List labels = ImmutableList.of(); + + com.google.bigtable.v2.Row proto = + com.google.bigtable.v2.Row.newBuilder() + .setKey(key) + .addFamilies( + com.google.bigtable.v2.Family.newBuilder() + .setName(family2) + .addColumns( + com.google.bigtable.v2.Column.newBuilder() + .setQualifier(col1) + .addCells( + com.google.bigtable.v2.Cell.newBuilder() + .setTimestampMicros(2_000) + .setValue(value2))) + .addColumns( + com.google.bigtable.v2.Column.newBuilder() + .setQualifier(col2) + .addCells( + com.google.bigtable.v2.Cell.newBuilder() + .setTimestampMicros(2_000) + .setValue(value2)))) + .addFamilies( + com.google.bigtable.v2.Family.newBuilder() + .setName(family1) + .addColumns( + com.google.bigtable.v2.Column.newBuilder() + .setQualifier(col1) + .addCells( + com.google.bigtable.v2.Cell.newBuilder() + .setTimestampMicros(1_000) + .setValue(value1))) + .addColumns( + com.google.bigtable.v2.Column.newBuilder() + .setQualifier(col2) + .addCells( + com.google.bigtable.v2.Cell.newBuilder() + .setTimestampMicros(1_000) + .setValue(value1)))) + .build(); + + Row row = adapter.createRowFromProto(proto); + assertThat(row) + .isEqualTo( + Row.create( + key, + ImmutableList.of( + RowCell.create(family1, col1, 1_000, labels, value1), + RowCell.create(family1, col2, 1_000, labels, value1), + RowCell.create(family2, col1, 2_000, labels, value2), + RowCell.create(family2, col2, 2_000, labels, value2)))); + } }