|
| 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