[Groonga-commit] groonga/grngo at 7dbd134 [master] Simplify Table.InsertRow().

アーカイブの一覧に戻る

susumu.yata null+****@clear*****
Mon Jul 13 17:19:29 JST 2015


susumu.yata	2015-07-13 17:19:29 +0900 (Mon, 13 Jul 2015)

  New Revision: 7dbd1343e40eed85538cb5c39883d7d904214a6e
  https://github.com/groonga/grngo/commit/7dbd1343e40eed85538cb5c39883d7d904214a6e

  Message:
    Simplify Table.InsertRow().

  Modified files:
    grngo.c
    grngo.go
    grngo.h

  Modified: grngo.c (+110 -65)
===================================================================
--- grngo.c    2015-07-10 17:27:49 +0900 (4318943)
+++ grngo.c    2015-07-13 17:19:29 +0900 (4c92fcd)
@@ -1,5 +1,6 @@
 #include "grngo.h"
 
+#include <math.h>
 #include <string.h>
 
 #define GRNGO_MAX_BUILTIN_TYPE_ID GRN_DB_WGS84_GEO_POINT
@@ -141,83 +142,127 @@ grn_bool grngo_column_get_value_info(grn_ctx *ctx, grn_obj *column,
   return GRN_TRUE;
 }
 
-// grngo_table_insert_row() calls grn_table_add() and converts the result.
-static grngo_row_info grngo_table_insert_row(
-    grn_ctx *ctx, grn_obj *table, const void *key_ptr, size_t key_size) {
-  grngo_row_info row_info;
-  int inserted;
-  row_info.id = grn_table_add(ctx, table, key_ptr, key_size, &inserted);
-  row_info.inserted = inserted ? GRN_TRUE : GRN_FALSE;
-  return row_info;
-}
-
-grngo_row_info grngo_table_insert_void(grn_ctx *ctx, grn_obj *table) {
-  return grngo_table_insert_row(ctx, table, NULL, 0);
-}
-
-grngo_row_info grngo_table_insert_bool(grn_ctx *ctx, grn_obj *table,
-                                       grn_bool key) {
-  return grngo_table_insert_row(ctx, table, &key, sizeof(key));
-}
-
-grngo_row_info grngo_table_insert_int8(grn_ctx *ctx, grn_obj *table,
-                                       int8_t key) {
-  return grngo_table_insert_row(ctx, table, &key, sizeof(key));
-}
-
-grngo_row_info grngo_table_insert_int16(grn_ctx *ctx, grn_obj *table,
-                                        int16_t key) {
-  return grngo_table_insert_row(ctx, table, &key, sizeof(key));
-}
-
-grngo_row_info grngo_table_insert_int32(grn_ctx *ctx, grn_obj *table,
-                                        int32_t key) {
-  return grngo_table_insert_row(ctx, table, &key, sizeof(key));
-}
-
-grngo_row_info grngo_table_insert_int64(grn_ctx *ctx, grn_obj *table,
-                                        int64_t key) {
-  return grngo_table_insert_row(ctx, table, &key, sizeof(key));
-}
-
-grngo_row_info grngo_table_insert_uint8(grn_ctx *ctx, grn_obj *table,
-                                        uint8_t key) {
-  return grngo_table_insert_row(ctx, table, &key, sizeof(key));
-}
-
-grngo_row_info grngo_table_insert_uint16(grn_ctx *ctx, grn_obj *table,
-                                         uint16_t key) {
-  return grngo_table_insert_row(ctx, table, &key, sizeof(key));
+// 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) {
+  if (!ctx || !table || !grn_obj_is_table(ctx, table) ||
+      (!key && (key_size != 0)) || !inserted || !id) {
+    return 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) {
+    if (ctx->rc != GRN_SUCCESS) {
+      return ctx->rc;
+    }
+    return GRN_UNKNOWN_ERROR;
+  }
+  *inserted = (grn_bool)tmp_inserted;
+  *id = tmp_id;
+  return GRN_SUCCESS;
 }
 
