Kouhei Sutou
null+****@clear*****
Fri Oct 24 18:42:45 JST 2014
Kouhei Sutou 2014-10-24 18:42:45 +0900 (Fri, 24 Oct 2014) New Revision: 4f7e2a509a62bf701220bf809830b8774a0cfb49 https://github.com/groonga/groonga/commit/4f7e2a509a62bf701220bf809830b8774a0cfb49 Message: load: support updating existing record by _id key Added files: test/command/suite/load/array/duplicated_id_key.expected test/command/suite/load/array/duplicated_id_key.test test/command/suite/load/array/id_update.expected test/command/suite/load/array/id_update.test test/command/suite/load/array/invalid_id_value.expected test/command/suite/load/array/invalid_id_value.test Modified files: lib/db.c Modified: lib/db.c (+58 -2) =================================================================== --- lib/db.c 2014-10-24 17:40:03 +0900 (79ac5c0) +++ lib/db.c 2014-10-24 18:42:45 +0900 (3b2391e) @@ -10564,7 +10564,7 @@ brace_close(grn_ctx *ctx, grn_loader *loader) (int)GRN_TEXT_LEN(key_column_name), GRN_TEXT_VALUE(key_column_name), column_name_size, column_name); - return; + goto exit; } key_column_name = value; v++; @@ -10577,7 +10577,62 @@ brace_close(grn_ctx *ctx, grn_loader *loader) } break; case GRN_TABLE_NO_KEY : - id = grn_table_add(ctx, loader->table, NULL, 0, NULL); + { + grn_obj *v; + grn_bool found_id_column = GRN_FALSE; + for (v = value; v + 1 < ve; v = values_next(ctx, v)) { + char *column_name = GRN_TEXT_VALUE(v); + unsigned int column_name_size = GRN_TEXT_LEN(v); + if (v->header.domain == GRN_DB_TEXT && + (name_equal(column_name, column_name_size, + GRN_COLUMN_NAME_ID))) { + grn_obj *id_column; + grn_obj *id_value; + if (found_id_column) { + GRN_LOG(ctx, GRN_LOG_ERROR, "duplicated '_id' column"); + goto exit; + } + found_id_column = GRN_TRUE; + id_column = v; + v = values_next(ctx, v); + id_value = v; + switch (id_value->header.type) { + case GRN_DB_UINT32 : + id = GRN_UINT32_VALUE(id_value); + break; + case GRN_DB_INT32 : + id = GRN_INT32_VALUE(id_value); + break; + default : + { + grn_obj casted_id_value; + GRN_UINT32_INIT(&casted_id_value, 0); + if (grn_obj_cast(ctx, id_value, &casted_id_value, GRN_FALSE)) { + grn_obj inspected; + GRN_TEXT_INIT(&inspected, 0); + grn_inspect(ctx, &inspected, id_value); + ERR(GRN_INVALID_ARGUMENT, + "<%.*s>: failed to cast to <UInt32>: <%.*s>", + (int)column_name_size, column_name, + (int)GRN_TEXT_LEN(&inspected), + GRN_TEXT_VALUE(&inspected)); + grn_obj_unlink(ctx, &inspected); + goto exit; + } else { + id = GRN_UINT32_VALUE(&casted_id_value); + } + GRN_OBJ_FIN(ctx, &casted_id_value); + } + break; + } + } else { + v = values_next(ctx, v); + } + } + } + if (id == GRN_ID_NIL) { + id = grn_table_add(ctx, loader->table, NULL, 0, NULL); + } break; default : break; @@ -10635,6 +10690,7 @@ brace_close(grn_ctx *ctx, grn_loader *loader) GRN_LOG(ctx, GRN_LOG_ERROR, "neither _key nor _id is assigned"); } } + exit: loader->values_size = begin; } } Added: test/command/suite/load/array/duplicated_id_key.expected (+44 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/load/array/duplicated_id_key.expected 2014-10-24 18:42:45 +0900 (28b960b) @@ -0,0 +1,44 @@ +table_create Memos TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Memos content COLUMN_SCALAR Text +[[0,0.0,0.0],true] +load --table Memos +[ +{"content": "Groonga is fast"} +] +[[0,0.0,0.0],1] +load --table Memos +[ +{"_id": 1, "content": "Mroonga is fast", "_id": 1} +] +[[0,0.0,0.0],0] +#|e| duplicated '_id' column +select Memos +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "content", + "Text" + ] + ], + [ + 1, + "Groonga is fast" + ] + ] + ] +] Added: test/command/suite/load/array/duplicated_id_key.test (+14 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/load/array/duplicated_id_key.test 2014-10-24 18:42:45 +0900 (62f4f3a) @@ -0,0 +1,14 @@ +table_create Memos TABLE_NO_KEY +column_create Memos content COLUMN_SCALAR Text + +load --table Memos +[ +{"content": "Groonga is fast"} +] + +load --table Memos +[ +{"_id": 1, "content": "Mroonga is fast", "_id": 1} +] + +select Memos Added: test/command/suite/load/array/id_update.expected (+43 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/load/array/id_update.expected 2014-10-24 18:42:45 +0900 (32f69df) @@ -0,0 +1,43 @@ +table_create Memos TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Memos content COLUMN_SCALAR Text +[[0,0.0,0.0],true] +load --table Memos +[ +{"content": "Groonga is fast"} +] +[[0,0.0,0.0],1] +load --table Memos +[ +{"_id": 1, "content": "Mroonga is fast"} +] +[[0,0.0,0.0],1] +select Memos +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "content", + "Text" + ] + ], + [ + 1, + "Mroonga is fast" + ] + ] + ] +] Added: test/command/suite/load/array/id_update.test (+14 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/load/array/id_update.test 2014-10-24 18:42:45 +0900 (5b39721) @@ -0,0 +1,14 @@ +table_create Memos TABLE_NO_KEY +column_create Memos content COLUMN_SCALAR Text + +load --table Memos +[ +{"content": "Groonga is fast"} +] + +load --table Memos +[ +{"_id": 1, "content": "Mroonga is fast"} +] + +select Memos Added: test/command/suite/load/array/invalid_id_value.expected (+44 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/load/array/invalid_id_value.expected 2014-10-24 18:42:45 +0900 (db27270) @@ -0,0 +1,44 @@ +table_create Memos TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Memos content COLUMN_SCALAR Text +[[0,0.0,0.0],true] +load --table Memos +[ +{"content": "Groonga is fast"} +] +[[0,0.0,0.0],1] +load --table Memos +[ +{"_id": "invalid", "content": "Mroonga is fast"} +] +[[0,0.0,0.0],0] +#|e| <_id>: failed to cast to <UInt32>: <"invalid"> +select Memos +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "content", + "Text" + ] + ], + [ + 1, + "Groonga is fast" + ] + ] + ] +] Added: test/command/suite/load/array/invalid_id_value.test (+14 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/load/array/invalid_id_value.test 2014-10-24 18:42:45 +0900 (0d17269) @@ -0,0 +1,14 @@ +table_create Memos TABLE_NO_KEY +column_create Memos content COLUMN_SCALAR Text + +load --table Memos +[ +{"content": "Groonga is fast"} +] + +load --table Memos +[ +{"_id": "invalid", "content": "Mroonga is fast"} +] + +select Memos -------------- next part -------------- HTML����������������������������...Télécharger