Skip to content

Commit

Permalink
dump: add --dump_schema option
Browse files Browse the repository at this point in the history
You can disable dumping schema by "--dump_schema no".
  • Loading branch information
kou committed Apr 4, 2015
1 parent 307dc1d commit 41b1f1f
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 33 deletions.
82 changes: 61 additions & 21 deletions lib/proc.c
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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. */
Expand All @@ -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
Expand Down Expand Up @@ -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");
Expand Down
Expand Up @@ -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
Expand Up @@ -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

Expand Up @@ -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

22 changes: 22 additions & 0 deletions 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
17 changes: 17 additions & 0 deletions 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
1 change: 0 additions & 1 deletion test/command/suite/dump/schema/plugin/multiple.expected
Expand Up @@ -5,4 +5,3 @@ plugin_register query_expanders/tsv
dump
plugin_register token_filters/stop_word
plugin_register query_expanders/tsv

1 change: 0 additions & 1 deletion test/command/suite/dump/schema/plugin/native.expected
Expand Up @@ -2,4 +2,3 @@ plugin_register token_filters/stop_word
[[0,0.0,0.0],true]
dump
plugin_register token_filters/stop_word

1 change: 0 additions & 1 deletion test/command/suite/dump/schema/plugin/ruby.expected
Expand Up @@ -2,4 +2,3 @@ plugin_register sharding
[[0,0.0,0.0],true]
dump
plugin_register sharding

Expand Up @@ -6,4 +6,3 @@ dump
table_create Names TABLE_PAT_KEY ShortText

table_create Users TABLE_DAT_KEY Names

Expand Up @@ -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

Expand Up @@ -6,4 +6,3 @@ dump
table_create Names TABLE_PAT_KEY ShortText

table_create Users TABLE_HASH_KEY Names

Expand Up @@ -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

Expand Up @@ -2,4 +2,3 @@ table_create Users TABLE_NO_KEY
[[0,0.0,0.0],true]
dump
table_create Users TABLE_NO_KEY

Expand Up @@ -6,4 +6,3 @@ dump
table_create Names TABLE_PAT_KEY ShortText

table_create Users TABLE_PAT_KEY Names

Expand Up @@ -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

0 comments on commit 41b1f1f

Please sign in to comment.