Kouhei Sutou
null+****@clear*****
Tue Oct 31 15:36:18 JST 2017
Kouhei Sutou 2017-10-31 15:36:18 +0900 (Tue, 31 Oct 2017) New Revision: b9ad72ffd18f6cd0cd0d49b1e726beeafaa0d9f9 https://github.com/groonga/groonga/commit/b9ad72ffd18f6cd0cd0d49b1e726beeafaa0d9f9 Message: sharding: extract dynamic column related code New API: * Groonga::LabeledArguments Added files: lib/mrb/scripts/labeled_arguments.rb plugins/sharding/dynamic_columns.rb plugins/sharding/keys_parsable.rb Modified files: lib/mrb/scripts/initialize/post.rb lib/mrb/scripts/sources.am plugins/sharding.rb plugins/sharding/logical_select.rb plugins/sharding/sources.am Modified: lib/mrb/scripts/initialize/post.rb (+2 -0) =================================================================== --- lib/mrb/scripts/initialize/post.rb 2017-10-31 14:59:54 +0900 (c1215f3e1) +++ lib/mrb/scripts/initialize/post.rb 2017-10-31 15:36:18 +0900 (5265f25a4) @@ -25,4 +25,6 @@ require "plugin_loader" require "eval_context" +require "labeled_arguments" + require "command_line_parser" Added: lib/mrb/scripts/labeled_arguments.rb (+21 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/scripts/labeled_arguments.rb 2017-10-31 15:36:18 +0900 (d58dccdd4) @@ -0,0 +1,21 @@ +module Groonga + class LabeledArguments + include Enumerable + + def initialize(arguments, prefix_pattern) + @arguments = arguments + @pattern = /\A#{prefix_pattern}\[(.+?)\]\.(.+)\z/ + end + + def each(&block) + labeled_arguments = {} + @arguments.each do |key, value| + match_data =****@patte*****(key) + next if match_data.nil? + labeled_argument = (labeled_arguments[match_data[1]] ||= {}) + labeled_argument[match_data[2]] = value + end + labeled_arguments.each(&block) + end + end +end Modified: lib/mrb/scripts/sources.am (+1 -0) =================================================================== --- lib/mrb/scripts/sources.am 2017-10-31 14:59:54 +0900 (9a9e2bae6) +++ lib/mrb/scripts/sources.am 2017-10-31 15:36:18 +0900 (ae1a95935) @@ -19,6 +19,7 @@ RUBY_SCRIPT_FILES = \ index_column.rb \ index_cursor.rb \ index_info.rb \ + labeled_arguments.rb \ logger.rb \ object.rb \ operator.rb \ Modified: plugins/sharding.rb (+3 -0) =================================================================== --- plugins/sharding.rb 2017-10-31 14:59:54 +0900 (86401c1fa) +++ plugins/sharding.rb 2017-10-31 15:36:18 +0900 (9acec1074) @@ -1,6 +1,9 @@ require "sharding/parameters" require "sharding/range_expression_builder" require "sharding/logical_enumerator" +require "sharding/keys_parsable" + +require "sharding/dynamic_columns" require "sharding/logical_parameters" Added: plugins/sharding/dynamic_columns.rb (+135 -0) 100644 =================================================================== --- /dev/null +++ plugins/sharding/dynamic_columns.rb 2017-10-31 15:36:18 +0900 (7dcac791e) @@ -0,0 +1,135 @@ +module Groonga + module Sharding + class DynamicColumns + class << self + def parse(input) + initial_contexts = [] + filtered_contexts = [] + output_contexts = [] + labeled_arguments = LabeledArguments.new(input, /columns?/) + labeled_arguments.each do |label, arguments| + contexts = nil + case arguments["stage"] + when "initial" + contexts = initial_contexts + when "filtered" + contexts = filtered_contexts + when "output" + contexts = output_contexts + else + next + end + contexts << DynamicColumnExecuteContext.new(label, arguments) + end + + new(initial_contexts, + filtered_contexts, + output_contexts) + end + end + + def initialize(initial_contexts, + filtered_contexts, + output_contexts) + @initial_contexts = initial_contexts + @filtered_contexts = filtered_contexts + @output_contexts = output_contexts + end + + def each_initial(&block) + @initial_contexts.each(&block) + end + + def each_filtered(&block) + @filtered_contexts.each(&block) + end + + def each_output(&block) + @output_contexts.each(&block) + end + + def close + @initial_contexts.each do |context| + context.close + end + @filtered_contexts.each do |context| + context.close + end + @output_contexts.each do |context| + context.close + end + end + end + + class DynamicColumnExecuteContext + include KeysParsable + + attr_reader :label + attr_reader :stage + attr_reader :type + attr_reader :flags + attr_reader :value + attr_reader :window_sort_keys + attr_reader :window_group_keys + def initialize(label, arguments) + @label = label + @stage = arguments["stage"] + @type = parse_type(arguments["type"]) + @flags = parse_flags(arguments["flags"] || "COLUMN_SCALAR") + @value = arguments["value"] + @window_sort_keys = parse_keys(arguments["window.sort_keys"]) + @window_group_keys = parse_keys(arguments["window.group_keys"]) + end + + def close + end + + def apply(table, condition=nil) + column = table.create_column(@label, @flags, @type) + return if table.empty? + + expression = Expression.create(table) + begin + expression.parse(@value) + if @window_sort_keys.empty? and @window_group_keys.empty? + expression.condition = condition if condition + table.apply_expression(column, expression) + else + table.apply_window_function(column, expression, + :sort_keys => @window_sort_keys, + :group_keys => @window_group_keys) + end + ensure + expression.close + end + end + + private + def parse_type(type_raw) + return nil if type_raw.nil? + + type = Context.instance[type_raw] + if type.nil? + message = "#{error_message_tag} unknown type: <#{type_raw}>" + raise InvalidArgument, message + end + + case type + when Type, Table + type + else + message = "#{error_message_tag} invalid type: #{type.grn_inspect}" + raise InvalidArgument, message + end + end + + def parse_flags(flags_raw) + Column.parse_flags(error_message_tag, flags_raw) + end + + def error_message_tag + "[logical_select][columns][#{@stage}][#{@label}]" + end + end + end +end Added: plugins/sharding/keys_parsable.rb (+12 -0) 100644 =================================================================== --- /dev/null +++ plugins/sharding/keys_parsable.rb 2017-10-31 15:36:18 +0900 (ed189cb69) @@ -0,0 +1,12 @@ +module Groonga + module Sharding + module KeysParsable + private + def parse_keys(raw_keys) + return [] if raw_keys.nil? + + raw_keys.strip.split(/ *, */) + end + end + end +end Modified: plugins/sharding/logical_select.rb (+4 -168) =================================================================== --- plugins/sharding/logical_select.rb 2017-10-31 14:59:54 +0900 (b6f05a42a) +++ plugins/sharding/logical_select.rb 2017-10-31 15:36:18 +0900 (b9a6f442f) @@ -230,33 +230,6 @@ module Groonga end end - class LabeledArgumentParser - def initialize(parameters) - @parameters = parameters - end - - def parse(prefix_pattern) - pattern = /\A#{prefix_pattern}\[(.+?)\]\.(.+)\z/ - labeled_arguments = {} - @parameters.each do |key, value| - match_data = pattern.match(key) - next if match_data.nil? - labeled_argument = (labeled_arguments[match_data[1]] ||= {}) - labeled_argument[match_data[2]] = value - end - labeled_arguments - end - end - - module KeysParsable - private - def parse_keys(raw_keys) - return [] if raw_keys.nil? - - raw_keys.strip.split(/ *, */) - end - end - module Calculatable def calc_target(table) return nil if @calc_target_name.nil? @@ -355,140 +328,6 @@ module Groonga end end - class DynamicColumns - class << self - def parse(input) - parser = LabeledArgumentParser.new(input) - columns = parser.parse(/columns?/) - - initial_contexts = [] - filtered_contexts = [] - output_contexts = [] - columns.each do |label, parameters| - contexts = nil - case parameters["stage"] - when "initial" - contexts = initial_contexts - when "filtered" - contexts = filtered_contexts - when "output" - contexts = output_contexts - else - next - end - contexts << DynamicColumnExecuteContext.new(label, parameters) - end - - new(initial_contexts, - filtered_contexts, - output_contexts) - end - end - - def initialize(initial_contexts, - filtered_contexts, - output_contexts) - @initial_contexts = initial_contexts - @filtered_contexts = filtered_contexts - @output_contexts = output_contexts - end - - def each_initial(&block) - @initial_contexts.each(&block) - end - - def each_filtered(&block) - @filtered_contexts.each(&block) - end - - def each_output(&block) - @output_contexts.each(&block) - end - - def close - @initial_contexts.each do |context| - context.close - end - @filtered_contexts.each do |context| - context.close - end - @output_contexts.each do |context| - context.close - end - end - end - - class DynamicColumnExecuteContext - include KeysParsable - - attr_reader :label - attr_reader :stage - attr_reader :type - attr_reader :flags - attr_reader :value - attr_reader :window_sort_keys - attr_reader :window_group_keys - def initialize(label, parameters) - @label = label - @stage = parameters["stage"] - @type = parse_type(parameters["type"]) - @flags = parse_flags(parameters["flags"] || "COLUMN_SCALAR") - @value = parameters["value"] - @window_sort_keys = parse_keys(parameters["window.sort_keys"]) - @window_group_keys = parse_keys(parameters["window.group_keys"]) - end - - def close - end - - def apply(table, condition=nil) - column = table.create_column(@label, @flags, @type) - return if table.empty? - - expression = Expression.create(table) - begin - expression.parse(@value) - if @window_sort_keys.empty? and @window_group_keys.empty? - expression.condition = condition if condition - table.apply_expression(column, expression) - else - table.apply_window_function(column, expression, - :sort_keys => @window_sort_keys, - :group_keys => @window_group_keys) - end - ensure - expression.close - end - end - - private - def parse_type(type_raw) - return nil if type_raw.nil? - - type = Context.instance[type_raw] - if type.nil? - message = "#{error_message_tag} unknown type: <#{type_raw}>" - raise InvalidArgument, message - end - - case type - when Type, Table - type - else - message = "#{error_message_tag} invalid type: #{type.grn_inspect}" - raise InvalidArgument, message - end - end - - def parse_flags(flags_raw) - Column.parse_flags(error_message_tag, flags_raw) - end - - def error_message_tag - "[logical_select][columns][#{@stage}][#{@label}]" - end - end - class PlainDrilldownExecuteContext include KeysParsable include Calculatable @@ -558,16 +397,13 @@ module Groonga class << self def parse(input) - parser = LabeledArgumentParser.new(input) - drilldowns = parser.parse(/drilldowns?/) - contexts = {} - drilldowns.each do |label, parameters| - next if parameters["keys"].nil? - context = LabeledDrilldownExecuteContext.new(label, parameters) + labeled_arguments = LabeledArguments.new(input, /drilldowns?/) + labeled_arguments.each do |label, arguments| + next if arguments["keys"].nil? + context = LabeledDrilldownExecuteContext.new(label, arguments) contexts[label] = context end - new(contexts) end end Modified: plugins/sharding/sources.am (+3 -1) =================================================================== --- plugins/sharding/sources.am 2017-10-31 14:59:54 +0900 (df2b6d023) +++ plugins/sharding/sources.am 2017-10-31 15:36:18 +0900 (0cadce3bd) @@ -7,4 +7,6 @@ sharding_scripts = \ logical_shard_list.rb \ logical_table_remove.rb \ parameters.rb \ - range_expression_builder.rb + range_expression_builder.rb \ + keys_parsable.rb \ + dynamic_columns.rb -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20171031/a0458816/attachment-0001.htm