-grngo_row_info grngo_table_insert_uint32(grn_ctx *ctx, grn_obj *table,
-                                         uint32_t key) {
-  return grngo_table_insert_row(ctx, table, &key, sizeof(key));
+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_row_info grngo_table_insert_uint64(grn_ctx *ctx, grn_obj *table,
-                                         uint64_t key) {
-  return grngo_table_insert_row(ctx, table, &key, sizeof(key));
+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_row_info grngo_table_insert_time(grn_ctx *ctx, grn_obj *table,
-                                       int64_t 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) {
+  switch (builtin_type) {
+    case GRN_DB_INT8: {
+      if ((key < INT8_MIN) || (key > INT8_MAX)) {
+        return GRN_INVALID_ARGUMENT;
+      }
+      int8_t tmp_key = (int8_t)key;
+      return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key),
+                                    inserted, id);
+    }
+    case GRN_DB_INT16: {
+      if ((key < INT16_MIN) || (key > INT16_MAX)) {
+        return GRN_INVALID_ARGUMENT;
+      }
+      int16_t tmp_key = (int16_t)key;
+      return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key),
+                                    inserted, id);
+    }
+    case GRN_DB_INT32: {
+      if ((key < INT32_MIN) || (key > INT32_MAX)) {
+        return GRN_INVALID_ARGUMENT;
+      }
+      int32_t tmp_key = (int32_t)key;
+      return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key),
+                                    inserted, id);
+    }
+    case GRN_DB_INT64:
+    case GRN_DB_TIME: {
+      return grngo_table_insert_row(ctx, table, &key, sizeof(key), inserted, id);
+    }
+    case GRN_DB_UINT8: {
+      if ((key < 0) || (key > (int64_t)UINT8_MAX)) {
+        return GRN_INVALID_ARGUMENT;
+      }
+      uint8_t tmp_key = (uint8_t)key;
+      return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key),
+                                    inserted, id);
+    }
+    case GRN_DB_UINT16: {
+      if ((key < 0) || (key > (int64_t)UINT16_MAX)) {
+        return GRN_INVALID_ARGUMENT;
+      }
+      uint16_t tmp_key = (uint16_t)key;
+      return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key),
+                                    inserted, id);
+    }
+    case GRN_DB_UINT32: {
+      if ((key < 0) || (key > (int64_t)UINT32_MAX)) {
+        return GRN_INVALID_ARGUMENT;
+      }
+      uint32_t tmp_key = (uint32_t)key;
+      return grngo_table_insert_row(ctx, table, &tmp_key, sizeof(tmp_key),
+                                    inserted, id);
+    }
+    case GRN_DB_UINT64: {
+      if (key < 0) {
+        return GRN_INVALID_ARGUMENT;
+      }
+      return grngo_table_insert_row(ctx, table, &key, sizeof(key),
+                                    inserted, id);
+    }
+    default: {
+      return GRN_INVALID_ARGUMENT;
+    }
+  }
 }
 
