Skip to content

Commit 1ccf063

Browse files
authored
fix: Clean up RowResultAdapter (#3267)
* fix: Clean up RowResultAdapter * use rawCells() and update javadoc * update getKey
1 parent bfa43de commit 1ccf063

File tree

2 files changed

+42
-51
lines changed

2 files changed

+42
-51
lines changed

bigtable-client-core-parent/bigtable-hbase/src/main/java/com/google/cloud/bigtable/hbase/wrappers/veneer/RowResultAdapter.java

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.cloud.bigtable.hbase.adapters.read.RowCell;
2323
import com.google.cloud.bigtable.hbase.util.ByteStringer;
2424
import com.google.cloud.bigtable.hbase.util.TimestampConverter;
25-
import com.google.common.annotations.VisibleForTesting;
2625
import com.google.common.base.Preconditions;
2726
import com.google.common.collect.ImmutableList;
2827
import com.google.protobuf.ByteString;
@@ -47,59 +46,44 @@
4746
* on duplicate labelled cells for its implementation. So this adapter will not deduplicate labelled
4847
* cells.
4948
*
49+
* <p>This adapter will also return and check for scan marker rows, which will be an empty row with
50+
* the scan marker row label.
51+
*
5052
* <p>For internal use only - public for technical reasons.
5153
*/
5254
@InternalApi("For internal usage only")
5355
public class RowResultAdapter implements RowAdapter<Result> {
5456

5557
private static final byte[] EMPTY_VALUE = new byte[0];
58+
private static final String SCAN_MARKER_ROW_LABEL = "bigtable-scan-marker-row";
5659

5760
@Override
5861
public RowBuilder<Result> createRowBuilder() {
5962
return new RowResultBuilder();
6063
}
6164

65+
/**
66+
* Checks if the result is a scan marker row. Returns true if the row's family, qualifier, and
67+
* value are empty, and only has a scan marker row label.
68+
*/
6269
@Override
6370
public boolean isScanMarkerRow(Result result) {
64-
Preconditions.checkState(result instanceof RowResult, "Should be an instance of RowResult");
65-
return ((RowResult) result).isMarkerRow();
71+
if (result.rawCells().length != 1 || !(result.rawCells()[0] instanceof RowCell)) {
72+
return false;
73+
}
74+
75+
RowCell cell = (RowCell) result.rawCells()[0];
76+
return cell.getLabels().size() == 1
77+
&& cell.getLabels().get(0).equals(SCAN_MARKER_ROW_LABEL)
78+
&& cell.getValueArray().length == 0
79+
&& cell.getFamilyArray().length == 0
80+
&& cell.getQualifierArray().length == 0;
6681
}
6782

6883
@Override
6984
public ByteString getKey(Result result) {
70-
Preconditions.checkState(result instanceof RowResult, "Should be an instance of RowResult");
71-
return ((RowResult) result).getKey();
72-
}
73-
74-
@VisibleForTesting
75-
static class RowResult extends Result {
76-
private final ByteString rowKey;
77-
private final boolean isMarkerRow;
78-
79-
static RowResult create(ByteString rowKey, List<Cell> cells) {
80-
return new RowResult(rowKey, false, cells);
81-
}
82-
83-
static RowResult createMarker(ByteString rowKey) {
84-
return new RowResult(rowKey, true, ImmutableList.<Cell>of());
85-
}
86-
87-
private RowResult(ByteString rowKey, boolean isMarkerRow, List<Cell> cells) {
88-
this.rowKey = rowKey;
89-
this.isMarkerRow = isMarkerRow;
90-
91-
// Only default ctor of Result is public, so instantiating cells through copyFrom() because
92-
// value(), size(), isEmpty() rawCells() etc. directly usages Result's cells field.
93-
this.copyFrom(Result.create(cells));
94-
}
95-
96-
ByteString getKey() {
97-
return rowKey;
98-
}
99-
100-
boolean isMarkerRow() {
101-
return isMarkerRow;
102-
}
85+
Cell cell = result.rawCells()[0];
86+
return ByteStringer.wrap(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
10387
}
10488

10589
static class RowResultBuilder implements RowBuilder<Result> {
@@ -226,7 +210,7 @@ public Result finishRow() {
226210
combined.addAll(familyCellList);
227211
}
228212

229-
return RowResult.create(currentKey, combined.build());
213+
return Result.create(combined.build());
230214
}
231215

232216
@Override
@@ -243,9 +227,21 @@ public void reset() {
243227
this.previousNoLabelCell = null;
244228
}
245229

230+
/**
231+
* Creates a marker row with rowKey with a scan marker row label and empty family, qualifier and
232+
* value.
233+
*/
246234
@Override
247235
public Result createScanMarkerRow(ByteString rowKey) {
248-
return RowResult.createMarker(rowKey);
236+
return Result.create(
237+
ImmutableList.<Cell>of(
238+
new RowCell(
239+
ByteStringer.extract(rowKey),
240+
EMPTY_VALUE,
241+
EMPTY_VALUE,
242+
0l,
243+
EMPTY_VALUE,
244+
ImmutableList.<String>of(SCAN_MARKER_ROW_LABEL))));
249245
}
250246
}
251247
}

bigtable-client-core-parent/bigtable-hbase/src/test/java/com/google/cloud/bigtable/hbase/wrappers/veneer/TestRowResultAdapter.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
import static org.junit.Assert.assertArrayEquals;
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertSame;
2221
import static org.junit.Assert.assertTrue;
2322

2423
import com.google.cloud.bigtable.data.v2.models.RowAdapter;
2524
import com.google.cloud.bigtable.hbase.adapters.read.RowCell;
26-
import com.google.cloud.bigtable.hbase.wrappers.veneer.RowResultAdapter.RowResult;
2725
import com.google.common.collect.ImmutableList;
2826
import com.google.protobuf.ByteString;
2927
import java.util.ArrayList;
@@ -63,9 +61,8 @@ public void testWithSingleCellRow() {
6361
rowBuilder.cellValue(VALUE);
6462
rowBuilder.finishCell();
6563

66-
RowResult expected =
67-
RowResult.create(
68-
ROW_KEY,
64+
Result expected =
65+
Result.create(
6966
ImmutableList.<Cell>of(
7067
new RowCell(
7168
ROW_KEY.toByteArray(),
@@ -186,9 +183,8 @@ public void testDeduplicationLogic() {
186183
Result actual = rowBuilder.finishRow();
187184
assertEquals(3, actual.size());
188185

189-
RowResult expected =
190-
RowResult.create(
191-
ROW_KEY,
186+
Result expected =
187+
Result.create(
192188
ImmutableList.<Cell>of(
193189
new RowCell(
194190
ROW_KEY.toByteArray(),
@@ -251,13 +247,12 @@ public void testFamilyOrdering() {
251247

252248
@Test
253249
public void testWithMarkerRow() {
254-
RowResult markerRow = RowResult.createMarker(ROW_KEY);
250+
Result markerRow = new RowResultAdapter.RowResultBuilder().createScanMarkerRow(ROW_KEY);
255251
assertTrue(underTest.isScanMarkerRow(markerRow));
256-
assertSame(ROW_KEY, underTest.getKey(markerRow));
252+
assertEquals(ROW_KEY, underTest.getKey(markerRow));
257253

258-
RowResult resultWithOneCell =
259-
RowResult.create(
260-
ROW_KEY,
254+
Result resultWithOneCell =
255+
Result.create(
261256
ImmutableList.<Cell>of(
262257
new RowCell(
263258
ROW_KEY.toByteArray(),

0 commit comments

Comments
 (0)