[Groonga-commit] groonga/groonga [master] add hidden truncate command. fixes #888

Zurück zum Archiv-Index

null+****@clear***** null+****@clear*****
2011年 3月 27日 (日) 17:59:57 JST


Kouhei Sutou	2011-03-27 08:59:57 +0000 (Sun, 27 Mar 2011)

  New Revision: 2d58a26dacb2b8f92ff326a0df8bc09f3ce8735a

  Log:
    add hidden truncate command. fixes #888

  Added files:
    test/unit/core/test-command-truncate.c
  Modified files:
    lib/proc.c
    test/unit/core/Makefile.am

  Modified: lib/proc.c (+40 -0)
===================================================================
--- lib/proc.c    2011-03-27 08:17:44 +0000 (09535fe)
+++ lib/proc.c    2011-03-27 08:59:57 +0000 (c42ab18)
@@ -2119,6 +2119,43 @@ proc_check(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
 }
 
 static grn_obj *
+proc_truncate(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
+{
+  int table_name_len = GRN_TEXT_LEN(VAR(0));
+  if (table_name_len == 0) {
+      ERR(GRN_INVALID_ARGUMENT, "table name is missing");
+  } else {
+    const char *table_name = GRN_TEXT_VALUE(VAR(0));
+    grn_obj *table = grn_ctx_get(ctx, table_name, table_name_len);
+    if (!table) {
+      ERR(GRN_INVALID_ARGUMENT,
+          "no such table: <%.*s>", table_name, table_name_len);
+    } else {
+      switch (table->header.type) {
+      case GRN_TABLE_PAT_KEY :
+      case GRN_TABLE_HASH_KEY :
+      case GRN_TABLE_NO_KEY :
+        grn_table_truncate(ctx, table);
+        break;
+      default:
+        {
+          grn_obj buffer;
+          GRN_TEXT_INIT(&buffer, 0);
+          grn_inspect(ctx, &buffer, table);
+          ERR(GRN_INVALID_ARGUMENT,
+              "not a table object: %.*s",
+              GRN_TEXT_LEN(&buffer), GRN_TEXT_VALUE(&buffer));
+          GRN_OBJ_FIN(ctx, &buffer);
+        }
+        break;
+      }
+    }
+  }
+  GRN_OUTPUT_BOOL(!ctx->rc);
+  return NULL;
+}
+
+static grn_obj *
 func_rand(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
 {
   int val;
@@ -2386,6 +2423,9 @@ grn_db_init_builtin_query(grn_ctx *ctx)
   DEF_VAR(vars[0], "obj");
   DEF_COMMAND("check", proc_check, 1, vars);
 
+  DEF_VAR(vars[0], "table");
+  DEF_COMMAND("truncate", proc_truncate, 1, vars);
+
   DEF_VAR(vars[0], "seed");
   grn_proc_create(ctx, "rand", 4, GRN_PROC_FUNCTION, func_rand, NULL, NULL, 0, vars);
 

  Modified: test/unit/core/Makefile.am (+2 -0)
===================================================================
--- test/unit/core/Makefile.am    2011-03-27 08:17:44 +0000 (a6cbe28)
+++ test/unit/core/Makefile.am    2011-03-27 08:59:57 +0000 (078841f)
@@ -52,6 +52,7 @@ noinst_LTLIBRARIES =				\
 	test-command-cache-limit.la		\
 	test-command-delete.la			\
 	test-command-dump.la			\
+	test-command-truncate.la		\
 	test-geo.la				\
 	test-accessor.la
 endif
@@ -129,5 +130,6 @@ 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_command_truncate_la_SOURCES	= test-command-truncate.c
 test_geo_la_SOURCES			= test-geo.c
 test_accessor_la_SOURCES		= test-accessor.c

  Added: test/unit/core/test-command-truncate.c (+103 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/core/test-command-truncate.c    2011-03-27 08:59:57 +0000 (7784348)
@@ -0,0 +1,103 @@
+/* -*- c-basic-offset: 2; coding: utf-8 -*- */
+/*
+  Copyright(C) 2011 Kouhei Sutou <kou****@clear*****>
+
+  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_no_columns(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-truncate",
+                                   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_no_columns(void)
+{
+  assert_send_command("table_create Users TABLE_HASH_KEY ShortText");
+  assert_send_command("load --table Users\n"
+                      "[\n"
+                      "{\"_key\":\"mori\"}\n"
+                      "{\"_key\":\"gunyara-kun\"}\n"
+                      "{\"_key\":\"yu\"}\n"
+                      "]");
+  cut_assert_equal_string(
+      "[[[3],"
+       "[[\"_id\",\"UInt32\"],[\"_key\",\"ShortText\"]],"
+       "[1,\"mori\"],"
+       "[2,\"gunyara-kun\"],"
+       "[3,\"yu\"]]]",
+    send_command("select Users"));
+  assert_send_command("truncate Users");
+  cut_assert_equal_string(
+      "[[[0],"
+       "[[\"_id\",\"UInt32\"],[\"_key\",\"ShortText\"]]"
+       "]]",
+    send_command("select Users"));
+}




Groonga-commit メーリングリストの案内
Zurück zum Archiv-Index