[Groonga-commit] groonga/groonga [master] add 'tables' option to 'dump' command.

Back to archive index

null+****@clear***** null+****@clear*****
2011年 1月 26日 (水) 19:15:32 JST


Kouhei Sutou	2011-01-26 10:15:32 +0000 (Wed, 26 Jan 2011)

  New Revision: 3cc5e21addbf80508b89d7f0faa57bec03c7c644

  Log:
    add 'tables' option to 'dump' command.

  Modified files:
    doc/ja/source/commands/dump.txt
    lib/proc.c
    test/unit/core/test-command-dump.c

  Modified: doc/ja/source/commands/dump.txt (+32 -5)
===================================================================
--- doc/ja/source/commands/dump.txt    2011-01-26 08:03:37 +0000 (3b9f9a0)
+++ doc/ja/source/commands/dump.txt    2011-01-26 10:15:32 +0000 (584652e)
@@ -12,7 +12,7 @@ dump - データベースのスキーマとデータを出力する
 ----
 ::
 
-  dump
+  dump [tables]
 
 説明
 ----
@@ -30,7 +30,9 @@ dumpが出力するフォーマットは直接groongaが解釈できるフォー
 引数
 ----
 
-ありません。
+``tables``
+
+  出力対象のテーブルを「,」(カンマ)区切りで指定します。存在しないテーブルを指定した場合は無視されます。
 
 返値
 ----
@@ -39,16 +41,15 @@ dumpが出力するフォーマットは直接groongaが解釈できるフォー
 
 例
 --
-::
 
-  dump
+データベース内のすべてのデータを出力::
 
+  > dump
   table_create LocalNames 48 ShortText
   table_create Entries 48 ShortText
   column_create Entries local_name 0 LocalNames
   column_create LocalNames Entries_local_name 2 Entries local_name
   ...
-
   load --table LocalNames
   [
   ["_key"],
@@ -58,5 +59,31 @@ dumpが出力するフォーマットは直接groongaが解釈できるフォー
   ]
   ...
 
+データベース内のスキーマと特定のテーブルのデータのみ出力::
+
+  > dump --tables Users,Sites
+  table_create Users TABLE_HASH_KEY ShortText
+  column_create Users name COLUMN_SCALAR ShortText
+  table_create Comments TABLE_PAT_KEY ShortText
+  column_create Comments text COLUMN_SCALAR ShortText
+  table_create Sites TABLE_NO_KEY
+  column_create Sites url COLUMN_SCALAR ShortText
+  load --table Users
+  [
+  ["_key"],
+  ["mori"],
+  ["yu"],
+  ...
+  ]
+  load --table Sites
+  [
+  ["_id","url"],
+  [1,"http://groonga.org/"],
+  [2,"http://qwik.jp/senna/"],
+  ...
+  ]
+
+
+
 
 

  Modified: lib/proc.c (+62 -2)
===================================================================
--- lib/proc.c    2011-01-26 08:03:37 +0000 (a518545)
+++ lib/proc.c    2011-01-26 10:15:32 +0000 (95b130c)
@@ -1879,6 +1879,61 @@ dump_scheme(grn_ctx *ctx, grn_obj *outbuf)
 }
 
 static void
