Kouhei Sutou 2018-12-26 10:21:15 +0900 (Wed, 26 Dec 2018) Revision: 7a8764849f2dbadbc92d9dad190dfcdbe354a24b https://github.com/groonga/groonga/commit/7a8764849f2dbadbc92d9dad190dfcdbe354a24b Message: truncate: ensure truncating all columns that refers the target table Added files: test/command/suite/truncate/table/referenced_dat.expected test/command/suite/truncate/table/referenced_dat.test test/command/suite/truncate/table/referenced_hash.expected test/command/suite/truncate/table/referenced_hash.test test/command/suite/truncate/table/referenced_pat.expected test/command/suite/truncate/table/referenced_pat.test Modified files: lib/db.c Modified: lib/db.c (+50 -18) =================================================================== --- lib/db.c 2018-12-25 17:03:47 +0900 (020c7ddc6) +++ lib/db.c 2018-12-26 10:21:15 +0900 (c58be7955) @@ -2357,6 +2357,51 @@ exit : GRN_API_RETURN(rc); } +static grn_rc +grn_table_truncate_reference_columns(grn_ctx *ctx, grn_obj *table) +{ + grn_bool is_close_opened_object_mode; + grn_table_cursor *cursor; + grn_id table_id; + grn_id id; + + is_close_opened_object_mode = (grn_thread_get_limit() == 1); + + cursor = grn_table_cursor_open(ctx, grn_ctx_db(ctx), + NULL, 0, NULL, 0, + 0, -1, + GRN_CURSOR_BY_ID); + if (!cursor) { + return ctx->rc; + } + + table_id = grn_obj_id(ctx, table); + while ((id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL) { + grn_obj *object; + + if (is_close_opened_object_mode) { + grn_ctx_push_temporary_open_space(ctx); + } + + object = grn_ctx_at(ctx, id); + if (grn_obj_is_column(ctx, object) && + grn_obj_get_range(ctx, object) == table_id) { + grn_column_truncate(ctx, object); + } + + if (is_close_opened_object_mode) { + grn_ctx_pop_temporary_open_space(ctx); + } + + if (ctx->rc != GRN_SUCCESS) { + break; + } + } + grn_table_cursor_close(ctx, cursor); + + return ctx->rc; +} + grn_rc grn_table_truncate(grn_ctx *ctx, grn_obj *table) { @@ -2386,33 +2431,20 @@ grn_table_truncate(grn_ctx *ctx, grn_obj *table) grn_table_get_normalizer_string(ctx, table, &normalizer); GRN_TEXT_INIT(&token_filters, 0); grn_table_get_token_filters_string(ctx, table, &token_filters); + + rc = grn_table_truncate_reference_columns(ctx, table); + if (rc != GRN_SUCCESS) { + goto exit; + } } switch (table->header.type) { case GRN_TABLE_PAT_KEY : - for (hooks = DB_OBJ(table)->hooks[GRN_HOOK_INSERT]; hooks; hooks = hooks->next) { - grn_obj_default_set_value_hook_data *data = (void *)GRN_NEXT_ADDR(hooks); - grn_obj *target = grn_ctx_at(ctx, data->target); - if (target->header.type != GRN_COLUMN_INDEX) { continue; } - if ((rc = grn_ii_truncate(ctx, (grn_ii *)target))) { goto exit; } - } rc = grn_pat_truncate(ctx, (grn_pat *)table); break; case GRN_TABLE_DAT_KEY : - for (hooks = DB_OBJ(table)->hooks[GRN_HOOK_INSERT]; hooks; hooks = hooks->next) { - grn_obj_default_set_value_hook_data *data = (void *)GRN_NEXT_ADDR(hooks); - grn_obj *target = grn_ctx_at(ctx, data->target); - if (target->header.type != GRN_COLUMN_INDEX) { continue; } - if ((rc = grn_ii_truncate(ctx, (grn_ii *)target))) { goto exit; } - } rc = grn_dat_truncate(ctx, (grn_dat *)table); break; case GRN_TABLE_HASH_KEY : - for (hooks = DB_OBJ(table)->hooks[GRN_HOOK_INSERT]; hooks; hooks = hooks->next) { - grn_obj_default_set_value_hook_data *data = (void *)GRN_NEXT_ADDR(hooks); - grn_obj *target = grn_ctx_at(ctx, data->target); - if (target->header.type != GRN_COLUMN_INDEX) { continue; } - if ((rc = grn_ii_truncate(ctx, (grn_ii *)target))) { goto exit; } - } rc = grn_hash_truncate(ctx, (grn_hash *)table); break; case GRN_TABLE_NO_KEY : Added: test/command/suite/truncate/table/referenced_dat.expected (+91 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/truncate/table/referenced_dat.expected 2018-12-26 10:21:15 +0900 (4b123bb82) @@ -0,0 +1,91 @@ +table_create Tags TABLE_DAT_KEY ShortText +[[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 tags_key COLUMN_INDEX|WITH_POSITION Tags _key +[[0,0.0,0.0],true] +table_create Memos TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Memos tags COLUMN_VECTOR Tags +[[0,0.0,0.0],true] +load --table Memos +[ +{"tags": ["Great"]} +] +[[0,0.0,0.0],1] +select Tags --match_columns '_key' --query 'Great' +[[0,0.0,0.0],[[[1],[["_id","UInt32"],["_key","ShortText"]],[1,"Great"]]]] +select Memos --output_columns tags +[[0,0.0,0.0],[[[1],[["tags","Tags"]],[["Great"]]]]] +select Terms +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "ShortText" + ], + [ + "tags_key", + "UInt32" + ] + ], + [ + 1, + "great", + 1 + ] + ] + ] +] +truncate Tags +[[0,0.0,0.0],true] +select Memos --output_columns tags +[[0,0.0,0.0],[[[1],[["tags","Tags"]],[[]]]]] +select Terms +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "ShortText" + ], + [ + "tags_key", + "UInt32" + ] + ], + [ + 1, + "great", + 0 + ] + ] + ] +] Added: test/command/suite/truncate/table/referenced_dat.test (+25 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/truncate/table/referenced_dat.test 2018-12-26 10:21:15 +0900 (4320cd616) @@ -0,0 +1,25 @@ +table_create Tags TABLE_DAT_KEY ShortText + +table_create Terms TABLE_PAT_KEY ShortText \ + --default_tokenizer TokenBigram \ + --normalizer NormalizerAuto +column_create Terms tags_key COLUMN_INDEX|WITH_POSITION Tags _key + +table_create Memos TABLE_NO_KEY +column_create Memos tags COLUMN_VECTOR Tags +load --table Memos +[ +{"tags": ["Great"]} +] + +select Tags \ + --match_columns '_key' \ + --query 'Great' + +select Memos --output_columns tags +select Terms + +truncate Tags + +select Memos --output_columns tags +select Terms Added: test/command/suite/truncate/table/referenced_hash.expected (+91 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/truncate/table/referenced_hash.expected 2018-12-26 10:21:15 +0900 (aa8dee3e1) @@ -0,0 +1,91 @@ +table_create Tags TABLE_HASH_KEY ShortText +[[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 tags_key COLUMN_INDEX|WITH_POSITION Tags _key +[[0,0.0,0.0],true] +table_create Memos TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Memos tags COLUMN_VECTOR Tags +[[0,0.0,0.0],true] +load --table Memos +[ +{"tags": ["Great"]} +] +[[0,0.0,0.0],1] +select Tags --match_columns '_key' --query 'Great' +[[0,0.0,0.0],[[[1],[["_id","UInt32"],["_key","ShortText"]],[1,"Great"]]]] +select Memos --output_columns tags +[[0,0.0,0.0],[[[1],[["tags","Tags"]],[["Great"]]]]] +select Terms +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "ShortText" + ], + [ + "tags_key", + "UInt32" + ] + ], + [ + 1, + "great", + 1 + ] + ] + ] +] +truncate Tags +[[0,0.0,0.0],true] +select Memos --output_columns tags +[[0,0.0,0.0],[[[1],[["tags","Tags"]],[[]]]]] +select Terms +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "ShortText" + ], + [ + "tags_key", + "UInt32" + ] + ], + [ + 1, + "great", + 0 + ] + ] + ] +] Added: test/command/suite/truncate/table/referenced_hash.test (+25 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/truncate/table/referenced_hash.test 2018-12-26 10:21:15 +0900 (a4b2b8211) @@ -0,0 +1,25 @@ +table_create Tags TABLE_HASH_KEY ShortText + +table_create Terms TABLE_PAT_KEY ShortText \ + --default_tokenizer TokenBigram \ + --normalizer NormalizerAuto +column_create Terms tags_key COLUMN_INDEX|WITH_POSITION Tags _key + +table_create Memos TABLE_NO_KEY +column_create Memos tags COLUMN_VECTOR Tags +load --table Memos +[ +{"tags": ["Great"]} +] + +select Tags \ + --match_columns '_key' \ + --query 'Great' + +select Memos --output_columns tags +select Terms + +truncate Tags + +select Memos --output_columns tags +select Terms Added: test/command/suite/truncate/table/referenced_pat.expected (+91 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/truncate/table/referenced_pat.expected 2018-12-26 10:21:15 +0900 (a2b7df969) @@ -0,0 +1,91 @@ +table_create Tags TABLE_PAT_KEY ShortText +[[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 tags_key COLUMN_INDEX|WITH_POSITION Tags _key +[[0,0.0,0.0],true] +table_create Memos TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Memos tags COLUMN_VECTOR Tags +[[0,0.0,0.0],true] +load --table Memos +[ +{"tags": ["Great"]} +] +[[0,0.0,0.0],1] +select Tags --match_columns '_key' --query 'Great' +[[0,0.0,0.0],[[[1],[["_id","UInt32"],["_key","ShortText"]],[1,"Great"]]]] +select Memos --output_columns tags +[[0,0.0,0.0],[[[1],[["tags","Tags"]],[["Great"]]]]] +select Terms +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "ShortText" + ], + [ + "tags_key", + "UInt32" + ] + ], + [ + 1, + "great", + 1 + ] + ] + ] +] +truncate Tags +[[0,0.0,0.0],true] +select Memos --output_columns tags +[[0,0.0,0.0],[[[1],[["tags","Tags"]],[[]]]]] +select Terms +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "ShortText" + ], + [ + "tags_key", + "UInt32" + ] + ], + [ + 1, + "great", + 0 + ] + ] + ] +] Added: test/command/suite/truncate/table/referenced_pat.test (+25 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/truncate/table/referenced_pat.test 2018-12-26 10:21:15 +0900 (208544fe9) @@ -0,0 +1,25 @@ +table_create Tags TABLE_PAT_KEY ShortText + +table_create Terms TABLE_PAT_KEY ShortText \ + --default_tokenizer TokenBigram \ + --normalizer NormalizerAuto +column_create Terms tags_key COLUMN_INDEX|WITH_POSITION Tags _key + +table_create Memos TABLE_NO_KEY +column_create Memos tags COLUMN_VECTOR Tags +load --table Memos +[ +{"tags": ["Great"]} +] + +select Tags \ + --match_columns '_key' \ + --query 'Great' + +select Memos --output_columns tags +select Terms + +truncate Tags + +select Memos --output_columns tags +select Terms -------------- next part -------------- An HTML attachment was scrubbed... URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20181226/a7ab290d/attachment-0001.html>