Class: Relaxo::QueryServer::Reducer
- Defined in:
- lib/relaxo/query_server/reducer.rb
Overview
Implements the ‘reduce` and `rereduce` functions along with all associated state.
Constant Summary
Constants inherited from Loader
Instance Method Summary collapse
-
#initialize(context) ⇒ Reducer
constructor
Create a reducer attached to the given context.
-
#reduce(functions, items) ⇒ Object
Apply the reduce function to a given list of items.
-
#rereduce(functions, values) ⇒ Object
Apply the rereduce functions to a given list of values.
Methods inherited from Loader
#add_libraries, #load, #load_default
Constructor Details
#initialize(context) ⇒ Reducer
Create a reducer attached to the given context.
41 42 43 44 45 |
# File 'lib/relaxo/query_server/reducer.rb', line 41 def initialize(context) super() @context = context end |
Instance Method Details
#reduce(functions, items) ⇒ Object
Apply the reduce function to a given list of items. Functions are typically in the form of:
functions = [lambda{|keys,values,rereduce| ...}]
such that:
items = [[key1, value1], [key2, value2], [key3, value3]]
functions.map{|function| function.call(all keys, all values, false)}
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/relaxo/query_server/reducer.rb', line 58 def reduce(functions, items) load_default functions = functions.collect do |function_text| @context.parse_function(function_text, binding) end keys, values = [], [] items.each do |value| keys << value[0] values << value[1] end result = functions.map do |function| ReducingProcess.new(self, function).run(keys, values, false) end return [true, result] end |
#rereduce(functions, values) ⇒ Object
Apply the rereduce functions to a given list of values.
lambda{|keys,values,rereduce| ...}.call([], values, true)
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/relaxo/query_server/reducer.rb', line 86 def rereduce(functions, values) load_default functions = functions.collect do |function_text| @context.parse_function(function_text, binding) end result = functions.map do |function| ReducingProcess.new(self, function).run([], values, true) end return [true, result] end |