[Groonga-commit] groonga/grngo at f906004 [master] Add error handling for grngo_table_get_name().

Back to archive index

susumu.yata null+****@clear*****
Fri Jul 10 15:27:46 JST 2015


susumu.yata	2015-07-10 15:27:46 +0900 (Fri, 10 Jul 2015)

  New Revision: f906004ebebcb7208f1cbec19e6ed94ba69100a0
  https://github.com/groonga/grngo/commit/f906004ebebcb7208f1cbec19e6ed94ba69100a0

  Message:
    Add error handling for grngo_table_get_name().
    
    GitHub: #12

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

  Modified: grngo.c (+34 -30)
===================================================================
--- grngo.c    2015-07-10 14:58:39 +0900 (c5a5126)
+++ grngo.c    2015-07-10 15:27:46 +0900 (d1d65a9)
@@ -32,6 +32,40 @@ grn_rc grngo_find_table(grn_ctx *ctx, const char *name, size_t name_len,
   }
 }
 
+grn_rc grngo_table_get_name(grn_ctx *ctx, grn_obj *table, char **name) {
+  if (!ctx || !table || !name) {
+    return GRN_INVALID_ARGUMENT;
+  }
+  switch (table->header.type) {
+    case GRN_TABLE_HASH_KEY:
+    case GRN_TABLE_PAT_KEY:
+    case GRN_TABLE_DAT_KEY:
+    case GRN_TABLE_NO_KEY: {
+      break;
+    }
+    default: {
+      // The object is not a table.
+      return GRN_INVALID_FORMAT;
+    }
+  }
+  char buf[GRN_TABLE_MAX_KEY_SIZE];
+  int len = grn_obj_name(ctx, table, buf, GRN_TABLE_MAX_KEY_SIZE);
+  if (len <= 0) {
+    if (ctx->rc != GRN_SUCCESS) {
+      return ctx->rc;
+    }
+    return GRN_UNKNOWN_ERROR;
+  }
+  char *table_name = (char *)malloc(len + 1);
+  if (!table_name) {
+    return GRN_NO_MEMORY_AVAILABLE;
+  }
+  memcpy(table_name, buf, len);
+  table_name[len] = '\0';
+  *name = table_name;
+  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) {
@@ -141,36 +175,6 @@ grn_bool grngo_column_get_value_info(grn_ctx *ctx, grn_obj *column,
   return GRN_TRUE;
 }
 
-char *grngo_table_get_name(grn_ctx *ctx, grn_obj *table) {
-  if (!table) {
-    return NULL;
-  }
-  switch (table->header.type) {
-    case GRN_TABLE_HASH_KEY:
-    case GRN_TABLE_PAT_KEY:
-    case GRN_TABLE_DAT_KEY:
-    case GRN_TABLE_NO_KEY: {
-      break;
-    }
-    default: {
-      // The object is not a table.
-      return NULL;
-    }
-  }
-  char buf[GRN_TABLE_MAX_KEY_SIZE];
-  int len = grn_obj_name(ctx, table, buf, GRN_TABLE_MAX_KEY_SIZE);
-  if (len <= 0) {
-    return NULL;
-  }
-  char *table_name = (char *)malloc(len + 1);
-  if (!table_name) {
-    return NULL;
-  }
-  memcpy(table_name, buf, len);
-  table_name[len] = '\0';
-  return table_name;
-}
-
 // 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) {

  Modified: grngo.go (+12 -9)
===================================================================
--- grngo.go    2015-07-10 14:58:39 +0900 (e2c61be)
+++ grngo.go    2015-07-10 15:27:46 +0900 (24c33f7)
@@ -733,9 +733,10 @@ func (db *DB) FindTable(name string) (*Table, error) {
 		if keyType == Void {
 			return nil, fmt.Errorf("reference to void: name = <%s>", name)
 		}
-		cKeyTableName := C.grngo_table_get_name(db.ctx, keyInfo.ref_table)
-		if cKeyTableName == nil {
-			return nil, fmt.Errorf("grngo_table_get_name() failed")
+		var cKeyTableName *C.char
+		rc := C.grngo_table_get_name(db.ctx, keyInfo.ref_table, &cKeyTableName)
+		if rc != C.GRN_SUCCESS {
+			return nil, newGrnError("grngo_table_get_name()", &rc, db.ctx)
 		}
 		defer C.free(unsafe.Pointer(cKeyTableName))
 		var err error
@@ -756,9 +757,10 @@ func (db *DB) FindTable(name string) (*Table, error) {
 		if valueType == Void {
 			return nil, fmt.Errorf("reference to void: name = <%s>", name)
 		}
-		cValueTableName := C.grngo_table_get_name(db.ctx, valueInfo.ref_table)
-		if cValueTableName == nil {
-			return nil, fmt.Errorf("grngo_table_get_name() failed")
+		var cValueTableName *C.char
+		rc := C.grngo_table_get_name(db.ctx, valueInfo.ref_table, &cValueTableName)
+		if rc != C.GRN_SUCCESS {
+			return nil, newGrnError("grngo_table_get_name()", &rc, db.ctx)
 		}
 		defer C.free(unsafe.Pointer(cValueTableName))
 		var err error
@@ -1120,9 +1122,10 @@ func (table *Table) findColumn(name string) (*Column, error) {
 			if valueType == Void {
 				return nil, fmt.Errorf("reference to void: name = <%s>", name)
 			}
-			cValueTableName := C.grngo_table_get_name(table.db.ctx, valueInfo.ref_table)
-			if cValueTableName == nil {
-				return nil, fmt.Errorf("grngo_table_get_name() failed")
+			var cValueTableName *C.char
+			rc := C.grngo_table_get_name(table.db.ctx, valueInfo.ref_table, &cValueTableName)
+			if rc != C.GRN_SUCCESS {
+				return nil, newGrnError("grngo_table_get_name()", &rc, table.db.ctx)
 			}
 			defer C.free(unsafe.Pointer(cValueTableName))
 			var err error

  Modified: grngo.h (+4 -5)
===================================================================
--- grngo.h    2015-07-10 14:58:39 +0900 (83afe21)
+++ grngo.h    2015-07-10 15:27:46 +0900 (9dac671)
@@ -19,6 +19,10 @@ typedef struct {
 // grngo_find_table finds a table.
 grn_rc grngo_find_table(grn_ctx *ctx, const char *name, size_t name_len,
                         grn_obj **table);
+// grngo_table_get_name gets the name (zero-terminated) of a table.
+// The address of the name is written to **name.
+// Note that the name must be freed by free().
+grn_rc grngo_table_get_name(grn_ctx *ctx, grn_obj *table, char **name);
 
 typedef struct {
   grn_builtin_type data_type;  // Data type (GRN_DB_VOID, GRN_DB_BOOL, etc.).
@@ -38,11 +42,6 @@ grn_bool 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);
 
-// grngo_table_get_name() returns the name of table.
-// On success, a non-NULL pointer is returned and it must be freed by free().
-// On failure, NULL is returned.
-char *grngo_table_get_name(grn_ctx *ctx, grn_obj *table);
-
 typedef struct {
   grn_id   id;       // Row ID, GRN_ID_NIL means the info is invalid.
   grn_bool inserted; // Inserted or not.
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index