Class: Gcloud::Datastore::Dataset

Inherits:
Object
  • Object
show all
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

Examples:

require "gcloud"

gcloud = Gcloud.new
dataset = gcloud.datastore

query = dataset.query("Task").
  where("completed", "=", true)

tasks = dataset.run query

Direct Known Subclasses

Transaction

Defined Under Namespace

Classes: LookupResults, QueryResults

Instance Method Summary collapse

Instance Method Details

#allocate_ids(incomplete_key, count = 1) ⇒ Array<Gcloud::Datastore::Key>

Generate IDs for a Key before creating an entity.

Examples:

empty_key = dataset.key "Task"
task_keys = dataset.allocate_ids empty_key, 5

Parameters:

  • incomplete_key (Key)

    A Key without id or name set.

  • count (String) (defaults to: 1)

    The number of new key IDs to create.

Returns:


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.

Examples:

gcloud = Gcloud.new
dataset = gcloud.datastore
dataset.delete entity1, entity2

Parameters:

  • *entities_or_keys (Entity, Key)

    One or more Entity or Key objects to remove.

Returns:

  • (Boolean)

    Returns true if successful


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

Examples:

entity = dataset.entity

The previous example is equivalent to:

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]"

The previous example is equivalent to:

key = Gcloud::Datastore::Key.new "User", "[email protected]"
entity = Gcloud::Datastore::Entity.new
entity.key = key

The newly created entity can also be configured using a block:

user = dataset.entity "User", "[email protected]" do |u|
  u["name"] = "Heidi Henderson"

The previous example is equivalent to:

key = Gcloud::Datastore::Key.new "User", "[email protected]"
entity = Gcloud::Datastore::Entity.new
entity.key = key
entity["name"] = "Heidi Henderson"

Parameters:

  • key_or_kind (Key, String, nil) (defaults to: nil)

    A Key object or kind string value. This is optional.

  • id_or_name (Integer, String, nil) (defaults to: nil)

    The Key's id or name value if a kind was provided in the first parameter.

Yields:

Returns:


372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/gcloud/datastore/dataset.rb', line 372

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.

Examples:

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

Parameters:

  • key_or_kind (Key, String)

    A Key object or kind string value.

  • id_or_name (Integer, String, nil) (defaults to: nil)

    The Key's id or name value if a kind was provided in the first parameter.

Returns:


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.

Examples:

gcloud = Gcloud.new
dataset = gcloud.datastore
key1 = dataset.key "Task", 123456
key2 = dataset.key "Task", 987654
tasks = dataset.find_all key1, key2

Parameters:

  • *keys (Key)

    One or more Key objects to find records for.

Returns:


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.

Examples:

key = dataset.key "User", "[email protected]"

The previous example is equivalent to:

key = Gcloud::Datastore::Key.new "User", "[email protected]"

Parameters:

  • kind (String) (defaults to: nil)

    The kind of the Key. This is optional.

  • id_or_name (Integer, String) (defaults to: nil)

    The id or name of the Key. This is optional.

Returns:


328
329
330
# File 'lib/gcloud/datastore/dataset.rb', line 328

def key kind = nil, id_or_name = nil
  Key.new kind, id_or_name
end

#projectObject

The Datastore project connected to.

Examples:

require "gcloud"

gcloud = Gcloud.new "my-todo-project",
                    "/path/to/keyfile.json"

dataset = gcloud.datastore
dataset.project #=> "my-todo-project"

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.

Examples:

query = dataset.query("Task").
  where("completed", "=", true)
tasks = dataset.run query

The previous example is equivalent to:

query = Gcloud::Datastore::Query.new.
  kind("Task").
  where("completed", "=", true)
tasks = dataset.run query

Parameters:

  • *kinds (String)

    The kind of entities to query. This is optional.

Returns:


306
307
308
309
310
# File 'lib/gcloud/datastore/dataset.rb', line 306

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.

Examples:

query = dataset.query("Task").
  where("completed", "=", true)
tasks = dataset.run query

Run the query within a namespace with the namespace option:

query = Gcloud::Datastore::Query.new.kind("Task").
  where("completed", "=", true)
tasks = dataset.run query, namespace: "ns~todo-project"

Parameters:

  • query (Query)

    The Query object with the search criteria.

  • namespace (String)

    The namespace the query is to run within.

Returns:


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.

Examples:

dataset.save task1, task2

Parameters:

  • *entities (Entity)

    One or more entity objects to be saved without id or name set.

Returns:


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

#transactionObject

Creates a Datastore Transaction.

Examples:

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

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

274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/gcloud/datastore/dataset.rb', line 274

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