YUKI Hiroshi
null+****@clear*****
Thu Apr 24 18:44:13 JST 2014
YUKI Hiroshi 2014-04-24 18:44:13 +0900 (Thu, 24 Apr 2014) New Revision: 2084d0dfd76fe1a1f2779fd0358f8b24e23744c9 https://github.com/droonga/droonga-engine/commit/2084d0dfd76fe1a1f2779fd0358f8b24e23744c9 Message: Add ability to generate catalog.json with a basic pattern Modified files: bin/droonga-catalog-generate lib/droonga/catalog_generator.rb Modified: bin/droonga-catalog-generate (+20 -0) =================================================================== --- bin/droonga-catalog-generate 2014-04-24 18:32:03 +0900 (c2dfe5b) +++ bin/droonga-catalog-generate 2014-04-24 18:44:13 +0900 (47f3858) @@ -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 Modified: lib/droonga/catalog_generator.rb (+56 -1) =================================================================== --- lib/droonga/catalog_generator.rb 2014-04-24 18:32:03 +0900 (85ff88d) +++ lib/droonga/catalog_generator.rb 2014-04-24 18:44:13 +0900 (421a572) @@ -67,7 +67,8 @@ module Droonga end def replicas - @options[:replicas] || [] + return @options[:replicas] if @options[:replicas] + @generated_replicas ||= Replicas.new(@options).to_json end def to_catalog @@ -80,6 +81,60 @@ module Droonga 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 -------------- next part -------------- HTML����������������������������...Download