Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Row> 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(),
cell.getValue().size());
rowBuilder.cellValue(cell.getValue());
rowBuilder.finishCell();
}
}
}

return rowBuilder.finishRow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +34,7 @@

@RunWith(JUnit4.class)
public class RowTest {

@Test
public void compareTest() {
Row row1 =
Expand Down Expand Up @@ -162,4 +166,75 @@ 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() {
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(rowKey)
.addFamilies(
Family.newBuilder()
.setName(family2)
.addColumns(
Column.newBuilder()
.setQualifier(qualifier1)
.addCells(
Cell.newBuilder()
.setValue(value)
.setTimestampMicros(98765)
.addLabels(label)
.build())
.build()))
.addFamilies(
Family.newBuilder()
.setName(family1)
.addColumns(
Column.newBuilder()
.setQualifier(qualifier1)
.addCells(
Cell.newBuilder()
.setValue(value)
.setTimestampMicros(12345)
.addLabels(label)
.build())
.build())
.addColumns(
Column.newBuilder()
.setQualifier(qualifier2)
.addCells(
Cell.newBuilder()
.setValue(value)
.setTimestampMicros(54321)
.addLabels(label)
.build())
.build())
.build())
.build();
Row row = Row.fromProto(rowProto);

List<String> labels = ImmutableList.of(label);
assertThat(row.getKey()).isEqualTo(rowKey);
assertThat(row.getCells(family1))
.containsExactly(
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));
}
}