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

Bigtable: expose helper to convert proto rows into model rows #4458

Merged
merged 1 commit into from
Feb 6, 2019
Merged
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 @@ -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;
Expand Down Expand Up @@ -43,6 +47,34 @@ public ByteString getKey(Row row) {
return row.getKey();
}

/**
* Helper to convert a proto Row to a model Row.
*
* <p>For internal use only.
*/
@InternalApi
public Row createRowFromProto(com.google.bigtable.v2.Row row) {
RowBuilder<Row> 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<Row> {
private ByteString currentKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReadModifyWriteRow, Row> {
private final UnaryCallable<ReadModifyWriteRowRequest, ReadModifyWriteRowResponse> inner;
private final RequestContext requestContext;
private final RowAdapter<Row> rowAdapter;
private final DefaultRowAdapter rowAdapter;

ReadModifyWriteRowCallable(
UnaryCallable<ReadModifyWriteRowRequest, ReadModifyWriteRowResponse> inner,
Expand All @@ -64,25 +59,6 @@ public Row apply(ReadModifyWriteRowResponse readModifyWriteRowResponse) {
}

private Row convertResponse(ReadModifyWriteRowResponse response) {
RowBuilder<Row> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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))));
}
}