[Groonga-commit] ranguba/rroonga at f20498a [master] Bind grn_ii_estimate_size() that is exported since Groonga 4.0.8

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Dec 12 17:24:33 JST 2014


Kouhei Sutou	2014-12-12 17:24:33 +0900 (Fri, 12 Dec 2014)

  New Revision: f20498aa4dcebb3d3d06ed1d3b0ab6ea42155d41
  https://github.com/ranguba/rroonga/commit/f20498aa4dcebb3d3d06ed1d3b0ab6ea42155d41

  Message:
    Bind grn_ii_estimate_size() that is exported since Groonga 4.0.8
    
    GitHub: fix #38

  Modified files:
    ext/groonga/rb-grn-index-column.c
    rroonga-build.rb
    test/test-index-column.rb

  Modified: ext/groonga/rb-grn-index-column.c (+94 -0)
===================================================================
--- ext/groonga/rb-grn-index-column.c    2014-12-03 23:36:55 +0900 (23741ad)
+++ ext/groonga/rb-grn-index-column.c    2014-12-12 17:24:33 +0900 (71f74bc)
@@ -955,6 +955,97 @@ rb_grn_index_column_open_cursor (int argc, VALUE *argv, VALUE self)
         return rb_cursor;
 }
 
+/*
+ * Estimates the number of documents found by the given token ID.
+ *
+ * @example Token ID style
+ *    # Define schema
+ *    Groonga::Schema.define do |schema|
+ *      schema.create_table("Articles") do |table|
+ *        table.text("content")
+ *      end
+ *
+ *      schema.create_table("Terms",
+ *                          :type => :hash,
+ *                          :key_type => "ShortText",
+ *                          :default_tokenizer => "TokenBigram",
+ *                          :normalizer => "NormalizerAuto") do |table|
+ *        table.index("Articles.content",
+ *                    :name => "articles_content",
+ *                    :with_position => true,
+ *                    :with_section => true)
+ *      end
+ *    end
+ *    articles = Groonga["Articles"]
+ *    terms = Groonga["Terms"]
+ *    index = Groonga["Terms.articles_content"]
+ *
+ *    # Add data
+ *    articles.add(:content => "Groonga is fast")
+ *    articles.add(:content => "Rroonga is fast")
+ *    articles.add(:content => "Mroonga is fast")
+ *
+ *    # Estimate the number of documents found by token ID
+ *    p****@index*****_size(@terms["fast"].id)    # => 7
+ *    p****@index*****_size(@terms["Groonga"].id) # => 1
+ *
+ * @example Token record style
+ *    # Define schema
+ *    Groonga::Schema.define do |schema|
+ *      schema.create_table("Articles") do |table|
+ *        table.text("content")
+ *      end
+ *
+ *      schema.create_table("Terms",
+ *                          :type => :hash,
+ *                          :key_type => "ShortText",
+ *                          :default_tokenizer => "TokenBigram",
+ *                          :normalizer => "NormalizerAuto") do |table|
+ *        table.index("Articles.content",
+ *                    :name => "articles_content",
+ *                    :with_position => true,
+ *                    :with_section => true)
+ *      end
+ *    end
+ *    articles = Groonga["Articles"]
+ *    terms = Groonga["Terms"]
+ *    index = Groonga["Terms.articles_content"]
+ *
+ *    # Add data
+ *    articles.add(:content => "Groonga is fast")
+ *    articles.add(:content => "Rroonga is fast")
+ *    articles.add(:content => "Mroonga is fast")
+ *
+ *    # Estimate the number of documents found by token record
+ *    p****@index*****_size(@terms["fast"])    # => 7
+ *    p****@index*****_size(@terms["Groonga"]) # => 1
+ *
+ * @overload estimate_size(token_id)
+ *   @param [Integer, Record] token_id The token ID to be estimated.
+ *   @return [Integer] The estimated number of documents found by the
+ *     given token ID.
+ */
+static VALUE
+rb_grn_index_column_estimate_size (VALUE self, VALUE rb_token_id)
+{
+    grn_ctx *context;
+    grn_obj *column;
+    grn_obj *domain_object;
+    grn_id token_id;
+    unsigned int size;
+
+    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
+                                    NULL, &domain_object,
+                                    NULL, NULL, NULL,
+                                    NULL, NULL,
+                                    NULL, NULL);
+
+    token_id = RVAL2GRNID(rb_token_id, context, domain_object, self);
+    size = grn_ii_estimate_size(context, (grn_ii *)column, token_id);
+
+    return UINT2NUM(size);
+}
+
 void
 rb_grn_init_index_column (VALUE mGrn)
 {
@@ -990,4 +1081,7 @@ rb_grn_init_index_column (VALUE mGrn)
 
     rb_define_method(rb_cGrnIndexColumn, "open_cursor",
                      rb_grn_index_column_open_cursor, -1);
+
+    rb_define_method(rb_cGrnIndexColumn, "estimate_size",
+                     rb_grn_index_column_estimate_size, 1);
 }

  Modified: rroonga-build.rb (+2 -2)
===================================================================
--- rroonga-build.rb    2014-12-03 23:36:55 +0900 (77842b0)
+++ rroonga-build.rb    2014-12-12 17:24:33 +0900 (e31ed02)
@@ -19,9 +19,9 @@ module RroongaBuild
   module RequiredGroongaVersion
     MAJOR = 4
     MINOR = 0
-    MICRO = 7
+    MICRO = 8
     VERSION = [MAJOR, MINOR, MICRO]
-    RELEASED_DATE = Time.utc(2014, 10, 29)
+    RELEASED_DATE = Time.utc(2014, 11, 29)
   end
 
   module LatestGroongaVersion

  Modified: test/test-index-column.rb (+41 -0)
===================================================================
--- test/test-index-column.rb    2014-12-03 23:36:55 +0900 (8ab9f26)
+++ test/test-index-column.rb    2014-12-12 17:24:33 +0900 (4434be1)
@@ -500,4 +500,45 @@ class IndexColumnTest < Test::Unit::TestCase
       end
     end
   end
+
+  class EstimateSizeTest < self
+    setup
+    def setup_schema
+      Groonga::Schema.define do |schema|
+        schema.create_table("Articles") do |table|
+          table.text("content")
+        end
+
+        schema.create_table("Terms",
+                            :type => :hash,
+                            :key_type => "ShortText",
+                            :default_tokenizer => "TokenBigram",
+                            :normalizer => "NormalizerAuto") do |table|
+          table.index("Articles.content",
+                      :name => "articles_content",
+                      :with_position => true,
+                      :with_section => true)
+        end
+      end
+
+      @articles = Groonga["Articles"]
+      @terms = Groonga["Terms"]
+      @index = Groonga["Terms.articles_content"]
+    end
+
+    setup
+    def setup_data
+      @articles.add(:content => "Groonga is fast")
+      @articles.add(:content => "Rroonga is fast")
+      @articles.add(:content => "Mroonga is fast")
+    end
+
+    def test_id
+      assert_equal(7, @index.estimate_size(@terms["fast"].id))
+    end
+
+    def test_record
+      assert_equal(7, @index.estimate_size(@terms["fast"]))
+    end
+  end
 end
-------------- next part --------------
HTML����������������������������...
Télécharger 



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