Class: Google::Cloud::Spanner::BatchSnapshot
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::BatchSnapshot
- Defined in:
- lib/google/cloud/spanner/batch_snapshot.rb
Overview
BatchSnapshot
Represents a read-only transaction that can be configured to read at timestamps in the past and allows for exporting arbitrarily large amounts of data from Cloud Spanner databases. This is a snapshot which additionally allows to partition a read or query request. The read/query request can then be executed independently over each partition while observing the same snapshot of the database. A BatchSnapshot can also be shared across multiple processes/machines by passing around its serialized value and then recreating the transaction using #dump.
Unlike locking read-write transactions, BatchSnapshot will never abort. They can fail if the chosen read timestamp is garbage collected; however any read or query activity within an hour on the transaction avoids garbage collection and most applications do not need to worry about this in practice.
See Google::Cloud::Spanner::BatchClient#batch_snapshot and Google::Cloud::Spanner::BatchClient#load_batch_snapshot.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the batch snapshot and releases the underlying resources.
-
#dump ⇒ String
(also: #serialize)
Serializes the batch snapshot object so it can be recreated on another process.
-
#execute_partition(partition, call_options: nil) ⇒ Object
Execute the partition to return a Results.
-
#execute_query(sql, params: nil, types: nil, query_options: nil, call_options: nil, directed_read_options: nil) ⇒ Google::Cloud::Spanner::Results
(also: #execute, #query, #execute_sql)
Executes a SQL query.
-
#partition_query(sql, params: nil, types: nil, partition_size_bytes: nil, max_partitions: nil, query_options: nil, call_options: nil, data_boost_enabled: false, directed_read_options: nil) ⇒ Array<Google::Cloud::Spanner::Partition>
Returns a list of Partition objects to execute a batch query against a database.
-
#partition_read(table, columns, keys: nil, index: nil, partition_size_bytes: nil, max_partitions: nil, call_options: nil, data_boost_enabled: false, directed_read_options: nil) ⇒ Array<Google::Cloud::Spanner::Partition>
Returns a list of Partition objects to read zero or more rows from a database.
-
#read(table, columns, keys: nil, index: nil, limit: nil, call_options: nil, directed_read_options: nil) ⇒ Google::Cloud::Spanner::Results
Read rows from a database table, as a simple alternative to #execute_query.
-
#timestamp ⇒ Time
The read timestamp chosen for batch snapshot.
-
#transaction_id ⇒ String
Identifier of the batch snapshot transaction.
Instance Method Details
#close ⇒ Object
Closes the batch snapshot and releases the underlying resources.
This should only be called once the batch snapshot is no longer needed anywhere. In particular if this batch snapshot is being used across multiple machines, calling this method on any of the machines will render the batch snapshot invalid everywhere.
454 455 456 457 458 |
# File 'lib/google/cloud/spanner/batch_snapshot.rb', line 454 def close ensure_session! session.release! end |
#dump ⇒ String Also known as: serialize
Serializes the batch snapshot object so it can be recreated on another process. See Google::Cloud::Spanner::BatchClient#load_batch_snapshot.
838 839 840 |
# File 'lib/google/cloud/spanner/batch_snapshot.rb', line 838 def dump JSON.dump to_h end |
#execute_partition(partition, call_options: nil) ⇒ Object
Execute the partition to return a Results. The result returned could be zero or more rows. The row metadata may be absent if no rows are returned.
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/google/cloud/spanner/batch_snapshot.rb', line 414 def execute_partition partition, call_options: nil ensure_session! partition = Partition.load partition unless partition.is_a? Partition # TODO: raise if partition.empty? # TODO: raise if session.path != partition.session # TODO: raise if grpc.transaction != partition.transaction opts = { call_options: } if partition.execute? execute_partition_query partition, **opts elsif partition.read? execute_partition_read partition, **opts end end |
#execute_query(sql, params: nil, types: nil, query_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.
702 703 704 705 706 707 708 709 710 711 712 713 |
# File 'lib/google/cloud/spanner/batch_snapshot.rb', line 702 def execute_query sql, params: nil, types: nil, query_options: nil, call_options: nil, directed_read_options: nil ensure_session! params, types = Convert.to_input_params_and_types params, types session.execute_query sql, params: params, types: types, transaction: tx_selector, query_options: , call_options: , directed_read_options: || @directed_read_options end |
#partition_query(sql, params: nil, types: nil, partition_size_bytes: nil, max_partitions: nil, query_options: nil, call_options: nil, data_boost_enabled: false, directed_read_options: nil) ⇒ Array<Google::Cloud::Spanner::Partition>
Returns a list of Partition objects to execute a batch query against a database.
These partitions can be executed across multiple processes, even across different machines. The partition size and count can be configured, although the values given may not necessarily be honored depending on the query and options in the request.
The request will fail if the query is not root partitionable. For a query to be root partitionable, it needs to satisfy a few conditions. For example, if the query execution plan contains a distributed union operator, then it must be the first operator in the plan. For more information about other conditions, see Read data in parallel.
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.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/google/cloud/spanner/batch_snapshot.rb', line 236 def partition_query sql, params: nil, types: nil, partition_size_bytes: nil, max_partitions: nil, query_options: nil, call_options: nil, data_boost_enabled: false, directed_read_options: nil ensure_session! params, types = Convert.to_input_params_and_types params, types results = session.partition_query \ sql, tx_selector, params: params, types: types, partition_size_bytes: partition_size_bytes, max_partitions: max_partitions, call_options: results.partitions.map do |grpc| # Convert partition protos to execute sql request protos execute_sql_grpc = V1::ExecuteSqlRequest.new( { session: session.path, sql: sql, params: params, param_types: types, transaction: tx_selector, partition_token: grpc.partition_token, query_options: , data_boost_enabled: data_boost_enabled, directed_read_options: || @directed_read_options }.compact ) Partition.from_execute_sql_grpc execute_sql_grpc end end |
#partition_read(table, columns, keys: nil, index: nil, partition_size_bytes: nil, max_partitions: nil, call_options: nil, data_boost_enabled: false, directed_read_options: nil) ⇒ Array<Google::Cloud::Spanner::Partition>
Returns a list of Partition objects to read zero or more rows from a database.
These partitions can be executed across multiple processes, even across different machines. The partition size and count can be configured, although the values given may not necessarily be honored depending on the query and options in the request.
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.
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/google/cloud/spanner/batch_snapshot.rb', line 343 def partition_read table, columns, keys: nil, index: nil, partition_size_bytes: nil, max_partitions: nil, call_options: nil, data_boost_enabled: false, directed_read_options: nil ensure_session! columns = Array(columns).map(&:to_s) keys = Convert.to_key_set keys results = session.partition_read \ table, columns, tx_selector, keys: keys, index: index, partition_size_bytes: partition_size_bytes, max_partitions: max_partitions, call_options: results.partitions.map do |grpc| # Convert partition protos to read request protos read_grpc = V1::ReadRequest.new( { session: session.path, table: table, columns: columns, key_set: keys, index: index, transaction: tx_selector, partition_token: grpc.partition_token, data_boost_enabled: data_boost_enabled, directed_read_options: || @directed_read_options }.compact ) Partition.from_read_grpc read_grpc end end |
#read(table, columns, keys: nil, index: nil, limit: nil, call_options: nil, directed_read_options: 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.
778 779 780 781 782 783 784 785 786 787 788 789 |
# File 'lib/google/cloud/spanner/batch_snapshot.rb', line 778 def read table, columns, keys: nil, index: nil, limit: nil, call_options: nil, directed_read_options: nil ensure_session! columns = Array(columns).map(&:to_s) keys = Convert.to_key_set keys session.read table, columns, keys: keys, index: index, limit: limit, transaction: tx_selector, call_options: , directed_read_options: || @directed_read_options end |
#timestamp ⇒ Time
The read timestamp chosen for batch snapshot.
90 91 92 93 |
# File 'lib/google/cloud/spanner/batch_snapshot.rb', line 90 def return nil if grpc.nil? Convert. grpc. end |
#transaction_id ⇒ String
Identifier of the batch snapshot transaction.
82 83 84 85 |
# File 'lib/google/cloud/spanner/batch_snapshot.rb', line 82 def transaction_id return nil if grpc.nil? grpc.id end |