[Groonga-commit] droonga/fluent-plugin-droonga at adb59aa [master] Add feed command to watch handler

Back to archive index

Yoji Shidara null+****@clear*****
Fri Oct 11 15:03:18 JST 2013


Yoji Shidara	2013-10-11 15:03:18 +0900 (Fri, 11 Oct 2013)

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

  Message:
    Add feed command to watch handler

  Modified files:
    lib/droonga/plugin/handler_watch.rb

  Modified: lib/droonga/plugin/handler_watch.rb (+74 -0)
===================================================================
--- lib/droonga/plugin/handler_watch.rb    2013-10-11 15:02:51 +0900 (8c85d2c)
+++ lib/droonga/plugin/handler_watch.rb    2013-10-11 15:03:18 +0900 (5c6a0a0)
@@ -20,6 +20,7 @@ require "droonga/handler"
 module Droonga
   class WatchHandler < Droonga::Handler
     Droonga::HandlerPlugin.register("watch", self)
+    EXACT_MATCH = false
 
     command "watch"
     def watch(request)
@@ -46,6 +47,19 @@ module Droonga
       end
     end
 
+    command "feed"
+    def feed(request)
+      values = request["values"]
+
+      hits = []
+      values.each do |key, value|
+        scan_body(hits, value)
+      end
+      #publish(hits, request)
+      p [hits, request]
+      # TODO publish
+    end
+
     private
     def parse_request(request)
       user = request["user"]
@@ -70,5 +84,65 @@ module Droonga
       end
       memo
     end
+
+    def scan_body(hits, body)
+      trimmed = body.strip
+      candidates = {}
+      @context['Keyword'].scan(trimmed).each do |keyword, word, start, length|
+        @context['Query'].select do |query|
+          query.keywords =~ keyword
+        end.each do |record|
+          candidates[record.key] ||= []
+          candidates[record.key] << keyword
+        end
+      end
+      candidates.each do |query, keywords|
+        hits << query if query_match(query, keywords)
+      end
+    end
+
+    def query_match(query, keywords)
+      return true unless EXACT_MATCH
+      @conditions = {} unless @conditions
+      condition = @conditions[query.id]
+      unless condition
+        condition = JSON.parse(query.key)
+        @conditions[query.id] = condition
+        # CAUTION: @conditions can be huge.
+      end
+      words = {}
+      keywords.each do |keyword|
+        words[keyword.key] = true
+      end
+      eval_condition(condition, words)
+    end
+
+    def eval_condition(condition, words)
+      case condition
+      when Hash
+        # todo
+      when String
+        words[condition]
+      when Array
+        case condition.first
+        when "||"
+          condition[1..-1].each do |element|
+            return true if eval_condition(element, words)
+          end
+          false
+        when "&&"
+          condition[1..-1].each do |element|
+            return false unless eval_condition(element, words)
+          end
+          true
+        when "-"
+          return false unless eval_condition(condition[1], words)
+          condition[2..-1].each do |element|
+            return false if eval_condition(element, words)
+          end
+          true
+        end
+      end
+    end
   end
 end
-------------- next part --------------
HTML����������������������������...
Download 



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