Kouhei Sutou
null+****@clear*****
Sun Dec 28 22:36:26 JST 2014
Kouhei Sutou 2014-12-28 22:36:26 +0900 (Sun, 28 Dec 2014) New Revision: ad7a80ec4a48b14eaef685bc32679bb6adf0f37d https://github.com/groonga/groonga/commit/ad7a80ec4a48b14eaef685bc32679bb6adf0f37d Message: truncate: support truncating column Added files: test/command/suite/truncate/column/index.expected test/command/suite/truncate/column/index.test test/command/suite/truncate/column/scalar_fix_size.expected test/command/suite/truncate/column/scalar_fix_size.test test/command/suite/truncate/column/scalar_variable_size.expected test/command/suite/truncate/column/scalar_variable_size.test Modified files: include/groonga/groonga.h lib/proc.c Modified: include/groonga/groonga.h (+1 -0) =================================================================== --- include/groonga/groonga.h 2014-12-28 22:15:34 +0900 (82c26f6) +++ include/groonga/groonga.h 2014-12-28 22:36:26 +0900 (523cd86) @@ -762,6 +762,7 @@ GRN_API grn_rc grn_column_index_update(grn_ctx *ctx, grn_obj *column, grn_id id, unsigned int section, grn_obj *oldvalue, grn_obj *newvalue); GRN_API grn_obj *grn_column_table(grn_ctx *ctx, grn_obj *column); +GRN_API grn_rc grn_column_truncate(grn_ctx *ctx, grn_obj *column); /*------------------------------------------------------------- * API for db, table and/or column Modified: lib/proc.c (+48 -28) =================================================================== --- lib/proc.c 2014-12-28 22:15:34 +0900 (a368a52) +++ lib/proc.c 2014-12-28 22:36:26 +0900 (f519711) @@ -3386,37 +3386,56 @@ proc_check(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) static grn_obj * proc_truncate(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) { - int table_name_len = GRN_TEXT_LEN(VAR(0)); - if (table_name_len == 0) { - ERR(GRN_INVALID_ARGUMENT, "table name is missing"); + const char *target_name; + int target_name_len; + + target_name_len = GRN_TEXT_LEN(VAR(0)); + if (target_name_len > 0) { + target_name = GRN_TEXT_VALUE(VAR(0)); } else { - const char *table_name = GRN_TEXT_VALUE(VAR(0)); - grn_obj *table = grn_ctx_get(ctx, table_name, table_name_len); - if (!table) { + target_name_len = GRN_TEXT_LEN(VAR(1)); + if (target_name_len == 0) { + ERR(GRN_INVALID_ARGUMENT, "[truncate] table name is missing"); + goto exit; + } + target_name = GRN_TEXT_VALUE(VAR(1)); + } + + { + grn_obj *target = grn_ctx_get(ctx, target_name, target_name_len); + if (!target) { ERR(GRN_INVALID_ARGUMENT, - "no such table: <%.*s>", table_name_len, table_name); - } else { - switch (table->header.type) { - case GRN_TABLE_HASH_KEY : - case GRN_TABLE_PAT_KEY : - case GRN_TABLE_DAT_KEY : - case GRN_TABLE_NO_KEY : - grn_table_truncate(ctx, table); - break; - default: - { - grn_obj buffer; - GRN_TEXT_INIT(&buffer, 0); - grn_inspect(ctx, &buffer, table); - ERR(GRN_INVALID_ARGUMENT, - "not a table object: %.*s", - (int)GRN_TEXT_LEN(&buffer), GRN_TEXT_VALUE(&buffer)); - GRN_OBJ_FIN(ctx, &buffer); - } - break; + "[truncate] no such target: <%.*s>", target_name_len, target_name); + goto exit; + } + + switch (target->header.type) { + case GRN_TABLE_HASH_KEY : + case GRN_TABLE_PAT_KEY : + case GRN_TABLE_DAT_KEY : + case GRN_TABLE_NO_KEY : + grn_table_truncate(ctx, target); + break; + case GRN_COLUMN_FIX_SIZE : + case GRN_COLUMN_VAR_SIZE : + case GRN_COLUMN_INDEX : + grn_column_truncate(ctx, target); + break; + default: + { + grn_obj buffer; + GRN_TEXT_INIT(&buffer, 0); + grn_inspect(ctx, &buffer, target); + ERR(GRN_INVALID_ARGUMENT, + "[truncate] not a table nor column object: <%.*s>", + (int)GRN_TEXT_LEN(&buffer), GRN_TEXT_VALUE(&buffer)); + GRN_OBJ_FIN(ctx, &buffer); } + break; } } + +exit : GRN_OUTPUT_BOOL(!ctx->rc); return NULL; } @@ -6537,8 +6556,9 @@ grn_db_init_builtin_query(grn_ctx *ctx) DEF_VAR(vars[0], "obj"); DEF_COMMAND("check", proc_check, 1, vars); - DEF_VAR(vars[0], "table"); - DEF_COMMAND("truncate", proc_truncate, 1, vars); + DEF_VAR(vars[0], "target_name"); + DEF_VAR(vars[1], "table"); + DEF_COMMAND("truncate", proc_truncate, 2, vars); DEF_VAR(vars[0], "normalizer"); DEF_VAR(vars[1], "string"); Added: test/command/suite/truncate/column/index.expected (+112 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/truncate/column/index.expected 2014-12-28 22:36:26 +0900 (e051085) @@ -0,0 +1,112 @@ +table_create Logs TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Logs message COLUMN_SCALAR 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 logs_message COLUMN_INDEX|WITH_POSITION Logs message +[[0,0.0,0.0],true] +load --table Logs +[ +{"message": "Hello"}, +{"message": "World"} +] +[[0,0.0,0.0],2] +select Terms +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 2 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "ShortText" + ], + [ + "logs_message", + "UInt32" + ] + ], + [ + 1, + "hello", + 1 + ], + [ + 2, + "world", + 1 + ] + ] + ] +] +truncate Terms.logs_message +[[0,0.0,0.0],true] +select Terms +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 2 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "ShortText" + ], + [ + "logs_message", + "UInt32" + ] + ], + [ + 1, + "hello", + 0 + ], + [ + 2, + "world", + 0 + ] + ] + ] +] +dump +table_create Logs TABLE_NO_KEY +column_create Logs message COLUMN_SCALAR ShortText +table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto +column_create Terms logs_message COLUMN_INDEX|WITH_POSITION Logs message +load --table Logs +[ +["_id","message"], +[1,"Hello"], +[2,"World"] +] +load --table Terms +[ +["_key"], +["hello"], +["world"] +] + Added: test/command/suite/truncate/column/index.test (+20 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/truncate/column/index.test 2014-12-28 22:36:26 +0900 (936b9d0) @@ -0,0 +1,20 @@ +table_create Logs TABLE_NO_KEY +column_create Logs message COLUMN_SCALAR ShortText + +table_create Terms TABLE_PAT_KEY ShortText \ + --default_tokenizer TokenBigram \ + --normalizer NormalizerAuto +column_create Terms logs_message COLUMN_INDEX|WITH_POSITION Logs message + +load --table Logs +[ +{"message": "Hello"}, +{"message": "World"} +] + +select Terms + +truncate Terms.logs_message + +select Terms +dump Added: test/command/suite/truncate/column/scalar_fix_size.expected (+36 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/truncate/column/scalar_fix_size.expected 2014-12-28 22:36:26 +0900 (89a2c62) @@ -0,0 +1,36 @@ +table_create Logs TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Logs id COLUMN_SCALAR Int32 +[[0,0.0,0.0],true] +column_create Logs message COLUMN_SCALAR ShortText +[[0,0.0,0.0],true] +load --table Logs +[ +{"id": 11, "message": "Hello"}, +{"id": 22, "message": "World"} +] +[[0,0.0,0.0],2] +dump +table_create Logs TABLE_NO_KEY +column_create Logs id COLUMN_SCALAR Int32 +column_create Logs message COLUMN_SCALAR ShortText +load --table Logs +[ +["_id","id","message"], +[1,11,"Hello"], +[2,22,"World"] +] + +truncate Logs.id +[[0,0.0,0.0],true] +dump +table_create Logs TABLE_NO_KEY +column_create Logs id COLUMN_SCALAR Int32 +column_create Logs message COLUMN_SCALAR ShortText +load --table Logs +[ +["_id","id","message"], +[1,0,"Hello"], +[2,0,"World"] +] + Added: test/command/suite/truncate/column/scalar_fix_size.test (+15 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/truncate/column/scalar_fix_size.test 2014-12-28 22:36:26 +0900 (d8395b9) @@ -0,0 +1,15 @@ +table_create Logs TABLE_NO_KEY +column_create Logs id COLUMN_SCALAR Int32 +column_create Logs message COLUMN_SCALAR ShortText + +load --table Logs +[ +{"id": 11, "message": "Hello"}, +{"id": 22, "message": "World"} +] + +dump + +truncate Logs.id + +dump Added: test/command/suite/truncate/column/scalar_variable_size.expected (+36 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/truncate/column/scalar_variable_size.expected 2014-12-28 22:36:26 +0900 (2bb2d28) @@ -0,0 +1,36 @@ +table_create Logs TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Logs id COLUMN_SCALAR Int32 +[[0,0.0,0.0],true] +column_create Logs message COLUMN_SCALAR ShortText +[[0,0.0,0.0],true] +load --table Logs +[ +{"id": 11, "message": "Hello"}, +{"id": 22, "message": "World"} +] +[[0,0.0,0.0],2] +dump +table_create Logs TABLE_NO_KEY +column_create Logs id COLUMN_SCALAR Int32 +column_create Logs message COLUMN_SCALAR ShortText +load --table Logs +[ +["_id","id","message"], +[1,11,"Hello"], +[2,22,"World"] +] + +truncate Logs.message +[[0,0.0,0.0],true] +dump +table_create Logs TABLE_NO_KEY +column_create Logs id COLUMN_SCALAR Int32 +column_create Logs message COLUMN_SCALAR ShortText +load --table Logs +[ +["_id","id","message"], +[1,11,""], +[2,22,""] +] + Added: test/command/suite/truncate/column/scalar_variable_size.test (+15 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/truncate/column/scalar_variable_size.test 2014-12-28 22:36:26 +0900 (3d7d7f0) @@ -0,0 +1,15 @@ +table_create Logs TABLE_NO_KEY +column_create Logs id COLUMN_SCALAR Int32 +column_create Logs message COLUMN_SCALAR ShortText + +load --table Logs +[ +{"id": 11, "message": "Hello"}, +{"id": 22, "message": "World"} +] + +dump + +truncate Logs.message + +dump -------------- next part -------------- HTML����������������������������...Download