[Groonga-commit] groonga/groonga at cd132e1 [master] table_remove: disable removing patricia trie table referenced by scalar column

Back to archive index

Kouhei Sutou null+****@clear*****
Sat Aug 3 18:30:17 JST 2013


Kouhei Sutou	2013-08-03 18:30:17 +0900 (Sat, 03 Aug 2013)

  New Revision: cd132e11beb48b59dac6d1d8abae15aaa4b87a3a
  https://github.com/groonga/groonga/commit/cd132e11beb48b59dac6d1d8abae15aaa4b87a3a

  Message:
    table_remove: disable removing patricia trie table referenced by scalar column

  Added files:
    test/command/suite/table_remove/patricia_trie/referenced/column_scalar.expected
    test/command/suite/table_remove/patricia_trie/referenced/column_scalar.test
  Modified files:
    lib/db.c

  Modified: lib/db.c (+59 -0)
===================================================================
--- lib/db.c    2013-08-03 17:35:58 +0900 (d653be1)
+++ lib/db.c    2013-08-03 18:30:17 +0900 (fafd1c4)
@@ -6639,10 +6639,69 @@ _grn_obj_remove_db(grn_ctx *ctx, grn_obj *obj, grn_obj *db, grn_id id,
   if (path) { grn_pat_remove(ctx, path); }
 }
 
+static grn_bool
+is_removable_table(grn_ctx *ctx, grn_obj *table, grn_obj *db)
+{
+  grn_bool removable = GRN_TRUE;
+  grn_id table_id;
+  char table_name[GRN_TABLE_MAX_KEY_SIZE];
+  int table_name_size;
+  grn_table_cursor *cursor;
+
+  table_id = DB_OBJ(table)->id;
+  table_name_size = grn_obj_name(ctx, table, table_name, GRN_TABLE_MAX_KEY_SIZE);
+  if ((cursor = grn_table_cursor_open(ctx, db, NULL, 0, NULL, 0, 0, -1,
+                                      GRN_CURSOR_BY_ID))) {
+    grn_id id;
+    while ((id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL) {
+      grn_obj *object;
+
+      object = grn_ctx_at(ctx, id);
+      if (!object) {
+        ERRCLR(ctx);
+        continue;
+      }
+
+      switch (object->header.type) {
+      case GRN_COLUMN_FIX_SIZE :
+        if (object->header.domain == table_id) {
+          break;
+        }
+        if (DB_OBJ(object)->range == table_id) {
+          char column_name[GRN_TABLE_MAX_KEY_SIZE];
+          int column_name_size;
+          column_name_size = grn_obj_name(ctx, object, column_name,
+                                          GRN_TABLE_MAX_KEY_SIZE);
+          ERR(GRN_OPERATION_NOT_PERMITTED,
+              "[table][remove] column that references the table exists: "
+              "<%.*s> -> <%.*s>",
+              column_name_size, column_name,
+              table_name_size, table_name);
+          removable = GRN_FALSE;
+        }
+        break;
+      default:
+        break;
+      }
+      grn_obj_unlink(ctx, object);
+
+      if (!removable) {
+        break;
+      }
+    }
+    grn_table_cursor_close(ctx, cursor);
+  }
+
+  return removable;
+}
+
 static void
 _grn_obj_remove_pat(grn_ctx *ctx, grn_obj *obj, grn_obj *db, grn_id id,
                     const char *path)
 {
+  if (!is_removable_table(ctx, obj, db)) {
+    return;
+  }
   remove_index(ctx, obj, GRN_HOOK_INSERT);
   remove_columns(ctx, obj);
   grn_obj_close(ctx, obj);

  Added: test/command/suite/table_remove/patricia_trie/referenced/column_scalar.expected (+24 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/table_remove/patricia_trie/referenced/column_scalar.expected    2013-08-03 18:30:17 +0900 (bbded74)
@@ -0,0 +1,24 @@
+table_create Names TABLE_PAT_KEY ShortText
+[[0,0.0,0.0],true]
+table_create Users TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Users name COLUMN_SCALAR Names
+[[0,0.0,0.0],true]
+table_remove Names
+[
+  [
+    [
+      -2,
+      0.0,
+      0.0
+    ],
+    "[table][remove] column that references the table exists: <Users.name> -> <Names>"
+  ],
+  false
+]
+#|e| [table][remove] column that references the table exists: <Users.name> -> <Names>
+dump
+table_create Names TABLE_PAT_KEY ShortText
+table_create Users TABLE_HASH_KEY ShortText
+column_create Users name COLUMN_SCALAR Names
+

  Added: test/command/suite/table_remove/patricia_trie/referenced/column_scalar.test (+7 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/table_remove/patricia_trie/referenced/column_scalar.test    2013-08-03 18:30:17 +0900 (34d3c4e)
@@ -0,0 +1,7 @@
+table_create Names TABLE_PAT_KEY ShortText
+table_create Users TABLE_HASH_KEY ShortText
+column_create Users name COLUMN_SCALAR Names
+
+table_remove Names
+
+dump
-------------- next part --------------
HTML����������������������������...
Download 



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