[Groonga-commit] long-long-float/droonga-engine at 6542cfb [master] Extract CatalogModifier from SerfEventHandler

アーカイブの一覧に戻る

YUKI Hiroshi null+****@clear*****
Thu Aug 28 16:24:49 JST 2014


YUKI Hiroshi	2014-08-28 16:24:49 +0900 (Thu, 28 Aug 2014)

  New Revision: 6542cfb17d197b429f6a213127078392aebdc20f
  https://github.com/long-long-float/droonga-engine/commit/6542cfb17d197b429f6a213127078392aebdc20f

  Message:
    Extract CatalogModifier from SerfEventHandler

  Added files:
    lib/droonga/catalog_modifier.rb
  Modified files:
    lib/droonga/command/serf_event_handler.rb

  Added: lib/droonga/catalog_modifier.rb (+43 -0) 100644
===================================================================
--- /dev/null
+++ lib/droonga/catalog_modifier.rb    2014-08-28 16:24:49 +0900 (b4b849d)
@@ -0,0 +1,43 @@
+# Copyright (C) 2014 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require "json"
+
+require "droonga/path"
+require "droonga/catalog_generator"
+require "droonga/safe_file_writer"
+
+module Droonga
+  class CatalogModifier
+    class << self
+      def modify
+        new.modify do |generator|
+          yield(generator)
+        end
+      end
+    end
+
+    def initialize
+      @generator = CatalogGenerator.new
+      @catalog = JSON.parse(Path.catalog.read)
+      @generator.load(@catalog)
+    end
+
+    def modify
+      yield(@generator)
+      SafeFileWriter.write(Path.catalog, JSON.pretty_generate(@generator.generate))
+    end
+  end
+end

  Modified: lib/droonga/command/serf_event_handler.rb (+6 -13)
===================================================================
--- lib/droonga/command/serf_event_handler.rb    2014-08-28 16:19:09 +0900 (8237cfb)
+++ lib/droonga/command/serf_event_handler.rb    2014-08-28 16:24:49 +0900 (ce42562)
@@ -18,6 +18,7 @@ require "json"
 require "droonga/path"
 require "droonga/serf"
 require "droonga/catalog_generator"
+require "droonga/catalog_modifier"
 require "droonga/catalog_fetcher"
 require "droonga/data_absorber"
 require "droonga/safe_file_writer"
@@ -171,7 +172,7 @@ module Droonga
         if @payload["copy"]
           log("starting to copy data from #{source_host}")
 
-          modify_catalog do |modifier|
+          CatalogModifier.modify do |modifier|
             modifier.datasets[dataset_name].replicas.hosts = [host]
           end
           sleep(5) #TODO: wait for restart. this should be done more safely, to avoid starting of absorbing with old catalog.json.
@@ -188,7 +189,7 @@ module Droonga
 
         log("joining to the cluster: update myself")
 
-        modify_catalog do |modifier|
+        CatalogModifier.modify do |modifier|
           modifier.datasets[dataset_name].replicas.hosts += other_hosts
           modifier.datasets[dataset_name].replicas.hosts.uniq!
         end
@@ -203,7 +204,7 @@ module Droonga
 
         log("new replicas: #{hosts.join(",")}")
 
-        modify_catalog do |modifier|
+        CatalogModifier.modify do |modifier|
           modifier.datasets[dataset].replicas.hosts = hosts
         end
       end
@@ -220,7 +221,7 @@ module Droonga
 
         log("adding replicas: #{hosts.join(",")}")
 
-        modify_catalog do |modifier|
+        CatalogModifier.modify do |modifier|
           modifier.datasets[dataset].replicas.hosts += hosts
           modifier.datasets[dataset].replicas.hosts.uniq!
         end
@@ -235,19 +236,11 @@ module Droonga
 
         log("removing replicas: #{hosts.join(",")}")
 
-        modify_catalog do |modifier|
+        CatalogModifier.modify do |modifier|
           modifier.datasets[dataset].replicas.hosts -= hosts
         end
       end
 
-      def modify_catalog
-        generator = CatalogGenerator.new
-        current_catalog = JSON.parse(Path.catalog.read)
-        generator.load(current_catalog)
-        yield(generator)
-        SafeFileWriter.write(Path.catalog, JSON.pretty_generate(generator.generate))
-      end
-
       def absorb_data
         source = @payload["source"]
         return unless source
-------------- next part --------------
HTML����������������������������...
ダウンロード 



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