trafficbroker-mandy 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/mappers/base_mapper.rb +6 -1
- data/lib/reducers/base_reducer.rb +6 -1
- data/lib/support/array_serializer.rb +10 -0
- data/lib/support/tuple.rb +13 -2
- data/lib/test_runner.rb +42 -6
- metadata +1 -1
data/lib/mappers/base_mapper.rb
CHANGED
@@ -27,7 +27,7 @@ module Mandy
|
|
27
27
|
|
28
28
|
def emit(key, value=nil)
|
29
29
|
key = 'nil' if key.nil?
|
30
|
-
@output.puts(value.nil? ? key.to_s : "#{key}\t#{value}")
|
30
|
+
@output.puts(value.nil? ? key.to_s : "#{serialize(key)}\t#{serialize(value)}")
|
31
31
|
end
|
32
32
|
|
33
33
|
private
|
@@ -35,6 +35,11 @@ module Mandy
|
|
35
35
|
def mapper(key,value)
|
36
36
|
#nil
|
37
37
|
end
|
38
|
+
|
39
|
+
def serialize(value)
|
40
|
+
value = ArraySerializer.new(value) if value.is_a?(Array)
|
41
|
+
value.to_s
|
42
|
+
end
|
38
43
|
end
|
39
44
|
end
|
40
45
|
end
|
@@ -32,7 +32,7 @@ module Mandy
|
|
32
32
|
|
33
33
|
def emit(key, value=nil)
|
34
34
|
key = 'nil' if key.nil?
|
35
|
-
@output.puts(value.nil? ? key.to_s : "#{key}\t#{value}")
|
35
|
+
@output.puts(value.nil? ? key.to_s : "#{serialize(key)}\t#{serialize(value)}")
|
36
36
|
end
|
37
37
|
|
38
38
|
private
|
@@ -40,6 +40,11 @@ module Mandy
|
|
40
40
|
def reducer(key,values)
|
41
41
|
#nil
|
42
42
|
end
|
43
|
+
|
44
|
+
def serialize(value)
|
45
|
+
value = ArraySerializer.new(value) if value.is_a?(Array)
|
46
|
+
value.to_s
|
47
|
+
end
|
43
48
|
end
|
44
49
|
end
|
45
50
|
end
|
@@ -3,6 +3,8 @@ module Mandy
|
|
3
3
|
|
4
4
|
SEPERATOR = '|' unless defined?(SEPERATOR)
|
5
5
|
|
6
|
+
attr_reader :items
|
7
|
+
|
6
8
|
def initialize(items)
|
7
9
|
@items = items || []
|
8
10
|
end
|
@@ -11,6 +13,14 @@ module Mandy
|
|
11
13
|
@items.join(SEPERATOR)
|
12
14
|
end
|
13
15
|
|
16
|
+
def ==(other)
|
17
|
+
(self.class == other.class && self.items == other.items) || (other.is_a?(Array) && self.items == other)
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_a
|
21
|
+
@items
|
22
|
+
end
|
23
|
+
|
14
24
|
def self.from_s(str)
|
15
25
|
str.split(SEPERATOR)
|
16
26
|
end
|
data/lib/support/tuple.rb
CHANGED
@@ -5,8 +5,10 @@ module Mandy
|
|
5
5
|
|
6
6
|
attr_accessor :name, :value
|
7
7
|
|
8
|
-
def initialize(name, value)
|
8
|
+
def initialize(name, value, name_accessor = nil, value_accessor = nil)
|
9
9
|
@name, @value = name, value
|
10
|
+
alias_accessor(name_accessor, :name) unless name_accessor.nil?
|
11
|
+
alias_accessor(value_accessor, :value) unless value_accessor.nil?
|
10
12
|
end
|
11
13
|
|
12
14
|
def to_s
|
@@ -14,7 +16,9 @@ module Mandy
|
|
14
16
|
end
|
15
17
|
|
16
18
|
def self.from_s(str)
|
17
|
-
|
19
|
+
parts = str.split(SEPERATOR)
|
20
|
+
raise "Can't create tuple from #{str.inspect}. Format should be 'A#{SEPERATOR}B'" unless parts.size==2
|
21
|
+
new(*parts)
|
18
22
|
end
|
19
23
|
|
20
24
|
def inspect
|
@@ -25,5 +29,12 @@ module Mandy
|
|
25
29
|
return false unless self.class == other.class
|
26
30
|
self.name == other.name && self.value == other.value
|
27
31
|
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def alias_accessor(new_accessor, old_accessor)
|
36
|
+
self.class.send(:alias_method, new_accessor, old_accessor)
|
37
|
+
self.class.send(:alias_method, :"#{new_accessor}=", :"#{old_accessor}=")
|
38
|
+
end
|
28
39
|
end
|
29
40
|
end
|
data/lib/test_runner.rb
CHANGED
@@ -1,25 +1,31 @@
|
|
1
1
|
module Mandy
|
2
2
|
class TestRunner
|
3
|
+
attr_reader :job
|
4
|
+
|
3
5
|
def initialize(job=Mandy::Job.jobs.first.name)
|
4
6
|
@job = Mandy::Job.find_by_name(job)
|
5
7
|
end
|
6
8
|
|
7
|
-
def map(
|
8
|
-
|
9
|
-
input_stream = StringIO.new(
|
9
|
+
def map(input_stream, output_stream=StringIO.new(''), &blk)
|
10
|
+
input_stream = input_from_array(input_stream) if input_stream.is_a?(Array)
|
11
|
+
input_stream = StringIO.new(input_stream.to_s) unless input_stream.is_a?(StringIO)
|
10
12
|
@job.run_map(input_stream, output_stream, &blk)
|
11
13
|
output_stream.rewind
|
12
14
|
output_stream
|
13
15
|
end
|
14
16
|
|
15
|
-
def reduce(
|
16
|
-
|
17
|
-
input_stream = StringIO.new(
|
17
|
+
def reduce(input_stream, output_stream=StringIO.new(''), &blk)
|
18
|
+
input_stream = input_from_hash(input_stream) if input_stream.is_a?(Hash)
|
19
|
+
input_stream = StringIO.new(input_stream.to_s) unless input_stream.is_a?(StringIO)
|
18
20
|
@job.run_reduce(input_stream, output_stream, &blk)
|
19
21
|
output_stream.rewind
|
20
22
|
output_stream
|
21
23
|
end
|
22
24
|
|
25
|
+
def self.end_to_end(verbose=false)
|
26
|
+
CompositeJobRunner.new(Mandy::Job.jobs,verbose)
|
27
|
+
end
|
28
|
+
|
23
29
|
private
|
24
30
|
|
25
31
|
def input_from_array(input)
|
@@ -34,5 +40,35 @@ module Mandy
|
|
34
40
|
end
|
35
41
|
input_from_array(output.sort)
|
36
42
|
end
|
43
|
+
|
44
|
+
class CompositeJobRunner
|
45
|
+
def initialize(jobs, verbose=false)
|
46
|
+
@jobs = jobs
|
47
|
+
@verbose = verbose
|
48
|
+
@job_runners = @jobs.map { |job| Mandy::TestRunner.new(job.name) }
|
49
|
+
end
|
50
|
+
|
51
|
+
def execute(input_stream, output_stream=StringIO.new(''))
|
52
|
+
map_temp = StringIO.new('')
|
53
|
+
reduce_temp = StringIO.new('')
|
54
|
+
@job_runners.each_with_index do |runner, index|
|
55
|
+
runner.map(input_stream, map_temp)
|
56
|
+
if @verbose
|
57
|
+
puts "#{runner.job.name} [MAP] #{map_temp.readlines.inspect}"
|
58
|
+
map_temp.rewind
|
59
|
+
end
|
60
|
+
reduce_input = StringIO.new(map_temp.readlines.sort.join(''))
|
61
|
+
runner.reduce(reduce_input, (index==@job_runners.size-1 ? output_stream : reduce_temp))
|
62
|
+
if @verbose
|
63
|
+
puts "#{runner.job.name} [RED] #{reduce_temp.readlines.inspect}"
|
64
|
+
reduce_temp.rewind
|
65
|
+
end
|
66
|
+
input_stream = reduce_temp
|
67
|
+
map_temp = StringIO.new('')
|
68
|
+
reduce_temp = StringIO.new('')
|
69
|
+
end
|
70
|
+
output_stream
|
71
|
+
end
|
72
|
+
end
|
37
73
|
end
|
38
74
|
end
|