YUKI Hiroshi
null+****@clear*****
Wed Apr 15 14:43:49 JST 2015
YUKI Hiroshi 2015-04-15 14:43:49 +0900 (Wed, 15 Apr 2015) New Revision: 07335b4b2fd5b5d1a29703285be48b12984d0c6c https://github.com/droonga/droonga-engine/commit/07335b4b2fd5b5d1a29703285be48b12984d0c6c Message: Fix indent Modified files: lib/droonga/catalog/fetcher.rb lib/droonga/catalog/generator.rb lib/droonga/catalog/loader.rb lib/droonga/catalog/modifier.rb Modified: lib/droonga/catalog/fetcher.rb (+32 -32) =================================================================== --- lib/droonga/catalog/fetcher.rb 2015-04-15 14:39:36 +0900 (48cf590) +++ lib/droonga/catalog/fetcher.rb 2015-04-15 14:43:49 +0900 (946d5ad) @@ -22,43 +22,43 @@ require "droonga/catalog/dataset" module Droonga module Catalog - class Fetcher - class EmptyResponse < StandardError - end + class Fetcher + class EmptyResponse < StandardError + end - class EmptyCatalog < StandardError - end + class EmptyCatalog < StandardError + end - def initialize(client_options) - @client_options = default_options.merge(client_options) - end + def initialize(client_options) + @client_options = default_options.merge(client_options) + end - def fetch(options={}) - message = { - "dataset" => options[:dataset] || Catalog::Dataset::DEFAULT_NAME, - "type" => "catalog.fetch" - } - response = nil - Droonga::Client.open(@client_options) do |client| - response = client.request(message) + def fetch(options={}) + message = { + "dataset" => options[:dataset] || Catalog::Dataset::DEFAULT_NAME, + "type" => "catalog.fetch" + } + response = nil + Droonga::Client.open(@client_options) do |client| + response = client.request(message) + end + raise EmptyResponse.new unless response + raise EmptyCatalog.new unless response["body"] + response["body"] end - raise EmptyResponse.new unless response - raise EmptyCatalog.new unless response["body"] - response["body"] - end - private - def default_options - { - :host => "127.0.0.1", - :port => NodeName::DEFAULT_PORT, - :tag => NodeName::DEFAULT_TAG, - :protocol => :droonga, - :timeout => 1, - :receiver_host => Socket.gethostname, - :receiver_port => 0, - } + private + def default_options + { + :host => "127.0.0.1", + :port => NodeName::DEFAULT_PORT, + :tag => NodeName::DEFAULT_TAG, + :protocol => :droonga, + :timeout => 1, + :receiver_host => Socket.gethostname, + :receiver_port => 0, + } + end end end - end end Modified: lib/droonga/catalog/generator.rb (+178 -178) =================================================================== --- lib/droonga/catalog/generator.rb 2015-04-15 14:39:36 +0900 (8d7f104) +++ lib/droonga/catalog/generator.rb 2015-04-15 14:43:49 +0900 (90303fa) @@ -20,226 +20,226 @@ require "droonga/catalog/dataset" module Droonga module Catalog - class Generator - DEFAULT_DATASET = Catalog::Dataset::DEFAULT_NAME - DEFAULT_HOSTS = [NodeName::DEFAULT_HOST] - DEFAULT_N_WORKERS = 4 - DEFAULT_N_SLICES = 1 - DEFAULT_PLUGINS = ["groonga", "search", "crud", "dump", "system", "catalog"] - DEFAULT_PORT = NodeName::DEFAULT_PORT - DEFAULT_TAG = NodeName::DEFAULT_TAG - - attr_reader :datasets - - class << self - def generate(datasets_params) - generator = new - datasets_params.each do |name, params| - generator.add_dataset(name, params) - end - generator.generate + class Generator + DEFAULT_DATASET = Catalog::Dataset::DEFAULT_NAME + DEFAULT_HOSTS = [NodeName::DEFAULT_HOST] + DEFAULT_N_WORKERS = 4 + DEFAULT_N_SLICES = 1 + DEFAULT_PLUGINS = ["groonga", "search", "crud", "dump", "system", "catalog"] + DEFAULT_PORT = NodeName::DEFAULT_PORT + DEFAULT_TAG = NodeName::DEFAULT_TAG + + attr_reader :datasets + + class << self + def generate(datasets_params) + generator = new + datasets_params.each do |name, params| + generator.add_dataset(name, params) + end + generator.generate + end end - end - def initialize - @version = 2 - @effective_date = Time.now - @datasets = {} - end + def initialize + @version = 2 + @effective_date = Time.now + @datasets = {} + end - def add_dataset(name, options) - @datasets[name] = Dataset.new(name, options) - end + def add_dataset(name, options) + @datasets[name] = Dataset.new(name, options) + end - def generate - { - "version" => @version, - "effectiveDate" => @effective_date.iso8601, - "datasets" => catalog_datasets, - } - end + def generate + { + "version" => @version, + "effectiveDate" => @effective_date.iso8601, + "datasets" => catalog_datasets, + } + end - def load(catalog) - catalog["datasets"].each do |name, catalog_dataset| - load_dataset(name, catalog_dataset) + def load(catalog) + catalog["datasets"].each do |name, catalog_dataset| + load_dataset(name, catalog_dataset) + end + self end - self - end - def dataset_for_host(host) - @datasets.each do |name, dataset| - if dataset.replicas.hosts.include?(host) - return dataset + def dataset_for_host(host) + @datasets.each do |name, dataset| + if dataset.replicas.hosts.include?(host) + return dataset + end end + nil end - nil - end - def modify(dataset_modifications) - dataset_modifications.each do |name, modification| - dataset = @datasets[name] - next unless dataset + def modify(dataset_modifications) + dataset_modifications.each do |name, modification| + dataset = @datasets[name] + next unless dataset - replicas = dataset.replicas + replicas = dataset.replicas - if modification[:replica_hosts] - replicas.hosts = modification[:replica_hosts] - end + if modification[:replica_hosts] + replicas.hosts = modification[:replica_hosts] + end - if modification[:add_replica_hosts] - replicas.hosts += modification[:add_replica_hosts] - replicas.hosts.uniq! - end + if modification[:add_replica_hosts] + replicas.hosts += modification[:add_replica_hosts] + replicas.hosts.uniq! + end - if modification[:remove_replica_hosts] - replicas.hosts -= modification[:remove_replica_hosts] + if modification[:remove_replica_hosts] + replicas.hosts -= modification[:remove_replica_hosts] + end end end - end - private - def catalog_datasets - catalog_datasets = {} - @datasets.each do |name, dataset| - catalog_datasets[name] = dataset.to_catalog + private + def catalog_datasets + catalog_datasets = {} + @datasets.each do |name, dataset| + catalog_datasets[name] = dataset.to_catalog + end + catalog_datasets end - catalog_datasets - end - def load_dataset(name, catalog_dataset) - options = {} - options[:n_workers] = catalog_dataset["nWorkers"] - options[:plugins] = catalog_dataset["plugins"] - options[:schema] = catalog_dataset["schema"] - options[:fact] = catalog_dataset["fact"] - options[:replicas] = catalog_dataset["replicas"] - add_dataset(name, options) - end + def load_dataset(name, catalog_dataset) + options = {} + options[:n_workers] = catalog_dataset["nWorkers"] + options[:plugins] = catalog_dataset["plugins"] + options[:schema] = catalog_dataset["schema"] + options[:fact] = catalog_dataset["fact"] + options[:replicas] = catalog_dataset["replicas"] + add_dataset(name, options) + end - class Dataset - attr_reader :name + class Dataset + attr_reader :name - def initialize(name, options) - @name = name - @options = options - end + def initialize(name, options) + @name = name + @options = options + end - def n_workers - @options[:n_workers] || DEFAULT_N_WORKERS - end + def n_workers + @options[:n_workers] || DEFAULT_N_WORKERS + end - def plugins - @options[:plugins] || DEFAULT_PLUGINS - end + def plugins + @options[:plugins] || DEFAULT_PLUGINS + end - def schema - @options[:schema] || {} - end + def schema + @options[:schema] || {} + end - def fact - @options[:fact] - end + def fact + @options[:fact] + end - def replicas - @replicas ||= create_replicas - end + def replicas + @replicas ||= create_replicas + end - def to_catalog - catalog = { - "nWorkers" => n_workers, - "plugins" => plugins, - "schema" => schema, - "replicas" => replicas.to_catalog, - } - catalog["fact"] = fact if fact - catalog - end + def to_catalog + catalog = { + "nWorkers" => n_workers, + "plugins" => plugins, + "schema" => schema, + "replicas" => replicas.to_catalog, + } + catalog["fact"] = fact if fact + catalog + end - private - def create_replicas - catalog_replicas = @options[:replicas] - if catalog_replicas - replicas = Replicas.new - replicas.load(catalog_replicas) - replicas - else - Replicas.new(@options) + private + def create_replicas + catalog_replicas = @options[:replicas] + if catalog_replicas + replicas = Replicas.new + replicas.load(catalog_replicas) + replicas + else + Replicas.new(@options) + end end end - end - class Replicas - attr_accessor :hosts - attr_reader :port, :tag, :n_slices + class Replicas + attr_accessor :hosts + attr_reader :port, :tag, :n_slices - def initialize(options={}) - @hosts = options[:hosts] || DEFAULT_HOSTS - @port = options[:port] - @tag = options[:tag] - @n_slices = options[:n_slices] - end + def initialize(options={}) + @hosts = options[:hosts] || DEFAULT_HOSTS + @port = options[:port] + @tag = options[:tag] + @n_slices = options[:n_slices] + end - def load(catalog_replicas) - dataset = Catalog::Dataset.new("temporary", - "replicas" => catalog_replicas) - @hosts = dataset.replicas.collect do |replica| - replica.slices.first.volume.address.host - end - collection_volume = dataset.replicas.first - slices = collection_volume.slices - @n_slices = slices.size - single_volume_address = slices.first.volume.address - @port = single_volume_address.port - @tag = single_volume_address.tag - end + def load(catalog_replicas) + dataset = Catalog::Dataset.new("temporary", + "replicas" => catalog_replicas) + @hosts = dataset.replicas.collect do |replica| + replica.slices.first.volume.address.host + end + collection_volume = dataset.replicas.first + slices = collection_volume.slices + @n_slices = slices.size + single_volume_address = slices.first.volume.address + @port = single_volume_address.port + @tag = single_volume_address.tag + end - def to_catalog - catalog_replicas = [] - @hosts.each do |host| - replica = Replica.new(host, :port => @port, - :tag => @tag, - :n_slices => @n_slices) - catalog_replicas << replica.to_catalog + def to_catalog + catalog_replicas = [] + @hosts.each do |host| + replica = Replica.new(host, :port => @port, + :tag => @tag, + :n_slices => @n_slices) + catalog_replicas << replica.to_catalog + end + catalog_replicas end - catalog_replicas end - end - class Replica - def initialize(host, options={}) - @host = host - @port = options[:port] || DEFAULT_PORT - @tag = options[:tag] || DEFAULT_TAG - @n_slices = options[:n_slices] || DEFAULT_N_SLICES - end + class Replica + def initialize(host, options={}) + @host = host + @port = options[:port] || DEFAULT_PORT + @tag = options[:tag] || DEFAULT_TAG + @n_slices = options[:n_slices] || DEFAULT_N_SLICES + end - def to_catalog - slices = [] - @n_slices.times do |i| - slices << catalog_slice(i) + def to_catalog + slices = [] + @n_slices.times do |i| + slices << catalog_slice(i) + end + { + "dimension" => "_key", + "slicer" => "hash", + "slices" => slices, + } end - { - "dimension" => "_key", - "slicer" => "hash", - "slices" => slices, - } - end - private - def catalog_slice(nth_slice) - local_name = "%03d" % nth_slice - { - "weight" => weight, - "volume" => { - "address" => "#{@host}:#{@port}/#{@tag}.#{local_name}", - }, - } - end + private + def catalog_slice(nth_slice) + local_name = "%03d" % nth_slice + { + "weight" => weight, + "volume" => { + "address" => "#{@host}:#{@port}/#{@tag}.#{local_name}", + }, + } + end - def weight - @weight ||= 100 / @n_slices + def weight + @weight ||= 100 / @n_slices + end end end end - end end Modified: lib/droonga/catalog/loader.rb (+34 -34) =================================================================== --- lib/droonga/catalog/loader.rb 2015-04-15 14:39:36 +0900 (1f573e6) +++ lib/droonga/catalog/loader.rb 2015-04-15 14:43:49 +0900 (a7d5de3) @@ -20,47 +20,47 @@ require "droonga/catalog/version2" module Droonga module Catalog - class Loader - def initialize(path=nil) - @path = path - end - - def load - unless @path - raise Error.new("nothing specified") + class Loader + def initialize(path=nil) + @path = path end - data = nil - begin - data = File.open(@path) do |file| - JSON.parse(file.read) + def load + unless @path + raise Error.new("nothing specified") end - rescue Errno::ENOENT => error - raise Error.new("Missing catalog file #{@path}") - rescue JSON::ParserError => error - raise Error.new("Syntax error in #{@path}:\n#{error.to_s}") - end - parse(data) - end - def parse(data) - unless data.is_a?(Hash) - raise Error.new("Root element of catalog must be an object in #{@path}: " + - "#{JSON.generate(data)}") + data = nil + begin + data = File.open(@path) do |file| + JSON.parse(file.read) + end + rescue Errno::ENOENT => error + raise Error.new("Missing catalog file #{@path}") + rescue JSON::ParserError => error + raise Error.new("Syntax error in #{@path}:\n#{error.to_s}") + end + parse(data) end - version = data["version"] - case version - when 1 - Catalog::Version1.new(data, @path) - when 2 - Catalog::Version2.new(data, @path) - when nil - raise Error.new("Catalog version must be specified in #{@path}") - else - raise Error.new("Unsupported catalog version <#{version}> is specified in #{@path}") + def parse(data) + unless data.is_a?(Hash) + raise Error.new("Root element of catalog must be an object in #{@path}: " + + "#{JSON.generate(data)}") + end + + version = data["version"] + case version + when 1 + Catalog::Version1.new(data, @path) + when 2 + Catalog::Version2.new(data, @path) + when nil + raise Error.new("Catalog version must be specified in #{@path}") + else + raise Error.new("Unsupported catalog version <#{version}> is specified in #{@path}") + end end end end - end end Modified: lib/droonga/catalog/modifier.rb (+10 -10) =================================================================== --- lib/droonga/catalog/modifier.rb 2015-04-15 14:39:36 +0900 (6a7152f) +++ lib/droonga/catalog/modifier.rb 2015-04-15 14:43:49 +0900 (bd2a553) @@ -21,18 +21,18 @@ require "droonga/safe_file_writer" module Droonga module Catalog - class Modifier - def initialize(source_catalog) - @generator = Catalog::Generator.new - @generator.load(source_catalog) - end + class Modifier + def initialize(source_catalog) + @generator = Catalog::Generator.new + @generator.load(source_catalog) + end - def modify - SafeFileWriter.write(Path.catalog) do |output, file| - yield(@generator, file) - output.puts(JSON.pretty_generate(@generator.generate)) + def modify + SafeFileWriter.write(Path.catalog) do |output, file| + yield(@generator, file) + output.puts(JSON.pretty_generate(@generator.generate)) + end end end end - end end -------------- next part -------------- HTML����������������������������... 다운로드