[Groonga-commit] groonga/groonga at 8364c38 [master] load: support "--output_ids yes" parameter

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Dec 6 15:52:57 JST 2016


Kouhei Sutou	2016-12-06 15:52:57 +0900 (Tue, 06 Dec 2016)

  New Revision: 8364c38a2138d5ec4e0cac8da4d0aeeb18f20479
  https://github.com/groonga/groonga/commit/8364c38a2138d5ec4e0cac8da4d0aeeb18f20479

  Message:
    load: support "--output_ids yes" parameter
    
    If "--output_ids yes" is specified and command version is 3 or later,
    loaded record IDs are also returned. ID is 0 for record that is failed
    to add.

  Added files:
    test/command/suite/load/command_version/3/output_ids.expected
    test/command/suite/load/command_version/3/output_ids.test
  Modified files:
    lib/ctx.c
    lib/db.c
    lib/grn_ctx_impl.h
    lib/grn_db.h
    lib/proc.c
    plugins/table/table.c

  Modified: lib/ctx.c (+3 -0)
===================================================================
--- lib/ctx.c    2016-12-06 15:20:44 +0900 (ba31555)
+++ lib/ctx.c    2016-12-06 15:52:57 +0900 (bc70d38)
@@ -147,6 +147,7 @@ grn_loader_init(grn_loader *loader)
   GRN_TEXT_INIT(&loader->values, 0);
   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);
   loader->id_offset = -1;
   loader->key_offset = -1;
   loader->table = NULL;
@@ -159,6 +160,7 @@ grn_loader_init(grn_loader *loader)
   loader->columns_status = GRN_LOADER_COLUMNS_UNSET;
   loader->rc = GRN_SUCCESS;
   loader->errbuf[0] = '\0';
+  loader->output_ids = GRN_FALSE;
 }
 
 void
@@ -176,6 +178,7 @@ grn_ctx_loader_clear(grn_ctx *ctx)
   GRN_OBJ_FIN(ctx, &loader->values);
   GRN_OBJ_FIN(ctx, &loader->level);
   GRN_OBJ_FIN(ctx, &loader->columns);
+  GRN_OBJ_FIN(ctx, &loader->ids);
   grn_loader_init(loader);
 }
 

  Modified: lib/db.c (+14 -1)
===================================================================
--- lib/db.c    2016-12-06 15:20:44 +0900 (0d78a38)
+++ lib/db.c    2016-12-06 15:52:57 +0900 (6b602ac)
@@ -14003,6 +14003,9 @@ bracket_close(grn_ctx *ctx, grn_loader *loader)
   }
   loader->nrecords++;
 exit:
+  if (depth > 0 && loader->output_ids) {
+    GRN_UINT32_PUT(ctx, &(loader->ids), id);
+  }
   loader->values_size = begin;
 }
 
@@ -14156,6 +14159,9 @@ brace_close(grn_ctx *ctx, grn_loader *loader)
   }
   loader->nrecords++;
 exit:
+  if (loader->output_ids) {
+    GRN_UINT32_PUT(ctx, &(loader->ids), id);
+  }
   loader->values_size = begin;
 }
 
@@ -14579,6 +14585,7 @@ grn_load_(grn_ctx *ctx, grn_content_type input_type,
           const char *values, unsigned int values_len,
           const char *ifexists, unsigned int ifexists_len,
           const char *each, unsigned int each_len,
+          grn_obj *output_ids,
           uint32_t emit_level)
 {
   grn_loader *loader = &ctx->impl->loader;
@@ -14630,6 +14637,10 @@ grn_load_(grn_ctx *ctx, grn_content_type input_type,
                        GRN_EXPR_SYNTAX_SCRIPT|GRN_EXPR_ALLOW_UPDATE);
       }
     }
