Skip to content

Commit 577539f

Browse files
committed
cherry pick of googleapis#928
1 parent 58c8585 commit 577539f

37 files changed

+2319
-256
lines changed

.kokoro/nightly/integration.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ env_vars: {
2121
value: "java-docs-samples-testing"
2222
}
2323

24+
env_vars: {
25+
key: "DATASTORE_PROJECT_ID"
26+
value: "java-docs-samples-testing"
27+
}
28+
2429
env_vars: {
2530
key: "ENABLE_FLAKYBOT"
2631
value: "true"

.kokoro/nightly/java11-integration.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ env_vars: {
2121
value: "gcloud-devel"
2222
}
2323

24+
env_vars: {
25+
key: "DATASTORE_PROJECT_ID"
26+
value: "gcloud-devel"
27+
}
28+
2429
env_vars: {
2530
key: "ENABLE_FLAKYBOT"
2631
value: "true"

.kokoro/presubmit/graalvm-native-17.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ env_vars: {
3030
env_vars: {
3131
key: "SECRET_MANAGER_KEYS"
3232
value: "java-it-service-account"
33+
}
34+
35+
env_vars: {
36+
key: "DATASTORE_PROJECT_ID"
37+
value: "gcloud-devel"
3338
}

.kokoro/presubmit/graalvm-native.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ env_vars: {
3131
key: "SECRET_MANAGER_KEYS"
3232
value: "java-it-service-account"
3333
}
34+
35+
env_vars: {
36+
key: "DATASTORE_PROJECT_ID"
37+
value: "gcloud-devel"
38+
}

.kokoro/presubmit/integration.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ env_vars: {
2222
value: "gcloud-devel"
2323
}
2424

25+
env_vars: {
26+
key: "DATASTORE_PROJECT_ID"
27+
value: "gcloud-devel"
28+
}
29+
2530
env_vars: {
2631
key: "GOOGLE_APPLICATION_CREDENTIALS"
2732
value: "secret_manager/java-it-service-account"

datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/DatastoreOptions.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
public class DatastoreOptions {
4242
private final String projectId;
43+
private final String databaseId;
4344
private final String projectEndpoint;
4445
private final String host;
4546
private final String localHost;
@@ -56,6 +57,7 @@ public class DatastoreOptions {
5657
b.projectId != null || b.projectEndpoint != null,
5758
"Either project ID or project endpoint must be provided.");
5859
this.projectId = b.projectId;
60+
this.databaseId = b.databaseId;
5961
this.projectEndpoint = b.projectEndpoint;
6062
this.host = b.host;
6163
this.localHost = b.localHost;
@@ -72,6 +74,7 @@ public static class Builder {
7274
"Can set at most one of project endpoint, host, and local host.";
7375

7476
private String projectId;
77+
private String databaseId;
7578
private String projectEndpoint;
7679
private String host;
7780
private String localHost;
@@ -83,6 +86,7 @@ public Builder() {}
8386

8487
public Builder(DatastoreOptions options) {
8588
this.projectId = options.projectId;
89+
this.databaseId = options.databaseId;
8690
this.projectEndpoint = options.projectEndpoint;
8791
this.host = options.host;
8892
this.localHost = options.localHost;
@@ -102,6 +106,12 @@ public Builder projectId(String projectId) {
102106
return this;
103107
}
104108

109+
/** Sets the database ID used to access Cloud Datastore. */
110+
public Builder databaseId(String databaseId) {
111+
this.databaseId = databaseId;
112+
return this;
113+
}
114+
105115
/**
106116
* Sets the host used to access Cloud Datastore. To connect to the Cloud Datastore Emulator, use
107117
* {@link #localHost} instead.
@@ -176,6 +186,10 @@ public String getProjectId() {
176186
return projectId;
177187
}
178188

189+
public String getDatabaseId() {
190+
return databaseId;
191+
}
192+
179193
public String getProjectEndpoint() {
180194
return projectEndpoint;
181195
}

datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/QuerySplitterImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ private List<Key> getScatterKeys(
221221
do {
222222
RunQueryRequest.Builder scatterRequest =
223223
RunQueryRequest.newBuilder().setPartitionId(partition).setQuery(scatterPointQuery);
224+
scatterRequest.setProjectId(partition.getProjectId());
225+
scatterRequest.setDatabaseId(partition.getDatabaseId());
224226
if (readTime != null) {
225227
scatterRequest.setReadOptions(ReadOptions.newBuilder().setReadTime(readTime).build());
226228
}

datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/DatastoreClientTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ public void create_LocalHost() {
215215
.isEqualTo("http://localhost:8080/v1/projects/project-id");
216216
}
217217

218+
@Test
219+
public void setDatabaseId() {
220+
DatastoreOptions options =
221+
new DatastoreOptions.Builder()
222+
.projectId(PROJECT_ID)
223+
.databaseId("test-db")
224+
.localHost("localhost:8080")
225+
.build();
226+
assertThat(options.getProjectId()).isEqualTo(PROJECT_ID);
227+
assertThat(options.getDatabaseId()).isEqualTo("test-db");
228+
}
229+
218230
@Test
219231
public void create_LocalHostIp() {
220232
Datastore datastore =

datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/QuerySplitterTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,60 @@ public void getSplits() throws Exception {
193193
RunQueryRequest expectedSplitQueryRequest =
194194
RunQueryRequest.newBuilder()
195195
.setPartitionId(PARTITION)
196+
.setProjectId(PROJECT_ID)
197+
.setQuery(
198+
splitQuery.toBuilder().setLimit(Int32Value.newBuilder().setValue(2 * 32).build()))
199+
.build();
200+
201+
assertArrayEquals(expectedSplitQueryRequest.toByteArray(), mockClient.getLastBody());
202+
}
203+
204+
@Test
205+
public void getSplitsWithDatabaseId() throws Exception {
206+
Datastore datastore = factory.create(options.build());
207+
MockDatastoreFactory mockClient = (MockDatastoreFactory) factory;
208+
209+
PartitionId partition =
210+
PartitionId.newBuilder().setProjectId(PROJECT_ID).setDatabaseId("test-database").build();
211+
212+
RunQueryResponse splitQueryResponse =
213+
RunQueryResponse.newBuilder()
214+
.setQuery(splitQuery)
215+
.setBatch(
216+
QueryResultBatch.newBuilder()
217+
.setEntityResultType(ResultType.KEY_ONLY)
218+
.setMoreResults(MoreResultsType.NO_MORE_RESULTS)
219+
.addEntityResults(makeKeyOnlyEntity(splitKey0))
220+
.addEntityResults(makeKeyOnlyEntity(splitKey1))
221+
.addEntityResults(makeKeyOnlyEntity(splitKey2))
222+
.addEntityResults(makeKeyOnlyEntity(splitKey3))
223+
.build())
224+
.build();
225+
226+
mockClient.setNextResponse(splitQueryResponse);
227+
228+
List<Query> splitQueries = QuerySplitterImpl.INSTANCE.getSplits(query, partition, 3, datastore);
229+
230+
assertThat(splitQueries)
231+
.containsExactly(
232+
query
233+
.toBuilder()
234+
.setFilter(makeFilterWithKeyRange(propertyFilter, null, splitKey1))
235+
.build(),
236+
query
237+
.toBuilder()
238+
.setFilter(makeFilterWithKeyRange(propertyFilter, splitKey1, splitKey3))
239+
.build(),
240+
query
241+
.toBuilder()
242+
.setFilter(makeFilterWithKeyRange(propertyFilter, splitKey3, null))
243+
.build());
244+
245+
RunQueryRequest expectedSplitQueryRequest =
246+
RunQueryRequest.newBuilder()
247+
.setPartitionId(partition)
248+
.setProjectId(PROJECT_ID)
249+
.setDatabaseId("test-database")
196250
.setQuery(
197251
splitQuery.toBuilder().setLimit(Int32Value.newBuilder().setValue(2 * 32).build()))
198252
.build();
@@ -235,6 +289,7 @@ public void notEnoughSplits() throws Exception {
235289
RunQueryRequest expectedSplitQueryRequest =
236290
RunQueryRequest.newBuilder()
237291
.setPartitionId(PARTITION)
292+
.setProjectId(PROJECT_ID)
238293
.setQuery(
239294
splitQuery.toBuilder().setLimit(Int32Value.newBuilder().setValue(99 * 32).build()))
240295
.build();
@@ -286,6 +341,7 @@ public void getSplits_withReadTime() throws Exception {
286341
RunQueryRequest expectedSplitQueryRequest =
287342
RunQueryRequest.newBuilder()
288343
.setPartitionId(PARTITION)
344+
.setProjectId(PROJECT_ID)
289345
.setQuery(
290346
splitQuery.toBuilder().setLimit(Int32Value.newBuilder().setValue(2 * 32).build()))
291347
.setReadOptions(ReadOptions.newBuilder().setReadTime(readTime))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
* http://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.datastore.v1.client.it;
17+
18+
import static com.google.datastore.v1.client.DatastoreHelper.makeFilter;
19+
import static com.google.datastore.v1.client.DatastoreHelper.makeValue;
20+
21+
import com.google.datastore.v1.Filter;
22+
import com.google.datastore.v1.KindExpression;
23+
import com.google.datastore.v1.PartitionId;
24+
import com.google.datastore.v1.PropertyFilter;
25+
import com.google.datastore.v1.Query;
26+
import com.google.datastore.v1.client.Datastore;
27+
import com.google.datastore.v1.client.DatastoreException;
28+
import com.google.datastore.v1.client.DatastoreHelper;
29+
import java.io.IOException;
30+
import java.security.GeneralSecurityException;
31+
import java.util.List;
32+
import org.junit.Assert;
33+
import org.junit.Before;
34+
import org.junit.Test;
35+
36+
public class ITDatastoreProtoClientTest {
37+
38+
private static Datastore DATASTORE;
39+
40+
private static PartitionId PARTITION;
41+
42+
private static final String KIND = "test-kind";
43+
private static final String PROJECT_ID = System.getenv(DatastoreHelper.PROJECT_ID_ENV_VAR);
44+
45+
@Before
46+
public void setUp() throws GeneralSecurityException, IOException {
47+
DATASTORE = DatastoreHelper.getDatastoreFromEnv();
48+
}
49+
50+
@Test
51+
public void testQuerySplitterWithDefaultDb() throws DatastoreException {
52+
Filter propertyFilter =
53+
makeFilter("foo", PropertyFilter.Operator.EQUAL, makeValue("value")).build();
54+
Query query =
55+
Query.newBuilder()
56+
.addKind(KindExpression.newBuilder().setName(KIND).build())
57+
.setFilter(propertyFilter)
58+
.build();
59+
60+
PARTITION = PartitionId.newBuilder().setProjectId(PROJECT_ID).build();
61+
62+
List<Query> splits =
63+
DatastoreHelper.getQuerySplitter().getSplits(query, PARTITION, 2, DATASTORE);
64+
splits.forEach(
65+
split -> {
66+
Assert.assertEquals(KIND, split.getKind(0).getName());
67+
Assert.assertEquals(propertyFilter, split.getFilter());
68+
});
69+
}
70+
71+
@Test
72+
public void testQuerySplitterWithDb() throws DatastoreException {
73+
Filter propertyFilter =
74+
makeFilter("foo", PropertyFilter.Operator.EQUAL, makeValue("value")).build();
75+
Query query =
76+
Query.newBuilder()
77+
.addKind(KindExpression.newBuilder().setName(KIND).build())
78+
.setFilter(propertyFilter)
79+
.build();
80+
81+
PARTITION = PartitionId.newBuilder().setProjectId(PROJECT_ID).setDatabaseId("test-db").build();
82+
83+
List<Query> splits =
84+
DatastoreHelper.getQuerySplitter().getSplits(query, PARTITION, 2, DATASTORE);
85+
splits.forEach(
86+
split -> {
87+
Assert.assertEquals(KIND, split.getKind(0).getName());
88+
Assert.assertEquals(propertyFilter, split.getFilter());
89+
});
90+
}
91+
}

0 commit comments

Comments
 (0)