Class: E3DB::Client::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/e3db/client.rb

Overview

A set of records returned by #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.

Instance Method Summary collapse

Constructor Details

#initialize(client, query) ⇒ Result

Returns a new instance of Result.



512
513
514
515
# File 'lib/e3db/client.rb', line 512

def initialize(client, query)
  @client = client
  @query = query
end

Instance Method Details

#eachObject

Invoke a block for each record matching a query.



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# 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