[Groonga-commit] groonga/groonga-command at 3a4d8f5 [master] select: support labeled drilldowns

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Oct 11 16:38:52 JST 2016


Kouhei Sutou	2016-10-11 16:38:52 +0900 (Tue, 11 Oct 2016)

  New Revision: 3a4d8f5830f7c26edb2327409b43aa60069df3d7
  https://github.com/groonga/groonga-command/commit/3a4d8f5830f7c26edb2327409b43aa60069df3d7

  Message:
    select: support labeled drilldowns

  Modified files:
    lib/groonga/command/base.rb
    lib/groonga/command/select.rb
    test/command/test-select.rb

  Modified: lib/groonga/command/base.rb (+11 -2)
===================================================================
--- lib/groonga/command/base.rb    2016-08-15 16:07:33 +0900 (116c887)
+++ lib/groonga/command/base.rb    2016-10-11 16:38:52 +0900 (fb44e0e)
@@ -184,8 +184,12 @@ module Groonga
       end
 
       def integer_value(name)
-        value = self[name]
+        parse_integer_value(self[name])
+      end
+
+      def parse_integer_value(value)
         return value if value.nil?
+        return nil if value.empty?
 
         begin
           Integer(value)
@@ -195,7 +199,12 @@ module Groonga
       end
 
       def array_value(name)
-        (self[name] || "").strip.split(/\s*,\s*/)
+        parse_array_value(self[name] || "")
+      end
+
+      def parse_array_value(value)
+        return nil if value.nil?
+        value.strip.split(/\s*,\s*/)
       end
     end
   end

  Modified: lib/groonga/command/select.rb (+51 -3)
===================================================================
--- lib/groonga/command/select.rb    2016-08-15 16:07:33 +0900 (0d319f8)
+++ lib/groonga/command/select.rb    2016-10-11 16:38:52 +0900 (a8f8861)
@@ -1,6 +1,4 @@
-# -*- coding: utf-8 -*-
-#
-# Copyright (C) 2012-2013  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2012-2016  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -77,6 +75,10 @@ module Groonga
         @drilldowns ||= array_value(:drilldown)
       end
 
+      def labeled_drilldowns
+        @labeled_drilldowns ||= parse_labeled_drilldowns
+      end
+
       def output_columns
         self[:output_columns]
       end
@@ -90,6 +92,52 @@ module Groonga
           condition
         end
       end
+
+      def parse_labeled_drilldowns
+        raw_labeled_drilldowns = {}
+        @arguments.each do |name, value|
+          case name.to_s
+          when /\Adrilldowns?\[(.+?)\]\.(.+?)\z/
+            label = $1
+            parameter_name = $2
+            raw_labeled_drilldowns[label] ||= {}
+            raw_labeled_drilldowns[label][parameter_name] = value
+          end
+        end
+        build_labeled_drilldowns(raw_labeled_drilldowns)
+      end
+
+      def build_labeled_drilldowns(raw_labeled_drilldowns)
+        labeled_drilldowns = {}
+        raw_labeled_drilldowns.each do |label, raw_drilldown|
+          keys = parse_array_value(raw_drilldown["keys"])
+          sort_keys = raw_drilldown["sort_keys"] || raw_drilldown["sortby"]
+          sort_keys = parse_array_value(sort_keys)
+          output_columns = parse_array_value(raw_drilldown["output_columns"])
+          offset = parse_integer_value(raw_drilldown["offset"])
+          limit = parse_integer_value(raw_drilldown["limit"])
+          calc_types = parse_array_value(raw_drilldown["calc_types"])
+          calc_target = raw_drilldown["calc_target"]
+          drilldown = Drilldown.new(keys,
+                                    sort_keys,
+                                    output_columns,
+                                    offset,
+                                    limit,
+                                    calc_types,
+                                    calc_target)
+          labeled_drilldowns[label] = drilldown
+        end
+        labeled_drilldowns
+      end
+
+      class Drilldown < Struct.new(:keys,
+                                   :sort_keys,
+                                   :output_columns,
+                                   :offset,
+                                   :limit,
+                                   :calc_types,
+                                   :calc_target)
+      end
     end
   end
 end

  Modified: test/command/test-select.rb (+50 -3)
===================================================================
--- test/command/test-select.rb    2016-08-15 16:07:33 +0900 (d4e7193)
+++ test/command/test-select.rb    2016-10-11 16:38:52 +0900 (9598009)
@@ -1,6 +1,4 @@
-# -*- coding: utf-8 -*-
-#
-# Copyright (C) 2011-2013  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2011-2016  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -126,4 +124,53 @@ class SelectCommandTest < Test::Unit::TestCase
                    command.conditions)
     end
   end
+
+  class LabeledDrilldownsTest < self
+    def test_multiple
+      parameters = {
+        "drilldowns[tag].keys" => "tag",
+        "drilldowns[tag].sort_keys" => "-_nsubrecs,_key",
+        "drilldowns[tag].output_columns" => "_key,_nsubrecs,_min,_max",
+        "drilldowns[tag].offset" => "1",
+        "drilldowns[tag].limit" => "10",
+        "drilldowns[tag].calc_types" => "MIN,MAX",
+        "drilldowns[tag].calc_target" => "_nsubrecs",
+
+        "drilldowns[author_tag].keys" => "author,tag",
+        "drilldowns[author_tag].sort_keys" => "_value.author",
+        "drilldowns[author_tag].output_columns" => "_value.author,_nsubrecs",
+      }
+      command = select_command(parameters)
+      drilldowns = {
+        "author_tag" => drilldown(:keys => ["author", "tag"],
+                                  :sort_keys => ["_value.author"],
+                                  :output_columns => [
+                                    "_value.author",
+                                    "_nsubrecs",
+                                  ]),
+        "tag" => drilldown(:keys => ["tag"],
+                           :sort_keys => ["-_nsubrecs", "_key"],
+                           :output_columns => [
+                             "_key",
+                             "_nsubrecs",
+                             "_min",
+                             "_max",
+                           ],
+                           :offset => 1,
+                           :limit => 10,
+                           :calc_types => ["MIN", "MAX"],
+                           :calc_target => "_nsubrecs"),
+      }
+      assert_equal(drilldowns,
+                   command.labeled_drilldowns)
+    end
+
+    def drilldown(parameters)
+      drilldown = Groonga::Command::Select::Drilldown.new
+      parameters.each do |key, value|
+        drilldown[key] = value
+      end
+      drilldown
+    end
+  end
 end
-------------- next part --------------
HTML����������������������������...
Download 



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