8000 Added Query#fromProto to convert protobuf ReadRowsRequest by rahulKQL · Pull Request #4291 · googleapis/google-cloud-java · GitHub
[go: up one dir, main page]

Skip to content
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 @@ -16,6 +16,8 @@
package com.google.cloud.bigtable.data.v2.internal;

import com.google.api.core.InternalApi;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;

/**
Expand All @@ -26,6 +28,9 @@
*/
@InternalApi
public class NameUtil {
private static final Pattern TABLE_PATTERN =
Pattern.compile("projects/([^/]+)/instances/([^/]+)/tables/([^/]+)");

public static String formatInstanceName(@Nonnull String projectId, @Nonnull String instanceId) {
return "projects/" + projectId + "/instances/" + instanceId;
}
Expand All @@ -34,4 +39,12 @@ public static String formatTableName(
@Nonnull String projectId, @Nonnull String instanceId, @Nonnull String tableId) {
return formatInstanceName(projectId, instanceId) + "/tables/" + tableId;
}

public static String extractTableIdFromTableName(@Nonnull String fullTableName) {
Matcher matcher = TABLE_PATTERN.matcher(fullTableName);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid table name: " + fullTableName);
}
return matcher.group(3);
}
}
10BC0
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.io.Serializable;
import java.util.List;
import java.util.SortedSet;
import javax.annotation.Nonnull;

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

/**
* Wraps the protobuf {@link ReadRowsRequest}.
*
* <p>WARNING: Please note that the project id & instance id in the table name will be overwritten
* by the configuration in the BigtableDataClient.
*/
public static Query fromProto(@Nonnull ReadRowsRequest request) {
Preconditions.checkArgument(request != null, "ReadRowsRequest must not be null");

Query query = new Query(NameUtil.extractTableIdFromTableName(request.getTableName()));
query.builder = request.toBuilder();

return query;
}

private static ByteString wrapKey(String key) {
if (key == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
import java.util.List;
import java.util.SortedSet;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

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

@Rule public ExpectedException expect = ExpectedException.none();

@Before
public void setUp() {
requestContext = RequestContext.create(PROJECT_ID, INSTANCE_ID, APP_PROFILE_ID);
Expand Down Expand Up @@ -226,4 +230,32 @@ private static ReadRowsRequest.Builder expectedProtoBuilder() {
.setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
.setAppProfileId(APP_PROFILE_ID);
}

@Test
public void testFromProto() {
ReadRowsRequest request =
ReadRowsRequest.newBuilder()
.setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
.setAppProfileId(APP_PROFILE_ID)
.setFilter(RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*")))
.setRows(
RowSet.newBuilder()
.addRowKeys(ByteString.copyFromUtf8("row-key"))
.addRowRanges(
RowRange.newBuilder()
.setStartKeyClosed(ByteString.copyFromUtf8("j"))
.setEndKeyClosed(ByteString.copyFromUtf8("z"))))
.build();
Query query = Query.fromProto(request);

assertThat(query.toProto(requestContext)).isEqualTo(request);
}

@Test(expected = IllegalArgumentException.class)
public void testFromProtoWithEmptyTableId() {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Query.fromProto(ReadRowsRequest.getDefaultInstance());

expect.expect(IllegalArgumentException.class);
expect.expectMessage("Invalid table name:");
}
}
0