Class: Google::Cloud::Datastore::Transaction
- Defined in:
- lib/google/cloud/datastore/transaction.rb
Overview
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
The identifier of the transaction.
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) ⇒ Google::Cloud::Datastore::Entity?
(also: #get)
Retrieve an entity by providing key information.
-
#find_all(*keys) ⇒ Google::Cloud::Datastore::Dataset::LookupResults
(also: #lookup)
Retrieve the entities for the provided keys.
-
#insert(*entities) ⇒ Object
Insert entities in a transaction.
-
#reset! ⇒ Object
Reset the transaction.
-
#rollback ⇒ Object
Rolls a transaction back.
-
#run(query, namespace: nil) ⇒ Google::Cloud::Datastore::Dataset::QueryResults
(also: #run_query)
Retrieve entities specified by a Query.
-
#save(*entities) ⇒ Object
(also: #upsert)
Persist entities in a transaction.
-
#start ⇒ Object
(also: #begin_transaction)
Begins a transaction.
-
#update(*entities) ⇒ Object
Update entities in a transaction.
Methods inherited from Dataset
#allocate_ids, #database_id, #entity, #filter, #gql, #key, #project_id, #query, #read_only_transaction, #run_aggregation, #transaction
Instance Attribute Details
#id ⇒ String (readonly)
The identifier of the transaction.
46 47 48 |
# File 'lib/google/cloud/datastore/transaction.rb', line 46 def id @id end |
Instance Method Details
#commit {|commit| ... } ⇒ Object
Commits a transaction.
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/google/cloud/datastore/transaction.rb', line 322 def commit if @id.nil? raise TransactionError, "Cannot commit when not in a transaction." end yield @commit if block_given? ensure_service! commit_res = service.commit @commit.mutations, transaction: @id entities = @commit.entities returned_keys = commit_res.mutation_results.map(&:key) returned_keys.each_with_index do |key, index| next if entities[index].nil? entities[index].key = Key.from_grpc key unless key.nil? end # Make sure all entity keys are frozen so all show as persisted entities.each { |e| e.key.freeze unless e.persisted? } true end |
#delete(*entities_or_keys) ⇒ Object
Remove entities in a transaction.
161 162 163 164 165 |
# File 'lib/google/cloud/datastore/transaction.rb', line 161 def delete *entities_or_keys @commit.delete(*entities_or_keys) # Do not delete yet true end |
#find(key_or_kind, id_or_name = nil) ⇒ Google::Cloud::Datastore::Entity? Also known as: get
Retrieve an entity by providing key information. The lookup is run within the transaction.
190 191 192 193 194 195 196 |
# File 'lib/google/cloud/datastore/transaction.rb', line 190 def find key_or_kind, id_or_name = nil key = key_or_kind unless key.is_a? Google::Cloud::Datastore::Key key = Key.new key_or_kind, id_or_name end find_all(key).first end |
#find_all(*keys) ⇒ Google::Cloud::Datastore::Dataset::LookupResults Also known as: lookup
Retrieve the entities for the provided keys. The lookup is run within the transaction.
216 217 218 219 220 221 |
# File 'lib/google/cloud/datastore/transaction.rb', line 216 def find_all *keys ensure_service! lookup_res = service.lookup(*Array(keys).flatten.map(&:to_grpc), transaction: @id) LookupResults.from_grpc lookup_res, service, nil, @id end |
#insert(*entities) ⇒ Object
Insert entities in a transaction. An InvalidArgumentError will raised if the entities cannot be inserted.
115 116 117 118 119 |
# File 'lib/google/cloud/datastore/transaction.rb', line 115 def insert *entities @commit.insert(*entities) # Do not insert yet entities end |
#reset! ⇒ Object
Reset the transaction. #start must be called afterwards.
380 381 382 383 |
# File 'lib/google/cloud/datastore/transaction.rb', line 380 def reset! @id = nil @commit = Commit.new end |
#rollback ⇒ Object
Rolls a transaction back.
367 368 369 370 371 372 373 374 375 |
# File 'lib/google/cloud/datastore/transaction.rb', line 367 def rollback if @id.nil? raise TransactionError, "Cannot rollback when not in a transaction." end ensure_service! service.rollback @id true end |
#run(query, namespace: nil) ⇒ Google::Cloud::Datastore::Dataset::QueryResults Also known as: run_query
Retrieve entities specified by a Query. The query is run within the transaction.
254 255 256 257 258 259 260 261 262 263 |
# File 'lib/google/cloud/datastore/transaction.rb', line 254 def run query, namespace: nil ensure_service! unless query.is_a?(Query) || query.is_a?(GqlQuery) raise ArgumentError, "Cannot run a #{query.class} object." end query_res = service.run_query query.to_grpc, namespace, transaction: @id QueryResults.from_grpc query_res, service, namespace, query.to_grpc.dup end |
#save(*entities) ⇒ Object Also known as: upsert
Persist entities in a transaction.
83 84 85 86 87 |
# File 'lib/google/cloud/datastore/transaction.rb', line 83 def save *entities @commit.save(*entities) # Do not save yet entities end |
#start ⇒ Object Also known as: begin_transaction
Begins a transaction. This method is run when a new Transaction is created.
269 270 271 272 273 274 275 276 |
# File 'lib/google/cloud/datastore/transaction.rb', line 269 def start raise TransactionError, "Transaction already opened." unless @id.nil? ensure_service! tx_res = service.begin_transaction \ previous_transaction: @previous_transaction @id = tx_res.transaction end |
#update(*entities) ⇒ Object
Update entities in a transaction. An InvalidArgumentError will raised if the entities cannot be updated.
141 142 143 144 145 |
# File 'lib/google/cloud/datastore/transaction.rb', line 141 def update *entities @commit.update(*entities) # Do not update yet entities end |