Class: Google::Cloud::Spanner::Session
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::Session
- Defined in:
- lib/google/cloud/spanner/session.rb
Overview
Session
A session can be used to perform transactions that read and/or modify data in a Cloud Spanner database. Sessions are meant to be reused for many consecutive transactions.
Sessions can only execute one transaction at a time. To execute multiple concurrent read-write/write-only transactions, create multiple sessions. Note that standalone reads and queries use a transaction internally, and count toward the one transaction limit.
Cloud Spanner limits the number of sessions that can exist at any given time; thus, it is a good idea to delete idle and/or unneeded sessions. Aside from explicit deletes, Cloud Spanner can delete sessions for which no operations are sent for more than an hour.
Instance Attribute Summary collapse
Class Method Summary collapse
-
.from_grpc(grpc, service) ⇒ Object
Google::Spanner::V1::Session.
Instance Method Summary collapse
-
#commit(transaction_id: nil) {|commit| ... } ⇒ Time
Creates changes to be applied to rows in the database.
-
#create_transaction ⇒ Object
Creates a new transaction object every time.
-
#database_id ⇒ String
The unique identifier for the database.
-
#delete(table, keys = [], transaction_id: nil) ⇒ Time
Deletes rows from a table.
-
#execute(sql, params: nil, types: nil, transaction: nil, partition_token: nil) ⇒ Google::Cloud::Spanner::Results
Executes a SQL query.
-
#idle_since?(duration) ⇒ Boolean
Determines if the session has been idle longer than the given duration.
-
#initialize(grpc, service) ⇒ Session
constructor
A new instance of Session.
-
#insert(table, *rows, transaction_id: nil) ⇒ Time
Inserts new rows in a table.
-
#instance_id ⇒ String
The unique identifier for the instance.
-
#keepalive! ⇒ Object
Keeps the session alive by executing
"SELECT 1". - #partition_query(sql, transaction, params: nil, types: nil, partition_size_bytes: nil, max_partitions: nil) ⇒ Object
- #partition_read(table, columns, transaction, keys: nil, index: nil, partition_size_bytes: nil, max_partitions: nil) ⇒ Object
-
#path ⇒ String
The full path for the session resource.
-
#project_id ⇒ String
The unique identifier for the project.
-
#read(table, columns, keys: nil, index: nil, limit: nil, transaction: nil, partition_token: nil) ⇒ Google::Cloud::Spanner::Results
Read rows from a database table, as a simple alternative to #execute.
-
#release! ⇒ Object
Permanently deletes the session.
-
#reload! ⇒ Object
Reloads the session resource.
-
#replace(table, *rows, transaction_id: nil) ⇒ Time
Inserts or replaces rows in a table.
-
#rollback(transaction_id) ⇒ Object
Rolls back the transaction, releasing any locks it holds.
- #session ⇒ Object
-
#session_id ⇒ String
The unique identifier for the session.
-
#update(table, *rows, transaction_id: nil) ⇒ Time
Updates existing rows in a table.
-
#upsert(table, *rows, transaction_id: nil) ⇒ Time
(also: #save)
Inserts or updates rows in a table.
Constructor Details
#initialize(grpc, service) ⇒ Session
Returns a new instance of Session.
52 53 54 55 |
# File 'lib/google/cloud/spanner/session.rb', line 52 def initialize grpc, service @grpc = grpc @service = service end |
Instance Attribute Details
#grpc ⇒ Object
45 46 47 |
# File 'lib/google/cloud/spanner/session.rb', line 45 def grpc @grpc end |
#service ⇒ Object
49 50 51 |
# File 'lib/google/cloud/spanner/session.rb', line 49 def service @service end |
Class Method Details
.from_grpc(grpc, service) ⇒ Object
Google::Spanner::V1::Session.
580 581 582 |
# File 'lib/google/cloud/spanner/session.rb', line 580 def self.from_grpc grpc, service new grpc, service end |
Instance Method Details
#commit(transaction_id: nil) {|commit| ... } ⇒ Time
Creates changes to be applied to rows in the database.
296 297 298 299 300 301 302 303 304 |
# File 'lib/google/cloud/spanner/session.rb', line 296 def commit transaction_id: nil ensure_service! commit = Commit.new yield commit commit_resp = service.commit path, commit.mutations, transaction_id: transaction_id @last_updated_at = Time.now Convert. commit_resp. end |
#create_transaction ⇒ Object
Creates a new transaction object every time.
524 525 526 527 |
# File 'lib/google/cloud/spanner/session.rb', line 524 def create_transaction tx_grpc = service.begin_transaction path Transaction.from_grpc(tx_grpc, self) end |
#database_id ⇒ String
The unique identifier for the database.
71 72 73 |
# File 'lib/google/cloud/spanner/session.rb', line 71 def database_id @grpc.name.split("/")[5] end |
#delete(table, keys = [], transaction_id: nil) ⇒ Time
Deletes rows from a table. Succeeds whether or not the specified rows were present.
507 508 509 510 511 |
# File 'lib/google/cloud/spanner/session.rb', line 507 def delete table, keys = [], transaction_id: nil commit transaction_id: transaction_id do |c| c.delete table, keys end end |
#execute(sql, params: nil, types: nil, transaction: nil, partition_token: nil) ⇒ Google::Cloud::Spanner::Results
Executes a SQL query.
Arguments can be passed using params, Ruby types are mapped to
Spanner types as follows:
| Spanner | Ruby | Notes |
|---|---|---|
BOOL |
true/false |
|
INT64 |
Integer |
|
FLOAT64 |
Float |
|
STRING |
String |
|
DATE |
Date |
|
TIMESTAMP |
Time, DateTime |
|
BYTES |
File, IO, StringIO, or similar |
|
ARRAY |
Array |
Nested arrays are not supported. |
See Data types.
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/google/cloud/spanner/session.rb', line 183 def execute sql, params: nil, types: nil, transaction: nil, partition_token: nil ensure_service! results = Results.execute service, path, sql, params: params, types: types, transaction: transaction, partition_token: partition_token @last_updated_at = Time.now results end |
#idle_since?(duration) ⇒ Boolean
Determines if the session has been idle longer than the given duration.
572 573 574 575 |
# File 'lib/google/cloud/spanner/session.rb', line 572 def idle_since? duration return true if @last_updated_at.nil? Time.now > @last_updated_at + duration end |
#insert(table, *rows, transaction_id: nil) ⇒ Time
Inserts new rows in a table. If any of the rows already exist, the
write or request fails with error ALREADY_EXISTS.
390 391 392 393 394 |
# File 'lib/google/cloud/spanner/session.rb', line 390 def insert table, *rows, transaction_id: nil commit transaction_id: transaction_id do |c| c.insert table, rows end end |
#instance_id ⇒ String
The unique identifier for the instance.
65 66 67 |
# File 'lib/google/cloud/spanner/session.rb', line 65 def instance_id @grpc.name.split("/")[3] end |
#keepalive! ⇒ Object
Keeps the session alive by executing "SELECT 1".
549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/google/cloud/spanner/session.rb', line 549 def keepalive! ensure_service! execute "SELECT 1" return true rescue Google::Cloud::NotFoundError @grpc = service.create_session \ Admin::Database::V1::DatabaseAdminClient.database_path( project_id, instance_id, database_id ) return false end |
#partition_query(sql, transaction, params: nil, types: nil, partition_size_bytes: nil, max_partitions: nil) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/google/cloud/spanner/session.rb', line 243 def partition_query sql, transaction, params: nil, types: nil, partition_size_bytes: nil, max_partitions: nil ensure_service! results = service.partition_query \ path, sql, transaction, params: params, types: types, partition_size_bytes: partition_size_bytes, max_partitions: max_partitions @last_updated_at = Time.now results end |
#partition_read(table, columns, transaction, keys: nil, index: nil, partition_size_bytes: nil, max_partitions: nil) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/google/cloud/spanner/session.rb', line 257 def partition_read table, columns, transaction, keys: nil, index: nil, partition_size_bytes: nil, max_partitions: nil ensure_service! results = service.partition_read \ path, table, columns, transaction, keys: keys, index: index, partition_size_bytes: partition_size_bytes, max_partitions: max_partitions @last_updated_at = Time.now results end |
#path ⇒ String
The full path for the session resource. Values are of the form
projects/<project_id>/instances/<instance_id>/databases/<database_id>/sessions/<session_id>.
87 88 89 |
# File 'lib/google/cloud/spanner/session.rb', line 87 def path @grpc.name end |
#project_id ⇒ String
The unique identifier for the project.
59 60 61 |
# File 'lib/google/cloud/spanner/session.rb', line 59 def project_id @grpc.name.split("/")[1] end |
#read(table, columns, keys: nil, index: nil, limit: nil, transaction: nil, partition_token: nil) ⇒ Google::Cloud::Spanner::Results
Read rows from a database table, as a simple alternative to #execute.
231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/google/cloud/spanner/session.rb', line 231 def read table, columns, keys: nil, index: nil, limit: nil, transaction: nil, partition_token: nil ensure_service! results = Results.read service, path, table, columns, keys: keys, index: index, limit: limit, transaction: transaction, partition_token: partition_token @last_updated_at = Time.now results end |
#release! ⇒ Object
Permanently deletes the session.
563 564 565 566 |
# File 'lib/google/cloud/spanner/session.rb', line 563 def release! ensure_service! service.delete_session path end |
#reload! ⇒ Object
Reloads the session resource. Useful for determining if the session is still valid on the Spanner API.
532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/google/cloud/spanner/session.rb', line 532 def reload! ensure_service! @grpc = service.get_session path @last_updated_at = Time.now return self rescue Google::Cloud::NotFoundError @grpc = service.create_session \ Admin::Database::V1::DatabaseAdminClient.database_path( project_id, instance_id, database_id ) @last_updated_at = Time.now return self end |
#replace(table, *rows, transaction_id: nil) ⇒ Time
Inserts or replaces rows in a table. If any of the rows already exist,
it is deleted, and the column values provided are inserted instead.
Unlike #upsert, this means any values not explicitly written become
NULL.
480 481 482 483 484 |
# File 'lib/google/cloud/spanner/session.rb', line 480 def replace table, *rows, transaction_id: nil commit transaction_id: transaction_id do |c| c.replace table, rows end end |
#rollback(transaction_id) ⇒ Object
Rolls back the transaction, releasing any locks it holds.
515 516 517 518 519 |
# File 'lib/google/cloud/spanner/session.rb', line 515 def rollback transaction_id service.rollback path, transaction_id @last_updated_at = Time.now true end |
#session ⇒ Object
586 587 588 |
# File 'lib/google/cloud/spanner/session.rb', line 586 def session self end |
#session_id ⇒ String
The unique identifier for the session.
77 78 79 |
# File 'lib/google/cloud/spanner/session.rb', line 77 def session_id @grpc.name.split("/")[7] end |
#update(table, *rows, transaction_id: nil) ⇒ Time
Updates existing rows in a table. If any of the rows does not already
exist, the request fails with error NOT_FOUND.
434 435 436 437 438 |
# File 'lib/google/cloud/spanner/session.rb', line 434 def update table, *rows, transaction_id: nil commit transaction_id: transaction_id do |c| c.update table, rows end end |
#upsert(table, *rows, transaction_id: nil) ⇒ Time Also known as: save
Inserts or updates rows in a table. If any of the rows already exist, then its column values are overwritten with the ones provided. Any column values not explicitly written are preserved.
345 346 347 348 349 |
# File 'lib/google/cloud/spanner/session.rb', line 345 def upsert table, *rows, transaction_id: nil commit transaction_id: transaction_id do |c| c.upsert table, rows end end |