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.
-
#commit {|commit| ... } ⇒ Array<Gcloud::Datastore::Entity>
Make multiple changes in a single commit.
-
#delete(*entities_or_keys) ⇒ Boolean
Remove entities from the Datastore.
-
#entity(*key_or_path, project: nil, namespace: nil) {|entity| ... } ⇒ Gcloud::Datastore::Entity
Create a new empty Entity instance.
-
#find(key_or_kind, id_or_name = nil, consistency: nil) ⇒ Gcloud::Datastore::Entity?
(also: #get)
Retrieve an entity by key.
-
#find_all(*keys, consistency: nil) ⇒ Gcloud::Datastore::Dataset::LookupResults
(also: #lookup)
Retrieve the entities for the provided keys.
-
#gql(query, bindings = {}) ⇒ Gcloud::Datastore::GqlQuery
Create a new GqlQuery instance.
-
#initialize(project, credentials) ⇒ Dataset
constructor
See Gcloud#datastore.
-
#insert(*entities) ⇒ Array<Gcloud::Datastore::Entity>
Insert one or more entities to the Datastore.
-
#key(*path, project: nil, namespace: 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, consistency: nil) ⇒ Gcloud::Datastore::Dataset::QueryResults
(also: #run_query)
Retrieve entities specified by a Query.
-
#save(*entities) ⇒ Array<Gcloud::Datastore::Entity>
(also: #upsert)
Persist one or more entities to the Datastore.
-
#transaction {|tx| ... } ⇒ Object
Creates a Datastore Transaction.
-
#update(*entities) ⇒ Array<Gcloud::Datastore::Entity>
Update one or more entities to the Datastore.
Constructor Details
#initialize(project, credentials) ⇒ Dataset
See Gcloud#datastore
63 64 65 66 67 |
# File 'lib/gcloud/datastore/dataset.rb', line 63 def initialize project, credentials project = project.to_s # Always cast to a string fail ArgumentError, "project is missing" if project.empty? @service = Service.new project, credentials end |
Instance Attribute Details
#service ⇒ Object
57 58 59 |
# File 'lib/gcloud/datastore/dataset.rb', line 57 def service @service end |
Class Method Details
.default_project ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/gcloud/datastore/dataset.rb', line 87 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.
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/gcloud/datastore/dataset.rb', line 107 def allocate_ids incomplete_key, count = 1 if incomplete_key.complete? fail Gcloud::Datastore::Error, "An incomplete key must be provided." end ensure_service! incomplete_keys = count.times.map { incomplete_key.to_grpc } allocate_res = service.allocate_ids(*incomplete_keys) allocate_res.keys.map { |key| Key.from_grpc key } rescue GRPC::BadStatus => e raise Gcloud::Error.from_error(e) end |
#commit {|commit| ... } ⇒ Array<Gcloud::Datastore::Entity>
Make multiple changes in a single commit.
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/gcloud/datastore/dataset.rb', line 263 def commit return unless block_given? c = Commit.new yield c ensure_service! commit_res = service.commit c.mutations entities = c.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 entities.each { |e| e.key.freeze unless e.persisted? } entities rescue GRPC::BadStatus => e raise Gcloud::Error.from_error(e) end |
#delete(*entities_or_keys) ⇒ Boolean
Remove entities from the Datastore.
241 242 243 244 |
# File 'lib/gcloud/datastore/dataset.rb', line 241 def delete *entities_or_keys commit { |c| c.delete(*entities_or_keys) } true end |
#entity(*key_or_path, project: nil, namespace: nil) {|entity| ... } ⇒ Gcloud::Datastore::Entity
Create a new empty Entity instance. This is a convenience method to make the creation of Entity objects easier.
639 640 641 642 643 644 645 646 647 648 649 650 651 652 |
# File 'lib/gcloud/datastore/dataset.rb', line 639 def entity *key_or_path, project: nil, namespace: nil entity = Entity.new # Set the key if key_or_path.flatten.first.is_a? Gcloud::Datastore::Key entity.key = key_or_path.flatten.first else entity.key = key key_or_path, project: project, namespace: namespace end yield entity if block_given? entity end |
#find(key_or_kind, id_or_name = nil, consistency: nil) ⇒ Gcloud::Datastore::Entity? Also known as: get
Retrieve an entity by key.
306 307 308 309 310 311 312 |
# File 'lib/gcloud/datastore/dataset.rb', line 306 def find key_or_kind, id_or_name = nil, consistency: 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, consistency: consistency).first end |
#find_all(*keys, consistency: nil) ⇒ Gcloud::Datastore::Dataset::LookupResults Also known as: lookup
Retrieve the entities for the provided keys. The order of results is undefined and has no relation to the order of keys arguments.
339 340 341 342 343 344 345 346 347 |
# File 'lib/gcloud/datastore/dataset.rb', line 339 def find_all *keys, consistency: nil ensure_service! check_consistency! consistency lookup_res = service.lookup(*Array(keys).flatten.map(&:to_grpc), consistency: consistency) LookupResults.from_grpc lookup_res, service, consistency rescue GRPC::BadStatus => e raise Gcloud::Error.from_error(e) end |
#gql(query, bindings = {}) ⇒ Gcloud::Datastore::GqlQuery
Create a new GqlQuery instance. This is a convenience method to make the creation of GqlQuery objects easier.
522 523 524 525 526 527 |
# File 'lib/gcloud/datastore/dataset.rb', line 522 def gql query, bindings = {} gql = GqlQuery.new gql.query_string = query gql.named_bindings = bindings unless bindings.empty? gql end |
#insert(*entities) ⇒ Array<Gcloud::Datastore::Entity>
Insert one or more entities to the Datastore. An InvalidArgumentError will raised if the entities cannot be inserted.
201 202 203 |
# File 'lib/gcloud/datastore/dataset.rb', line 201 def insert *entities commit { |c| c.insert(*entities) } end |
#key(*path, project: nil, namespace: nil) ⇒ Gcloud::Datastore::Key
Create a new Key instance. This is a convenience method to make the creation of Key objects easier.
577 578 579 580 581 582 583 584 585 586 587 |
# File 'lib/gcloud/datastore/dataset.rb', line 577 def key *path, project: nil, namespace: nil path = path.flatten.each_slice(2).to_a # group in pairs kind, id_or_name = path.pop Key.new(kind, id_or_name).tap do |k| k.project = project k.namespace = namespace unless path.empty? k.parent = key path, project: project, namespace: namespace end end end |
#project ⇒ Object
The Datastore project connected to.
81 82 83 |
# File 'lib/gcloud/datastore/dataset.rb', line 81 def project service.project end |
#query(*kinds) ⇒ Gcloud::Datastore::Query
Create a new Query instance. This is a convenience method to make the creation of Query objects easier.
493 494 495 496 497 |
# File 'lib/gcloud/datastore/dataset.rb', line 493 def query *kinds query = Query.new query.kind(*kinds) unless kinds.empty? query end |
#run(query, namespace: nil, consistency: nil) ⇒ Gcloud::Datastore::Dataset::QueryResults Also known as: run_query
Retrieve entities specified by a Query.
393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/gcloud/datastore/dataset.rb', line 393 def run query, namespace: nil, consistency: nil ensure_service! unless query.is_a?(Query) || query.is_a?(GqlQuery) fail ArgumentError, "Cannot run a #{query.class} object." end check_consistency! consistency query_res = service.run_query query.to_grpc, namespace, consistency: consistency QueryResults.from_grpc query_res, service, namespace, query.to_grpc.dup rescue GRPC::BadStatus => e raise Gcloud::Error.from_error(e) end |
#save(*entities) ⇒ Array<Gcloud::Datastore::Entity> Also known as: upsert
Persist one or more entities to the Datastore.
160 161 162 |
# File 'lib/gcloud/datastore/dataset.rb', line 160 def save *entities commit { |c| c.save(*entities) } end |
#transaction {|tx| ... } ⇒ Object
Creates a Datastore Transaction.
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/gcloud/datastore/dataset.rb', line 455 def transaction tx = Transaction.new service return tx unless block_given? begin yield tx tx.commit rescue => e begin tx.rollback rescue => re msg = "Transaction failed to commit and rollback." raise TransactionError.new(msg, commit_error: e, rollback_error: re) end raise TransactionError.new("Transaction failed to commit.", commit_error: e) end end |
#update(*entities) ⇒ Array<Gcloud::Datastore::Entity>
Update one or more entities to the Datastore. An InvalidArgumentError will raised if the entities cannot be updated.
224 225 226 |
# File 'lib/gcloud/datastore/dataset.rb', line 224 def update *entities commit { |c| c.update(*entities) } end |