[Groonga-commit] groonga/groonga [master] support integer key table vector load.

Back to archive index

null+****@clear***** null+****@clear*****
2010年 8月 16日 (月) 15:35:58 JST


Kouhei Sutou	2010-08-16 06:35:58 +0000 (Mon, 16 Aug 2010)

  New Revision: b9e1309f0b11b2913eaee6c257285d1a3802f81c

  Log:
    support integer key table vector load.

  Added files:
    test/unit/core/test-command-dump.c
  Modified files:
    lib/db.c
    test/unit/core/Makefile.am
    test/unit/core/test-command-select.c

  Modified: lib/db.c (+34 -40)
===================================================================
--- lib/db.c    2010-08-18 07:42:53 +0000 (e347dd2)
+++ lib/db.c    2010-08-16 06:35:58 +0000 (b08c5d3)
@@ -3634,6 +3634,38 @@ grn_obj_is_persistent(grn_ctx *ctx, grn_obj *obj)
   return res;
 }
 
+#define SRC2RECORD() {\
+  grn_obj *table = grn_ctx_at(ctx, dest->header.domain);\
+  if (GRN_OBJ_TABLEP(table)) {\
+    grn_obj *p_key = src;\
+    grn_id id;\
+    if (table->header.type != GRN_TABLE_NO_KEY) {\
+      grn_obj key;\
+      GRN_OBJ_INIT(&key, GRN_BULK, 0, table->header.domain);\
+      if (src->header.domain != table->header.domain) {\
+        grn_obj_cast(ctx, src, &key, 1);\
+        p_key = &key;\
+      }\
+      if (GRN_BULK_VSIZE(p_key)) {\
+        id = addp ? grn_table_add_by_key(ctx, table, p_key, NULL)\
+                  : grn_table_get_by_key(ctx, table, p_key);\
+        if (id) { GRN_RECORD_SET(ctx, dest, id); }\
+      } else {\
+        GRN_RECORD_SET(ctx, dest, GRN_ID_NIL);\
+      }\
+      GRN_OBJ_FIN(ctx, &key);\
+    } else {\
+      grn_obj record_id;\
+      GRN_UINT32_INIT(&record_id, 0);\
+      grn_obj_cast(ctx, src, &record_id, 1);\
+      id = GRN_UINT32_VALUE(&record_id);\
+      if (id) { GRN_RECORD_SET(ctx, dest, id); }\
+    }\
+  } else {\
+    rc = GRN_FUNCTION_NOT_IMPLEMENTED;\
+  }\
+}
+
 #define NUM2DEST(getvalue,totext,tobool)\
   switch (dest->header.domain) {\
   case GRN_DB_BOOL :\
@@ -3675,15 +3707,7 @@ grn_obj_is_persistent(grn_ctx *ctx, grn_obj *obj)
     totext(ctx, dest, getvalue(src));\
     break;\
   default :\
-    {\
-      grn_obj *domain = grn_ctx_at(ctx, dest->header.domain);\
-      if (domain && domain->header.type == GRN_TABLE_NO_KEY) {\
-        GRN_RECORD_SET(ctx, dest, getvalue(src));\
-      } else {\
-        rc = GRN_FUNCTION_NOT_IMPLEMENTED;\
-      }\
-      break;\
-    }\
+    SRC2RECORD();\
   }
 
 #define TEXT2DEST(type,tonum,setvalue) {\
@@ -3894,37 +3918,7 @@ grn_obj_cast(grn_ctx *ctx, grn_obj *src, grn_obj *dest, int addp)
       }
       break;
     default :
-      {
-        grn_obj *table = grn_ctx_at(ctx, dest->header.domain);
-        if (GRN_OBJ_TABLEP(table)) {
-          grn_obj *p_key = src;
-          grn_id id;
-          if (table->header.type != GRN_TABLE_NO_KEY) {
-            grn_obj key;
-            GRN_OBJ_INIT(&key, GRN_BULK, 0, table->header.domain);
-            if (src->header.domain != table->header.domain) {
-              grn_obj_cast(ctx, src, &key, 1);
-              p_key = &key;
-            }
-            if (GRN_BULK_VSIZE(p_key)) {
-              id = addp ? grn_table_add_by_key(ctx, table, p_key, NULL)
-                        : grn_table_get_by_key(ctx, table, p_key);
-              if (id) { GRN_RECORD_SET(ctx, dest, id); }
-            } else {
-              GRN_RECORD_SET(ctx, dest, GRN_ID_NIL);
-            }
-            GRN_OBJ_FIN(ctx, &key);
-          } else {
-            grn_obj record_id;
-            GRN_UINT32_INIT(&record_id, 0);
-            grn_obj_cast(ctx, src, &record_id, 1);
-            id = GRN_UINT32_VALUE(&record_id);
-            if (id) { GRN_RECORD_SET(ctx, dest, id); }
-          }
-        } else {
-          rc = GRN_FUNCTION_NOT_IMPLEMENTED;
-        }
-      }
+      SRC2RECORD();
       break;
     }
     break;

  Modified: test/unit/core/Makefile.am (+2 -0)
===================================================================
--- test/unit/core/Makefile.am    2010-08-18 07:42:53 +0000 (679e39a)
+++ test/unit/core/Makefile.am    2010-08-16 06:35:58 +0000 (592e988)
@@ -47,6 +47,7 @@ noinst_LTLIBRARIES =				\
 	test-command-define-selector.la		\
 	test-command-cache-limit.la		\
 	test-command-delete.la			\
+	test-command-dump.la			\
 	test-geo.la				\
 	test-accessor.la
 endif
