Navigation Menu

Skip to content

Commit

Permalink
Separate "recursive-sum" collector from "sum".
Browse files Browse the repository at this point in the history
Because recursive sum can be slow.
  • Loading branch information
piroor committed Apr 21, 2015
1 parent 34ce500 commit e897a71
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/droonga/collectors.rb
@@ -1,4 +1,4 @@
# Copyright (C) 2014 Droonga Project
# Copyright (C) 2014-2015 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand All @@ -16,3 +16,4 @@
require "droonga/collectors/and"
require "droonga/collectors/or"
require "droonga/collectors/sum"
require "droonga/collectors/recursive_sum"
26 changes: 26 additions & 0 deletions lib/droonga/collectors/recursive_sum.rb
@@ -0,0 +1,26 @@
# Copyright (C) 2015 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

module Droonga
module Collectors
class RecursiveSum
class << self
def operator
"recursive-sum"
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/droonga/plugins/system/object_count.rb
Expand Up @@ -46,7 +46,7 @@ def handle(message)
define_single_step do |step|
step.name = "system.object-count"
step.handler = ObjectCountHandler
step.collector = Collectors::Sum
step.collector = Collectors::RecursiveSum
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions lib/droonga/reducer.rb
Expand Up @@ -58,6 +58,10 @@ def reduce(left_value, right_value)
reduced_value = sum(left_value, right_value)
reduced_value = self.class.apply_range(reduced_value,
"limit" => @deal["limit"])
when "recursive-sum"
reduced_value = recursive_sum(left_value, right_value)
reduced_value = self.class.apply_range(reduced_value,
"limit" => @deal["limit"])
when "average"
reduced_value = (left_value.to_f + right_value.to_f) / 2
when "sort"
Expand All @@ -76,6 +80,16 @@ def reduce(left_value, right_value)
def sum(x, y)
return x || y if x.nil? or y.nil?

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

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

if x.is_a?(Hash) and y.is_a?(Hash)
if number_hash?(x) and number_hash?(y)
sum_number_hashes(x, y)
Expand Down

0 comments on commit e897a71

Please sign in to comment.