class Riak::MapReduce::Results

@api private Collects and normalizes results from MapReduce requests

Public Class Methods

new(mr) click to toggle source

Creates a new result collector @param [MapReduce] mr the MapReduce query

# File lib/riak/map_reduce/results.rb, line 8
def initialize(mr)
  @keep_count = mr.query.select {|p| p.keep }.size
  @hash = create_results_hash(mr.query)
end

Public Instance Methods

add(phase, result) click to toggle source

Adds a new result to the collector @param [Fixnum] phase the phase index @param [Array] result the phase result

# File lib/riak/map_reduce/results.rb, line 16
def add(phase, result)
  @hash[phase] += result
end
report() click to toggle source

Coalesces the query results @return [Array] the query results, coalesced according to the

phase configuration
# File lib/riak/map_reduce/results.rb, line 23
def report
  if @keep_count > 1
    @hash.to_a.sort.map {|(num, results)| results }
  else
    @hash[@hash.keys.first]
  end
end

Private Instance Methods

create_results_hash(query) click to toggle source
# File lib/riak/map_reduce/results.rb, line 32
def create_results_hash(query)
  # When the query is empty, only bucket/key pairs are returned,
  # but implicitly in phase 0.
  return { 0 => [] } if query.empty?

  # Pre-populate the hash with empty results for kept phases.
  # Additionally, the last phase is always implictly kept, even
  # when keep is false.
  query.inject({}) do |hash, phase|
    if phase.keep || query[-1] == phase
      hash[query.index(phase)] = []
    end
    hash
  end
end