Class: Google::Cloud::Datastore::Dataset
- Inherits:
-
Object
- Object
- Google::Cloud::Datastore::Dataset
- Defined in:
- lib/google/cloud/datastore/dataset.rb,
lib/google/cloud/datastore/dataset/query_results.rb,
lib/google/cloud/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.
Dataset is the main object for interacting with Google Datastore. Entity objects are created, read, updated, and deleted by Dataset.
Direct Known Subclasses
Defined Under Namespace
Classes: LookupResults, QueryResults
Instance Method Summary collapse
-
#allocate_ids(incomplete_key, count = 1) ⇒ Array<Google::Cloud::Datastore::Key>
Generate IDs for a Key before creating an entity.
-
#commit {|commit| ... } ⇒ Array<Google::Cloud::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| ... } ⇒ Google::Cloud::Datastore::Entity
Create a new empty Entity instance.
-
#find(key_or_kind, id_or_name = nil, consistency: nil) ⇒ Google::Cloud::Datastore::Entity?
(also: #get)
Retrieve an entity by key.
-
#find_all(*keys, consistency: nil) ⇒ Google::Cloud::Datastore::Dataset::LookupResults
(also: #lookup)
Retrieve the entities for the provided keys.
-
#gql(query, bindings = {}) ⇒ Google::Cloud::Datastore::GqlQuery
Create a new GqlQuery instance.
-
#insert(*entities) ⇒ Array<Google::Cloud::Datastore::Entity>
Insert one or more entities to the Datastore.
-
#key(*path, project: nil, namespace: nil) ⇒ Google::Cloud::Datastore::Key
Create a new Key instance.
-
#project ⇒ Object
The Datastore project connected to.
-
#query(*kinds) ⇒ Google::Cloud::Datastore::Query
Create a new Query instance.
-
#run(query, namespace: nil, consistency: nil) ⇒ Google::Cloud::Datastore::Dataset::QueryResults
(also: #run_query)
Retrieve entities specified by a Query.
-
#save(*entities) ⇒ Array<Google::Cloud::Datastore::Entity>
(also: #upsert)
Persist one or more entities to the Datastore.
-
#transaction {|tx| ... } ⇒ Object
Creates a Datastore Transaction.
-
#update(*entities) ⇒ Array<Google::Cloud::Datastore::Entity>
Update one or more entities to the Datastore.
Instance Method Details
#allocate_ids(incomplete_key, count = 1) ⇒ Array<Google::Cloud::Datastore::Key>
Generate IDs for a Key before creating an entity.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/google/cloud/datastore/dataset.rb', line 110 def allocate_ids incomplete_key, count = 1 if incomplete_key.complete? fail Google::Cloud::Datastore::KeyError, "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 } end |
#commit {|commit| ... } ⇒ Array<Google::Cloud::Datastore::Entity>
Make multiple changes in a single commit.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/google/cloud/datastore/dataset.rb', line 297 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 end |
#delete(*entities_or_keys) ⇒ Boolean
Remove entities from the Datastore.
273 274 275 276 |
# File 'lib/google/cloud/datastore/dataset.rb', line 273 def delete *entities_or_keys commit { |c| c.delete(*entities_or_keys) } true end |
#entity(*key_or_path, project: nil, namespace: nil) {|entity| ... } ⇒ Google::Cloud::Datastore::Entity
Create a new empty Entity instance. This is a convenience method to make the creation of Entity objects easier.
767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
# File 'lib/google/cloud/datastore/dataset.rb', line 767 def entity *key_or_path, project: nil, namespace: nil entity = Entity.new # Set the key if key_or_path.flatten.first.is_a? Google::Cloud::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) ⇒ Google::Cloud::Datastore::Entity? Also known as: get
Retrieve an entity by key.
346 347 348 349 350 351 352 |
# File 'lib/google/cloud/datastore/dataset.rb', line 346 def find key_or_kind, id_or_name = nil, consistency: 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, consistency: consistency).first end |
#find_all(*keys, consistency: nil) ⇒ Google::Cloud::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.
380 381 382 383 384 385 386 |
# File 'lib/google/cloud/datastore/dataset.rb', line 380 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 end |
#gql(query, bindings = {}) ⇒ Google::Cloud::Datastore::GqlQuery
Create a new GqlQuery instance. This is a convenience method to make the creation of GqlQuery objects easier.
593 594 595 596 597 598 |
# File 'lib/google/cloud/datastore/dataset.rb', line 593 def gql query, bindings = {} gql = GqlQuery.new gql.query_string = query gql.named_bindings = bindings unless bindings.empty? gql end |
#insert(*entities) ⇒ Array<Google::Cloud::Datastore::Entity>
Insert one or more entities to the Datastore. An InvalidArgumentError will raised if the entities cannot be inserted.
223 224 225 |
# File 'lib/google/cloud/datastore/dataset.rb', line 223 def insert *entities commit { |c| c.insert(*entities) } end |
#key(*path, project: nil, namespace: nil) ⇒ Google::Cloud::Datastore::Key
Create a new Key instance. This is a convenience method to make the creation of Key objects easier.
683 684 685 686 687 688 689 690 691 692 693 |
# File 'lib/google/cloud/datastore/dataset.rb', line 683 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.
80 81 82 |
# File 'lib/google/cloud/datastore/dataset.rb', line 80 def project service.project end |
#query(*kinds) ⇒ Google::Cloud::Datastore::Query
Create a new Query instance. This is a convenience method to make the creation of Query objects easier.
556 557 558 559 560 |
# File 'lib/google/cloud/datastore/dataset.rb', line 556 def query *kinds query = Query.new query.kind(*kinds) unless kinds.empty? query end |
#run(query, namespace: nil, consistency: nil) ⇒ Google::Cloud::Datastore::Dataset::QueryResults Also known as: run_query
Retrieve entities specified by a Query.
452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/google/cloud/datastore/dataset.rb', line 452 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 end |
#save(*entities) ⇒ Array<Google::Cloud::Datastore::Entity> Also known as: upsert
Persist one or more entities to the Datastore.
174 175 176 |
# File 'lib/google/cloud/datastore/dataset.rb', line 174 def save *entities commit { |c| c.save(*entities) } end |
#transaction {|tx| ... } ⇒ Object
Creates a Datastore Transaction.
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/google/cloud/datastore/dataset.rb', line 511 def transaction tx = Transaction.new service return tx unless block_given? begin yield tx tx.commit rescue begin tx.rollback rescue raise TransactionError, "Transaction failed to commit and rollback." end raise TransactionError, "Transaction failed to commit." end end |
#update(*entities) ⇒ Array<Google::Cloud::Datastore::Entity>
Update one or more entities to the Datastore. An InvalidArgumentError will raised if the entities cannot be updated.
254 255 256 |
# File 'lib/google/cloud/datastore/dataset.rb', line 254 def update *entities commit { |c| c.update(*entities) } end |