Class: Droonga::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/watcher.rb

Constant Summary collapse

EXACT_MATCH =
false

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Watcher

Returns a new instance of Watcher.



22
23
24
25
26
27
28
# File 'lib/droonga/watcher.rb', line 22

def initialize(context)
  @context = context

  @subscriber_table = @context["Subscriber"]
  @query_table      = @context["Query"]
  @keyword_table    = @context["Keyword"]
end

Instance Method Details

#eval_condition(condition, words) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/droonga/watcher.rb', line 154

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

#feed(request, &block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/droonga/watcher.rb', line 95

def feed(request, &block)
  targets = request[:targets]

  hits = []
  targets.each do |key, target|
    scan_body(hits, target)
  end
  hits.uniq! # hits may be duplicated if multiple targets are matched

  publish(hits, request, &block)
end

#pick_keywords(memo, condition) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/droonga/watcher.rb', line 107

def pick_keywords(memo, condition)
  case condition
  when Hash
    memo << condition["query"]
  when String
    memo << condition
  when Array
    condition[1..-1].each do |element|
      pick_keywords(memo, element)
    end
  end
  memo
end

#publish(hits, request) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/droonga/watcher.rb', line 182

def publish(hits, request)
  routes = {}
  hits.each do |query|
    subscribers = @subscriber_table.select do |subscriber|
      subscriber.subscriptions =~ query
    end
    subscribers.each do |subscriber|
      route = subscriber.route.key
      routes[route] ||= []
      routes[route] << subscriber.key.key
    end
=begin
# "group" version. This is slower than above...
    route_records = subscribers.group("route",
                                      :max_n_sub_records => subscribers.size)
    route_records.each do |route_record|
      route = route_record._key
      routes[route] ||= []
      route_record.sub_records.each do |subscriber|
        routes[route] << subscriber.key.key
      end
    end
=end
  end
  routes.each do |route, subscribers|
    yield(route, subscribers)
  end
end

#query_match(query, keywords) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/droonga/watcher.rb', line 138

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

#scan_body(hits, body) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/droonga/watcher.rb', line 121

def scan_body(hits, body)
  trimmed = body.strip
  candidates = {}
  # FIXME scan reports the longest keyword matched only
  @keyword_table.scan(trimmed).each do |keyword, word, start, length|
    @query_table.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

#subscribe(request) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/droonga/watcher.rb', line 30

def subscribe(request)
  subscriber = request[:subscriber]
  condition  = request[:condition]
  query      = request[:query]
  route      = request[:route]

  # XXX better validation and error class must be written!!
  if subscriber.nil? or subscriber.empty? or condition.nil? or
     query.nil? or route.nil?
    raise "invalid request"
  end
  raise "too long query" if query.size > 4095

  query_record = @query_table[query]
  unless query_record
    keywords = pick_keywords([], condition)
    query_record = @query_table.add(query, :keywords => keywords)
  end
  subscriber_record = @subscriber_table[subscriber]
  if subscriber_record
    subscriptions = subscriber_record.subscriptions
    unless subscriptions.include?(query_record)
      subscriptions << query_record
      subscriber_record.subscriptions = subscriptions
    end
    subscriber_record.last_modified = Time.now
  else
    @subscriber_table.add(subscriber,
                          :subscriptions => [query_record],
                          :route => route,
                          :last_modified => Time.now)
  end
end

#unsubscribe(request) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/droonga/watcher.rb', line 64

def unsubscribe(request)
  subscriber = request[:subscriber]
  query      = request[:query]

  if subscriber.nil? or subscriber.empty?
    raise "invalid request"
  end

  subscriber_record = @subscriber_table[subscriber]
  return unless subscriber_record

  if query.nil? or query.empty?
    delete_subscriber(subscriber_record)
  else
    query_record = @query_table[query]
    return unless query_record

    subscriptions = subscriber_record.subscriptions
    new_subscriptions = subscriptions.select do |subscription|
      subscription != query_record
    end

    if new_subscriptions.empty?
      delete_subscriber(subscriber_record)
    else
      subscriber_record.subscriptions = new_subscriptions
      sweep_orphan_queries(subscriptions)
    end
  end
end