Masafumi Yokoyama
null+****@clear*****
Tue Jan 12 19:10:31 JST 2016
Masafumi Yokoyama 2016-01-12 19:10:31 +0900 (Tue, 12 Jan 2016) New Revision: 157e4b0aea96edb906729d69c00498d02ef98084 https://github.com/ranguba/rroonga/commit/157e4b0aea96edb906729d69c00498d02ef98084 Message: Add Groonga::IndexColumn#reindex to bind grn_obj_reindex() GitHub: #110 Modified files: ext/groonga/rb-grn-index-column.c test/test-index-column.rb Modified: ext/groonga/rb-grn-index-column.c (+82 -0) =================================================================== --- ext/groonga/rb-grn-index-column.c 2016-01-12 19:01:24 +0900 (09b8e65) +++ ext/groonga/rb-grn-index-column.c 2016-01-12 19:10:31 +0900 (45a471e) @@ -1,6 +1,7 @@ /* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* Copyright (C) 2009-2015 Kouhei Sutou <kou �� clear-code.com> + Copyright (C) 2016 Masafumi Yokoyama <yokoyama �� 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 @@ -1187,6 +1188,84 @@ rb_grn_index_column_estimate_size (int argc, VALUE *argv, VALUE self) return UINT2NUM(size); } +/* + * Recreates the index column. + * + * This method is useful when you have a broken index column. + * + * You can use {Groonga::Database#reindex} to recreate all index + * columns in a database. + * + * You can use {Groonga::Table#reindex} to recreate all index + * columns in a table. + * + * You can use {Groonga::FixSizeColumn#reindex} or + * {Groonga::VariableSizeColumn#reindex} to recreate all index + * columns. They use index columns of the data column as + * reindex target index columns. + * + * @example How to recreate the index 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["MeCabTerms.Memos_content"].reindex + * # They aren't called: + * # Groonga["BigramTerms.Memos_title"].reindex + * # Groonga["BigramTerms.Memos_content"].reindex + * # Groonga["MeCabTerms.Memos_title"].reindex + * + * @overload reindex + * @return [void] + * + * @see Groonga::Database#reindex + * @see Groonga::Table#reindex + * @see Groonga::FixSizeColumn#reindex + * @see Groonga::VariableSizeColumn#reindex + * + * @since 5.1.1 + */ +static VALUE +rb_grn_index_column_reindex (VALUE self) +{ + grn_rc rc; + grn_ctx *context; + grn_obj *column; + + rb_grn_index_column_deconstruct(SELF(self), &column, &context, + NULL, NULL, + NULL, 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_index_column (VALUE mGrn) { @@ -1225,4 +1304,7 @@ rb_grn_init_index_column (VALUE mGrn) rb_define_method(rb_cGrnIndexColumn, "estimate_size", rb_grn_index_column_estimate_size, -1); + + rb_define_method(rb_cGrnIndexColumn, "reindex", + rb_grn_index_column_reindex, 0); } Modified: test/test-index-column.rb (+70 -0) =================================================================== --- test/test-index-column.rb 2016-01-12 19:01:24 +0900 (e07a8e4) +++ test/test-index-column.rb 2016-01-12 19:10:31 +0900 (aff362c) @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # # Copyright (C) 2009-2015 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2016 Masafumi Yokoyama <yokoyama �� 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 @@ -175,6 +176,75 @@ class IndexColumnTest < Test::Unit::TestCase end 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["MeCabTerms.Memos_content"].reindex + + assert_equal({ + :bigram => [ + "1", + "もり", + ], + :mecab => [ + "memo", + "もり", + ], + }, + { + :bigram => bigram_terms.collect(&:_key).sort, + :mecab => mecab_terms.collect(&:_key).sort, + }) + end + class NGramTest < self setup def setup_schema -------------- next part -------------- HTML����������������������������...Download