From 41b1f1f5f13ca2d9fa31baa0ef5d106196d56c11 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Sat, 4 Apr 2015 22:22:04 +0900 Subject: [PATCH] dump: add --dump_schema option You can disable dumping schema by "--dump_schema no". --- lib/proc.c | 82 ++++++++++++++----- .../index/with_reference_column.expected | 1 + .../column/reference/each_other.expected | 1 - .../schema/column/vector/with_weight.expected | 1 - .../suite/dump/schema/dump_schema_no.expected | 22 +++++ .../suite/dump/schema/dump_schema_no.test | 17 ++++ .../dump/schema/plugin/multiple.expected | 1 - .../suite/dump/schema/plugin/native.expected | 1 - .../suite/dump/schema/plugin/ruby.expected | 1 - .../double_array_trie/reference_key.expected | 1 - .../table/double_array_trie/type_key.expected | 1 - .../schema/table/hash/reference_key.expected | 1 - .../dump/schema/table/hash/type_key.expected | 1 - .../dump/schema/table/no_key/minimum.expected | 1 - .../patricia_trie/reference_key.expected | 1 - .../table/patricia_trie/type_key.expected | 1 - 16 files changed, 101 insertions(+), 33 deletions(-) create mode 100644 test/command/suite/dump/schema/dump_schema_no.expected create mode 100644 test/command/suite/dump/schema/dump_schema_no.test diff --git a/lib/proc.c b/lib/proc.c index 2c50c339a8..564bf3fcf6 100644 --- a/lib/proc.c +++ b/lib/proc.c @@ -2874,8 +2874,7 @@ reference_column_p(grn_ctx *ctx, grn_obj *column) static void dump_columns(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table, - grn_obj *pending_reference_columns, - grn_obj *pending_index_columns) + grn_obj *pending_reference_columns) { grn_hash *columns; columns = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, @@ -2892,7 +2891,7 @@ dump_columns(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table, grn_obj *column; if ((column = grn_ctx_at(ctx, *key))) { if (GRN_OBJ_INDEX_COLUMNP(column)) { - GRN_PTR_PUT(ctx, pending_index_columns, column); + /* do nothing */ } else if (reference_column_p(ctx, column)) { GRN_PTR_PUT(ctx, pending_reference_columns, column); } else { @@ -3123,8 +3122,7 @@ exit : static void dump_table(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table, - grn_obj *pending_reference_columns, - grn_obj *pending_index_columns) + grn_obj *pending_reference_columns) { grn_obj *domain = NULL, *range = NULL; grn_obj_flags default_flags = GRN_OBJ_PERSISTENT; @@ -3209,9 +3207,7 @@ dump_table(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table, grn_obj_unlink(ctx, domain); } - dump_columns(ctx, outbuf, table, - pending_reference_columns, - pending_index_columns); + dump_columns(ctx, outbuf, table, pending_reference_columns); } static void @@ -3240,7 +3236,7 @@ dump_pending_columns(grn_ctx *ctx, grn_obj *outbuf, grn_obj *pending_columns) } static void -dump_schema(grn_ctx *ctx, grn_obj *outbuf, grn_obj *pending_index_columns) +dump_schema(grn_ctx *ctx, grn_obj *outbuf) { grn_obj *db = ctx->impl->db; grn_table_cursor *cur; @@ -3263,9 +3259,7 @@ dump_schema(grn_ctx *ctx, grn_obj *outbuf, grn_obj *pending_index_columns) case GRN_TABLE_PAT_KEY: case GRN_TABLE_DAT_KEY: case GRN_TABLE_NO_KEY: - dump_table(ctx, outbuf, object, - &pending_reference_columns, - pending_index_columns); + dump_table(ctx, outbuf, object, &pending_reference_columns); break; default: break; @@ -3367,6 +3361,51 @@ dump_all_records(grn_ctx *ctx, grn_obj *outbuf) } } +static void +dump_indexes(grn_ctx *ctx, grn_obj *outbuf) +{ + grn_obj *db = ctx->impl->db; + grn_table_cursor *cursor; + grn_id id; + grn_bool is_first_index_column = GRN_TRUE; + + cursor = grn_table_cursor_open(ctx, db, NULL, 0, NULL, 0, 0, -1, + GRN_CURSOR_BY_ID); + if (!cursor) { + return; + } + + while ((id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL) { + grn_obj *object; + + object = grn_ctx_at(ctx, id); + if (!object) { + /* XXX: this clause is executed when MeCab tokenizer is enabled in + database but the groonga isn't supported MeCab. + We should return error mesage about it and error exit status + but it's too difficult for this architecture. :< */ + ERRCLR(ctx); + continue; + } + + if (object->header.type == GRN_COLUMN_INDEX) { + grn_obj *table; + grn_obj *column = object; + + if (is_first_index_column && GRN_TEXT_LEN(outbuf) > 0) { + GRN_TEXT_PUTC(ctx, outbuf, '\n'); + } + is_first_index_column = GRN_FALSE; + + table = grn_ctx_at(ctx, column->header.domain); + dump_column(ctx, outbuf, table, column); + grn_obj_unlink(ctx, table); + } + grn_obj_unlink(ctx, object); + } + grn_table_cursor_close(ctx, cursor); +} + static grn_bool bool_option_value(grn_obj *option, grn_bool default_value) { @@ -3397,20 +3436,21 @@ proc_dump(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) grn_obj *outbuf = ctx->impl->outbuf; grn_obj *tables = VAR(0); grn_obj *dump_plugins_raw = VAR(1); + grn_obj *dump_schema_raw = VAR(2); grn_bool is_dump_plugins; - grn_obj pending_index_columns; - - GRN_PTR_INIT(&pending_index_columns, GRN_OBJ_VECTOR, GRN_ID_NIL); + grn_bool is_dump_schema; grn_ctx_set_output_type(ctx, GRN_CONTENT_GROONGA_COMMAND_LIST); is_dump_plugins = bool_option_value(dump_plugins_raw, GRN_TRUE); + is_dump_schema = bool_option_value(dump_schema_raw, GRN_TRUE); + if (is_dump_plugins) { dump_plugins(ctx, outbuf); - grn_ctx_output_flush(ctx, 0); } - dump_schema(ctx, outbuf, &pending_index_columns); - grn_ctx_output_flush(ctx, 0); + if (is_dump_schema) { + dump_schema(ctx, outbuf); + } /* To update index columns correctly, we first create the whole schema, then load non-derivative records, while skipping records of index columns. That way, groonga will silently do the job of updating index columns for us. */ @@ -3420,8 +3460,7 @@ proc_dump(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) dump_all_records(ctx, outbuf); } - dump_pending_columns(ctx, outbuf, &pending_index_columns); - grn_obj_close(ctx, &pending_index_columns); + dump_indexes(ctx, outbuf); /* remove the last newline because another one will be added by the caller. maybe, the caller of proc functions currently doesn't consider the @@ -6772,7 +6811,8 @@ grn_db_init_builtin_query(grn_ctx *ctx) DEF_VAR(vars[0], "tables"); DEF_VAR(vars[1], "dump_plugins"); - DEF_COMMAND("dump", proc_dump, 2, vars); + DEF_VAR(vars[2], "dump_schema"); + DEF_COMMAND("dump", proc_dump, 3, vars); /* Deprecated. Use "plugin_register" instead. */ DEF_VAR(vars[0], "path"); diff --git a/test/command/suite/dump/schema/column/index/with_reference_column.expected b/test/command/suite/dump/schema/column/index/with_reference_column.expected index 489c3fe2ff..a92f51dd5f 100644 --- a/test/command/suite/dump/schema/column/index/with_reference_column.expected +++ b/test/command/suite/dump/schema/column/index/with_reference_column.expected @@ -22,4 +22,5 @@ column_create Users name COLUMN_SCALAR ShortText table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto column_create Users bookmark COLUMN_SCALAR Bookmarks + column_create Terms users_name_index COLUMN_INDEX|WITH_POSITION Users name diff --git a/test/command/suite/dump/schema/column/reference/each_other.expected b/test/command/suite/dump/schema/column/reference/each_other.expected index c3afc52951..654bae5095 100644 --- a/test/command/suite/dump/schema/column/reference/each_other.expected +++ b/test/command/suite/dump/schema/column/reference/each_other.expected @@ -19,4 +19,3 @@ column_create Users name COLUMN_SCALAR ShortText column_create Bookmarks user COLUMN_SCALAR Users column_create Users bookmark COLUMN_SCALAR Bookmarks - diff --git a/test/command/suite/dump/schema/column/vector/with_weight.expected b/test/command/suite/dump/schema/column/vector/with_weight.expected index 1cb2f06761..23149d72d0 100644 --- a/test/command/suite/dump/schema/column/vector/with_weight.expected +++ b/test/command/suite/dump/schema/column/vector/with_weight.expected @@ -5,4 +5,3 @@ column_create Bookmarks tags COLUMN_VECTOR|WITH_WEIGHT ShortText dump table_create Bookmarks TABLE_HASH_KEY ShortText column_create Bookmarks tags COLUMN_VECTOR|WITH_WEIGHT ShortText - diff --git a/test/command/suite/dump/schema/dump_schema_no.expected b/test/command/suite/dump/schema/dump_schema_no.expected new file mode 100644 index 0000000000..3272ede1d3 --- /dev/null +++ b/test/command/suite/dump/schema/dump_schema_no.expected @@ -0,0 +1,22 @@ +plugin_register token_filters/stop_word +[[0,0.0,0.0],true] +table_create Bookmarks TABLE_HASH_KEY ShortText +[[0,0.0,0.0],true] +column_create Bookmarks title COLUMN_SCALAR ShortText +[[0,0.0,0.0],true] +table_create Users TABLE_HASH_KEY ShortText +[[0,0.0,0.0],true] +column_create Users name COLUMN_SCALAR ShortText +[[0,0.0,0.0],true] +column_create Users bookmark COLUMN_SCALAR Bookmarks +[[0,0.0,0.0],true] +column_create Bookmarks user COLUMN_SCALAR Users +[[0,0.0,0.0],true] +table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto +[[0,0.0,0.0],true] +column_create Terms users_name_index COLUMN_INDEX|WITH_POSITION Users name +[[0,0.0,0.0],true] +dump --dump_schema no +plugin_register token_filters/stop_word + +column_create Terms users_name_index COLUMN_INDEX|WITH_POSITION Users name diff --git a/test/command/suite/dump/schema/dump_schema_no.test b/test/command/suite/dump/schema/dump_schema_no.test new file mode 100644 index 0000000000..4e2f44f7ae --- /dev/null +++ b/test/command/suite/dump/schema/dump_schema_no.test @@ -0,0 +1,17 @@ +plugin_register token_filters/stop_word + +table_create Bookmarks TABLE_HASH_KEY ShortText +column_create Bookmarks title COLUMN_SCALAR ShortText + +table_create Users TABLE_HASH_KEY ShortText +column_create Users name COLUMN_SCALAR ShortText + +column_create Users bookmark COLUMN_SCALAR Bookmarks +column_create Bookmarks user COLUMN_SCALAR Users + +table_create Terms TABLE_PAT_KEY ShortText \ + --default_tokenizer TokenBigram \ + --normalizer NormalizerAuto +column_create Terms users_name_index COLUMN_INDEX|WITH_POSITION Users name + +dump --dump_schema no diff --git a/test/command/suite/dump/schema/plugin/multiple.expected b/test/command/suite/dump/schema/plugin/multiple.expected index 9bb11f0926..d4934485d0 100644 --- a/test/command/suite/dump/schema/plugin/multiple.expected +++ b/test/command/suite/dump/schema/plugin/multiple.expected @@ -5,4 +5,3 @@ plugin_register query_expanders/tsv dump plugin_register token_filters/stop_word plugin_register query_expanders/tsv - diff --git a/test/command/suite/dump/schema/plugin/native.expected b/test/command/suite/dump/schema/plugin/native.expected index 31cb23e327..0f4e362d4c 100644 --- a/test/command/suite/dump/schema/plugin/native.expected +++ b/test/command/suite/dump/schema/plugin/native.expected @@ -2,4 +2,3 @@ plugin_register token_filters/stop_word [[0,0.0,0.0],true] dump plugin_register token_filters/stop_word - diff --git a/test/command/suite/dump/schema/plugin/ruby.expected b/test/command/suite/dump/schema/plugin/ruby.expected index e3da4680fc..3fb1b1f192 100644 --- a/test/command/suite/dump/schema/plugin/ruby.expected +++ b/test/command/suite/dump/schema/plugin/ruby.expected @@ -2,4 +2,3 @@ plugin_register sharding [[0,0.0,0.0],true] dump plugin_register sharding - diff --git a/test/command/suite/dump/schema/table/double_array_trie/reference_key.expected b/test/command/suite/dump/schema/table/double_array_trie/reference_key.expected index 0ab9a1d651..b13b3840bd 100644 --- a/test/command/suite/dump/schema/table/double_array_trie/reference_key.expected +++ b/test/command/suite/dump/schema/table/double_array_trie/reference_key.expected @@ -6,4 +6,3 @@ dump table_create Names TABLE_PAT_KEY ShortText table_create Users TABLE_DAT_KEY Names - diff --git a/test/command/suite/dump/schema/table/double_array_trie/type_key.expected b/test/command/suite/dump/schema/table/double_array_trie/type_key.expected index a460876db4..ebbc2062dd 100644 --- a/test/command/suite/dump/schema/table/double_array_trie/type_key.expected +++ b/test/command/suite/dump/schema/table/double_array_trie/type_key.expected @@ -2,4 +2,3 @@ table_create Users TABLE_DAT_KEY ShortText [[0,0.0,0.0],true] dump table_create Users TABLE_DAT_KEY ShortText - diff --git a/test/command/suite/dump/schema/table/hash/reference_key.expected b/test/command/suite/dump/schema/table/hash/reference_key.expected index a3a5079941..c582c269cc 100644 --- a/test/command/suite/dump/schema/table/hash/reference_key.expected +++ b/test/command/suite/dump/schema/table/hash/reference_key.expected @@ -6,4 +6,3 @@ dump table_create Names TABLE_PAT_KEY ShortText table_create Users TABLE_HASH_KEY Names - diff --git a/test/command/suite/dump/schema/table/hash/type_key.expected b/test/command/suite/dump/schema/table/hash/type_key.expected index 66d84b3425..a8e86f03ff 100644 --- a/test/command/suite/dump/schema/table/hash/type_key.expected +++ b/test/command/suite/dump/schema/table/hash/type_key.expected @@ -2,4 +2,3 @@ table_create Users TABLE_HASH_KEY ShortText [[0,0.0,0.0],true] dump table_create Users TABLE_HASH_KEY ShortText - diff --git a/test/command/suite/dump/schema/table/no_key/minimum.expected b/test/command/suite/dump/schema/table/no_key/minimum.expected index dd94f11768..ecbf5f7bc4 100644 --- a/test/command/suite/dump/schema/table/no_key/minimum.expected +++ b/test/command/suite/dump/schema/table/no_key/minimum.expected @@ -2,4 +2,3 @@ table_create Users TABLE_NO_KEY [[0,0.0,0.0],true] dump table_create Users TABLE_NO_KEY - diff --git a/test/command/suite/dump/schema/table/patricia_trie/reference_key.expected b/test/command/suite/dump/schema/table/patricia_trie/reference_key.expected index c98aeb69bb..6513770881 100644 --- a/test/command/suite/dump/schema/table/patricia_trie/reference_key.expected +++ b/test/command/suite/dump/schema/table/patricia_trie/reference_key.expected @@ -6,4 +6,3 @@ dump table_create Names TABLE_PAT_KEY ShortText table_create Users TABLE_PAT_KEY Names - diff --git a/test/command/suite/dump/schema/table/patricia_trie/type_key.expected b/test/command/suite/dump/schema/table/patricia_trie/type_key.expected index ab2d6a3478..24bd23986d 100644 --- a/test/command/suite/dump/schema/table/patricia_trie/type_key.expected +++ b/test/command/suite/dump/schema/table/patricia_trie/type_key.expected @@ -2,4 +2,3 @@ table_create Users TABLE_PAT_KEY ShortText [[0,0.0,0.0],true] dump table_create Users TABLE_PAT_KEY ShortText -