[Groonga-commit] droonga/droonga-engine at a01ccd8 [master] Move logic to analyze catalog.json to CatalogGenerator

アーカイブの一覧に戻る

YUKI Hiroshi null+****@clear*****
Fri Jun 27 23:04:09 JST 2014


YUKI Hiroshi	2014-06-27 23:04:09 +0900 (Fri, 27 Jun 2014)

  New Revision: a01ccd8b7150ac67862271218a3347a82ac571d4
  https://github.com/droonga/droonga-engine/commit/a01ccd8b7150ac67862271218a3347a82ac571d4

  Message:
    Move logic to analyze catalog.json to CatalogGenerator

  Modified files:
    bin/droonga-engine-catalog-modify
    lib/droonga/catalog_generator.rb

  Modified: bin/droonga-engine-catalog-modify (+10 -39)
===================================================================
--- bin/droonga-engine-catalog-modify    2014-06-27 22:35:31 +0900 (6b01028)
+++ bin/droonga-engine-catalog-modify    2014-06-27 23:04:09 +0900 (b859f25)
@@ -85,54 +85,25 @@ def load_source(path)
     source_path = Pathname(path)
     source = source_path.read
   end
-  JSON.parse(source)
+  catalog = JSON.parse(source)
 end
 
-ADDRESS_MATCHER = /\A(.*):(\d+)\/([^\.]+)\.(.+)\z/
-def fill_dataset(options, source)
-  options[:n_workers] = source["nWorkers"]
-  options[:n_slices]  = source["replicas"].first["slices"].size
-  options[:plugins]   = source["plugins"]
-  options[:schema]    = source["schema"] if source["schema"]
-  options[:fact]      = source["fact"] if source["fact"]
+source_catalog = load_source(options.source_path)
+dataset_params = generator.catalog_to_params(source_catalog)
+datasets.each do |name, options|
+  dataset = dataset_params[name]
 
-  nodes = source["replicas"].collect do |replica|
-    ADDRESS_MATCHER =~ replica["slices"].first["volume"]["address"]
-    {
-      :host => $1,
-      :port => $2.to_i,
-      :tag  => $3,
-      :path => $4,
-    }
-  end
-  options[:tag]   = nodes.first[:tag]
-  options[:port]  = nodes.first[:port].to_i
-  options[:hosts] ||= nodes.collect do |node|
-    node[:host]
-  end
+  dataset[:hosts] = options[:hosts] if options[:hosts]
 
   if options[:add_replica_hosts]
-    options[:hosts] += options[:add_replica_hosts]
-    options[:hosts].uniq!
+    dataset[:hosts] += options[:add_replica_hosts]
+    dataset[:hosts].uniq!
   end
   if options[:remove_replica_hosts]
-    options[:hosts] -= options[:remove_replica_hosts]
+    dataset[:hosts] -= options[:remove_replica_hosts]
   end
-end
-
-source = load_source(options.source_path)
-datasets.each do |name, dataset|
-  source_dataset = source["datasets"][name]
-  fill_dataset(dataset, source_dataset)
-end
-
-if datasets[Droonga::CatalogGenerator::DEFAULT_DATASET].empty?
-  datasets.delete(Droonga::CatalogGenerator::DEFAULT_DATASET)
-end
 
-
-datasets.each do |name, options|
-  generator.add_dataset(name, options)
+  generator.add_dataset(name, dataset)
 end
 
 def open_output(path)

  Modified: lib/droonga/catalog_generator.rb (+43 -0)
===================================================================
--- lib/droonga/catalog_generator.rb    2014-06-27 22:35:31 +0900 (87aeff5)
+++ lib/droonga/catalog_generator.rb    2014-06-27 23:04:09 +0900 (06ebb18)
@@ -159,5 +159,48 @@ module Droonga
         @weight ||= 100 / @n_slices
       end
     end
+
+    public
+    class << self
+      def catalog_to_params(catalog)
+        new.catalog_to_params(catalog)
+      end
+    end
+
+    def catalog_to_params(catalog)
+      datasets = {}
+      catalog["datasets"].each do |name, dataset|
+        datasets[name] = dataset_to_params(dataset)
+      end
+      datasets
+    end
+
+    private
+    ADDRESS_MATCHER = /\A(.*):(\d+)\/([^\.]+)\.(.+)\z/
+
+    def dataset_to_params(dataset)
+      params = {}
+      params[:n_workers] = dataset["nWorkers"]
+      params[:n_slices]  = dataset["replicas"].first["slices"].size
+      params[:plugins]   = dataset["plugins"]
+      params[:schema]    = dataset["schema"] if dataset["schema"]
+      params[:fact]      = dataset["fact"] if dataset["fact"]
+
+      nodes = dataset["replicas"].collect do |replica|
+        ADDRESS_MATCHER =~ replica["slices"].first["volume"]["address"]
+        {
+          :host => $1,
+          :port => $2.to_i,
+          :tag  => $3,
+          :path => $4,
+        }
+      end
+      params[:tag]   = nodes.first[:tag]
+      params[:port]  = nodes.first[:port].to_i
+      params[:hosts] = nodes.collect do |node|
+        node[:host]
+      end
+      params
+    end
   end
 end
-------------- next part --------------
HTML����������������������������...
ダウンロード 



More information about the Groonga-commit mailing list
アーカイブの一覧に戻る