Class: Gcloud::Datastore::Transaction
- Defined in:
- lib/gcloud/datastore/transaction.rb
Overview
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Attributes inherited from Dataset
Instance Method Summary collapse
-
#commit {|commit| ... } ⇒ Object
Commits a transaction.
-
#delete(*entities_or_keys) ⇒ Object
Remove entities in a transaction.
-
#find(key_or_kind, id_or_name = nil) ⇒ Gcloud::Datastore::Entity?
(also: #get)
Retrieve an entity by providing key information.
-
#find_all(*keys) ⇒ Gcloud::Datastore::Dataset::LookupResults
(also: #lookup)
Retrieve the entities for the provided keys.
-
#initialize(connection) ⇒ Transaction
constructor
Takes a Connection instead of project and Credentials.
-
#reset! ⇒ Object
Reset the transaction.
-
#rollback ⇒ Object
Rolls a transaction back.
-
#run(query, namespace: nil) ⇒ Gcloud::Datastore::Dataset::QueryResults
(also: #run_query)
Retrieve entities specified by a Query.
-
#save(*entities) ⇒ Object
Persist entities in a transaction.
-
#start ⇒ Object
(also: #begin_transaction)
Begins a transaction.
Methods inherited from Dataset
#allocate_ids, default_project, #entity, #key, #project, #query, #transaction
Constructor Details
#initialize(connection) ⇒ Transaction
Takes a Connection instead of project and Credentials.
30 31 32 33 34 |
# File 'lib/gcloud/datastore/transaction.rb', line 30 def initialize connection @connection = connection reset! start end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
25 26 27 |
# File 'lib/gcloud/datastore/transaction.rb', line 25 def id @id end |
Instance Method Details
#commit {|commit| ... } ⇒ Object
Commits a transaction.
205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/gcloud/datastore/transaction.rb', line 205 def commit if @id.nil? fail TransactionError, "Cannot commit when not in a transaction." end yield @commit if block_given? response = connection.commit @commit.mutation, @id auto_id_assign_ids @commit.auto_id_entities, response.mutation_result.insert_auto_id_key # Make sure all entity keys are frozen so all show as persisted @commit.entities.each { |e| e.key.freeze unless e.persisted? } true end |
#delete(*entities_or_keys) ⇒ Object
Remove entities in a transaction.
62 63 64 65 66 |
# File 'lib/gcloud/datastore/transaction.rb', line 62 def delete *entities_or_keys @commit.delete(*entities_or_keys) # Do not delete yet true end |
#find(key_or_kind, id_or_name = nil) ⇒ Gcloud::Datastore::Entity? Also known as: get
Retrieve an entity by providing key information. The lookup is run within the transaction.
83 84 85 86 87 88 89 |
# File 'lib/gcloud/datastore/transaction.rb', line 83 def find key_or_kind, id_or_name = nil key = key_or_kind unless key.is_a? Gcloud::Datastore::Key key = Key.new key_or_kind, id_or_name end find_all(key).first end |
#find_all(*keys) ⇒ Gcloud::Datastore::Dataset::LookupResults Also known as: lookup
Retrieve the entities for the provided keys. The lookup is run within the transaction.
107 108 109 110 111 112 113 114 |
# File 'lib/gcloud/datastore/transaction.rb', line 107 def find_all *keys response = connection.lookup(*keys.map(&:to_proto), transaction: @id) entities = to_gcloud_entities response.found deferred = to_gcloud_keys response.deferred missing = to_gcloud_entities response.missing LookupResults.new entities, deferred, missing end |
#reset! ⇒ Object
Reset the transaction. #start must be called afterwards.
254 255 256 257 |
# File 'lib/gcloud/datastore/transaction.rb', line 254 def reset! @id = nil @commit = Commit.new end |
#rollback ⇒ Object
Rolls a transaction back.
242 243 244 245 246 247 248 249 |
# File 'lib/gcloud/datastore/transaction.rb', line 242 def rollback if @id.nil? fail TransactionError, "Cannot rollback when not in a transaction." end connection.rollback @id true end |
#run(query, namespace: nil) ⇒ Gcloud::Datastore::Dataset::QueryResults Also known as: run_query
Retrieve entities specified by a Query. The query is run within the transaction.
140 141 142 143 144 145 146 147 148 |
# File 'lib/gcloud/datastore/transaction.rb', line 140 def run query, namespace: nil partition = optional_partition_id namespace response = connection.run_query query.to_proto, partition, transaction: @id entities = to_gcloud_entities response.batch.entity_result cursor = Proto.encode_cursor response.batch.end_cursor more_results = Proto.to_more_results_string response.batch.more_results QueryResults.new entities, cursor, more_results end |
#save(*entities) ⇒ Object
Persist entities in a transaction.
46 47 48 49 50 |
# File 'lib/gcloud/datastore/transaction.rb', line 46 def save *entities @commit.save(*entities) # Do not save or assign auto_ids yet entities end |
#start ⇒ Object Also known as: begin_transaction
Begins a transaction. This method is run when a new Transaction is created.
154 155 156 157 158 159 |
# File 'lib/gcloud/datastore/transaction.rb', line 154 def start fail TransactionError, "Transaction already opened." unless @id.nil? response = connection.begin_transaction @id = response.transaction end |