Class: Google::Cloud::Spanner::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/spanner/service.rb

Overview

methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, credentials, timeout: nil, client_config: nil) ⇒ Service

Creates a new Service instance.



35
36
37
38
39
40
# File 'lib/google/cloud/spanner/service.rb', line 35

def initialize project, credentials, timeout: nil, client_config: nil
  @project = project
  @credentials = credentials
  @timeout = timeout
  @client_config = client_config || {}
end

Instance Attribute Details

#client_configObject

Returns the value of attribute client_config.



31
32
33
# File 'lib/google/cloud/spanner/service.rb', line 31

def client_config
  @client_config
end

#credentialsObject

Returns the value of attribute credentials.



31
32
33
# File 'lib/google/cloud/spanner/service.rb', line 31

def credentials
  @credentials
end

#mocked_databasesObject

Returns the value of attribute mocked_databases.



76
77
78
# File 'lib/google/cloud/spanner/service.rb', line 76

def mocked_databases
  @mocked_databases
end

#mocked_instancesObject

Returns the value of attribute mocked_instances.



64
65
66
# File 'lib/google/cloud/spanner/service.rb', line 64

def mocked_instances
  @mocked_instances
end

#mocked_serviceObject

Returns the value of attribute mocked_service.



52
53
54
# File 'lib/google/cloud/spanner/service.rb', line 52

def mocked_service
  @mocked_service
end

#projectObject

Returns the value of attribute project.



31
32
33
# File 'lib/google/cloud/spanner/service.rb', line 31

def project
  @project
end

#timeoutObject

Returns the value of attribute timeout.



31
32
33
# File 'lib/google/cloud/spanner/service.rb', line 31

def timeout
  @timeout
end

Instance Method Details

#begin_transaction(session_name) ⇒ Object



309
310
311
312
313
314
315
316
# File 'lib/google/cloud/spanner/service.rb', line 309

def begin_transaction session_name
  tx_opts = Google::Spanner::V1::TransactionOptions.new(read_write:
    Google::Spanner::V1::TransactionOptions::ReadWrite.new)
  opts = default_options_from_session session_name
  execute do
    service.begin_transaction session_name, tx_opts, options: opts
  end
end

#commit(session_name, mutations = [], transaction_id: nil) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/google/cloud/spanner/service.rb', line 287

def commit session_name, mutations = [], transaction_id: nil
  tx_opts = nil
  if transaction_id.nil?
    tx_opts = Google::Spanner::V1::TransactionOptions.new(read_write:
      Google::Spanner::V1::TransactionOptions::ReadWrite.new)
  end
  opts = default_options_from_session session_name
  execute do
    service.commit \
      session_name, mutations,
      transaction_id: transaction_id, single_use_transaction: tx_opts,
      options: opts
  end
end

#create_database(instance_id, database_id, statements: []) ⇒ Object



182
183
184
185
186
187
188
189
# File 'lib/google/cloud/spanner/service.rb', line 182

def create_database instance_id, database_id, statements: []
  execute do
    databases.create_database \
      instance_path(instance_id),
      "CREATE DATABASE `#{database_id}`",
      extra_statements: Array(statements)
  end
end

#create_instance(instance_id, name: nil, config: nil, nodes: nil, labels: nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/google/cloud/spanner/service.rb', line 97

def create_instance instance_id, name: nil, config: nil, nodes: nil,
                    labels: nil
  labels = Hash[labels.map { |k, v| [String(k), String(v)] }] if labels

  create_obj = Google::Spanner::Admin::Instance::V1::Instance.new({
    display_name: name, config: instance_config_path(config),
    node_count: nodes, labels: labels
  }.delete_if { |_, v| v.nil? })

  execute do
    instances.create_instance project_path, instance_id, create_obj
  end
end

#create_session(database_name) ⇒ Object



240
241
242
243
244
245
# File 'lib/google/cloud/spanner/service.rb', line 240

def create_session database_name
  opts = default_options_from_session database_name
  execute do
    service.create_session database_name, options: opts
  end
end

#create_snapshot(session_name, strong: nil, timestamp: nil, staleness: nil) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/google/cloud/spanner/service.rb', line 318

