google-cloud-spanner 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/google-cloud-spanner.rb +106 -0
- data/lib/google/cloud/spanner.rb +382 -0
- data/lib/google/cloud/spanner/admin/database/v1.rb +17 -0
- data/lib/google/cloud/spanner/admin/database/v1/database_admin_client.rb +703 -0
- data/lib/google/cloud/spanner/admin/database/v1/database_admin_client_config.json +73 -0
- data/lib/google/cloud/spanner/admin/database/v1/doc/google/iam/v1/policy.rb +139 -0
- data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/any.rb +114 -0
- data/lib/google/cloud/spanner/admin/database/v1/doc/google/rpc/status.rb +83 -0
- data/lib/google/cloud/spanner/admin/database/v1/doc/google/spanner/admin/database/v1/spanner_database_admin.rb +188 -0
- data/lib/google/cloud/spanner/admin/instance/v1.rb +17 -0
- data/lib/google/cloud/spanner/admin/instance/v1/doc/google/iam/v1/policy.rb +139 -0
- data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/any.rb +114 -0
- data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/field_mask.rb +223 -0
- data/lib/google/cloud/spanner/admin/instance/v1/doc/google/rpc/status.rb +83 -0
- data/lib/google/cloud/spanner/admin/instance/v1/doc/google/spanner/admin/instance/v1/spanner_instance_admin.rb +268 -0
- data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client.rb +868 -0
- data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client_config.json +78 -0
- data/lib/google/cloud/spanner/client.rb +1034 -0
- data/lib/google/cloud/spanner/commit.rb +351 -0
- data/lib/google/cloud/spanner/convert.rb +311 -0
- data/lib/google/cloud/spanner/credentials.rb +32 -0
- data/lib/google/cloud/spanner/data.rb +199 -0
- data/lib/google/cloud/spanner/database.rb +377 -0
- data/lib/google/cloud/spanner/database/job.rb +179 -0
- data/lib/google/cloud/spanner/database/list.rb +171 -0
- data/lib/google/cloud/spanner/errors.rb +73 -0
- data/lib/google/cloud/spanner/fields.rb +252 -0
- data/lib/google/cloud/spanner/instance.rb +472 -0
- data/lib/google/cloud/spanner/instance/config.rb +99 -0
- data/lib/google/cloud/spanner/instance/config/list.rb +171 -0
- data/lib/google/cloud/spanner/instance/job.rb +197 -0
- data/lib/google/cloud/spanner/instance/list.rb +167 -0
- data/lib/google/cloud/spanner/policy.rb +201 -0
- data/lib/google/cloud/spanner/pool.rb +279 -0
- data/lib/google/cloud/spanner/project.rb +480 -0
- data/lib/google/cloud/spanner/range.rb +99 -0
- data/lib/google/cloud/spanner/results.rb +280 -0
- data/lib/google/cloud/spanner/service.rb +458 -0
- data/lib/google/cloud/spanner/session.rb +565 -0
- data/lib/google/cloud/spanner/snapshot.rb +260 -0
- data/lib/google/cloud/spanner/transaction.rb +533 -0
- data/lib/google/cloud/spanner/v1.rb +17 -0
- data/lib/google/cloud/spanner/v1/doc/google/protobuf/duration.rb +77 -0
- data/lib/google/cloud/spanner/v1/doc/google/protobuf/struct.rb +73 -0
- data/lib/google/cloud/spanner/v1/doc/google/protobuf/timestamp.rb +81 -0
- data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/keys.rb +148 -0
- data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/mutation.rb +80 -0
- data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/query_plan.rb +120 -0
- data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/result_set.rb +175 -0
- data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/spanner.rb +206 -0
- data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/transaction.rb +351 -0
- data/lib/google/cloud/spanner/v1/spanner_client.rb +850 -0
- data/lib/google/cloud/spanner/v1/spanner_client_config.json +78 -0
- data/lib/google/cloud/spanner/version.rb +22 -0
- data/lib/google/spanner/admin/database/v1/spanner_database_admin_pb.rb +85 -0
- data/lib/google/spanner/admin/database/v1/spanner_database_admin_services_pb.rb +95 -0
- data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_pb.rb +106 -0
- data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_services_pb.rb +180 -0
- data/lib/google/spanner/v1/keys_pb.rb +33 -0
- data/lib/google/spanner/v1/mutation_pb.rb +38 -0
- data/lib/google/spanner/v1/query_plan_pb.rb +47 -0
- data/lib/google/spanner/v1/result_set_pb.rb +43 -0
- data/lib/google/spanner/v1/spanner_pb.rb +90 -0
- data/lib/google/spanner/v1/spanner_services_pb.rb +131 -0
- data/lib/google/spanner/v1/transaction_pb.rb +51 -0
- data/lib/google/spanner/v1/type_pb.rb +43 -0
- metadata +309 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 3ecf7b383ed94a1522a5d90bd7574113f087c5ab
|
|
4
|
+
data.tar.gz: c4698851d6e116762e2ea65a3e6e74f252af7060
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6014ed1d974e1524681604fc4da27ff8404308e7e5998ff6b2aa7c69cfc9d599d41a4312755177c33c4c499cc18c8623ce59fe433cebe30ce0dff96c63c73439
|
|
7
|
+
data.tar.gz: a38b63f7ebada70240457881d43d12df69238ad0562f2a9384647c74cf8ddcec34e286e7946e657c55cfe5d09e467e0ae2655fbe32ac89c4ab2cef26e6e2e7c1
|
|
@@ -0,0 +1,106 @@
|
|
|
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
|
+
# This file is here to be autorequired by bundler, so that the .spanner and
|
|
17
|
+
# #spanner methods can be available, but the library and all dependencies won't
|
|
18
|
+
# be loaded until required and used.
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
gem "google-cloud-core"
|
|
22
|
+
require "google/cloud"
|
|
23
|
+
|
|
24
|
+
module Google
|
|
25
|
+
module Cloud
|
|
26
|
+
##
|
|
27
|
+
# Creates a new object for connecting to the Spanner service.
|
|
28
|
+
# Each call creates a new connection.
|
|
29
|
+
#
|
|
30
|
+
# For more information on connecting to Google Cloud see the [Authentication
|
|
31
|
+
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
|
32
|
+
#
|
|
33
|
+
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
|
|
34
|
+
# set of resources and operations that the connection can access. See
|
|
35
|
+
# [Using OAuth 2.0 to Access Google
|
|
36
|
+
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
|
37
|
+
#
|
|
38
|
+
# The default scopes are:
|
|
39
|
+
#
|
|
40
|
+
# * `https://www.googleapis.com/auth/spanner`
|
|
41
|
+
# * `https://www.googleapis.com/auth/spanner.data`
|
|
42
|
+
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
|
43
|
+
# @param [Hash] client_config A hash of values to override the default
|
|
44
|
+
# behavior of the API client. Optional.
|
|
45
|
+
#
|
|
46
|
+
# @return [Google::Cloud::Spanner::Project]
|
|
47
|
+
#
|
|
48
|
+
# @example
|
|
49
|
+
# require "google/cloud"
|
|
50
|
+
#
|
|
51
|
+
# gcloud = Google::Cloud.new
|
|
52
|
+
# spanner = gcloud.spanner
|
|
53
|
+
#
|
|
54
|
+
# @example The default scope can be overridden with the `scope` option:
|
|
55
|
+
# require "google/cloud"
|
|
56
|
+
#
|
|
57
|
+
# gcloud = Google::Cloud.new
|
|
58
|
+
# platform_scope = "https://www.googleapis.com/auth/cloud-platform"
|
|
59
|
+
# spanner = gcloud.spanner scope: platform_scope
|
|
60
|
+
#
|
|
61
|
+
def spanner scope: nil, timeout: nil, client_config: nil
|
|
62
|
+
Google::Cloud.spanner @project, @keyfile, scope: scope,
|
|
63
|
+
timeout: (timeout || @timeout),
|
|
64
|
+
client_config: client_config
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
##
|
|
68
|
+
# Creates a new object for connecting to the Spanner service.
|
|
69
|
+
# Each call creates a new connection.
|
|
70
|
+
#
|
|
71
|
+
# For more information on connecting to Google Cloud see the [Authentication
|
|
72
|
+
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
|
73
|
+
#
|
|
74
|
+
# @param [String] project Project identifier for the Spanner service you are
|
|
75
|
+
# connecting to.
|
|
76
|
+
# @param [String, Hash] keyfile Keyfile downloaded from Google Cloud. If
|
|
77
|
+
# file path the file must be readable.
|
|
78
|
+
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
|
|
79
|
+
# set of resources and operations that the connection can access. See
|
|
80
|
+
# [Using OAuth 2.0 to Access Google
|
|
81
|
+
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
|
82
|
+
#
|
|
83
|
+
# The default scopes are:
|
|
84
|
+
#
|
|
85
|
+
# * `https://www.googleapis.com/auth/spanner`
|
|
86
|
+
# * `https://www.googleapis.com/auth/spanner.data`
|
|
87
|
+
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
|
88
|
+
# @param [Hash] client_config A hash of values to override the default
|
|
89
|
+
# behavior of the API client. Optional.
|
|
90
|
+
#
|
|
91
|
+
# @return [Google::Cloud::Spanner::Project]
|
|
92
|
+
#
|
|
93
|
+
# @example
|
|
94
|
+
# require "google/cloud"
|
|
95
|
+
#
|
|
96
|
+
# spanner = Google::Cloud.spanner
|
|
97
|
+
#
|
|
98
|
+
def self.spanner project = nil, keyfile = nil, scope: nil, timeout: nil,
|
|
99
|
+
client_config: nil
|
|
100
|
+
require "google/cloud/spanner"
|
|
101
|
+
Google::Cloud::Spanner.new project: project, keyfile: keyfile,
|
|
102
|
+
scope: scope, timeout: timeout,
|
|
103
|
+
client_config: client_config
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,382 @@
|
|
|
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"
|
|
17
|
+
require "google/cloud/spanner/project"
|
|
18
|
+
|
|
19
|
+
module Google
|
|
20
|
+
module Cloud
|
|
21
|
+
##
|
|
22
|
+
# # Cloud Spanner
|
|
23
|
+
#
|
|
24
|
+
# Cloud Spanner is a fully managed, mission-critical, relational database
|
|
25
|
+
# service that offers transactional consistency at global scale, schemas,
|
|
26
|
+
# SQL (ANSI 2011 with extensions), and automatic, synchronous replication
|
|
27
|
+
# for high availability.
|
|
28
|
+
#
|
|
29
|
+
# For more information about Cloud Spanner, read the [Cloud
|
|
30
|
+
# Spanner Documentation](https://cloud.google.com/spanner/docs/).
|
|
31
|
+
#
|
|
32
|
+
# The goal of google-cloud is to provide an API that is comfortable to
|
|
33
|
+
# Rubyists. Authentication is handled by {Google::Cloud#spanner}. You can
|
|
34
|
+
# provide the project and credential information to connect to the Cloud
|
|
35
|
+
# Spanner service, or if you are running on Google Compute Engine this
|
|
36
|
+
# configuration is taken care of for you. You can read more about the
|
|
37
|
+
# options for connecting in the [Authentication
|
|
38
|
+
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
|
39
|
+
#
|
|
40
|
+
# ## Creating instances
|
|
41
|
+
#
|
|
42
|
+
# When you first use Cloud Spanner, you must create an instance, which is an
|
|
43
|
+
# allocation of resources that are used by Cloud Spanner databases. When you
|
|
44
|
+
# create an instance, you choose where your data is stored and how many
|
|
45
|
+
# nodes are used for your data. (For more information, see [Instance
|
|
46
|
+
# Configuration](https://cloud.google.com/spanner/docs/instance-configuration)).
|
|
47
|
+
#
|
|
48
|
+
# Use {Spanner::Project#create_instance} to create an instance:
|
|
49
|
+
#
|
|
50
|
+
# ```ruby
|
|
51
|
+
# require "google/cloud/spanner"
|
|
52
|
+
#
|
|
53
|
+
# spanner = Google::Cloud::Spanner.new
|
|
54
|
+
#
|
|
55
|
+
# job = spanner.create_instance "my-instance",
|
|
56
|
+
# name: "My Instance",
|
|
57
|
+
# config: "regional-us-central1",
|
|
58
|
+
# nodes: 5,
|
|
59
|
+
# labels: { production: :env }
|
|
60
|
+
#
|
|
61
|
+
# job.done? #=> false
|
|
62
|
+
# job.reload! # API call
|
|
63
|
+
# job.done? #=> true
|
|
64
|
+
# instance = job.instance
|
|
65
|
+
# ```
|
|
66
|
+
#
|
|
67
|
+
# ## Creating databases
|
|
68
|
+
#
|
|
69
|
+
# Now that you have created an instance, you can create a database. Cloud
|
|
70
|
+
# Spanner databases hold the tables and indexes that allow you to read and
|
|
71
|
+
# write data. You may create multiple databases in an instance.
|
|
72
|
+
#
|
|
73
|
+
# Use {Spanner::Project#create_database} (or
|
|
74
|
+
# {Spanner::Instance#create_database}) to create a database:
|
|
75
|
+
#
|
|
76
|
+
# ```ruby
|
|
77
|
+
# require "google/cloud/spanner"
|
|
78
|
+
#
|
|
79
|
+
# spanner = Google::Cloud::Spanner.new
|
|
80
|
+
#
|
|
81
|
+
# job = spanner.create_database "my-instance", "my-database"
|
|
82
|
+
#
|
|
83
|
+
# job.done? #=> false
|
|
84
|
+
# job.reload! # API call
|
|
85
|
+
# job.done? #=> true
|
|
86
|
+
# database = job.database
|
|
87
|
+
# ```
|
|
88
|
+
#
|
|
89
|
+
# ## Updating database schemas
|
|
90
|
+
#
|
|
91
|
+
# Cloud Spanner supports schema updates to a database while the database
|
|
92
|
+
# continues to serve traffic. Schema updates do not require taking the
|
|
93
|
+
# database offline and they do not lock entire tables or columns; you can
|
|
94
|
+
# continue writing data to the database during the schema update.
|
|
95
|
+
#
|
|
96
|
+
# Use {Spanner::Database#update} to execute one or more statements in Cloud
|
|
97
|
+
# Spanner's Data Definition Language (DDL):
|
|
98
|
+
#
|
|
99
|
+
# ```ruby
|
|
100
|
+
# require "google/cloud/spanner"
|
|
101
|
+
#
|
|
102
|
+
# spanner = Google::Cloud::Spanner.new
|
|
103
|
+
#
|
|
104
|
+
# database = spanner.database "my-instance", "my-database"
|
|
105
|
+
#
|
|
106
|
+
# add_users_table_sql = %q(
|
|
107
|
+
# CREATE TABLE users (
|
|
108
|
+
# id INT64 NOT NULL,
|
|
109
|
+
# username STRING(25) NOT NULL,
|
|
110
|
+
# name STRING(45) NOT NULL,
|
|
111
|
+
# email STRING(128),
|
|
112
|
+
# ) PRIMARY KEY(id)
|
|
113
|
+
# )
|
|
114
|
+
#
|
|
115
|
+
# database.update statements: [add_users_table_sql]
|
|
116
|
+
# ```
|
|
117
|
+
#
|
|
118
|
+
# ## Creating clients
|
|
119
|
+
#
|
|
120
|
+
# In order to read and/or write data, you must create a database client.
|
|
121
|
+
# You can think of a client as a database connection: All of your
|
|
122
|
+
# interactions with Cloud Spanner data must go through a client. Typically
|
|
123
|
+
# you create a client when your application starts up, then you re-use that
|
|
124
|
+
# client to read, write, and execute transactions.
|
|
125
|
+
#
|
|
126
|
+
# Use {Spanner::Project#client} to create a client:
|
|
127
|
+
#
|
|
128
|
+
# ```ruby
|
|
129
|
+
# require "google/cloud/spanner"
|
|
130
|
+
#
|
|
131
|
+
# spanner = Google::Cloud::Spanner.new
|
|
132
|
+
#
|
|
133
|
+
# db = spanner.client "my-instance", "my-database"
|
|
134
|
+
#
|
|
135
|
+
# results = client.execute "SELECT 1"
|
|
136
|
+
#
|
|
137
|
+
# results.rows.each do |row|
|
|
138
|
+
# puts row
|
|
139
|
+
# end
|
|
140
|
+
# ```
|
|
141
|
+
#
|
|
142
|
+
# ## Writing data
|
|
143
|
+
#
|
|
144
|
+
# You write data using your client object. The client object supports
|
|
145
|
+
# various mutation operations, as well as combinations of inserts, updates,
|
|
146
|
+
# deletes, etc., that can be applied atomically to different rows and/or
|
|
147
|
+
# tables in a database.
|
|
148
|
+
#
|
|
149
|
+
# Use {Spanner::Client#commit} to execute various mutations atomically at a
|
|
150
|
+
# single logical point in time. All changes are accumulated in memory until
|
|
151
|
+
# the block completes. Unlike {Spanner::Client#transaction}, which can also
|
|
152
|
+
# perform reads, this operation accepts only mutations and makes a single
|
|
153
|
+
# API request.
|
|
154
|
+
#
|
|
155
|
+
# ```ruby
|
|
156
|
+
# require "google/cloud/spanner"
|
|
157
|
+
#
|
|
158
|
+
# spanner = Google::Cloud::Spanner.new
|
|
159
|
+
#
|
|
160
|
+
# db = spanner.client "my-instance", "my-database"
|
|
161
|
+
#
|
|
162
|
+
# db.commit do |c|
|
|
163
|
+
# c.update "users", [{ id: 1, name: "Charlie", active: false }]
|
|
164
|
+
# c.insert "users", [{ id: 2, name: "Harvey", active: true }]
|
|
165
|
+
# end
|
|
166
|
+
# ```
|
|
167
|
+
#
|
|
168
|
+
# ## Querying data using SQL
|
|
169
|
+
#
|
|
170
|
+
# Cloud Spanner supports a native SQL interface for reading data that is
|
|
171
|
+
# available through {Spanner::Client#execute}:
|
|
172
|
+
#
|
|
173
|
+
# ```ruby
|
|
174
|
+
# require "google/cloud/spanner"
|
|
175
|
+
#
|
|
176
|
+
# spanner = Google::Cloud::Spanner.new
|
|
177
|
+
#
|
|
178
|
+
# db = spanner.client "my-instance", "my-database"
|
|
179
|
+
#
|
|
180
|
+
# results = db.execute "SELECT * FROM users"
|
|
181
|
+
#
|
|
182
|
+
# results.rows.each do |row|
|
|
183
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
|
184
|
+
# end
|
|
185
|
+
# ```
|
|
186
|
+
#
|
|
187
|
+
# ## Reading data using the read method
|
|
188
|
+
#
|
|
189
|
+
# In addition to Cloud Spanner's SQL interface, Cloud Spanner also supports
|
|
190
|
+
# a read interface. Use the {Spanner::Client#read} method to read rows from
|
|
191
|
+
# the database, and use its `keys` option to pass unique identifiers as both
|
|
192
|
+
# lists and ranges:
|
|
193
|
+
#
|
|
194
|
+
# ```ruby
|
|
195
|
+
# require "google/cloud/spanner"
|
|
196
|
+
#
|
|
197
|
+
# spanner = Google::Cloud::Spanner.new
|
|
198
|
+
#
|
|
199
|
+
# db = spanner.client "my-instance", "my-database"
|
|
200
|
+
#
|
|
201
|
+
# results = db.read "users", [:id, :name], keys: 1..5
|
|
202
|
+
#
|
|
203
|
+
# results.rows.each do |row|
|
|
204
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
|
205
|
+
# end
|
|
206
|
+
# ```
|
|
207
|
+
#
|
|
208
|
+
# ## Using read-write transactions
|
|
209
|
+
#
|
|
210
|
+
# When an operation might write data depending on values it reads, you
|
|
211
|
+
# should use a read-write transaction to perform the reads and writes
|
|
212
|
+
# atomically.
|
|
213
|
+
#
|
|
214
|
+
# Suppose that sales of `Albums(1, 1)` are lower than expected and you want
|
|
215
|
+
# to move $200,000 from the marketing budget of `Albums(2, 2)` to it, but
|
|
216
|
+
# only if the budget of `Albums(2, 2)` is at least $300,000.
|
|
217
|
+
#
|
|
218
|
+
# Use {Spanner::Client#transaction} to execute both reads and writes
|
|
219
|
+
# atomically at a single logical point in time. All changes are accumulated
|
|
220
|
+
# in memory until the block completes. Transactions will be automatically
|
|
221
|
+
# retried when possible. This operation makes separate API requests to begin
|
|
222
|
+
# and commit the transaction.
|
|
223
|
+
#
|
|
224
|
+
# ```ruby
|
|
225
|
+
# require "google/cloud/spanner"
|
|
226
|
+
#
|
|
227
|
+
# spanner = Google::Cloud::Spanner.new
|
|
228
|
+
#
|
|
229
|
+
# db = spanner.client "my-instance", "my-database"
|
|
230
|
+
#
|
|
231
|
+
# db.transaction do |tx|
|
|
232
|
+
# # Read the second album budget.
|
|
233
|
+
# second_album_result = tx.read "Albums", ["marketing_budget"],
|
|
234
|
+
# keys: [[2, 2]], limit: 1
|
|
235
|
+
# second_album_row = second_album_result.first
|
|
236
|
+
# second_album_budget = second_album_row.values.first
|
|
237
|
+
#
|
|
238
|
+
# transfer_amount = 200000
|
|
239
|
+
#
|
|
240
|
+
# if second_album_budget < 300000
|
|
241
|
+
# # Raising an exception will automatically roll back the transaction.
|
|
242
|
+
# raise "The second album doesn't have enough funds to transfer"
|
|
243
|
+
# end
|
|
244
|
+
#
|
|
245
|
+
# # Read the first album's budget.
|
|
246
|
+
# first_album_result = tx.read "Albums", ["marketing_budget"],
|
|
247
|
+
# keys: [[1, 1]], limit: 1
|
|
248
|
+
# first_album_row = first_album_result.first
|
|
249
|
+
# first_album_budget = first_album_row.values.first
|
|
250
|
+
#
|
|
251
|
+
# # Update the budgets.
|
|
252
|
+
# second_album_budget -= transfer_amount
|
|
253
|
+
# first_album_budget += transfer_amount
|
|
254
|
+
# puts "Setting first album's budget to #{first_album_budget} and the " \
|
|
255
|
+
# "second album's budget to #{second_album_budget}."
|
|
256
|
+
#
|
|
257
|
+
# # Update the rows.
|
|
258
|
+
# rows = [
|
|
259
|
+
# {singer_id: 1, album_id: 1, marketing_budget: first_album_budget},
|
|
260
|
+
# {singer_id: 2, album_id: 2, marketing_budget: second_album_budget}
|
|
261
|
+
# ]
|
|
262
|
+
# tx.update "Albums", rows
|
|
263
|
+
# end
|
|
264
|
+
# ```
|
|
265
|
+
#
|
|
266
|
+
# ## Using read-only transactions
|
|
267
|
+
#
|
|
268
|
+
# Suppose you want to execute more than one read at the same timestamp.
|
|
269
|
+
# Read-only transactions observe a consistent prefix of the transaction
|
|
270
|
+
# commit history, so your application always gets consistent data. Because
|
|
271
|
+
# read-only transactions are much faster than locking read-write
|
|
272
|
+
# transactions, we strongly recommend that you do all of your transaction
|
|
273
|
+
# reads in read-only transactions if possible.
|
|
274
|
+
#
|
|
275
|
+
# Use a {Spanner::Snapshot} object to execute statements in a read-only
|
|
276
|
+
# transaction. The snapshot object is available via a block provided to
|
|
277
|
+
# {Spanner::Client#snapshot}:
|
|
278
|
+
#
|
|
279
|
+
# ```ruby
|
|
280
|
+
# require "google/cloud/spanner"
|
|
281
|
+
#
|
|
282
|
+
# spanner = Google::Cloud::Spanner.new
|
|
283
|
+
#
|
|
284
|
+
# db = spanner.client "my-instance", "my-database"
|
|
285
|
+
#
|
|
286
|
+
# db.snapshot do |snp|
|
|
287
|
+
# results_1 = snp.execute "SELECT * FROM users"
|
|
288
|
+
# results_1.rows.each do |row|
|
|
289
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
|
290
|
+
# end
|
|
291
|
+
#
|
|
292
|
+
# # Perform another read using the `read` method. Even if the data
|
|
293
|
+
# # is updated in-between the reads, the snapshot ensures that both
|
|
294
|
+
# # return the same data.
|
|
295
|
+
# results_2 = db.read "users", [:id, :name]
|
|
296
|
+
# results_2.rows.each do |row|
|
|
297
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
|
298
|
+
# end
|
|
299
|
+
# end
|
|
300
|
+
# ```
|
|
301
|
+
#
|
|
302
|
+
# ## Deleting databases
|
|
303
|
+
#
|
|
304
|
+
# Use {Spanner::Database#drop} to delete a database:
|
|
305
|
+
#
|
|
306
|
+
# ```ruby
|
|
307
|
+
# require "google/cloud/spanner"
|
|
308
|
+
#
|
|
309
|
+
# spanner = Google::Cloud::Spanner.new
|
|
310
|
+
#
|
|
311
|
+
# database = spanner.database "my-instance", "my-database"
|
|
312
|
+
#
|
|
313
|
+
# database.drop
|
|
314
|
+
# ```
|
|
315
|
+
#
|
|
316
|
+
# ## Deleting instances
|
|
317
|
+
#
|
|
318
|
+
# When you delete an instance, all databases within it are automatically
|
|
319
|
+
# deleted. (If you only delete databases and not your instance, you will
|
|
320
|
+
# still incur charges for the instance.) Use {Spanner::Instance#delete} to
|
|
321
|
+
# delete an instance:
|
|
322
|
+
#
|
|
323
|
+
# ```ruby
|
|
324
|
+
# require "google/cloud/spanner"
|
|
325
|
+
#
|
|
326
|
+
# spanner = Google::Cloud::Spanner.new
|
|
327
|
+
#
|
|
328
|
+
# instance = spanner.instance "my-instance"
|
|
329
|
+
#
|
|
330
|
+
# instance.delete
|
|
331
|
+
# ````
|
|
332
|
+
#
|
|
333
|
+
module Spanner
|
|
334
|
+
##
|
|
335
|
+
# Creates a new object for connecting to the Spanner service.
|
|
336
|
+
# Each call creates a new connection.
|
|
337
|
+
#
|
|
338
|
+
# For more information on connecting to Google Cloud see the
|
|
339
|
+
# [Authentication
|
|
340
|
+
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
|
341
|
+
#
|
|
342
|
+
# @param [String] project Project identifier for the Spanner service you
|
|
343
|
+
# are connecting to.
|
|
344
|
+
# @param [String, Hash] keyfile Keyfile downloaded from Google Cloud. If
|
|
345
|
+
# file path the file must be readable.
|
|
346
|
+
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling
|
|
347
|
+
# the set of resources and operations that the connection can access.
|
|
348
|
+
# See [Using OAuth 2.0 to Access Google
|
|
349
|
+
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
|
350
|
+
#
|
|
351
|
+
# The default scopes are:
|
|
352
|
+
#
|
|
353
|
+
# * `https://www.googleapis.com/auth/spanner`
|
|
354
|
+
# * `https://www.googleapis.com/auth/spanner.data`
|
|
355
|
+
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
|
356
|
+
# @param [Hash] client_config A hash of values to override the default
|
|
357
|
+
# behavior of the API client. Optional.
|
|
358
|
+
#
|
|
359
|
+
# @return [Google::Cloud::Spanner::Project]
|
|
360
|
+
#
|
|
361
|
+
# @example
|
|
362
|
+
# require "google/cloud/spanner"
|
|
363
|
+
#
|
|
364
|
+
# spanner = Google::Cloud::Spanner.new
|
|
365
|
+
#
|
|
366
|
+
def self.new project: nil, keyfile: nil, scope: nil, timeout: nil,
|
|
367
|
+
client_config: nil
|
|
368
|
+
project ||= Google::Cloud::Spanner::Project.default_project
|
|
369
|
+
if keyfile.nil?
|
|
370
|
+
credentials = Google::Cloud::Spanner::Credentials.default scope: scope
|
|
371
|
+
else
|
|
372
|
+
credentials = Google::Cloud::Spanner::Credentials.new(
|
|
373
|
+
keyfile, scope: scope)
|
|
374
|
+
end
|
|
375
|
+
Google::Cloud::Spanner::Project.new(
|
|
376
|
+
Google::Cloud::Spanner::Service.new(
|
|
377
|
+
project, credentials, timeout: timeout,
|
|
378
|
+
client_config: client_config))
|
|
379
|
+
end
|
|
380
|
+
end
|
|
381
|
+
end
|
|
382
|
+
end
|