[Groonga-commit] groonga/fluent-plugin-droonga:c9a4dda [master] Post output messages from inside the handler

Back to archive index

Daijiro MORI null+****@clear*****
Wed Apr 17 21:53:24 JST 2013


Daijiro MORI	2013-04-17 21:53:24 +0900 (Wed, 17 Apr 2013)

  New Revision: c9a4dda028815cab123aa600bba3c2cc65413f8b
  https://github.com/groonga/fluent-plugin-droonga/commit/c9a4dda028815cab123aa600bba3c2cc65413f8b

  Message:
    Post output messages from inside the handler

  Modified files:
    lib/droonga/handler.rb
    lib/droonga/plugin/handler_search.rb
    lib/droonga/worker.rb
    test/plugin/test_handler_search.rb

  Modified: lib/droonga/handler.rb (+4 -0)
===================================================================
--- lib/droonga/handler.rb    2013-04-17 21:23:36 +0900 (76edd59)
+++ lib/droonga/handler.rb    2013-04-17 21:53:24 +0900 (ff5fbcf)
@@ -52,6 +52,10 @@ module Droonga
       @context =****@worke*****
     end
 
+    def post(body, destination=nil)
+      @worker.post(body, destination)
+    end
+
     def shutdown
     end
 

  Modified: lib/droonga/plugin/handler_search.rb (+17 -17)
===================================================================
--- lib/droonga/plugin/handler_search.rb    2013-04-17 21:23:36 +0900 (8fe2b0a)
+++ lib/droonga/plugin/handler_search.rb    2013-04-17 21:53:24 +0900 (3ee5f17)
@@ -39,25 +39,25 @@ module Droonga
     def search(request)
       queries = request["queries"]
       outputs = {}
-      return outputs if queries.nil?
-
-      query_sorter = QuerySorter.new
-      queries.each do |name, query|
-        query_sorter.add(name, [query["source"]])
-      end
-      results = {}
-      query_sorter.tsort.each do |name|
-        if queries[name]
-          searcher = QuerySearcher.new(@context, queries[name])
-          results[name] = searcher.search(results)
-          outputs[name] = searcher.format if searcher.need_output?
-        elsif @context[name]
-          results[name] = @context[name]
-        else
-          raise UndefinedSourceError.new(name)
+      if queries
+        query_sorter = QuerySorter.new
+        queries.each do |name, query|
+          query_sorter.add(name, [query["source"]])
+        end
+        results = {}
+        query_sorter.tsort.each do |name|
+          if queries[name]
+            searcher = QuerySearcher.new(@context, queries[name])
+            results[name] = searcher.search(results)
+            outputs[name] = searcher.format if searcher.need_output?
+          elsif @context[name]
+            results[name] = @context[name]
+          else
+            raise UndefinedSourceError.new(name)
+          end
         end
       end
-      outputs
+      post(outputs)
     end
 
     class QuerySorter

  Modified: lib/droonga/worker.rb (+12 -9)
===================================================================
--- lib/droonga/worker.rb    2013-04-17 21:23:36 +0900 (2b655da)
+++ lib/droonga/worker.rb    2013-04-17 21:53:24 +0900 (596f8b0)
@@ -70,24 +70,27 @@ module Droonga
       @handlers << plugin.instantiate(self)
     end
 
-    def process_message(envelope)
-      @envelope = envelope
-      command = envelope["type"]
-      handler = find_handler(command)
-      return unless handler
-      result = handler.handle(command, envelope["body"])
-      output = get_output
+    def post(body, destination=nil)
+      output = get_output(destination)
       if output
         response = {
           inReplyTo: envelope["id"],
           statusCode: 200,
           type: (envelope["type"] || "") + ".result",
-          body: result
+          body: body
         }
         output.post("message", response)
       end
     end
 
+    def process_message(envelope)
+      @envelope = envelope
+      command = envelope["type"]
+      handler = find_handler(command)
+      return unless handler
+      handler.handle(command, envelope["body"])
+    end
+
     private
     def post_message(envelope)
       message = envelope.to_msgpack
@@ -161,7 +164,7 @@ module Droonga
       end
     end
 
-    def get_output
+    def get_output(destination)
       receiver = @envelope["replyTo"]
       return nil unless receiver
       unless receiver =~ /\A(.*):(\d+)\/(.*?)(\?.+)?\z/

  Modified: test/plugin/test_handler_search.rb (+9 -3)
===================================================================
--- test/plugin/test_handler_search.rb    2013-04-17 21:23:36 +0900 (6e9d89c)
+++ test/plugin/test_handler_search.rb    2013-04-17 21:53:24 +0900 (28cbe83)
@@ -16,11 +16,15 @@
 require "droonga/plugin/handler_search"
 
 class Worker
-  attr_reader :context
+  attr_reader :context, :body
 
   def initialize()
     @context = Groonga::Context.default
   end
+
+  def post(body, destination=nil)
+    @body = body
+  end
 end
 
 class SearchHandlerTest < Test::Unit::TestCase
@@ -46,7 +50,8 @@ class SearchHandlerTest < Test::Unit::TestCase
   end
 
   def setup_handler
-    @handler = Droonga::SearchHandler.new(Worker.new)
+    @worker = Worker.new
+    @handler = Droonga::SearchHandler.new(@worker)
   end
 
   def teardown_handler
@@ -54,7 +59,8 @@ class SearchHandlerTest < Test::Unit::TestCase
   end
 
   def search(request)
-    normalize_result_set(@handler.search(request))
+    @handler.search(request)
+    normalize_result_set(@worker.body)
   end
 
   def normalize_result_set(result_set)
-------------- next part --------------
HTML����������������������������...
Download 



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