Class: Google::Cloud::Spanner::Client
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::Client
- Defined in:
- lib/google/cloud/spanner/client.rb
Overview
Instance Method Summary collapse
-
#batch_write(exclude_txn_from_change_streams: false, request_options: nil, call_options: nil) {|batch_write| ... } ⇒ Google::Cloud::Spanner::BatchWriteResults
Batches the supplied mutation groups in a collection of efficient transactions.
-
#close ⇒ Object
Closes the client connection and releases resources.
-
#commit(exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil) {|commit| ... } ⇒ Time, CommitResponse
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.
-
#commit_timestamp ⇒ ColumnValue
Creates a column value object representing setting a field's value to the timestamp of the commit.
-
#database ⇒ Database
The Spanner database connected to.
-
#database_id ⇒ String
The unique identifier for the database.
-
#database_role ⇒ String
The Spanner session creator role.
-
#delete(table, keys = [], exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil) ⇒ Time, CommitResponse
Deletes rows from a table.
-
#directed_read_options ⇒ Hash
A hash of values to specify the custom directed read options for executing SQL query.
-
#execute_partition_update(sql, params: nil, types: nil, exclude_txn_from_change_streams: false, query_options: nil, request_options: nil, call_options: nil) ⇒ Integer
(also: #execute_pdml)
Executes a Partitioned DML SQL statement.
-
#execute_query(sql, params: nil, types: nil, single_use: nil, query_options: nil, request_options: nil, call_options: nil, directed_read_options: nil) ⇒ Google::Cloud::Spanner::Results
(also: #execute, #query, #execute_sql)
Executes a SQL query.
-
#fields(types) ⇒ Fields
Creates a configuration object (Fields) that may be provided to queries or used to create STRUCT objects.
-
#insert(table, rows, exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil) ⇒ Time, CommitResponse
Inserts new rows in a table.
-
#instance ⇒ Instance
The Spanner instance connected to.
-
#instance_id ⇒ String
The unique identifier for the instance.
-
#project ⇒ ::Google::Cloud::Spanner::Project
The Spanner project connected to.
-
#project_id ⇒ String
The unique identifier for the project.
-
#query_options ⇒ Hash
A hash of values to specify the custom query options for executing SQL query.
-
#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, request_options: nil, call_options: nil, directed_read_options: nil, order_by: nil, lock_hint: nil) ⇒ Google::Cloud::Spanner::Results
Read rows from a database table, as a simple alternative to #execute_query.
-
#replace(table, rows, exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil) ⇒ Time, CommitResponse
Inserts or replaces rows in a table.
-
#reset! ⇒ Object
Reset the client sessions.
-
#snapshot(strong: nil, timestamp: nil, read_timestamp: nil, staleness: nil, exact_staleness: nil, call_options: 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.
-
#transaction(deadline: 120, exclude_txn_from_change_streams: false, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil, isolation_level: nil) {|transaction| ... } ⇒ Time, CommitResponse
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, exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil) ⇒ Time, CommitResponse
Updates existing rows in a table.
-
#upsert(table, rows, exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil) ⇒ Time, CommitResponse
(also: #save)
Inserts or updates rows in a table.
Instance Method Details
#batch_write(exclude_txn_from_change_streams: false, request_options: nil, call_options: nil) {|batch_write| ... } ⇒ Google::Cloud::Spanner::BatchWriteResults
Batches the supplied mutation groups in a collection of efficient transactions.
All mutations in a group are committed atomically. However, mutations across groups can be committed non-atomically in an unspecified order and thus they must be independent of each other. Partial failure is possible, i.e., some groups may have been committed successfully, while others may have failed. The results of individual batches are streamed into the response as the batches are applied.
BatchWrite requests are not replay protected, meaning that each mutation group may be applied more than once. Replays of non-idempotent mutations may have undesirable effects. For example, replays of an insert mutation may produce an already exists error or if you use generated or commit timestamp-based keys, it may result in additional rows being added to the mutation's table. We recommend structuring your mutation groups to be idempotent to avoid this issue.
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 |
# File 'lib/google/cloud/spanner/client.rb', line 2083 def batch_write exclude_txn_from_change_streams: false, request_options: nil, call_options: nil, &block raise ArgumentError, "Must provide a block" unless block_given? @pool.with_session do |session| session.batch_write( exclude_txn_from_change_streams: exclude_txn_from_change_streams, request_options: , call_options: , &block ) end end |
#close ⇒ Object
Closes the client connection and releases resources.
2643 2644 2645 |
# File 'lib/google/cloud/spanner/client.rb', line 2643 def close @pool.close end |
#commit(exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil) {|commit| ... } ⇒ Time, CommitResponse
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. This method makes a single RPC, whereas #transaction 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.
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 |
# File 'lib/google/cloud/spanner/client.rb', line 1980 def commit exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil, &block raise ArgumentError, "Must provide a block" unless block_given? = Convert. \ , tag_type: :transaction_tag @pool.with_session do |session| session.commit( exclude_txn_from_change_streams: exclude_txn_from_change_streams, isolation_level: isolation_level || @isolation_level, commit_options: , request_options: , call_options: , read_lock_mode: read_lock_mode || @read_lock_mode, &block ) end end |
#commit_timestamp ⇒ ColumnValue
Creates a column value object representing setting a field's value to the timestamp of the commit. (See Google::Cloud::Spanner::ColumnValue.commit_timestamp)
This placeholder value can only be used for timestamp columns that have set the option "(allow_commit_timestamp=true)" in the schema.
2636 2637 2638 |
# File 'lib/google/cloud/spanner/client.rb', line 2636 def ColumnValue. end |
#database ⇒ Database
The Spanner database connected to.
149 150 151 |
# File 'lib/google/cloud/spanner/client.rb', line 149 def database @project.database instance_id, database_id end |
#database_id ⇒ String
The unique identifier for the database.
131 132 133 |
# File 'lib/google/cloud/spanner/client.rb', line 131 def database_id @database_id end |
#database_role ⇒ String
The Spanner session creator role.
155 156 157 |
# File 'lib/google/cloud/spanner/client.rb', line 155 def database_role @database_role end |
#delete(table, keys = [], exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil) ⇒ Time, CommitResponse
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.
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 |
# File 'lib/google/cloud/spanner/client.rb', line 1833 def delete table, keys = [], exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil = Convert. \ , tag_type: :transaction_tag @pool.with_session do |session| session.delete table, keys, exclude_txn_from_change_streams: exclude_txn_from_change_streams, isolation_level: isolation_level || @isolation_level, commit_options: , request_options: , call_options: , read_lock_mode: read_lock_mode || @read_lock_mode end end |
#directed_read_options ⇒ Hash
A hash of values to specify the custom directed read options for executing SQL query.
169 170 171 |
# File 'lib/google/cloud/spanner/client.rb', line 169 def @directed_read_options end |
#execute_partition_update(sql, params: nil, types: nil, exclude_txn_from_change_streams: false, query_options: nil, request_options: nil, call_options: nil) ⇒ Integer Also known as: execute_pdml
Executes a Partitioned DML SQL statement.
Partitioned DML is an alternate implementation with looser semantics to enable large-scale changes without running into transaction size limits or (accidentally) locking the entire table in one large transaction. At a high level, it partitions the keyspace and executes the statement on each partition in separate internal transactions.
Partitioned DML does not guarantee database-wide atomicity of the statement - it guarantees row-based atomicity, which includes updates to any indices. Additionally, it does not guarantee that it will execute exactly one time against each row - it guarantees "at least once" semantics.
Where DML statements must be executed using Transaction (see Transaction#execute_update), Partitioned DML statements are executed outside of a read/write transaction.
Not all DML statements can be executed in the Partitioned DML mode and the backend will return an error for the statements which are not supported.
DML statements must be fully-partitionable. Specifically, the statement must be expressible as the union of many statements which each access only a single row of the table. InvalidArgumentError is raised if the statement does not qualify.
The method will block until the update is complete. Running a DML statement with this method does not offer exactly once semantics, and therefore the DML statement should be idempotent. The DML statement must be fully-partitionable. Specifically, the statement must be expressible as the union of many statements which each access only a single row of the table. This is a Partitioned DML transaction in which a single Partitioned DML statement is executed. Partitioned DML partitions the and runs the DML statement over each partition in parallel using separate, internal transactions that commit independently. Partitioned DML transactions do not need to be committed.
Partitioned DML updates are used to execute a single DML statement with a different execution strategy that provides different, and often better, scalability properties for large, table-wide operations than DML in a Transaction#execute_update transaction. Smaller scoped statements, such as an OLTP workload, should prefer using Transaction#execute_update.
That said, Partitioned DML is not a drop-in replacement for standard DML used in Transaction#execute_update.
- The DML statement must be fully-partitionable. Specifically, the statement must be expressible as the union of many statements which each access only a single row of the table.
- The statement is not applied atomically to all rows of the table. Rather, the statement is applied atomically to partitions of the table, in independent internal transactions. Secondary index rows are updated atomically with the base table rows.
- Partitioned DML does not guarantee exactly-once execution semantics
against a partition. The statement will be applied at least once to
each partition. It is strongly recommended that the DML statement
should be idempotent to avoid unexpected results. For instance, it
is potentially dangerous to run a statement such as
UPDATE table SET column = column + 1as it could be run multiple times against some rows. - The partitions are committed automatically - there is no support for Commit or Rollback. If the call returns an error, or if the client issuing the DML statement dies, it is possible that some rows had the statement executed on them successfully. It is also possible that statement was never executed against other rows.
- If any error is encountered during the execution of the partitioned DML operation (for instance, a UNIQUE INDEX violation, division by zero, or a value that cannot be stored due to schema constraints), then the operation is stopped at that point and an error is returned. It is possible that at this point, some partitions have been committed (or even committed multiple times), and other partitions have not been run at all.
Given the above, Partitioned DML is good fit for large, database-wide, operations that are idempotent, such as deleting old rows from a very large table.
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 |
# File 'lib/google/cloud/spanner/client.rb', line 789 def execute_partition_update sql, params: nil, types: nil, exclude_txn_from_change_streams: false, query_options: nil, request_options: nil, call_options: nil ensure_service! params, types = Convert.to_input_params_and_types params, types = Convert. , tag_type: :request_tag route_to_leader = LARHeaders.partition_query results = nil @pool.with_session do |session| transaction = pdml_transaction session, exclude_txn_from_change_streams: exclude_txn_from_change_streams results = session.execute_query \ sql, params: params, types: types, transaction: transaction, query_options: , request_options: , call_options: , route_to_leader: route_to_leader end # Stream all PartialResultSet to get ResultSetStats results.rows.to_a # Raise an error if there is not a row count returned if results.row_count.nil? raise Google::Cloud::InvalidArgumentError, "Partitioned DML statement is invalid." end results.row_count end |
#execute_query(sql, params: nil, types: nil, single_use: nil, query_options: nil, request_options: nil, call_options: nil, directed_read_options: nil) ⇒ Google::Cloud::Spanner::Results Also known as: execute, query, execute_sql
Executes a SQL query.
The following settings can be provided:
:exclude_replicas(Hash) Exclude_replicas indicates what replicas should be excluded from serving requests. Spanner will not route requests to the replicas in this list.:include_replicas(Hash) Include_replicas indicates the order of replicas to process the request. If auto_failover_disabled is set to true and all replicas are exhausted without finding a healthy replica, Spanner will wait for a replica in the list to become available, requests may fail due to DEADLINE_EXCEEDED errors.
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/google/cloud/spanner/client.rb', line 509 def execute_query sql, params: nil, types: nil, single_use: nil, query_options: nil, request_options: nil, call_options: nil, directed_read_options: nil validate_single_use_args! single_use ensure_service! params, types = Convert.to_input_params_and_types params, types = Convert. , tag_type: :request_tag single_use_tx = single_use_transaction single_use route_to_leader = LARHeaders.execute_query false results = nil @pool.with_session do |session| results = session.execute_query \ sql, params: params, types: types, transaction: single_use_tx, query_options: , request_options: , call_options: , directed_read_options: || @directed_read_options, route_to_leader: route_to_leader end results end |
#fields(types) ⇒ Fields
Creates a configuration object (Fields) that may be provided to queries or used to create STRUCT objects. (The STRUCT will be represented by the Data class.) See #execute and/or Fields#struct.
For more information, see Data Types - Constructing a STRUCT.
2548 2549 2550 |
# File 'lib/google/cloud/spanner/client.rb', line 2548 def fields types Fields.new types end |
#insert(table, rows, exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil) ⇒ Time, CommitResponse
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.
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 |
# File 'lib/google/cloud/spanner/client.rb', line 1364 def insert table, rows, exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil = Convert. \ , tag_type: :transaction_tag @pool.with_session do |session| session.insert table, rows, exclude_txn_from_change_streams: exclude_txn_from_change_streams, isolation_level: isolation_level || @isolation_level, commit_options: , request_options: , call_options: , read_lock_mode: read_lock_mode || @read_lock_mode end end |
#instance ⇒ Instance
The Spanner instance connected to.
143 144 145 |
# File 'lib/google/cloud/spanner/client.rb', line 143 def instance @project.instance instance_id end |
#instance_id ⇒ String
The unique identifier for the instance.
125 126 127 |
# File 'lib/google/cloud/spanner/client.rb', line 125 def instance_id @instance_id end |
#project ⇒ ::Google::Cloud::Spanner::Project
The Spanner project connected to.
137 138 139 |
# File 'lib/google/cloud/spanner/client.rb', line 137 def project @project end |
#project_id ⇒ String
The unique identifier for the project.
119 120 121 |
# File 'lib/google/cloud/spanner/client.rb', line 119 def project_id @project.service.project end |
#query_options ⇒ Hash
A hash of values to specify the custom query options for executing SQL query.
162 163 164 |
# File 'lib/google/cloud/spanner/client.rb', line 162 def @query_options 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.
2605 2606 2607 2608 2609 |
# File 'lib/google/cloud/spanner/client.rb', line 2605 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, request_options: nil, call_options: nil, directed_read_options: nil, order_by: nil, lock_hint: nil) ⇒ Google::Cloud::Spanner::Results
Read rows from a database table, as a simple alternative to #execute_query.
The following settings can be provided:
:exclude_replicas(Hash) Exclude_replicas indicates what replicas should be excluded from serving requests. Spanner will not route requests to the replicas in this list.:include_replicas(Hash) Include_replicas indicates the order of replicas to process the request. If auto_failover_disabled is set to true and all replicas are exhausted without finding a healthy replica, Spanner will wait for a replica in the list to become available, requests may fail due to DEADLINE_EXCEEDED errors.
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 |
# File 'lib/google/cloud/spanner/client.rb', line 1019 def read table, columns, keys: nil, index: nil, limit: nil, single_use: nil, request_options: nil, call_options: nil, directed_read_options: nil, order_by: nil, lock_hint: nil validate_single_use_args! single_use ensure_service! columns = Array(columns).map(&:to_s) keys = Convert.to_key_set keys single_use_tx = single_use_transaction single_use route_to_leader = LARHeaders.read false = Convert. , tag_type: :request_tag results = nil @pool.with_session do |session| results = session.read \ table, columns, keys: keys, index: index, limit: limit, transaction: single_use_tx, request_options: , call_options: , directed_read_options: || @directed_read_options, route_to_leader: route_to_leader, order_by: order_by, lock_hint: lock_hint end results end |
#replace(table, rows, exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil) ⇒ Time, CommitResponse
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.
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 |
# File 'lib/google/cloud/spanner/client.rb', line 1696 def replace table, rows, exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil @pool.with_session do |session| session.replace table, rows, exclude_txn_from_change_streams: exclude_txn_from_change_streams, isolation_level: isolation_level || @isolation_level, commit_options: , request_options: , call_options: , read_lock_mode: read_lock_mode || @read_lock_mode end end |
#reset! ⇒ Object
Reset the client sessions.
2650 2651 2652 |
# File 'lib/google/cloud/spanner/client.rb', line 2650 def reset! @pool.reset! end |
#snapshot(strong: nil, timestamp: nil, read_timestamp: nil, staleness: nil, exact_staleness: nil, call_options: 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.
2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 |
# File 'lib/google/cloud/spanner/client.rb', line 2451 def snapshot strong: nil, timestamp: nil, read_timestamp: nil, staleness: nil, exact_staleness: nil, call_options: nil validate_snapshot_args! strong: strong, timestamp: , read_timestamp: , staleness: staleness, exact_staleness: exact_staleness ensure_service! unless Thread.current[IS_TRANSACTION_RUNNING_KEY].nil? raise "Nested snapshots are not allowed" end @pool.with_session do |session| snp_grpc = @project.service.create_snapshot \ session.path, strong: strong, timestamp: || , staleness: staleness || exact_staleness, call_options: Thread.current[IS_TRANSACTION_RUNNING_KEY] = true snp = Snapshot.from_grpc snp_grpc, session, directed_read_options: @directed_read_options yield snp if block_given? ensure Thread.current[IS_TRANSACTION_RUNNING_KEY] = nil end nil end |
#transaction(deadline: 120, exclude_txn_from_change_streams: false, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil, isolation_level: nil) {|transaction| ... } ⇒ Time, CommitResponse
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.
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 |
# File 'lib/google/cloud/spanner/client.rb', line 2282 def transaction deadline: 120, exclude_txn_from_change_streams: false, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil, isolation_level: nil ensure_service! unless Thread.current[IS_TRANSACTION_RUNNING_KEY].nil? raise "Nested transactions are not allowed" end deadline = validate_deadline deadline backoff = 1.0 start_time = current_time = Convert. \ , tag_type: :transaction_tag @pool.with_session do |session| transaction_tag = [:transaction_tag] if tx = session.create_empty_transaction \ exclude_txn_from_change_streams: exclude_txn_from_change_streams, read_lock_mode: read_lock_mode || @read_lock_mode, transaction_tag: transaction_tag, isolation_level: isolation_level || @isolation_level begin Thread.current[IS_TRANSACTION_RUNNING_KEY] = true yield tx unless tx.existing_transaction? # This typically will happen if the yielded `tx` object was only used to add mutations. # Then it never called any RPCs and didn't create a server-side Transaction object. # In which case we should make an explicit BeginTransaction call here. tx.safe_begin_transaction!( exclude_from_change_streams: exclude_txn_from_change_streams, request_options: , call_options: , read_lock_mode: read_lock_mode ) end transaction_id = tx.transaction_id # This "inner retry" mechanism is for Commit Response protocol. # Unlike the retry on `Aborted` errors it will not re-create a transaction. # This is intentional, as these retries are not related to e.g. # transactions deadlocking, so it's OK to retry "as-is". should_retry = true while should_retry commit_resp = @project.service.commit( tx.session.path, tx.mutations, transaction_id: transaction_id, exclude_txn_from_change_streams: exclude_txn_from_change_streams, commit_options: , request_options: , call_options: , precommit_token: tx.precommit_token, read_lock_mode: read_lock_mode ) tx.precommit_token = commit_resp.precommit_token should_retry = !commit_resp.precommit_token.nil? end resp = CommitResponse.from_grpc commit_resp ? resp : resp. rescue GRPC::Aborted, Google::Cloud::AbortedError, GRPC::Internal, Google::Cloud::InternalError => e check_and_propagate_err! e, (current_time - start_time > deadline) # Sleep the amount from RetryDelay, or incremental backoff sleep(delay_from_aborted(e) || backoff *= 1.3) # Create new transaction on the session and retry the block previous_transaction_id = tx.transaction_id if tx.existing_transaction? tx = session.create_empty_transaction( exclude_txn_from_change_streams: exclude_txn_from_change_streams, previous_transaction_id: previous_transaction_id, read_lock_mode: read_lock_mode, transaction_tag: transaction_tag ) retry rescue StandardError => e # Rollback transaction when handling unexpected error tx.session.rollback tx.transaction_id if tx.existing_transaction? # Return nil if raised with rollback. return nil if e.is_a? Rollback # Re-raise error. raise e ensure Thread.current[IS_TRANSACTION_RUNNING_KEY] = nil end end end |
#update(table, rows, exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil) ⇒ Time, CommitResponse
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.
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 |
# File 'lib/google/cloud/spanner/client.rb', line 1529 def update table, rows, exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil = Convert. \ , tag_type: :transaction_tag @pool.with_session do |session| session.update table, rows, exclude_txn_from_change_streams: exclude_txn_from_change_streams, isolation_level: isolation_level || @isolation_level, commit_options: , request_options: , call_options: , read_lock_mode: read_lock_mode || @read_lock_mode end end |
#upsert(table, rows, exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil) ⇒ Time, CommitResponse 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.
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 |
# File 'lib/google/cloud/spanner/client.rb', line 1196 def upsert table, rows, exclude_txn_from_change_streams: false, isolation_level: nil, commit_options: nil, request_options: nil, call_options: nil, read_lock_mode: nil = Convert. \ , tag_type: :transaction_tag @pool.with_session do |session| session.upsert table, rows, exclude_txn_from_change_streams: exclude_txn_from_change_streams, isolation_level: isolation_level || @isolation_level, commit_options: , request_options: , call_options: , read_lock_mode: read_lock_mode || @read_lock_mode end end |