Class: Google::Cloud::Datastore::V1::DatastoreClient
- Inherits:
-
Object
- Object
- Google::Cloud::Datastore::V1::DatastoreClient
- Defined in:
- lib/google/cloud/datastore/v1/datastore_client.rb
Overview
Each RPC normalizes the partition IDs of the keys in its input entities, and always returns entities with keys with normalized partition IDs. This applies to all keys and entities, including those in values, except keys with both an empty path and an empty or unset partition ID. Normalization of input keys sets the project ID (if not already set) to the project ID from the request.
Constant Summary collapse
- SERVICE_ADDRESS =
The default address of the service.
"datastore.googleapis.com".freeze
- DEFAULT_SERVICE_PORT =
The default port of the service.
443
- CODE_GEN_NAME_VERSION =
"gapic/0.1.0".freeze
- DEFAULT_TIMEOUT =
30
- ALL_SCOPES =
The scopes needed to make gRPC calls to all of the methods defined in this service.
[ "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/datastore" ].freeze
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#allocate_ids(project_id, keys, options: nil) ⇒ Google::Datastore::V1::AllocateIdsResponse
Allocates IDs for the given keys, which is useful for referencing an entity before it is inserted.
-
#begin_transaction(project_id, options: nil) ⇒ Google::Datastore::V1::BeginTransactionResponse
Begins a new transaction.
-
#commit(project_id, mode, mutations, transaction: nil, options: nil) ⇒ Google::Datastore::V1::CommitResponse
Commits a transaction, optionally creating, deleting or modifying some entities.
-
#initialize(service_path: SERVICE_ADDRESS, port: DEFAULT_SERVICE_PORT, channel: nil, chan_creds: nil, scopes: ALL_SCOPES, client_config: {}, timeout: DEFAULT_TIMEOUT, app_name: "gax", app_version: Google::Gax::VERSION) ⇒ DatastoreClient
constructor
A new instance of DatastoreClient.
-
#lookup(project_id, read_options, keys, options: nil) ⇒ Google::Datastore::V1::LookupResponse
Looks up entities by key.
-
#rollback(project_id, transaction, options: nil) ⇒ Google::Datastore::V1::RollbackResponse
Rolls back a transaction.
-
#run_query(project_id, partition_id, read_options, query: nil, gql_query: nil, options: nil) ⇒ Google::Datastore::V1::RunQueryResponse
Queries for entities.
Constructor Details
#initialize(service_path: SERVICE_ADDRESS, port: DEFAULT_SERVICE_PORT, channel: nil, chan_creds: nil, scopes: ALL_SCOPES, client_config: {}, timeout: DEFAULT_TIMEOUT, app_name: "gax", app_version: Google::Gax::VERSION) ⇒ DatastoreClient
Returns a new instance of DatastoreClient
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/google/cloud/datastore/v1/datastore_client.rb', line 83 def initialize \ service_path: SERVICE_ADDRESS, port: DEFAULT_SERVICE_PORT, channel: nil, chan_creds: nil, scopes: ALL_SCOPES, client_config: {}, timeout: DEFAULT_TIMEOUT, app_name: "gax", app_version: Google::Gax::VERSION # These require statements are intentionally placed here to initialize # the gRPC module only when it's required. # See https://github.com/googleapis/toolkit/issues/446 require "google/gax/grpc" require "google/datastore/v1/datastore_services_pb" google_api_client = "#{app_name}/#{app_version} " \ "#{CODE_GEN_NAME_VERSION} gax/#{Google::Gax::VERSION} " \ "ruby/#{RUBY_VERSION}".freeze headers = { :"x-goog-api-client" => google_api_client } client_config_file = Pathname.new(__dir__).join( "datastore_client_config.json" ) defaults = client_config_file.open do |f| Google::Gax.construct_settings( "google.datastore.v1.Datastore", JSON.parse(f.read), client_config, Google::Gax::Grpc::STATUS_CODE_NAMES, timeout, errors: Google::Gax::Grpc::API_ERRORS, kwargs: headers ) end @datastore_stub = Google::Gax::Grpc.create_stub( service_path, port, chan_creds: chan_creds, channel: channel, scopes: scopes, &Google::Datastore::V1::Datastore::Stub.method(:new) ) @lookup = Google::Gax.create_api_call( @datastore_stub.method(:lookup), defaults["lookup"] ) @run_query = Google::Gax.create_api_call( @datastore_stub.method(:run_query), defaults["run_query"] ) @begin_transaction = Google::Gax.create_api_call( @datastore_stub.method(:begin_transaction), defaults["begin_transaction"] ) @commit = Google::Gax.create_api_call( @datastore_stub.method(:commit), defaults["commit"] ) @rollback = Google::Gax.create_api_call( @datastore_stub.method(:rollback), defaults["rollback"] ) @allocate_ids = Google::Gax.create_api_call( @datastore_stub.method(:allocate_ids), defaults["allocate_ids"] ) end |
Instance Attribute Details
#datastore_stub ⇒ Google::Datastore::V1::Datastore::Stub (readonly)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/google/cloud/datastore/v1/datastore_client.rb', line 44 class DatastoreClient attr_reader :datastore_stub # The default address of the service. SERVICE_ADDRESS = "datastore.googleapis.com".freeze # The default port of the service. DEFAULT_SERVICE_PORT = 443 CODE_GEN_NAME_VERSION = "gapic/0.1.0".freeze DEFAULT_TIMEOUT = 30 # The scopes needed to make gRPC calls to all of the methods defined in # this service. ALL_SCOPES = [ "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/datastore" ].freeze # @param service_path [String] # The domain name of the API remote host. # @param port [Integer] # The port on which to connect to the remote host. # @param channel [Channel] # A Channel object through which to make calls. # @param chan_creds [Grpc::ChannelCredentials] # A ChannelCredentials for the setting up the RPC client. # @param client_config[Hash] # A Hash for call options for each method. See # Google::Gax#construct_settings for the structure of # this data. Falls back to the default config if not specified # or the specified config is missing data points. # @param timeout [Numeric] # The default timeout, in seconds, for calls made through this client. # @param app_name [String] # The codename of the calling service. # @param app_version [String] # The version of the calling service. def initialize \ service_path: SERVICE_ADDRESS, port: DEFAULT_SERVICE_PORT, channel: nil, chan_creds: nil, scopes: ALL_SCOPES, client_config: {}, timeout: DEFAULT_TIMEOUT, app_name: "gax", app_version: Google::Gax::VERSION # These require statements are intentionally placed here to initialize # the gRPC module only when it's required. # See https://github.com/googleapis/toolkit/issues/446 require "google/gax/grpc" require "google/datastore/v1/datastore_services_pb" google_api_client = "#{app_name}/#{app_version} " \ "#{CODE_GEN_NAME_VERSION} gax/#{Google::Gax::VERSION} " \ "ruby/#{RUBY_VERSION}".freeze headers = { :"x-goog-api-client" => google_api_client } client_config_file = Pathname.new(__dir__).join( "datastore_client_config.json" ) defaults = client_config_file.open do |f| Google::Gax.construct_settings( "google.datastore.v1.Datastore", JSON.parse(f.read), client_config, Google::Gax::Grpc::STATUS_CODE_NAMES, timeout, errors: Google::Gax::Grpc::API_ERRORS, kwargs: headers ) end @datastore_stub = Google::Gax::Grpc.create_stub( service_path, port, chan_creds: chan_creds, channel: channel, scopes: scopes, &Google::Datastore::V1::Datastore::Stub.method(:new) ) @lookup = Google::Gax.create_api_call( @datastore_stub.method(:lookup), defaults["lookup"] ) @run_query = Google::Gax.create_api_call( @datastore_stub.method(:run_query), defaults["run_query"] ) @begin_transaction = Google::Gax.create_api_call( @datastore_stub.method(:begin_transaction), defaults["begin_transaction"] ) @commit = Google::Gax.create_api_call( @datastore_stub.method(:commit), defaults["commit"] ) @rollback = Google::Gax.create_api_call( @datastore_stub.method(:rollback), defaults["rollback"] ) @allocate_ids = Google::Gax.create_api_call( @datastore_stub.method(:allocate_ids), defaults["allocate_ids"] ) end # Service calls # Looks up entities by key. # # @param project_id [String] # The ID of the project against which to make the request. # @param read_options [Google::Datastore::V1::ReadOptions] # The options for this lookup request. # @param keys [Array<Google::Datastore::V1::Key>] # Keys of entities to look up. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Datastore::V1::LookupResponse] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/datastore/v1/datastore_client" # # DatastoreClient = Google::Cloud::Datastore::V1::DatastoreClient # ReadOptions = Google::Datastore::V1::ReadOptions # # datastore_client = DatastoreClient.new # project_id = '' # read_options = ReadOptions.new # keys = [] # response = datastore_client.lookup(project_id, read_options, keys) def lookup \ project_id, , keys, options: nil req = Google::Datastore::V1::LookupRequest.new({ project_id: project_id, read_options: , keys: keys }.delete_if { |_, v| v.nil? }) @lookup.call(req, ) end # Queries for entities. # # @param project_id [String] # The ID of the project against which to make the request. # @param partition_id [Google::Datastore::V1::PartitionId] # Entities are partitioned into subsets, identified by a partition ID. # Queries are scoped to a single partition. # This partition ID is normalized with the standard default context # partition ID. # @param read_options [Google::Datastore::V1::ReadOptions] # The options for this query. # @param query [Google::Datastore::V1::Query] # The query to run. # @param gql_query [Google::Datastore::V1::GqlQuery] # The GQL query to run. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Datastore::V1::RunQueryResponse] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/datastore/v1/datastore_client" # # DatastoreClient = Google::Cloud::Datastore::V1::DatastoreClient # PartitionId = Google::Datastore::V1::PartitionId # ReadOptions = Google::Datastore::V1::ReadOptions # # datastore_client = DatastoreClient.new # project_id = '' # partition_id = PartitionId.new # read_options = ReadOptions.new # response = datastore_client.run_query(project_id, partition_id, read_options) def run_query \ project_id, partition_id, , query: nil, gql_query: nil, options: nil req = Google::Datastore::V1::RunQueryRequest.new({ project_id: project_id, partition_id: partition_id, read_options: , query: query, gql_query: gql_query }.delete_if { |_, v| v.nil? }) @run_query.call(req, ) end # Begins a new transaction. # # @param project_id [String] # The ID of the project against which to make the request. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Datastore::V1::BeginTransactionResponse] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/datastore/v1/datastore_client" # # DatastoreClient = Google::Cloud::Datastore::V1::DatastoreClient # # datastore_client = DatastoreClient.new # project_id = '' # response = datastore_client.begin_transaction(project_id) def begin_transaction \ project_id, options: nil req = Google::Datastore::V1::BeginTransactionRequest.new({ project_id: project_id }.delete_if { |_, v| v.nil? }) @begin_transaction.call(req, ) end # Commits a transaction, optionally creating, deleting or modifying some # entities. # # @param project_id [String] # The ID of the project against which to make the request. # @param mode [Google::Datastore::V1::CommitRequest::Mode] # The type of commit to perform. Defaults to +TRANSACTIONAL+. # @param transaction [String] # The identifier of the transaction associated with the commit. A # transaction identifier is returned by a call to # Datastore::BeginTransaction. # @param mutations [Array<Google::Datastore::V1::Mutation>] # The mutations to perform. # # When mode is +TRANSACTIONAL+, mutations affecting a single entity are # applied in order. The following sequences of mutations affecting a single # entity are not permitted in a single +Commit+ request: # # - +insert+ followed by +insert+ # - +update+ followed by +insert+ # - +upsert+ followed by +insert+ # - +delete+ followed by +update+ # # When mode is +NON_TRANSACTIONAL+, no two mutations may affect a single # entity. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Datastore::V1::CommitResponse] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/datastore/v1/datastore_client" # # DatastoreClient = Google::Cloud::Datastore::V1::DatastoreClient # Mode = Google::Datastore::V1::CommitRequest::Mode # # datastore_client = DatastoreClient.new # project_id = '' # mode = Mode::MODE_UNSPECIFIED # mutations = [] # response = datastore_client.commit(project_id, mode, mutations) def commit \ project_id, mode, mutations, transaction: nil, options: nil req = Google::Datastore::V1::CommitRequest.new({ project_id: project_id, mode: mode, mutations: mutations, transaction: transaction }.delete_if { |_, v| v.nil? }) @commit.call(req, ) end # Rolls back a transaction. # # @param project_id [String] # The ID of the project against which to make the request. # @param transaction [String] # The transaction identifier, returned by a call to # Datastore::BeginTransaction. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Datastore::V1::RollbackResponse] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/datastore/v1/datastore_client" # # DatastoreClient = Google::Cloud::Datastore::V1::DatastoreClient # # datastore_client = DatastoreClient.new # project_id = '' # transaction = '' # response = datastore_client.rollback(project_id, transaction) def rollback \ project_id, transaction, options: nil req = Google::Datastore::V1::RollbackRequest.new({ project_id: project_id, transaction: transaction }.delete_if { |_, v| v.nil? }) @rollback.call(req, ) end # Allocates IDs for the given keys, which is useful for referencing an entity # before it is inserted. # # @param project_id [String] # The ID of the project against which to make the request. # @param keys [Array<Google::Datastore::V1::Key>] # A list of keys with incomplete key paths for which to allocate IDs. # No key may be reserved/read-only. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Datastore::V1::AllocateIdsResponse] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/datastore/v1/datastore_client" # # DatastoreClient = Google::Cloud::Datastore::V1::DatastoreClient # # datastore_client = DatastoreClient.new # project_id = '' # keys = [] # response = datastore_client.allocate_ids(project_id, keys) def allocate_ids \ project_id, keys, options: nil req = Google::Datastore::V1::AllocateIdsRequest.new({ project_id: project_id, keys: keys }.delete_if { |_, v| v.nil? }) @allocate_ids.call(req, ) end end |
Instance Method Details
#allocate_ids(project_id, keys, options: nil) ⇒ Google::Datastore::V1::AllocateIdsResponse
Allocates IDs for the given keys, which is useful for referencing an entity before it is inserted.
382 383 384 385 386 387 388 389 390 391 |
# File 'lib/google/cloud/datastore/v1/datastore_client.rb', line 382 def allocate_ids \ project_id, keys, options: nil req = Google::Datastore::V1::AllocateIdsRequest.new({ project_id: project_id, keys: keys }.delete_if { |_, v| v.nil? }) @allocate_ids.call(req, ) end |
#begin_transaction(project_id, options: nil) ⇒ Google::Datastore::V1::BeginTransactionResponse
Begins a new transaction.
260 261 262 263 264 265 266 267 |
# File 'lib/google/cloud/datastore/v1/datastore_client.rb', line 260 def begin_transaction \ project_id, options: nil req = Google::Datastore::V1::BeginTransactionRequest.new({ project_id: project_id }.delete_if { |_, v| v.nil? }) @begin_transaction.call(req, ) end |
#commit(project_id, mode, mutations, transaction: nil, options: nil) ⇒ Google::Datastore::V1::CommitResponse
Commits a transaction, optionally creating, deleting or modifying some entities.
311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/google/cloud/datastore/v1/datastore_client.rb', line 311 def commit \ project_id, mode, mutations, transaction: nil, options: nil req = Google::Datastore::V1::CommitRequest.new({ project_id: project_id, mode: mode, mutations: mutations, transaction: transaction }.delete_if { |_, v| v.nil? }) @commit.call(req, ) end |
#lookup(project_id, read_options, keys, options: nil) ⇒ Google::Datastore::V1::LookupResponse
Looks up entities by key.
179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/google/cloud/datastore/v1/datastore_client.rb', line 179 def lookup \ project_id, , keys, options: nil req = Google::Datastore::V1::LookupRequest.new({ project_id: project_id, read_options: , keys: keys }.delete_if { |_, v| v.nil? }) @lookup.call(req, ) end |
#rollback(project_id, transaction, options: nil) ⇒ Google::Datastore::V1::RollbackResponse
Rolls back a transaction.
348 349 350 351 352 353 354 355 356 357 |
# File 'lib/google/cloud/datastore/v1/datastore_client.rb', line 348 def rollback \ project_id, transaction, options: nil req = Google::Datastore::V1::RollbackRequest.new({ project_id: project_id, transaction: transaction }.delete_if { |_, v| v.nil? }) @rollback.call(req, ) end |
#run_query(project_id, partition_id, read_options, query: nil, gql_query: nil, options: nil) ⇒ Google::Datastore::V1::RunQueryResponse
Queries for entities.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/google/cloud/datastore/v1/datastore_client.rb', line 225 def run_query \ project_id, partition_id, , query: nil, gql_query: nil, options: nil req = Google::Datastore::V1::RunQueryRequest.new({ project_id: project_id, partition_id: partition_id, read_options: , query: query, gql_query: gql_query }.delete_if { |_, v| v.nil? }) @run_query.call(req, ) end |