[Groonga-commit] groonga/groonga [master] Move get command to table plugin

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Aug 14 15:30:28 JST 2012


Kouhei Sutou	2012-08-14 15:30:28 +0900 (Tue, 14 Aug 2012)

  New Revision: 072d3485724fd5838084d526b5bc7a4d3d079546
  https://github.com/groonga/groonga/commit/072d3485724fd5838084d526b5bc7a4d3d079546

  Merged 1172487: Merge pull request #26 from groonga/move-get-to-table-plugin

  Log:
    Move get command to table plugin

  Added files:
    test/function/suite/table/get.expected
    test/function/suite/table/get.test
  Modified files:
    lib/proc.c
    plugins/table/table.c

  Modified: lib/proc.c (+0 -119)
===================================================================
--- lib/proc.c    2012-08-14 14:14:23 +0900 (5dfb90c)
+++ lib/proc.c    2012-08-14 15:30:28 +0900 (1e4cd30)
@@ -1616,119 +1616,6 @@ proc_log_reopen(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_dat
 }
 
 static grn_rc
-proc_get_resolve_parameters(grn_ctx *ctx, grn_user_data *user_data, grn_obj **table, grn_id *id)
-{
-  const char *table_text, *id_text, *key_text;
-  int table_length, id_length, key_length;
-
-  table_text = GRN_TEXT_VALUE(VAR(0));
-  table_length = GRN_TEXT_LEN(VAR(0));
-  if (table_length == 0) {
-    ERR(GRN_INVALID_ARGUMENT, "table isn't specified");
-    return ctx->rc;
-  }
-
-  *table = grn_ctx_get(ctx, table_text, table_length);
-  if (!*table) {
-    ERR(GRN_INVALID_ARGUMENT,
-        "table doesn't exist: <%.*s>", table_length, table_text);
-    return ctx->rc;
-  }
-
-  key_text = GRN_TEXT_VALUE(VAR(1));
-  key_length = GRN_TEXT_LEN(VAR(1));
-  id_text = GRN_TEXT_VALUE(VAR(3));
-  id_length = GRN_TEXT_LEN(VAR(3));
-  switch ((*table)->header.type) {
-  case GRN_TABLE_NO_KEY:
-    if (key_length) {
-      ERR(GRN_INVALID_ARGUMENT,
-          "should not specify key for NO_KEY table: <%.*s>: table: <%.*s>",
-          key_length, key_text,
-          table_length, table_text);
-      return ctx->rc;
-    }
-    if (id_length) {
-      const char *rest = NULL;
-      *id = grn_atoi(id_text, id_text + id_length, &rest);
-      if (rest == id_text) {
-        ERR(GRN_INVALID_ARGUMENT,
-            "ID should be a number: <%.*s>: table: <%.*s>",
-            id_length, id_text,
-            table_length, table_text);
-      }
-    } else {
-      ERR(GRN_INVALID_ARGUMENT,
-          "ID isn't specified: table: <%.*s>",
-          table_length, table_text);
-    }
-    break;
-  case GRN_TABLE_HASH_KEY:
-  case GRN_TABLE_PAT_KEY:
-  case GRN_TABLE_DAT_KEY:
-  case GRN_TABLE_VIEW:
-    if (key_length && id_length) {
-      ERR(GRN_INVALID_ARGUMENT,
-          "should not specify both key and ID: "
-          "key: <%.*s>: ID: <%.*s>: table: <%.*s>",
-          key_length, key_text,
-          id_length, id_text,
-          table_length, table_text);
-      return ctx->rc;
-    }
-    if (key_length) {
-      *id = grn_table_get(ctx, *table, key_text, key_length);
-      if (!*id) {
-        ERR(GRN_INVALID_ARGUMENT,
-            "nonexistent key: <%.*s>: table: <%.*s>",
-            key_length, key_text,
-            table_length, table_text);
-      }
-    } else {
-      if (id_length) {
-        const char *rest = NULL;
-        *id = grn_atoi(id_text, id_text + id_length, &rest);
-        if (rest == id_text) {
-          ERR(GRN_INVALID_ARGUMENT,
-              "ID should be a number: <%.*s>: table: <%.*s>",
-              id_length, id_text,
-              table_length, table_text);
-        }
-      } else {
-        ERR(GRN_INVALID_ARGUMENT,
-            "key nor ID isn't specified: table: <%.*s>",
-            table_length, table_text);
-      }
-    }
-    break;
-  default:
-    ERR(GRN_INVALID_ARGUMENT, "not a table: <%.*s>", table_length, table_text);
-    break;
-  }
-
-  return ctx->rc;
-}
-
-static grn_obj *
-proc_get(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
-{
-  grn_id id = GRN_ID_NIL;
-  grn_obj *table = NULL;
-  if (!proc_get_resolve_parameters(ctx, user_data, &table, &id)) {
-    grn_obj obj;
-    grn_obj_format format;
-    GRN_RECORD_INIT(&obj, 0, ((grn_db_obj *)table)->id);
-    GRN_OBJ_FORMAT_INIT(&format, 1, 0, 1, 0);
-    GRN_RECORD_SET(ctx, &obj, id);
-    grn_obj_columns(ctx, table, GRN_TEXT_VALUE(VAR(2)), GRN_TEXT_LEN(VAR(2)), &format.columns);
-    format.flags = 0 /* GRN_OBJ_FORMAT_WITH_COLUMN_NAMES */;
-    GRN_OUTPUT_OBJ(&obj, &format);
-    GRN_OBJ_FORMAT_FIN(ctx, &format);
-  }
-  return NULL;
-}
-
-static grn_rc
 proc_delete_validate_selector(grn_ctx *ctx, grn_obj *table, grn_obj *table_name,
                               grn_obj *key, grn_obj *id, grn_obj *filter)
 {
@@ -2947,12 +2834,6 @@ grn_db_init_builtin_query(grn_ctx *ctx)
 
   DEF_VAR(vars[0], "table");
   DEF_VAR(vars[1], "key");
-  DEF_VAR(vars[2], "output_columns");
-  DEF_VAR(vars[3], "id");
-  DEF_COMMAND("get", proc_get, 4, vars);
-
-  DEF_VAR(vars[0], "table");
-  DEF_VAR(vars[1], "key");
   DEF_VAR(vars[2], "id");
   DEF_VAR(vars[3], "filter");
   DEF_COMMAND("delete", proc_delete, 4, vars);

  Modified: plugins/table/table.c (+125 -0)
===================================================================
--- plugins/table/table.c    2012-08-14 14:14:23 +0900 (9eeb486)
+++ plugins/table/table.c    2012-08-14 15:30:28 +0900 (af4b857)
@@ -432,6 +432,125 @@ command_set(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
   return NULL;
 }
 
+static grn_rc
+command_get_resolve_parameters(grn_ctx *ctx, grn_user_data *user_data,
+                               grn_obj **table, grn_id *id)
+{
+  const char *table_text, *id_text, *key_text;
+  int table_length, id_length, key_length;
+
+  table_text = GRN_TEXT_VALUE(VAR(0));
+  table_length = GRN_TEXT_LEN(VAR(0));
+  if (table_length == 0) {
+    ERR(GRN_INVALID_ARGUMENT, "table isn't specified");
+    return ctx->rc;
+  }
+
+  *table = grn_ctx_get(ctx, table_text, table_length);
+  if (!*table) {
+    ERR(GRN_INVALID_ARGUMENT,
+        "table doesn't exist: <%.*s>", table_length, table_text);
+    return ctx->rc;
+  }
+
+  key_text = GRN_TEXT_VALUE(VAR(1));
+  key_length = GRN_TEXT_LEN(VAR(1));
+  id_text = GRN_TEXT_VALUE(VAR(3));
+  id_length = GRN_TEXT_LEN(VAR(3));
+  switch ((*table)->header.type) {
+  case GRN_TABLE_NO_KEY:
+    if (key_length) {
+      ERR(GRN_INVALID_ARGUMENT,
+          "should not specify key for NO_KEY table: <%.*s>: table: <%.*s>",
+          key_length, key_text,
+          table_length, table_text);
+      return ctx->rc;
+    }
+    if (id_length) {
+      const char *rest = NULL;
+      *id = grn_atoi(id_text, id_text + id_length, &rest);
+      if (rest == id_text) {
+        ERR(GRN_INVALID_ARGUMENT,
+            "ID should be a number: <%.*s>: table: <%.*s>",
+            id_length, id_text,
+            table_length, table_text);
+      }
+    } else {
+      ERR(GRN_INVALID_ARGUMENT,
+          "ID isn't specified: table: <%.*s>",
+          table_length, table_text);
+    }
+    break;
+  case GRN_TABLE_HASH_KEY:
+  case GRN_TABLE_PAT_KEY:
+  case GRN_TABLE_DAT_KEY:
+  case GRN_TABLE_VIEW:
+    if (key_length && id_length) {
+      ERR(GRN_INVALID_ARGUMENT,
+          "should not specify both key and ID: "
+          "key: <%.*s>: ID: <%.*s>: table: <%.*s>",
+          key_length, key_text,
+          id_length, id_text,
+          table_length, table_text);
+      return ctx->rc;
+    }
+    if (key_length) {
+      *id = grn_table_get(ctx, *table, key_text, key_length);
+      if (!*id) {
+        ERR(GRN_INVALID_ARGUMENT,
+            "nonexistent key: <%.*s>: table: <%.*s>",
+            key_length, key_text,
+            table_length, table_text);
+      }
+    } else {
+      if (id_length) {
+        const char *rest = NULL;
+        *id = grn_atoi(id_text, id_text + id_length, &rest);
+        if (rest == id_text) {
+          ERR(GRN_INVALID_ARGUMENT,
+              "ID should be a number: <%.*s>: table: <%.*s>",
+              id_length, id_text,
+              table_length, table_text);
+        }
+      } else {
+        ERR(GRN_INVALID_ARGUMENT,
+            "key nor ID isn't specified: table: <%.*s>",
+            table_length, table_text);
+      }
+    }
+    break;
+  default:
+    ERR(GRN_INVALID_ARGUMENT, "not a table: <%.*s>", table_length, table_text);
+    break;
+  }
+
+  return ctx->rc;
+}
+
+static grn_obj *
+command_get(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
+{
+  grn_id id = GRN_ID_NIL;
+  grn_obj *table = NULL;
+  if (!command_get_resolve_parameters(ctx, user_data, &table, &id)) {
+    grn_obj obj;
+    grn_obj_format format;
+    GRN_OUTPUT_ARRAY_OPEN("RESULT", 2);
+    GRN_RECORD_INIT(&obj, 0, ((grn_db_obj *)table)->id);
+    GRN_OBJ_FORMAT_INIT(&format, 1, 0, 1, 0);
+    GRN_RECORD_SET(ctx, &obj, id);
+    grn_obj_columns(ctx, table, GRN_TEXT_VALUE(VAR(2)), GRN_TEXT_LEN(VAR(2)),
+                    &format.columns);
+    format.flags =
+      GRN_OBJ_FORMAT_WITH_COLUMN_NAMES |
+      GRN_OBJ_FORMAT_XML_ELEMENT_RESULTSET;
+    GRN_OUTPUT_OBJ(&obj, &format);
+    GRN_OBJ_FORMAT_FIN(ctx, &format);
+    GRN_OUTPUT_ARRAY_CLOSE();
+  }
+  return NULL;
+}
+
 uint32_t
 grn_table_queue_size(grn_table_queue *queue)
 {
@@ -646,6 +765,12 @@ GRN_PLUGIN_REGISTER(grn_ctx *ctx)
   DEF_COMMAND("set", command_set, 6, vars);
 
   DEF_VAR(vars[0], "table");
+  DEF_VAR(vars[1], "key");
+  DEF_VAR(vars[2], "output_columns");
+  DEF_VAR(vars[3], "id");
+  DEF_COMMAND("get", command_get, 4, vars);
+
+  DEF_VAR(vars[0], "table");
   DEF_VAR(vars[1], "output_columns");
   DEF_VAR(vars[2], "non_block");
   DEF_COMMAND("pull", command_pull, 3, vars);

  Added: test/function/suite/table/get.expected (+45 -0) 100644
===================================================================
--- /dev/null
+++ test/function/suite/table/get.expected    2012-08-14 15:30:28 +0900 (2f4abaa)
@@ -0,0 +1,45 @@
+register table/table
+[[0,0.0,0.0],true]
+table_create Diaries TABLE_PAT_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Diaries title COLUMN_SCALAR ShortText
+[[0,0.0,0.0],true]
+column_create Diaries tags COLUMN_VECTOR ShortText
+[[0,0.0,0.0],true]
+load --table Diaries
+[
+{"_key":"2012-08-14", "title": "'get' command is moved!", "tags": ["groonga", "development"]},
+]
+[[0,0.0,0.0],1]
+get Diaries '2012-08-14' 'title, _key, tags'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        "title",
+        "ShortText"
+      ],
+      [
+        "_key",
+        "ShortText"
+      ],
+      [
+        "tags",
+        "ShortText"
+      ]
+    ],
+    [
+      "'get' command is moved!",
+      "2012-08-14",
+      [
+        "groonga",
+        "development"
+      ]
+    ]
+  ]
+]

  Added: test/function/suite/table/get.test (+10 -0) 100644
===================================================================
--- /dev/null
+++ test/function/suite/table/get.test    2012-08-14 15:30:28 +0900 (4c1facc)
@@ -0,0 +1,10 @@
+register table/table
+table_create Diaries TABLE_PAT_KEY ShortText
+column_create Diaries title COLUMN_SCALAR ShortText
+column_create Diaries tags COLUMN_VECTOR ShortText
+
+load --table Diaries
+[
+{"_key":"2012-08-14", "title": "'get' command is moved!", "tags": ["groonga", "development"]},
+]
+get Diaries '2012-08-14' 'title, _key, tags'
-------------- next part --------------
HTML����������������������������...
Download 



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