Kouhei Sutou
null+****@clear*****
Fri Apr 17 16:34:49 JST 2015
Kouhei Sutou 2015-04-17 16:34:49 +0900 (Fri, 17 Apr 2015) New Revision: 8895e638ba231f47d69456352b58bbc116870bc7 https://github.com/groonga/groonga/commit/8895e638ba231f47d69456352b58bbc116870bc7 Message: logical_count: re-support counting with range index Modified files: plugins/sharding/logical_count.rb Modified: plugins/sharding/logical_count.rb (+58 -6) =================================================================== --- plugins/sharding/logical_count.rb 2015-04-17 14:57:34 +0900 (cf283ec) +++ plugins/sharding/logical_count.rb 2015-04-17 16:34:49 +0900 (610dae8) @@ -45,18 +45,44 @@ module Groonga end end + range_index = nil + if filter.nil? + index_info = shard_key.find_index(Operator::LESS) + if index_info + range_index = index_info.index + end + end + case cover_type when :partial_min - filtered_count_n_records(table) do |expression| - expression_builder.build_partial_min(expression) + if range_index + count_n_records_in_range(range_index, + target_range.min, target_range.min_border, + nil, nil) + else + filtered_count_n_records(table) do |expression| + expression_builder.build_partial_min(expression) + end end when :partial_max - filtered_count_n_records(table) do |expression| - expression_builder.build_partial_max(expression) + if range_index + count_n_records_in_range(range_index, + nil, nil, + target_range.max, target_range.max_border) + else + filtered_count_n_records(table) do |expression| + expression_builder.build_partial_max(expression) + end end when :partial_min_and_max - filtered_count_n_records(table) do |expression| - expression_builder.build_partial_min_and_max(expression) + if range_index + count_n_records_in_range(range_index, + target_range.min, target_range.min_border, + target_range.max, target_range.max_border) + else + filtered_count_n_records(table) do |expression| + expression_builder.build_partial_min_and_max(expression) + end end end end @@ -75,6 +101,32 @@ module Groonga expression.close if expression end end + + def count_n_records_in_range(range_index, + min, min_border, max, max_border) + flags = TableCursorFlags::BY_KEY + case min_border + when :include + flags |= TableCursorFlags::GE + when :exclude + flags |= TableCursorFlags::GT + end + case max_border + when :include + flags |= TableCursorFlags::LE + when :exclude + flags |= TableCursorFlags::LT + end + + TableCursor.open(range_index.table, + :min => min, + :max => max, + :flags => flags) do |table_cursor| + IndexCursor.open(table_cursor, range_index) do |index_cursor| + index_cursor.count + end + end + end end end end -------------- next part -------------- HTML����������������������������... Download