Skip to content

Commit

Permalink
Use Address internal
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jul 19, 2014
1 parent 0651ea5 commit 3b0e31c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 63 deletions.
41 changes: 5 additions & 36 deletions lib/droonga/catalog/single_volume.rb
Expand Up @@ -13,55 +13,24 @@
# 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/address"

module Droonga
module Catalog
class SingleVolume
attr_reader :address
def initialize(data)
@data = data
parse_address
end

def address
@data["address"]
end

def host
@host
end

def port
@port
end

def tag
@tag
end

def name
@name
@address = Address.parse(@data["address"])
end

def node
"#{host}:#{port}/#{tag}"
@address.node
end

def all_nodes
@all_nodes ||= [node]
end

private
def parse_address
if /\A(.+):(\d+)\/([^.]+)\.(.+)\z/ =~ address
@host = $1
@port = $2.to_i
@tag = $3
@name = $4
else
format = "${host_name}:${port_number}/${tag}.${name}"
message = "volume address must be <#{format}> format: <#{address}>"
raise ArgumentError, message
end
end
end
end
end
10 changes: 5 additions & 5 deletions lib/droonga/catalog/version2.rb
Expand Up @@ -13,6 +13,7 @@
# 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/address"
require "droonga/catalog/base"
require "droonga/catalog/dataset"
require "droonga/catalog/version2_validator"
Expand All @@ -30,26 +31,25 @@ def datasets
@datasets
end

def slices(name)
def slices(node)
device = "."
pattern = Regexp.new("^#{name}\.")
results = {}
@datasets.each do |dataset_name, dataset|
n_workers = dataset.n_workers
plugins = dataset.plugins
dataset.replicas.each do |volume|
volume.slices.each do |slice|
volume_address = slice.volume.address
if pattern =~ volume_address
path = File.join([device, $POSTMATCH, "db"])
if volume_address.node == node
path = File.join([device, volume_address.name, "db"])
path = File.expand_path(path, base_path)
options = {
:dataset => dataset_name,
:database => path,
:n_workers => n_workers,
:plugins => plugins
}
results[volume_address] = options
results[volume_address.to_s] = options
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/droonga/catalog_generator.rb
Expand Up @@ -183,12 +183,12 @@ def load(catalog_replicas)
collection_volume = dataset.replicas.first
slices = collection_volume.slices
@hosts = slices.collect do |slice|
slice.volume.host
slice.volume.address.host
end
@n_slices = slices.size
single_volume = slices.first.volume
@port = single_volume.port
@tag = single_volume.tag
single_volume_address = slices.first.volume.address
@port = single_volume_address.port
@tag = single_volume_address.tag
end

def to_catalog
Expand Down
25 changes: 8 additions & 17 deletions test/unit/catalog/test_single_volume.rb
Expand Up @@ -24,25 +24,16 @@ def setup
@volume = Droonga::Catalog::SingleVolume.new(data)
end

def test_address
assert_equal("127.0.0.1:10047/tag.000",
@volume.address)
end

def test_host
assert_equal("127.0.0.1", @volume.host)
end

def test_port
assert_equal(10047, @volume.port)
end

def test_tag
assert_equal("tag", @volume.tag)
def address(host, port, tag, name)
Droonga::Address.new(:host => host,
:port => port,
:tag => tag,
:name => name)
end

def test_name
assert_equal("000", @volume.name)
def test_address
assert_equal(address("127.0.0.1", 10047, "tag", "000"),
@volume.address)
end

def test_node
Expand Down
2 changes: 1 addition & 1 deletion test/unit/catalog/test_slice.rb
Expand Up @@ -86,7 +86,7 @@ def test_single
}
slice = create_slice(data)
assert_equal("127.0.0.1:10047/volume.000",
slice.volume.address)
slice.volume.address.to_s)
end

def test_all_nodes
Expand Down

0 comments on commit 3b0e31c

Please sign in to comment.