[Groonga-commit] groonga/groonga-query-log at 454ab8d [master] verify-server: support rewriting "vector == ..."

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Sep 13 11:32:33 JST 2018


Kouhei Sutou	2018-09-13 11:32:33 +0900 (Thu, 13 Sep 2018)

  Revision: 454ab8d6a18125bf0613055dde25c0a6eae9fbcd
  https://github.com/groonga/groonga-query-log/commit/454ab8d6a18125bf0613055dde25c0a6eae9fbcd

  Message:
    verify-server: support rewriting "vector == ..."

  Added files:
    lib/groonga-query-log/filter-rewriter.rb
    test/test-filter-rewriter.rb
  Modified files:
    lib/groonga-query-log/command/verify-server.rb
    lib/groonga-query-log/server-verifier.rb

  Modified: lib/groonga-query-log/command/verify-server.rb (+13 -0)
===================================================================
--- lib/groonga-query-log/command/verify-server.rb    2018-09-13 10:27:14 +0900 (a8fd3bb)
+++ lib/groonga-query-log/command/verify-server.rb    2018-09-13 11:32:33 +0900 (908c19a)
@@ -201,6 +201,19 @@ module GroongaQueryLog
           @options.stop_on_failure = boolean
         end
 
+        parser.on("--[no-]rewrite-vector-equal",
+                  "Rewrite 'vector == ...' with 'vector @ ...'",
+                  "(#{@options.rewrite_vector_equal?})") do |boolean|
+          @options.rewrite_vector_equal = boolean
+        end
+
+        parser.on("--vector-accessor=ACCESSOR",
+                  "Mark ACCESSOR as rewrite vector targets",
+                  "You can specify multiple vector accessors by",
+                  "specifying this option multiple times") do |accessor|
+          @options.vector_accessors << accessor
+        end
+
         parser.separator("Debug options:")
         parser.separator("")
 

  Added: lib/groonga-query-log/filter-rewriter.rb (+45 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga-query-log/filter-rewriter.rb    2018-09-13 11:32:33 +0900 (f96571f)
@@ -0,0 +1,45 @@
+# Copyright (C) 2018  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
+
+module GroongaQueryLog
+  class FilterRewriter
+    def initialize(filter, options={})
+      @filter = filter
+      @options = options
+      @vector_accessors = @options[:vector_accssors] || []
+    end
+
+    def rewrite
+      rewritten = @filter
+      if @options[:rewrite_vector_equal]
+        rewritten = rewrite_vector_equal(rewritten)
+      end
+      rewritten
+    end
+
+    private
+    def rewrite_vector_equal(filter)
+      filter.gsub(/([a-zA-Z0-9.]+) *==/) do |matched|
+        variable = $1
+        if @vector_accessors.include?(variable)
+          "#{variable} @"
+        else
+          matched
+        end
+      end
+    end
+  end
+end

  Modified: lib/groonga-query-log/server-verifier.rb (+26 -0)
===================================================================
--- lib/groonga-query-log/server-verifier.rb    2018-09-13 10:27:14 +0900 (4d0a2b0)
+++ lib/groonga-query-log/server-verifier.rb    2018-09-13 11:32:33 +0900 (9a06c73)
@@ -19,6 +19,7 @@ require "thread"
 
 require "groonga/client"
 
+require "groonga-query-log/filter-rewriter"
 require "groonga-query-log/parser"
 require "groonga-query-log/response-comparer"
 
@@ -135,6 +136,12 @@ module GroongaQueryLog
     def verify_command(groonga1_client, groonga2_client, command)
       command["cache"] = "no" if****@optio*****_cache?
       command["output_type"] = "json"
+      filter = command["filter"]
+      if filter and****@optio*****_filter_rewrite?
+        rewriter = FilterRewriter.new(filter,
+                                      @options.to_filter_rewriter_options)
+        command["filter"] = rewriter.rewrite
+      end
       response1 = groonga1_client.execute(command)
       response2 = groonga2_client.execute(command)
       compare_options = {
@@ -182,6 +189,8 @@ module GroongaQueryLog
       attr_writer :verify_cache
       attr_accessor :ignored_drilldown_keys
       attr_writer :stop_on_failure
+      attr_writer :rewrite_vector_equal
+      attr_accessor :vector_accessors
       def initialize
         @groonga1 = GroongaOptions.new
         @groonga2 = GroongaOptions.new
@@ -204,6 +213,8 @@ module GroongaQueryLog
         @verify_cache = false
         @ignored_drilldown_keys = []
         @stop_on_failure = false
+        @rewrite_vector_equal = false
+        @vector_accessors = []
       end
 
       def request_queue_size
@@ -222,6 +233,10 @@ module GroongaQueryLog
         @stop_on_failure
       end
 
+      def rewrite_vector_equal?
+        @rewrite_vector_equal
+      end
+
       def target_command_name?(name)
         return false if name.nil?
 
@@ -240,6 +255,17 @@ module GroongaQueryLog
           yield($stdout)
         end
       end
+
+      def need_filter_rewrite?
+        rewrite_vector_equal?
+      end
+
+      def to_filter_rewriter_options
+        {
+          :rewrite_vector_equal => rewrite_vector_equal?,
+          :vector_accessors => vector_accessors,
+        }
+      end
     end
 
     class GroongaOptions

  Added: test/test-filter-rewriter.rb (+43 -0) 100644
===================================================================
--- /dev/null
+++ test/test-filter-rewriter.rb    2018-09-13 11:32:33 +0900 (64fd967)
@@ -0,0 +1,43 @@
+# Copyright (C) 2018  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
+
+class FilterRewriterTest < Test::Unit::TestCase
+  private
+  def rewrite(filter, options)
+    rewriter = GroongaQueryLog::FilterRewriter.new(filter, options)
+    rewriter.rewrite
+  end
+
+  class VectorEqualTest < self
+    def rewrite_vector_equal(filter, vector_accessors)
+      rewrite(filter,
+              :rewrite_vector_equal => true,
+              :vector_accssors => vector_accessors)
+    end
+
+    def test_not_target_accessor
+      assert_equal("vector == \"value\"",
+                   rewrite_vector_equal("vector == \"value\"",
+                                        ["nonexistent"]))
+    end
+
+    def test_parenthesis
+      assert_equal("((vector @ \"value\"))",
+                   rewrite_vector_equal("((vector == \"value\"))",
+                                        ["vector"]))
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180913/f365b2e8/attachment-0001.htm 



More information about the Groonga-commit mailing list
Back to archive index