google-cloud-spanner 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/lib/google-cloud-spanner.rb +106 -0
  3. data/lib/google/cloud/spanner.rb +382 -0
  4. data/lib/google/cloud/spanner/admin/database/v1.rb +17 -0
  5. data/lib/google/cloud/spanner/admin/database/v1/database_admin_client.rb +703 -0
  6. data/lib/google/cloud/spanner/admin/database/v1/database_admin_client_config.json +73 -0
  7. data/lib/google/cloud/spanner/admin/database/v1/doc/google/iam/v1/policy.rb +139 -0
  8. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/any.rb +114 -0
  9. data/lib/google/cloud/spanner/admin/database/v1/doc/google/rpc/status.rb +83 -0
  10. data/lib/google/cloud/spanner/admin/database/v1/doc/google/spanner/admin/database/v1/spanner_database_admin.rb +188 -0
  11. data/lib/google/cloud/spanner/admin/instance/v1.rb +17 -0
  12. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/iam/v1/policy.rb +139 -0
  13. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/any.rb +114 -0
  14. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/field_mask.rb +223 -0
  15. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/rpc/status.rb +83 -0
  16. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/spanner/admin/instance/v1/spanner_instance_admin.rb +268 -0
  17. data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client.rb +868 -0
  18. data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client_config.json +78 -0
  19. data/lib/google/cloud/spanner/client.rb +1034 -0
  20. data/lib/google/cloud/spanner/commit.rb +351 -0
  21. data/lib/google/cloud/spanner/convert.rb +311 -0
  22. data/lib/google/cloud/spanner/credentials.rb +32 -0
  23. data/lib/google/cloud/spanner/data.rb +199 -0
  24. data/lib/google/cloud/spanner/database.rb +377 -0
  25. data/lib/google/cloud/spanner/database/job.rb +179 -0
  26. data/lib/google/cloud/spanner/database/list.rb +171 -0
  27. data/lib/google/cloud/spanner/errors.rb +73 -0
  28. data/lib/google/cloud/spanner/fields.rb +252 -0
  29. data/lib/google/cloud/spanner/instance.rb +472 -0
  30. data/lib/google/cloud/spanner/instance/config.rb +99 -0
  31. data/lib/google/cloud/spanner/instance/config/list.rb +171 -0
  32. data/lib/google/cloud/spanner/instance/job.rb +197 -0
  33. data/lib/google/cloud/spanner/instance/list.rb +167 -0
  34. data/lib/google/cloud/spanner/policy.rb +201 -0
  35. data/lib/google/cloud/spanner/pool.rb +279 -0
  36. data/lib/google/cloud/spanner/project.rb +480 -0
  37. data/lib/google/cloud/spanner/range.rb +99 -0
  38. data/lib/google/cloud/spanner/results.rb +280 -0
  39. data/lib/google/cloud/spanner/service.rb +458 -0
  40. data/lib/google/cloud/spanner/session.rb +565 -0
  41. data/lib/google/cloud/spanner/snapshot.rb +260 -0
  42. data/lib/google/cloud/spanner/transaction.rb +533 -0
  43. data/lib/google/cloud/spanner/v1.rb +17 -0
  44. data/lib/google/cloud/spanner/v1/doc/google/protobuf/duration.rb +77 -0
  45. data/lib/google/cloud/spanner/v1/doc/google/protobuf/struct.rb +73 -0
  46. data/lib/google/cloud/spanner/v1/doc/google/protobuf/timestamp.rb +81 -0
  47. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/keys.rb +148 -0
  48. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/mutation.rb +80 -0
  49. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/query_plan.rb +120 -0
  50. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/result_set.rb +175 -0
  51. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/spanner.rb +206 -0
  52. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/transaction.rb +351 -0
  53. data/lib/google/cloud/spanner/v1/spanner_client.rb +850 -0
  54. data/lib/google/cloud/spanner/v1/spanner_client_config.json +78 -0
  55. data/lib/google/cloud/spanner/version.rb +22 -0
  56. data/lib/google/spanner/admin/database/v1/spanner_database_admin_pb.rb +85 -0
  57. data/lib/google/spanner/admin/database/v1/spanner_database_admin_services_pb.rb +95 -0
  58. data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_pb.rb +106 -0
  59. data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_services_pb.rb +180 -0
  60. data/lib/google/spanner/v1/keys_pb.rb +33 -0
  61. data/lib/google/spanner/v1/mutation_pb.rb +38 -0
  62. data/lib/google/spanner/v1/query_plan_pb.rb +47 -0
  63. data/lib/google/spanner/v1/result_set_pb.rb +43 -0
  64. data/lib/google/spanner/v1/spanner_pb.rb +90 -0
  65. data/lib/google/spanner/v1/spanner_services_pb.rb +131 -0
  66. data/lib/google/spanner/v1/transaction_pb.rb +51 -0
  67. data/lib/google/spanner/v1/type_pb.rb +43 -0
  68. metadata +309 -0
