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,
lib/google/cloud/datastore/dataset/aggregate_query_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: AggregateQueryResults, 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.
-
#database_id ⇒ String
The Datastore database connected to.
-
#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.
-
#filter(name, operator, value) ⇒ Google::Cloud::Datastore::Filter
Create a new Filter instance.
-
#find(key_or_kind, id_or_name = nil, consistency: nil, read_time: nil) ⇒ Google::Cloud::Datastore::Entity?
(also: #get)
Retrieve an entity by key.
-
#find_all(*keys, consistency: nil, read_time: 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_id ⇒ Object
(also: #project)
The Datastore project connected to.
-
#query(*kinds) ⇒ Google::Cloud::Datastore::Query
Create a new Query instance.
-
#read_only_transaction(read_time: nil) {|tx| ... } ⇒ Object
(also: #snapshot)
Creates a read-only transaction that provides a consistent snapshot of Cloud Datastore.
-
#run(query, namespace: nil, consistency: nil, read_time: nil) ⇒ Google::Cloud::Datastore::Dataset::QueryResults
(also: #run_query)
Retrieve entities specified by a Query.
-
#run_aggregation(aggregate_query, namespace: nil, consistency: nil, read_time: nil) ⇒ Google::Cloud::Datastore::Dataset::AggregateQueryResults
Retrieve aggregate results specified by an AggregateQuery.
-
#save(*entities) ⇒ Array<Google::Cloud::Datastore::Entity>
(also: #upsert)
Persist one or more entities to the Datastore.
-
#transaction(deadline: nil, previous_transaction: nil) {|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.
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/google/cloud/datastore/dataset.rb', line 123 def allocate_ids incomplete_key, count = 1 if incomplete_key.complete? raise Datastore::KeyError, "An incomplete key must be provided." end ensure_service! incomplete_keys = Array.new(count) { 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.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/google/cloud/datastore/dataset.rb', line 309 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 |
#database_id ⇒ String
The Datastore database connected to.
103 104 105 |
# File 'lib/google/cloud/datastore/dataset.rb', line 103 def database_id service.database end |
#delete(*entities_or_keys) ⇒ Boolean
Remove entities from the Datastore.
285 286 287 288 |
# File 'lib/google/cloud/datastore/dataset.rb', line 285 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.
969 970 971 972 973 974 975 976 977 978 979 980 981 982 |
# File 'lib/google/cloud/datastore/dataset.rb', line 969 def entity *key_or_path, project: nil, namespace: nil entity = Entity.new # Set the key entity.key = if key_or_path.flatten.first.is_a? Datastore::Key key_or_path.flatten.first else key key_or_path, project: project, namespace: namespace end yield entity if block_given? entity end |
#filter(name, operator, value) ⇒ Google::Cloud::Datastore::Filter
Create a new Filter instance. This is a convenience method to make the creation of Filter objects easier.
1012 1013 1014 |
# File 'lib/google/cloud/datastore/dataset.rb', line 1012 def filter name, operator, value Filter.new name, operator, value end |
#find(key_or_kind, id_or_name = nil, consistency: nil, read_time: nil) ⇒ Google::Cloud::Datastore::Entity? Also known as: get
Retrieve an entity by key.
360 361 362 363 364 365 366 |
# File 'lib/google/cloud/datastore/dataset.rb', line 360 def find key_or_kind, id_or_name = nil, consistency: nil, read_time: 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, read_time: read_time).first end |
#find_all(*keys, consistency: nil, read_time: 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.
396 397 398 399 400 401 402 |
# File 'lib/google/cloud/datastore/dataset.rb', line 396 def find_all *keys, consistency: nil, read_time: nil ensure_service! check_consistency! consistency lookup_res = service.lookup(*Array(keys).flatten.map(&:to_grpc), consistency: consistency, read_time: read_time) LookupResults.from_grpc lookup_res, service, consistency, nil, read_time 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.
795 796 797 798 799 800 |
# File 'lib/google/cloud/datastore/dataset.rb', line 795 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.
235 236 237 |
# File 'lib/google/cloud/datastore/dataset.rb', line 235 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.
885 886 887 888 889 890 891 892 893 894 895 |
# File 'lib/google/cloud/datastore/dataset.rb', line 885 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_id ⇒ Object Also known as: project
The Datastore project connected to.
82 83 84 |
# File 'lib/google/cloud/datastore/dataset.rb', line 82 def project_id 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.
758 759 760 761 762 |
# File 'lib/google/cloud/datastore/dataset.rb', line 758 def query *kinds query = Query.new query.kind(*kinds) unless kinds.empty? query end |
#read_only_transaction(read_time: nil) {|tx| ... } ⇒ Object Also known as: snapshot
Creates a read-only transaction that provides a consistent snapshot of Cloud Datastore. This can be useful when multiple reads are needed to render a page or export data that must be consistent.
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.
Read-only single-group transactions never fail due to concurrent modifications, so you don't have to implement retries upon failure. However, multi-entity-group transactions can fail due to concurrent modifications, so these should have retries.
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 |
# File 'lib/google/cloud/datastore/dataset.rb', line 712 def read_only_transaction read_time: nil tx = ReadOnlyTransaction.new service, read_time: read_time return tx unless block_given? begin yield tx tx.commit rescue StandardError begin tx.rollback rescue StandardError raise TransactionError, "Transaction failed to commit and rollback." end raise TransactionError, "Transaction failed to commit." end end |
#run(query, namespace: nil, consistency: nil, read_time: nil) ⇒ Google::Cloud::Datastore::Dataset::QueryResults Also known as: run_query
Retrieve entities specified by a Query.
470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/google/cloud/datastore/dataset.rb', line 470 def run query, namespace: nil, consistency: nil, read_time: nil ensure_service! unless query.is_a?(Query) || query.is_a?(GqlQuery) raise ArgumentError, "Cannot run a #{query.class} object." end check_consistency! consistency query_res = service.run_query query.to_grpc, namespace, consistency: consistency, read_time: read_time QueryResults.from_grpc query_res, service, namespace, query.to_grpc.dup, read_time end |
#run_aggregation(aggregate_query, namespace: nil, consistency: nil, read_time: nil) ⇒ Google::Cloud::Datastore::Dataset::AggregateQueryResults
Retrieve aggregate results specified by an AggregateQuery.
556 557 558 559 560 561 562 563 564 565 |
# File 'lib/google/cloud/datastore/dataset.rb', line 556 def run_aggregation aggregate_query, namespace: nil, consistency: nil, read_time: nil ensure_service! unless aggregate_query.is_a?(AggregateQuery) || aggregate_query.is_a?(GqlQuery) raise ArgumentError, "Cannot run a #{aggregate_query.class} object." end check_consistency! consistency aggregate_query_res = service.run_aggregation_query aggregate_query.to_grpc, namespace, consistency: consistency, read_time: read_time AggregateQueryResults.from_grpc aggregate_query_res end |
#save(*entities) ⇒ Array<Google::Cloud::Datastore::Entity> Also known as: upsert
Persist one or more entities to the Datastore.
186 187 188 |
# File 'lib/google/cloud/datastore/dataset.rb', line 186 def save *entities commit { |c| c.save(*entities) } end |
#transaction(deadline: nil, previous_transaction: nil) {|tx| ... } ⇒ Object
Creates a Datastore Transaction.
Transactions using the block syntax are committed upon block completion and are automatically retried when known errors are raised during commit. All other errors will be passed on.
All changes are accumulated in memory until the block completes.
Transactions will be automatically retried when possible, until
deadline
is reached. This operation makes separate API requests to
begin and commit the transaction.
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
# File 'lib/google/cloud/datastore/dataset.rb', line 637 def transaction deadline: nil, previous_transaction: nil deadline = validate_deadline deadline backoff = 1.0 start_time = Time.now tx = Transaction.new \ service, previous_transaction: previous_transaction return tx unless block_given? begin yield tx tx.commit rescue Google::Cloud::UnavailableError => e # Re-raise if deadline has passed raise e if Time.now - start_time > deadline # Sleep with incremental backoff sleep backoff *= 1.3 # Create new transaction and retry the block tx = Transaction.new service, previous_transaction: tx.id retry rescue StandardError begin tx.rollback rescue StandardError 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.
266 267 268 |
# File 'lib/google/cloud/datastore/dataset.rb', line 266 def update *entities commit { |c| c.update(*entities) } end |