class E3DB::Client::Result

A set of records returned by {Client#query}. This implements the `Enumerable` interface which can be used to loop over the records in the result set (using eg: `Enumerable#each`).

Every traversal of the result set will execute a query to the server, so if multiple in-memory traversals are needed, use `Enumerable#to_a` to fetch all records into an array first.

Public Class Methods

new(client, query) click to toggle source
# File lib/e3db/client.rb, line 512
def initialize(client, query)
  @client = client
  @query = query
end

Public Instance Methods

each() { |record| ... } click to toggle source

Invoke a block for each record matching a query.

# File lib/e3db/client.rb, line 518
def each
  # Every invocation of 'each' gets its own copy of the query since
  # it will be modified as we loop through the result pages. This
  # allows multiple traversals of the same result set to start from
  # the beginning each time.
  q = Query.new(@query.to_hash)
  loop do
    json = @client.instance_eval { query1(q) }
    results = json[:results]
    results.each do |r|
      if q.include_data
        record = @client.decrypt_record(Record.new({ :meta => r[:meta], :data => r[:record_data] }), EAK.new(r[:access_key]))
      else
        record = Record.new(data: Hash.new, meta: Meta.new(r[:meta]))
      end

      yield record
    end

    if results.length < q.count
      break
    end

    q.after_index = json[:last_index]
  end
end