[Groonga-commit] groonga/groonga at 657c338 [master] Add object_remove command

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Feb 8 18:08:34 JST 2016


Kouhei Sutou	2016-02-08 18:08:34 +0900 (Mon, 08 Feb 2016)

  New Revision: 657c338bbaeeeeccdb970425fc08d4f3c671bdd5
  https://github.com/groonga/groonga/commit/657c338bbaeeeeccdb970425fc08d4f3c671bdd5

  Message:
    Add object_remove command

  Added files:
    test/command/suite/object_remove/nonexistent.expected
    test/command/suite/object_remove/nonexistent.test
  Modified files:
    lib/grn_proc.h
    lib/proc/proc_object.c

  Modified: lib/grn_proc.h (+1 -0)
===================================================================
--- lib/grn_proc.h    2016-02-08 17:49:05 +0900 (ed9eb7d)
+++ lib/grn_proc.h    2016-02-08 18:08:34 +0900 (04d9997)
@@ -40,6 +40,7 @@ void grn_proc_init_lock_acquire(grn_ctx *ctx);
 void grn_proc_init_lock_clear(grn_ctx *ctx);
 void grn_proc_init_lock_release(grn_ctx *ctx);
 void grn_proc_init_object_exist(grn_ctx *ctx);
+void grn_proc_init_object_remove(grn_ctx *ctx);
 void grn_proc_init_schema(grn_ctx *ctx);
 void grn_proc_init_table_create(grn_ctx *ctx);
 void grn_proc_init_table_list(grn_ctx *ctx);

  Modified: lib/proc/proc_object.c (+79 -0)
===================================================================
--- lib/proc/proc_object.c    2016-02-08 17:49:05 +0900 (3e27aa6)
+++ lib/proc/proc_object.c    2016-02-08 18:08:34 +0900 (d3f8be5)
@@ -17,6 +17,7 @@
 */
 
 #include "../grn_proc.h"
+#include "../grn_io.h"
 
 #include <groonga/plugin.h>
 
@@ -58,3 +59,81 @@ grn_proc_init_object_exist(grn_ctx *ctx)
                             1,
                             vars);
 }
+
+static grn_obj *
+command_object_remove(grn_ctx *ctx,
+                      int nargs,
+                      grn_obj **args,
+                      grn_user_data *user_data)
+{
+  grn_obj *db;
+  grn_obj *name;
+  grn_bool force;
+  grn_obj *target;
+  grn_bool failed_to_open;
+
+  db = grn_ctx_db(ctx);
+  name = grn_plugin_proc_get_var(ctx, user_data, "name", -1);
+  force = grn_plugin_proc_get_var_bool(ctx, user_data, "force", -1, GRN_FALSE);
+
+  if (GRN_TEXT_LEN(name) == 0) {
+    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
+                     "[object][remove] name is missing");
+    grn_ctx_output_bool(ctx, GRN_FALSE);
+    return NULL;
+  }
+
+  target = grn_ctx_get(ctx,
+                       GRN_TEXT_VALUE(name),
+                       GRN_TEXT_LEN(name));
+  if (target) {
+    grn_obj_remove(ctx, target);
+    grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS);
+    return NULL;
+  }
+
+  failed_to_open = (ctx->rc != GRN_SUCCESS);
+
+  if (force) {
+    grn_id id;
+    id = grn_table_get(ctx, db,
+                       GRN_TEXT_VALUE(name),
+                       GRN_TEXT_LEN(name));
+    if (id != GRN_ID_NIL) {
+      char path[PATH_MAX];
+      grn_obj_delete_by_id(ctx, db, id, GRN_TRUE);
+      grn_obj_path_by_id(ctx, db, id, path);
+      grn_io_remove(ctx, path);
+      grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS);
+      return NULL;
+    }
+  }
+
+  if (failed_to_open) {
+    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
+                     "[object][remove] failed to open the target object: <%.*s>",
+                     (int)GRN_TEXT_LEN(name),
+                     GRN_TEXT_VALUE(name));
+  } else {
+    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
+                     "[object][remove] target object doesn't exist: <%.*s>",
+                     (int)GRN_TEXT_LEN(name),
+                     GRN_TEXT_VALUE(name));
+  }
+  grn_ctx_output_bool(ctx, GRN_FALSE);
+  return NULL;
+}
+
+void
+grn_proc_init_object_remove(grn_ctx *ctx)
+{
+  grn_expr_var vars[2];
+
+  grn_plugin_expr_var_init(ctx, &(vars[0]), "name", -1);
+  grn_plugin_expr_var_init(ctx, &(vars[1]), "force", -1);
+  grn_plugin_command_create(ctx,
+                            "object_remove", -1,
+                            command_object_remove,
+                            2,
+                            vars);
+}

  Added: test/command/suite/object_remove/nonexistent.expected (+13 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/object_remove/nonexistent.expected    2016-02-08 18:08:34 +0900 (a4d7c28)
@@ -0,0 +1,13 @@
+object_remove nonexistent
+[
+  [
+    [
+      -22,
+      0.0,
+      0.0
+    ],
+    "[object][remove] target object doesn't exist: <nonexistent>"
+  ],
+  false
+]
+#|e| [object][remove] target object doesn't exist: <nonexistent>

  Added: test/command/suite/object_remove/nonexistent.test (+1 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/object_remove/nonexistent.test    2016-02-08 18:08:34 +0900 (9e6561a)
@@ -0,0 +1 @@
+object_remove nonexistent
-------------- next part --------------
HTML����������������������������...
Download 



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