[Groonga-commit] groonga/groonga at 5904197 [master] sub_filter: support accessor that contains index column as scope

Back to archive index

Kouhei Sutou null+****@clear*****
Fri May 8 00:01:31 JST 2015


Kouhei Sutou	2015-05-08 00:01:31 +0900 (Fri, 08 May 2015)

  New Revision: 59041977386ac7b47611748f5feef41ebb081d26
  https://github.com/groonga/groonga/commit/59041977386ac7b47611748f5feef41ebb081d26

  Message:
    sub_filter: support accessor that contains index column as scope

  Added files:
    test/command/suite/select/function/sub_filter/accessor/index.expected
    test/command/suite/select/function/sub_filter/accessor/index.test
  Modified files:
    lib/db.c
    test/command/suite/select/function/sub_filter/column/index.expected
    test/command/suite/select/function/sub_filter/column/index.test

  Modified: lib/db.c (+98 -28)
===================================================================
--- lib/db.c    2015-05-07 22:45:48 +0900 (37227c2)
+++ lib/db.c    2015-05-08 00:01:31 +0900 (944a9b2)
@@ -2780,17 +2780,44 @@ grn_accessor_resolve(grn_ctx *ctx, grn_obj *accessor, int deep,
   }
 
   for (i = n_accessors; i > 0; i--) {
-    grn_obj *index;
+    grn_obj *index = NULL;
+    grn_obj *column = NULL;
     grn_operator index_op = GRN_OP_MATCH;
+    grn_id next_res_domain_id = GRN_ID_NIL;
 
     a = (grn_accessor *)GRN_PTR_VALUE_AT(&accessor_stack, i - 1);
     if (a->obj->header.type == GRN_COLUMN_INDEX) {
-      index = a->obj;
+      grn_obj source_ids;
+      unsigned int i, n_ids;
+
+      next_res_domain_id = a->obj->header.domain;
+
+      GRN_UINT32_INIT(&source_ids, GRN_OBJ_VECTOR);
+      grn_obj_get_info(ctx, a->obj, GRN_INFO_SOURCE, &source_ids);
+      n_ids = GRN_BULK_VSIZE(&source_ids) / sizeof(grn_id);
+      for (i = 0; i < n_ids; i++) {
+        grn_id source_id;
+        grn_obj *source;
+
+        source_id = GRN_UINT32_VALUE_AT(&source_ids, i);
+        source = grn_ctx_at(ctx, source_id);
+        if (DB_OBJ(source)->range == next_res_domain_id) {
+          column = source;
+          break;
+        }
+        grn_obj_unlink(ctx, source);
+      }
+
+      if (!column) {
+        rc = GRN_INVALID_ARGUMENT;
+        break;
+      }
     } else {
       if (grn_column_index(ctx, a->obj, index_op, &index, 1, NULL) == 0) {
         rc = GRN_INVALID_ARGUMENT;
         break;
       }
+      next_res_domain_id = DB_OBJ(index)->range;
     }
 
     {
@@ -2798,12 +2825,12 @@ grn_accessor_resolve(grn_ctx *ctx, grn_obj *accessor, int deep,
       grn_obj *next_res;
       grn_rset_recinfo *recinfo;
       {
-        grn_obj *range = grn_ctx_at(ctx, DB_OBJ(index)->range);
+        grn_obj *next_res_domain = grn_ctx_at(ctx, next_res_domain_id);
         next_res = grn_table_create(ctx, NULL, 0, NULL,
                                     GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC,
-                                    range, NULL);
+                                    next_res_domain, NULL);
         rc = ctx->rc;
-        grn_obj_unlink(ctx, range);
+        grn_obj_unlink(ctx, next_res_domain);
         if (!next_res) {
           if (current_res != base_res) {
             grn_obj_unlink(ctx, current_res);
@@ -2811,33 +2838,76 @@ grn_accessor_resolve(grn_ctx *ctx, grn_obj *accessor, int deep,
           break;
         }
       }
-      GRN_HASH_EACH(ctx, (grn_hash *)current_res, id, &tid, NULL, &recinfo, {
-        grn_ii *ii = (grn_ii *)index;
-        grn_ii_cursor *ii_cursor;
-        grn_ii_posting *posting;
+      if (index) {
+        GRN_HASH_EACH(ctx, (grn_hash *)current_res, id, &tid, NULL, &recinfo, {
+          grn_ii *ii = (grn_ii *)index;
+          grn_ii_cursor *ii_cursor;
+          grn_ii_posting *posting;
 
-        ii_cursor = grn_ii_cursor_open(ctx, ii, *tid,
-                                       GRN_ID_NIL, GRN_ID_MAX,
-                                       ii->n_elements,
-                                       0);
-        if (!ii_cursor) {
-          continue;
-        }
+          ii_cursor = grn_ii_cursor_open(ctx, ii, *tid,
+                                         GRN_ID_NIL, GRN_ID_MAX,
+                                         ii->n_elements,
+                                         0);
+          if (!ii_cursor) {
+            continue;
+          }
 
-        while ((posting = grn_ii_cursor_next(ctx, ii_cursor))) {
-          grn_ii_posting add_posting = *posting;
-          add_posting.weight += recinfo->score - 1;
-          grn_ii_posting_add(ctx,
-                             &add_posting,
-                             (grn_hash *)next_res,
-                             GRN_OP_OR);
-        }
-        grn_ii_cursor_close(ctx, ii_cursor);
+          while ((posting = grn_ii_cursor_next(ctx, ii_cursor))) {
+            grn_ii_posting add_posting = *posting;
+            add_posting.weight += recinfo->score - 1;
+            grn_ii_posting_add(ctx,
+                               &add_posting,
+                               (grn_hash *)next_res,
+                               GRN_OP_OR);
+          }
+          grn_ii_cursor_close(ctx, ii_cursor);
 
-        if (rc != GRN_SUCCESS) {
-          break;
+          if (rc != GRN_SUCCESS) {
+            break;
+          }
+        });
+      } else {
+        grn_obj_flags column_value_flags = 0;
+        grn_obj column_value;
+        grn_ii_posting add_posting;
+
+        if (column->header.type == GRN_COLUMN_VAR_SIZE) {
+          column_value_flags |= GRN_OBJ_VECTOR;
         }
-      });
+        GRN_VALUE_FIX_SIZE_INIT(&column_value,
+                                column_value_flags,
+                                next_res_domain_id);
+
+        add_posting.sid = 0;
+        add_posting.pos = 0;
+        add_posting.weight = 0;
+
+        GRN_HASH_EACH(ctx, (grn_hash *)current_res, id, &tid, NULL, &recinfo, {
+          int i;
+          int n_elements;
+          grn_rc rc = GRN_SUCCESS;
+
+          add_posting.weight = recinfo->score - 1;
+
+          GRN_BULK_REWIND(&column_value);
+          grn_obj_get_value(ctx, column, *tid, &column_value);
+
+          n_elements = GRN_BULK_VSIZE(&column_value) / sizeof(grn_id);
+          for (i = 0; i < n_elements; i++) {
+            add_posting.rid = GRN_RECORD_VALUE_AT(&column_value, i);
+            rc = grn_ii_posting_add(ctx,
+                                    &add_posting,
+                                    (grn_hash *)next_res,
+                                    GRN_OP_OR);
+            if (rc != GRN_SUCCESS) {
+              break;
+            }
+          }
+          if (rc != GRN_SUCCESS) {
+            break;
+          }
+        });
+      }
       if (current_res != base_res) {
         grn_obj_unlink(ctx, current_res);
       }

  Added: test/command/suite/select/function/sub_filter/accessor/index.expected (+78 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/sub_filter/accessor/index.expected    2015-05-08 00:01:31 +0900 (bf62e44)
@@ -0,0 +1,78 @@
+table_create Users TABLE_PAT_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Users birthday COLUMN_SCALAR Time
+[[0,0.0,0.0],true]
+table_create Files TABLE_PAT_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Files author COLUMN_SCALAR Users
+[[0,0.0,0.0],true]
+column_create Users files_author_index COLUMN_INDEX Files author
+[[0,0.0,0.0],true]
+table_create Groups TABLE_PAT_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Groups users COLUMN_VECTOR Users
+[[0,0.0,0.0],true]
+column_create Users groups_index COLUMN_INDEX Groups users
+[[0,0.0,0.0],true]
+load --table Users
+[
+{"_key": "Alice",  "birthday": "1992-02-09 00:00:00"},
+{"_key": "Bob",    "birthday": "1988-01-04 00:00:00"},
+{"_key": "Carlos", "birthday": "1982-12-29 00:00:00"}
+]
+[[0,0.0,0.0],3]
+load --table Files
+[
+{"_key": "include/groonga.h", "author": "Alice"},
+{"_key": "src/groonga.c",     "author": "Bob"},
+{"_key": "lib/groonga.rb",    "author": "Carlos"},
+{"_key": "README.textile",    "author": "Alice"},
+{"_key": "ha_mroonga.cc",     "author": "Bob"},
+{"_key": "ha_mroonga.hpp",    "author": "Carlos"}
+]
+[[0,0.0,0.0],6]
+load --table Groups
+[
+{"_key": "group1", "users": ["Alice"]},
+{"_key": "group2", "users": ["Bob"]},
+{"_key": "group3", "users": ["Alice", "Carlos"]}
+]
+[[0,0.0,0.0],3]
+select Groups   --filter 'sub_filter(users.files_author_index, "_key @^ \\"ha_\\"")'   --output_columns '_key, users'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        2
+      ],
+      [
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "users",
+          "Users"
+        ]
+      ],
+      [
+        "group3",
+        [
+          "Alice",
+          "Carlos"
+        ]
+      ],
+      [
+        "group2",
+        [
+          "Bob"
+        ]
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/sub_filter/accessor/index.test (+42 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/sub_filter/accessor/index.test    2015-05-08 00:01:31 +0900 (ca1ad35)
@@ -0,0 +1,42 @@
+table_create Users TABLE_PAT_KEY ShortText
+column_create Users birthday COLUMN_SCALAR Time
+
+table_create Files TABLE_PAT_KEY ShortText
+column_create Files author COLUMN_SCALAR Users
+
+column_create Users files_author_index COLUMN_INDEX Files author
+
+
+table_create Groups TABLE_PAT_KEY ShortText
+column_create Groups users COLUMN_VECTOR Users
+
+column_create Users groups_index COLUMN_INDEX Groups users
+
+
+load --table Users
+[
+{"_key": "Alice",  "birthday": "1992-02-09 00:00:00"},
+{"_key": "Bob",    "birthday": "1988-01-04 00:00:00"},
+{"_key": "Carlos", "birthday": "1982-12-29 00:00:00"}
+]
+
+load --table Files
+[
+{"_key": "include/groonga.h", "author": "Alice"},
+{"_key": "src/groonga.c",     "author": "Bob"},
+{"_key": "lib/groonga.rb",    "author": "Carlos"},
+{"_key": "README.textile",    "author": "Alice"},
+{"_key": "ha_mroonga.cc",     "author": "Bob"},
+{"_key": "ha_mroonga.hpp",    "author": "Carlos"}
+]
+
+load --table Groups
+[
+{"_key": "group1", "users": ["Alice"]},
+{"_key": "group2", "users": ["Bob"]},
+{"_key": "group3", "users": ["Alice", "Carlos"]}
+]
+
+select Groups \
+  --filter 'sub_filter(users.files_author_index, "_key @^ \\"ha_\\"")' \
+  --output_columns '_key, users'

  Modified: test/command/suite/select/function/sub_filter/column/index.expected (+15 -8)
===================================================================
--- test/command/suite/select/function/sub_filter/column/index.expected    2015-05-07 22:45:48 +0900 (e1f3469)
+++ test/command/suite/select/function/sub_filter/column/index.expected    2015-05-08 00:01:31 +0900 (028be0e)
@@ -14,13 +14,6 @@ table_create Lexicon TABLE_PAT_KEY|KEY_NORMALIZE ShortText   --default_tokenizer
 [[0,0.0,0.0],true]
 column_create Lexicon comments_content COLUMN_INDEX|WITH_POSITION   Comments content
 [[0,0.0,0.0],true]
-load --table Comments
-[
-{"_key": "comment1", "article": "article1", "content": "I'm using Groonga too!"},
-{"_key": "comment2", "article": "article3", "content": "I'm using Groonga and Mroonga!"},
-{"_key": "comment3", "article": "article1", "content": "I'm using Groonga too!"}
-]
-[[0,0.0,0.0],3]
 load --table Articles
 [
 {"_key": "article1", "content": "Groonga is fast!"},
@@ -28,6 +21,16 @@ load --table Articles
 {"_key": "article3", "content": "Mroonga is fast!"}
 ]
 [[0,0.0,0.0],3]
+load --table Comments
+[
+{"_key": "comment1", "article": "article1", "content": "I'm using Groonga too!"},
+{"_key": "comment2", "article": "article3", "content": "I'm using Groonga and Mroonga!"},
+{"_key": "comment3", "article": "article1", "content": "I'm using Groonga too!"},
+{"_key": "comment4", "article": "article2", "content": "Good news!"},
+{"_key": "comment5", "article": "article3", "content": "Wow!"},
+{"_key": "comment6", "article": "article1", "content": "Mroonga is also fast!"}
+]
+[[0,0.0,0.0],6]
 select Articles   --filter 'sub_filter(comment_index, "content @ \\"Mroonga\\"")'   --output_columns "_key, content"
 [
   [
@@ -38,7 +41,7 @@ select Articles   --filter 'sub_filter(comment_index, "content @ \\"Mroonga\\"")
   [
     [
       [
-        1
+        2
       ],
       [
         [
@@ -53,6 +56,10 @@ select Articles   --filter 'sub_filter(comment_index, "content @ \\"Mroonga\\"")
       [
         "article3",
         "Mroonga is fast!"
+      ],
+      [
+        "article1",
+        "Groonga is fast!"
       ]
     ]
   ]

  Modified: test/command/suite/select/function/sub_filter/column/index.test (+10 -7)
===================================================================
--- test/command/suite/select/function/sub_filter/column/index.test    2015-05-07 22:45:48 +0900 (75c2ee9)
+++ test/command/suite/select/function/sub_filter/column/index.test    2015-05-08 00:01:31 +0900 (53de754)
@@ -13,13 +13,6 @@ column_create Lexicon comments_content COLUMN_INDEX|WITH_POSITION \
   Comments content
 
 
-load --table Comments
-[
-{"_key": "comment1", "article": "article1", "content": "I'm using Groonga too!"},
-{"_key": "comment2", "article": "article3", "content": "I'm using Groonga and Mroonga!"},
-{"_key": "comment3", "article": "article1", "content": "I'm using Groonga too!"}
-]
-
 load --table Articles
 [
 {"_key": "article1", "content": "Groonga is fast!"},
@@ -27,6 +20,16 @@ load --table Articles
 {"_key": "article3", "content": "Mroonga is fast!"}
 ]
 
+load --table Comments
+[
+{"_key": "comment1", "article": "article1", "content": "I'm using Groonga too!"},
+{"_key": "comment2", "article": "article3", "content": "I'm using Groonga and Mroonga!"},
+{"_key": "comment3", "article": "article1", "content": "I'm using Groonga too!"},
+{"_key": "comment4", "article": "article2", "content": "Good news!"},
+{"_key": "comment5", "article": "article3", "content": "Wow!"},
+{"_key": "comment6", "article": "article1", "content": "Mroonga is also fast!"}
+]
+
 select Articles \
   --filter 'sub_filter(comment_index, "content @ \\"Mroonga\\"")' \
   --output_columns "_key, content"
-------------- next part --------------
HTML����������������������������...
下载 



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