Class: Gcloud::Datastore::Dataset::LookupResults

Inherits:
Array
  • Object
show all
Defined in:
lib/gcloud/datastore/dataset/lookup_results.rb

Overview

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.

Examples:

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:

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arr = []) ⇒ LookupResults

Returns a new instance of LookupResults.



57
58
59
# File 'lib/gcloud/datastore/dataset/lookup_results.rb', line 57

def initialize arr = []
  super arr
end

Instance Attribute Details

#deferredObject

Keys that were not looked up due to resource constraints.



49
50
51
# File 'lib/gcloud/datastore/dataset/lookup_results.rb', line 49

def deferred
  @deferred
end

#missingObject

Entities not found, with only the key populated.



53
54
55
# File 'lib/gcloud/datastore/dataset/lookup_results.rb', line 53

def missing
  @missing
end

Class Method Details

.from_grpc(lookup_res, service, consistency = nil, tx = nil) ⇒ Object

Google::Dataset::V1beta3::LookupResponse object.



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/gcloud/datastore/dataset/lookup_results.rb', line 177

def self.from_grpc lookup_res, service, consistency = nil, tx = nil
  entities = to_gcloud_entities lookup_res.found
  deferred = to_gcloud_keys lookup_res.deferred
  missing  = to_gcloud_entities lookup_res.missing
  new(entities).tap do |lr|
    lr.instance_variable_set :@service,     service
    lr.instance_variable_set :@consistency, consistency
    lr.instance_variable_set :@transaction, tx
    lr.instance_variable_set :@deferred,    deferred
    lr.instance_variable_set :@missing,     missing
  end
end

Instance Method Details

#all(request_limit: nil) {|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.

Examples:

Iterating each result by passing a block:

gcloud = Gcloud.new
datastore = gcloud.datastore

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

Using the enumerator by not passing a block:

gcloud = Gcloud.new
datastore = gcloud.datastore

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).each do |task|
  task.key
end

Limit the number of API calls made:

gcloud = Gcloud.new
datastore = gcloud.datastore

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 |task|
  puts "Task #{task.key.id} (#cursor)"
end

Parameters:

  • 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 Parameters:

  • result (Entity)

    The lookup result object.

Returns:

  • (Enumerator)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/gcloud/datastore/dataset/lookup_results.rb', line 157

def all request_limit: nil
  request_limit = request_limit.to_i if request_limit
  unless block_given?
    return enum_for(:all, request_limit: request_limit)
  end
  results = self
  loop do
    results.each { |r| yield r }
    if request_limit
      request_limit -= 1
      break if request_limit < 0
    end
    break unless results.next?
    results = results.next
  end
end

#nextLookupResults

Retrieve the next page of results.

Examples:

gcloud = Gcloud.new
datastore = gcloud.datastore

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

Returns:



97
98
99
100
101
102
103
104
# File 'lib/gcloud/datastore/dataset/lookup_results.rb', line 97

def next
  return nil unless next?
  ensure_service!
  lookup_res = @service.lookup(*Array(@deferred).flatten.map(&:to_grpc),
                               consistency: @consistency,
                               transaction: @transaction)
  self.class.from_grpc lookup_res, @service, @consistency
end

#next?Boolean

Whether there are more results available.

Examples:

gcloud = Gcloud.new
datastore = gcloud.datastore

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

Returns:

  • (Boolean)


77
78
79
# File 'lib/gcloud/datastore/dataset/lookup_results.rb', line 77

def next?
  Array(@deferred).any?
end