diff --git a/ext/groonga/rb-grn-column.c b/ext/groonga/rb-grn-column.c index 9092422d..d84f9ad5 100644 --- a/ext/groonga/rb-grn-column.c +++ b/ext/groonga/rb-grn-column.c @@ -2,6 +2,7 @@ /* vim: set sts=4 sw=4 ts=8 noet: */ /* Copyright (C) 2009-2015 Kouhei Sutou + Copyright (C) 2016 Masafumi Yokoyama This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -753,6 +754,45 @@ rb_grn_column_get_indexes (int argc, VALUE *argv, VALUE self) return rb_indexes; } +/* + * Return all indexes on `column`. + * + * @overload all_indexes + * @return [Array] All indexes on `column`. + * + * @since 6.0.0 + */ +static VALUE +rb_grn_column_get_all_indexes (VALUE self) +{ + grn_ctx *context; + grn_obj *column; + grn_index_datum *index_data = NULL; + int i, n_indexes; + VALUE rb_indexes; + + rb_grn_column_deconstruct(SELF(self), &column, &context, + NULL, NULL, + NULL, NULL, NULL); + + rb_indexes = rb_ary_new(); + n_indexes = grn_column_get_all_index_data(context, column, NULL, 0); + if (n_indexes == 0) + return rb_indexes; + + index_data = xmalloc(sizeof(grn_index_datum) * n_indexes); + n_indexes = grn_column_get_all_index_data(context, column, + index_data, n_indexes); + for (i = 0; i < n_indexes; i++) { + VALUE rb_index; + rb_index = GRNOBJECT2RVAL(Qnil, context, index_data[i].index, GRN_FALSE); + rb_ary_push(rb_indexes, rb_index); + grn_obj_unlink(context, index_data[i].index); + } + xfree(index_data); + return rb_indexes; +} + /* * Renames the column to name. * @since 1.3.0 @@ -813,6 +853,7 @@ rb_grn_init_column (VALUE mGrn) rb_grn_column_with_weight_p, 0); rb_define_method(rb_cGrnColumn, "indexes", rb_grn_column_get_indexes, -1); + rb_define_method(rb_cGrnColumn, "all_indexes", rb_grn_column_get_all_indexes, 0); rb_define_method(rb_cGrnColumn, "rename", rb_grn_column_rename, 1); diff --git a/test/test-column.rb b/test/test-column.rb index 17d03ea8..58b677ee 100644 --- a/test/test-column.rb +++ b/test/test-column.rb @@ -1,4 +1,5 @@ # Copyright (C) 2009-2013 Kouhei Sutou +# Copyright (C) 2016 Masafumi Yokoyama # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -584,4 +585,36 @@ def test_indexed assert_equal(["title1"], select.call) end end + + class AllIndexedTest < self + def setup + super + Groonga::Schema.define do |schema| + schema.create_table("Comments") do |table| + table.short_text("title") + end + end + @title = Groonga["Comments.title"] + end + + def test_nothing + assert_equal([], @title.all_indexes) + end + + def test_one_index + Groonga::Schema.define do |schema| + schema.create_table("Terms", + :type => :patricia_trie, + :default_tokenizer => "TokenBigram") do |table| + table.index("Comments.title") + end + end + assert_equal([Groonga["Terms.Comments_title"]], + @title.all_indexes) + end + + def test_multiple_indexes + # TODO + end + end end