[Groonga-commit] groonga/groonga at 807c076 [master] load: support --output_errors yes

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Feb 3 17:03:31 JST 2017


Kouhei Sutou	2017-02-03 17:03:31 +0900 (Fri, 03 Feb 2017)

  New Revision: 807c07614a925de642a4a8e9c8619bd61c2926c5
  https://github.com/groonga/groonga/commit/807c07614a925de642a4a8e9c8619bd61c2926c5

  Message:
    load: support --output_errors yes
    
    It requires command version 3.
    
    If you specify "yes", you can get errors for each load failed record.

  Added files:
    test/command/suite/load/command_version/3/output_errors/array.expected
    test/command/suite/load/command_version/3/output_errors/array.test
    test/command/suite/load/command_version/3/output_errors/object.expected
    test/command/suite/load/command_version/3/output_errors/object.test
  Modified files:
    lib/ctx.c
    lib/grn_ctx_impl.h
    lib/grn_load.h
    lib/load.c
    lib/proc.c

  Modified: lib/ctx.c (+6 -1)
===================================================================
--- lib/ctx.c    2017-02-03 16:54:29 +0900 (dd3ce12)
+++ lib/ctx.c    2017-02-03 17:03:31 +0900 (916a8eb)
@@ -1,6 +1,6 @@
 /* -*- c-basic-offset: 2 -*- */
 /*
-  Copyright(C) 2009-2016 Brazil
+  Copyright(C) 2009-2017 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -148,6 +148,8 @@ grn_loader_init(grn_loader *loader)
   GRN_UINT32_INIT(&loader->level, GRN_OBJ_VECTOR);
   GRN_PTR_INIT(&loader->columns, GRN_OBJ_VECTOR, GRN_ID_NIL);
   GRN_UINT32_INIT(&loader->ids, GRN_OBJ_VECTOR);
+  GRN_INT32_INIT(&loader->return_codes, GRN_OBJ_VECTOR);
+  GRN_TEXT_INIT(&loader->error_messages, GRN_OBJ_VECTOR);
   loader->id_offset = -1;
   loader->key_offset = -1;
   loader->table = NULL;
@@ -161,6 +163,7 @@ grn_loader_init(grn_loader *loader)
   loader->rc = GRN_SUCCESS;
   loader->errbuf[0] = '\0';
   loader->output_ids = GRN_FALSE;
+  loader->output_errors = GRN_FALSE;
 }
 
 void
@@ -179,6 +182,8 @@ grn_ctx_loader_clear(grn_ctx *ctx)
   GRN_OBJ_FIN(ctx, &loader->level);
   GRN_OBJ_FIN(ctx, &loader->columns);
   GRN_OBJ_FIN(ctx, &loader->ids);
+  GRN_OBJ_FIN(ctx, &loader->return_codes);
+  GRN_OBJ_FIN(ctx, &loader->error_messages);
   grn_loader_init(loader);
 }
 

  Modified: lib/grn_ctx_impl.h (+3 -0)
===================================================================
--- lib/grn_ctx_impl.h    2017-02-03 16:54:29 +0900 (dc01827)
+++ lib/grn_ctx_impl.h    2017-02-03 17:03:31 +0900 (2d72065)
@@ -75,6 +75,8 @@ typedef struct {
   grn_obj level;
   grn_obj columns;
   grn_obj ids;
+  grn_obj return_codes;
+  grn_obj error_messages;
   uint32_t emit_level;
   int32_t id_offset;  /* Position of _id in values or -1 if _id is N/A. */
   int32_t key_offset; /* Position of _key in values or -1 if _key is N/A. */
@@ -91,6 +93,7 @@ typedef struct {
   grn_rc rc;
   char errbuf[GRN_CTX_MSGSIZE];
   grn_bool output_ids;
+  grn_bool output_errors;
 } grn_loader;
 
 #define GRN_CTX_N_SEGMENTS 512

  Modified: lib/grn_load.h (+1 -0)
===================================================================
--- lib/grn_load.h    2017-02-03 16:54:29 +0900 (1f0331d)
+++ lib/grn_load.h    2017-02-03 17:03:31 +0900 (b4345c9)
@@ -36,6 +36,7 @@ typedef struct grn_load_input_ {
   grn_raw_string if_exists;
   grn_raw_string each;
   grn_bool output_ids;
+  grn_bool output_errors;
   uint32_t emit_level;
 } grn_load_input;
 

  Modified: lib/load.c (+27 -2)
