Kouhei Sutou
null+****@clear*****
Fri Mar 21 15:11:38 JST 2014
Kouhei Sutou 2014-03-21 15:11:38 +0900 (Fri, 21 Mar 2014) New Revision: e30da7c253ba18f0a34b981d4f0f4d5cb65524d9 https://github.com/droonga/fluent-plugin-droonga/commit/e30da7c253ba18f0a34b981d4f0f4d5cb65524d9 Message: Move selecting replicas feature to ReplicaCollection from Catalog::Version2 Added files: test/unit/catalog/test_replica_collection.rb Modified files: lib/droonga/catalog/replica_collection.rb lib/droonga/catalog/version2.rb Modified: lib/droonga/catalog/replica_collection.rb (+13 -0) =================================================================== --- lib/droonga/catalog/replica_collection.rb 2014-03-21 14:59:37 +0900 (f50bbbc) +++ lib/droonga/catalog/replica_collection.rb 2014-03-21 15:11:38 +0900 (b7eafca) @@ -38,6 +38,19 @@ module Droonga def hash to_a.hash end + + def select(how=nil) + case how + when :top + [@replicas.first] + when :random + [@replicas.sample] + when :all + @replicas + else + super + end + end end end end Modified: lib/droonga/catalog/version2.rb (+2 -13) =================================================================== --- lib/droonga/catalog/version2.rb 2014-03-21 14:59:37 +0900 (e10a2de) +++ lib/droonga/catalog/version2.rb 2014-03-21 15:11:38 +0900 (b7d7630) @@ -62,7 +62,7 @@ module Droonga dataset = dataset(name) case args["type"] when "broadcast" - replicas = select_replicas(dataset.replicas, args["replica"]) + replicas = dataset.replicas.select(args["replica"].to_sym) replicas.each do |replica| slices = select_slices(replica) slices.each do |slice| @@ -70,7 +70,7 @@ module Droonga end end when "scatter" - replicas = select_replicas(dataset.replicas, args["replica"]) + replicas = dataset.replicas.select(args["replica"].to_sym) replicas.each do |replica| dimension = replica["dimension"] || "_key" key = args["key"] || args["record"][dimension] @@ -123,17 +123,6 @@ module Droonga end end - def select_replicas(replicas, how) - case how - when "top" - [replicas.first] - when "random" - [replicas.sample] - when "all" - replicas - end - end - def select_slices(replica, range=0..-1) sorted_slices = replica["slices"].sort_by do |slice| slice["label"] Added: test/unit/catalog/test_replica_collection.rb (+50 -0) 100644 =================================================================== --- /dev/null +++ test/unit/catalog/test_replica_collection.rb 2014-03-21 15:11:38 +0900 (e70761e) @@ -0,0 +1,50 @@ +# Copyright (C) 2014 Droonga Project +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1 as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +require "droonga/catalog/replica_collection" + +class CatalogReplicaCollectionTest < Test::Unit::TestCase + private + def create_replica_collection(replicas) + Droonga::Catalog::ReplicaCollection.new(replicas) + end + + class SelectTest < self + def setup + replicas = [ + "replica1", + "replica2", + "replica3", + ] + @collection = create_replica_collection(replicas) + end + + def test_top + assert_equal(["replica1"], @collection.select(:top)) + end + + def test_random + random_replicas =****@colle*****(:random).collect do |replica| + replica.gsub(/\Areplica[123]\z/, "any replica") + end + assert_equal(["any replica"], random_replicas) + end + + def test_all + assert_equal(["replica1", "replica2", "replica3"], + @collection.select(:all)) + end + end +end -------------- next part -------------- HTML����������������������������...Download