[Groonga-commit] droonga/fluent-plugin-droonga at 3b7c30c [master] Isolate collector plugin from handler plugin

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Nov 24 19:40:06 JST 2013


Kouhei Sutou	2013-11-24 19:40:06 +0900 (Sun, 24 Nov 2013)

  New Revision: 3b7c30c383985e81eff66ca88a37cdd443cccb02
  https://github.com/droonga/fluent-plugin-droonga/commit/3b7c30c383985e81eff66ca88a37cdd443cccb02

  Message:
    Isolate collector plugin from handler plugin

  Added files:
    lib/droonga/collector_plugin.rb
  Modified files:
    lib/droonga/collector.rb
    lib/droonga/dispatcher.rb
  Renamed files:
    lib/droonga/plugin/collector/basic.rb
      (from lib/droonga/plugin/handler/collector.rb)

  Modified: lib/droonga/collector.rb (+25 -23)
===================================================================
--- lib/droonga/collector.rb    2013-11-24 18:36:34 +0900 (7f9a4df)
+++ lib/droonga/collector.rb    2013-11-24 19:40:06 +0900 (d61db86)
@@ -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/legacy_plugin"
+require "droonga/collector_plugin"
 
 module Droonga
   class Collector
@@ -26,6 +26,7 @@ module Droonga
       @tasks = tasks
       @n_dones = 0
       @inputs = inputs
+      @plugins = load_plugins(["basic"]) # TODO: make customizable
     end
 
     def handle(name, value)
@@ -62,7 +63,11 @@ module Droonga
             message["descendants"] = descendants
             message["id"] = @id
           end
-          @dispatcher.deliver(@id, task["route"], message, command, synchronous)
+          if @id == task["route"]
+            process(command, message)
+          else
+            @dispatcher.deliver(@id, task["route"], message, command, synchronous)
+          end
         end
         return if task["n_of_inputs"] < n_of_expects
         #the task is done
@@ -87,30 +92,27 @@ module Droonga
         @dispatcher.collectors.delete(@id) if @n_dones ==****@tasks*****
       end
     end
-  end
 
-  class CollectorHandler < Droonga::LegacyPlugin
-    attr_reader :task, :input_name, :component, :output_values, :body, :output_names
-    def handle(command, request, *arguments)
-      return false unless request.is_a? Hash
-      @task = request["task"]
-      return false unles****@task*****_a? Hash
-      @component = @task["component"]
-      return false unles****@compo*****_a? Hash
-      @output_values = @task["values"]
-      @body = @component["body"]
-      @output_names = @component["outputs"]
-      @id = request["id"]
-      @value = request["value"]
-      @input_name = request["name"]
-      @descendants = request["descendants"]
-      invoke(command, @value, *arguments)
-      output if @descendants
-      true
+    private
+    def process(command, message)
+      plugin = find_plugin(command)
+      if plugin.nil?
+        raise "unknown collector plugin: <#{command}>: " +
+                "TODO: improve error hndling"
+      end
+      plugin.process(command, message)
     end
 
-    def prefer_synchronous?(command)
-      return true
+    def load_plugins(names)
+      names.collect do |name|
+        CollectorPlugin.repository.instantiate(name, @dispatcher)
+      end
+    end
+
+    def find_plugin(command)
+      @plugins.find do |plugin|
+        plugin.processable?(command)
+      end
     end
   end
 end

  Added: lib/droonga/collector_plugin.rb (+82 -0) 100644
===================================================================
--- /dev/null
+++ lib/droonga/collector_plugin.rb    2013-11-24 19:40:06 +0900 (d92c775)
@@ -0,0 +1,82 @@
+# -*- coding: utf-8 -*-
+#
+# 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/plugin"
+
+module Droonga
+  class CollectorPlugin < Plugin
+    extend PluginRegisterable
+
+    attr_reader :task, :input_name, :component, :output_values, :body, :output_names
+    def initialize(dispatcher)
+      super()
+      @dispatcher = dispatcher
+    end
+
+    def process(command, message)
+      return false unless message.is_a? Hash
+      @task = message["task"]
+      return false unles****@task*****_a? Hash
+      @component = @task["component"]
+      return false unles****@compo*****_a? Hash
+      @output_values = @task["values"]
+      @body = @component["body"]
+      @output_names = @component["outputs"]
+      @id = message["id"]
+      @value = message["value"]
+      @input_name = message["name"]
+      @descendants = message["descendants"]
+      super(command, @value)
+      output if @descendants
+      true
+    end
+
+    # TODO: consider better name
+    def emit(value, name=nil)
+      unless name
+        if @output_names
+          name = @output_names.first
+        else
+          @output_values = @task["values"] = value
+          return
+        end
+      end
+      @output_values[name] = value
+    end
+
+    def post(message, destination=nil)
+      @distributor.post(message, destination)
+    end
+
+    def output
+      result = @task["values"]
+      post(result, @component["post"]) if @component["post"]
+      @descendants.each do |name, dests|
+        message = {
+          "id" => @id,
+          "input" => name,
+          "value" => result[name]
+        }
+        dests.each do |routes|
+          routes.each do |route|
+            post(message, "to"=>route, "type"=>"dispatcher")
+          end
+        end
+      end
+    end
+  end
+end

  Modified: lib/droonga/dispatcher.rb (+1 -1)
===================================================================
--- lib/droonga/dispatcher.rb    2013-11-24 18:36:34 +0900 (6efac4e)
+++ lib/droonga/dispatcher.rb    2013-11-24 19:40:06 +0900 (9a79017)
@@ -33,7 +33,7 @@ module Droonga
       @collectors = {}
       @current_id = 0
       @local = Regexp.new("^#{@name}")
-      plugins = ["collector"] + (Droonga.catalog.option("plugins")||[])
+      plugins = (Droonga.catalog.option("plugins")||[])
       plugins.each do |plugin|
         @worker.add_legacy_plugin(plugin)
       end

  Renamed: lib/droonga/plugin/collector/basic.rb (+3 -4) 92%
===================================================================
--- lib/droonga/plugin/handler/collector.rb    2013-11-24 18:36:34 +0900 (c5716af)
+++ lib/droonga/plugin/collector/basic.rb    2013-11-24 19:40:06 +0900 (b2446dc)
@@ -15,12 +15,11 @@
 # 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/legacy_plugin"
-require "droonga/searcher"
+require "droonga/collector_plugin"
 
 module Droonga
-  class BasicCollectorHandler < Droonga::CollectorHandler
-    Droonga::LegacyPlugin.repository.register("collector", self)
+  class BasicCollector < Droonga::CollectorPlugin
+    repository.register("basic", self)
 
     command :collector_gather
     def collector_gather(request)
-------------- next part --------------
HTML����������������������������...
Download 



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