[Groonga-commit] groonga/groonga at 07369cc [master] Support alias

Back to archive index

Kouhei Sutou null+****@clear*****
Wed Jan 13 18:14:09 JST 2016


Kouhei Sutou	2016-01-13 18:14:09 +0900 (Wed, 13 Jan 2016)

  New Revision: 07369cc32003279c7c559657f38f44d2eec3a124
  https://github.com/groonga/groonga/commit/07369cc32003279c7c559657f38f44d2eec3a124

  Message:
    Support alias
    
    You can use different object name (table name, column name, tokenizer
    name and so son) by defining an alias name mapping.
    
    The mapping is consists of table that supports key (= not TABLE_NO_KEY)
    and ShortText scalar column. Key is the table is old name (= alias
    name) and column value is the new name (= real name).
    
    The old name and real name pair can be resolved recursively. Be careful
    to circular mapping!

  Added files:
    test/command/suite/select/filter/alias/nested.expected
    test/command/suite/select/filter/alias/nested.test
    test/command/suite/select/filter/alias/one_level.expected
    test/command/suite/select/filter/alias/one_level.test
  Modified files:
    lib/db.c

  Modified: lib/db.c (+60 -3)
===================================================================
--- lib/db.c    2016-01-13 16:46:14 +0900 (e448205)
+++ lib/db.c    2016-01-13 18:14:09 +0900 (629ced1)
@@ -510,7 +510,6 @@ grn_db_close(grn_ctx *ctx, grn_obj *db)
 grn_obj *
 grn_ctx_get(grn_ctx *ctx, const char *name, int name_size)
 {
-  grn_id id;
   grn_obj *obj = NULL;
   grn_obj *db;
   if (!ctx || !ctx->impl || !(db = ctx->impl->db)) {
@@ -519,12 +518,70 @@ grn_ctx_get(grn_ctx *ctx, const char *name, int name_size)
   GRN_API_ENTER;
   if (GRN_DB_P(db)) {
     grn_db *s = (grn_db *)db;
+    grn_obj *alias_table = NULL;
+    grn_obj *alias_column = NULL;
+    grn_obj alias_name_buffer;
+
     if (name_size < 0) {
       name_size = strlen(name);
     }
-    if ((id = grn_table_get(ctx, s->keys, name, name_size))) {
-      obj = grn_ctx_at(ctx, id);
+    GRN_TEXT_INIT(&alias_name_buffer, 0);
+    while (GRN_TRUE) {
+      grn_id id;
+
+      id = grn_table_get(ctx, s->keys, name, name_size);
+      if (id) {
+        obj = grn_ctx_at(ctx, id);
+        break;
+      }
+
+      if (!alias_column) {
+        grn_id alias_column_id;
+        const char *alias_column_name;
+        uint32_t alias_column_name_size;
+
+        grn_conf_get(ctx,
+                     "alias.column", -1,
+                     &alias_column_name, &alias_column_name_size);
+        if (!alias_column_name) {
+          break;
+        }
+        alias_column_id = grn_table_get(ctx,
+                                        s->keys,
+                                        alias_column_name,
+                                        alias_column_name_size);
+        if (!alias_column_id) {
+          break;
+        }
+        alias_column = grn_ctx_at(ctx, alias_column_id);
+        if (alias_column->header.type != GRN_COLUMN_VAR_SIZE) {
+          break;
+        }
+        if (alias_column->header.flags & GRN_OBJ_VECTOR) {
+          break;
+        }
+        if (DB_OBJ(alias_column)->range != GRN_DB_SHORT_TEXT) {
+          break;
+        }
+        alias_table = grn_ctx_at(ctx, alias_column->header.domain);
+        if (alias_table->header.type == GRN_TABLE_NO_KEY) {
+          break;
+        }
+      }
+
+      {
+        grn_id alias_id;
+        alias_id = grn_table_get(ctx, alias_table, name, name_size);
+        if (!alias_id) {
+          break;
+        }
+        GRN_BULK_REWIND(&alias_name_buffer);
+        grn_obj_get_value(ctx, alias_column, alias_id, &alias_name_buffer);
+        name = GRN_TEXT_VALUE(&alias_name_buffer);
+        name_size = GRN_TEXT_LEN(&alias_name_buffer);
+      }
     }
+    GRN_OBJ_FIN(ctx, &alias_name_buffer);
   }
   GRN_API_RETURN(obj);
 }

  Added: test/command/suite/select/filter/alias/nested.expected (+58 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/filter/alias/nested.expected    2016-01-13 18:14:09 +0900 (cd6615a)
@@ -0,0 +1,58 @@
+conf_set alias.column Aliases.real_name
+[[0,0.0,0.0],true]
+table_create Aliases TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Aliases real_name COLUMN_SCALAR ShortText
+[[0,0.0,0.0],true]
+table_create Memos TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Memos title COLUMN_SCALAR Text
+[[0,0.0,0.0],true]
+load --table Memos
+[
+{"_key": "groonga", "title": "Groonga"}
+]
+[[0,0.0,0.0],1]
+select Memos --filter 'caption == "Groonga"'
+[[[-63,0.0,0.0],"Syntax error: <caption| |== \"Groonga\">"],[]]
+#|e| Syntax error: <caption| |== "Groonga">
+load --table Aliases
+[
+{"_key": "Memos.caption", "real_name": "Memos.subject"},
+{"_key": "Memos.subject", "real_name": "Memos.title"}
+]
+[[0,0.0,0.0],2]
+select Memos --filter 'caption == "Groonga"'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "_id",
+          "UInt32"
+        ],
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "title",
+          "Text"
+        ]
+      ],
+      [
+        1,
+        "groonga",
+        "Groonga"
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/filter/alias/nested.test (+22 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/filter/alias/nested.test    2016-01-13 18:14:09 +0900 (b664597)
@@ -0,0 +1,22 @@
+conf_set alias.column Aliases.real_name
+
+table_create Aliases TABLE_HASH_KEY ShortText
+column_create Aliases real_name COLUMN_SCALAR ShortText
+
+table_create Memos TABLE_HASH_KEY ShortText
+column_create Memos title COLUMN_SCALAR Text
+
+load --table Memos
+[
+{"_key": "groonga", "title": "Groonga"}
+]
+
+select Memos --filter 'caption == "Groonga"'
+
+load --table Aliases
+[
+{"_key": "Memos.caption", "real_name": "Memos.subject"},
+{"_key": "Memos.subject", "real_name": "Memos.title"}
+]
+
+select Memos --filter 'caption == "Groonga"'

  Added: test/command/suite/select/filter/alias/one_level.expected (+57 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/filter/alias/one_level.expected    2016-01-13 18:14:09 +0900 (eba5943)
@@ -0,0 +1,57 @@
+conf_set alias.column Aliases.real_name
+[[0,0.0,0.0],true]
+table_create Aliases TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Aliases real_name COLUMN_SCALAR ShortText
+[[0,0.0,0.0],true]
+table_create Memos TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Memos title COLUMN_SCALAR Text
+[[0,0.0,0.0],true]
+load --table Memos
+[
+{"_key": "groonga", "title": "Groonga"}
+]
+[[0,0.0,0.0],1]
+select Memos --filter 'caption == "Groonga"'
+[[[-63,0.0,0.0],"Syntax error: <caption| |== \"Groonga\">"],[]]
+#|e| Syntax error: <caption| |== "Groonga">
+load --table Aliases
+[
+{"_key": "Memos.caption", "real_name": "Memos.title"}
+]
+[[0,0.0,0.0],1]
+select Memos --filter 'caption == "Groonga"'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "_id",
+          "UInt32"
+        ],
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "title",
+          "Text"
+        ]
+      ],
+      [
+        1,
+        "groonga",
+        "Groonga"
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/filter/alias/one_level.test (+21 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/filter/alias/one_level.test    2016-01-13 18:14:09 +0900 (d8a5490)
@@ -0,0 +1,21 @@
+conf_set alias.column Aliases.real_name
+
+table_create Aliases TABLE_HASH_KEY ShortText
+column_create Aliases real_name COLUMN_SCALAR ShortText
+
+table_create Memos TABLE_HASH_KEY ShortText
+column_create Memos title COLUMN_SCALAR Text
+
+load --table Memos
+[
+{"_key": "groonga", "title": "Groonga"}
+]
+
+select Memos --filter 'caption == "Groonga"'
+
+load --table Aliases
+[
+{"_key": "Memos.caption", "real_name": "Memos.title"}
+]
+
+select Memos --filter 'caption == "Groonga"'
-------------- next part --------------
HTML����������������������������...
Download 



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