Class: Droonga::BasicCollector

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

Direct Known Subclasses

GroongaCollector, SearchCollector

Constant Summary collapse

UNLIMITED =
-1

Instance Attribute Summary

Attributes inherited from CollectorPlugin

#body, #component, #input_name, #output_names, #output_values, #task

Instance Method Summary collapse

Methods inherited from CollectorPlugin

#emit, #initialize, #process

Methods included from PluginRegisterable

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

Methods inherited from Plugin

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

Constructor Details

This class inherits a constructor from Droonga::CollectorPlugin

Instance Method Details

#apply_output_range(items, output) ⇒ Object



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

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



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

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



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

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



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

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
# 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 "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



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

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



139
140
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
# File 'lib/droonga/plugin/collector/basic.rb', line 139

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