Skip to content

Commit 4a18ad2

Browse files
authored
Refactor: refactor grpc query splitter logic (#1674)
* Init commit to migrate query splitter * Add query splitter and datastore factory * fix unused dependencies * Fix formatting * Fix copyright * Add initial tests and utilities * Fix dependencies for datastore parent version * Add unit tests * fix non-complying * Fix non-complying errors * Skip native tests
1 parent c772f3e commit 4a18ad2

19 files changed

+3757
-1
lines changed

.readme-partials.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ custom_content: |
226226
<dependency>
227227
<groupId>com.google.cloud</groupId>
228228
<artifactId>google-cloud-datastore</artifactId>
229-
<version>2.20.0-grpc-experimental-1-SNAPSHOT</version>
229+
<version>2.22.0-grpc-experimental-1-SNAPSHOT</version>
230230
</dependency>
231231
```
232232

google-cloud-datastore-utils/pom.xml

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.google.cloud</groupId>
5+
<artifactId>google-cloud-datastore-utils</artifactId>
6+
<version>2.22.0-grpc-experimental-1-SNAPSHOT</version><!-- {x-version-update:google-cloud-datastore:current} -->
7+
<packaging>jar</packaging>
8+
<name>Google Cloud Datastore Utilities</name>
9+
<url>https://github.com/googleapis/java-datastore</url>
10+
<description>
11+
Java datastore client utility library.
12+
</description>
13+
<parent>
14+
<groupId>com.google.cloud</groupId>
15+
<artifactId>google-cloud-datastore-parent</artifactId>
16+
<version>2.22.0-grpc-experimental-1-SNAPSHOT</version><!-- {x-version-update:google-cloud-datastore:current} -->
17+
</parent>
18+
<properties>
19+
<site.installationModule>google-cloud-datastore-utils</site.installationModule>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.google.api-client</groupId>
24+
<artifactId>google-api-client</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.google.http-client</groupId>
28+
<artifactId>google-http-client-protobuf</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>com.google.http-client</groupId>
32+
<artifactId>google-http-client-gson</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.google.api.grpc</groupId>
36+
<artifactId>proto-google-cloud-datastore-v1</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.google.api</groupId>
40+
<artifactId>api-common</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.google.protobuf</groupId>
44+
<artifactId>protobuf-java</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.google.guava</groupId>
48+
<artifactId>guava</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.api.grpc</groupId>
52+
<artifactId>proto-google-common-protos</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.google.http-client</groupId>
56+
<artifactId>google-http-client</artifactId>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.google.http-client</groupId>
60+
<artifactId>google-http-client-jackson2</artifactId>
61+
</dependency>
62+
<dependency>
63+
<groupId>com.google.oauth-client</groupId>
64+
<artifactId>google-oauth-client</artifactId>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.google.code.findbugs</groupId>
68+
<artifactId>jsr305</artifactId>
69+
</dependency>
70+
<!-- Test dependencies -->
71+
<dependency>
72+
<groupId>junit</groupId>
73+
<artifactId>junit</artifactId>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>com.google.truth</groupId>
78+
<artifactId>truth</artifactId>
79+
<version>1.4.2</version>
80+
<scope>test</scope>
81+
<exclusions>
82+
<exclusion>
83+
<groupId>org.checkerframework</groupId>
84+
<artifactId>checker-qual</artifactId>
85+
</exclusion>
86+
</exclusions>
87+
</dependency>
88+
</dependencies>
89+
90+
<build>
91+
<plugins>
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-surefire-plugin</artifactId>
95+
<configuration>
96+
<argLine>-Xmx2048m</argLine>
97+
</configuration>
98+
</plugin>
99+
</plugins>
100+
</build>
101+
102+
<profiles>
103+
<profile>
104+
<id>native</id>
105+
<properties>
106+
<!-- skip native tests for this module, it will be tested in google-cloud-datastore-->
107+
<skipTests>true</skipTests>
108+
</properties>
109+
</profile>
110+
</profiles>
111+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
* 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.utils;
17+
18+
import com.google.datastore.v1.*;
19+
import com.google.rpc.Code;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
23+
/**
24+
* Provides access to Cloud Datastore.
25+
*
26+
* <p>This class is thread-safe.
27+
*/
28+
public class Datastore {
29+
30+
final RemoteRpc remoteRpc;
31+
32+
Datastore(RemoteRpc remoteRpc) {
33+
this.remoteRpc = remoteRpc;
34+
}
35+
36+
/** Reset the RPC count. */
37+
public void resetRpcCount() {
38+
remoteRpc.resetRpcCount();
39+
}
40+
41+
/**
42+
* Returns the number of RPC calls made since the client was created or {@link #resetRpcCount} was
43+
* called.
44+
*/
45+
public int getRpcCount() {
46+
return remoteRpc.getRpcCount();
47+
}
48+
49+
private com.google.datastore.utils.DatastoreException invalidResponseException(
50+
String method, IOException exception) {
51+
return RemoteRpc.makeException(
52+
remoteRpc.getUrl(), method, Code.UNAVAILABLE, "Invalid response", exception);
53+
}
54+
55+
public AllocateIdsResponse allocateIds(AllocateIdsRequest request)
56+
throws com.google.datastore.utils.DatastoreException {
57+
try (InputStream is =
58+
remoteRpc.call("allocateIds", request, request.getProjectId(), request.getDatabaseId())) {
59+
return AllocateIdsResponse.parseFrom(is);
60+
} catch (IOException exception) {
61+
throw invalidResponseException("allocateIds", exception);
62+
}
63+
}
64+
65+
public BeginTransactionResponse beginTransaction(BeginTransactionRequest request)
66+
throws com.google.datastore.utils.DatastoreException {
67+
try (InputStream is =
68+
remoteRpc.call(
69+
"beginTransaction", request, request.getProjectId(), request.getDatabaseId())) {
70+
return BeginTransactionResponse.parseFrom(is);
71+
} catch (IOException exception) {
72+
throw invalidResponseException("beginTransaction", exception);
73+
}
74+
}
75+
76+
public CommitResponse commit(CommitRequest request)
77+
throws com.google.datastore.utils.DatastoreException {
78+
try (InputStream is =
79+
remoteRpc.call("commit", request, request.getProjectId(), request.getDatabaseId())) {
80+
return CommitResponse.parseFrom(is);
81+
} catch (IOException exception) {
82+
throw invalidResponseException("commit", exception);
83+
}
84+
}
85+
86+
public LookupResponse lookup(LookupRequest request)
87+
throws com.google.datastore.utils.DatastoreException {
88+
try (InputStream is =
89+
remoteRpc.call("lookup", request, request.getProjectId(), request.getDatabaseId())) {
90+
return LookupResponse.parseFrom(is);
91+
} catch (IOException exception) {
92+
throw invalidResponseException("lookup", exception);
93+
}
94+
}
95+
96+
public ReserveIdsResponse reserveIds(ReserveIdsRequest request)
97+
throws com.google.datastore.utils.DatastoreException {
98+
try (InputStream is =
99+
remoteRpc.call("reserveIds", request, request.getProjectId(), request.getDatabaseId())) {
100+
return ReserveIdsResponse.parseFrom(is);
101+
} catch (IOException exception) {
102+
throw invalidResponseException("reserveIds", exception);
103+
}
104+
}
105+
106+
public RollbackResponse rollback(RollbackRequest request)
107+
throws com.google.datastore.utils.DatastoreException {
108+
try (InputStream is =
109+
remoteRpc.call("rollback", request, request.getProjectId(), request.getDatabaseId())) {
110+
return RollbackResponse.parseFrom(is);
111+
} catch (IOException exception) {
112+
throw invalidResponseException("rollback", exception);
113+
}
114+
}
115+
116+
public RunQueryResponse runQuery(RunQueryRequest request)
117+
throws com.google.datastore.utils.DatastoreException {
118+
try (InputStream is =
119+
remoteRpc.call("runQuery", request, request.getProjectId(), request.getDatabaseId())) {
120+
return RunQueryResponse.parseFrom(is);
121+
} catch (IOException exception) {
122+
throw invalidResponseException("runQuery", exception);
123+
}
124+
}
125+
126+
public RunAggregationQueryResponse runAggregationQuery(RunAggregationQueryRequest request)
127+
throws DatastoreException {
128+
try (InputStream is =
129+
remoteRpc.call(
130+
"runAggregationQuery", request, request.getProjectId(), request.getDatabaseId())) {
131+
return RunAggregationQueryResponse.parseFrom(is);
132+
} catch (IOException exception) {
133+
throw invalidResponseException("runAggregationQuery", exception);
134+
}
135+
}
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
* 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.utils;
17+
18+
import com.google.rpc.Code;
19+
20+
/** Indicates an error in a {@link Datastore} call. */
21+
public class DatastoreException extends Exception {
22+
private final String methodName;
23+
private final Code code;
24+
25+
public DatastoreException(String methodName, Code code, String message, Throwable cause) {
26+
super(message, cause);
27+
this.methodName = methodName;
28+
this.code = code;
29+
}
30+
31+
/** @return the canonical error code */
32+
public Code getCode() {
33+
return code;
34+
}
35+
36+
/** @return the datastore method name */
37+
public String getMethodName() {
38+
return methodName;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return String.format("%s, code=%s", super.toString(), code);
44+
}
45+
}

0 commit comments

Comments
 (0)