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. Gcloud::Datastore::Entity objects are created, read, updated, and deleted by Gcloud::Datastore::Dataset.
require "gcloud"
gcloud = Gcloud.new
dataset = gcloud.datastore
query = dataset.query("Task").
where("completed", "=", true)
tasks = dataset.run query
See Gcloud#datastore
Direct Known Subclasses
Defined Under Namespace
Classes: LookupResults, QueryResults
Instance Attribute Summary collapse
-
#connection ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.default_project ⇒ Object
Default project.
Instance Method Summary collapse
-
#allocate_ids(incomplete_key, count = 1) ⇒ Object
Generate IDs for a Key before creating an entity.
-
#delete(*entities_or_keys) ⇒ Object
Remove entities from the Datastore.
-
#entity(key_or_kind = nil, id_or_name = nil) {|entity| ... } ⇒ Object
Create a new empty Entity instance.
-
#find(key_or_kind, id_or_name = nil) ⇒ Object
(also: #get)
Retrieve an entity by providing key information.
-
#find_all(*keys) ⇒ Object
(also: #lookup)
Retrieve the entities for the provided keys.
-
#initialize(project, credentials) ⇒ Dataset
constructor
Creates a new Dataset instance.
-
#key(kind = nil, id_or_name = nil) ⇒ Object
Create a new Key instance.
-
#project ⇒ Object
The Datastore project connected to.
-
#query(*kinds) ⇒ Object
Create a new Query instance.
-
#run(query, namespace: nil) ⇒ Object
(also: #run_query)
Retrieve entities specified by a Query.
-
#save(*entities) ⇒ Object
Persist one or more entities to the Datastore.
-
#transaction ⇒ Object
Creates a Datastore Transaction.
Constructor Details
#initialize(project, credentials) ⇒ Dataset
Creates a new Dataset instance.
See Gcloud#datastore
55 56 57 58 59 |
# File 'lib/gcloud/datastore/dataset.rb', line 55 def initialize project, credentials #:nodoc: 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
:nodoc:
49 50 51 |
# File 'lib/gcloud/datastore/dataset.rb', line 49 def connection @connection end |
Class Method Details
.default_project ⇒ Object
Default project.
80 81 82 83 84 85 86 |
# File 'lib/gcloud/datastore/dataset.rb', line 80 def self.default_project #:nodoc: 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) ⇒ Object
Generate IDs for a Key before creating an entity.
Parameters
incomplete_key-
A Key without
idornameset. (Key) count-
The number of new key IDs to create. (
Integer)
Returns
Array of Gcloud::Datastore::Key
Example
empty_key = dataset.key "Task"
task_keys = dataset.allocate_ids empty_key, 5
107 108 109 110 111 112 113 114 115 116 117 |
# 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 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) ⇒ Object
Remove entities from the Datastore.
Parameters
entities_or_keys-
One or more Entity or Key objects to remove. (
EntityorKey)
Returns
true if successful
Example
gcloud = Gcloud.new
dataset = gcloud.datastore
dataset.delete entity1, entity2
226 227 228 229 230 231 232 233 234 235 |
# File 'lib/gcloud/datastore/dataset.rb', line 226 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| ... } ⇒ Object
Create a new empty Entity instance. This is a convenience method to make the creation of Entity objects easier.
Parameters
key_or_kind-
A Key object or
kindstring value. This is optional. (KeyorStringornil) id_or_name-
The Key’s
idornamevalue if akindwas provided in the first parameter. (IntegerorStringornil)
Returns
Gcloud::Datastore::Entity
Examples
entity = dataset.entity
This code is equivalent to the following:
entity = Gcloud::Datastore::Entity.new
The key can also be passed in as an object:
key = dataset.key "User", "[email protected]"
entity = dataset.entity key
Or the key values can be passed in as parameters:
entity = dataset.entity "User", "[email protected]"
This code is equivalent to the following:
key = Gcloud::Datastore::Key.new "User", "[email protected]"
entity = Gcloud::Datastore::Entity.new
entity.key = key
The newly created entity object can also be configured using a block:
user = dataset.entity "User", "[email protected]" do |u|
u["name"] = "Heidi Henderson"
end
This code is equivalent to the following:
key = Gcloud::Datastore::Key.new "User", "[email protected]"
entity = Gcloud::Datastore::Entity.new
entity.key = key
entity["name"] = "Heidi Henderson"
444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/gcloud/datastore/dataset.rb', line 444 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) ⇒ Object Also known as: get
Retrieve an entity by providing key information.
Parameters
key_or_kind-
A Key object or
kindstring value. (KeyorString) id_or_name-
The Key’s
idornamevalue if akindwas provided in the first parameter. (IntegerorStringornil)
Returns
Gcloud::Datastore::Entity or nil
Example
Finding an entity with a key:
key = dataset.key "Task", 123456
task = dataset.find key
Finding an entity with a kind and id/name:
task = dataset.find "Task", 123456
170 171 172 173 174 175 176 |
# File 'lib/gcloud/datastore/dataset.rb', line 170 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) ⇒ Object Also known as: lookup
Retrieve the entities for the provided keys.
Parameters
keys-
One or more Key objects to find records for. (
Key)
Returns
Gcloud::Datastore::Dataset::LookupResults
Example
gcloud = Gcloud.new
dataset = gcloud.datastore
key1 = dataset.key "Task", 123456
key2 = dataset.key "Task", 987654
tasks = dataset.find_all key1, key2
199 200 201 202 203 204 205 |
# File 'lib/gcloud/datastore/dataset.rb', line 199 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) ⇒ Object
Create a new Key instance. This is a convenience method to make the creation of Key objects easier.
Parameters
kind-
The kind of the Key. This is optional. (
String) id_or_name-
The id or name of the Key. This is optional. (
IntegerorString)
Returns
Gcloud::Datastore::Key
Example
key = dataset.key "User", "[email protected]"
This code is equivalent to the following:
key = Gcloud::Datastore::Key.new "User", "[email protected]"
387 388 389 |
# File 'lib/gcloud/datastore/dataset.rb', line 387 def key kind = nil, id_or_name = nil Key.new kind, id_or_name end |
#project ⇒ Object
The Datastore project connected to.
Example
require "gcloud"
gcloud = Gcloud.new "my-todo-project",
"/path/to/keyfile.json"
dataset = gcloud.datastore
dataset.project #=> "my-todo-project"
74 75 76 |
# File 'lib/gcloud/datastore/dataset.rb', line 74 def project connection.dataset_id end |
#query(*kinds) ⇒ Object
Create a new Query instance. This is a convenience method to make the creation of Query objects easier.
Parameters
kinds-
The kind of entities to query. This is optional. (
String)
Returns
Gcloud::Datastore::Query
Example
query = dataset.query("Task").
where("completed", "=", true)
tasks = dataset.run query
This code is equivalent to the following:
query = Gcloud::Datastore::Query.new.
kind("Task").
where("completed", "=", true)
tasks = dataset.run query
358 359 360 361 362 |
# File 'lib/gcloud/datastore/dataset.rb', line 358 def query *kinds query = Query.new query.kind(*kinds) unless kinds.empty? query end |
#run(query, namespace: nil) ⇒ Object Also known as: run_query
Retrieve entities specified by a Query.
Parameters
query-
The Query object with the search criteria. (
Query) namespace-
The namespace the query is to run within. (
String)
Returns
Gcloud::Datastore::Dataset::QueryResults
Examples
query = dataset.query("Task").
where("completed", "=", true)
tasks = dataset.run query
The query can optionally run within namespace when the namespace option is provided:
query = Gcloud::Datastore::Query.new.kind("Task").
where("completed", "=", true)
tasks = dataset.run query, namespace: "ns~todo-project"
264 265 266 267 268 269 270 271 |
# File 'lib/gcloud/datastore/dataset.rb', line 264 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) ⇒ Object
Persist one or more entities to the Datastore.
Parameters
entities-
One or more entity objects to be saved without
idornameset. (Entity)
Returns
Array of Gcloud::Datastore::Entity
Example
dataset.save task1, task2
136 137 138 139 140 141 142 |
# File 'lib/gcloud/datastore/dataset.rb', line 136 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 ⇒ Object
Creates a Datastore Transaction.
Example
Runs the given block in a database transaction:
require "gcloud"
gcloud = Gcloud.new
dataset = gcloud.datastore
user = dataset.entity "User", "heidi" do |u|
u["name"] = "Heidi Henderson"
u["email"] = "[email protected]"
end
dataset.transaction do |tx|
if tx.find(user.key).nil?
tx.save user
end
end
Alternatively, if no block is given a Transaction object is returned:
require "gcloud"
gcloud = Gcloud.new
dataset = gcloud.datastore
user = dataset.entity "User", "heidi" do |u|
u["name"] = "Heidi Henderson"
u["email"] = "[email protected]"
end
tx = dataset.transaction
begin
if tx.find(user.key).nil?
tx.save user
end
tx.commit
rescue
tx.rollback
end
319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/gcloud/datastore/dataset.rb', line 319 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 |