+dump_selected_tables_records(grn_ctx *ctx, grn_obj *outbuf, grn_obj *tables)
+{
+  const char *p, *e;
+
+  p = GRN_TEXT_VALUE(tables);
+  e = p + GRN_TEXT_LEN(tables);
+  while (p < e) {
+    int len;
+    grn_obj *table;
+    const char *token, *token_e;
+
+    if ((len = grn_isspace(p, ctx->encoding))) {
+      p += len;
+      continue;
+    }
+
+    token = p;
+    if (!(('a' <= *p && *p <= 'z') ||
+          ('A' <= *p && *p <= 'Z') ||
+          (*p == '_'))) {
+      while (p < e && !grn_isspace(p, ctx->encoding)) {
+        p++;
+      }
+      GRN_LOG(ctx, GRN_LOG_WARNING, "invalid table name is ignored: <%.*s>\n",
+              p - token, token);
+      continue;
+    }
+    while (p < e &&
+           (('a' <= *p && *p <= 'z') ||
+            ('A' <= *p && *p <= 'Z') ||
+            ('0' <= *p && *p <= '9') ||
+            (*p == '_'))) {
+      p++;
+    }
+    token_e = p;
+    while (p < e && (len = grn_isspace(p, ctx->encoding))) {
+      p += len;
+      continue;
+    }
+    if (p < e && *p == ',') {
+      p++;
+    }
+
+    if ((table = grn_ctx_get(ctx, token, token_e - token))) {
+      dump_records(ctx, outbuf, table);
+      grn_obj_unlink(ctx, table);
+    } else {
+      GRN_LOG(ctx, GRN_LOG_WARNING,
+              "nonexistent table name is ignored: <%.*s>\n",
+              token_e - token, token);
+    }
+  }
+}
+
+static void
 dump_all_records(grn_ctx *ctx, grn_obj *outbuf)
 {
   grn_obj *db = ctx->impl->db;
@@ -1908,7 +1963,11 @@ proc_dump(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
   /* To update index columns correctly, we first create the whole scheme, then
      load non-derivative records, while skipping records of index columns. That
      way, groonga will silently do the job of updating index columns for us. */
-  dump_all_records(ctx, outbuf);
+  if (GRN_TEXT_LEN(VAR(0)) > 0) {
+    dump_selected_tables_records(ctx, outbuf, VAR(0));
+  } else {
+    dump_all_records(ctx, outbuf);
+  }
 
   /* remove the last newline because another one will be added by the calller.
      maybe, the caller of proc functions currently doesn't consider the
@@ -2317,7 +2376,8 @@ grn_db_init_builtin_query(grn_ctx *ctx)
   DEF_VAR(vars[0], "max");
   DEF_COMMAND("cache_limit", proc_cache_limit, 1, vars);
 
-  DEF_COMMAND("dump", proc_dump, 0, vars);
+  DEF_VAR(vars[0], "tables");
+  DEF_COMMAND("dump", proc_dump, 1, vars);
 
   DEF_VAR(vars[0], "path");
   DEF_COMMAND("register", proc_register, 1, vars);

  Modified: test/unit/core/test-command-dump.c (+46 -0)
===================================================================
--- test/unit/core/test-command-dump.c    2011-01-26 08:03:37 +0000 (8716be4)
+++ test/unit/core/test-command-dump.c    2011-01-26 10:15:32 +0000 (2d60802)
@@ -36,6 +36,7 @@ void test_vector_column(gconstpointer data);
 void test_unsequantial_records_in_table_with_keys(void);
 void test_nil_reference(void);
 void test_load_with_vector_int32_reference_key(void);
+void test_tables_argument(void);
 
 static gchar *tmp_directory;
 
@@ -534,3 +535,48 @@ test_load_with_vector_int32_reference_key(void)
   assert_send_commands(commands);
   cut_assert_equal_string(commands, send_command("dump"));
 }
+
+void
+test_tables_argument(void)
+{
+  const gchar *define_schema_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"
+    "table_create sites TABLE_NO_KEY\n"
+    "column_create sites url COLUMN_SCALAR ShortText\n"
+    "column_create comments author COLUMN_VECTOR users";
+  const gchar *load_users_commands =
+    "load --table users\n"
+    "[\n"
+    "[\"_key\",\"name\"],\n"
+    "[1000,\"ryoqun\"],\n"
+    "[1001,\"hayamiz\"]\n"
+    "]";
+  const gchar *load_comments_commands =
+    "load --table comments\n"
+    "[\n"
+    "[\"_key\",\"text\",\"author\"],\n"
+    "[\"groonga\",\"it is fast\",[1000,1001]]\n"
+    "]";
+  const gchar *load_sites_commands =
+    "load --table sites\n"
+    "[\n"
+    "[\"_id\",\"url\"],\n"
+    "[1,\"http://groonga.org/\"],\n"
+    "[2,\"http://qwik.jp/senna/\"]\n"
+    "]";
+
+  assert_send_commands(define_schema_commands);
+  assert_send_commands(load_users_commands);
+  assert_send_commands(load_comments_commands);
+  assert_send_commands(load_sites_commands);
+  cut_assert_equal_string(cut_take_printf("%s\n"
+                                          "%s\n"
+                                          "%s",
+                                          define_schema_commands,
+                                          load_users_commands,
+                                          load_sites_commands),
+                          send_command("dump --tables users,sites"));
+}




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