Skip to content

Commit 601e644

Browse files
committed
fix according to PR comment
1 parent 4c48d5b commit 601e644

6 files changed

Lines changed: 64 additions & 9 deletions

File tree

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,41 @@ public static String extractMaterializedViewIdFromMaterializedViewName(
106106
return matcher.group(3);
107107
}
108108

109+
/** A helper to convert fully qualified tableName andauthorizedViewName to a {@link TargetId} */
110+
public static TargetId extractTargetId(
111+
@Nonnull String tableName, @Nonnull String authorizedViewName) {
112+
if (tableName.isEmpty() && authorizedViewName.isEmpty()) {
113+
throw new IllegalArgumentException(
114+
"Either table name, authorized view name or materialized view name must be specified. Table name: "
115+
+ tableName
116+
+ ", authorized view name: "
117+
+ authorizedViewName);
118+
}
119+
int names = 0;
120+
if (!tableName.isEmpty()) {
121+
++names;
122+
}
123+
if (!authorizedViewName.isEmpty()) {
124+
++names;
125+
}
126+
if (names > 1) {
127+
throw new IllegalArgumentException(
128+
"Only one of table name and authorized view name can be specified at the same time. Table name: "
129+
+ tableName
130+
+ ", authorized view name: "
131+
+ authorizedViewName);
132+
}
133+
134+
if (!tableName.isEmpty()) {
135+
String tableId = extractTableIdFromTableName(tableName);
136+
return TableId.of(tableId);
137+
} else if (!authorizedViewName.isEmpty()) {
138+
String tableId = extractTableIdFromAuthorizedViewName(authorizedViewName);
139+
String authorizedViewId = extractAuthorizedViewIdFromAuthorizedViewName(authorizedViewName);
140+
return AuthorizedViewId.of(tableId, authorizedViewId);
141+
}
142+
}
143+
109144
/**
110145
* A helper to convert fully qualified tableName, authorizedViewName and materializedViewName to a
111146
* {@link TargetId}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,8 @@ public static BulkMutation fromProto(@Nonnull MutateRowsRequest request) {
155155
String tableName = request.getTableName();
156156
String authorizedViewName = request.getAuthorizedViewName();
157157

158-
// Materialized Views are read only entities.
159158
BulkMutation bulkMutation =
160-
BulkMutation.create(NameUtil.extractTargetId(tableName, authorizedViewName, ""));
159+
BulkMutation.create(NameUtil.extractTargetId(tableName, authorizedViewName));
161160
bulkMutation.builder = request.toBuilder();
162161

163162
return bulkMutation;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,9 @@ public static ConditionalRowMutation fromProto(@Nonnull CheckAndMutateRowRequest
177177
String tableName = request.getTableName();
178178
String authorizedViewName = request.getAuthorizedViewName();
179179

180-
// Materialized Views are read only entities.
181180
ConditionalRowMutation rowMutation =
182181
ConditionalRowMutation.create(
183-
NameUtil.extractTargetId(tableName, authorizedViewName, ""), request.getRowKey());
182+
NameUtil.extractTargetId(tableName, authorizedViewName), request.getRowKey());
184183
rowMutation.builder = request.toBuilder();
185184

186185
return rowMutation;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,9 @@ public static ReadModifyWriteRow fromProto(@Nonnull ReadModifyWriteRowRequest re
174174
String tableName = request.getTableName();
175175
String authorizedViewName = request.getAuthorizedViewName();
176176

177-
// Materialized Views are read only entities.
178177
ReadModifyWriteRow row =
179178
ReadModifyWriteRow.create(
180-
NameUtil.extractTargetId(tableName, authorizedViewName, ""), request.getRowKey());
179+
NameUtil.extractTargetId(tableName, authorizedViewName), request.getRowKey());
181180
row.builder = request.toBuilder();
182181

183182
return row;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ public static RowMutation fromProto(@Nonnull MutateRowRequest request) {
304304
String authorizedViewName = request.getAuthorizedViewName();
305305

306306
return RowMutation.create(
307-
// Materialized Views are read only entities.
308-
NameUtil.extractTargetId(tableName, authorizedViewName, ""),
307+
NameUtil.extractTargetId(tableName, authorizedViewName),
309308
request.getRowKey(),
310309
Mutation.fromProto(request.getMutationsList()));
311310
}

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/internal/NameUtilTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,31 @@ public void extractTableNameFromAuthorizedViewNameTest() {
103103
}
104104

105105
@Test
106-
public void testExtractTargetId() {
106+
public void testExtractTargetId2() {
107+
String testTableName = "projects/my-project/instances/my-instance/tables/my-table";
108+
String testAuthorizedViewName =
109+
"projects/my-project/instances/my-instance/tables/my-table/authorizedViews/my-authorized-view";
110+
assertThat(
111+
com.google.cloud.bigtable.data.v2.internal.NameUtil.extractTargetId(
112+
testTableName, "", ""))
113+
.isEqualTo(TableId.of("my-table"));
114+
assertThat(
115+
com.google.cloud.bigtable.data.v2.internal.NameUtil.extractTargetId(
116+
"", testAuthorizedViewName, ""))
117+
.isEqualTo(AuthorizedViewId.of("my-table", "my-authorized-view"));
118+
119+
// No name is provided
120+
exception.expect(IllegalArgumentException.class);
121+
com.google.cloud.bigtable.data.v2.internal.NameUtil.extractTargetId("", "");
122+
123+
// Multiple names are provided
124+
exception.expect(IllegalArgumentException.class);
125+
com.google.cloud.bigtable.data.v2.internal.NameUtil.extractTargetId(
126+
testTableName, testAuthorizedViewName);
127+
}
128+
129+
@Test
130+
public void testExtractTargetId3() {
107131
String testTableName = "projects/my-project/instances/my-instance/tables/my-table";
108132
String testAuthorizedViewName =
109133
"projects/my-project/instances/my-instance/tables/my-table/authorizedViews/my-authorized-view";

0 commit comments

Comments
 (0)