[Groonga-commit] groonga/groonga [master] [query-log][analyzer] improve filter condition extraction.

Back to archive index

null+****@clear***** null+****@clear*****
2011年 6月 1日 (水) 12:27:09 JST


Kouhei Sutou	2011-06-01 03:27:09 +0000 (Wed, 01 Jun 2011)

  New Revision: 29097113aba5d819e34afb3b6aeb0b398b5750c4

  Log:
    [query-log][analyzer] improve filter condition extraction.

  Modified files:
    test/unit/tools/test-query-log-analyzer.rb
    tools/groonga-query-log-analyzer.rb

  Modified: test/unit/tools/test-query-log-analyzer.rb (+39 -7)
===================================================================
--- test/unit/tools/test-query-log-analyzer.rb    2011-06-01 03:03:35 +0000 (58df751)
+++ test/unit/tools/test-query-log-analyzer.rb    2011-06-01 03:27:09 +0000 (2dbc3d5)
@@ -18,23 +18,55 @@
 require "groonga-query-log-analyzer"
 
 module QueryLogAalyzerTest
+  module CommandParseTestUtils
+    private
+    def command(name, parameters)
+      GroongaQueryLogAnaylzer::Command.new(name, parameters)
+    end
+
+    def parse(command_path, parameters)
+      path = "#{command_path}?"
+      path << parameters.collect do |key, value|
+        [CGI.escape(key.to_s), CGI.escape(value.to_s)].join("=")
+      end.join("&")
+      GroongaQueryLogAnaylzer::Command.parse(path)
+    end
+  end
+
   class CommandParseTest < Test::Unit::TestCase
+    include CommandParseTestUtils
+
     def test_simple
-      select = parse("/d/select.json?table=Users&filter=age<=30")
+      select = parse("/d/select.json",
+                     :table => "Users",
+                     :filter => "age<=30")
       assert_equal(command("select",
                            "table" => "Users",
                            "filter" => "age<=30",
                            "output_type" => "json"),
                    select)
     end
+  end
 
-    private
-    def command(name, parameters)
-      GroongaQueryLogAnaylzer::Command.new(name, parameters)
-    end
+  class SelectCommandFilterParseTest < Test::Unit::TestCase
+    include CommandParseTestUtils
 
-    def parse(command_path)
-      GroongaQueryLogAnaylzer::Command.parse(command_path)
+    def test_simple
+      filter = 'geo_in_rectangle(location,' +
+                                '"35.73360x139.7394","62614x139.7714") && ' +
+               '((type == "たいやき" || type == "和菓子")) && ' +
+               'keyword @ "たいやき" &! keyword @ "白" &! keyword @ "養殖"'
+      select = parse("/d/select.json",
+                     :table => "Users",
+                     :filter => filter)
+      assert_equal(['geo_in_rectangle(location,' +
+                                     '"35.73360x139.7394","62614x139.7714")',
+                     'type == "たいやき"',
+                     'type == "和菓子"',
+                     'keyword @ "たいやき"',
+                     'keyword @ "白"',
+                     'keyword @ "養殖"'],
+                   select.conditions)
     end
   end
 end

  Modified: tools/groonga-query-log-analyzer.rb (+17 -4)
===================================================================
--- tools/groonga-query-log-analyzer.rb    2011-06-01 03:03:35 +0000 (0d8d5c2)
+++ tools/groonga-query-log-analyzer.rb    2011-06-01 03:27:09 +0000 (977cb15)
@@ -60,6 +60,11 @@ class GroongaQueryLogAnaylzer
 
   class Command
     class << self
+      @@registered_commands = {}
+      def register(name, klass)
+        @@registered_commands[name] = klass
+      end
+
       def parse(command_path)
         name, parameters_string = command_path.split(/\?/, 2)
         parameters = {}
@@ -70,7 +75,8 @@ class GroongaQueryLogAnaylzer
         name = name.gsub(/\A\/d\//, '')
         name, output_type = name.split(/\./, 2)
         parameters["output_type"] = output_type if output_type
-        new(name, parameters)
+        command_class = @@registered_commands[name] || self
+        command_class.new(name, parameters)
       end
     end
 
@@ -88,12 +94,19 @@ class GroongaQueryLogAnaylzer
   end
 
   class SelectCommand < Command
+    register("select", self)
+
     def sortby
       @parameters["sortby"]
     end
 
-    def filters
-      @parameters["filter"].split(/(?:&&|&!|\|\|)/)
+    def conditions
+      @parameters["filter"].split(/(?:&&|&!|\|\|)/).collect do |condition|
+        condition = condition.strip
+        condition = condition.gsub(/\A[\s\(]*/, '')
+        condition = condition.gsub(/[\s\)]*\z/, '') unless /\(/ =~ condition
+        condition
+      end
     end
 
     def output_columns
@@ -169,7 +182,7 @@ class GroongaQueryLogAnaylzer
     def format_trace_label(label, i)
       case label
       when /\Afilter\(/
-        "#{label} <#{@select_command.filters[i]}>"
+        "#{label} <#{@select_command.conditions[i]}>"
       when /\Asort\(/
         "#{label} <#{@select_command.sortby}>"
       when /\Aoutput\(/




Groonga-commit メーリングリストの案内
Back to archive index