-grngo_row_info grngo_table_insert_float(grn_ctx *ctx, grn_obj *table,
-                                        double key) {
-  return grngo_table_insert_row(ctx, table, &key, sizeof(key));
+grn_rc grngo_table_insert_float(grn_ctx *ctx, grn_obj *table,
+                                double key, grn_bool *inserted, grn_id *id) {
+  if (isnan(key)) {
+    return GRN_INVALID_ARGUMENT;
+  }
+  return grngo_table_insert_row(ctx, table, &key, sizeof(key), inserted, id);
 }
 
-grngo_row_info grngo_table_insert_text(grn_ctx *ctx, grn_obj *table,
-                                       const grngo_text *key) {
-  return grngo_table_insert_row(ctx, table, key->ptr, key->size);
+grn_rc grngo_table_insert_text(grn_ctx *ctx, grn_obj *table,
+                               const grngo_text *key,
+                               grn_bool *inserted, grn_id *id) {
+  if (!key || (!key->ptr && (key->size != 0))) {
+    return GRN_INVALID_ARGUMENT;
+  }
+  return grngo_table_insert_row(ctx, table, key->ptr, key->size, inserted, id);
 }
 
-grngo_row_info grngo_table_insert_geo_point(grn_ctx *ctx, grn_obj *table,
-                                            grn_geo_point key) {
-  return grngo_table_insert_row(ctx, table, &key, sizeof(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) {
+  return grngo_table_insert_row(ctx, table, key, sizeof(*key), inserted, id);
 }
 
 grn_bool grngo_column_set_bool(grn_ctx *ctx, grn_obj *column,

  Modified: grngo.go (+64 -120)
===================================================================
--- grngo.go    2015-07-10 17:27:49 +0900 (0e16a6a)
+++ grngo.go    2015-07-13 17:19:29 +0900 (e4aeea7)
@@ -875,138 +875,82 @@ func newTable(db *DB, obj *C.grn_obj, name string, keyType DataType, keyTable *T
 	return &table
 }
 
-// insertVoid inserts an empty row.
-func (table *Table) insertVoid() (bool, uint32, error) {
-	if table.keyType != Void {
-		return false, NilID, newInvalidKeyTypeError(table.keyType, Void)
-	}
-	rowInfo := C.grngo_table_insert_void(table.db.ctx, table.obj)
-	if rowInfo.id == C.GRN_ID_NIL {
-		return false, NilID, fmt.Errorf("grngo_table_insert_void() failed")
-	}
-	return rowInfo.inserted == C.GRN_TRUE, uint32(rowInfo.id), nil
-}
-
-// insertBool inserts a row with Bool key.
-func (table *Table) insertBool(key bool) (bool, uint32, error) {
-	if table.keyType != Bool {
-		return false, NilID, newInvalidKeyTypeError(table.keyType, Bool)
-	}
-	grnKey := C.grn_bool(C.GRN_FALSE)
-	if key {
-		grnKey = C.grn_bool(C.GRN_TRUE)
-	}
-	rowInfo := C.grngo_table_insert_bool(table.db.ctx, table.obj, grnKey)
-	if rowInfo.id == C.GRN_ID_NIL {
-		return false, NilID, fmt.Errorf("grngo_table_insert_bool() failed")
-	}
-	return rowInfo.inserted == C.GRN_TRUE, uint32(rowInfo.id), nil
-}
-
-// insertInt inserts a row with Int key.
-func (table *Table) insertInt(key int64) (bool, uint32, error) {
-	var rowInfo C.grngo_row_info
-	switch table.keyType {
-	case Int8:
-		grnKey := C.int8_t(key)
-		rowInfo = C.grngo_table_insert_int8(table.db.ctx, table.obj, grnKey)
-	case Int16:
-		grnKey := C.int16_t(key)
-		rowInfo = C.grngo_table_insert_int16(table.db.ctx, table.obj, grnKey)
-	case Int32:
-		grnKey := C.int32_t(key)
-		rowInfo = C.grngo_table_insert_int32(table.db.ctx, table.obj, grnKey)
-	case Int64:
-		grnKey := C.int64_t(key)
-		rowInfo = C.grngo_table_insert_int64(table.db.ctx, table.obj, grnKey)
-	case UInt8:
-		grnKey := C.uint8_t(key)
-		rowInfo = C.grngo_table_insert_uint8(table.db.ctx, table.obj, grnKey)
-	case UInt16:
-		grnKey := C.uint16_t(key)
-		rowInfo = C.grngo_table_insert_uint16(table.db.ctx, table.obj, grnKey)
-	case UInt32:
-		grnKey := C.uint32_t(key)
-		rowInfo = C.grngo_table_insert_uint32(table.db.ctx, table.obj, grnKey)
-	case UInt64:
-		grnKey := C.uint64_t(key)
-		rowInfo = C.grngo_table_insert_uint64(table.db.ctx, table.obj, grnKey)
-	case Time:
-		grnKey := C.int64_t(key)
-		rowInfo = C.grngo_table_insert_time(table.db.ctx, table.obj, grnKey)
-	default:
-		return false, NilID, newInvalidKeyTypeError(table.keyType, LazyInt)
-	}
-	if rowInfo.id == C.GRN_ID_NIL {
-		return false, NilID, fmt.Errorf("grngo_table_insert_int*() failed")
-	}
-	return rowInfo.inserted == C.GRN_TRUE, uint32(rowInfo.id), nil
-}
-
-// insertFloat inserts a row with Float key.
-func (table *Table) insertFloat(key float64) (bool, uint32, error) {
-	if table.keyType != Float {
-		return false, NilID, newInvalidKeyTypeError(table.keyType, Float)
-	}
-	grnKey := C.double(key)
-	rowInfo := C.grngo_table_insert_float(table.db.ctx, table.obj, grnKey)
-	if rowInfo.id == C.GRN_ID_NIL {
-		return false, NilID, fmt.Errorf("grngo_table_insert_float() failed")
-	}
-	return rowInfo.inserted == C.GRN_TRUE, uint32(rowInfo.id), nil
-}
-
-// insertText inserts a row with Text key.
-func (table *Table) insertText(key []byte) (bool, uint32, error) {
-	if table.keyType != ShortText {
-		return false, NilID, newInvalidKeyTypeError(table.keyType, Text)
-	}
-	var grnKey C.grngo_text
-	if len(key) != 0 {
-		grnKey.ptr = (*C.char)(unsafe.Pointer(&key[0]))
-		grnKey.size = C.size_t(len(key))
-	}
-	rowInfo := C.grngo_table_insert_text(table.db.ctx, table.obj, &grnKey)
-	if rowInfo.id == C.GRN_ID_NIL {
-		return false, NilID, fmt.Errorf("grngo_table_insert_text() failed")
-	}
-	return rowInfo.inserted == C.GRN_TRUE, uint32(rowInfo.id), nil
-}
-
-// insertGeoPoint inserts a row with GeoPoint key.
-func (table *Table) insertGeoPoint(key GeoPoint) (bool, uint32, error) {
-	switch table.keyType {
-	case TokyoGeoPoint, WGS84GeoPoint:
-	default:
-		return false, NilID, newInvalidKeyTypeError(table.keyType, LazyGeoPoint)
-	}
-	grnKey := C.grn_geo_point{C.int(key.Latitude), C.int(key.Longitude)}
-	rowInfo := C.grngo_table_insert_geo_point(table.db.ctx, table.obj, grnKey)
-	if rowInfo.id == C.GRN_ID_NIL {
-		return false, NilID, fmt.Errorf("grngo_table_insert_geo_point() failed")
-	}
-	return rowInfo.inserted == C.GRN_TRUE, uint32(rowInfo.id), nil
-}
-
 // InsertRow finds or inserts a row.
 func (table *Table) InsertRow(key interface{}) (inserted bool, id uint32, err error) {
-	switch value := key.(type) {
+	var rc C.grn_rc = C.GRN_SUCCESS
+	var tmpInserted C.grn_bool
+	var tmpID C.grn_id
+	var opName string
+	switch key := key.(type) {
 	case nil:
-		return table.insertVoid()
+		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()"
+		}
 	case bool:
-		return table.insertBool(value)
+		if table.keyType != Bool {
+			return false, NilID, newInvalidKeyTypeError(table.keyType, Bool)
+		}
+		tmpKey := C.grn_bool(C.GRN_FALSE)
+		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()"
+		}
 	case int64:
-		return table.insertInt(value)
+		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()"
+		}
 	case float64:
-		return table.insertFloat(value)
+		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()"
+		}
 	case []byte:
-		return table.insertText(value)
+		if table.keyType != ShortText {
+			return false, NilID, newInvalidKeyTypeError(table.keyType, Text)
+		}
+		var tmpKey C.grngo_text
+		if len(key) != 0 {
+			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()"
+		}
 	case GeoPoint:
-		return table.insertGeoPoint(value)
+		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()"
+		}
 	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)
+	}
+	return tmpInserted == C.GRN_TRUE, uint32(tmpID), nil
 }
 
 // SetValue assigns a value.

  Modified: grngo.h (+21 -38)
