susumu.yata
null+****@clear*****
Mon Jul 13 17:38:07 JST 2015
susumu.yata 2015-07-13 17:38:07 +0900 (Mon, 13 Jul 2015) New Revision: ed03dc777a3eb6b7facd73f323365706500e64de https://github.com/groonga/grngo/commit/ed03dc777a3eb6b7facd73f323365706500e64de Message: Simplify Table.InsertRow(). Modified files: grngo.c grngo.go grngo.h Modified: grngo.c (+58 -58) =================================================================== --- grngo.c 2015-07-13 17:19:29 +0900 (4c92fcd) +++ grngo.c 2015-07-13 17:38:07 +0900 (793e66b) @@ -142,127 +142,127 @@ grn_bool grngo_column_get_value_info(grn_ctx *ctx, grn_obj *column, return GRN_TRUE; } +// grngo_table_insertion_error generates an error result. +static grngo_table_insertion_result grngo_table_insertion_error(grn_rc rc) { + grngo_table_insertion_result result; + result.rc = rc; + result.inserted = GRN_FALSE; + result.id = GRN_ID_NIL; + return result; +} + // grngo_table_insert_row calls grn_table_add to insert a row. -static grn_rc grngo_table_insert_row(grn_ctx *ctx, grn_obj *table, - const void *key, size_t key_size, - grn_bool *inserted, grn_id *id) { +static grngo_table_insertion_result grngo_table_insert_row( + grn_ctx *ctx, grn_obj *table, const void *key, size_t key_size) { if (!ctx || !table || !grn_obj_is_table(ctx, table) || - (!key && (key_size != 0)) || !inserted || !id) { - return GRN_INVALID_ARGUMENT; + (!key && (key_size != 0))) { + return grngo_table_insertion_error(GRN_INVALID_ARGUMENT); } - int tmp_inserted; - grn_id tmp_id = grn_table_add(ctx, table, key, key_size, &tmp_inserted); - if (tmp_id == GRN_ID_NIL) { + int inserted; + grn_id id = grn_table_add(ctx, table, key, key_size, &inserted); + if (id == GRN_ID_NIL) { if (ctx->rc != GRN_SUCCESS) { - return ctx->rc; + return grngo_table_insertion_error(ctx->rc); } - return GRN_UNKNOWN_ERROR; + return grngo_table_insertion_error(GRN_UNKNOWN_ERROR); } - *inserted = (grn_bool)tmp_inserted; - *id = tmp_id; - return GRN_SUCCESS; + grngo_table_insertion_result result; + result.rc = GRN_SUCCESS; + result.inserted = (grn_bool)inserted; + result.id = id; + return result; } -grn_rc grngo_table_insert_void(grn_ctx *ctx, grn_obj *table, - grn_bool *inserted, grn_id *id) { - return grngo_table_insert_row(ctx, table, NULL, 0, inserted, id); +grngo_table_insertion_result grngo_table_insert_void( + grn_ctx *ctx, grn_obj *table) { + return grngo_table_insert_row(ctx, table, NULL, 0); } -grn_rc grngo_table_insert_bool(grn_ctx *ctx, grn_obj *table, - grn_bool key, grn_bool *inserted, grn_id *id) { - return grngo_table_insert_row(ctx, table, &key, sizeof(key), inserted, id); +grngo_table_insertion_result grngo_table_insert_bool( + grn_ctx *ctx, grn_obj *table, grn_bool key) { + return grngo_table_insert_row(ctx, table, &key, sizeof(key)); } -grn_rc grngo_table_insert_int(grn_ctx *ctx, grn_obj *table, - grn_builtin_type builtin_type, - int64_t key, grn_bool *inserted, grn_id *id) { +grngo_table_insertion_result grngo_table_insert_int( + grn_ctx *ctx, grn_obj *table, grn_builtin_type builtin_type, int64_t key) { switch (builtin_type) { case GRN_DB_INT8: { if ((key < INT8_MIN) || (key > INT8_MAX)) { - return GRN_INVALID_ARGUMENT; + return grngo_table_insertion_error(GRN_INVALID_ARGUMENT); } int8_t tmp_key = (int8_t)key; - return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key), - inserted, id); + return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key)); } case GRN_DB_INT16: { if ((key < INT16_MIN) || (key > INT16_MAX)) { - return GRN_INVALID_ARGUMENT; + return grngo_table_insertion_error(GRN_INVALID_ARGUMENT); } int16_t tmp_key = (int16_t)key; - return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key), - inserted, id); + return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key)); } case GRN_DB_INT32: { if ((key < INT32_MIN) || (key > INT32_MAX)) { - return GRN_INVALID_ARGUMENT; + return grngo_table_insertion_error(GRN_INVALID_ARGUMENT); } int32_t tmp_key = (int32_t)key; - return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key), - inserted, id); + return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key)); } case GRN_DB_INT64: case GRN_DB_TIME: { - return grngo_table_insert_row(ctx, table, &key, sizeof(key), inserted, id); + return grngo_table_insert_row(ctx, table, &key, sizeof(key)); } case GRN_DB_UINT8: { if ((key < 0) || (key > (int64_t)UINT8_MAX)) { - return GRN_INVALID_ARGUMENT; + return grngo_table_insertion_error(GRN_INVALID_ARGUMENT); } uint8_t tmp_key = (uint8_t)key; - return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key), - inserted, id); + return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key)); } case GRN_DB_UINT16: { if ((key < 0) || (key > (int64_t)UINT16_MAX)) { - return GRN_INVALID_ARGUMENT; + return grngo_table_insertion_error(GRN_INVALID_ARGUMENT); } uint16_t tmp_key = (uint16_t)key; - return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key), - inserted, id); + return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key)); } case GRN_DB_UINT32: { if ((key < 0) || (key > (int64_t)UINT32_MAX)) { - return GRN_INVALID_ARGUMENT; + return grngo_table_insertion_error(GRN_INVALID_ARGUMENT); } uint32_t tmp_key = (uint32_t)key; - return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key), - inserted, id); + return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key)); } case GRN_DB_UINT64: { if (key < 0) { - return GRN_INVALID_ARGUMENT; + return grngo_table_insertion_error(GRN_INVALID_ARGUMENT); } - return grngo_table_insert_row(ctx, table, &key, sizeof(key), - inserted, id); + return grngo_table_insert_row(ctx, table, &key, sizeof(key)); } default: { - return GRN_INVALID_ARGUMENT; + return grngo_table_insertion_error(GRN_UNKNOWN_ERROR); } } } -grn_rc grngo_table_insert_float(grn_ctx *ctx, grn_obj *table, - double key, grn_bool *inserted, grn_id *id) { +grngo_table_insertion_result grngo_table_insert_float( + grn_ctx *ctx, grn_obj *table, double key) { if (isnan(key)) { - return GRN_INVALID_ARGUMENT; + return grngo_table_insertion_error(GRN_INVALID_ARGUMENT); } - return grngo_table_insert_row(ctx, table, &key, sizeof(key), inserted, id); + return grngo_table_insert_row(ctx, table, &key, sizeof(key)); } -grn_rc grngo_table_insert_text(grn_ctx *ctx, grn_obj *table, - const grngo_text *key, - grn_bool *inserted, grn_id *id) { +grngo_table_insertion_result grngo_table_insert_text( + grn_ctx *ctx, grn_obj *table, const grngo_text *key) { if (!key || (!key->ptr && (key->size != 0))) { - return GRN_INVALID_ARGUMENT; + return grngo_table_insertion_error(GRN_INVALID_ARGUMENT); } - return grngo_table_insert_row(ctx, table, key->ptr, key->size, inserted, id); + return grngo_table_insert_row(ctx, table, key->ptr, key->size); } -grn_rc grngo_table_insert_geo_point(grn_ctx *ctx, grn_obj *table, - const grn_geo_point *key, - grn_bool *inserted, grn_id *id) { - return grngo_table_insert_row(ctx, table, key, sizeof(*key), inserted, id); +grngo_table_insertion_result grngo_table_insert_geo_point( + grn_ctx *ctx, grn_obj *table, const grn_geo_point *key) { + return grngo_table_insert_row(ctx, table, key, sizeof(*key)); } grn_bool grngo_column_set_bool(grn_ctx *ctx, grn_obj *column, Modified: grngo.go (+10 -31) =================================================================== --- grngo.go 2015-07-13 17:19:29 +0900 (e4aeea7) +++ grngo.go 2015-07-13 17:38:07 +0900 (24f516c) @@ -877,19 +877,13 @@ func newTable(db *DB, obj *C.grn_obj, name string, keyType DataType, keyTable *T // InsertRow finds or inserts a row. func (table *Table) InsertRow(key interface{}) (inserted bool, id uint32, err error) { - var rc C.grn_rc = C.GRN_SUCCESS - var tmpInserted C.grn_bool - var tmpID C.grn_id - var opName string + var result C.grngo_table_insertion_result switch key := key.(type) { case nil: if table.keyType != Void { return false, NilID, newInvalidKeyTypeError(table.keyType, Void) } - rc = C.grngo_table_insert_void(table.db.ctx, table.obj, &tmpInserted, &tmpID) - if rc != C.GRN_SUCCESS { - opName = "grngo_table_insert_void()" - } + result = C.grngo_table_insert_void(table.db.ctx, table.obj) case bool: if table.keyType != Bool { return false, NilID, newInvalidKeyTypeError(table.keyType, Bool) @@ -898,29 +892,20 @@ func (table *Table) InsertRow(key interface{}) (inserted bool, id uint32, err er if key { tmpKey = C.grn_bool(C.GRN_TRUE) } - rc = C.grngo_table_insert_bool(table.db.ctx, table.obj, tmpKey, &tmpInserted, &tmpID) - if rc != C.GRN_SUCCESS { - opName = "grngo_table_insert_bool()" - } + result = C.grngo_table_insert_bool(table.db.ctx, table.obj, tmpKey) case int64: switch table.keyType { case Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Time: default: return false, NilID, newInvalidKeyTypeError(table.keyType, LazyInt) } - rc = C.grngo_table_insert_int(table.db.ctx, table.obj, C.grn_builtin_type(table.keyType), C.int64_t(key), &tmpInserted, &tmpID) - if rc != C.GRN_SUCCESS { - opName = "grngo_table_insert_int()" - } + result = C.grngo_table_insert_int(table.db.ctx, table.obj, C.grn_builtin_type(table.keyType), C.int64_t(key)) case float64: if table.keyType != Float { return false, NilID, newInvalidKeyTypeError(table.keyType, Float) } tmpKey := C.double(key) - rc = C.grngo_table_insert_float(table.db.ctx, table.obj, tmpKey, &tmpInserted, &tmpID) - if rc != C.GRN_SUCCESS { - opName = "grngo_table_insert_float()" - } + result = C.grngo_table_insert_float(table.db.ctx, table.obj, tmpKey) case []byte: if table.keyType != ShortText { return false, NilID, newInvalidKeyTypeError(table.keyType, Text) @@ -930,27 +915,21 @@ func (table *Table) InsertRow(key interface{}) (inserted bool, id uint32, err er tmpKey.ptr = (*C.char)(unsafe.Pointer(&key[0])) tmpKey.size = C.size_t(len(key)) } - rc = C.grngo_table_insert_text(table.db.ctx, table.obj, &tmpKey, &tmpInserted, &tmpID) - if rc != C.GRN_SUCCESS { - opName = "grngo_table_insert_text()" - } + result = C.grngo_table_insert_text(table.db.ctx, table.obj, &tmpKey) case GeoPoint: if (table.keyType != TokyoGeoPoint) && (table.keyType != WGS84GeoPoint) { return false, NilID, newInvalidKeyTypeError(table.keyType, LazyGeoPoint) } tmpKey := C.grn_geo_point{C.int(key.Latitude), C.int(key.Longitude)} - rc = C.grngo_table_insert_geo_point(table.db.ctx, table.obj, &tmpKey, &tmpInserted, &tmpID) - if rc != C.GRN_SUCCESS { - opName = "grngo_table_insert_geo_point()" - } + result = C.grngo_table_insert_geo_point(table.db.ctx, table.obj, &tmpKey) default: return false, NilID, fmt.Errorf( "unsupported key type: typeName = <%s>", reflect.TypeOf(key).Name()) } - if rc != C.GRN_SUCCESS { - return false, NilID, newGrnError(opName, &rc, table.db.ctx) + if result.rc != C.GRN_SUCCESS { + return false, NilID, newGrnError("grngo_table_insert_*()", &result.rc, table.db.ctx) } - return tmpInserted == C.GRN_TRUE, uint32(tmpID), nil + return result.inserted == C.GRN_TRUE, uint32(result.id), nil } // SetValue assigns a value. Modified: grngo.h (+19 -15) =================================================================== --- grngo.h 2015-07-13 17:19:29 +0900 (db88cae) +++ grngo.h 2015-07-13 17:38:07 +0900 (d3a665b) @@ -55,27 +55,31 @@ grn_rc grngo_table_get_value_info(grn_ctx *ctx, grn_obj *table, grn_bool grngo_column_get_value_info(grn_ctx *ctx, grn_obj *column, grngo_type_info *value_info); +typedef struct { + grn_rc rc; // rc stores a return code. + grn_bool inserted; // inserted stores whether a row was inserted or not. + grn_id id; // id stores the ID of an inserted or found row. + // GRN_ID_NIL means that an operation failed. +} grngo_table_insertion_result; + // grngo_table_insert_void inserts an empty row. -grn_rc grngo_table_insert_void(grn_ctx *ctx, grn_obj *table, - grn_bool *inserted, grn_id *id); +grngo_table_insertion_result grngo_table_insert_void( + grn_ctx *ctx, grn_obj *table); // grngo_table_insert_bool inserts a row with a Bool key. -grn_rc grngo_table_insert_bool(grn_ctx *ctx, grn_obj *table, - grn_bool key, grn_bool *inserted, grn_id *id); +grngo_table_insertion_result grngo_table_insert_bool( + grn_ctx *ctx, grn_obj *table, grn_bool key); // grngo_table_insert_int inserts a row with an (U)IntXX key. -grn_rc grngo_table_insert_int(grn_ctx *ctx, grn_obj *table, - grn_builtin_type builtin_type, - int64_t key, grn_bool *inserted, grn_id *id); +grngo_table_insertion_result grngo_table_insert_int( + grn_ctx *ctx, grn_obj *table, grn_builtin_type builtin_type, int64_t key); // grngo_table_insert_float inserts a row with a Float key. -grn_rc grngo_table_insert_float(grn_ctx *ctx, grn_obj *table, - double key, grn_bool *inserted, grn_id *id); +grngo_table_insertion_result grngo_table_insert_float( + grn_ctx *ctx, grn_obj *table, double key); // grngo_table_insert_text inserts a row with a ShortText key. -grn_rc grngo_table_insert_text(grn_ctx *ctx, grn_obj *table, - const grngo_text *key, - grn_bool *inserted, grn_id *id); +grngo_table_insertion_result grngo_table_insert_text( + grn_ctx *ctx, grn_obj *table, const grngo_text *key); // grngo_table_insert_geo_point inserts a row with a (Tokyo/WGS84)GeoPoint key. -grn_rc grngo_table_insert_geo_point(grn_ctx *ctx, grn_obj *table, - const grn_geo_point *key, - grn_bool *inserted, grn_id *id); +grngo_table_insertion_result grngo_table_insert_geo_point( + grn_ctx *ctx, grn_obj *table, const grn_geo_point *key); // grngo_column_set_bool() assigns a Bool value. grn_bool grngo_column_set_bool(grn_ctx *ctx, grn_obj *column, -------------- next part -------------- HTML����������������������������...Download