[Groonga-commit] groonga/groonga at 19a7488 [master] mrb: support estimating size for LESS operator

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Feb 19 16:41:01 JST 2015


Kouhei Sutou	2015-02-19 16:41:01 +0900 (Thu, 19 Feb 2015)

  New Revision: 19a7488190d2f28f075c071c21a1db4a0cdab757
  https://github.com/groonga/groonga/commit/19a7488190d2f28f075c071c21a1db4a0cdab757

  Message:
    mrb: support estimating size for LESS operator

  Modified files:
    lib/mrb/scripts/expression_size_estimator.rb
    test/query_optimizer/suite/test_estimate_size.rb

  Modified: lib/mrb/scripts/expression_size_estimator.rb (+32 -5)
===================================================================
--- lib/mrb/scripts/expression_size_estimator.rb    2015-02-19 16:38:44 +0900 (8da5739)
+++ lib/mrb/scripts/expression_size_estimator.rb    2015-02-19 16:41:01 +0900 (ce0cf98)
@@ -15,16 +15,43 @@ module Groonga
         if search_index.nil?
           @table.size
         else
-          index_column = search_index.index_column
-          if index_column.is_a?(Accessor)
-            # TODO
-            @table.size
+          case data.op
+          when Operator::MATCH
+            estimate_match(data, search_index) || @table.size
+          when Operator::LESS
+            estimate_less(data, search_index) || @table.size
           else
-            index_column.estimate_size(:query => data.query.value)
+            @table.size
           end
         end
       end
       sizes.min
     end
+
+    private
+    def estimate_match(data, search_index)
+      index_column = search_index.index_column
+      if index_column.is_a?(Accessor)
+        # TODO
+        return nil
+      end
+
+      index_column.estimate_size(:query => data.query.value)
+    end
+
+    def estimate_less(data, search_index)
+      index_column = search_index.index_column
+      if index_column.is_a?(Accessor)
+        # TODO
+        return nil
+      end
+
+      lexicon = index_column.lexicon
+      max = data.query.value
+      flags = TableCursorFlags::LT
+      TableCursor.open(lexicon, :max => max, :flags => flags) do |cursor|
+        index_column.estimate_size(:lexicon_cursor => cursor)
+      end
+    end
   end
 end

  Modified: test/query_optimizer/suite/test_estimate_size.rb (+31 -0)
===================================================================
--- test/query_optimizer/suite/test_estimate_size.rb    2015-02-19 16:38:44 +0900 (5d7d53b)
+++ test/query_optimizer/suite/test_estimate_size.rb    2015-02-19 16:41:01 +0900 (84ab6c6)
@@ -42,4 +42,35 @@ class TestEstimateSize < QueryOptimizerTestCase
       assert_equal(6, estimate_size("message @ 'Groonga'"))
     end
   end
+
+  class TestLessSearch < self
+    def setup
+      Groonga::Schema.define do |schema|
+        schema.create_table("Logs") do |table|
+          table.time("timestamp")
+        end
+
+        schema.create_table("Times",
+                            :type => :patricia_trie,
+                            :key_type => :time) do |table|
+          table.index("Logs", "timestamp")
+        end
+      end
+      super
+    end
+
+    def test_no_record
+      assert_equal(0, estimate_size("timestamp < '2015-02-19 02:19:00'"))
+    end
+
+    def test_have_record
+      @logs.add(:timestamp => "2015-02-19 02:17:00")
+      @logs.add(:timestamp => "2015-02-19 02:17:00")
+      @logs.add(:timestamp => "2015-02-19 02:18:00")
+      @logs.add(:timestamp => "2015-02-19 02:18:00")
+      @logs.add(:timestamp => "2015-02-19 02:19:00")
+      @logs.add(:timestamp => "2015-02-19 02:19:00")
+      assert_equal(8, estimate_size("timestamp < '2015-02-19 02:19:00'"))
+    end
+  end
 end
-------------- next part --------------
HTML����������������������������...
Download 



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