Kouhei Sutou
null+****@clear*****
Tue Oct 11 17:51:02 JST 2016
Kouhei Sutou 2016-10-11 17:51:02 +0900 (Tue, 11 Oct 2016) New Revision: 974730980361185af930330a6af221480005837e https://github.com/ranguba/groonga-client/commit/974730980361185af930330a6af221480005837e Message: Support labeled drilldowns There is a backward incompatibility for select and command version 3. Groonga::Client::Response::Select#drilldowns returns { "label1" => drilldown1, "label2" => drilldown2, } for labeled drilldowns and command version 3 case. Before 0.3.1, [ drilldown1, drilldown2, ] was returned for command version 3 case. It's OK because command version 3 is experimental feature. Modified files: groonga-client.gemspec lib/groonga/client/response/select.rb test/response/test-select-command-version1.rb test/response/test-select-command-version3.rb Modified: groonga-client.gemspec (+1 -1) =================================================================== --- groonga-client.gemspec 2016-10-05 19:14:46 +0900 (ad7856f) +++ groonga-client.gemspec 2016-10-11 17:51:02 +0900 (9612b01) @@ -45,7 +45,7 @@ Gem::Specification.new do |spec| spec.test_files += Dir.glob("test/**/*") spec.add_runtime_dependency("gqtp", ">= 1.0.4") - spec.add_runtime_dependency("groonga-command", ">= 1.2.0") + spec.add_runtime_dependency("groonga-command", ">= 1.2.8") spec.add_runtime_dependency("hashie") spec.add_development_dependency("bundler") Modified: lib/groonga/client/response/select.rb (+27 -7) =================================================================== --- lib/groonga/client/response/select.rb 2016-10-05 19:14:46 +0900 (e0c8b80) +++ lib/groonga/client/response/select.rb 2016-10-11 17:51:02 +0900 (b7c0af8) @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2015 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2013-2016 Kouhei Sutou <kou �� clear-code.com> # Copyright (C) 2013 Kosuke Asami # # This library is free software; you can redistribute it and/or @@ -27,6 +27,14 @@ module Groonga # a search condition. attr_accessor :n_hits attr_accessor :records + + # @return [::Array<Groonga::Client::Response::Select::Drilldown>, + # ::Hash<String, Groonga::Client::Response::Select::Drilldown>] + # If labeled drilldowns are used or command version 3 or + # later is used, `{"label1" => drilldown1, "label2" => drilldown2}` + # is returned since 0.3.1. + # + # Otherwise, `[drilldown1, drilldown2]` is returned. attr_accessor :drilldowns def body=(body) @@ -105,18 +113,30 @@ module Groonga end def parse_drilldowns_v1(raw_drilldowns) - (raw_drilldowns || []).collect.with_index do |raw_drilldown, i| - key =****@comma*****[i] - n_hits, records = parse_match_records_v1(raw_drilldown) - Drilldown.new(key, n_hits, records) + request_drilldowns =****@comma***** + if request_drilldowns.empty? and !@command.labeled_drilldowns.empty? + drilldowns = {} + (raw_drilldowns[0] || {}).each do |label, raw_drilldown| + n_hits, records = parse_match_records_v1(raw_drilldown) + drilldowns[label] = Drilldown.new(label, n_hits, records) + end + drilldowns + else + (raw_drilldowns || []).collect.with_index do |raw_drilldown, i| + key = request_drilldowns[i] + n_hits, records = parse_match_records_v1(raw_drilldown) + Drilldown.new(key, n_hits, records) + end end end def parse_drilldowns_v3(raw_drilldowns) - (raw_drilldowns || {}).collect do |(key, raw_drilldown)| + drilldowns = {} + (raw_drilldowns || {}).each do |key, raw_drilldown| n_hits, records = parse_match_records_v3(raw_drilldown) - Drilldown.new(key, n_hits, records) + drilldowns[key] = Drilldown.new(key, n_hits, records) end + drilldowns end class Drilldown < Struct.new(:key, :n_hits, :records) Modified: test/response/test-select-command-version1.rb (+132 -0) =================================================================== --- test/response/test-select-command-version1.rb 2016-10-05 19:14:46 +0900 (a559223) +++ test/response/test-select-command-version1.rb 2016-10-11 17:51:02 +0900 (5a76457) @@ -183,5 +183,137 @@ class TestResponseSelectCommandVersion1 < Test::Unit::TestCase create_response(body).drilldowns end end + + class TestLabeledDrilldowns < self + def setup + pair_arguments = { + "drilldowns[tag].keys" => "tag", + "drilldowns[tag].output_columns" => "_key,_nsubrecs", + "drilldowns[author].keys" => "author", + "drilldowns[author].output_columns" => "_key,_nsubrecs", + } + @command = Groonga::Command::Select.new("select", pair_arguments) + end + + def test_key + body = [ + [[0], []], + { + "tag" => [ + [29], + [ + ["_key", "ShortText"], + ["_nsubrecs", "Int32"], + ], + ["Groonga", 2], + ["Ruby", 9], + ["Rroonga", 1], + ], + "author" => [ + [4], + [ + ["_key", "ShortText"], + ["_nsubrecs", "Int32"], + ], + ["Alice", 2], + ["Bob", 1], + ["Chris", 4], + ], + }, + ] + assert_equal({ + "tag" => "tag", + "author" => "author", + }, + collect_values(body, &:key)) + end + + def test_n_hits + body = [ + [[0], []], + { + "tag" => [ + [29], + [ + ["_key", "ShortText"], + ["_nsubrecs", "Int32"], + ], + ["Groonga", 2], + ["Ruby", 9], + ["Rroonga", 1], + ], + "author" => [ + [4], + [ + ["_key", "ShortText"], + ["_nsubrecs", "Int32"], + ], + ["Alice", 2], + ["Bob", 1], + ["Chris", 4], + ], + }, + ] + assert_equal({ + "tag" => 29, + "author" => 4, + }, + collect_values(body, &:n_hits)) + end + + def test_items + body = [ + [[0], []], + { + "tag" => [ + [29], + [ + ["_key", "ShortText"], + ["_nsubrecs", "Int32"], + ], + ["Groonga", 2], + ["Ruby", 9], + ["Rroonga", 1], + ], + "author" => [ + [4], + [ + ["_key", "ShortText"], + ["_nsubrecs", "Int32"], + ], + ["Alice", 2], + ["Bob", 1], + ["Chris", 4], + ], + }, + ] + assert_equal({ + "tag" => [ + {"_key" => "Groonga", "_nsubrecs" => 2}, + {"_key" => "Ruby", "_nsubrecs" => 9}, + {"_key" => "Rroonga", "_nsubrecs" => 1}, + ], + "author" => [ + {"_key" => "Alice", "_nsubrecs" => 2}, + {"_key" => "Bob", "_nsubrecs" => 1}, + {"_key" => "Chris", "_nsubrecs" => 4}, + ], + }, + collect_values(body, &:items)) + end + + private + def drilldowns(body) + create_response(body).drilldowns + end + + def collect_values(body) + values = {} + drilldowns(body).each do |label, drilldown| + values[label] = yield(drilldown) + end + values + end + end end end Modified: test/response/test-select-command-version3.rb (+16 -8) =================================================================== --- test/response/test-select-command-version3.rb 2016-10-05 19:14:46 +0900 (79bb24a) +++ test/response/test-select-command-version3.rb 2016-10-11 17:51:02 +0900 (911b709) @@ -195,8 +195,8 @@ class TestResponseSelectCommandVersion3 < Test::Unit::TestCase }, }, } - assert_equal(["tag"], - drilldowns(body).collect(&:key)) + assert_equal({"tag" => "tag"}, + collect_values(body, &:key)) end def test_n_hits @@ -225,8 +225,8 @@ class TestResponseSelectCommandVersion3 < Test::Unit::TestCase }, }, } - assert_equal([29], - drilldowns(body).collect(&:n_hits)) + assert_equal({"tag" => 29}, + collect_values(body, &:n_hits)) end def test_items @@ -255,20 +255,28 @@ class TestResponseSelectCommandVersion3 < Test::Unit::TestCase }, }, } - assert_equal([ - [ + assert_equal({ + "tag" => [ {"_key" => "groonga", "_nsubrecs" => 29}, {"_key" => "Ruby", "_nsubrecs" => 19}, {"_key" => "rroonga", "_nsubrecs" => 9}, ], - ], - drilldowns(body).collect(&:items)) + }, + collect_values(body, &:items)) end private def drilldowns(body) create_response(body).drilldowns end + + def collect_values(body) + values = {} + drilldowns(body).each do |label, drilldown| + values[label] = yield(drilldown) + end + values + end end end end -------------- next part -------------- HTML����������������������������...Download