Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split classes and improved executor.
- Loading branch information
1 parent
9da6259
commit 4fc820a
Showing
15 changed files
with
409 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
module MultiRedis | ||
|
||
class Executor | ||
|
||
def initialize options = {} | ||
@operations = [] | ||
@redis = options[:redis] | ||
end | ||
|
||
def add operation, *args | ||
@operations << { op: operation, args: args } | ||
end | ||
|
||
def execute options = {} | ||
|
||
redis = @redis || MultiRedis.redis | ||
|
||
total = 0 | ||
execution = Array.new @operations.length do |i| | ||
total += @operations[i][:op].steps.length | ||
OperationExecution.new @operations[i][:op], @operations[i][:args], redis | ||
end | ||
|
||
while execution.any?{ |oe| !oe.done? } && total >= 1 | ||
total -= 1 # safeguard against infinite loop | ||
|
||
execution.each do |oe| | ||
oe.execute_current_step while oe.next? :call | ||
end | ||
|
||
TYPES.each do |type| | ||
if execution.any?{ |oe| oe.next? type } | ||
shared_results = [] # TODO: use shared context object | ||
redis.send type do | ||
execution.each do |oe| | ||
oe.execute_current_step shared_results 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 | ||
|
||
private | ||
|
||
TYPES = [ :pipelined, :multi ] | ||
|
||
class OperationExecution | ||
attr_reader :final_results | ||
|
||
def initialize operation, args, redis | ||
|
||
@operation = operation | ||
@args = args | ||
|
||
@context = Context.new redis | ||
@steps = operation.steps | ||
|
||
@current_index = 0 | ||
end | ||
|
||
def done? | ||
!current_step | ||
end | ||
|
||
def next? type | ||
current_step && current_step.type == type | ||
end | ||
|
||
def execute_current_step shared_results = nil | ||
results = @context.execute shared_results, current_step, *@args | ||
@current_index += 1 | ||
@final_results = results | ||
end | ||
|
||
def resolve_futures! | ||
@context.resolve_futures! | ||
end | ||
|
||
def resolve_operation_future! | ||
@operation.future.value = @final_results if @operation.future | ||
end | ||
|
||
private | ||
|
||
def current_step | ||
@steps[@current_index] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module MultiRedis | ||
|
||
module Extension | ||
|
||
def multi_redis_operation symbol, options = {}, &block | ||
op = Operation.new options.merge(target: self), &block | ||
define_method symbol do |*args| | ||
op.execute *args | ||
end | ||
self | ||
end | ||
end | ||
end |
Oops, something went wrong.