YUKI Hiroshi
null+****@clear*****
Wed Nov 27 20:05:16 JST 2013
YUKI Hiroshi 2013-11-27 20:05:16 +0900 (Wed, 27 Nov 2013) New Revision: 8252672b3a40fc7255a443bc77b652036db90097 https://github.com/droonga/fluent-plugin-droonga/commit/8252672b3a40fc7255a443bc77b652036db90097 Message: Apply limit and offset for reduced values Modified files: lib/droonga/plugin/collector/basic.rb lib/droonga/plugin/distributor/search.rb Modified: lib/droonga/plugin/collector/basic.rb (+15 -2) =================================================================== --- lib/droonga/plugin/collector/basic.rb 2013-11-27 19:11:50 +0900 (988be4e) +++ lib/droonga/plugin/collector/basic.rb 2013-11-27 20:05:16 +0900 (8aa721c) @@ -41,16 +41,29 @@ module Droonga def reduce(elements, *values) result = {} elements.each do |key, deal| + reduced_values = nil + case deal["type"] when "sum" - result[key] = values[0][key] + values[1][key] + reduced_values = values[0][key] + values[1][key] when "sort" - result[key] = merge(values[0][key], values[1][key], deal["order"]) + reduced_values = merge(values[0][key], values[1][key], deal["order"]) + end + + if deal["offset"] + reduced_values = reduced_values[deal["offset"]..-1] end + if deal["limit"] && deal["limit"] != UNLIMITED + reduced_values = reduced_values[0..deal["limit"]] + end + + result[key] = reduced_values end return result end + UNLIMITED = -1 + def merge(x, y, order) index = 0 y.each do |_y| Modified: lib/droonga/plugin/distributor/search.rb (+59 -1) =================================================================== --- lib/droonga/plugin/distributor/search.rb 2013-11-27 19:11:50 +0900 (666bf3a) +++ lib/droonga/plugin/distributor/search.rb 2013-11-27 20:05:16 +0900 (89ce30a) @@ -31,26 +31,34 @@ module Droonga request["queries"].each do |input_name, query| output = query["output"] next unless output + input_names << input_name output_name = input_name + "_reduced" output_names << output_name name_mapper[output_name] = input_name - # TODO: offset & limit must be arranged here. + + final_offset, final_limit = calculate_offset_and_limit!(query) + elements = {} output["elements"].each do |element| case element when "count" elements[element] = { "type" => "sum", + "offset" => final_offset, + "limit" => final_limit, } when "records" # TODO: must take "sortBy" section into account. elements[element] = { "type" => "sort", "order" => ["<"], + "offset" => final_offset, + "limit" => final_limit, } end end + reducer = { "inputs"=> [input_name], "outputs"=> [output_name], @@ -81,5 +89,55 @@ module Droonga message.push(searcher) post(message) end + + private + UNLIMITED = -1 + + def calculate_offset_and_limit!(query) + rich_sort = query["sortBy"].is_a?(Hash) + + # offset for workers must be zero. + sort_offset = 0 + if rich_sort + sort_offset = query["sortBy"]["offset"] || 0 + query["sortBy"]["offset"] = 0 + end + + output_offset = query["output"]["offset"] || 0 + query["output"]["offset"] = 0 + + final_offset = sort_offset + output_offset + + # we have to calculate limit based on offset. + # <A, B = limited integer (0...MAXINT)> + # | sort | output | => | worker's sort limit | worker's output limit | final limit | + # ========================= ================================================================== + # | UNLIMITED | UNLIMITED | => | UNLIMITED | UNLIMITED | UNLIMITED | + # | UNLIMITED | B | => | final_offset + B | UNLIMITED | B | + # | A | UNLIMITED | => | final_offset + A | UNLIMITED | A | + # | A | B | => | final_offset + min(A, B) | UNLIMITED | min(A, B) | + sort_limit = UNLIMITED + if rich_sort + sort_limit = query["sortBy"]["limit"] || UNLIMITED + end + output_limit = query["output"]["limit"] || 0 + query["output"]["limit"] = UNLIMITED + + final_limit = 0 + if sort_limit == UNLIMITED && output_limit == UNLIMITED + final_limit = UNLIMITED + else + if sort_limit == UNLIMITED + final_limit = output_limit + elsif output_limit == UNLIMITED + final_limit = sort_limit + else + final_limit = [sort_limit, output_limit].min + end + query["sortBy"]["limit"] = final_offset + final_limit if rich_sort + end + + [final_offset, final_limit] + end end end -------------- next part -------------- HTML����������������������������...Download