def create_snapshot session_name, strong: nil, timestamp: nil,
                    staleness: nil
  tx_opts = Google::Spanner::V1::TransactionOptions.new(read_only:
    Google::Spanner::V1::TransactionOptions::ReadOnly.new({
      strong: strong,
      read_timestamp: Convert.time_to_timestamp(timestamp),
      exact_staleness: Convert.number_to_duration(staleness),
      return_read_timestamp: true
    }.delete_if { |_, v| v.nil? }))
  opts = default_options_from_session session_name
  execute do
    service.begin_transaction session_name, tx_opts, options: opts
  end
end

#databasesObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/google/cloud/spanner/service.rb', line 66

def databases
  return mocked_databases if mocked_databases
  @databases ||= \
    Admin::Database::V1::DatabaseAdminClient.new(
      credentials: credentials,
      timeout: timeout,
      client_config: client_config,
      lib_name: "gccl",
      lib_version: Google::Cloud::Spanner::VERSION)
end

#delete_instance(name) ⇒ Object



120
121
122
123
124
# File 'lib/google/cloud/spanner/service.rb', line 120

def delete_instance name
  execute do
    instances.delete_instance instance_path(name)
  end
end

#delete_session(session_name) ⇒ Object



247
248
249
250
251
252
# File 'lib/google/cloud/spanner/service.rb', line 247

def delete_session session_name
  opts = default_options_from_session session_name
  execute do
    service.delete_session session_name, options: opts
  end
end

#drop_database(instance_id, database_id) ⇒ Object



191
192
193
194
195
# File 'lib/google/cloud/spanner/service.rb', line 191

def drop_database instance_id, database_id
  execute do
    databases.drop_database database_path(instance_id, database_id)
  end
end

#get_database(instance_id, database_id) ⇒ Object



176
177
178
179
180
# File 'lib/google/cloud/spanner/service.rb', line 176

def get_database instance_id, database_id
  execute do
    databases.get_database database_path(instance_id, database_id)
  end
end

#get_database_ddl(instance_id, database_id) ⇒ Object



197
198
199
200
201
# File 'lib/google/cloud/spanner/service.rb', line 197

def get_database_ddl instance_id, database_id
  execute do
    databases.get_database_ddl database_path(instance_id, database_id)
  end
end

#get_database_policy(instance_id, database_id) ⇒ Object



213
214
215
216
217
# File 'lib/google/cloud/spanner/service.rb', line 213

def get_database_policy instance_id, database_id
  execute do
    databases.get_iam_policy database_path(instance_id, database_id)
  end
end

#get_instance(name) ⇒ Object



91
92
93
94
95
# File 'lib/google/cloud/spanner/service.rb', line 91

def get_instance name
  execute do
    instances.get_instance instance_path(name)
  end
end

#get_instance_config(name) ⇒ Object



157
158
159
160
161
# File 'lib/google/cloud/spanner/service.rb', line 157

def get_instance_config name
  execute do
    instances.get_instance_config instance_config_path(name)
  end
end

#get_instance_policy(name) ⇒ Object



126
127
128
129
130
# File 'lib/google/cloud/spanner/service.rb', line 126

def get_instance_policy name
  execute do
    instances.get_iam_policy instance_path(name)
  end
end

#get_session(session_name) ⇒ Object



233
234
235
236
237
238
# File 'lib/google/cloud/spanner/service.rb', line 233

def get_session session_name
  opts = default_options_from_session session_name
  execute do
    service.get_session session_name, options: opts
  end
end

#inspectObject



333
334
335
# File 'lib/google/cloud/spanner/service.rb', line 333

def inspect
  "#{self.class}(#{@project})"
end

#instancesObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/google/cloud/spanner/service.rb', line 54

def instances
  return mocked_instances if mocked_instances
  @instances ||= \
    Admin::Instance::V1::InstanceAdminClient.new(
      credentials: credentials,
      timeout: timeout,
      client_config: client_config,
      lib_name: "gccl",
      lib_version: Google::Cloud::Spanner::VERSION)
end

#list_databases(instance_id, token: nil, max: nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/google/cloud/spanner/service.rb', line 163

def list_databases instance_id, token: nil, max: nil
  call_options = nil
  call_options = Google::Gax::CallOptions.new page_token: token if token

  execute do
    paged_enum = databases.list_databases instance_path(instance_id),
                                          page_size: max,
                                          options: call_options

    paged_enum.page.response
  end
end

#list_instance_configs(token: nil, max: nil) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/google/cloud/spanner/service.rb', line 144

def list_instance_configs token: nil, max: nil
  call_options = nil
  call_options = Google::Gax::CallOptions.new page_token: token if token

  execute do
    paged_enum = instances.list_instance_configs project_path,
                                                 page_size: max,
                                                 options: call_options

    paged_enum.page.response
  end
