Navigation Menu

Skip to content

Commit

Permalink
Add ability to generate catalog.json with a basic pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 24, 2014
1 parent 5fd323a commit 2084d0d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
20 changes: 20 additions & 0 deletions bin/droonga-catalog-generate
Expand Up @@ -45,6 +45,26 @@ parser.on("--n-workers=N", Integer,
"Use N workers for the current dataset.") do |n|
current_dataset[:n_workers] = n
end
parser.on("--host=NAME",
"Use the NAME as the host for the current dataset.") do |host|
current_dataset[:host] = host
end
parser.on("--port=PORT",, Integer
"Use the PORT as the port for the current dataset.") do |port|
current_dataset[:port] = port
end
parser.on("--tag=TAG",
"Use the TAG as the tag for the current dataset.") do |tag|
current_dataset[:tag] = tag
end
parser.on("--n-replicas=N", Integer,
"Use N replicas for the current dataset.") do |n|
current_dataset[:n_replicas] = n
end
parser.on("--n-slices=N", Integer,
"Use N slices for a replica.") do |n|
current_dataset[:n_replicas] = n
end
parser.on("--plugins=PLUGIN1,PLUGIN2,...", Array,
"Use PLUGINS for the current dataset.") do |plugins|
current_dataset[:plugins] = plugins
Expand Down
57 changes: 56 additions & 1 deletion lib/droonga/catalog_generator.rb
Expand Up @@ -67,7 +67,8 @@ def fact
end

def replicas
@options[:replicas] || []
return @options[:replicas] if @options[:replicas]
@generated_replicas ||= Replicas.new(@options).to_json
end

def to_catalog
Expand All @@ -80,6 +81,60 @@ def to_catalog
catalog["fact"] = fact if fact
catalog
end

private
end

class Replicas
def initialize(parameters={})
@host = parameters[:host] || "127.0.0.1"
@port = parameters[:port] || 10031
@tag = parameters[:tag] || "droonga"
@n_replicas = parameters[:n_replicas] || 2
@n_slices = parameters[:n_slices] || 1

@n_volumes = 0
end

def to_json
@json ||= generate_json
end

private
def generate_json
replicas = []
@n_replicas.times do |index|
replicas << generate_replica
end
replicas
end

def generate_replica
slices = []
@n_slices.times do |index|
slices << generate_slice
end
{
"dimension": "_key",
"slicer": "hash",
"slices": slices
}
end

def generate_slice
name = sprintf('%03d', @n_volumes)
@n_volumes += 1
{
"weight": weight,
"volume": {
"address": "#{host}:#{port}/#{tag}.#{name}"
}
}
end

def weight
@weight ||= 100 / @n_slices
end
end
end
end

0 comments on commit 2084d0d

Please sign in to comment.