@@ -0,0 +1,480 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "google/cloud/spanner/errors"
17
+ require "google/cloud/env"
18
+ require "google/cloud/spanner/service"
19
+ require "google/cloud/spanner/client"
20
+ require "google/cloud/spanner/instance"
21
+ require "google/cloud/spanner/database"
22
+ require "google/cloud/spanner/range"
23
+
24
+ module Google
25
+ module Cloud
26
+ module Spanner
27
+ ##
28
+ # # Project
29
+ #
30
+ # Projects are top-level containers in Google Cloud Platform. They store
31
+ # information about billing and authorized users, and they contain
32
+ # Cloud Spanner data. Each project has a friendly name and a unique ID.
33
+ #
34
+ # Google::Cloud::Spanner::Project is the main object for interacting with
35
+ # Cloud Spanner.
36
+ #
37
+ # {Google::Cloud::Spanner::Instance} and
38
+ # {Google::Cloud::Spanner::Database} objects are created,
39
+ # accessed, and managed by Google::Cloud::Spanner::Project.
40
+ #
41
+ # A {Google::Cloud::Spanner::Client} obtained from a project can be used
42
+ # to read and/or modify data in a Cloud Spanner database.
43
+ #
44
+ # See {Google::Cloud::Spanner.new} and {Google::Cloud#spanner}.
45
+ #
46
+ # @example Obtaining an instance and a database from a project.
47
+ # require "google/cloud"
48
+ #
49
+ # spanner = Google::Cloud::Spanner.new
50
+ # instance = spanner.instance "my-instance"
51
+ # database = instance.database "my-database"
52
+ #
53
+ # @example Obtaining a client for use with a database.
54
+ # require "google/cloud/spanner"
55
+ #
56
+ # spanner = Google::Cloud::Spanner.new
57
+ #
58
+ # db = spanner.client "my-instance", "my-database"
59
+ #
60
+ # db.transaction do |tx|
61
+ # results = tx.execute "SELECT * FROM users"
62
+ #
63
+ # results.rows.each do |row|
64
+ # puts "User #{row[:id]} is #{row[:name]}"
65
+ # end
66
+ # end
67
+ #
68
+ class Project
69
+ ##
70
+ # @private The Service object.
71
+ attr_accessor :service
72
+
73
+ ##
74
+ # @private Creates a new Spanner Project instance.
75
+ def initialize service
76
+ @service = service
77
+ end
78
+
79
+ ##
80
+ # The identifier for the Cloud Spanner project.
81
+ #
82
+ # @example
83
+ # require "google/cloud"
84
+ #
85
+ # spanner = Google::Cloud::Spanner.new(
86
+ # project: "my-project-id",
87
+ # keyfile: "/path/to/keyfile.json"
88
+ # )
89
+ #
90
+ # spanner.project #=> "my-project-id"
91
+ #
92
+ def project
93
+ service.project
94
+ end
95
+ alias_method :project_id, :project
96
+
97
+ ##
98
+ # @private Default project.
99
+ def self.default_project
100
+ ENV["SPANNER_PROJECT"] ||
101
+ ENV["GOOGLE_CLOUD_PROJECT"] ||
102
+ ENV["GCLOUD_PROJECT"] ||
103
+ Google::Cloud.env.project_id
104
+ end
105
+
106
+ ##
107
+ # Retrieves the list of Cloud Spanner instances for the project.
108
+ #
109
+ # @param [String] token The `token` value returned by the last call to
110
+ # `instances`; indicates that this is a continuation of a call,
111
+ # and that the system should return the next page of data.
112
+ # @param [Integer] max Maximum number of instances to return.
113
+ #
114
+ # @return [Array<Google::Cloud::Spanner::Instance>] The list of
115
+ # instances. (See {Google::Cloud::Spanner::Instance::List})
116
+ #
117
+ # @example
118
+ # require "google/cloud/spanner"
119
+ #
120
+ # spanner = Google::Cloud::Spanner.new
121
+ #
122
+ # instances = spanner.instances
123
+ # instances.each do |instance|
124
+ # puts instance.instance_id
125
+ # end
126
+ #
127
+ # @example Retrieve all: (See {Instance::Config::List::List#all})
128
+ # require "google/cloud/spanner"
129
+ #
130
+ # spanner = Google::Cloud::Spanner.new
131
+ #
132
+ # instances = spanner.instances
133
+ # instances.all do |instance|
134
+ # puts instance.instance_id
135
+ # end
136
+ #
137
+ def instances token: nil, max: nil
138
+ ensure_service!
139
+ grpc = service.list_instances token: token, max: max
140
+ Instance::List.from_grpc grpc, service, max
141
+ end
142
+
143
+ ##
144
+ # Retrieves a Cloud Spanner instance by unique identifier.
145
+ #
146
+ # @param [String] instance_id The unique identifier for the instance.
147
+ #
148
+ # @return [Google::Cloud::Spanner::Instance, nil] The instance, or `nil`
149
+ # if the instance does not exist.
150
+ #
151
+ # @example
152
+ # require "google/cloud/spanner"
153
+ #
154
+ # spanner = Google::Cloud::Spanner.new
155
+ # instance = spanner.instance "my-instance"
156
+ #
157
+ # @example Will return `nil` if instance does not exist.
158
+ # require "google/cloud/spanner"
159
+ #
160
+ # spanner = Google::Cloud::Spanner.new
161
+ # instance = spanner.instance "non-existing" #=> nil
162
+ #
163
+ def instance instance_id
164
+ ensure_service!
165
+ grpc = service.get_instance instance_id
166
+ Instance.from_grpc grpc, service
167
+ rescue Google::Cloud::NotFoundError
168
+ nil
169
+ end
170
+
171
+ ##
172
+ # Creates a Cloud Spanner instance and starts preparing it to begin
173
+ # serving.
174
+ #
175
+ # See {Instance::Job}.
176
+ #
177
+ # @param [String] instance_id The unique identifier for the instance,
178
+ # which cannot be changed after the instance is created. Values are of
179
+ # the form `[a-z][-a-z0-9]*[a-z0-9]` and must be between 6 and 30
180
+ # characters in length. Required.
181
+ # @param [String] name The descriptive name for this instance as it
182
+ # appears in UIs. Must be unique per project and between 4 and 30
183
+ # characters in length. Required.
184
+ # @param [String, Instance::Config] config The name of the instance's
185
+ # configuration. Values can be the `instance_config_id`, the full
186
+ # path, or an {Instance::Config} object. Required.
187
+ # @param [Integer] nodes The number of nodes allocated to this instance.
188
+ # Required.
189
+ # @param [Hash] labels Cloud Labels are a flexible and lightweight
190
+ # mechanism for organizing cloud resources into groups that reflect a
191
+ # customer's organizational needs and deployment strategies. Cloud
192
+ # Labels can be used to filter collections of resources. They can be
193
+ # used to control how resource metrics are aggregated. And they can be
194
+ # used as arguments to policy management rules (e.g. route, firewall,
195
+ # load balancing, etc.).
196
+ #
197
+ # * Label keys must be between 1 and 63 characters long and must
198
+ # conform to the following regular expression:
199
+ # `[a-z]([-a-z0-9]*[a-z0-9])?`.
200
+ # * Label values must be between 0 and 63 characters long and must
201
+ # conform to the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
202
+ # * No more than 64 labels can be associated with a given resource.
203
+ #
204
+ # @return [Instance::Job] The job representing the long-running,
205
+ # asynchronous processing of an instance create operation.
206
+ #
207
+ # @example
208
+ # require "google/cloud/spanner"
209
+ #
210
+ # spanner = Google::Cloud::Spanner.new
211
+ #
212
+ # job = spanner.create_instance "my-new-instance",
213
+ # name: "My New Instance",
214
+ # config: "regional-us-central1",
215
+ # nodes: 5,
216
+ # labels: { production: :env }
217
+ #
218
+ # job.done? #=> false
219
+ # job.reload! # API call
220
+ # job.done? #=> true
221
+ # instance = job.instance
222
+ #
223
+ def create_instance instance_id, name: nil, config: nil, nodes: nil,
224
+ labels: nil
225
+ config = config.path if config.respond_to? :path
226
+ grpc = service.create_instance \
227
+ instance_id, name: name, config: config, nodes: nodes,
228
+ labels: labels
229
+ Instance::Job.from_grpc grpc, service
230
+ end
231
+
232
+ ##
233
+ # Retrieves the list of instance configurations for the project.
234
+ #
235
+ # @param [String] token The `token` value returned by the last call to
236
+ # `instance_configs`; indicates that this is a continuation of a call,
237
+ # and that the system should return the next page of data.
238
+ # @param [Integer] max Maximum number of instance configs to return.
239
+ #
240
+ # @return [Array<Google::Cloud::Spanner::Instance::Config>] The list of
241
+ # instance configurations. (See
242
+ # {Google::Cloud::Spanner::Instance::Config::List})
243
+ #
244
+ # @example
245
+ # require "google/cloud/spanner"
246
+ #
247
+ # spanner = Google::Cloud::Spanner.new
248
+ #
249
+ # instance_configs = spanner.instance_configs
250
+ # instance_configs.each do |config|
251
+ # puts config.instance_config_id
252
+ # end
253
+ #
254
+ # @example Retrieve all: (See {Instance::Config::List::List#all})
255
+ # require "google/cloud/spanner"
256
+ #
257
+ # spanner = Google::Cloud::Spanner.new
258
+ #
259
+ # instance_configs = spanner.instance_configs
260
+ # instance_configs.all do |config|
261
+ # puts config.instance_config_id
262
+ # end
263
+ #
264
+ def instance_configs token: nil, max: nil
265
+ ensure_service!
266
+ grpc = service.list_instance_configs token: token, max: max
267
+ Instance::Config::List.from_grpc grpc, service, max
268
+ end
269
+
270
+ ##
271
+ # Retrieves an instance configuration by unique identifier.
272
+ #
273
+ # @param [String] instance_config_id The instance configuration
274
+ # identifier. Values can be the `instance_config_id`, or the full
275
+ # path.
276
+ #
277
+ # @return [Google::Cloud::Spanner::Instance::Config, nil] The instance
278
+ # configuration, or `nil` if the instance configuration does not
279
+ # exist.
280
+ #
281
+ # @example
282
+ # require "google/cloud/spanner"
283
+ #
284
+ # spanner = Google::Cloud::Spanner.new
285
+ # config = spanner.instance_config "regional-us-central1"
286
+ #
287
+ # @example Will return `nil` if instance config does not exist.
288
+ # require "google/cloud/spanner"
289
+ #
290
+ # spanner = Google::Cloud::Spanner.new
291
+ # config = spanner.instance_config "non-existing" #=> nil
292
+ #
293
+ def instance_config instance_config_id
294
+ ensure_service!
295
+ grpc = service.get_instance_config instance_config_id
296
+ Instance::Config.from_grpc grpc
297
+ rescue Google::Cloud::NotFoundError
298
+ nil
299
+ end
300
+
301
+ ##
302
+ # Retrieves the list of databases for the project.
303
+ #
304
+ # @param [String] instance_id The unique identifier for the instance.
305
+ # @param [String] token The `token` value returned by the last call to
306
+ # `databases`; indicates that this is a continuation of a call,
307
+ # and that the system should return the next page of data.
308
+ # @param [Integer] max Maximum number of databases to return.
309
+ #
310
+ # @return [Array<Google::Cloud::Spanner::Database>] The list of
311
+ # databases. (See {Google::Cloud::Spanner::Database::List})
312
+ #
313
+ # @example
314
+ # require "google/cloud/spanner"
315
+ #
316
+ # spanner = Google::Cloud::Spanner.new
317
+ #
318
+ # databases = spanner.databases "my-instance"
319
+ # databases.each do |database|
320
+ # puts database.database_id
321
+ # end
322
+ #
323
+ # @example Retrieve all: (See {Instance::Config::List::List#all})
324
+ # require "google/cloud/spanner"
325
+ #
326
+ # spanner = Google::Cloud::Spanner.new
327
+ #
328
+ # databases = spanner.databases "my-instance"
329
+ # databases.all do |database|
330
+ # puts database.database_id
331
+ # end
332
+ #
333
+ def databases instance_id, token: nil, max: nil
334
+ ensure_service!
335
+ grpc = service.list_databases instance_id, token: token, max: max
336
+ Database::List.from_grpc grpc, service, instance_id, max
337
+ end
338
+
339
+ ##
340
+ # Retrieves a database by unique identifier.
341
+ #
342
+ # @param [String] instance_id The unique identifier for the instance.
343
+ # @param [String] database_id The unique identifier for the database.
344
+ #
345
+ # @return [Google::Cloud::Spanner::Database, nil] The database, or `nil`
346
+ # if the database does not exist.
347
+ #
348
+ # @example
349
+ # require "google/cloud/spanner"
350
+ #
351
+ # spanner = Google::Cloud::Spanner.new
352
+ # database = spanner.database "my-instance", "my-database"
353
+ #
354
+ # @example Will return `nil` if instance does not exist.
355
+ # require "google/cloud/spanner"
356
+ #
357
+ # spanner = Google::Cloud::Spanner.new
358
+ # database = spanner.database "my-instance", "my-database" #=> nil
359
+ #
360
+ def database instance_id, database_id
361
+ ensure_service!
362
+ grpc = service.get_database instance_id, database_id
363
+ Database.from_grpc grpc, service
364
+ rescue Google::Cloud::NotFoundError
365
+ nil
366
+ end
367
+
368
+ ##
369
+ # Creates a database and starts preparing it to begin serving.
370
+ #
371
+ # See {Database::Job}.
372
+ #
373
+ # @param [String] instance_id The unique identifier for the instance.
374
+ # Required.
375
+ # @param [String] database_id The unique identifier for the database,
376
+ # which cannot be changed after the database is created. Values are of
377
+ # the form `[a-z][a-z0-9_\-]*[a-z0-9]` and must be between 2 and 30
378
+ # characters in length. Required.
379
+ # @param [Array<String>] statements DDL statements to run inside the
380
+ # newly created database. Statements can create tables, indexes, etc.
381
+ # These statements execute atomically with the creation of the
382
+ # database: if there is an error in any statement, the database is not
383
+ # created. Optional.
384
+ #
385
+ # @return [Database::Job] The job representing the long-running,
386
+ # asynchronous processing of a database create operation.
387
+ #
388
+ # @example
389
+ # require "google/cloud/spanner"
390
+ #
391
+ # spanner = Google::Cloud::Spanner.new
392
+ #
393
+ # job = spanner.create_database "my-instance",
394
+ # "my-new-database"
395
+ #
396
+ # job.done? #=> false
397
+ # job.reload! # API call
398
+ # job.done? #=> true
399
+ # database = job.database
400
+ #
401
+ def create_database instance_id, database_id, statements: []
402
+ grpc = service.create_database instance_id, database_id,
403
+ statements: statements
404
+ Database::Job.from_grpc grpc, service
405
+ end
406
+
407
+ ##
408
+ # Creates a Cloud Spanner client. A client is used to read and/or modify
409
+ # data in a Cloud Spanner database.
410
+ #
411
+ # @param [String] instance_id The unique identifier for the instance.
412
+ # Required.
413
+ # @param [String] database_id The unique identifier for the database.
414
+ # Required.
415
+ # @param [Hash] pool Settings to control how and when sessions are
416
+ # managed by the client. The following settings can be provided:
417
+ #
418
+ # * `:min` (Integer) Minimum number of sessions that the client will
419
+ # maintain at any point in time. The default is 10.
420
+ # * `:max` (Integer) Maximum number of sessions that the client will
421
+ # have at any point in time. The default is 100.
422
+ # * `:keepalive` (Numeric) The amount of time a session can be idle
423
+ # before an attempt is made to prevent the idle sessions from being
424
+ # closed by the Cloud Spanner service. The default is 1800 (30
425
+ # minutes).
426
+ # * `:write_ratio` (Float) The ratio of sessions with pre-allocated
427
+ # transactions to those without. Pre-allocating transactions
428
+ # improves the performance of writes made by the client. The higher
429
+ # the value, the more transactions are pre-allocated. The value must
430
+ # be >= 0 and <= 1. The default is 0.3.
431
+ # * `:fail` (true/false) When `true` the client raises a
432
+ # {SessionLimitError} when the client has allocated the `max` number
433
+ # of sessions. When `false` the client blocks until a session
434
+ # becomes available. The default is `true`.
435
+ #
436
+ # @return [Client] The newly created client.
437
+ #
438
+ # @example
439
+ # require "google/cloud/spanner"
440
+ #
441
+ # spanner = Google::Cloud::Spanner.new
442
+ #
443
+ # db = spanner.client "my-instance", "my-database"
444
+ #
445
+ # db.transaction do |tx|
446
+ # results = tx.execute "SELECT * FROM users"
447
+ #
448
+ # results.rows.each do |row|
449
+ # puts "User #{row[:id]} is #{row[:name]}"
450
+ # end
451
+ # end
452
+ #
453
+ def client instance_id, database_id, pool: {}
454
+ Client.new self, instance_id, database_id,
455
+ valid_session_pool_options(pool)
456
+ end
457
+
458
+ protected
459
+
460
+ ##
461
+ # @private Raise an error unless an active connection to the service is
462
+ # available.
463
+ def ensure_service!
464
+ fail "Must have active connection to service" unless service
465
+ end
466
+
467
+ def database_path instance_id, database_id
468
+ Admin::Database::V1::DatabaseAdminClient.database_path(
469
+ project, instance_id, database_id)
470
+ end
471
+
472
+ def valid_session_pool_options opts = {}
473
+ { min: opts[:min], max: opts[:max], keepalive: opts[:keepalive],
474
+ write_ratio: opts[:write_ratio], fail: opts[:fail]
475
+ }.delete_if { |_k, v| v.nil? }
476
+ end
477
+ end
478
+ end
479
+ end
480
+ end