[Groonga-commit] ranguba/rroonga at 66450c3 [bind-grn_obj_reindex] Add Groonga::VariableSizeColumn#reindex to bind grn_obj_reindex()

Back to archive index

Masafumi Yokoyama null+****@clear*****
Tue Jan 12 18:47:27 JST 2016


Masafumi Yokoyama	2016-01-12 18:47:27 +0900 (Tue, 12 Jan 2016)

  New Revision: 66450c350993f76219360c0d5eda0832c332cd77
  https://github.com/ranguba/rroonga/commit/66450c350993f76219360c0d5eda0832c332cd77

  Message:
    Add Groonga::VariableSizeColumn#reindex to bind grn_obj_reindex()
    
    GitHub: #110

  Modified files:
    ext/groonga/rb-grn-variable-size-column.c
    test/test-variable-size-column.rb

  Modified: ext/groonga/rb-grn-variable-size-column.c (+75 -1)
===================================================================
--- ext/groonga/rb-grn-variable-size-column.c    2016-01-10 15:44:47 +0900 (9e593fb)
+++ ext/groonga/rb-grn-variable-size-column.c    2016-01-12 18:47:27 +0900 (5d6cfb0)
@@ -1,7 +1,7 @@
 /* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /*
   Copyright (C) 2009-2014  Kouhei Sutou <kou �� clear-code.com>
-  Copyright (C) 2014  Masafumi Yokoyama <myokoym �� gmail.com>
+  Copyright (C) 2014-2016  Masafumi Yokoyama <myokoym �� gmail.com>
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -564,6 +564,77 @@ rb_grn_variable_size_column_defrag (int argc, VALUE *argv, VALUE self)
     return INT2NUM(n_segments);
 }
 
+/*
+ * Recreates all index columns for the column.
+ *
+ * This method is useful when you have any broken index columns for
+ * the column. You don't need to specify each index column. But this
+ * method spends more time rather than you specify only reindex
+ * needed index columns.
+ *
+ * @example How to recreate all index columns for the column
+ *   Groonga::Schema.define do |schema|
+ *     schema.create_table("Memos") do |table|
+ *       table.short_text("title")
+ *       table.text("content")
+ *     end
+ *
+ *     schema.create_table("BigramTerms",
+ *                         :type => :patricia_trie,
+ *                         :key_type => :short_text,
+ *                         :normalizer => "NormalizerAuto",
+ *                         :default_tokenizer => "TokenBigram") do |table|
+ *       table.index("Memos.title")
+ *       table.index("Memos.content")
+ *     end
+ *
+ *     schema.create_table("MeCabTerms",
+ *                         :type => :patricia_trie,
+ *                         :key_type => :short_text,
+ *                         :normalizer => "NormalizerAuto",
+ *                         :default_tokenizer => "TokenMecab") do |table|
+ *       table.index("Memos.title")
+ *       table.index("Memos.content")
+ *     end
+ *   end
+ *
+ *   Groonga["Memos.content"].reindex
+ *   # They are called:
+ *   #   Groonga["BigramTerms.Memos_content"].reindex
+ *   #   Groonga["MeCabTerms.Memos_content"].reindex
+ *   #
+ *   # They aren't called:
+ *   #   Groonga["BigramTerms.Memos_title"].reindex
+ *   #   Groonga["MeCabTerms.Memos_title"].reindex
+ *
+ * @overload reindex
+ *   @return [void]
+ *
+ * @see Groonga::Database#reindex
+ * @see Groonga::Table#reindex
+ * @see Groonga::FixSizeColumn#reindex
+ * @see Groonga::IndexColumn#reindex
+ *
+ * @since 5.1.1
+ */
+static VALUE
+rb_grn_variable_size_column_reindex (VALUE self)
+{
+    grn_rc rc;
+    grn_ctx *context;
+    grn_obj *column;
+
+    rb_grn_variable_size_column_deconstruct(SELF(self), &column, &context,
+                                            NULL, NULL, NULL, NULL,
+                                            NULL, NULL);
+
+    rc = grn_obj_reindex(context, column);
+    rb_grn_context_check(context, self);
+    rb_grn_rc_check(rc, self);
+
+    return Qnil;
+}
+
 void
 rb_grn_init_variable_size_column (VALUE mGrn)
 {
@@ -579,4 +650,7 @@ rb_grn_init_variable_size_column (VALUE mGrn)
                      rb_grn_variable_size_column_compressed_p, -1);
     rb_define_method(rb_cGrnVariableSizeColumn, "defrag",
                      rb_grn_variable_size_column_defrag, -1);
+
+    rb_define_method(rb_cGrnVariableSizeColumn, "reindex",
+                     rb_grn_variable_size_column_reindex, 0);
 }

  Modified: test/test-variable-size-column.rb (+70 -0)
===================================================================
--- test/test-variable-size-column.rb    2016-01-10 15:44:47 +0900 (048a620)
+++ test/test-variable-size-column.rb    2016-01-12 18:47:27 +0900 (21b919b)
@@ -124,6 +124,76 @@ class VariableSizeColumnTest < Test::Unit::TestCase
     assert_equal(1, @name.defrag)
   end
 
+  def test_reindex
+    Groonga::Schema.define do |schema|
+      schema.create_table("Memos", :type => :array) do |table|
+        table.short_text("title")
+        table.text("content")
+      end
+
+      schema.create_table("BigramTerms",
+                          :type => :patricia_trie,
+                          :key_type => :short_text,
+                          :normalizer => "NormalizerAuto",
+                          :default_tokenizer => "TokenBigram") do |table|
+        table.index("Memos.title")
+        table.index("Memos.content")
+      end
+
+      schema.create_table("MeCabTerms",
+                          :type => :patricia_trie,
+                          :key_type => :short_text,
+                          :normalizer => "NormalizerAuto",
+                          :default_tokenizer => "TokenMecab") do |table|
+        table.index("Memos.title")
+        table.index("Memos.content")
+      end
+    end
+
+    memos = context["Memos"]
+    memos.add(:title => "memo1", :content => "もり")
+
+    bigram_terms = context["BigramTerms"]
+    bigram_terms.delete("memo")
+    bigram_terms.delete("り")
+
+    mecab_terms = context["MeCabTerms"]
+    mecab_terms.delete("1")
+    mecab_terms.delete("もり")
+
+    assert_equal({
+                   :bigram => [
+                     "1",
+                     "もり",
+                   ],
+                   :mecab => [
+                     "memo",
+                   ],
+                 },
+                 {
+                   :bigram => bigram_terms.collect(&:_key).sort,
+                   :mecab => mecab_terms.collect(&:_key).sort,
+                 })
+
+    context["Memos.content"].reindex
+
+    assert_equal({
+                   :bigram => [
+                     "1",
+                     "もり",
+                     "り",
+                   ],
+                   :mecab => [
+                     "memo",
+                     "もり",
+                   ],
+                 },
+                 {
+                   :bigram => bigram_terms.collect(&:_key).sort,
+                   :mecab => mecab_terms.collect(&:_key).sort,
+                 })
+  end
+
   class VectorTest < self
     class ReferenceTest < self
       def test_append
-------------- next part --------------
HTML����������������������������...
Télécharger 



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