[Groonga-commit] groonga/grngo at 95ab8b6 [master] Add error handling for grngo_find_table().

Back to archive index

susumu.yata null+****@clear*****
Fri Jul 10 14:58:39 JST 2015


susumu.yata	2015-07-10 14:58:39 +0900 (Fri, 10 Jul 2015)

  New Revision: 95ab8b6b26cfa4ea193b5538d883133757898b40
  https://github.com/groonga/grngo/commit/95ab8b6b26cfa4ea193b5538d883133757898b40

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

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

  Modified: grngo.c (+13 -4)
===================================================================
--- grngo.c    2015-07-10 14:49:04 +0900 (b182d6d)
+++ grngo.c    2015-07-10 14:58:39 +0900 (c5a5126)
@@ -4,21 +4,30 @@
 
 #define GRNGO_MAX_DATA_TYPE_ID GRN_DB_WGS84_GEO_POINT
 
-grn_obj *grngo_find_table(grn_ctx *ctx, const char *name, int name_len) {
+grn_rc grngo_find_table(grn_ctx *ctx, const char *name, size_t name_len,
+                        grn_obj **table) {
+  if (!ctx || !name || !table) {
+    return GRN_INVALID_ARGUMENT;
+  }
   grn_obj *obj = grn_ctx_get(ctx, name, name_len);
   if (!obj) {
-    return NULL;
+    if (ctx->rc != GRN_SUCCESS) {
+      return ctx->rc;
+    }
+    return GRN_UNKNOWN_ERROR;
   }
   switch (obj->header.type) {
     case GRN_TABLE_HASH_KEY:
     case GRN_TABLE_PAT_KEY:
     case GRN_TABLE_DAT_KEY:
     case GRN_TABLE_NO_KEY: {
-      return obj;
+      *table = obj;
+      return GRN_SUCCESS;
     }
     default: {
       // The object is not a table.
-      return NULL;
+      grn_obj_unlink(ctx, obj);
+      return GRN_INVALID_FORMAT;
     }
   }
 }

  Modified: grngo.go (+6 -4)
===================================================================
--- grngo.go    2015-07-10 14:49:04 +0900 (a1626db)
+++ grngo.go    2015-07-10 14:58:39 +0900 (e2c61be)
@@ -528,7 +528,8 @@ func (db *DB) Refresh() error {
 	for _, table := range db.tables {
 		nameBytes := []byte(table.name)
 		cName := (*C.char)(unsafe.Pointer(&nameBytes[0]))
-		tableObj := C.grngo_find_table(db.ctx, cName, C.int(len(nameBytes)))
+		var tableObj *C.grn_obj
+		C.grngo_find_table(db.ctx, cName, C.size_t(len(nameBytes)), &tableObj)
 		if tableObj != table.obj {
 			continue
 		}
@@ -714,9 +715,10 @@ func (db *DB) FindTable(name string) (*Table, error) {
 	if len(nameBytes) != 0 {
 		cName = (*C.char)(unsafe.Pointer(&nameBytes[0]))
 	}
-	obj := C.grngo_find_table(db.ctx, cName, C.int(len(nameBytes)))
-	if obj == nil {
-		return nil, fmt.Errorf("table not found: name = <%s>", name)
+	var obj *C.grn_obj
+	rc := C.grngo_find_table(db.ctx, cName, C.size_t(len(nameBytes)), &obj)
+	if rc != C.GRN_SUCCESS {
+		return nil, newGrnError("grngo_find_table()", &rc, db.ctx)
 	}
 	var keyInfo C.grngo_type_info
 	if ok := C.grngo_table_get_key_info(db.ctx, obj, &keyInfo); ok != C.GRN_TRUE {

  Modified: grngo.h (+3 -4)
===================================================================
--- grngo.h    2015-07-10 14:49:04 +0900 (d959c09)
+++ grngo.h    2015-07-10 14:58:39 +0900 (83afe21)
@@ -16,10 +16,9 @@ typedef struct {
   size_t size;
 } grngo_vector;
 
-// grngo_find_table() finds a table with the given name.
-// If found, an object associated with the table is returned.
-// If not found, NULL is returned.
-grn_obj *grngo_find_table(grn_ctx *ctx, const char *name, int name_len);
+// grngo_find_table finds a table.
+grn_rc grngo_find_table(grn_ctx *ctx, const char *name, size_t name_len,
+                        grn_obj **table);
 
 typedef struct {
   grn_builtin_type data_type;  // Data type (GRN_DB_VOID, GRN_DB_BOOL, etc.).
-------------- next part --------------
HTML����������������������������...
Download 



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