Class: Droonga::BasicCollector

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

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, #output, #post, #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_attributes_and_format(items, output) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/droonga/plugin/collector/basic.rb', line 55

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/droonga/plugin/collector/basic.rb', line 40

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

#collector_reduce(request) ⇒ Object



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

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(value, output)
  end
end

#compare(x, y, operators) ⇒ Object



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

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, operators) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/droonga/plugin/collector/basic.rb', line 106

def merge(x, y, operators)
  # Normalize operators at first for optimization.
  operators ||= []
  operators = operators.collect do |operator|
    if operator.is_a?(String)
      { "operator" => operator }
    else
      operator
    end
  end

  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

#reduce(elements, *values) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/droonga/plugin/collector/basic.rb', line 87

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], deal["operators"])
    end

    reduced_values = apply_output_range(reduced_values, "limit" => deal["limit"])

    result[key] = reduced_values
  end
  return result
end