null+****@clear*****
null+****@clear*****
2010年 8月 3日 (火) 17:28:33 JST
Kouhei Sutou 2010-08-03 08:28:33 +0000 (Tue, 03 Aug 2010) New Revision: afd439c162a9f80b0f7ef6dccd8b74dce7e2d5f6 Log: support vector in XML output. Modified files: src/groonga.c test/unit/http/test-http-select-basic.rb test/unit/http/test-http-select-drilldown.rb Modified: src/groonga.c (+24 -11) =================================================================== --- src/groonga.c 2010-08-03 08:11:34 +0000 (d140364) +++ src/groonga.c 2010-08-03 08:28:33 +0000 (c74ecca) @@ -232,7 +232,7 @@ transform_xml(grn_ctx *ctx, grn_obj *output, grn_obj *transformed) unsigned int len; int offset = 0, limit = 0, record_n = 0; int column_n, column_text_n, result_set_n = -1; - int in_vector = 0; + int in_vector = 0, first_vector_element = 0; s = GRN_TEXT_VALUE(output); e = GRN_BULK_CURR(output); @@ -286,6 +286,7 @@ transform_xml(grn_ctx *ctx, grn_obj *output, grn_obj *transformed) } } else if (EQUAL_NAME_P("VECTOR")) { in_vector = 1; + first_vector_element = 1; } break; case XML_END_ELEMENT : @@ -312,27 +313,36 @@ transform_xml(grn_ctx *ctx, grn_obj *output, grn_obj *transformed) "</SEGMENTS>\n"); } else if (EQUAL_NAME_P("VECTOR")) { in_vector = 0; + first_vector_element = 0; + GRN_TEXT_PUTS(ctx, transformed, "</FIELD>\n"); } else { switch (place) { case XML_PLACE_HIT : { int i = column_n; char *c = GRN_TEXT_VALUE(&columns); - while (i--) { - while (*c) { + if (!in_vector || first_vector_element) { + while (i--) { + while (*c) { + c++; + } c++; } - c++; } if (result_set_n == 0) { - GRN_TEXT_PUTS(ctx, transformed, "<FIELD NAME=\""); - GRN_TEXT_PUTS(ctx, transformed, c); - GRN_TEXT_PUTS(ctx, transformed, "\">"); + if (!in_vector || first_vector_element) { + GRN_TEXT_PUTS(ctx, transformed, "<FIELD NAME=\""); + GRN_TEXT_PUTS(ctx, transformed, c); + GRN_TEXT_PUTS(ctx, transformed, "\">"); + } + if (in_vector && !first_vector_element) { + GRN_TEXT_PUTS(ctx, transformed, ", "); + } + GRN_TEXT_PUT(ctx, transformed, + GRN_TEXT_VALUE(&buf), GRN_TEXT_LEN(&buf)); if (!in_vector) { - GRN_TEXT_PUT(ctx, transformed, - GRN_TEXT_VALUE(&buf), GRN_TEXT_LEN(&buf)); + GRN_TEXT_PUTS(ctx, transformed, "</FIELD>\n"); } - GRN_TEXT_PUTS(ctx, transformed, "</FIELD>\n"); } else { GRN_TEXT_PUTS(ctx, transformed, c); GRN_TEXT_PUTS(ctx, transformed, "=\""); @@ -341,7 +351,10 @@ transform_xml(grn_ctx *ctx, grn_obj *output, grn_obj *transformed) GRN_TEXT_PUTS(ctx, transformed, "\" "); } } - column_n++; + if (!in_vector || first_vector_element) { + column_n++; + first_vector_element = 0; + } break; default : if (EQUAL_NAME_P("NHITS")) { Modified: test/unit/http/test-http-select-basic.rb (+41 -0) =================================================================== --- test/unit/http/test-http-select-basic.rb 2010-08-03 08:11:34 +0000 (27bef0f) +++ test/unit/http/test-http-select-basic.rb 2010-08-03 08:28:33 +0000 (1093246) @@ -760,6 +760,47 @@ EOF :output_columns => "_id,_key,real_name,hp"}) end + def test_xml_vector + table_create("Softwares", + :flags => Table::HASH_KEY, + :key_type => "ShortText") + column_create("Softwares", "tags", Column::VECTOR, "ShortText") + column_create("Softwares", "description", Column::SCALAR, "ShortText") + load("Softwares", + [[:_key, :tags, :description], + ["groonga", + ["full-text search", "C"], + "full-text search engine"], + ["Ruby", + ["language", "script", "fun"], + "An object oriented script language"]]) + + expected = <<EOF +<?xml version="1.0" encoding="utf-8"?> +<SEGMENTS> +<SEGMENT> +<RESULTPAGE> +<RESULTSET OFFSET="0" LIMIT="2" NHITS="2"> +<HIT NO="1"> +<FIELD NAME="_key">groonga</FIELD> +<FIELD NAME="tags">full-text search, C</FIELD> +<FIELD NAME="description">full-text search engine</FIELD> +</HIT> +<HIT NO="2"> +<FIELD NAME="_key">Ruby</FIELD> +<FIELD NAME="tags">language, script, fun</FIELD> +<FIELD NAME="description">An object oriented script language</FIELD> +</HIT> +</RESULTSET> +</RESULTPAGE> +</SEGMENT> +</SEGMENTS> +EOF + assert_select_xml(expected, + {:table => "Softwares", + :output_columns => "_key tags description"}) + end + def test_xml_with_offset create_users_table load_many_users Modified: test/unit/http/test-http-select-drilldown.rb (+6 -6) =================================================================== --- test/unit/http/test-http-select-drilldown.rb 2010-08-03 08:11:34 +0000 (dddfe07) +++ test/unit/http/test-http-select-drilldown.rb 2010-08-03 08:28:33 +0000 (610e072) @@ -235,7 +235,7 @@ module HTTPSelectDrilldownTests <FIELD NAME="place">razil.jp</FIELD> <FIELD NAME="place.name">ブラジル</FIELD> <FIELD NAME="title">groongaリリース(前編)</FIELD> -<FIELD NAME="person"></FIELD> +<FIELD NAME="person">グニャラくん</FIELD> <FIELD NAME="date">20091218.0</FIELD> </HIT> <HIT NO="2"> @@ -243,7 +243,7 @@ module HTTPSelectDrilldownTests <FIELD NAME="place">shinjuku</FIELD> <FIELD NAME="place.name">新宿</FIELD> <FIELD NAME="title">groongaリリース(後編)</FIELD> -<FIELD NAME="person"></FIELD> +<FIELD NAME="person">グニャラくん</FIELD> <FIELD NAME="date">20091218.0</FIELD> </HIT> <HIT NO="3"> @@ -251,7 +251,7 @@ module HTTPSelectDrilldownTests <FIELD NAME="place">razil.jp</FIELD> <FIELD NAME="place.name">ブラジル</FIELD> <FIELD NAME="title">groonga(ぐるんが)解説・パート1</FIELD> -<FIELD NAME="person"></FIELD> +<FIELD NAME="person">morita</FIELD> <FIELD NAME="date">20091218.0</FIELD> </HIT> <HIT NO="4"> @@ -259,7 +259,7 @@ module HTTPSelectDrilldownTests <FIELD NAME="place">shinjuku</FIELD> <FIELD NAME="place.name">新宿</FIELD> <FIELD NAME="title">groonga(ぐるんが)解説・パート2</FIELD> -<FIELD NAME="person"></FIELD> +<FIELD NAME="person">yu</FIELD> <FIELD NAME="date">20091219.0</FIELD> </HIT> <HIT NO="5"> @@ -267,7 +267,7 @@ module HTTPSelectDrilldownTests <FIELD NAME="place">shinjuku</FIELD> <FIELD NAME="place.name">新宿</FIELD> <FIELD NAME="title">groonga(ぐるんが)解説・パート3</FIELD> -<FIELD NAME="person"></FIELD> +<FIELD NAME="person">yu</FIELD> <FIELD NAME="date">20091220.0</FIELD> </HIT> <HIT NO="6"> @@ -275,7 +275,7 @@ module HTTPSelectDrilldownTests <FIELD NAME="place">shinjuku</FIELD> <FIELD NAME="place.name">新宿</FIELD> <FIELD NAME="title">groonga(ぐるんが)解説・パート4</FIELD> -<FIELD NAME="person"></FIELD> +<FIELD NAME="person">yu</FIELD> <FIELD NAME="date">20091220.0</FIELD> </HIT> </RESULTSET>