null+****@clear*****
null+****@clear*****
2010年 7月 7日 (水) 18:41:05 JST
Kouhei Sutou 2010-07-07 09:41:05 +0000 (Wed, 07 Jul 2010) New Revision: cede28d5732721030dc1c61cdce087c0cdabbbc4 Log: add ii inspector. Modified files: lib/util.c test/unit/core/test-inspect.c Modified: lib/util.c (+84 -4) =================================================================== --- lib/util.c 2010-07-07 05:35:11 +0000 (209e4f5) +++ lib/util.c 2010-07-07 09:41:05 +0000 (39590f9) @@ -62,12 +62,88 @@ exit: return ctx->rc; } -grn_rc +static grn_rc grn_accessor_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) { return grn_column_name_(ctx, obj, buf); } +static grn_rc +grn_ii_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) +{ + grn_obj sources; + int name_size, i, n, have_flags = 0; + grn_id range_id, *source_ids; + + GRN_TEXT_PUTS(ctx, buf, "#<column:index "); + name_size = grn_obj_name(ctx, obj, NULL, 0); + if (name_size) { + grn_bulk_space(ctx, buf, name_size); + grn_obj_name(ctx, obj, GRN_BULK_CURR(buf) - name_size, name_size); + } + + range_id = grn_obj_get_range(ctx, obj); + if (range_id) { + grn_obj *range = grn_ctx_at(ctx, range_id); + if (range) { + GRN_TEXT_PUTS(ctx, buf, " range:"); + name_size = grn_obj_name(ctx, range, NULL, 0); + if (name_size) { + grn_bulk_space(ctx, buf, name_size); + grn_obj_name(ctx, range, GRN_BULK_CURR(buf) - name_size, name_size); + } + grn_obj_unlink(ctx, range); + } + } + + GRN_TEXT_INIT(&sources, 0); + grn_obj_get_info(ctx, obj, GRN_INFO_SOURCE, &sources); + source_ids = (grn_id *)GRN_BULK_HEAD(&sources); + n = GRN_BULK_VSIZE(&sources) / sizeof(grn_id); + GRN_TEXT_PUTS(ctx, buf, " sources:["); + for (i = 0; i < n; i++) { + grn_id source_id; + grn_obj *source; + if (i) { GRN_TEXT_PUTS(ctx, buf, ", "); } + source_id = source_ids[i]; + source = grn_ctx_at(ctx, source_id); + if (source) { + name_size = grn_obj_name(ctx, source, NULL, 0); + if (name_size) { + grn_bulk_space(ctx, buf, name_size); + grn_obj_name(ctx, source, GRN_BULK_CURR(buf) - name_size, name_size); + } + } else { + grn_text_lltoa(ctx, buf, source_id); + } + } + GRN_TEXT_PUTS(ctx, buf, "]"); + GRN_OBJ_FIN(ctx, &sources); + + GRN_TEXT_PUTS(ctx, buf, " flags:"); + if (obj->header.flags & GRN_OBJ_WITH_SECTION) { + GRN_TEXT_PUTS(ctx, buf, "SECTION"); + have_flags = 1; + } + if (obj->header.flags & GRN_OBJ_WITH_WEIGHT) { + if (have_flags) { GRN_TEXT_PUTS(ctx, buf, "|"); } + GRN_TEXT_PUTS(ctx, buf, "WEIGHT"); + have_flags = 1; + } + if (obj->header.flags & GRN_OBJ_WITH_POSITION) { + if (have_flags) { GRN_TEXT_PUTS(ctx, buf, "|"); } + GRN_TEXT_PUTS(ctx, buf, "POSITION"); + have_flags = 1; + } + if (!have_flags) { + GRN_TEXT_PUTS(ctx, buf, "NONE"); + } + + GRN_TEXT_PUTS(ctx, buf, ">"); + + return GRN_SUCCESS; +} + grn_obj * grn_inspect(grn_ctx *ctx, grn_obj *buffer, grn_obj *obj) { @@ -81,13 +157,17 @@ grn_inspect(grn_ctx *ctx, grn_obj *buffer, grn_obj *obj) } switch (obj->header.type) { - case GRN_EXPR: + case GRN_EXPR : grn_expr_inspect(ctx, buffer, obj); return buffer; - case GRN_ACCESSOR: - case GRN_ACCESSOR_VIEW: + case GRN_ACCESSOR : + case GRN_ACCESSOR_VIEW : grn_accessor_inspect(ctx, buffer, obj); return buffer; + case GRN_COLUMN_INDEX : + grn_ii_inspect(ctx, buffer, obj); + default: + break; } grn_text_otoj(ctx, buffer, obj, NULL); Modified: test/unit/core/test-inspect.c (+21 -0) =================================================================== --- test/unit/core/test-inspect.c 2010-07-07 05:35:11 +0000 (0b25659) +++ test/unit/core/test-inspect.c 2010-07-07 09:41:05 +0000 (4b1a916) @@ -59,6 +59,7 @@ void data_accessor_column_name(void); void test_accessor_column_name(gconstpointer data); void data_accessor_dynamic_pseudo_column_name(void); void test_accessor_dynamic_pseudo_column_name(gconstpointer data); +void test_column_index(void); static gchar *tmp_directory; @@ -577,3 +578,23 @@ test_accessor_dynamic_pseudo_column_name(gconstpointer data) inspected = grn_inspect(context, NULL, accessor); cut_assert_equal_string(accessor_name, inspected_string()); } + +void +test_column_index(void) +{ + grn_obj *column; + + assert_send_command("table_create Sites TABLE_PAT_KEY ShortText"); + assert_send_command("table_create Terms TABLE_PAT_KEY ShortText"); + assert_send_command("column_create Terms Sites_key COLUMN_INDEX Sites _key"); + + column = get_object("Terms.Sites_key"); + inspected = grn_inspect(context, NULL, column); + cut_assert_equal_string("#<column:index " + "Terms.Sites_key " + "range:Sites " + "sources:[Sites] " + "flags:NONE" + ">", + inspected_string()); +}