Skip to content

Commit

Permalink
Merge pull request #120 from ranguba/bind-grn_column_get_all_index_data
Browse files Browse the repository at this point in the history
Bind grn_column_get_all_index_data()

Patch by Masafumi Yokoyama. Thanks!!!
  • Loading branch information
kou committed Mar 5, 2016
2 parents 947b49c + 7707a5e commit dc4f11e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
41 changes: 41 additions & 0 deletions ext/groonga/rb-grn-column.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* vim: set sts=4 sw=4 ts=8 noet: */
/*
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
Expand Down Expand Up @@ -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<index_column>] 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
Expand Down Expand Up @@ -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);

Expand Down
33 changes: 33 additions & 0 deletions test/test-column.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (C) 2009-2013 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
Expand Down Expand Up @@ -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

0 comments on commit dc4f11e

Please sign in to comment.