[Groonga-commit] droonga/fluent-plugin-droonga at 7d8a118 [master] Add catalog loader

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Dec 26 19:15:38 JST 2013


Kouhei Sutou	2013-12-26 19:15:38 +0900 (Thu, 26 Dec 2013)

  New Revision: 7d8a118afd12a87066e574484c915c235a00bb33
  https://github.com/droonga/fluent-plugin-droonga/commit/7d8a118afd12a87066e574484c915c235a00bb33

  Message:
    Add catalog loader

  Copied files:
    lib/droonga/catalog_loader.rb
      (from lib/droonga/catalog.rb)
  Modified files:
    lib/droonga/catalog.rb
    lib/droonga/catalog/base.rb
  Renamed files:
    test/unit/catalog/test_version1.rb
      (from test/unit/test_catalog.rb)
    test/unit/fixtures/catalog/version1.json
      (from test/unit/fixtures/catalog.json)

  Modified: lib/droonga/catalog.rb (+3 -2)
===================================================================
--- lib/droonga/catalog.rb    2013-12-26 19:04:05 +0900 (b007ee8)
+++ lib/droonga/catalog.rb    2013-12-26 19:15:38 +0900 (84afe63)
@@ -15,7 +15,7 @@
 # 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/version1"
+require "droonga/catalog_loader"
 
 module Droonga
   class << self
@@ -31,7 +31,8 @@ module Droonga
       def load(path=nil)
         path = ENV["DROONGA_CATALOG"] || PATH
         path = File.expand_path(path)
-        Version1.new(path)
+        loader = CatalogLoader.new(path)
+        loader.load
       end
     end
   end

  Modified: lib/droonga/catalog/base.rb (+9 -16)
===================================================================
--- lib/droonga/catalog/base.rb    2013-12-26 19:04:05 +0900 (b98a691)
+++ lib/droonga/catalog/base.rb    2013-12-26 19:15:38 +0900 (f633df2)
@@ -26,15 +26,12 @@ module Droonga
         end
       end
 
-      attr_reader :path
+      attr_reader :base_path
+      def initialize(data, base_path)
+        @data = data
+        @base_path = base_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|
+        @data["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|
@@ -50,11 +47,7 @@ module Droonga
           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)
+        @options = @data["options"] || {}
       end
 
       def option(name)
@@ -62,10 +55,10 @@ module Droonga
       end
 
       def get_partitions(name)
-        device = @catalog["farms"][name]["device"]
+        device = @data["farms"][name]["device"]
         pattern = Regexp.new("^#{name}\.")
         results = {}
-        @catalog["datasets"].each do |key, dataset|
+        @data["datasets"].each do |key, dataset|
           workers = dataset["workers"]
           plugins = dataset["plugins"]
           dataset["ring"].each do |key, part|
@@ -124,7 +117,7 @@ module Droonga
       end
 
       def dataset(name)
-        dataset = @catalog["datasets"][name]
+        dataset = @data["datasets"][name]
         raise UnknownDataset.new(name) unless dataset
         dataset
       end

  Copied: lib/droonga/catalog_loader.rb (+9 -14) 72%
===================================================================
--- lib/droonga/catalog.rb    2013-12-26 19:04:05 +0900 (b007ee8)
+++ lib/droonga/catalog_loader.rb    2013-12-26 19:15:38 +0900 (c0665d0)
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-#
 # Copyright (C) 2013 Droonga Project
 #
 # This library is free software; you can redistribute it and/or
@@ -15,24 +13,21 @@
 # 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 "json"
+
 require "droonga/catalog/version1"
 
 module Droonga
-  class << self
-    def catalog
-      @catalog ||= Catalog.load
+  class CatalogLoader
+    def initialize(path)
+      @path = path
     end
-  end
-
-  module Catalog
-    PATH = "catalog.json"
 
-    class << self
-      def load(path=nil)
-        path = ENV["DROONGA_CATALOG"] || PATH
-        path = File.expand_path(path)
-        Version1.new(path)
+    def load
+      data = File.open(@path) do |file|
+        JSON.parse(file.read)
       end
+      Catalog::Version1.new(data, File.dirname(@path))
     end
   end
 end

  Renamed: test/unit/catalog/test_version1.rb (+12 -4) 84%
===================================================================
--- test/unit/test_catalog.rb    2013-12-26 19:04:05 +0900 (16cb31a)
+++ test/unit/catalog/test_version1.rb    2013-12-26 19:15:38 +0900 (ecd0c21)
@@ -13,11 +13,12 @@
 # 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"
+require "droonga/catalog/version1"
 
 class CatalogTest < Test::Unit::TestCase
   def setup
-    @catalog = Droonga::Catalog.new(catalog_path)
+    data = JSON.parse(File.read(catalog_path))
+    @catalog = Droonga::Catalog::Version1.new(data, base_path)
   end
 
   def test_option
@@ -26,7 +27,6 @@ class CatalogTest < Test::Unit::TestCase
 
   def test_get_partitions
     partitions =****@catal*****_partitions("localhost:23003/test")
-    base_path = File.expand_path("../fixtures", __FILE__)
     assert_equal({
                    "localhost:23003/test.000" => {
                      :database  => "#{base_path}/000/db",
@@ -53,7 +53,15 @@ class CatalogTest < Test::Unit::TestCase
   end
 
   private
+  def fixture_path(base_path)
+    File.expand_path("../../fixtures/#{base_path}", __FILE__)
+  end
+
   def catalog_path
-    @catalog_path ||= File.expand_path("../fixtures/catalog.json", __FILE__)
+    @catalog_path ||= fixture_path("catalog/version1.json")
+  end
+
+  def base_path
+    File.dirname(catalog_path)
   end
 end

  Renamed: test/unit/fixtures/catalog/version1.json (+0 -0) 100%
===================================================================
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index