[Groonga-commit] groonga/groonga at fed963e [master] vector_new: add a new function that creates a new vector

アーカイブの一覧に戻る

Kouhei Sutou null+****@clear*****
Fri Mar 24 16:34:22 JST 2017


Kouhei Sutou	2017-03-24 16:34:22 +0900 (Fri, 24 Mar 2017)

  New Revision: fed963e0e3fd3ef52a54387ae9076f7b3798555f
  https://github.com/groonga/groonga/commit/fed963e0e3fd3ef52a54387ae9076f7b3798555f

  Message:
    vector_new: add a new function that creates a new vector

  Added files:
    test/command/suite/select/columns/flags/vector.expected
    test/command/suite/select/columns/flags/vector.test
  Modified files:
    plugins/functions/vector.c

  Modified: plugins/functions/vector.c (+51 -1)
===================================================================
--- plugins/functions/vector.c    2017-03-24 15:10:55 +0900 (05f0d52)
+++ plugins/functions/vector.c    2017-03-24 16:34:22 +0900 (f5df5b1)
@@ -1,6 +1,6 @@
 /* -*- c-basic-offset: 2 -*- */
 /*
-  Copyright(C) 2015-2016 Brazil
+  Copyright(C) 2015-2017 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -274,6 +274,53 @@ func_vector_slice(grn_ctx *ctx, int n_args, grn_obj **args,
   return slice;
 }
 
+static grn_obj *
+func_vector_new(grn_ctx *ctx, int n_args, grn_obj **args,
+                grn_user_data *user_data)
+{
+  grn_obj *vector = NULL;
+  int i;
+
+  if (n_args == 0) {
+    return grn_plugin_proc_alloc(ctx, user_data, GRN_DB_UINT32, GRN_OBJ_VECTOR);
+  }
+
+  vector = grn_plugin_proc_alloc(ctx,
+                                 user_data,
+                                 args[0]->header.domain,
+                                 GRN_OBJ_VECTOR);
+  if (!vector) {
+    return NULL;
+  }
+
+  for (i = 0; i < n_args; i++) {
+    grn_obj *element = args[i];
+    switch (vector->header.type) {
+    case GRN_VECTOR :
+      grn_vector_add_element(ctx,
+                             vector,
+                             GRN_BULK_HEAD(element),
+                             GRN_BULK_VSIZE(element),
+                             0,
+                             element->header.domain);
+      break;
+    case GRN_UVECTOR :
+      grn_bulk_write(ctx,
+                     vector,
+                     GRN_BULK_HEAD(element),
+                     GRN_BULK_VSIZE(element));
+      break;
+    case GRN_PVECTOR :
+      GRN_PTR_PUT(ctx, vector, element);
+      break;
+    default :
+      break;
+    }
+  }
+
+  return vector;
+}
+
 grn_rc
 GRN_PLUGIN_INIT(grn_ctx *ctx)
 {
@@ -291,6 +338,9 @@ GRN_PLUGIN_REGISTER(grn_ctx *ctx)
   grn_proc_create(ctx, "vector_slice", -1, GRN_PROC_FUNCTION, func_vector_slice,
                   NULL, NULL, 0, NULL);
 
+  grn_proc_create(ctx, "vector_new", -1, GRN_PROC_FUNCTION, func_vector_new,
+                  NULL, NULL, 0, NULL);
+
   return rc;
 }
 

  Added: test/command/suite/select/columns/flags/vector.expected (+60 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/columns/flags/vector.expected    2017-03-24 16:34:22 +0900 (84ff8e1)
@@ -0,0 +1,60 @@
+plugin_register functions/vector
+[[0,0.0,0.0],true]
+table_create Items TABLE_NO_KEY
+[[0,0.0,0.0],true]
+load --table Items
+[
+{},
+{},
+{}
+]
+[[0,0.0,0.0],3]
+select Items   --columns[vector].stage initial   --columns[vector].flags COLUMN_VECTOR   --columns[vector].type UInt32   --columns[vector].value 'vector_new(1, 2, 3)'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        3
+      ],
+      [
+        [
+          "_id",
+          "UInt32"
+        ],
+        [
+          "vector",
+          "UInt32"
+        ]
+      ],
+      [
+        1,
+        [
+          1,
+          2,
+          3
+        ]
+      ],
+      [
+        2,
+        [
+          1,
+          2,
+          3
+        ]
+      ],
+      [
+        3,
+        [
+          1,
+          2,
+          3
+        ]
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/columns/flags/vector.test (+16 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/columns/flags/vector.test    2017-03-24 16:34:22 +0900 (1c03ca2)
@@ -0,0 +1,16 @@
+plugin_register functions/vector
+
+table_create Items TABLE_NO_KEY
+
+load --table Items
+[
+{},
+{},
+{}
+]
+
+select Items \
+  --columns[vector].stage initial \
+  --columns[vector].flags COLUMN_VECTOR \
+  --columns[vector].type UInt32 \
+  --columns[vector].value 'vector_new(1, 2, 3)'
-------------- next part --------------
HTML����������������������������...
ダウンロード 



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