Class: MultiRedis::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_redis/executor.rb

Defined Under Namespace

Classes: OperationExecution

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Executor

Returns a new instance of Executor.


5
6
7
8
# File 'lib/multi_redis/executor.rb', line 5

def initialize options = {}
  @operations = []
  @redis = options[:redis]
end

Instance Method Details

#add(operation, *args) ⇒ Object


10
11
12
# File 'lib/multi_redis/executor.rb', line 10

def add operation, *args
  @operations << { op: operation, args: args }
end

#execute(options = {}) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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