Kouhei Sutou
null+****@clear*****
Tue Sep 9 17:04:14 JST 2014
Kouhei Sutou 2014-09-09 17:04:14 +0900 (Tue, 09 Sep 2014) New Revision: c655a25e37c850ab84e9559e8059bf8932fb3bb3 https://github.com/groonga/groonga-query-log/commit/c655a25e37c850ab84e9559e8059bf8932fb3bb3 Message: Add IncompatibilityDetector It detects incompatible parameters from statistic. Added files: lib/groonga/query-log/incompatibility-detector.rb test/test-incompatibility-detector.rb Added: lib/groonga/query-log/incompatibility-detector.rb (+90 -0) 100644 =================================================================== --- /dev/null +++ lib/groonga/query-log/incompatibility-detector.rb 2014-09-09 17:04:14 +0900 (27da756) @@ -0,0 +1,90 @@ +# Copyright (C) 2014 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# 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 + +require "groonga/query-log/parser" + +module Groonga + module QueryLog + class IncompatibilityDetector + attr_reader :version + def initialize(version) + @version = version + end + + private + def build_message(command, parameter, description, value) + components = [ + command, + parameter, + description, + "<#{value}>", + ] + components.join(": ") + end + + class Version1 < self + def initialize + super(1) + end + + def detect(statistic) + [] + end + end + + class Version2 < self + def initialize + super(2) + end + + def detect(statistic) + case statistic.command.name + when "select" + detect_select(statistic) + else + [] + end + end + + private + def detect_select(statistic) + command = statistic.command + incompatibles = [] + space_delimiter_unacceptable_parameters = ["output_columns"] + space_delimiter_unacceptable_parameters.each do |parameter| + value = command[parameter] + if space_delimiter?(value) + description = "space is used as delimiter" + message = build_message("select", parameter, description, value) + incompatibles << message + end + end + incompatibles + end + + def space_delimiter?(string) + return false if string.nil? + return false if have_function_call?(string) + string.split(/\s+/) != string.split(/\s*,\s*/) + end + + def have_function_call?(string) + string.include?("(") + end + end + end + end +end Added: test/test-incompatibility-detector.rb (+57 -0) 100644 =================================================================== --- /dev/null +++ test/test-incompatibility-detector.rb 2014-09-09 17:04:14 +0900 (212e82e) @@ -0,0 +1,57 @@ +# Copyright (C) 2014 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# 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 + +require "groonga/query-log/incompatibility-detector" + +class IncompatibilityDetectorTest < Test::Unit::TestCase + def detect(command) + query_log = <<-LOG +2012-12-12 17:39:17.628846|0x7fff786aa2b0|>#{command} +2012-12-12 17:39:17.630052|0x7fff786aa2b0|<000000001217140 rc=0 + LOG + statistic = parse_query_log(query_log) + @detector.detect(statistic) + end + + def parse_query_log(query_log) + parser = Groonga::QueryLog::Parser.new + parser.parse(query_log) do |statistic| + return statistic + end + end + + sub_test_case("version1") do + def setup + @detector = Groonga::QueryLog::IncompatibilityDetector::Version1.new + end + end + + sub_test_case("version2") do + def setup + @detector = Groonga::QueryLog::IncompatibilityDetector::Version2.new + end + + sub_test_case("select") do + sub_test_case("output_columns") do + def test_space_delimiter + message = + "select: output_columns: space is used as delimiter: <_id _key>" + assert_equal([message], detect("select --output_columns '_id _key'")) + end + end + end + end +end -------------- next part -------------- HTML����������������������������...Download