Class: Google::Cloud::Spanner::Client
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::Client
- Defined in:
- lib/google/cloud/spanner/client.rb
Overview
# Client
A client is used to read and/or modify data in a Cloud Spanner database.
See Project#client.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the client connection and releases resources.
-
#commit {|commit| ... } ⇒ Time
Creates and commits a transaction for writes that execute atomically at a single logical point in time across columns, rows, and tables in a database.
-
#create_new_session ⇒ Object
Creates a new session object every time.
-
#database ⇒ Database
The Spanner database connected to.
-
#database_id ⇒ String
The unique identifier for the database.
-
#delete(table, keys = []) ⇒ Time
Deletes rows from a table.
-
#execute(sql, params: nil, types: nil, single_use: nil) ⇒ Google::Cloud::Spanner::Results
(also: #query)
Executes a SQL query.
-
#fields(types) ⇒ Fields
Creates fields object from types.
-
#fields_for(table) ⇒ Fields
Executes a query to retrieve the field names and types for a table.
-
#initialize(project, instance_id, database_id, opts = {}) ⇒ Client
constructor
A new instance of Client.
-
#insert(table, *rows) ⇒ Time
Inserts new rows in a table.
- #inspect ⇒ Object
-
#instance ⇒ Instance
The Spanner instance connected to.
-
#instance_id ⇒ String
The unique identifier for the instance.
-
#project ⇒ Project
The Spanner project connected to.
-
#project_id ⇒ String
The unique identifier for the project.
-
#range(beginning, ending, exclude_begin: false, exclude_end: false) ⇒ Google::Cloud::Spanner::Range
Creates a Spanner Range.
-
#read(table, columns, keys: nil, index: nil, limit: nil, single_use: nil) ⇒ Google::Cloud::Spanner::Results
Read rows from a database table, as a simple alternative to #execute.
-
#replace(table, *rows) ⇒ Time
Inserts or replaces rows in a table.
-
#snapshot(strong: nil, timestamp: nil, read_timestamp: nil, staleness: nil, exact_staleness: nil) {|snapshot| ... } ⇒ Object
Creates a snapshot read-only transaction for reads that execute atomically at a single logical point in time across columns, rows, and tables in a database.
- #to_s ⇒ Object
-
#transaction(deadline: 120) {|transaction| ... } ⇒ Time
Creates a transaction for reads and writes that execute atomically at a single logical point in time across columns, rows, and tables in a database.
-
#update(table, *rows) ⇒ Time
Updates existing rows in a table.
-
#upsert(table, *rows) ⇒ Time
(also: #save)
Inserts or updates rows in a table.
Constructor Details
#initialize(project, instance_id, database_id, opts = {}) ⇒ Client
Returns a new instance of Client.
54 55 56 57 58 59 |
# File 'lib/google/cloud/spanner/client.rb', line 54 def initialize project, instance_id, database_id, opts = {} @project = project @instance_id = instance_id @database_id = database_id @pool = Pool.new self, opts end |
Instance Method Details
#close ⇒ Object
Closes the client connection and releases resources.
937 938 939 |
# File 'lib/google/cloud/spanner/client.rb', line 937 def close @pool.close end |
#commit {|commit| ... } ⇒ Time
Creates and commits a transaction for writes that execute atomically at a single logical point in time across columns, rows, and tables in a database.
All changes are accumulated in memory until the block completes. Unlike #transaction, which can also perform reads, this operation accepts only mutations and makes a single API request.
Note: This method does not feature replay protection present in Transaction#commit (See #transaction). This method makes a single RPC, whereas Transaction#commit requires two RPCs (one of which may be performed in advance), and so this method may be appropriate for latency sensitive and/or high throughput blind changes.
656 657 658 659 660 661 662 |
# File 'lib/google/cloud/spanner/client.rb', line 656 def commit &block fail ArgumentError, "Must provide a block" unless block_given? @pool.with_session do |session| session.commit(&block) end end |
#create_new_session ⇒ Object
Creates a new session object every time.
944 945 946 947 948 949 950 |
# File 'lib/google/cloud/spanner/client.rb', line 944 def create_new_session ensure_service! grpc = @project.service.create_session \ Admin::Database::V1::DatabaseAdminClient.database_path( project_id, instance_id, database_id) Session.from_grpc(grpc, @project.service) end |
#database ⇒ Database
The Spanner database connected to.
93 94 95 |
# File 'lib/google/cloud/spanner/client.rb', line 93 def database @project.database instance_id, database_id end |
#database_id ⇒ String
The unique identifier for the database.
75 76 77 |
# File 'lib/google/cloud/spanner/client.rb', line 75 def database_id @database_id end |
#delete(table, keys = []) ⇒ Time
Deletes rows from a table. Succeeds whether or not the specified rows were present.
Changes are made immediately upon calling this method using a single-use transaction. To make multiple changes in the same single-use transaction use #commit. To make changes in a transaction that supports reads and automatic retry protection use #transaction.
Note: This method does not feature replay protection present in Transaction#delete (See #transaction). This method makes a single RPC, whereas Transaction#delete requires two RPCs (one of which may be performed in advance), and so this method may be appropriate for latency sensitive and/or high throughput blind deletions.
618 619 620 621 622 |
# File 'lib/google/cloud/spanner/client.rb', line 618 def delete table, keys = [] @pool.with_session do |session| session.delete table, keys end end |
#execute(sql, params: nil, types: nil, single_use: nil) ⇒ Google::Cloud::Spanner::Results Also known as: query
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](cloud.google.com/spanner/docs/data-definition-language#data_types).
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/google/cloud/spanner/client.rb', line 236 def execute sql, params: nil, types: nil, single_use: nil validate_single_use_args! single_use ensure_service! single_use_tx = single_use_transaction single_use results = nil @pool.with_session do |session| results = session.execute \ sql, params: params, types: types, transaction: single_use_tx end results end |
#fields(types) ⇒ Fields
Creates fields object from types.
871 872 873 |
# File 'lib/google/cloud/spanner/client.rb', line 871 def fields types Fields.new types end |
#fields_for(table) ⇒ Fields
Executes a query to retrieve the field names and types for a table.
896 897 898 |
# File 'lib/google/cloud/spanner/client.rb', line 896 def fields_for table execute("SELECT * FROM #{table} WHERE 1 = 0").fields end |
#insert(table, *rows) ⇒ Time
Inserts new rows in a table. If any of the rows already exist, the write or request fails with AlreadyExistsError.
Changes are made immediately upon calling this method using a single-use transaction. To make multiple changes in the same single-use transaction use #commit. To make changes in a transaction that supports reads and automatic retry protection use #transaction.
Note: This method does not feature replay protection present in Transaction#insert (See #transaction). This method makes a single RPC, whereas Transaction#insert requires two RPCs (one of which may be performed in advance), and so this method may be appropriate for latency sensitive and/or high throughput blind inserts.
468 469 470 471 472 |
# File 'lib/google/cloud/spanner/client.rb', line 468 def insert table, *rows @pool.with_session do |session| session.insert table, rows end end |
#inspect ⇒ Object
959 960 961 |
# File 'lib/google/cloud/spanner/client.rb', line 959 def inspect "#<#{self.class.name} #{self}>" end |
#instance ⇒ Instance
The Spanner instance connected to.
87 88 89 |
# File 'lib/google/cloud/spanner/client.rb', line 87 def instance @project.instance instance_id end |
#instance_id ⇒ String
The unique identifier for the instance.
69 70 71 |
# File 'lib/google/cloud/spanner/client.rb', line 69 def instance_id @instance_id end |
#project ⇒ Project
The Spanner project connected to.
81 82 83 |
# File 'lib/google/cloud/spanner/client.rb', line 81 def project @project end |
#project_id ⇒ String
The unique identifier for the project.
63 64 65 |
# File 'lib/google/cloud/spanner/client.rb', line 63 def project_id @project.service.project end |
#range(beginning, ending, exclude_begin: false, exclude_end: false) ⇒ Google::Cloud::Spanner::Range
Creates a Spanner Range. This can be used in place of a Ruby Range when needing to exclude the beginning value.
928 929 930 931 932 |
# File 'lib/google/cloud/spanner/client.rb', line 928 def range beginning, ending, exclude_begin: false, exclude_end: false Range.new beginning, ending, exclude_begin: exclude_begin, exclude_end: exclude_end end |
#read(table, columns, keys: nil, index: nil, limit: nil, single_use: nil) ⇒ Google::Cloud::Spanner::Results
Read rows from a database table, as a simple alternative to #execute.
347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/google/cloud/spanner/client.rb', line 347 def read table, columns, keys: nil, index: nil, limit: nil, single_use: nil validate_single_use_args! single_use ensure_service! single_use_tx = single_use_transaction single_use results = nil @pool.with_session do |session| results = session.read \ table, columns, keys: keys, index: index, limit: limit, transaction: single_use_tx end results end |
#replace(table, *rows) ⇒ 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`.
Changes are made immediately upon calling this method using a single-use transaction. To make multiple changes in the same single-use transaction use #commit. To make changes in a transaction that supports reads and automatic retry protection use #transaction.
Note: This method does not feature replay protection present in Transaction#replace (See #transaction). This method makes a single RPC, whereas Transaction#replace requires two RPCs (one of which may be performed in advance), and so this method may be appropriate for latency sensitive and/or high throughput blind replaces.
580 581 582 583 584 |
# File 'lib/google/cloud/spanner/client.rb', line 580 def replace table, *rows @pool.with_session do |session| session.replace table, rows end end |
#snapshot(strong: nil, timestamp: nil, read_timestamp: nil, staleness: nil, exact_staleness: nil) {|snapshot| ... } ⇒ Object
Creates a snapshot read-only transaction for reads that execute atomically at a single logical point in time across columns, rows, and tables in a database. For transactions that only read, snapshot read-only transactions provide simpler semantics and are almost always faster than read-write transactions.
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 |
# File 'lib/google/cloud/spanner/client.rb', line 823 def snapshot strong: nil, timestamp: nil, read_timestamp: nil, staleness: nil, exact_staleness: nil validate_snapshot_args! strong: strong, timestamp: , read_timestamp: , staleness: staleness, exact_staleness: exact_staleness ensure_service! unless Thread.current[:transaction_id].nil? fail "Nested snapshots are not allowed" end @pool.with_session do |session| begin snp_grpc = @project.service.create_snapshot \ session.path, strong: strong, timestamp: ( || ), staleness: (staleness || exact_staleness) Thread.current[:transaction_id] = snp_grpc.id snp = Snapshot.from_grpc(snp_grpc, session) yield snp if block_given? ensure Thread.current[:transaction_id] = nil end end nil end |
#to_s ⇒ Object
953 954 955 956 |
# File 'lib/google/cloud/spanner/client.rb', line 953 def to_s "(project_id: #{project_id}, instance_id: #{instance_id}, " \ "database_id: #{database_id})" end |
#transaction(deadline: 120) {|transaction| ... } ⇒ Time
Creates a transaction for reads and writes that execute atomically at a single logical point in time across columns, rows, and tables in a database.
The transaction will always commit unless an error is raised. If the error raised is Rollback the transaction method will return without passing on the error. All other errors will be passed on.
All changes are accumulated in memory until the block completes. Transactions will be automatically retried when possible, until ‘deadline` is reached. This operation makes separate API requests to begin and commit the transaction.
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 |
# File 'lib/google/cloud/spanner/client.rb', line 724 def transaction deadline: 120, &block ensure_service! unless Thread.current[:transaction_id].nil? fail "Nested transactions are not allowed" end deadline = validate_deadline deadline backoff = 1.0 start_time = current_time @pool.with_transaction do |tx| begin Thread.current[:transaction_id] = tx.transaction_id block.call tx commit_resp = @project.service.commit \ tx.session.path, tx.mutations, transaction_id: tx.transaction_id return Convert. commit_resp. rescue GRPC::Aborted, Google::Cloud::AbortedError => err # Re-raise if deadline has passed if current_time - start_time > deadline if err.is_a? GRPC::BadStatus err = Google::Cloud::Error.from_error err end raise err end # Sleep the amount from RetryDelay, or incremental backoff sleep(delay_from_aborted(err) || backoff *= 1.3) # Create new transaction on the session and retry the block tx = tx.session.create_transaction retry rescue => err # Rollback transaction when handling unexpected error tx.session.rollback tx.transaction_id # Return nil if raised with rollback. return nil if err.is_a? Rollback # Re-raise error. raise err ensure Thread.current[:transaction_id] = nil end end end |
#update(table, *rows) ⇒ Time
Updates existing rows in a table. If any of the rows does not already exist, the request fails with NotFoundError.
Changes are made immediately upon calling this method using a single-use transaction. To make multiple changes in the same single-use transaction use #commit. To make changes in a transaction that supports reads and automatic retry protection use #transaction.
Note: This method does not feature replay protection present in Transaction#update (See #transaction). This method makes a single RPC, whereas Transaction#update requires two RPCs (one of which may be performed in advance), and so this method may be appropriate for latency sensitive and/or high throughput blind updates.
523 524 525 526 527 |
# File 'lib/google/cloud/spanner/client.rb', line 523 def update table, *rows @pool.with_session do |session| session.update table, rows end end |
#upsert(table, *rows) ⇒ 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.
Changes are made immediately upon calling this method using a single-use transaction. To make multiple changes in the same single-use transaction use #commit. To make changes in a transaction that supports reads and automatic retry protection use #transaction.
Note: This method does not feature replay protection present in Transaction#upsert (See #transaction). This method makes a single RPC, whereas Transaction#upsert requires two RPCs (one of which may be performed in advance), and so this method may be appropriate for latency sensitive and/or high throughput blind upserts.
412 413 414 415 416 |
# File 'lib/google/cloud/spanner/client.rb', line 412 def upsert table, *rows @pool.with_session do |session| session.upsert table, rows end end |