+    if (output_ids && GRN_TEXT_LEN(output_ids) > 0) {
+      loader->output_ids =
+        grn_proc_option_value_bool(ctx, output_ids, GRN_FALSE);
+    }
   } else {
     if (!loader->table) {
       ERR(GRN_INVALID_ARGUMENT, "mandatory \"table\" parameter is absent");
@@ -14669,7 +14680,9 @@ grn_load(grn_ctx *ctx, grn_content_type input_type,
   GRN_API_ENTER;
   grn_load_(ctx, input_type, table, table_len,
             columns, columns_len, values, values_len,
-            ifexists, ifexists_len, each, each_len, 1);
+            ifexists, ifexists_len, each, each_len,
+            NULL,
+            1);
   GRN_API_RETURN(ctx->rc);
 }
 

  Modified: lib/grn_ctx_impl.h (+2 -0)
===================================================================
--- lib/grn_ctx_impl.h    2016-12-06 15:20:44 +0900 (5a29d5e)
+++ lib/grn_ctx_impl.h    2016-12-06 15:52:57 +0900 (dc01827)
@@ -74,6 +74,7 @@ typedef struct {
   grn_obj values;
   grn_obj level;
   grn_obj columns;
+  grn_obj ids;
   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. */
@@ -89,6 +90,7 @@ typedef struct {
   grn_loader_columns_status columns_status;
   grn_rc rc;
   char errbuf[GRN_CTX_MSGSIZE];
+  grn_bool output_ids;
 } grn_loader;
 
 #define GRN_CTX_N_SEGMENTS 512

  Modified: lib/grn_db.h (+1 -0)
===================================================================
--- lib/grn_db.h    2016-12-06 15:20:44 +0900 (2877568)
+++ lib/grn_db.h    2016-12-06 15:52:57 +0900 (d8d5427)
@@ -433,6 +433,7 @@ GRN_API void grn_load_(grn_ctx *ctx, grn_content_type input_type,
                        const char *values, unsigned int values_len,
                        const char *ifexists, unsigned int ifexists_len,
                        const char *each, unsigned int each_len,
+                       grn_obj *output_ids,
                        uint32_t emit_level);
 
 GRN_API grn_rc grn_table_group_with_range_gap(grn_ctx *ctx, grn_obj *table,

  Modified: lib/proc.c (+27 -8)
===================================================================
--- lib/proc.c    2016-12-06 15:20:44 +0900 (30cdb20)
+++ lib/proc.c    2016-12-06 15:52:57 +0900 (8c1c284)
@@ -132,12 +132,14 @@ exit :
 static grn_obj *
 proc_load(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
 {
-  grn_load(ctx, grn_get_ctype(VAR(4)),
-           GRN_TEXT_VALUE(VAR(1)), GRN_TEXT_LEN(VAR(1)),
-           GRN_TEXT_VALUE(VAR(2)), GRN_TEXT_LEN(VAR(2)),
-           GRN_TEXT_VALUE(VAR(0)), GRN_TEXT_LEN(VAR(0)),
-           GRN_TEXT_VALUE(VAR(3)), GRN_TEXT_LEN(VAR(3)),
-           GRN_TEXT_VALUE(VAR(5)), GRN_TEXT_LEN(VAR(5)));
+  grn_load_(ctx, grn_get_ctype(VAR(4)),
+            GRN_TEXT_VALUE(VAR(1)), GRN_TEXT_LEN(VAR(1)),
+            GRN_TEXT_VALUE(VAR(2)), GRN_TEXT_LEN(VAR(2)),
+            GRN_TEXT_VALUE(VAR(0)), GRN_TEXT_LEN(VAR(0)),
+            GRN_TEXT_VALUE(VAR(3)), GRN_TEXT_LEN(VAR(3)),
+            GRN_TEXT_VALUE(VAR(5)), GRN_TEXT_LEN(VAR(5)),
+            VAR(6),
+            1);
   if (ctx->rc == GRN_CANCEL) {
     ctx->impl->loader.stat = GRN_LOADER_END;
     ctx->impl->loader.rc = GRN_SUCCESS;
@@ -152,9 +154,25 @@ proc_load(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
       grn_strcpy(ctx->errbuf, GRN_CTX_MSGSIZE, ctx->impl->loader.errbuf);
     }
     if (grn_ctx_get_command_version(ctx) >= GRN_COMMAND_VERSION_3) {
-      GRN_OUTPUT_MAP_OPEN("RESULT", 1);
+      int n_elements = 1;
+      if (ctx->impl->loader.output_ids) {
+        n_elements++;
+      }
+      GRN_OUTPUT_MAP_OPEN("result", n_elements);
       GRN_OUTPUT_CSTR("n_loaded_records");
       GRN_OUTPUT_INT64(ctx->impl->loader.nrecords);
+      if (ctx->impl->loader.output_ids) {
+        grn_obj *ids = &(ctx->impl->loader.ids);
+        int i, n_ids;
+
+        GRN_OUTPUT_CSTR("ids");
+        n_ids = GRN_BULK_VSIZE(ids) / sizeof(uint32_t);
+        GRN_OUTPUT_ARRAY_OPEN("ids", n_ids);
+        for (i = 0; i < n_ids; i++) {
+          GRN_OUTPUT_UINT64(GRN_UINT32_VALUE_AT(ids, i));
+        }
+        GRN_OUTPUT_ARRAY_CLOSE();
+      }
       GRN_OUTPUT_MAP_CLOSE();
     } else {
       GRN_OUTPUT_INT64(ctx->impl->loader.nrecords);
@@ -3450,7 +3468,8 @@ grn_db_init_builtin_commands(grn_ctx *ctx)
   DEF_VAR(vars[3], "ifexists");
   DEF_VAR(vars[4], "input_type");
   DEF_VAR(vars[5], "each");
-  DEF_COMMAND("load", proc_load, 6, vars);
+  DEF_VAR(vars[6], "output_ids");
+  DEF_COMMAND("load", proc_load, 7, vars);
 
   DEF_COMMAND("status", proc_status, 0, vars);
 

  Modified: plugins/table/table.c (+6 -2)
===================================================================
--- plugins/table/table.c    2016-12-06 15:20:44 +0900 (3b0c09d)
+++ plugins/table/table.c    2016-12-06 15:52:57 +0900 (69d600f)
@@ -391,7 +391,9 @@ command_add(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
             GRN_TEXT_VALUE(VAR(0)), GRN_TEXT_LEN(VAR(0)),
             NULL, 0,
             GRN_TEXT_VALUE(VAR(1)), GRN_TEXT_LEN(VAR(1)),
-            NULL, 0, NULL, 0, 0);
+            NULL, 0, NULL, 0,
+            NULL,
+            0);
   GRN_OUTPUT_BOOL(ctx->impl->loader.nrecords);
   if (ctx->impl->loader.table) {
     grn_db_touch(ctx, DB_OBJ(ctx->impl->loader.table)->db);
@@ -577,7 +579,9 @@ command_push(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
                     GRN_TEXT_VALUE(VAR(0)), GRN_TEXT_LEN(VAR(0)),
                     NULL, 0,
                     GRN_TEXT_VALUE(VAR(1)), GRN_TEXT_LEN(VAR(1)),
-                    NULL, 0, NULL, 0, 0);
+                    NULL, 0, NULL, 0,
+                    NULL,
+                    0);
           if (grn_table_queue_size(queue) == queue->cap) {
             grn_table_queue_tail_increment(queue);
           }

  Added: test/command/suite/load/command_version/3/output_ids.expected (+24 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/load/command_version/3/output_ids.expected    2016-12-06 15:52:57 +0900 (2b3b8da)
@@ -0,0 +1,24 @@
+table_create Users TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+load --table Users --command_version 3 --output_ids yes
+[
+{"_key": "Alice"},
+{},
+{"_key": "Chris"}
+]
+{
+  "header": {
+    "return_code": 0,
+    "start_time": 0.0,
+    "elapsed_time": 0.0
+  },
+  "body": {
+    "n_loaded_records": 2,
+    "ids": [
+      1,
+      0,
+      2
+    ]
+  }
+}
+#|e| neither _key nor _id is assigned

  Added: test/command/suite/load/command_version/3/output_ids.test (+8 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/load/command_version/3/output_ids.test    2016-12-06 15:52:57 +0900 (ad7d8fc)
@@ -0,0 +1,8 @@
+table_create Users TABLE_HASH_KEY ShortText
+
+load --table Users --command_version 3 --output_ids yes
+[
+{"_key": "Alice"},
+{},
+{"_key": "Chris"}
+]
-------------- next part --------------
HTML����������������������������...
Download 



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