google-cloud-datastore - Class Google::Cloud::Datastore::Dataset::LookupResults (v2.2.4)

Reference documentation and code samples for the google-cloud-datastore class Google::Cloud::Datastore::Dataset::LookupResults.

LookupResults is a special case Array with additional values. A LookupResults object is returned from Dataset#find_all and contains the entities as well as the Keys that were deferred from the results and the Entities that were missing in the dataset.

Please be cautious when treating the QueryResults as an Array. Many common Array methods will return a new Array instance.

Inherits

  • Array

Examples

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

tasks = datastore.find_all task_key1, task_key2, task_key3
tasks.size #=> 3
tasks.deferred #=> []
tasks.missing #=> []

Caution, many Array methods will return a new Array instance:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

tasks = datastore.find_all task_key1, task_key2, task_key3
tasks.size #=> 3
tasks.deferred #=> []
tasks.missing #=> []
descriptions = tasks.map { |t| t["description"] }
descriptions.size #=> 3
descriptions.deferred #=> raise NoMethodError
descriptions.missing #=> raise NoMethodError

Methods

#all

def all(request_limit: nil, &block) { |result| ... } -> Enumerator

Retrieves all lookup results by repeatedly loading #next until #next? returns false. Calls the given block once for each result, which is passed as the parameter.

An Enumerator is returned if no block is given.

This method may make several API calls until all lookup results are retrieved. Be sure to use as narrow a search criteria as possible. Please use with caution.

Parameter
  • request_limit (Integer) (defaults to: nil) — The upper limit of API requests to make to load all lookup results. Default is no limit.
Yields
  • (result) — The block for accessing each lookup result.
Yield Parameter
  • result (Entity) — The lookup result object.
Returns
  • (Enumerator)
Examples

Iterating each result by passing a block:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

task_key1 = datastore.key "Task", "sampleTask1"
task_key2 = datastore.key "Task", "sampleTask2"
tasks = datastore.find_all task_key1, task_key2
tasks.all do |t|
  puts "Task #{t.key.id} (#cursor)"
end

Using the enumerator by not passing a block:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

task_key1 = datastore.key "Task", "sampleTask1"
task_key2 = datastore.key "Task", "sampleTask2"
tasks = datastore.find_all task_key1, task_key2
all_keys = tasks.all.map(&:key)

Limit the number of API calls made:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

task_key1 = datastore.key "Task", "sampleTask1"
task_key2 = datastore.key "Task", "sampleTask2"
tasks = datastore.find_all task_key1, task_key2
tasks.all(request_limit: 10) do |t|
  puts "Task #{t.key.id} (#cursor)"
end

#deferred

def deferred()

Keys that were not looked up due to resource constraints.

#deferred=

def deferred=(value)

Keys that were not looked up due to resource constraints.

#missing

def missing()

Entities not found, with only the key populated.

#missing=

def missing=(value)

Entities not found, with only the key populated.

#next

def next() -> LookupResults

Retrieve the next page of results.

Returns
Example
require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

task_key1 = datastore.key "Task", "sampleTask1"
task_key2 = datastore.key "Task", "sampleTask2"
tasks = datastore.find_all task_key1, task_key2
if tasks.next?
  next_tasks = tasks.next
end

#next?

def next?() -> Boolean

Whether there are more results available.

Returns
  • (Boolean)
Example
require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

task_key1 = datastore.key "Task", "sampleTask1"
task_key2 = datastore.key "Task", "sampleTask2"
tasks = datastore.find_all task_key1, task_key2
if tasks.next?
  next_tasks = tasks.next
end