[Groonga-commit] groonga/groonga at 9dc5a33 [master] select: support dynamic column as output stage

Back to archive index

Naoya Murakami null+****@clear*****
Sun Jun 26 18:50:30 JST 2016


Naoya Murakami	2016-06-26 18:50:30 +0900 (Sun, 26 Jun 2016)

  New Revision: 9dc5a33658ec59b460827b0bbab9b1bfb00d97ac
  https://github.com/groonga/groonga/commit/9dc5a33658ec59b460827b0bbab9b1bfb00d97ac

  Merged 4426854: Merge pull request #568 from naoa/dynamic-column-as-output

  Message:
    select: support dynamic column as output stage

  Added files:
    test/command/suite/select/columns/stage/output/limit.expected
    test/command/suite/select/columns/stage/output/limit.test
    test/command/suite/select/columns/stage/output/no_sort_keys.expected
    test/command/suite/select/columns/stage/output/no_sort_keys.test
  Modified files:
    lib/proc/proc_select.c

  Modified: lib/proc/proc_select.c (+90 -1)
===================================================================
--- lib/proc/proc_select.c    2016-06-26 18:21:57 +0900 (b649c95)
+++ lib/proc/proc_select.c    2016-06-26 18:50:30 +0900 (cafc357)
@@ -55,7 +55,8 @@ typedef struct {
 
 typedef enum {
   GRN_COLUMN_STAGE_INITIAL,
-  GRN_COLUMN_STAGE_FILTERED
+  GRN_COLUMN_STAGE_FILTERED,
+  GRN_COLUMN_STAGE_OUTPUT
 } grn_column_stage;
 
 typedef struct {
@@ -72,6 +73,7 @@ typedef struct {
 typedef struct {
   grn_hash *initial;
   grn_hash *filtered;
+  grn_hash *output;
 } grn_columns;
 
 typedef struct {
@@ -131,6 +133,7 @@ typedef struct {
     grn_obj *initial;
     grn_obj *result;
     grn_obj *sorted;
+    grn_obj *output;
   } tables;
   struct {
     grn_obj *match_columns;
@@ -256,6 +259,8 @@ grn_column_stage_name(grn_column_stage stage)
     return "initial";
   case GRN_COLUMN_STAGE_FILTERED :
     return "filtered";
+  case GRN_COLUMN_STAGE_OUTPUT :
+    return "output";
   default :
     return "unknown";
   }
@@ -445,6 +450,7 @@ grn_columns_init(grn_ctx *ctx, grn_columns *columns)
 {
   columns->initial = NULL;
   columns->filtered = NULL;
+  columns->output = NULL;
 }
 
 static void
@@ -457,6 +463,10 @@ grn_columns_fin(grn_ctx *ctx, grn_columns *columns)
   if (columns->filtered) {
     grn_hash_close(ctx, columns->filtered);
   }
+
+  if (columns->output) {
+    grn_hash_close(ctx, columns->output);
+  }
 }
 
 static grn_bool
@@ -522,6 +532,9 @@ grn_columns_collect(grn_ctx *ctx,
     } else if (GRN_BULK_EQUAL_STRING(value, "filtered")) {
       stage = GRN_COLUMN_STAGE_FILTERED;
       target_columns = &(columns->filtered);
+    } else if (GRN_BULK_EQUAL_STRING(value, "output")) {
+      stage = GRN_COLUMN_STAGE_OUTPUT;
+      target_columns = &(columns->output);
     } else {
       continue;
     }
@@ -581,6 +594,16 @@ grn_columns_fill(grn_ctx *ctx,
     }
   }
 
+  if (columns->output) {
+    if (!grn_column_data_collect(ctx,
+                                 user_data,
+                                 columns->output,
+                                 prefix,
+                                 prefix_length)) {
+      return GRN_FALSE;
+    }
+  }
+
   return GRN_TRUE;
 }
 
@@ -938,6 +961,41 @@ grn_select_create_all_selected_result_table(grn_ctx *ctx,
   return result;
 }
 
+static grn_obj *
+grn_select_create_offset_and_limit_sorted_table(grn_ctx *ctx,
+                                                grn_obj *table,
+                                                int offset,
+                                                int limit)
+{
+  grn_obj *result;
+  grn_table_cursor *cursor;
+
+  result = grn_table_create(ctx, NULL, 0, NULL,
+                            GRN_OBJ_TABLE_NO_KEY,
+                            NULL,
+                            table);
+
+  if (!result) {
+    return NULL;
+  }
+
+  cursor = grn_table_cursor_open(ctx, table, NULL, 0, NULL, 0,
+                                 offset, limit, GRN_CURSOR_ASCENDING);
+  if (cursor) {
+    grn_id id;
+    while ((id = grn_table_cursor_next(ctx, cursor))) {
+      grn_id *value;
+      if (grn_array_add(ctx, (grn_array *)result, (void **)&value)) {
+        *value = id;
+      }
+    }
+    grn_table_cursor_close(ctx, cursor);
+  }
+
+  return result;
+}
+
+
 static void
 grn_select_apply_columns(grn_ctx *ctx,
                          grn_select_data *data,
@@ -1574,6 +1632,33 @@ grn_select_sort(grn_ctx *ctx,
 }
 
 static grn_bool
+grn_select_apply_output_columns(grn_ctx *ctx,
+                                grn_select_data *data)
+{
+  if (!data->columns.output) {
+    return GRN_TRUE;
+  }
+
+  if (!data->tables.sorted) {
+    data->tables.sorted =
+      grn_select_create_offset_and_limit_sorted_table(ctx,
+                                                      data->tables.result,
+                                                      data->offset,
+                                                      data->limit);
+    if (!data->tables.sorted) {
+      return GRN_FALSE;
+    }
+  }
+
+  grn_select_apply_columns(ctx,
+                           data,
+                           data->tables.sorted,
+                           data->columns.output);
+
+  return ctx->rc == GRN_SUCCESS;
+}
+
+static grn_bool
 grn_select_output_match_open(grn_ctx *ctx,
                              grn_select_data *data,
                              grn_obj_format *format,
@@ -2876,6 +2961,10 @@ grn_select(grn_ctx *ctx, grn_select_data *data)
         goto exit;
       }
 
+      if (!grn_select_apply_output_columns(ctx, data)) {
+        goto exit;
+      }
+
       if (!grn_select_prepare_slices(ctx, data)) {
         goto exit;
       }

  Added: test/command/suite/select/columns/stage/output/limit.expected (+44 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/columns/stage/output/limit.expected    2016-06-26 18:50:30 +0900 (bd06f97)
@@ -0,0 +1,44 @@
+table_create Memos TABLE_NO_KEY
+[[0,0.0,0.0],true]
+column_create Memos content COLUMN_SCALAR Text
+[[0,0.0,0.0],true]
+load --table Memos
+[
+{"content": "Groonga is fast."},
+{"content": "Mroonga is fast and easy to use."},
+{"content": "PGroonga is fast and easy to use."}
+]
+[[0,0.0,0.0],3]
+select Memos   --query 'content:@fast'   --sort_keys -_id   --limit 2   --columns[highlighted_content].stage output   --columns[highlighted_content].type Text   --columns[highlighted_content].flags COLUMN_SCALAR   --columns[highlighted_content].value 'highlight_html(content)'   --output_columns _id,highlighted_content
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        3
+      ],
+      [
+        [
+          "_id",
+          "UInt32"
+        ],
+        [
+          "highlighted_content",
+          "Text"
+        ]
+      ],
+      [
+        3,
+        "PGroonga is <span class=\"keyword\">fast</span> and easy to use."
+      ],
+      [
+        2,
+        "Mroonga is <span class=\"keyword\">fast</span> and easy to use."
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/columns/stage/output/limit.test (+19 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/columns/stage/output/limit.test    2016-06-26 18:50:30 +0900 (3baf10c)
@@ -0,0 +1,19 @@
+table_create Memos TABLE_NO_KEY
+column_create Memos content COLUMN_SCALAR Text
+
+load --table Memos
+[
+{"content": "Groonga is fast."},
+{"content": "Mroonga is fast and easy to use."},
+{"content": "PGroonga is fast and easy to use."}
+]
+
+select Memos \
+  --query 'content:@fast' \
+  --sort_keys -_id \
+  --limit 2 \
+  --columns[highlighted_content].stage output \
+  --columns[highlighted_content].type Text \
+  --columns[highlighted_content].flags COLUMN_SCALAR \
+  --columns[highlighted_content].value 'highlight_html(content)' \
+  --output_columns _id,highlighted_content

  Added: test/command/suite/select/columns/stage/output/no_sort_keys.expected (+44 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/columns/stage/output/no_sort_keys.expected    2016-06-26 18:50:30 +0900 (cf6f6d4)
@@ -0,0 +1,44 @@
+table_create Memos TABLE_NO_KEY
+[[0,0.0,0.0],true]
+column_create Memos content COLUMN_SCALAR Text
+[[0,0.0,0.0],true]
+load --table Memos
+[
+{"content": "Groonga is fast."},
+{"content": "Mroonga is fast and easy to use."},
+{"content": "PGroonga is fast and easy to use."}
+]
+[[0,0.0,0.0],3]
+select Memos   --query 'content:@fast'   --limit 2   --columns[highlighted_content].stage output   --columns[highlighted_content].type Text   --columns[highlighted_content].flags COLUMN_SCALAR   --columns[highlighted_content].value 'highlight_html(content)'   --output_columns _id,highlighted_content
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        3
+      ],
+      [
+        [
+          "_id",
+          "UInt32"
+        ],
+        [
+          "highlighted_content",
+          "Text"
+        ]
+      ],
+      [
+        1,
+        "Groonga is <span class=\"keyword\">fast</span>."
+      ],
+      [
+        2,
+        "Mroonga is <span class=\"keyword\">fast</span> and easy to use."
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/columns/stage/output/no_sort_keys.test (+18 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/columns/stage/output/no_sort_keys.test    2016-06-26 18:50:30 +0900 (1582f87)
@@ -0,0 +1,18 @@
+table_create Memos TABLE_NO_KEY
+column_create Memos content COLUMN_SCALAR Text
+
+load --table Memos
+[
+{"content": "Groonga is fast."},
+{"content": "Mroonga is fast and easy to use."},
+{"content": "PGroonga is fast and easy to use."}
+]
+
+select Memos \
+  --query 'content:@fast' \
+  --limit 2 \
+  --columns[highlighted_content].stage output \
+  --columns[highlighted_content].type Text \
+  --columns[highlighted_content].flags COLUMN_SCALAR \
+  --columns[highlighted_content].value 'highlight_html(content)' \
+  --output_columns _id,highlighted_content
-------------- next part --------------
HTML����������������������������...
下載 



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