Class: Droonga::BasicCollector
Constant Summary
collapse
- UNLIMITED =
-1
Instance Attribute Summary
#body, #component, #input_name, #output_names, #output_values, #task
Instance Method Summary
collapse
#emit, #initialize, #process
#command, extended, #inherited, #method_name, #processable?, #repository
Methods inherited from Plugin
#initialize, #process, #processable?, #shutdown, #start
Instance Method Details
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/droonga/plugin/collector/basic.rb', line 81
def apply_output_attributes_and_format(items, output)
attributes = output["attributes"]
if attributes
format = output["format"]
if format == "complex"
items.collect! do |item|
complex_item = {}
attributes.each_with_index do |label, index|
complex_item[label] = item[index]
end
complex_item
end
else
items.collect! do |item|
item[0...attributes.size]
end
end
end
items
end
|
#apply_output_range(items, output) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/droonga/plugin/collector/basic.rb', line 66
def apply_output_range(items, output)
if items && items.is_a?(Array)
offset = output["offset"] || 0
unless offset.zero?
items = items[offset..-1] || []
end
limit = output["limit"] || 0
unless limit == UNLIMITED
items = items[0...limit]
end
end
items
end
|
#collector_gather(result) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/droonga/plugin/collector/basic.rb', line 27
def collector_gather(result)
output = body ? body[input_name] : input_name
if output.is_a?(Hash)
elements = output["elements"]
if elements && elements.is_a?(Hash)
elements.each do |element, mapper|
case mapper["type"]
when "count"
result[element] = result[mapper["target"]].size
when "sort"
end
end
elements.each do |element, mapper|
if mapper["no_output"]
result.delete(element)
next
end
case mapper["type"]
when "count"
when "sort"
if result[element]
result[element] = apply_output_range(result[element], mapper)
result[element] = apply_output_attributes_and_format(result[element], mapper)
end
end
end
end
output = output["output"]
end
emit(output, result)
end
|
#collector_reduce(request) ⇒ Object
103
104
105
106
107
108
109
110
111
|
# File 'lib/droonga/plugin/collector/basic.rb', line 103
def collector_reduce(request)
return unless request
body[input_name].each do |output, elements|
value = request
old_value = output_values[output]
value = reduce(elements, old_value, request) if old_value
emit(output, value)
end
end
|
#compare(x, y, operators) ⇒ Object
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/droonga/plugin/collector/basic.rb', line 165
def compare(x, y, operators)
operators.each_with_index do |operator, index|
column = operator["column"] || index
operator = operator["operator"]
_x = x[column]
_y = y[column]
return true if _x.__send__(operator, _y)
end
return false
end
|
#merge(x, y, options = {}) ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/droonga/plugin/collector/basic.rb', line 135
def merge(x, y, options={})
operators = options[:operators] = normalize_operators(options[:operators])
unify_by_key!(x, y, options)
index = 0
y.each do |_y|
loop do
_x = x[index]
break unless _x
break if compare(_y, _x, operators)
index += 1
end
x.insert(index, _y)
index += 1
end
return x
end
|
#normalize_operators(operators) ⇒ Object
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/droonga/plugin/collector/basic.rb', line 154
def normalize_operators(operators)
operators ||= []
operators.collect do |operator|
if operator.is_a?(String)
{ "operator" => operator }
else
operator
end
end
end
|
#reduce(elements, *values) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/droonga/plugin/collector/basic.rb', line 113
def reduce(elements, *values)
result = {}
elements.each do |key, deal|
reduced_values = nil
case deal["type"]
when "sum"
reduced_values = values[0][key] + values[1][key]
when "sort"
reduced_values = merge(values[0][key],
values[1][key],
:operators => deal["operators"],
:key_column => deal["key_column"])
end
reduced_values = apply_output_range(reduced_values, "limit" => deal["limit"])
result[key] = reduced_values
end
return result
end
|
#unify_by_key!(base_items, unified_items, options = {}) ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/droonga/plugin/collector/basic.rb', line 176
def unify_by_key!(base_items, unified_items, options={})
key_column_index = options[:key_column]
return unless key_column_index
if base_items.size < unified_items.size
base_items, unified_items = unified_items, base_items
end
rest_unified_items = unified_items.dup
base_items.reject! do |base_item|
key = base_item[key_column_index]
rest_unified_items.any? do |unified_item|
if unified_item[key_column_index] == key
base_item.each_with_index do |value, column|
next if column == key_column_index
unified_item[column] += value
end
rest_unified_items -= [unified_item]
true
else
false
end
end
end
unless rest_unified_items.size == unified_items.size
unified_items.sort! do |a, b|
if compare(a, b, options[:operators])
-1
else
1
end
end
end
end
|