|
| 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 | +} |
0 commit comments