[Groonga-commit] droonga/fluent-plugin-droonga at 19b7ccc [master] Allow to merge two hashes

Back to archive index

YUKI Hiroshi null+****@clear*****
Mon Jan 27 17:27:05 JST 2014


YUKI Hiroshi	2014-01-27 17:27:05 +0900 (Mon, 27 Jan 2014)

  New Revision: 19b7ccc08e81df59561fa5a9bde8262711de3218
  https://github.com/droonga/fluent-plugin-droonga/commit/19b7ccc08e81df59561fa5a9bde8262711de3218

  Message:
    Allow to merge two hashes

  Modified files:
    lib/droonga/plugin/collector/basic.rb

  Modified: lib/droonga/plugin/collector/basic.rb (+15 -1)
===================================================================
--- lib/droonga/plugin/collector/basic.rb    2014-01-27 17:21:56 +0900 (fc80e7e)
+++ lib/droonga/plugin/collector/basic.rb    2014-01-27 17:27:05 +0900 (9039e40)
@@ -53,7 +53,7 @@ module Droonga
       when "and"
         reduced_value = left_value && right_value
       when "sum"
-        reduced_value = left_value + right_value
+        reduced_value = sum(left_value, right_value)
         reduced_value = apply_output_range(reduced_value,
                                            "limit" => deal["limit"])
       when "average"
@@ -85,6 +85,20 @@ module Droonga
       items
     end
 
+    def sum(x, y)
+      return x || y if x.nil? or y.nil?
+
+      if x.is_a?(Hash) && y.is_a?(Hash)
+        all_keys = (x.keys + y.keys).uniq
+        all_keys.each do |key|
+          x[key] = sum(x[key], y[key])
+        end
+        x
+      else
+        x + y
+      end
+    end
+
     def merge(x, y, options={})
       operators = options[:operators] = normalize_operators(options[:operators])
 
-------------- next part --------------
HTML����������������������������...
下载 



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