class MultiRedis::Executor

Constants

TYPES

Public Class Methods

new(options = {}) click to toggle source
# File lib/multi_redis/executor.rb, line 5
def initialize options = {}
  @operations = []
  @redis = options[:redis]
end

Public Instance Methods

add(operation, *args) click to toggle source
# File lib/multi_redis/executor.rb, line 10
def add operation, *args
  @operations << { op: operation, args: args }
end
execute(options = {}) click to toggle source
# File lib/multi_redis/executor.rb, line 14
def execute options = {}

  redis = @redis || MultiRedis.redis
  shared_context = Context.new redis

  total = 0
  execution = @operations.collect do |operation|
    total += operation[:op].steps.length
    OperationExecution.new operation[:op], operation[:args], shared_context
  end

  while execution.any?{ |oe| !oe.done? } && total >= 1
    total -= 1 # safeguard against infinite loop

    TYPES.each do |type|

      execution.each do |oe|
        oe.execute_current_step while oe.next? :call
      end

      if execution.any?{ |oe| oe.next? type }
        shared_context.last_replies.clear
        redis.send type do
          execution.each do |oe|
            oe.execute_current_step if oe.next? type
          end
        end
        execution.each{ |oe| oe.resolve_futures! }
      end
    end
  end

  execution.each{ |oe| oe.resolve_operation_future! }
  execution.collect!{ |oe| oe.final_results }
end