[Groonga-commit] groonga/groonga at 1d4186a [master] select: extract code for drilldown

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Nov 16 16:39:30 JST 2014


Kouhei Sutou	2014-11-16 16:39:30 +0900 (Sun, 16 Nov 2014)

  New Revision: 1d4186a2369bfaf5c2fe62b650d8f8b59433da83
  https://github.com/groonga/groonga/commit/1d4186a2369bfaf5c2fe62b650d8f8b59433da83

  Message:
    select: extract code for drilldown

  Modified files:
    lib/proc.c

  Modified: lib/proc.c (+79 -56)
===================================================================
--- lib/proc.c    2014-11-16 15:52:56 +0900 (6a9315f)
+++ lib/proc.c    2014-11-16 16:39:30 +0900 (9babbed)
@@ -539,6 +539,78 @@ grn_select_apply_adjuster(grn_ctx *ctx, grn_obj *table, grn_obj *res,
   }
 }
 
+static void
+grn_select_drilldown(grn_ctx *ctx, grn_obj *table,
+                     grn_table_sort_key *keys, uint32_t n_keys,
+                     int offset, int limit,
+                     const char *sortby, unsigned int sortby_len,
+                     const char *output_columns, unsigned int output_columns_len)
+{
+  uint32_t i;
+  grn_table_group_result g = {NULL, 0, 0, 1, GRN_TABLE_GROUP_CALC_COUNT, 0};
+  if (keys) {
+    for (i = 0; i < n_keys; i++) {
+      uint32_t n_hits;
+      int real_offset = offset;
+      int real_limit = limit;
+
+      g.table = grn_table_create_for_group(ctx, NULL, 0, NULL,
+                                           keys[i].key, table, 0);
+      if (!g.table) {
+        continue;
+      }
+
+      grn_table_group(ctx, table, &keys[i], 1, &g, 1);
+      n_hits = grn_table_size(ctx, g.table);
+
+      grn_normalize_offset_and_limit(ctx, n_hits, &real_offset, &real_limit);
+
+      if (sortby_len) {
+        grn_table_sort_key *sort_keys;
+        uint32_t n_sort_keys;
+        sort_keys = grn_table_sort_key_from_str(ctx,
+                                                sortby, sortby_len,
+                                                g.table, &n_sort_keys);
+        if (sort_keys) {
+          grn_obj *sorted;
+          sorted = grn_table_create(ctx, NULL, 0, NULL, GRN_OBJ_TABLE_NO_KEY,
+                                    NULL, g.table);
+          if (sorted) {
+            grn_obj_format format;
+            grn_table_sort(ctx, g.table, real_offset, real_limit,
+                           sorted, sort_keys, n_sort_keys);
+            GRN_OBJ_FORMAT_INIT(&format, n_hits, 0, real_limit, real_offset);
+            format.flags =
+              GRN_OBJ_FORMAT_WITH_COLUMN_NAMES|
+              GRN_OBJ_FORMAT_XML_ELEMENT_NAVIGATIONENTRY;
+            grn_obj_columns(ctx, sorted,
+                            output_columns, output_columns_len,
+                            &format.columns);
+            GRN_OUTPUT_OBJ(sorted, &format);
+            GRN_OBJ_FORMAT_FIN(ctx, &format);
+            grn_obj_unlink(ctx, sorted);
+          }
+          grn_table_sort_key_close(ctx, sort_keys, n_sort_keys);
+        }
+      } else {
+        grn_obj_format format;
+        GRN_OBJ_FORMAT_INIT(&format, n_hits, real_offset,
+                            real_limit, real_offset);
+        format.flags =
+          GRN_OBJ_FORMAT_WITH_COLUMN_NAMES|
+          GRN_OBJ_FORMAT_XML_ELEMENT_NAVIGATIONENTRY;
+        grn_obj_columns(ctx, g.table, output_columns, output_columns_len,
+                        &format.columns);
+        GRN_OUTPUT_OBJ(g.table, &format);
+        GRN_OBJ_FORMAT_FIN(ctx, &format);
+      }
+      grn_obj_unlink(ctx, g.table);
+      GRN_QUERY_LOG(ctx, GRN_QUERY_LOG_SIZE,
+                    ":", "drilldown(%d)", n_hits);
+    }
+  }
+}
+
 grn_rc
 grn_select(grn_ctx *ctx, const char *table, unsigned int table_len,
            const char *match_columns, unsigned int match_columns_len,
@@ -833,62 +905,13 @@ grn_select(grn_ctx *ctx, const char *table, unsigned int table_len,
       }
       GRN_QUERY_LOG(ctx, GRN_QUERY_LOG_SIZE,
                     ":", "output(%d)", limit);
-      if (!ctx->rc && drilldown_len) {
-        uint32_t i;
-        grn_table_group_result g = {NULL, 0, 0, 1, GRN_TABLE_GROUP_CALC_COUNT, 0};
-        if (gkeys) {
-          for (i = 0; i < ngkeys; i++) {
-            if ((g.table = grn_table_create_for_group(ctx, NULL, 0, NULL,
-                                                      gkeys[i].key, res, 0))) {
-              int n_drilldown_offset = drilldown_offset,
-                  n_drilldown_limit = drilldown_limit;
-
-              grn_table_group(ctx, res, &gkeys[i], 1, &g, 1);
-              nhits = grn_table_size(ctx, g.table);
-
-              grn_normalize_offset_and_limit(ctx, nhits,
-                                             &n_drilldown_offset, &n_drilldown_limit);
-
-              if (drilldown_sortby_len) {
-                if ((keys = grn_table_sort_key_from_str(ctx,
-                                                        drilldown_sortby, drilldown_sortby_len,
-                                                        g.table, &nkeys))) {
-                  if ((sorted = grn_table_create(ctx, NULL, 0, NULL, GRN_OBJ_TABLE_NO_KEY,
-                                                 NULL, g.table))) {
-                    grn_table_sort(ctx, g.table, n_drilldown_offset, n_drilldown_limit,
-                                   sorted, keys, nkeys);
-                    GRN_OBJ_FORMAT_INIT(&format, nhits, 0,
-                                        n_drilldown_limit, n_drilldown_offset);
-                    format.flags =
-                      GRN_OBJ_FORMAT_WITH_COLUMN_NAMES|
-                      GRN_OBJ_FORMAT_XML_ELEMENT_NAVIGATIONENTRY;
-                    grn_obj_columns(ctx, sorted,
-                                    drilldown_output_columns, drilldown_output_columns_len,
-                                    &format.columns);
-                    GRN_OUTPUT_OBJ(sorted, &format);
-                    GRN_OBJ_FORMAT_FIN(ctx, &format);
-                    grn_obj_unlink(ctx, sorted);
-                  }
-                  grn_table_sort_key_close(ctx, keys, nkeys);
-                }
-              } else {
-                GRN_OBJ_FORMAT_INIT(&format, nhits, n_drilldown_offset,
-                                    n_drilldown_limit, n_drilldown_offset);
-                format.flags =
-                  GRN_OBJ_FORMAT_WITH_COLUMN_NAMES|
-                  GRN_OBJ_FORMAT_XML_ELEMENT_NAVIGATIONENTRY;
-                grn_obj_columns(ctx, g.table, drilldown_output_columns,
-                                drilldown_output_columns_len, &format.columns);
-                GRN_OUTPUT_OBJ(g.table, &format);
-                GRN_OBJ_FORMAT_FIN(ctx, &format);
-              }
-              grn_obj_unlink(ctx, g.table);
-            }
-            GRN_QUERY_LOG(ctx, GRN_QUERY_LOG_SIZE,
-                          ":", "drilldown(%d)", nhits);
-          }
-          grn_table_sort_key_close(ctx, gkeys, ngkeys);
-        }
+      if (!ctx->rc && gkeys) {
+        grn_select_drilldown(ctx, res, gkeys, ngkeys,
+                             drilldown_offset, drilldown_limit,
+                             drilldown_sortby, drilldown_sortby_len,
+                             drilldown_output_columns,
+                             drilldown_output_columns_len);
+        grn_table_sort_key_close(ctx, gkeys, ngkeys);
       }
       if (res != table_) { grn_obj_unlink(ctx, res); }
     } else {
-------------- next part --------------
HTML����������������������������...
下載 



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