From 778caccb36ce1eaf6de3ed0ad579514640f9a1f3 Mon Sep 17 00:00:00 2001 From: Rahul Kesharwani Date: Wed, 6 Feb 2019 20:41:23 +0530 Subject: [PATCH 1/3] Adding Row#fromProto to generate models.Row out of protobufRow. --- .../cloud/bigtable/data/v2/models/Row.java | 30 ++++++++ .../bigtable/data/v2/models/RowTest.java | 73 +++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/Row.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/Row.java index 83b71bf19e9d..2a29179ed387 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/Row.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/Row.java @@ -18,7 +18,11 @@ import com.google.api.core.InternalApi; import com.google.api.core.InternalExtensionOnly; import com.google.auto.value.AutoValue; +import com.google.bigtable.v2.Cell; +import com.google.bigtable.v2.Column; +import com.google.bigtable.v2.Family; import com.google.cloud.bigtable.data.v2.internal.ByteStringComparator; +import com.google.cloud.bigtable.data.v2.models.RowAdapter.RowBuilder; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.protobuf.ByteString; @@ -167,4 +171,30 @@ private int getLast(@Nonnull String family, @Nullable ByteString qualifier, int } return index; } + + /** wraps the protobuf {@link com.google.bigtable.v2.Row} */ + public static Row fromProto(@Nonnull com.google.bigtable.v2.Row rowProto) { + Preconditions.checkArgument(rowProto != null, "Row must not be null"); + + DefaultRowAdapter adapter = new DefaultRowAdapter(); + RowBuilder rowBuilder = adapter.createRowBuilder(); + rowBuilder.startRow(rowProto.getKey()); + + for (Family family : rowProto.getFamiliesList()) { + for (Column column : family.getColumnsList()) { + for (Cell cell : column.getCellsList()) { + rowBuilder.startCell( + family.getName(), + column.getQualifier(), + cell.getTimestampMicros(), + cell.getLabelsList(), + column.getCellsCount()); + rowBuilder.cellValue(cell.getValue()); + rowBuilder.finishCell(); + } + } + } + + return rowBuilder.finishRow(); + } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowTest.java index b361ff2d0a3b..480d3f6f2f60 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowTest.java @@ -17,6 +17,9 @@ import static com.google.common.truth.Truth.assertThat; +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.io.ByteArrayInputStream; @@ -31,6 +34,13 @@ @RunWith(JUnit4.class) public class RowTest { + private static final ByteString QUALIFIER_1 = ByteString.copyFromUtf8("Firstqualifier"); + private static final ByteString QUALIFIER_2 = ByteString.copyFromUtf8("Anotherqualifier"); + private static final int TIMESTAMP = 12345; + private static final String LABEL = "label"; + private static final ByteString VALUE = ByteString.copyFromUtf8("test-value"); + private static final ByteString ROW_KEY = ByteString.copyFromUtf8("test-key"); + @Test public void compareTest() { Row row1 = @@ -162,4 +172,67 @@ public void getQualifierCellsTest() { assertThat(row.getCells("family4", col1)) .containsExactly(RowCell.create("family4", col1, 1_000, labels, value)); } + + @Test + public void testFromProtoWithEmptyRow() { + com.google.bigtable.v2.Row rowProto = com.google.bigtable.v2.Row.getDefaultInstance(); + Row row = Row.fromProto(rowProto); + assertThat(row.getKey()).isEqualTo(ByteString.EMPTY); + assertThat(row.getCells()).isEmpty(); + } + + @Test + public void testFromProto() { + com.google.bigtable.v2.Row rowProto = + com.google.bigtable.v2.Row.newBuilder() + .setKey(ROW_KEY) + .addFamilies( + Family.newBuilder() + .setName("secondFamily") + .addColumns( + Column.newBuilder() + .setQualifier(QUALIFIER_1) + .addCells( + Cell.newBuilder() + .setValue(VALUE) + .setTimestampMicros(TIMESTAMP) + .addLabels(LABEL) + .build()) + .build())) + .addFamilies( + Family.newBuilder() + .setName("firstFamily") + .addColumns( + Column.newBuilder() + .setQualifier(QUALIFIER_1) + .addCells( + Cell.newBuilder() + .setValue(VALUE) + .setTimestampMicros(TIMESTAMP) + .addLabels(LABEL) + .build()) + .build()) + .addColumns( + Column.newBuilder() + .setQualifier(QUALIFIER_2) + .addCells( + Cell.newBuilder() + .setValue(VALUE) + .setTimestampMicros(54321) + .addLabels(LABEL) + .build()) + .build()) + .build()) + .build(); + Row row = Row.fromProto(rowProto); + + List labels = ImmutableList.of(LABEL); + assertThat(row.getKey()).isEqualTo(ROW_KEY); + assertThat(row.getCells("firstFamily")) + .containsExactly( + RowCell.create("firstFamily", QUALIFIER_1, TIMESTAMP, labels, VALUE), + RowCell.create("firstFamily", QUALIFIER_2, 54321, labels, VALUE)); + assertThat(row.getCells("secondFamily")) + .containsExactly(RowCell.create("secondFamily", QUALIFIER_1, TIMESTAMP, labels, VALUE)); + } } From 5030067fb9a63192af06ed23691a9348cf7a2dca Mon Sep 17 00:00:00 2001 From: Rahul Kesharwani Date: Wed, 6 Feb 2019 20:50:25 +0530 Subject: [PATCH 2/3] Fixed test case constants --- .../bigtable/data/v2/models/RowTest.java | 56 ++++++++++--------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowTest.java index 480d3f6f2f60..dcc5757c4ddf 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowTest.java @@ -34,12 +34,6 @@ @RunWith(JUnit4.class) public class RowTest { - private static final ByteString QUALIFIER_1 = ByteString.copyFromUtf8("Firstqualifier"); - private static final ByteString QUALIFIER_2 = ByteString.copyFromUtf8("Anotherqualifier"); - private static final int TIMESTAMP = 12345; - private static final String LABEL = "label"; - private static final ByteString VALUE = ByteString.copyFromUtf8("test-value"); - private static final ByteString ROW_KEY = ByteString.copyFromUtf8("test-key"); @Test public void compareTest() { @@ -183,56 +177,64 @@ public void testFromProtoWithEmptyRow() { @Test public void testFromProto() { + ByteString rowKey = ByteString.copyFromUtf8("test-key"); + ByteString qualifier1 = ByteString.copyFromUtf8("qualifier1"); + ByteString qualifier2 = ByteString.copyFromUtf8("qualifier2"); + ByteString value = ByteString.copyFromUtf8("test-value"); + String family1 = "firstFamily"; + String family2 = "secondFamily"; + String label = "label"; + com.google.bigtable.v2.Row rowProto = com.google.bigtable.v2.Row.newBuilder() - .setKey(ROW_KEY) + .setKey(rowKey) .addFamilies( Family.newBuilder() - .setName("secondFamily") + .setName(family2) .addColumns( Column.newBuilder() - .setQualifier(QUALIFIER_1) + .setQualifier(qualifier1) .addCells( Cell.newBuilder() - .setValue(VALUE) - .setTimestampMicros(TIMESTAMP) - .addLabels(LABEL) + .setValue(value) + .setTimestampMicros(98765) + .addLabels(label) .build()) .build())) .addFamilies( Family.newBuilder() - .setName("firstFamily") + .setName(family1) .addColumns( Column.newBuilder() - .setQualifier(QUALIFIER_1) + .setQualifier(qualifier1) .addCells( Cell.newBuilder() - .setValue(VALUE) - .setTimestampMicros(TIMESTAMP) - .addLabels(LABEL) + .setValue(value) + .setTimestampMicros(12345) + .addLabels(label) .build()) .build()) .addColumns( Column.newBuilder() - .setQualifier(QUALIFIER_2) + .setQualifier(qualifier2) .addCells( Cell.newBuilder() - .setValue(VALUE) + .setValue(value) .setTimestampMicros(54321) - .addLabels(LABEL) + .addLabels(label) .build()) .build()) .build()) .build(); Row row = Row.fromProto(rowProto); - List labels = ImmutableList.of(LABEL); - assertThat(row.getKey()).isEqualTo(ROW_KEY); - assertThat(row.getCells("firstFamily")) + List labels = ImmutableList.of(label); + assertThat(row.getKey()).isEqualTo(rowKey); + assertThat(row.getCells(family1)) .containsExactly( - RowCell.create("firstFamily", QUALIFIER_1, TIMESTAMP, labels, VALUE), - RowCell.create("firstFamily", QUALIFIER_2, 54321, labels, VALUE)); - assertThat(row.getCells("secondFamily")) - .containsExactly(RowCell.create("secondFamily", QUALIFIER_1, TIMESTAMP, labels, VALUE)); + RowCell.create(family1, qualifier1, 12345, labels, value), + RowCell.create(family1, qualifier2, 54321, labels, value)); + assertThat(row.getCells(family2)) + .containsExactly(RowCell.create(family2, qualifier1, 98765, labels, value)); } } From eb40354d0eefbc156417691432255b6c9d51b4a3 Mon Sep 17 00:00:00 2001 From: Rahul Kesharwani Date: Thu, 7 Feb 2019 17:33:16 +0530 Subject: [PATCH 3/3] updated RowBuilder#startCell size argument to cell#value#size --- .../java/com/google/cloud/bigtable/data/v2/models/Row.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/Row.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/Row.java index 2a29179ed387..07c686883a50 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/Row.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/Row.java @@ -172,7 +172,7 @@ private int getLast(@Nonnull String family, @Nullable ByteString qualifier, int return index; } - /** wraps the protobuf {@link com.google.bigtable.v2.Row} */ + /** Wraps the protobuf {@link com.google.bigtable.v2.Row} */ public static Row fromProto(@Nonnull com.google.bigtable.v2.Row rowProto) { Preconditions.checkArgument(rowProto != null, "Row must not be null"); @@ -188,7 +188,7 @@ public static Row fromProto(@Nonnull com.google.bigtable.v2.Row rowProto) { column.getQualifier(), cell.getTimestampMicros(), cell.getLabelsList(), - column.getCellsCount()); + cell.getValue().size()); rowBuilder.cellValue(cell.getValue()); rowBuilder.finishCell(); }