Kouhei Sutou
null+****@clear*****
Tue Apr 14 18:11:56 JST 2015
Kouhei Sutou 2015-04-14 18:11:56 +0900 (Tue, 14 Apr 2015) New Revision: ff4085ae5d7a860bbfaf88f9f9562f6a0754906c https://github.com/ranguba/rroonga/commit/ff4085ae5d7a860bbfaf88f9f9562f6a0754906c Message: Support estimating size by lexicon cursor Modified files: ext/groonga/rb-grn-index-column.c test/test-index-column.rb Modified: ext/groonga/rb-grn-index-column.c (+48 -2) =================================================================== --- ext/groonga/rb-grn-index-column.c 2015-04-14 17:53:14 +0900 (39396ce) +++ ext/groonga/rb-grn-index-column.c 2015-04-14 18:11:56 +0900 (09b8e65) @@ -957,7 +957,8 @@ rb_grn_index_column_open_cursor (int argc, VALUE *argv, VALUE self) } /* - * Estimates the number of documents found by the given token ID. + * Estimates the number of documents found by the given token ID, + * query or lexicon cursor. * * @example Token ID style * # Define schema @@ -1040,7 +1041,6 @@ rb_grn_index_column_open_cursor (int argc, VALUE *argv, VALUE self) * end * end * articles = Groonga["Articles"] - * terms = Groonga["Terms"] * index = Groonga["Terms.articles_content"] * * # Add data @@ -1051,6 +1051,38 @@ rb_grn_index_column_open_cursor (int argc, VALUE *argv, VALUE self) * # Estimate the number of documents found by query * p****@index*****_size("roonga") # => 6 * + * @example Lexicon cursor style + * # Define schema + * Groonga::Schema.define do |schema| + * schema.create_table("Memos", + * :type => :hash, + * :key_type => "ShortText") do |table| + * table.short_text("tags", :type => :vector) + * end + * + * schema.create_table("Tags", + * :type => :patricia_trie, + * :key_type => "ShortText") do |table| + * table.index("Memos.tags", + * :name => "memos_tags") + * end + * end + * memos = Groonga["Memos"] + * tags = Groonga["Tags"] + * index = Groonga["Tags.memos_tags"] + * + * # Add data + * memos.add(:tags => ["Groonga"]) + * memos.add(:tags => ["Rroonga", "Ruby"]) + * memos.add(:tags => ["grndump", "Rroonga"]) + * + * # Estimate the number of documents found by lexicon cursor + * # Iterates tags that start with "R". + * tags.open_prefix_cursor("R") do |cursor| + * # The cursor iterates "Rroonga" and "Ruby". + * p index.estimate_size(cursor) # => 6 + * end + * * @overload estimate_size(token_id) * @param token_id [Integer, Record] The token ID to be estimated. * @return [Integer] The estimated number of documents found by the @@ -1075,6 +1107,13 @@ rb_grn_index_column_open_cursor (int argc, VALUE *argv, VALUE self) * * @since 5.0.1 * + * @overload estimate_size(lexicon_cursor) + * @param lexicon_cursor [Groonga::TableCursor] The cursor for lexicon. + * @return [Integer] The estimated number of documents found by term IDS + * in the given lexicon cursor. + * + * @since 5.0.1 + * * @since 4.0.7 */ static VALUE @@ -1132,6 +1171,13 @@ rb_grn_index_column_estimate_size (int argc, VALUE *argv, VALUE self) size = grn_ii_estimate_size_for_query(context, (grn_ii *)column, query, query_length, &options); + } else if (RVAL2CBOOL(rb_obj_is_kind_of(rb_target, rb_cGrnTableCursor))) { + grn_table_cursor *lexicon_cursor; + + lexicon_cursor = RVAL2GRNTABLECURSOR(rb_target, &context); + size = grn_ii_estimate_size_for_lexicon_cursor(context, + (grn_ii *)column, + lexicon_cursor); } else { grn_id token_id; token_id = RVAL2GRNID(rb_target, context, domain_object, self); Modified: test/test-index-column.rb (+35 -0) =================================================================== --- test/test-index-column.rb 2015-04-14 17:53:14 +0900 (3df88e7) +++ test/test-index-column.rb 2015-04-14 18:11:56 +0900 (d92ec8d) @@ -578,5 +578,40 @@ class IndexColumnTest < Test::Unit::TestCase assert_equal(6, @index.estimate_size("roonga")) end end + + sub_test_case "lexicon cursor" do + setup + def setup_schema + Groonga::Schema.define do |schema| + schema.create_table("Memos") do |table| + table.short_text("tags", :type => :vector) + end + + schema.create_table("Tags", + :type => :patricia_trie, + :key_type => "ShortText") do |table| + table.index("Memos.tags", + :name => "memos_tags") + end + end + + @memos = Groonga["Memos"] + @tags = Groonga["Tags"] + @index = Groonga["Tags.memos_tags"] + end + + setup + def setup_data + @memos.add(:tags => ["Groonga"]) + @memos.add(:tags => ["Rroonga", "Ruby"]) + @memos.add(:tags => ["grndump", "Rroonga"]) + end + + def test_query + @tags.open_prefix_cursor("R") do |cursor| + assert_equal(5, @index.estimate_size(cursor)) + end + end + end end end -------------- next part -------------- HTML����������������������������...Download