Class: Droonga::BasicCollector

Inherits:
CollectorPlugin show all
Defined in:
lib/droonga/plugin/collector/basic.rb

Direct Known Subclasses

SearchCollector

Constant Summary collapse

UNLIMITED =
-1

Instance Attribute Summary

Attributes inherited from CollectorPlugin

#body, #input_name, #output_names, #output_values, #step, #task

Instance Method Summary collapse

Methods inherited from CollectorPlugin

#emit, #initialize, #process

Methods included from PluginRegisterable

#command, #commands, extended, #find_command, #inherited, #method_name, #processable?, #repository

Methods inherited from LegacyPlugin

#initialize, #process, #processable?, #shutdown, #start

Constructor Details

This class inherits a constructor from Droonga::CollectorPlugin

Instance Method Details

#apply_output_range(items, output) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/droonga/plugin/collector/basic.rb', line 75

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
# 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)
    output = output["output"]
  end
  emit(output, result)
end

#collector_reduce(request) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/droonga/plugin/collector/basic.rb', line 36

def collector_reduce(request)
  body[input_name].each do |output, deal|
    left_value = output_values[output]
    right_value = request
    value = reduce(deal, left_value, right_value)
    emit(output, value)
  end
end

#compare(x, y, operators) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/droonga/plugin/collector/basic.rb', line 130

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/droonga/plugin/collector/basic.rb', line 100

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



119
120
121
122
123
124
125
126
127
128
# File 'lib/droonga/plugin/collector/basic.rb', line 119

def normalize_operators(operators)
  operators ||= []
  operators.collect do |operator|
    if operator.is_a?(String)
      { "operator" => operator }
    else
      operator
    end
  end
end

#reduce(deal, left_value, right_value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/droonga/plugin/collector/basic.rb', line 45

def reduce(deal, left_value, right_value)
  if left_value.nil? || right_value.nil?
    return right_value || left_value
  end

  reduced_value = nil

  case deal["type"]
  when "and"
    reduced_value = left_value && right_value
  when "or"
    reduced_value = left_value || right_value
  when "sum"
    reduced_value = sum(left_value, right_value)
    reduced_value = apply_output_range(reduced_value,
                                       "limit" => deal["limit"])
  when "average"
    reduced_value = (left_value.to_f + right_value.to_f) / 2
  when "sort"
    reduced_value = merge(left_value,
                          right_value,
                          :operators => deal["operators"],
                          :key_column => deal["key_column"])
    reduced_value = apply_output_range(reduced_value,
                                       "limit" => deal["limit"])
  end

  reduced_value
end

#sum(x, y) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/droonga/plugin/collector/basic.rb', line 90

def sum(x, y)
  return x || y if x.nil? or y.nil?

  if x.is_a?(Hash) && y.is_a?(Hash)
    x.merge(y)
  else
    x + y
  end
end

#unify_by_key!(base_items, unified_items, options = {}) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/droonga/plugin/collector/basic.rb', line 141

def unify_by_key!(base_items, unified_items, options={})
  key_column_index = options[:key_column]
  return unless key_column_index

  # The unified records must be smaller than the base, because
  # I sort unified records at last. I want to sort only smaller array.
  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