Class: Google::Cloud::Datastore::ReadOnlyTransaction
- Inherits:
-
Object
- Object
- Google::Cloud::Datastore::ReadOnlyTransaction
- Defined in:
- lib/google/cloud/datastore/read_only_transaction.rb
Overview
ReadOnlyTransaction
Represents a read-only Datastore transaction that only allows reads.
A read-only transaction cannot modify entities; in return they do not contend with other read-write or read-only transactions. Using a read-only transaction for transactions that only read data will potentially improve throughput.
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
-
#commit ⇒ Object
Commits the 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.
-
#reset! ⇒ Object
Reset the transaction.
-
#rollback ⇒ Object
Rolls back the transaction.
-
#run(query, namespace: nil) ⇒ Google::Cloud::Datastore::Dataset::QueryResults
(also: #run_query)
Retrieve entities specified by a Query.
-
#start ⇒ Object
(also: #begin_transaction)
Begins a transaction.
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id
56 57 58 |
# File 'lib/google/cloud/datastore/read_only_transaction.rb', line 56 def id @id end |
Instance Method Details
#commit ⇒ Object
Commits the transaction.
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/google/cloud/datastore/read_only_transaction.rb', line 192 def commit if @id.nil? raise TransactionError, "Cannot commit when not in a transaction." end ensure_service! service.commit [], transaction: @id 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.
91 92 93 94 95 96 97 |
# File 'lib/google/cloud/datastore/read_only_transaction.rb', line 91 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.
120 121 122 123 124 125 |
# File 'lib/google/cloud/datastore/read_only_transaction.rb', line 120 def find_all *keys ensure_service! lookup_res = service.lookup(*Array(keys).flatten.map(&:to_grpc), transaction: @id) Dataset::LookupResults.from_grpc lookup_res, service, nil, @id end |
#reset! ⇒ Object
Reset the transaction. #start must be called afterwards.
235 236 237 |
# File 'lib/google/cloud/datastore/read_only_transaction.rb', line 235 def reset! @id = nil end |
#rollback ⇒ Object
Rolls back the transaction.
222 223 224 225 226 227 228 229 230 |
# File 'lib/google/cloud/datastore/read_only_transaction.rb', line 222 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.
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/google/cloud/datastore/read_only_transaction.rb', line 148 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 Dataset::QueryResults.from_grpc query_res, service, namespace, query.to_grpc.dup end |
#start ⇒ Object Also known as: begin_transaction
Begins a transaction. This method is run when a new ReadOnlyTransaction is created.
164 165 166 167 168 169 170 |
# File 'lib/google/cloud/datastore/read_only_transaction.rb', line 164 def start raise TransactionError, "Transaction already opened." unless @id.nil? ensure_service! tx_res = service.begin_transaction read_only: true @id = tx_res.transaction end |