Skip to content

Commit b9af31f

Browse files
committed
Merge remote-tracking branch 'dpe/master' into repo-migration
2 parents b842b7d + 9f115b7 commit b9af31f

22 files changed

+4132
-0
lines changed

datastore-v1-proto-client/pom.xml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright (C) 2015 Google Inc.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
6+
use this file except in compliance with the License. You may obtain a copy of
7+
the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
License for the specific language governing permissions and limitations under
15+
the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<artifactId>datastore-v1-proto-client</artifactId>
21+
<version>1.6.3</version>
22+
23+
<name>${project.groupId}:${project.artifactId}</name>
24+
25+
<packaging>jar</packaging>
26+
<description>
27+
Low level client for accessing Google Cloud Datastore v1.
28+
</description>
29+
<parent>
30+
<groupId>com.google.cloud.datastore</groupId>
31+
<artifactId>datastore-v1-proto-client-parent</artifactId>
32+
<version>1.6.3</version>
33+
</parent>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>com.google.api.grpc</groupId>
38+
<artifactId>proto-google-cloud-datastore-v1</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>com.google.http-client</groupId>
43+
<artifactId>google-http-client</artifactId>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>com.google.http-client</groupId>
48+
<artifactId>google-http-client-protobuf</artifactId>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>com.google.http-client</groupId>
53+
<artifactId>google-http-client-jackson2</artifactId>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>com.google.oauth-client</groupId>
58+
<artifactId>google-oauth-client</artifactId>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>com.google.api-client</groupId>
63+
<artifactId>google-api-client</artifactId>
64+
</dependency>
65+
66+
<dependency>
67+
<groupId>com.google.guava</groupId>
68+
<artifactId>guava</artifactId>
69+
</dependency>
70+
71+
<!-- Test dependencies. -->
72+
<dependency>
73+
<groupId>junit</groupId>
74+
<artifactId>junit</artifactId>
75+
<scope>test</scope>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>com.google.truth</groupId>
80+
<artifactId>truth</artifactId>
81+
<scope>test</scope>
82+
</dependency>
83+
</dependencies>
84+
85+
<build>
86+
<plugins>
87+
<plugin>
88+
<groupId>org.apache.maven.plugins</groupId>
89+
<artifactId>maven-compiler-plugin</artifactId>
90+
</plugin>
91+
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-source-plugin</artifactId>
95+
</plugin>
96+
97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-javadoc-plugin</artifactId>
100+
</plugin>
101+
102+
<plugin>
103+
<groupId>org.apache.maven.plugins</groupId>
104+
<artifactId>maven-gpg-plugin</artifactId>
105+
</plugin>
106+
</plugins>
107+
</build>
108+
</project>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2021 Google LLC. All Rights Reserved.
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;
17+
18+
import com.google.common.annotations.VisibleForTesting;
19+
import com.google.api.client.http.HttpResponse;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.security.MessageDigest;
23+
24+
/** This class provides End-to-End Checksum API for http protocol. */
25+
class ChecksumEnforcingInputStream extends InputStream {
26+
private final InputStream delegate;
27+
private final MessageDigest messageDigest;
28+
private final String expectedChecksum;
29+
30+
ChecksumEnforcingInputStream(InputStream originalInputStream,
31+
HttpResponse response,
32+
MessageDigest digest) {
33+
this(originalInputStream, EndToEndChecksumHandler.getChecksumHeader(response), digest);
34+
}
35+
36+
@VisibleForTesting
37+
ChecksumEnforcingInputStream(InputStream originalInputStream,
38+
String checksum,
39+
MessageDigest digest) {
40+
delegate = originalInputStream;
41+
expectedChecksum = checksum;
42+
messageDigest = digest;
43+
}
44+
45+
@Override
46+
public int available() throws IOException {
47+
return delegate.available();
48+
}
49+
50+
@Override
51+
public void close() throws IOException {
52+
delegate.close();
53+
}
54+
55+
@Override
56+
public void mark(int readlimit) {
57+
throw new RuntimeException("mark(int) Not Supported");
58+
}
59+
60+
@Override
61+
public boolean markSupported() {
62+
// This class doesn't support mark, reset methods!
63+
return false;
64+
}
65+
66+
@Override
67+
public int read() throws IOException {
68+
throw new RuntimeException("read() Not Supported");
69+
}
70+
71+
@Override
72+
public int read(byte[] b) throws IOException {
73+
throw new RuntimeException("read(byte[]) Not Supported");
74+
}
75+
76+
@Override
77+
public int read(byte[] b, int off, int len) throws IOException {
78+
if (len <= 0) return 0;
79+
int i = delegate.read(b, off, len);
80+
if (i > 0) {
81+
messageDigest.update(b, off, i);
82+
} else {
83+
// no more payload to read. compute checksum and verify
84+
if (!expectedChecksum.equalsIgnoreCase(
85+
com.google.common.io.BaseEncoding.base16().encode(messageDigest.digest()))) {
86+
throw new IOException("possible memory corruption on payload detected");
87+
}
88+
}
89+
return i;
90+
}
91+
92+
@Override
93+
public void reset() throws IOException {
94+
throw new RuntimeException("reset() Not Supported");
95+
}
96+
97+
@Override
98+
public long skip(long n) throws IOException {
99+
if (n <= 0) return 0;
100+
// TODO: handle the case of n > Integer.MAX_VALUE ( that is, n > (2GB - 1). It is highly
101+
// unlikely that callers will want to skip that many bytes. That is the entire payload
102+
if (n > Integer.MAX_VALUE) {
103+
throw new IOException("can't skip more than Integer.MAX bytes");
104+
}
105+
int intSkip = (int) n;
106+
byte[] b = new byte[intSkip];
107+
return read(b, 0, intSkip);
108+
}
109+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
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;
17+
18+
import com.google.datastore.v1.AllocateIdsRequest;
19+
import com.google.datastore.v1.AllocateIdsResponse;
20+
import com.google.datastore.v1.BeginTransactionRequest;
21+
import com.google.datastore.v1.BeginTransactionResponse;
22+
import com.google.datastore.v1.CommitRequest;
23+
import com.google.datastore.v1.CommitResponse;
24+
import com.google.datastore.v1.LookupRequest;
25+
import com.google.datastore.v1.LookupResponse;
26+
import com.google.datastore.v1.ReserveIdsRequest;
27+
import com.google.datastore.v1.ReserveIdsResponse;
28+
import com.google.datastore.v1.RollbackRequest;
29+
import com.google.datastore.v1.RollbackResponse;
30+
import com.google.datastore.v1.RunQueryRequest;
31+
import com.google.datastore.v1.RunQueryResponse;
32+
import com.google.rpc.Code;
33+
import java.io.IOException;
34+
import java.io.InputStream;
35+
36+
/**
37+
* Provides access to Cloud Datastore.
38+
*
39+
* <p>This class is thread-safe.
40+
*/
41+
public class Datastore {
42+
43+
final RemoteRpc remoteRpc;
44+
45+
Datastore(RemoteRpc remoteRpc) {
46+
this.remoteRpc = remoteRpc;
47+
}
48+
49+
/**
50+
* Reset the RPC count.
51+
*/
52+
public void resetRpcCount() {
53+
remoteRpc.resetRpcCount();
54+
}
55+
56+
/**
57+
* Returns the number of RPC calls made since the client was created
58+
* or {@link #resetRpcCount} was called.
59+
*/
60+
public int getRpcCount() {
61+
return remoteRpc.getRpcCount();
62+
}
63+
64+
private DatastoreException invalidResponseException(String method, IOException exception) {
65+
return RemoteRpc.makeException(remoteRpc.getUrl(), method, Code.UNAVAILABLE,
66+
"Invalid response", exception);
67+
}
68+
69+
public AllocateIdsResponse allocateIds(AllocateIdsRequest request) throws DatastoreException {
70+
try (InputStream is = remoteRpc.call("allocateIds", request)) {
71+
return AllocateIdsResponse.parseFrom(is);
72+
} catch (IOException exception) {
73+
throw invalidResponseException("allocateIds", exception);
74+
}
75+
}
76+
77+
public BeginTransactionResponse beginTransaction(BeginTransactionRequest request)
78+
throws DatastoreException {
79+
try (InputStream is = remoteRpc.call("beginTransaction", request)) {
80+
return BeginTransactionResponse.parseFrom(is);
81+
} catch (IOException exception) {
82+
throw invalidResponseException("beginTransaction", exception);
83+
}
84+
}
85+
86+
public CommitResponse commit(CommitRequest request) throws DatastoreException {
87+
try (InputStream is = remoteRpc.call("commit", request)) {
88+
return CommitResponse.parseFrom(is);
89+
} catch (IOException exception) {
90+
throw invalidResponseException("commit", exception);
91+
}
92+
}
93+
94+
public LookupResponse lookup(LookupRequest request) throws DatastoreException {
95+
try (InputStream is = remoteRpc.call("lookup", request)) {
96+
return LookupResponse.parseFrom(is);
97+
} catch (IOException exception) {
98+
throw invalidResponseException("lookup", exception);
99+
}
100+
}
101+
102+
public ReserveIdsResponse reserveIds(ReserveIdsRequest request) throws DatastoreException {
103+
try (InputStream is = remoteRpc.call("reserveIds", request)) {
104+
return ReserveIdsResponse.parseFrom(is);
105+
} catch (IOException exception) {
106+
throw invalidResponseException("reserveIds", exception);
107+
}
108+
}
109+
110+
public RollbackResponse rollback(RollbackRequest request) throws DatastoreException {
111+
try (InputStream is = remoteRpc.call("rollback", request)) {
112+
return RollbackResponse.parseFrom(is);
113+
} catch (IOException exception) {
114+
throw invalidResponseException("rollback", exception);
115+
}
116+
}
117+
118+
public RunQueryResponse runQuery(RunQueryRequest request) throws DatastoreException {
119+
try (InputStream is = remoteRpc.call("runQuery", request)) {
120+
return RunQueryResponse.parseFrom(is);
121+
} catch (IOException exception) {
122+
throw invalidResponseException("runQuery", exception);
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)