[Groonga-commit] droonga/fluent-plugin-droonga at 2345362 [master] Implement reduce function.

Back to archive index

Daijiro MORI null+****@clear*****
Sun Aug 18 15:06:09 JST 2013


Daijiro MORI	2013-08-18 15:06:09 +0900 (Sun, 18 Aug 2013)

  New Revision: 2345362f7ac4f6128e14561c5bd16f68443aaff8
  https://github.com/droonga/fluent-plugin-droonga/commit/2345362f7ac4f6128e14561c5bd16f68443aaff8

  Message:
    Implement reduce function.

  Modified files:
    lib/droonga/plugin/handler_proxy.rb
    lib/droonga/proxy.rb

  Modified: lib/droonga/plugin/handler_proxy.rb (+46 -12)
===================================================================
--- lib/droonga/plugin/handler_proxy.rb    2013-08-17 16:00:00 +0900 (4a28535)
+++ lib/droonga/plugin/handler_proxy.rb    2013-08-18 15:06:09 +0900 (7bf9571)
@@ -32,25 +32,59 @@ module Droonga
 
     command :proxy_gather
     def proxy_gather(request)
-      output = body ? body[name] : name
+      output = body ? body[input_name] : input_name
       emit(request, output)
     end
 
     command :proxy_reduce
     def proxy_reduce(request)
-      output = outputs.first
-      a = values[output]
-      b = request
-      return unless b
-      unless a
-        emit b
-        return
+      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
-      a.each do |key, value|
-        next unless b[key]
-        a[key] = value + b[key]
+    end
+
+    def reduce(elements, *values)
+      result = {}
+      elements.each do |key, deal|
+        func, *args = deal
+        case func
+        when "sum"
+          result[key] = values[0][key] + values[1][key]
+        when "sort"
+          result[key] = merge(values[0][key], values[1][key], args)
+        end
+      end
+      return result
+    end
+
+    def merge(x, y, order)
+      index = 0
+      y.each do |_y|
+        loop do
+          _x = x[index]
+          break unless _x
+          break if compare(_y, _x, order)
+          index += 1
+        end
+        x.insert(index, _y)
+        index += 1
+      end
+      return x
+    end
+
+    def compare(x, y, operators)
+      for index in 0..x.size-1 do
+        _x = x[index]
+        _y = y[index]
+        operator = operators[index]
+        break unless operator
+        return true if _x.__send__(operator, _y)
       end
-      emit(a, output)
+      return false
     end
   end
 end

  Modified: lib/droonga/proxy.rb (+6 -6)
===================================================================
--- lib/droonga/proxy.rb    2013-08-17 16:00:00 +0900 (9e15477)
+++ lib/droonga/proxy.rb    2013-08-18 15:06:09 +0900 (435b140)
@@ -293,21 +293,21 @@ module Droonga
   end
 
   class ProxyHandler < Droonga::Handler
-    attr_reader :task, :name, :component, :values, :body, :outputs
+    attr_reader :task, :input_name, :component, :output_values, :body, :output_names
     def handle(command, request, *arguments)
       @task = request["task"]
-      @name = request["name"]
+      @input_name = request["name"]
       @component = @task["component"]
-      @outputs = @component["outputs"]
+      @output_names = @component["outputs"]
       @body = @component["body"]
-      @values = @task["values"]
+      @output_values = @task["values"]
       super(command, request["value"], *arguments)
     end
 
     def emit(value, name = nil)
       unless name
-        if component["outputs"]
-          name = component["outputs"].first
+        if @output_names
+          name = @output_names.first
         else
           @task["values"] = value
           return
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index