Skip to content

Commit 969190d

Browse files
ron-galandre-sampaio
authored andcommitted
feat(bigtable): Add support for data APIs for materialized views
1 parent 4e45837 commit 969190d

File tree

13 files changed

+240
-24
lines changed

13 files changed

+240
-24
lines changed

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

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.google.api.core.InternalApi;
1919
import com.google.cloud.bigtable.data.v2.models.AuthorizedViewId;
20+
import com.google.cloud.bigtable.data.v2.models.MaterializedViewId;
2021
import com.google.cloud.bigtable.data.v2.models.TableId;
2122
import com.google.cloud.bigtable.data.v2.models.TargetId;
2223
import java.util.regex.Matcher;
@@ -35,6 +36,8 @@ public class NameUtil {
3536
Pattern.compile("projects/([^/]+)/instances/([^/]+)/tables/([^/]+)");
3637
private static final Pattern AUTHORIZED_VIEW_PATTERN =
3738
Pattern.compile("projects/([^/]+)/instances/([^/]+)/tables/([^/]+)/authorizedViews/([^/]+)");
39+
private static final Pattern MATERIALIZED_VIEW_PATTERN =
40+
Pattern.compile("projects/([^/]+)/instances/([^/]+)/materializedView/([^/]+)");
3841

3942
public static String formatInstanceName(@Nonnull String projectId, @Nonnull String instanceId) {
4043
return "projects/" + projectId + "/instances/" + instanceId;
@@ -53,6 +56,11 @@ public static String formatAuthorizedViewName(
5356
return formatTableName(projectId, instanceId, tableId) + "/authorizedViews/" + authorizedViewId;
5457
}
5558

59+
public static String formatMaterializedViewName(
60+
@Nonnull String projectId, @Nonnull String instanceId, @Nonnull String materializedViewId) {
61+
return formatInstanceName(projectId, instanceId) + "/materializedViews/" + materializedViewId;
62+
}
63+
5664
public static String extractTableIdFromTableName(@Nonnull String fullTableName) {
5765
Matcher matcher = TABLE_PATTERN.matcher(fullTableName);
5866
if (!matcher.matches()) {
@@ -88,31 +96,64 @@ public static String extractAuthorizedViewIdFromAuthorizedViewName(
8896
return matcher.group(4);
8997
}
9098

91-
/** A helper to convert fully qualified tableName and authorizedViewName to a {@link TargetId} */
99+
public static String extractMaterializedViewIdFromMaterializedViewName(
100+
@Nonnull String fullMaterializedViewName) {
101+
Matcher matcher = MATERIALIZED_VIEW_PATTERN.matcher(fullMaterializedViewName);
102+
if (!matcher.matches()) {
103+
throw new IllegalArgumentException(
104+
"Invalid materialized view name: " + fullMaterializedViewName);
105+
}
106+
return matcher.group(3);
107+
}
108+
109+
/**
110+
* A helper to convert fully qualified tableName, authorizedViewName and materializedViewName to a
111+
* {@link TargetId}
112+
*/
92113
public static TargetId extractTargetId(
93-
@Nonnull String tableName, @Nonnull String authorizedViewName) {
94-
if (tableName.isEmpty() && authorizedViewName.isEmpty()) {
114+
@Nonnull String tableName,
115+
@Nonnull String authorizedViewName,
116+
@Nonnull String materializedViewName) {
117+
if (tableName.isEmpty() && authorizedViewName.isEmpty() && materializedViewName.isEmpty()) {
95118
throw new IllegalArgumentException(
96-
"Either table name or authorized view name must be specified. Table name: "
119+
"Either table name, authorized view name or materialized view name must be specified. Table name: "
97120
+ tableName
98121
+ ", authorized view name: "
99-
+ authorizedViewName);
122+
+ authorizedViewName
123+
+ ", materialized view name: "
124+
+ materializedViewName);
125+
}
126+
int names = 0;
127+
if (!tableName.isEmpty()) {
128+
++names;
129+
}
130+
if (!authorizedViewName.isEmpty()) {
131+
++names;
100132
}
101-
if (!tableName.isEmpty() && !authorizedViewName.isEmpty()) {
133+
if (!materializedViewName.isEmpty()) {
134+
++names;
135+
}
136+
if (names > 1) {
102137
throw new IllegalArgumentException(
103-
"Table name and authorized view name cannot be specified at the same time. Table name: "
138+
"Only one of table name, authorized view name and materialized view name can be specified at the same time. Table name: "
104139
+ tableName
105140
+ ", authorized view name: "
106-
+ authorizedViewName);
141+
+ authorizedViewName
142+
+ ", materialized view name: "
143+
+ materializedViewName);
107144
}
108145

109146
if (!tableName.isEmpty()) {
110147
String tableId = extractTableIdFromTableName(tableName);
111148
return TableId.of(tableId);
112-
} else {
149+
} else if (!authorizedViewName.isEmpty()) {
113150
String tableId = extractTableIdFromAuthorizedViewName(authorizedViewName);
114151
String authorizedViewId = extractAuthorizedViewIdFromAuthorizedViewName(authorizedViewName);
115152
return AuthorizedViewId.of(tableId, authorizedViewId);
153+
} else {
154+
String materializedViewId =
155+
extractMaterializedViewIdFromMaterializedViewName(materializedViewName);
156+
return MaterializedViewId.of(materializedViewId);
116157
}
117158
}
118159
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@ public String toResourceName(String projectId, String instanceId) {
5252
public boolean scopedForAuthorizedView() {
5353
return true;
5454
}
55+
56+
@Override
57+
@InternalApi
58+
public boolean scopedForMaterializedView() {
59+
return false;
60+
}
5561
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ 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.
158159
BulkMutation bulkMutation =
159-
BulkMutation.create(NameUtil.extractTargetId(tableName, authorizedViewName));
160+
BulkMutation.create(NameUtil.extractTargetId(tableName, authorizedViewName, ""));
160161
bulkMutation.builder = request.toBuilder();
161162

162163
return bulkMutation;

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

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

180+
// Materialized Views are read only entities.
180181
ConditionalRowMutation rowMutation =
181182
ConditionalRowMutation.create(
182-
NameUtil.extractTargetId(tableName, authorizedViewName), request.getRowKey());
183+
NameUtil.extractTargetId(tableName, authorizedViewName, ""), request.getRowKey());
183184
rowMutation.builder = request.toBuilder();
184185

185186
return rowMutation;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2024 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+
17+
package com.google.cloud.bigtable.data.v2.models;
18+
19+
import com.google.api.core.InternalApi;
20+
import com.google.auto.value.AutoValue;
21+
import com.google.cloud.bigtable.data.v2.internal.NameUtil;
22+
import com.google.common.base.Preconditions;
23+
24+
/**
25+
* An implementation of a {@link TargetId} for materialized views.
26+
*
27+
* <p>See {@link com.google.cloud.bigtable.admin.v2.models.MaterializedView} for more details about
28+
* an materialized view.
29+
*/
30+
@AutoValue
31+
public abstract class MaterializedViewId implements TargetId {
32+
/** Constructs a new MaterializedViewId object from the specified materializedViewId. */
33+
public static MaterializedViewId of(String materializedViewId) {
34+
Preconditions.checkNotNull(materializedViewId, "materialized view id can't be null.");
35+
return new AutoValue_MaterializedViewId(materializedViewId);
36+
}
37+
38+
abstract String getMaterializedViewId();
39+
40+
@Override
41+
@InternalApi
42+
public String toResourceName(String projectId, String instanceId) {
43+
return NameUtil.formatMaterializedViewName(projectId, instanceId, getMaterializedViewId());
44+
}
45+
46+
@Override
47+
@InternalApi
48+
public boolean scopedForAuthorizedView() {
49+
return false;
50+
}
51+
52+
@Override
53+
@InternalApi
54+
public boolean scopedForMaterializedView() {
55+
return true;
56+
}
57+
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public static Query create(String tableId) {
6262
* com.google.cloud.bigtable.data.v2.BigtableDataSettings}.
6363
*
6464
* @see AuthorizedViewId
65+
* @see MaterializedViewId
6566
* @see TableId
6667
*/
6768
public static Query create(TargetId targetId) {
@@ -317,7 +318,9 @@ public ByteStringRange getBound() {
317318
public ReadRowsRequest toProto(RequestContext requestContext) {
318319
String resourceName =
319320
targetId.toResourceName(requestContext.getProjectId(), requestContext.getInstanceId());
320-
if (targetId.scopedForAuthorizedView()) {
321+
if (targetId.scopedForMaterializedView()) {
322+
builder.setMaterializedViewName(resourceName);
323+
} else if (targetId.scopedForAuthorizedView()) {
321324
builder.setAuthorizedViewName(resourceName);
322325
} else {
323326
builder.setTableName(resourceName);
@@ -335,8 +338,10 @@ public static Query fromProto(@Nonnull ReadRowsRequest request) {
335338
Preconditions.checkArgument(request != null, "ReadRowsRequest must not be null");
336339
String tableName = request.getTableName();
337340
String authorizedViewName = request.getAuthorizedViewName();
341+
String materializedViewName = request.getMaterializedViewName();
338342

339-
Query query = new Query(NameUtil.extractTargetId(tableName, authorizedViewName));
343+
Query query =
344+
new Query(NameUtil.extractTargetId(tableName, authorizedViewName, materializedViewName));
340345
query.builder = request.toBuilder();
341346

342347
return query;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,10 @@ 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.
177178
ReadModifyWriteRow row =
178179
ReadModifyWriteRow.create(
179-
NameUtil.extractTargetId(tableName, authorizedViewName), request.getRowKey());
180+
NameUtil.extractTargetId(tableName, authorizedViewName, ""), request.getRowKey());
180181
row.builder = request.toBuilder();
181182

182183
return row;

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

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

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

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public com.google.bigtable.v2.SampleRowKeysRequest toProto(RequestContext reques
4444
com.google.bigtable.v2.SampleRowKeysRequest.newBuilder();
4545
String resourceName =
4646
targetId.toResourceName(requestContext.getProjectId(), requestContext.getInstanceId());
47-
if (targetId.scopedForAuthorizedView()) {
47+
if (targetId.scopedForMaterializedView()) {
48+
builder.setMaterializedViewName(resourceName);
49+
} else if (targetId.scopedForAuthorizedView()) {
4850
builder.setAuthorizedViewName(resourceName);
4951
} else {
5052
builder.setTableName(resourceName);
@@ -55,17 +57,19 @@ public com.google.bigtable.v2.SampleRowKeysRequest toProto(RequestContext reques
5557
/**
5658
* Wraps the protobuf {@link com.google.bigtable.v2.SampleRowKeysRequest}.
5759
*
58-
* <p>WARNING: Please note that the project id & instance id in the table/authorized view name
59-
* will be overwritten by the configuration in the BigtableDataClient.
60+
* <p>WARNING: Please note that the project id & instance id in the table/authorized
61+
* view/materialized view name will be overwritten by the configuration in the BigtableDataClient.
6062
*/
6163
@InternalApi
6264
public static SampleRowKeysRequest fromProto(
6365
@Nonnull com.google.bigtable.v2.SampleRowKeysRequest request) {
6466
String tableName = request.getTableName();
6567
String authorizedViewName = request.getAuthorizedViewName();
68+
String materializedViewName = request.getMaterializedViewName();
6669

6770
SampleRowKeysRequest sampleRowKeysRequest =
68-
SampleRowKeysRequest.create(NameUtil.extractTargetId(tableName, authorizedViewName));
71+
SampleRowKeysRequest.create(
72+
NameUtil.extractTargetId(tableName, authorizedViewName, materializedViewName));
6973

7074
return sampleRowKeysRequest;
7175
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,10 @@ public String toResourceName(String projectId, String instanceId) {
4444
public boolean scopedForAuthorizedView() {
4545
return false;
4646
}
47+
48+
@Override
49+
@InternalApi
50+
public boolean scopedForMaterializedView() {
51+
return false;
52+
}
4753
}

0 commit comments

Comments
 (0)