Navigation Menu

Skip to content

Commit

Permalink
Extract preparing data code as a method to be overriden
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Feb 26, 2014
1 parent 32d0a89 commit be09d8c
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/droonga/catalog/base.rb
Expand Up @@ -32,21 +32,7 @@ def initialize(data, path)
validate
raise MultiplexError.new(@errors) unless @errors.empty?

@data["datasets"].each do |name, dataset|
number_of_partitions = dataset["number_of_partitions"]
next if number_of_partitions < 2
total_weight = compute_total_weight(dataset)
continuum = []
dataset["ring"].each do |key, value|
points = number_of_partitions * 160 * value["weight"] / total_weight
points.times do |point|
hash = Digest::SHA1.hexdigest("#{key}:#{point}")
continuum << [hash[0..7].to_i(16), key]
end
end
dataset["continuum"] = continuum.sort do |a, b| a[0] - b[0]; end
end
@options = @data["options"] || {}
prepare_data
end

def option(name)
Expand Down Expand Up @@ -143,6 +129,24 @@ def select_range_and_replicas(partition, args, routes)
end

private
def prepare_data
@data["datasets"].each do |name, dataset|
number_of_partitions = dataset["number_of_partitions"]
next if number_of_partitions < 2
total_weight = compute_total_weight(dataset)
continuum = []
dataset["ring"].each do |key, value|
points = number_of_partitions * 160 * value["weight"] / total_weight
points.times do |point|
hash = Digest::SHA1.hexdigest("#{key}:#{point}")
continuum << [hash[0..7].to_i(16), key]
end
end
dataset["continuum"] = continuum.sort do |a, b| a[0] - b[0]; end
end
@options = @data["options"] || {}
end

def compute_total_weight(dataset)
dataset["ring"].reduce(0) do |result, zone|
result + zone[1]["weight"]
Expand Down

0 comments on commit be09d8c

Please sign in to comment.