===================================================================
--- grngo.h    2015-07-10 17:27:49 +0900 (764773e)
+++ grngo.h    2015-07-13 17:19:29 +0900 (db88cae)
@@ -55,44 +55,27 @@ 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_id   id;       // Row ID, GRN_ID_NIL means the info is invalid.
-  grn_bool inserted; // Inserted or not.
-} grngo_row_info;
-
-// grngo_table_insert_void() inserts an empty row.
-grngo_row_info grngo_table_insert_void(grn_ctx *ctx, grn_obj *table);
-// grngo_table_insert_bool() inserts a row with Bool key.
-grngo_row_info grngo_table_insert_bool(grn_ctx *ctx, grn_obj *table,
-                                       grn_bool key);
-// grngo_table_insert_int*() inserts a row with Int key.
-grngo_row_info grngo_table_insert_int8(grn_ctx *ctx, grn_obj *table,
-                                       int8_t key);
-grngo_row_info grngo_table_insert_int16(grn_ctx *ctx, grn_obj *table,
-                                        int16_t key);
-grngo_row_info grngo_table_insert_int32(grn_ctx *ctx, grn_obj *table,
-                                        int32_t key);
-grngo_row_info grngo_table_insert_int64(grn_ctx *ctx, grn_obj *table,
-                                        int64_t key);
-grngo_row_info grngo_table_insert_uint8(grn_ctx *ctx, grn_obj *table,
-                                        uint8_t key);
-grngo_row_info grngo_table_insert_uint16(grn_ctx *ctx, grn_obj *table,
-                                         uint16_t key);
-grngo_row_info grngo_table_insert_uint32(grn_ctx *ctx, grn_obj *table,
-                                         uint32_t key);
-grngo_row_info grngo_table_insert_uint64(grn_ctx *ctx, grn_obj *table,
-                                         uint64_t key);
-grngo_row_info grngo_table_insert_time(grn_ctx *ctx, grn_obj *table,
-                                       int64_t key);
-// grngo_table_insert_float() inserts a row with Float key.
-grngo_row_info grngo_table_insert_float(grn_ctx *ctx, grn_obj *table,
-                                        double key);
-// grngo_table_insert_text() inserts a row with Text key.
-grngo_row_info grngo_table_insert_text(grn_ctx *ctx, grn_obj *table,
-                                       const grngo_text *key);
-// grngo_table_insert_geo_point() inserts a row with GeoPoint key.
-grngo_row_info grngo_table_insert_geo_point(grn_ctx *ctx, grn_obj *table,
-                                            grn_geo_point key);
+// 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_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_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_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_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_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_column_set_bool() assigns a Bool value.
 grn_bool grngo_column_set_bool(grn_ctx *ctx, grn_obj *column,
-------------- next part --------------
HTML����������������������������...
ダウンロード 



More information about the Groonga-commit mailing list
アーカイブの一覧に戻る