Class: Gcloud::Datastore::Dataset
- Inherits:
-
Object
- Object
- Gcloud::Datastore::Dataset
- Defined in:
- lib/gcloud/datastore/dataset.rb,
lib/gcloud/datastore/dataset/query_results.rb,
lib/gcloud/datastore/dataset/lookup_results.rb
Overview
# Dataset
Dataset is the data saved in a project’s Datastore. Dataset is analogous to a database in relational database world.
Gcloud::Datastore::Dataset is the main object for interacting with Google Datastore. Entity objects are created, read, updated, and deleted by Gcloud::Datastore::Dataset.
See Gcloud#datastore
Direct Known Subclasses
Defined Under Namespace
Classes: LookupResults, QueryResults
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#allocate_ids(incomplete_key, count = 1) ⇒ Array<Gcloud::Datastore::Key>
Generate IDs for a Key before creating an entity.
-
#delete(*entities_or_keys) ⇒ Boolean
Remove entities from the Datastore.
-
#entity(key_or_kind = nil, id_or_name = nil) {|entity| ... } ⇒ Gcloud::Datastore::Entity
Create a new empty Entity instance.
-
#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(project, credentials) ⇒ Dataset
constructor
See Gcloud#datastore.
-
#key(kind = nil, id_or_name = nil) ⇒ Gcloud::Datastore::Key
Create a new Key instance.
-
#project ⇒ Object
The Datastore project connected to.
-
#query(*kinds) ⇒ Gcloud::Datastore::Query
Create a new Query instance.
-
#run(query, namespace: nil) ⇒ Gcloud::Datastore::Dataset::QueryResults
(also: #run_query)
Retrieve entities specified by a Query.
-
#save(*entities) ⇒ Array<Gcloud::Datastore::Entity>
Persist one or more entities to the Datastore.
-
#transaction {|tx| ... } ⇒ Object
Creates a Datastore Transaction.
Constructor Details
#initialize(project, credentials) ⇒ Dataset
See Gcloud#datastore
58 59 60 61 62 |
# File 'lib/gcloud/datastore/dataset.rb', line 58 def initialize project, credentials project = project.to_s # Always cast to a string fail ArgumentError, "project is missing" if project.empty? @connection = Connection.new project, credentials end |
Instance Attribute Details
#connection ⇒ Object
52 53 54 |
# File 'lib/gcloud/datastore/dataset.rb', line 52 def connection @connection end |
Class Method Details
.default_project ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/gcloud/datastore/dataset.rb', line 82 def self.default_project ENV["DATASTORE_DATASET"] || ENV["DATASTORE_PROJECT"] || ENV["GCLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"] || Gcloud::GCE.project_id end |
Instance Method Details
#allocate_ids(incomplete_key, count = 1) ⇒ Array<Gcloud::Datastore::Key>
Generate IDs for a Key before creating an entity.
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/gcloud/datastore/dataset.rb', line 102 def allocate_ids incomplete_key, count = 1 if incomplete_key.complete? fail Gcloud::Datastore::Error, "An incomplete key must be provided." end incomplete_keys = count.times.map { incomplete_key.to_proto } response = connection.allocate_ids(*incomplete_keys) Array(response.key).map do |key| Key.from_proto key end end |
#delete(*entities_or_keys) ⇒ Boolean
Remove entities from the Datastore.
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/gcloud/datastore/dataset.rb', line 194 def delete *entities_or_keys keys = entities_or_keys.map do |e_or_k| e_or_k.respond_to?(:key) ? e_or_k.key.to_proto : e_or_k.to_proto end mutation = Proto.new_mutation.tap do |m| m.delete = keys end connection.commit mutation true end |
#entity(key_or_kind = nil, id_or_name = nil) {|entity| ... } ⇒ Gcloud::Datastore::Entity
Create a new empty Entity instance. This is a convenience method to make the creation of Entity objects easier.
end
377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/gcloud/datastore/dataset.rb', line 377 def entity key_or_kind = nil, id_or_name = nil entity = Entity.new # Set the key key = key_or_kind unless key.is_a? Gcloud::Datastore::Key key = Key.new key_or_kind, id_or_name end entity.key = key yield entity if block_given? entity end |
#find(key_or_kind, id_or_name = nil) ⇒ Gcloud::Datastore::Entity? Also known as: get
Retrieve an entity by providing key information.
149 150 151 152 153 154 155 |
# File 'lib/gcloud/datastore/dataset.rb', line 149 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.
172 173 174 175 176 177 178 |
# File 'lib/gcloud/datastore/dataset.rb', line 172 def find_all *keys response = connection.lookup(*keys.map(&:to_proto)) entities = to_gcloud_entities response.found deferred = to_gcloud_keys response.deferred missing = to_gcloud_entities response.missing LookupResults.new entities, deferred, missing end |
#key(kind = nil, id_or_name = nil) ⇒ Gcloud::Datastore::Key
Create a new Key instance. This is a convenience method to make the creation of Key objects easier.
331 332 333 |
# File 'lib/gcloud/datastore/dataset.rb', line 331 def key kind = nil, id_or_name = nil Key.new kind, id_or_name end |
#project ⇒ Object
The Datastore project connected to.
76 77 78 |
# File 'lib/gcloud/datastore/dataset.rb', line 76 def project connection.dataset_id end |
#query(*kinds) ⇒ Gcloud::Datastore::Query
Create a new Query instance. This is a convenience method to make the creation of Query objects easier.
309 310 311 312 313 |
# File 'lib/gcloud/datastore/dataset.rb', line 309 def query *kinds query = Query.new query.kind(*kinds) unless kinds.empty? query end |
#run(query, namespace: nil) ⇒ Gcloud::Datastore::Dataset::QueryResults Also known as: run_query
Retrieve entities specified by a Query.
223 224 225 226 227 228 229 230 |
# File 'lib/gcloud/datastore/dataset.rb', line 223 def run query, namespace: nil partition = optional_partition_id namespace response = connection.run_query query.to_proto, partition 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) ⇒ Array<Gcloud::Datastore::Entity>
Persist one or more entities to the Datastore.
125 126 127 128 129 130 131 |
# File 'lib/gcloud/datastore/dataset.rb', line 125 def save *entities mutation = Proto.new_mutation save_entities_to_mutation entities, mutation response = connection.commit mutation auto_id_assign_ids response.mutation_result.insert_auto_id_key entities end |
#transaction {|tx| ... } ⇒ Object
Creates a Datastore Transaction.
277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/gcloud/datastore/dataset.rb', line 277 def transaction tx = Transaction.new connection return tx unless block_given? begin yield tx tx.commit rescue => e tx.rollback raise TransactionError.new("Transaction failed to commit.", e) end end |