mandy 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/job.rb CHANGED
@@ -17,8 +17,6 @@ module Mandy
17
17
  @name = name
18
18
  @settings = {}
19
19
  @modules = []
20
- @mapper_class = Mandy::Mappers::PassThroughMapper
21
- @reducer_class = Mandy::Reducers::PassThroughReducer
22
20
  set('mapred.job.name', name)
23
21
  instance_eval(&blk) if blk
24
22
  end
@@ -57,35 +55,67 @@ module Mandy
57
55
  end
58
56
  end
59
57
 
58
+ def setup(&blk)
59
+ @setup = blk
60
+ end
61
+
62
+ def teardown(&blk)
63
+ @teardown = blk
64
+ end
65
+
60
66
  def map(klass=nil, &blk)
61
- @mapper_class = klass || Mandy::Mappers::Base.compile(&blk)
62
- @modules.each {|m| @mapper_class.send(:include, m) }
63
- @mapper_class
67
+ @map = klass || blk
64
68
  end
65
69
 
66
70
  def reduce(klass=nil, &blk)
67
- @reducer_class = klass || Mandy::Reducers::Base.compile(&blk)
68
- @modules.each {|m| @reducer_class.send(:include, m) }
69
- @reducer_class
71
+ @reduce = klass || blk
70
72
  end
71
73
 
72
74
  def run_map(input=STDIN, output=STDOUT, &blk)
73
- @mapper_class.send(:include, Mandy::IO::OutputFormatting) unless reducer_defined?
74
- mapper = @mapper_class.new(input, output, @input_format, @output_format)
75
+ mapper_class.send(:include, Mandy::IO::OutputFormatting) unless reducer_defined?
76
+ mapper = mapper_class.new(input, output, @input_format, @output_format)
75
77
  yield(mapper) if blk
76
78
  mapper.execute
77
79
  end
78
80
 
79
81
  def run_reduce(input=STDIN, output=STDOUT, &blk)
80
- reducer = @reducer_class.new(input, output, @input_format, @output_format)
82
+ reducer = reducer_class.new(input, output, @input_format, @output_format)
81
83
  yield(reducer) if blk
82
84
  reducer.execute
83
85
  end
84
86
 
85
87
  private
88
+
89
+ def mapper_class
90
+ return Mandy::Mappers::PassThroughMapper unless @map
91
+ @mapper_class ||= compile_map
92
+ end
86
93
 
94
+ def reducer_class
95
+ return Mandy::Reducers::PassThroughReducer unless @reduce
96
+ @reducer_class ||= compile_reduce
97
+ end
98
+
99
+ def compile_map
100
+ args = {}
101
+ args[:setup] = @setup if @setup
102
+ args[:teardown] = @teardown if @teardown
103
+ @mapper_class = @map.is_a?(Proc) ? Mandy::Mappers::Base.compile(args, &@map) : @map
104
+ @modules.each {|m| @mapper_class.send(:include, m) }
105
+ @mapper_class
106
+ end
107
+
108
+ def compile_reduce
109
+ args = {}
110
+ args[:setup] = @setup if @setup
111
+ args[:teardown] = @teardown if @teardown
112
+ @reducer_class = @reduce.is_a?(Proc) ? Mandy::Reducers::Base.compile(args, &@reduce) : @reduce
113
+ @modules.each {|m| @reducer_class.send(:include, m) }
114
+ @reducer_class
115
+ end
116
+
87
117
  def reducer_defined?
88
- @reducer_class != Mandy::Reducers::PassThroughReducer
118
+ reducer_class != Mandy::Reducers::PassThroughReducer
89
119
  end
90
120
 
91
121
  end
@@ -3,15 +3,18 @@ module Mandy
3
3
  class Base < Mandy::Task
4
4
  include Mandy::IO::InputFormatting
5
5
 
6
- def self.compile(&blk)
6
+ def self.compile(opts={}, &blk)
7
7
  Class.new(Mandy::Mappers::Base) do
8
8
  self.class_eval do
9
9
  define_method(:mapper, blk) if blk
10
+ define_method(:setup, opts[:setup]) if opts[:setup]
11
+ define_method(:teardown, opts[:teardown]) if opts[:teardown]
10
12
  end
11
13
  end
12
14
  end
13
15
 
14
16
  def execute
17
+ setup if self.respond_to?(:setup)
15
18
  @input.each_line do |line|
16
19
  key, value = line.split(KEY_VALUE_SEPERATOR, 2)
17
20
  key, value = nil, key if value.nil?
@@ -19,6 +22,7 @@ module Mandy
19
22
  args = [input_deserialize_key(key), input_deserialize_value(value)].compact
20
23
  mapper(*args)
21
24
  end
25
+ teardown if self.respond_to?(:teardown)
22
26
  end
23
27
 
24
28
  private
@@ -3,15 +3,18 @@ module Mandy
3
3
  class Base < Mandy::Task
4
4
  include Mandy::IO::OutputFormatting
5
5
 
6
- def self.compile(&blk)
6
+ def self.compile(opts={}, &blk)
7
7
  Class.new(Mandy::Reducers::Base) do
8
8
  self.class_eval do
9
9
  define_method(:reducer, blk) if blk
10
+ define_method(:setup, opts[:setup]) if opts[:setup]
11
+ define_method(:teardown, opts[:teardown]) if opts[:teardown]
10
12
  end
11
13
  end
12
14
  end
13
15
 
14
16
  def execute
17
+ setup if self.respond_to?(:setup)
15
18
  last_key, values = nil, []
16
19
  @input.each_line do |line|
17
20
  key, value = line.split(KEY_VALUE_SEPERATOR)
@@ -24,6 +27,7 @@ module Mandy
24
27
  values << value
25
28
  end
26
29
  reducer(deserialize_key(last_key), values.map {|v| deserialize_value(v) })
30
+ teardown if self.respond_to?(:teardown)
27
31
  end
28
32
 
29
33
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mandy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Kent