===================================================================
--- lib/load.c    2017-02-03 16:54:29 +0900 (6c9f204)
+++ lib/load.c    2017-02-03 17:03:31 +0900 (5997210)
@@ -294,6 +294,7 @@ bracket_close(grn_ctx *ctx, grn_loader *loader)
   uint32_t ncols;   /* Number of columns except _id and _key. */
   uint32_t nvalues; /* Number of values in brackets. */
   uint32_t depth;
+  grn_bool is_record_load = GRN_FALSE;
 
   cols = (grn_obj **)GRN_BULK_HEAD(&loader->columns);
   ncols = GRN_BULK_VSIZE(&loader->columns) / sizeof(grn_obj *);
@@ -404,6 +405,8 @@ bracket_close(grn_ctx *ctx, grn_loader *loader)
     goto exit;
   }
 
+  is_record_load = GRN_TRUE;
+
   /* Target columns and _id or _key are already specified. */
   if (!nvalues) {
     /*
@@ -473,8 +476,19 @@ bracket_close(grn_ctx *ctx, grn_loader *loader)
   }
   loader->nrecords++;
 exit:
-  if (depth > 0 && loader->output_ids) {
-    GRN_UINT32_PUT(ctx, &(loader->ids), id);
+  if (is_record_load) {
+    if (loader->output_ids) {
+      GRN_UINT32_PUT(ctx, &(loader->ids), id);
+    }
+    if (loader->output_errors) {
+      GRN_INT32_PUT(ctx, &(loader->return_codes), ctx->rc);
+      grn_vector_add_element(ctx,
+                             &(loader->error_messages),
+                             ctx->errbuf,
+                             strlen(ctx->errbuf),
+                             0,
+                             GRN_DB_TEXT);
+    }
   }
   loader->values_size = begin;
 }
@@ -632,6 +646,15 @@ exit:
   if (loader->output_ids) {
     GRN_UINT32_PUT(ctx, &(loader->ids), id);
   }
+  if (loader->output_errors) {
+    GRN_INT32_PUT(ctx, &(loader->return_codes), ctx->rc);
+    grn_vector_add_element(ctx,
+                           &(loader->error_messages),
+                           ctx->errbuf,
+                           strlen(ctx->errbuf),
+                           0,
+                           GRN_DB_TEXT);
+  }
   loader->values_size = begin;
 }
 
@@ -1115,6 +1138,7 @@ grn_load_internal(grn_ctx *ctx, grn_load_input *input)
       }
     }
     loader->output_ids = input->output_ids;
+    loader->output_errors = input->output_errors;
   } else {
     if (!loader->table) {
       ERR(GRN_INVALID_ARGUMENT, "mandatory \"table\" parameter is absent");
@@ -1165,6 +1189,7 @@ grn_load(grn_ctx *ctx, grn_content_type input_type,
     input.each.value = each;
     input.each.length = each_len;
     input.output_ids = GRN_FALSE;
+    input.output_errors = GRN_FALSE;
     input.emit_level = 1;
     grn_load_internal(ctx, &input);
   }

  Modified: lib/proc.c (+37 -1)
===================================================================
--- lib/proc.c    2017-02-03 16:54:29 +0900 (7d36b4f)
+++ lib/proc.c    2017-02-03 17:03:31 +0900 (ed68742)
@@ -160,6 +160,10 @@ proc_load(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
                                                   user_data,
                                                   "output_ids", -1,
                                                   GRN_FALSE);
+  input.output_errors = grn_plugin_proc_get_var_bool(ctx,
+                                                     user_data,
+                                                     "output_errors", -1,
+                                                     GRN_FALSE);
   input.emit_level = 1;
 
   grn_load_internal(ctx, &input);
@@ -181,6 +185,9 @@ proc_load(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
       if (ctx->impl->loader.output_ids) {
         n_elements++;
       }
+      if (ctx->impl->loader.output_errors) {
+        n_elements++;
+      }
       GRN_OUTPUT_MAP_OPEN("result", n_elements);
       GRN_OUTPUT_CSTR("n_loaded_records");
       GRN_OUTPUT_INT64(ctx->impl->loader.nrecords);
@@ -196,6 +203,34 @@ proc_load(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
         }
         GRN_OUTPUT_ARRAY_CLOSE();
       }
+      if (ctx->impl->loader.output_errors) {
+        grn_obj *return_codes = &(ctx->impl->loader.return_codes);
+        grn_obj *error_messages = &(ctx->impl->loader.error_messages);
+        int i, n;
+
+        GRN_OUTPUT_CSTR("errors");
+        n = GRN_BULK_VSIZE(return_codes) / sizeof(int32_t);
+        GRN_OUTPUT_ARRAY_OPEN("errors", n);
+        for (i = 0; i < n; i++) {
+          const char *message;
+          unsigned int message_size;
+
+          message_size = grn_vector_get_element(ctx,
+                                                error_messages,
+                                                i,
+                                                &message,
+                                                NULL,
+                                                NULL);
+
+          GRN_OUTPUT_MAP_OPEN("error", 2);
+          GRN_OUTPUT_CSTR("return_code");
+          GRN_OUTPUT_INT64(GRN_INT32_VALUE_AT(return_codes, i));
+          GRN_OUTPUT_CSTR("message");
+          GRN_OUTPUT_STR(message, message_size);
+          GRN_OUTPUT_MAP_CLOSE();
+        }
+        GRN_OUTPUT_ARRAY_CLOSE();
+      }
       GRN_OUTPUT_MAP_CLOSE();
     } else {
       GRN_OUTPUT_INT64(ctx->impl->loader.nrecords);
@@ -3675,7 +3710,8 @@ grn_db_init_builtin_commands(grn_ctx *ctx)
   DEF_VAR(vars[4], "input_type");
   DEF_VAR(vars[5], "each");
   DEF_VAR(vars[6], "output_ids");
-  DEF_COMMAND("load", proc_load, 7, vars);
+  DEF_VAR(vars[7], "output_errors");
+  DEF_COMMAND("load", proc_load, 8, vars);
 
   DEF_COMMAND("status", proc_status, 0, vars);
 

  Added: test/command/suite/load/command_version/3/output_errors/array.expected (+45 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/load/command_version/3/output_errors/array.expected    2017-02-03 17:03:31 +0900 (68355ba)
@@ -0,0 +1,45 @@
+table_create Numbers TABLE_HASH_KEY UInt32
+[[0,0.0,0.0],true]
+load --table Numbers --command_version 3 --output_ids yes --output_errors yes
+[
+["_key"],
+[1],
+["String"],
+[-1]
+]
+{
+  "header": {
+    "return_code": -22,
+    "start_time": 0.0,
+    "elapsed_time": 0.0,
+    "error": {
+      "message": "cast failed",
+      "function": "grn_table_add_by_key",
+      "file": "db.c",
+      "line": 1574
+    }
+  },
+  "body": {
+    "n_loaded_records": 2,
+    "loaded_ids": [
+      1,
+      0,
+      2
+    ],
+    "errors": [
+      {
+        "return_code": 0,
+        "message": ""
+      },
+      {
+        "return_code": -22,
+        "message": "cast failed"
+      },
+      {
+        "return_code": 0,
+        "message": ""
+      }
+    ]
+  }
+}
+#|e| cast failed

  Added: test/command/suite/load/command_version/3/output_errors/array.test (+9 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/load/command_version/3/output_errors/array.test    2017-02-03 17:03:31 +0900 (81f4475)
@@ -0,0 +1,9 @@
+table_create Numbers TABLE_HASH_KEY UInt32
+
+load --table Numbers --command_version 3 --output_ids yes --output_errors yes
+[
+["_key"],
+[1],
+["String"],
+[-1]
+]

  Added: test/command/suite/load/command_version/3/output_errors/object.expected (+44 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/load/command_version/3/output_errors/object.expected    2017-02-03 17:03:31 +0900 (2322133)
@@ -0,0 +1,44 @@
+table_create Numbers TABLE_HASH_KEY UInt32
+[[0,0.0,0.0],true]
+load --table Numbers --command_version 3 --output_ids yes --output_errors yes
+[
+{"_key": 1},
+{"_key": "String"},
+{"_key": -1}
+]
+{
+  "header": {
+    "return_code": -22,
+    "start_time": 0.0,
+    "elapsed_time": 0.0,
+    "error": {
+      "message": "cast failed",
+      "function": "grn_table_add_by_key",
+      "file": "db.c",
+      "line": 1574
+    }
+  },
+  "body": {
+    "n_loaded_records": 2,
+    "loaded_ids": [
+      1,
+      0,
+      2
+    ],
+    "errors": [
+      {
+        "return_code": 0,
+        "message": ""
+      },
+      {
+        "return_code": -22,
+        "message": "cast failed"
+      },
+      {
+        "return_code": 0,
+        "message": ""
+      }
+    ]
+  }
+}
+#|e| cast failed

  Added: test/command/suite/load/command_version/3/output_errors/object.test (+8 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/load/command_version/3/output_errors/object.test    2017-02-03 17:03:31 +0900 (de15168)
@@ -0,0 +1,8 @@
+table_create Numbers TABLE_HASH_KEY UInt32
+
+load --table Numbers --command_version 3 --output_ids yes --output_errors yes
+[
+{"_key": 1},
+{"_key": "String"},
+{"_key": -1}
+]
-------------- next part --------------
HTML����������������������������...
Descargar 



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