Kouhei Sutou
null+****@clear*****
Thu Dec 26 19:03:43 JST 2013
Kouhei Sutou 2013-12-26 19:03:43 +0900 (Thu, 26 Dec 2013) New Revision: 2926944e7976ff5b526538fb95f4927652d126d6 https://github.com/droonga/fluent-plugin-droonga/commit/2926944e7976ff5b526538fb95f4927652d126d6 Message: Create Catalog::Version1 Added files: lib/droonga/catalog/version1.rb Copied files: lib/droonga/catalog/base.rb (from lib/droonga/catalog.rb) Modified files: lib/droonga/catalog.rb Modified: lib/droonga/catalog.rb (+9 -132) =================================================================== --- lib/droonga/catalog.rb 2013-12-26 18:46:04 +0900 (f59a6fa) +++ lib/droonga/catalog.rb 2013-12-26 19:03:43 +0900 (b007ee8) @@ -15,147 +15,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 "digest/sha1" -require "zlib" -require "droonga/message_processing_error" +require "droonga/catalog/version1" module Droonga class << self def catalog - @catalog ||= Catalog.new + @catalog ||= Catalog.load end end - class Catalog - class UnknownDataset < NotFound - def initialize(dataset) - super("The dataset #{dataset.inspect} does not exist.") - end - end - - CATALOG_FILE_PATH = "catalog.json" - - attr_reader :path - - def initialize(path=nil) - @path = path || default_path - - open(@path) do |file| - @catalog = JSON.parse(file.read) - end - @catalog["datasets"].each do |name, dataset| - number_of_partitions = dataset["number_of_partitions"] - next if number_of_partitions < 2 - total_weight = dataset["ring"].reduce do |a, b| - a[1]["weight"] + b[1]["weight"] - end - 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 = @catalog["options"] || {} - end - - def base_path - @base_path ||= File.dirname(@path) - end - - def option(name) - @options[name] - end + module Catalog + PATH = "catalog.json" - def get_partitions(name) - device = @catalog["farms"][name]["device"] - pattern = Regexp.new("^#{name}\.") - results = {} - @catalog["datasets"].each do |key, dataset| - workers = dataset["workers"] - plugins = dataset["plugins"] - dataset["ring"].each do |key, part| - part["partitions"].each do |range, partitions| - partitions.each do |partition| - if partition =~ pattern - path = File.join([device, $POSTMATCH, "db"]) - path = File.expand_path(path, base_path) - options = { - :database => path, - :n_workers => workers, - :handlers => plugins - } - results[partition] = options - end - end - end - end + class << self + def load(path=nil) + path = ENV["DROONGA_CATALOG"] || PATH + path = File.expand_path(path) + Version1.new(path) end - return results - end - - def get_routes(name, args) - routes = [] - dataset = dataset(name) - case args["type"] - when "broadcast" - dataset["ring"].each do |key, partition| - select_range_and_replicas(partition, args, routes) - end - when "scatter" - name = get_partition(dataset, args["key"]) - partition = dataset["ring"][name] - select_range_and_replicas(partition, args, routes) - end - return routes - end - - def get_partition(dataset, key) - continuum = dataset["continuum"] - return dataset["ring"].keys[0] unless continuum - hash = Zlib.crc32(key) - min = 0 - max = continuum.size - 1 - while (min < max) do - index = (min + max) / 2 - value, key = continuum[index] - return key if value == hash - if value > hash - max = index - else - min = index + 1 - end - end - return continuum[max][1] - end - - def dataset(name) - dataset = @catalog["datasets"][name] - raise UnknownDataset.new(name) unless dataset - dataset - end - - def select_range_and_replicas(partition, args, routes) - date_range = args["date_range"] || 0..-1 - partition["partitions"].sort[date_range].each do |time, replicas| - case args["replica"] - when "top" - routes << replicas[0] - when "random" - routes << replicas[rand(replicas.size)] - when "all" - routes.concat(replicas) - end - end - end - - private - def default_path - path = ENV["DROONGA_CATALOG"] || Catalog::CATALOG_FILE_PATH - File.expand_path(path) end end end Copied: lib/droonga/catalog/base.rb (+2 -16) 93% =================================================================== --- lib/droonga/catalog.rb 2013-12-26 18:46:04 +0900 (f59a6fa) +++ lib/droonga/catalog/base.rb 2013-12-26 19:03:43 +0900 (4f81cd4) @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -# # Copyright (C) 2013 Droonga Project # # This library is free software; you can redistribute it and/or @@ -20,21 +18,14 @@ require "zlib" require "droonga/message_processing_error" module Droonga - class << self - def catalog - @catalog ||= Catalog.new - end - end - - class Catalog + module Catalog + class Base class UnknownDataset < NotFound def initialize(dataset) super("The dataset #{dataset.inspect} does not exist.") end end - CATALOG_FILE_PATH = "catalog.json" - attr_reader :path def initialize(path=nil) @@ -151,11 +142,6 @@ module Droonga end end end - - private - def default_path - path = ENV["DROONGA_CATALOG"] || Catalog::CATALOG_FILE_PATH - File.expand_path(path) end end end Added: lib/droonga/catalog/version1.rb (+23 -0) 100644 =================================================================== --- /dev/null +++ lib/droonga/catalog/version1.rb 2013-12-26 19:03:43 +0900 (a07e32e) @@ -0,0 +1,23 @@ +# Copyright (C) 2013 Droonga Project +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1 as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# 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/catalog/base" + +module Droonga + module Catalog + class Version1 < Base + end + end +end -------------- next part -------------- HTML����������������������������...Download