Skip to content

Commit e67ac61

Browse files
chore: add internal api for row merging (#1465)
This is a feature for an internal google integration Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/java-bigtable/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> ☕️ If you write sample code, please follow the [samples format]( https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
1 parent 1c0a682 commit e67ac61

File tree

4 files changed

+206
-2
lines changed

4 files changed

+206
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ If you are using Maven without BOM, add this to your dependencies:
4949
If you are using Gradle 5.x or later, add this to your dependencies:
5050

5151
```Groovy
52-
implementation platform('com.google.cloud:libraries-bom:26.1.3')
52+
implementation platform('com.google.cloud:libraries-bom:26.1.4')
5353
5454
implementation 'com.google.cloud:google-cloud-bigtable'
5555
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.bigtable.data.v2.internal;
17+
18+
import com.google.api.core.InternalApi;
19+
import com.google.bigtable.v2.ReadRowsResponse;
20+
import com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter;
21+
import com.google.cloud.bigtable.data.v2.models.Row;
22+
import com.google.cloud.bigtable.data.v2.models.RowAdapter.RowBuilder;
23+
import com.google.cloud.bigtable.data.v2.stub.readrows.RowMerger;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
@InternalApi("For internal google use only")
28+
public class RowMergerUtil implements AutoCloseable {
29+
private final RowMerger<Row> merger;
30+
31+
public RowMergerUtil() {
32+
RowBuilder<Row> rowBuilder = new DefaultRowAdapter().createRowBuilder();
33+
merger = new RowMerger<>(rowBuilder);
34+
}
35+
36+
@Override
37+
public void close() {
38+
if (merger.hasPartialFrame()) {
39+
throw new IllegalStateException("Tried to close merger with unmerged partial data");
40+
}
41+
}
42+
43+
public List<Row> parseReadRowsResponses(Iterable<ReadRowsResponse> responses) {
44+
List<Row> rows = new ArrayList<>();
45+
46+
for (ReadRowsResponse response : responses) {
47+
merger.push(response);
48+
while (merger.hasFullFrame()) {
49+
rows.add(merger.pop());
50+
}
51+
}
52+
53+
return rows;
54+
}
55+
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/DefaultRowAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public Row createRowFromProto(com.google.bigtable.v2.Row row) {
7575
return builder.finishRow();
7676
}
7777

78-
/** {@inheritDoc} */
78+
/** Internal implementation detail for {@link DefaultRowAdapter}. */
79+
@InternalApi()
7980
public class DefaultRowBuilder implements RowBuilder<Row> {
8081
private ByteString currentKey;
8182
private TreeMap<String, ImmutableList.Builder<RowCell>> cellsByFamily;
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.bigtable.data.v2.internal;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
20+
import com.google.bigtable.v2.ReadRowsResponse;
21+
import com.google.bigtable.v2.ReadRowsResponse.CellChunk;
22+
import com.google.cloud.bigtable.data.v2.models.Row;
23+
import com.google.cloud.bigtable.data.v2.models.RowCell;
24+
import com.google.common.collect.ImmutableList;
25+
import com.google.protobuf.ByteString;
26+
import com.google.protobuf.BytesValue;
27+
import com.google.protobuf.StringValue;
28+
import java.util.List;
29+
import junit.framework.TestCase;
30+
import org.junit.Assert;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
@RunWith(JUnit4.class)
36+
public class RowMergerUtilTest extends TestCase {
37+
38+
@Test
39+
public void testEmpty() {
40+
try (RowMergerUtil util = new RowMergerUtil()) {}
41+
42+
try (RowMergerUtil util = new RowMergerUtil()) {
43+
util.parseReadRowsResponses(ImmutableList.of());
44+
}
45+
}
46+
47+
@Test
48+
public void testSingle() {
49+
List<ReadRowsResponse> responses =
50+
ImmutableList.of(
51+
ReadRowsResponse.newBuilder()
52+
.addChunks(
53+
CellChunk.newBuilder()
54+
.setRowKey(ByteString.copyFromUtf8("key"))
55+
.setFamilyName(StringValue.newBuilder().setValue("family"))
56+
.setQualifier(
57+
BytesValue.newBuilder().setValue(ByteString.copyFromUtf8("qualifier")))
58+
.setTimestampMicros(1000)
59+
.setValue(ByteString.copyFromUtf8("value"))
60+
.setCommitRow(true))
61+
.build());
62+
try (RowMergerUtil util = new RowMergerUtil()) {
63+
List<Row> rows = util.parseReadRowsResponses(responses);
64+
assertThat(rows)
65+
.containsExactly(
66+
Row.create(
67+
ByteString.copyFromUtf8("key"),
68+
ImmutableList.of(
69+
RowCell.create(
70+
"family",
71+
ByteString.copyFromUtf8("qualifier"),
72+
1000,
73+
ImmutableList.of(),
74+
ByteString.copyFromUtf8("value")))));
75+
}
76+
}
77+
78+
@Test
79+
public void testMultiple() {
80+
List<ReadRowsResponse> responses =
81+
ImmutableList.of(
82+
ReadRowsResponse.newBuilder()
83+
.addChunks(
84+
CellChunk.newBuilder()
85+
.setRowKey(ByteString.copyFromUtf8("key"))
86+
.setFamilyName(StringValue.newBuilder().setValue("family"))
87+
.setQualifier(
88+
BytesValue.newBuilder().setValue(ByteString.copyFromUtf8("qualifier")))
89+
.setTimestampMicros(1000)
90+
.setValue(ByteString.copyFromUtf8("value"))
91+
.setCommitRow(true))
92+
.build(),
93+
ReadRowsResponse.newBuilder()
94+
.addChunks(
95+
CellChunk.newBuilder()
96+
.setRowKey(ByteString.copyFromUtf8("key2"))
97+
.setFamilyName(StringValue.newBuilder().setValue("family"))
98+
.setQualifier(
99+
BytesValue.newBuilder().setValue(ByteString.copyFromUtf8("qualifier")))
100+
.setTimestampMicros(1000)
101+
.setValue(ByteString.copyFromUtf8("value"))
102+
.setCommitRow(true))
103+
.build());
104+
try (RowMergerUtil util = new RowMergerUtil()) {
105+
assertThat(util.parseReadRowsResponses(responses))
106+
.containsExactly(
107+
Row.create(
108+
ByteString.copyFromUtf8("key"),
109+
ImmutableList.of(
110+
RowCell.create(
111+
"family",
112+
ByteString.copyFromUtf8("qualifier"),
113+
1000,
114+
ImmutableList.of(),
115+
ByteString.copyFromUtf8("value")))),
116+
Row.create(
117+
ByteString.copyFromUtf8("key2"),
118+
ImmutableList.of(
119+
RowCell.create(
120+
"family",
121+
ByteString.copyFromUtf8("qualifier"),
122+
1000,
123+
ImmutableList.of(),
124+
ByteString.copyFromUtf8("value")))));
125+
}
126+
}
127+
128+
@Test
129+
public void testPartial() {
130+
List<ReadRowsResponse> responses =
131+
ImmutableList.of(
132+
ReadRowsResponse.newBuilder()
133+
.addChunks(
134+
CellChunk.newBuilder()
135+
.setRowKey(ByteString.copyFromUtf8("key"))
136+
.setFamilyName(StringValue.newBuilder().setValue("family"))
137+
.setQualifier(
138+
BytesValue.newBuilder().setValue(ByteString.copyFromUtf8("qualifier")))
139+
.setTimestampMicros(1000)
140+
.setValue(ByteString.copyFromUtf8("value"))
141+
.setCommitRow(false))
142+
.build());
143+
144+
RowMergerUtil util = new RowMergerUtil();
145+
util.parseReadRowsResponses(responses);
146+
Assert.assertThrows(IllegalStateException.class, util::close);
147+
}
148+
}

0 commit comments

Comments
 (0)