Class: Jrf::Stage
- Inherits:
-
Object
- Object
- Jrf::Stage
- Defined in:
- lib/jrf/stage.rb
Defined Under Namespace
Classes: MapReducer, ReducerToken
Instance Attribute Summary collapse
-
#src ⇒ Object
readonly
Returns the value of attribute src.
Class Method Summary collapse
Instance Method Summary collapse
- #call(input) ⇒ Object
-
#decomposable? ⇒ Boolean
Returns true if all reducers in this stage are DecomposableReduce instances, meaning partial accumulators from parallel workers can be merged.
- #finish ⇒ Object
-
#initialize(block, src: nil) ⇒ Stage
constructor
A new instance of Stage.
-
#merge_partials!(other_partials) ⇒ Object
Merges an array of partial accumulators (from another worker) into this stage’s reducers.
-
#partial_accumulators ⇒ Object
Returns an array of raw accumulator values, one per reducer.
-
#replace_accumulators!(partials) ⇒ Object
Replaces all reducer accumulators with the given values.
- #step_apply(collection, &block) ⇒ Object
- #step_group_by(key, &block) ⇒ Object
- #step_map(builtin, collection, &block) ⇒ Object
- #step_reduce(value, initial:, finish: nil, merge: nil, step_fn: nil, &step_block) ⇒ Object
Constructor Details
#initialize(block, src: nil) ⇒ Stage
Returns a new instance of Stage.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/jrf/stage.rb', line 26 def initialize(block, src: nil) @src = src @reducers = [] @cursor = 0 @template = nil @mode = nil # nil=unknown, :reducer, :passthrough @map_transforms = {} @ctx = Class.new(RowContext) do define_method(:__jrf_expr__, &block) end.new end |
Instance Attribute Details
#src ⇒ Object (readonly)
Returns the value of attribute src.
11 12 13 |
# File 'lib/jrf/stage.rb', line 11 def src @src end |
Class Method Details
.resolve_template(template, reducers) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/jrf/stage.rb', line 13 def self.resolve_template(template, reducers) if template.is_a?(ReducerToken) rows = reducers.fetch(template.index).finish rows.length == 1 ? rows.first : rows elsif template.is_a?(Array) template.map { |v| resolve_template(v, reducers) } elsif template.is_a?(Hash) template.transform_values { |v| resolve_template(v, reducers) } else template end end |
Instance Method Details
#call(input) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/jrf/stage.rb', line 38 def call(input) @ctx.reset(input) @cursor = 0 @ctx.__jrf_current_stage = self result = @ctx.__jrf_expr__ if @mode.nil? if @reducers.any? @mode = :reducer @template = result else @mode = :passthrough end end (@mode == :reducer) ? Control::DROPPED : result end |
#decomposable? ⇒ Boolean
Returns true if all reducers in this stage are DecomposableReduce instances, meaning partial accumulators from parallel workers can be merged.
178 179 180 181 |
# File 'lib/jrf/stage.rb', line 178 def decomposable? @mode == :reducer && @reducers.any? && @reducers.all? { |r| r.is_a?(Reducers::DecomposableReduce) } end |
#finish ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/jrf/stage.rb', line 166 def finish return [] unless @mode == :reducer && @reducers.any? if @template.is_a?(ReducerToken) @reducers.fetch(@template.index).finish else [self.class.resolve_template(@template, @reducers)] end end |
#merge_partials!(other_partials) ⇒ Object
Merges an array of partial accumulators (from another worker) into this stage’s reducers.
196 197 198 199 200 |
# File 'lib/jrf/stage.rb', line 196 def merge_partials!(other_partials) @reducers.each_with_index do |reducer, i| reducer.merge_partial(other_partials[i]) end end |
#partial_accumulators ⇒ Object
Returns an array of raw accumulator values, one per reducer.
184 185 186 |
# File 'lib/jrf/stage.rb', line 184 def partial_accumulators @reducers.map(&:partial) end |
#replace_accumulators!(partials) ⇒ Object
Replaces all reducer accumulators with the given values.
189 190 191 192 193 |
# File 'lib/jrf/stage.rb', line 189 def replace_accumulators!(partials) @reducers.each_with_index do |reducer, i| reducer.instance_variable_set(:@acc, partials[i]) end end |
#step_apply(collection, &block) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/jrf/stage.rb', line 122 def step_apply(collection, &block) raise TypeError, "apply expects Array, got #{collection.class}" unless collection.is_a?(Array) apply_reducers = [] template = nil results = [] collection.each do |v| with_scoped_reducers(apply_reducers) do result = @ctx.send(:__jrf_with_current_input, v) { block.call(v) } template ||= result results << result end end if apply_reducers.any? self.class.resolve_template(template, apply_reducers) else results.each_with_object([]) do |mapped, arr| next if mapped.equal?(Control::DROPPED) if mapped.is_a?(Control::Flat) arr.concat(Array(mapped.value)) else arr << mapped end end end end |
#step_group_by(key, &block) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/jrf/stage.rb', line 151 def step_group_by(key, &block) idx = @cursor map_reducer = (@reducers[idx] ||= MapReducer.new(:group_by, false)) row = @ctx._ slot = map_reducer.slot(key) with_scoped_reducers(slot.reducers) do result = @ctx.send(:__jrf_with_current_input, row) { block.call(row) } slot.template ||= result end @cursor += 1 ReducerToken.new(idx) end |
#step_map(builtin, collection, &block) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/jrf/stage.rb', line 77 def step_map(builtin, collection, &block) idx = @cursor @cursor += 1 if collection.is_a?(Array) raise TypeError, "map_values expects Hash, got Array" if builtin == :map_values elsif !collection.is_a?(Hash) raise TypeError, "#{builtin} expects #{builtin == :map_values ? "Hash" : "Array or Hash"}, got #{collection.class}" end # Transformation mode (detected on first call) if @map_transforms[idx] return transform_collection(builtin, collection, &block) end map_reducer = (@reducers[idx] ||= MapReducer.new(builtin, collection.is_a?(Array))) if collection.is_a?(Array) collection.each_with_index do |v, i| slot = map_reducer.slot(i) with_scoped_reducers(slot.reducers) do result = @ctx.send(:__jrf_with_current_input, v) { block.call(v) } slot.template ||= result end end else collection.each do |k, v| slot = map_reducer.slot(k) with_scoped_reducers(slot.reducers) do result = @ctx.send(:__jrf_with_current_input, v) { invoke_block(builtin, block, k, v) } slot.template ||= result end end end # Detect transformation: no reducers were allocated in any slot if @mode.nil? && map_reducer.slots.values.all? { |s| s.reducers.empty? } @map_transforms[idx] = true @reducers[idx] = nil return transformed_slots(builtin, map_reducer) end ReducerToken.new(idx) end |
#step_reduce(value, initial:, finish: nil, merge: nil, step_fn: nil, &step_block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/jrf/stage.rb', line 56 def step_reduce(value, initial:, finish: nil, merge: nil, step_fn: nil, &step_block) idx = @cursor step_fn ||= step_block if @reducers[idx].nil? finish_rows = finish || ->(acc) { [acc] } @reducers[idx] = if merge Reducers.decomposable_reduce(initial, merge: merge, finish: finish_rows, &step_fn) else Reducers.reduce(initial, finish: finish_rows, &step_fn) end result = ReducerToken.new(idx) else result = Control::DROPPED end @reducers[idx].step(value) @cursor = idx + 1 result end |