null+****@clear*****
null+****@clear*****
2010年 7月 15日 (木) 16:35:48 JST
Kouhei Sutou 2010-07-15 07:35:48 +0000 (Thu, 15 Jul 2010) New Revision: 28010081ef37e4912885275b17aec0217d1b12a7 Log: add more details to table inspectioin. Modified files: lib/util.c test/unit/core/test-inspect.c Modified: lib/util.c (+151 -24) =================================================================== --- lib/util.c 2010-07-15 05:04:54 +0000 (26c3237) +++ lib/util.c 2010-07-15 07:35:48 +0000 (226db73) @@ -62,6 +62,22 @@ grn_normalize_offset_and_limit(grn_ctx *ctx, int size, int *p_offset, int *p_lim } static grn_rc +grn_name_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) +{ + int name_size; + + 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); + } else { + GRN_TEXT_PUTS(ctx, buf, "(nil)"); + } + + return GRN_SUCCESS; +} + +static grn_rc grn_accessor_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) { return grn_column_name_(ctx, obj, buf); @@ -70,15 +86,10 @@ grn_accessor_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) static grn_rc grn_type_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) { - int name_size; grn_id range_id; GRN_TEXT_PUTS(ctx, buf, "#<type "); - 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); - } + grn_name_inspect(ctx, buf, obj); range_id = grn_obj_get_range(ctx, obj); GRN_TEXT_PUTS(ctx, buf, " size:"); @@ -113,25 +124,16 @@ grn_type_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) static grn_rc grn_column_inspect_common(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) { - int name_size; grn_id range_id; - 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); - } + grn_name_inspect(ctx, buf, obj); range_id = grn_obj_get_range(ctx, obj); if (range_id) { grn_obj *range = grn_ctx_at(ctx, range_id); GRN_TEXT_PUTS(ctx, buf, " range:"); if (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_name_inspect(ctx, buf, range); grn_obj_unlink(ctx, range); } else { grn_text_lltoa(ctx, buf, range_id); @@ -197,7 +199,7 @@ 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; + int i, n, have_flags = 0; grn_id *source_ids; GRN_TEXT_PUTS(ctx, buf, "#<column:index "); @@ -215,11 +217,7 @@ grn_ii_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) 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); - } + grn_name_inspect(ctx, buf, source); } else { grn_text_lltoa(ctx, buf, source_id); } @@ -251,6 +249,130 @@ grn_ii_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) return GRN_SUCCESS; } +static grn_rc +grn_table_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) +{ + grn_hash *cols; + grn_id range_id; + grn_obj *range; + + GRN_TEXT_PUTS(ctx, buf, "#<table:"); + switch (obj->header.type) { + case GRN_TABLE_HASH_KEY: + GRN_TEXT_PUTS(ctx, buf, "hash"); + break; + case GRN_TABLE_PAT_KEY: + GRN_TEXT_PUTS(ctx, buf, "pat"); + break; + case GRN_TABLE_NO_KEY: + GRN_TEXT_PUTS(ctx, buf, "no_key"); + break; + } + GRN_TEXT_PUTS(ctx, buf, " "); + + grn_name_inspect(ctx, buf, obj); + + if (obj->header.type != GRN_TABLE_NO_KEY) { + grn_obj *domain; + grn_id domain_id; + GRN_TEXT_PUTS(ctx, buf, " key:"); + domain_id = obj->header.domain; + domain = grn_ctx_at(ctx, domain_id); + if (domain) { + grn_name_inspect(ctx, buf, domain); + grn_obj_unlink(ctx, domain); + } else if (domain_id) { + grn_text_lltoa(ctx, buf, domain_id); + } else { + GRN_TEXT_PUTS(ctx, buf, "(nil)"); + } + } + + GRN_TEXT_PUTS(ctx, buf, " value:"); + range_id = grn_obj_get_range(ctx, obj); + range = grn_ctx_at(ctx, range_id); + if (range) { + grn_name_inspect(ctx, buf, range); + } else if (range_id) { + grn_text_lltoa(ctx, buf, range_id); + } else { + GRN_TEXT_PUTS(ctx, buf, "(nil)"); + } + + GRN_TEXT_PUTS(ctx, buf, " size:"); + grn_text_lltoa(ctx, buf, grn_table_size(ctx, obj)); + + GRN_TEXT_PUTS(ctx, buf, " columns:["); + if ((cols = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, + GRN_OBJ_TABLE_HASH_KEY|GRN_HASH_TINY))) { + if (grn_table_columns(ctx, obj, "", 0, (grn_obj *)cols)) { + int i = 0; + grn_id *key; + GRN_HASH_EACH(ctx, cols, id, &key, NULL, NULL, { + grn_obj *col = grn_ctx_at(ctx, *key); + if (col) { + if (i++ > 0) { GRN_TEXT_PUTS(ctx, buf, ", "); } + grn_column_name_(ctx, col, buf); + } + }); + } + grn_hash_close(ctx, cols); + } + GRN_TEXT_PUTS(ctx, buf, "]"); + + if (obj->header.type == GRN_TABLE_NO_KEY) { + grn_table_cursor *tc; + GRN_TEXT_PUTS(ctx, buf, " ids:["); + tc = grn_table_cursor_open(ctx, obj, NULL, 0, NULL, 0, + 0, -1, GRN_CURSOR_ASCENDING); + if (tc) { + int i = 0; + grn_id id; + while ((id = grn_table_cursor_next(ctx, tc))) { + if (i++ > 0) { GRN_TEXT_PUTS(ctx, buf, ", "); } + grn_text_lltoa(ctx, buf, id); + } + grn_table_cursor_close(ctx, tc); + } + GRN_TEXT_PUTS(ctx, buf, "]"); + } else { + grn_table_cursor *tc; + grn_obj *default_tokenizer; + + GRN_TEXT_PUTS(ctx, buf, " default_tokenizer:"); + default_tokenizer = grn_obj_get_info(ctx, obj, + GRN_INFO_DEFAULT_TOKENIZER, NULL); + if (default_tokenizer) { + grn_name_inspect(ctx, buf, default_tokenizer); + } else { + GRN_TEXT_PUTS(ctx, buf, "(nil)"); + } + + GRN_TEXT_PUTS(ctx, buf, " keys:["); + tc = grn_table_cursor_open(ctx, obj, NULL, 0, NULL, 0, + 0, -1, GRN_CURSOR_ASCENDING); + if (tc) { + int i = 0; + grn_id id; + grn_obj key; + GRN_OBJ_INIT(&key, GRN_BULK, 0, obj->header.domain); + while ((id = grn_table_cursor_next(ctx, tc))) { + if (i++ > 0) { GRN_TEXT_PUTS(ctx, buf, ", "); } + grn_table_get_key2(ctx, obj, id, &key); + grn_inspect(ctx, buf, &key); + GRN_BULK_REWIND(&key); + } + GRN_OBJ_FIN(ctx, &key); + grn_table_cursor_close(ctx, tc); + } + GRN_TEXT_PUTS(ctx, buf, "]"); + } + + GRN_TEXT_PUTS(ctx, buf, ">"); + + return GRN_SUCCESS; +} + grn_obj * grn_inspect(grn_ctx *ctx, grn_obj *buffer, grn_obj *obj) { @@ -283,7 +405,12 @@ grn_inspect(grn_ctx *ctx, grn_obj *buffer, grn_obj *obj) case GRN_COLUMN_INDEX : grn_ii_inspect(ctx, buffer, obj); return buffer; - default: + case GRN_TABLE_HASH_KEY : + case GRN_TABLE_PAT_KEY : + case GRN_TABLE_NO_KEY : + grn_table_inspect(ctx, buffer, obj); + return buffer; + default : break; } Modified: test/unit/core/test-inspect.c (+57 -7) =================================================================== --- test/unit/core/test-inspect.c 2010-07-15 05:04:54 +0000 (6afe64b) +++ test/unit/core/test-inspect.c 2010-07-15 07:35:48 +0000 (87b7a5b) @@ -63,6 +63,7 @@ void test_column_fix_size(void); void test_column_var_size(void); void test_column_index(void); void test_type(void); +void test_table_hash(void); static gchar *tmp_directory; @@ -363,20 +364,33 @@ test_array_empty(void) { assert_send_command("table_create Sites TABLE_NO_KEY"); inspected = grn_inspect(context, NULL, get("Sites")); - cut_assert_equal_string("[]", inspected_string()); + cut_assert_equal_string("#<table:no_key " + "Sites " + "value:(nil) " + "size:0 " + "columns:[] " + "ids:[]" + ">", + inspected_string()); } void test_array_with_records(void) { - cut_omit("array with record isn't supported yet."); assert_send_command("table_create Sites TABLE_NO_KEY"); assert_send_command("column_create Sites name COLUMN_SCALAR Text"); assert_send_command("load " "'[[\"name\"],[\"groonga.org\"],[\"razil.jp\"]]' " "Sites"); inspected = grn_inspect(context, NULL, get("Sites")); - cut_assert_equal_string("[1, 2]", inspected_string()); + cut_assert_equal_string("#<table:no_key " + "Sites " + "value:(nil) " + "size:2 " + "columns:[name] " + "ids:[1, 2]" + ">", + inspected_string()); } void @@ -384,7 +398,16 @@ test_hash_empty(void) { assert_send_command("table_create Sites TABLE_HASH_KEY ShortText"); inspected = grn_inspect(context, NULL, get("Sites")); - cut_assert_equal_string("[]", inspected_string()); + cut_assert_equal_string("#<table:hash " + "Sites " + "key:ShortText " + "value:(nil) " + "size:0 " + "columns:[] " + "default_tokenizer:(nil) " + "keys:[]" + ">", + inspected_string()); } void @@ -400,7 +423,16 @@ test_hash_with_records(void) "]' " "Sites"); inspected = grn_inspect(context, NULL, get("Sites")); - cut_assert_equal_string("[\"groonga.org\",\"razil.jp\"]", inspected_string()); + cut_assert_equal_string("#<table:hash " + "Sites " + "key:ShortText " + "value:(nil) " + "size:2 " + "columns:[name] " + "default_tokenizer:(nil) " + "keys:[\"groonga.org\", \"razil.jp\"]" + ">", + inspected_string()); } void @@ -408,7 +440,16 @@ test_patricia_trie_empty(void) { assert_send_command("table_create Sites TABLE_PAT_KEY ShortText"); inspected = grn_inspect(context, NULL, get("Sites")); - cut_assert_equal_string("[]", inspected_string()); + cut_assert_equal_string("#<table:pat " + "Sites " + "key:ShortText " + "value:(nil) " + "size:0 " + "columns:[] " + "default_tokenizer:(nil) " + "keys:[]" + ">", + inspected_string()); } void @@ -424,7 +465,16 @@ test_patricia_trie_with_records(void) "]' " "Sites"); inspected = grn_inspect(context, NULL, get("Sites")); - cut_assert_equal_string("[\"groonga.org\",\"razil.jp\"]", inspected_string()); + cut_assert_equal_string("#<table:pat " + "Sites " + "key:ShortText " + "value:(nil) " + "size:2 " + "columns:[name] " + "default_tokenizer:(nil) " + "keys:[\"groonga.org\", \"razil.jp\"]" + ">", + inspected_string()); } void