end

#list_instances(token: nil, max: nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/google/cloud/spanner/service.rb', line 78

def list_instances token: nil, max: nil
  call_options = nil
  call_options = Google::Gax::CallOptions.new page_token: token if token

  execute do
    paged_enum = instances.list_instances project_path,
                                          page_size: max,
                                          options: call_options

    paged_enum.page.response
  end
end

#rollback(session_name, transaction_id) ⇒ Object



302
303
304
305
306
307
# File 'lib/google/cloud/spanner/service.rb', line 302

def rollback session_name, transaction_id
  opts = default_options_from_session session_name
  execute do
    service.rollback session_name, transaction_id, options: opts
  end
end

#serviceObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/google/cloud/spanner/service.rb', line 42

def service
  return mocked_service if mocked_service
  @service ||= \
    V1::SpannerClient.new(
      credentials: credentials,
      timeout: timeout,
      client_config: client_config,
      lib_name: "gccl",
      lib_version: Google::Cloud::Spanner::VERSION)
end

#set_database_policy(instance_id, database_id, new_policy) ⇒ Object



219
220
221
222
223
224
# File 'lib/google/cloud/spanner/service.rb', line 219

def set_database_policy instance_id, database_id, new_policy
  execute do
    databases.set_iam_policy \
      database_path(instance_id, database_id), new_policy
  end
end

#set_instance_policy(name, new_policy) ⇒ Object



132
133
134
135
136
# File 'lib/google/cloud/spanner/service.rb', line 132

def set_instance_policy name, new_policy
  execute do
    instances.set_iam_policy instance_path(name), new_policy
  end
end

#streaming_execute_sql(session_name, sql, transaction: nil, params: nil, types: nil, resume_token: nil) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/google/cloud/spanner/service.rb', line 254

def streaming_execute_sql session_name, sql, transaction: nil,
                          params: nil, types: nil, resume_token: nil
  input_params = nil
  input_param_types = nil
  unless params.nil?
    input_param_pairs = Convert.to_query_params params, types
    input_params = Google::Protobuf::Struct.new(
      fields: Hash[input_param_pairs.map { |k, v| [k, v.first] }])
    input_param_types = Hash[
      input_param_pairs.map { |k, v| [k, v.last] }]
  end
  opts = default_options_from_session session_name
  execute do
    service.execute_streaming_sql \
      session_name, sql, transaction: transaction, params: input_params,
                         param_types: input_param_types,
                         resume_token: resume_token, options: opts
  end
end

#streaming_read_table(session_name, table_name, columns, keys: nil, index: nil, transaction: nil, limit: nil, resume_token: nil) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/google/cloud/spanner/service.rb', line 274

def streaming_read_table session_name, table_name, columns, keys: nil,
                         index: nil, transaction: nil, limit: nil,
                         resume_token: nil
  columns.map!(&:to_s)
  opts = default_options_from_session session_name
  execute do
    service.streaming_read \
      session_name, table_name, columns, key_set(keys),
      transaction: transaction, index: index, limit: limit,
      resume_token: resume_token, options: opts
  end
end

#test_database_permissions(instance_id, database_id, permissions) ⇒ Object



226
227
228
229
230
231
# File 'lib/google/cloud/spanner/service.rb', line 226

def test_database_permissions instance_id, database_id, permissions
  execute do
    databases.test_iam_permissions \
      database_path(instance_id, database_id), permissions
  end
end

#test_instance_permissions(name, permissions) ⇒ Object



138
139
140
141
142
# File 'lib/google/cloud/spanner/service.rb', line 138

def test_instance_permissions name, permissions
  execute do
    instances.test_iam_permissions instance_path(name), permissions
  end
end

#update_database_ddl(instance_id, database_id, statements: [], operation_id: nil) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/google/cloud/spanner/service.rb', line 203

def update_database_ddl instance_id, database_id, statements: [],
                        operation_id: nil
  execute do
    databases.update_database_ddl \
      database_path(instance_id, database_id),
      Array(statements),
      operation_id: operation_id
  end
end

#update_instance(instance_obj) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/google/cloud/spanner/service.rb', line 111

def update_instance instance_obj
  mask = Google::Protobuf::FieldMask.new(
    paths: %w(display_name node_count labels))

  execute do
    instances.update_instance instance_obj, mask
  end
end