@@ -119,5 +120,6 @@ test_command_select_prefix_search_la_SOURCES	= test-command-select-prefix-search
 test_command_define_selector_la_SOURCES	= test-command-define-selector.c
 test_command_cache_limit_la_SOURCES	= test-command-cache-limit.c
 test_command_delete_la_SOURCES		= test-command-delete.c
+test_command_dump_la_SOURCES		= test-command-dump.c
 test_geo_la_SOURCES			= test-geo.c
 test_accessor_la_SOURCES		= test-accessor.c

  Added: test/unit/core/test-command-dump.c (+102 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/core/test-command-dump.c    2010-08-16 06:35:58 +0000 (cc7fc91)
@@ -0,0 +1,102 @@
+/* -*- c-basic-offset: 2; coding: utf-8 -*- */
+/* Copyright(C) 2010 Brazil
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License version 2.1 as published by the Free Software Foundation.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#include "str.h"
+#include <stdio.h>
+
+#include <gcutter.h>
+
+#include "../lib/grn-assertions.h"
+
+void test_load_with_vector_int32_reference_key(void);
+
+static gchar *tmp_directory;
+
+static grn_ctx *context;
+static grn_obj *database;
+
+void
+cut_startup(void)
+{
+  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
+                                   "command-dump",
+                                   NULL);
+}
+
+void
+cut_shutdown(void)
+{
+  g_free(tmp_directory);
+}
+
+static void
+remove_tmp_directory(void)
+{
+  cut_remove_path(tmp_directory, NULL);
+}
+
+void
+cut_setup(void)
+{
+  const gchar *database_path;
+
+  remove_tmp_directory();
+  g_mkdir_with_parents(tmp_directory, 0700);
+
+  context = g_new0(grn_ctx, 1);
+  grn_ctx_init(context, 0);
+
+  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
+  database = grn_db_create(context, database_path, NULL);
+}
+
+void
+cut_teardown(void)
+{
+  if (context) {
+    grn_obj_unlink(context, database);
+    grn_ctx_fin(context);
+    g_free(context);
+  }
+
+  remove_tmp_directory();
+}
+
+void
+test_load_with_vector_int32_reference_key(void)
+{
+  const gchar *commands =
+    "table_create users TABLE_HASH_KEY Int32\n"
+    "column_create users name COLUMN_SCALAR ShortText\n"
+    "table_create comments TABLE_PAT_KEY ShortText\n"
+    "column_create comments text COLUMN_SCALAR ShortText\n"
+    "column_create comments author COLUMN_VECTOR users\n"
+    "load --table users\n"
+    "[\n"
+    "[\"_key\",\"name\"],\n"
+    "[1000,\"ryoqun\"],\n"
+    "[1001,\"hayamiz\"]\n"
+    "]\n"
+    "load --table comments\n"
+    "[\n"
+    "[\"_key\",\"text\",\"author\"],\n"
+    "[\"groonga\",\"it is fast\",[1000,1001]]\n"
+    "]";
+
+  assert_send_commands(commands);
+  cut_assert_equal_string(commands, send_command("dump"));
+}

  Modified: test/unit/core/test-command-select.c (+36 -1)
===================================================================
--- test/unit/core/test-command-select.c    2010-08-18 07:42:53 +0000 (4378a2d)
+++ test/unit/core/test-command-select.c    2010-08-16 06:35:58 +0000 (5a28f1f)
@@ -1,5 +1,5 @@
 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
-/* Copyright(C) 2009 Brazil
+/* Copyright(C) 2009-2010 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -30,6 +30,7 @@ void test_unmatched_output_columns(void);
 void test_vector_int32(void);
 void test_vector_text(void);
 void test_vector_reference_id(void);
+void test_vector_int32_reference_key(void);
 void test_nonexistent_id(void);
 void test_tautology(void);
 void test_contradiction(void);
@@ -309,6 +310,40 @@ test_vector_reference_id(void)
 }
 
 void
+test_vector_int32_reference_key(void)
+{
+  assert_send_command("table_create Users TABLE_HASH_KEY Int32");
+  assert_send_command("column_create Users name COLUMN_SCALAR ShortText");
+  assert_send_command("table_create Comments TABLE_PAT_KEY ShortText");
+  assert_send_command("column_create Comments text COLUMN_SCALAR ShortText");
+  assert_send_command("column_create Comments authors COLUMN_VECTOR Users");
+
+  cut_assert_equal_string("2",
+                          send_command("load --table Users\n"
+                                       "[\n"
+                                       "[\"_key\",\"name\"],\n"
+                                       "[1000,\"ryoqun\"],\n"
+                                       "[1001,\"hayamiz\"]\n"
+                                       "]"));
+
+  cut_assert_equal_string(
+    "1",
+    send_command("load --table Comments\n"
+                 "[\n"
+                 "[\"_key\",\"text\",\"authors\"],\n"
+                 "[\"groonga\",\"it is fast\",[1000,1001]]\n"
+                 "]"));
+
+  cut_assert_equal_string("[[[1],"
+                           "[[\"_id\",\"UInt32\"],"
+                            "[\"_key\",\"ShortText\"],"
+                            "[\"text\",\"ShortText\"],"
+                            "[\"authors\",\"Users\"]],"
+                           "[1,\"groonga\",\"it is fast\",[1000,1001]]]]",
+                          send_command("select Comments"));
+}
+
+void
 test_nonexistent_id(void)
 {
   assert_send_commands("table_create Sites TABLE_PAT_KEY ShortText\n"




Groonga-commit メーリングリストの案内
Back to archive index