Skip to content

Commit 3d11659

Browse files
rahulKQLJesseLovelace
authored andcommitted
Added Query#fromProto to convert protobuf ReadRowsRequest (#4291)
* Adding Query.fromProto * Adding Query.fromProto * adding changes as per feedback * updated javadoc as per feedback * reformatted Query & QueryTest.java
1 parent 4f4f558 commit 3d11659

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

  • google-cloud-clients/google-cloud-bigtable/src

google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/NameUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.google.cloud.bigtable.data.v2.internal;
1717

1818
import com.google.api.core.InternalApi;
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
1921
import javax.annotation.Nonnull;
2022

2123
/**
@@ -26,6 +28,9 @@
2628
*/
2729
@InternalApi
2830
public class NameUtil {
31+
private static final Pattern TABLE_PATTERN =
32+
Pattern.compile("projects/([^/]+)/instances/([^/]+)/tables/([^/]+)");
33+
2934
public static String formatInstanceName(@Nonnull String projectId, @Nonnull String instanceId) {
3035
return "projects/" + projectId + "/instances/" + instanceId;
3136
}
@@ -34,4 +39,12 @@ public static String formatTableName(
3439
@Nonnull String projectId, @Nonnull String instanceId, @Nonnull String tableId) {
3540
return formatInstanceName(projectId, instanceId) + "/tables/" + tableId;
3641
}
42+
43+
public static String extractTableIdFromTableName(@Nonnull String fullTableName) {
44+
Matcher matcher = TABLE_PATTERN.matcher(fullTableName);
45+
if (!matcher.matches()) {
46+
throw new IllegalArgumentException("Invalid table name: " + fullTableName);
47+
}
48+
return matcher.group(3);
49+
}
3750
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.io.Serializable;
3737
import java.util.List;
3838
import java.util.SortedSet;
39+
import javax.annotation.Nonnull;
3940

4041
/** A simple wrapper to construct a query for the ReadRows RPC. */
4142
public final class Query implements Serializable {
@@ -258,6 +259,21 @@ public ReadRowsRequest toProto(RequestContext requestContext) {
258259
.build();
259260
}
260261

262+
/**
263+
* Wraps the protobuf {@link ReadRowsRequest}.
264+
*
265+
* <p>WARNING: Please note that the project id & instance id in the table name will be overwritten
266+
* by the configuration in the BigtableDataClient.
267+
*/
268+
public static Query fromProto(@Nonnull ReadRowsRequest request) {
269+
Preconditions.checkArgument(request != null, "ReadRowsRequest must not be null");
270+
271+
Query query = new Query(NameUtil.extractTableIdFromTableName(request.getTableName()));
272+
query.builder = request.toBuilder();
273+
274+
return query;
275+
}
276+
261277
private static ByteString wrapKey(String key) {
262278
if (key == null) {
263279
return null;

google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/QueryTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
import java.util.List;
3939
import java.util.SortedSet;
4040
import org.junit.Before;
41+
import org.junit.Rule;
4142
import org.junit.Test;
43+
import org.junit.rules.ExpectedException;
4244
import org.junit.runner.RunWith;
4345
import org.junit.runners.JUnit4;
4446

@@ -50,6 +52,8 @@ public class QueryTest {
5052
private static final String APP_PROFILE_ID = "fake-profile-id";
5153
private RequestContext requestContext;
5254

55+
@Rule public ExpectedException expect = ExpectedException.none();
56+
5357
@Before
5458
public void setUp() {
5559
requestContext = RequestContext.create(PROJECT_ID, INSTANCE_ID, APP_PROFILE_ID);
@@ -226,4 +230,32 @@ private static ReadRowsRequest.Builder expectedProtoBuilder() {
226230
.setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
227231
.setAppProfileId(APP_PROFILE_ID);
228232
}
233+
234+
@Test
235+
public void testFromProto() {
236+
ReadRowsRequest request =
237+
ReadRowsRequest.newBuilder()
238+
.setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
239+
.setAppProfileId(APP_PROFILE_ID)
240+
.setFilter(RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*")))
241+
.setRows(
242+
RowSet.newBuilder()
243+
.addRowKeys(ByteString.copyFromUtf8("row-key"))
244+
.addRowRanges(
245+
RowRange.newBuilder()
246+
.setStartKeyClosed(ByteString.copyFromUtf8("j"))
247+
.setEndKeyClosed(ByteString.copyFromUtf8("z"))))
248+
.build();
249+
Query query = Query.fromProto(request);
250+
251+
assertThat(query.toProto(requestContext)).isEqualTo(request);
252+
}
253+
254+
@Test(expected = IllegalArgumentException.class)
255+
public void testFromProtoWithEmptyTableId() {
256+
Query.fromProto(ReadRowsRequest.getDefaultInstance());
257+
258+
expect.expect(IllegalArgumentException.class);
259+
expect.expectMessage("Invalid table name:");
260+
}
229261
}

0 commit comments

Comments
 (0)