[Groonga-commit] groonga/grngo at 1002459 [master] Add error handling to grngo_column_get_value_info().

アーカイブの一覧に戻る

susumu.yata null+****@clear*****
Tue Jul 14 09:00:59 JST 2015


susumu.yata	2015-07-14 09:00:59 +0900 (Tue, 14 Jul 2015)

  New Revision: 1002459cc9cecdb3d73c2fa5be3a47268f1ff712
  https://github.com/groonga/grngo/commit/1002459cc9cecdb3d73c2fa5be3a47268f1ff712

  Message:
    Add error handling to grngo_column_get_value_info().
    
    GitHub: #12

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

  Modified: grngo.c (+18 -19)
===================================================================
--- grngo.c    2015-07-14 08:41:56 +0900 (793e66b)
+++ grngo.c    2015-07-14 09:00:59 +0900 (f47bc29)
@@ -98,20 +98,18 @@ grn_rc grngo_table_get_value_info(grn_ctx *ctx, grn_obj *table,
   return GRN_SUCCESS;
 }
 
-// grngo_init_type_info() initializes the members of type_info.
-// The initialized type info specifies a valid Void type.
-static void grngo_init_type_info(grngo_type_info *type_info) {
+static void grngo_column_type_info_init(grngo_column_type_info *type_info) {
   type_info->data_type = GRN_DB_VOID;
-  type_info->dimension = 0;
+  type_info->is_vector = GRN_FALSE;
   type_info->ref_table = NULL;
 }
 
-grn_bool grngo_column_get_value_info(grn_ctx *ctx, grn_obj *column,
-                                     grngo_type_info *value_info) {
-  grngo_init_type_info(value_info);
-  if (!column) {
-    return GRN_FALSE;
+grn_rc grngo_column_get_value_info(grn_ctx *ctx, grn_obj *column,
+                                   grngo_column_type_info *value_info) {
+  if (!ctx || !column || !value_info) {
+    return GRN_INVALID_ARGUMENT;
   }
+  grngo_column_type_info_init(value_info);
   switch (column->header.type) {
     case GRN_COLUMN_FIX_SIZE: {
       break;
@@ -119,27 +117,28 @@ grn_bool grngo_column_get_value_info(grn_ctx *ctx, grn_obj *column,
     case GRN_COLUMN_VAR_SIZE: {
       grn_obj_flags type = column->header.flags & GRN_OBJ_COLUMN_TYPE_MASK;
       if (type == GRN_OBJ_COLUMN_VECTOR) {
-        ++value_info->dimension;
+        value_info->is_vector = GRN_TRUE;
       }
       break;
     }
     default: {
-      // The object is not a data column.
-      return GRN_FALSE;
+      return GRN_INVALID_ARGUMENT;
     }
   }
   grn_id range = grn_obj_get_range(ctx, column);
   if (range <= GRNGO_MAX_BUILTIN_TYPE_ID) {
     value_info->data_type = range;
-    return GRN_TRUE;
+    return GRN_SUCCESS;
   }
-  value_info->ref_table = grn_ctx_at(ctx, range);
-  grngo_table_type_info key_info;
-  if (grngo_table_get_key_info(ctx, value_info->ref_table, &key_info) != GRN_SUCCESS) {
-    return GRN_FALSE;
+  grn_obj *ref_table = grn_ctx_at(ctx, range);
+  if (!ref_table || !grn_obj_is_table(ctx, ref_table)) {
+    if (ctx->rc != GRN_SUCCESS) {
+      return ctx->rc;
+    }
+    return GRN_UNKNOWN_ERROR;
   }
-  value_info->data_type = key_info.data_type;
-  return GRN_TRUE;
+  value_info->ref_table = ref_table;
+  return GRN_SUCCESS;
 }
 
 // grngo_table_insertion_error generates an error result.

  Modified: grngo.go (+8 -10)
===================================================================
--- grngo.go    2015-07-14 08:41:56 +0900 (98c63b3)
+++ grngo.go    2015-07-14 09:00:59 +0900 (4904d12)
@@ -1058,19 +1058,16 @@ func (table *Table) findColumn(name string) (*Column, error) {
 		valueType = table.valueType
 		valueTable = table.valueTable
 	default:
-		var valueInfo C.grngo_type_info
-		if ok := C.grngo_column_get_value_info(table.db.ctx, obj, &valueInfo); ok != C.GRN_TRUE {
-			return nil, fmt.Errorf("grngo_column_get_value_info() failed: name = <%s>",
-				name)
-		}
 		// Check the value type.
+		var valueInfo C.grngo_column_type_info
+		rc := C.grngo_column_get_value_info(table.db.ctx, obj, &valueInfo)
+		if rc != C.GRN_SUCCESS {
+			return nil, newGrnError("grngo_column_get_value_info", &rc, table.db.ctx)
+		}
 		valueType = DataType(valueInfo.data_type)
-		isVector = valueInfo.dimension > 0
-		// Find the destination table if the value is table reference.
+		isVector = valueInfo.is_vector == C.GRN_TRUE
 		if valueInfo.ref_table != nil {
-			if valueType == Void {
-				return nil, fmt.Errorf("reference to void: name = <%s>", name)
-			}
+			defer C.grn_obj_unlink(table.db.ctx, valueInfo.ref_table)
 			var cValueTableName *C.char
 			rc := C.grngo_table_get_name(table.db.ctx, valueInfo.ref_table, &cValueTableName)
 			if rc != C.GRN_SUCCESS {
@@ -1082,6 +1079,7 @@ func (table *Table) findColumn(name string) (*Column, error) {
 			if err != nil {
 				return nil, err
 			}
+			valueType = valueTable.keyType
 		}
 	}
 	column := newColumn(table, obj, name, valueType, isVector, valueTable)

  Modified: grngo.h (+17 -13)
===================================================================
--- grngo.h    2015-07-14 08:41:56 +0900 (d3a665b)
+++ grngo.h    2015-07-14 09:00:59 +0900 (b8b06be)
@@ -31,29 +31,33 @@ typedef struct {
   grn_obj          *ref_table; // The referenced table of table reference.
 } grngo_table_type_info;
 
-typedef struct {
-  grn_builtin_type data_type;  // Data type (GRN_DB_VOID, GRN_DB_BOOL, etc.).
-                               // If the type is table reference, the key type
-                               // of the referenced table is stored.
-  int              dimension;  // Vector depth, 0 means the type is scalar.
-  grn_obj          *ref_table; // The referenced table of table reference.
-} grngo_type_info;
-
-// grngo_table_get_key_info gets information of the table key (_key).
+// grngo_table_get_key_info gets information of the table keys (_key).
 //
 // Note that key_info->ref_table should be unlinked by grn_obj_unlink() if it
 // is not NULL.
 grn_rc grngo_table_get_key_info(grn_ctx *ctx, grn_obj *table,
                                 grngo_table_type_info *key_info);
-// grngo_table_get_value_info gets information of the table value (_value).
+// grngo_table_get_value_info gets information of the table values (_value).
+//
 // Note that value_info->ref_table should be unlinked by grn_obj_unlink() if it
 // is not NULL.
 grn_rc grngo_table_get_value_info(grn_ctx *ctx, grn_obj *table,
                                   grngo_table_type_info *value_info);
 
-// grngo_column_get_value_info() gets information of the column value.
-grn_bool grngo_column_get_value_info(grn_ctx *ctx, grn_obj *column,
-                                     grngo_type_info *value_info);
+typedef struct {
+  grn_builtin_type data_type;  // Data type (GRN_DB_VOID, GRN_DB_BOOL, etc.).
+                               // If the type is table reference, GRN_DB_VOID
+                               // is stored.
+  grn_bool         is_vector;  // Whether or not the data type is vector.
+  grn_obj          *ref_table; // The referenced table of table reference.
+} grngo_column_type_info;
+
+// grngo_column_get_value_info() gets information of the column values.
+//
+// Note that value_info->ref_table should be unlinked by grn_obj_unlink() if it
+// is not NULL.
+grn_rc grngo_column_get_value_info(grn_ctx *ctx, grn_obj *column,
+                                   grngo_column_type_info *value_info);
 
 typedef struct {
   grn_rc   rc;       // rc stores a return code.
-------------- next part --------------
HTML����������������������������...
ダウンロード 



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