lib/sup/index.rb in sup-0.0.8 vs lib/sup/index.rb in sup-0.1
- old
+ new
@@ -5,27 +5,100 @@
require 'ferret'
module Redwood
class Index
+ class LockError < StandardError
+ def initialize h
+ @h = h
+ end
+
+ def method_missing m; @h[m.to_s] end
+ end
+
include Singleton
attr_reader :index
def initialize dir=BASE_DIR
@dir = dir
@sources = {}
@sources_dirty = false
wsa = Ferret::Analysis::WhiteSpaceAnalyzer.new false
- sa = Ferret::Analysis::StandardAnalyzer.new Ferret::Analysis::FULL_ENGLISH_STOP_WORDS, true
+ sa = Ferret::Analysis::StandardAnalyzer.new [], true
@analyzer = Ferret::Analysis::PerFieldAnalyzer.new wsa
@analyzer[:body] = sa
- @qparser ||= Ferret::QueryParser.new :default_field => :body, :analyzer => @analyzer
+ @analyzer[:subject] = sa
+ @qparser ||= Ferret::QueryParser.new :default_field => :body, :analyzer => @analyzer, :or_default => false
+ @lock = Lockfile.new lockfile, :retries => 0, :max_age => nil
self.class.i_am_the_instance self
end
+ def lockfile; File.join @dir, "lock" end
+
+ def lock
+ Redwood::log "locking #{lockfile}..."
+ begin
+ @lock.lock
+ rescue Lockfile::MaxTriesLockError
+ raise LockError, @lock.lockinfo_on_disk
+ end
+ end
+
+ def start_lock_update_thread
+ @lock_update_thread = Redwood::reporting_thread do
+ while true
+ sleep 30
+ @lock.touch_yourself
+ end
+ end
+ end
+
+ def stop_lock_update_thread
+ @lock_update_thread.kill if @lock_update_thread
+ @lock_update_thread = nil
+ end
+
+ def fancy_lock_error_message_for e
+ secs = Time.now - e.mtime
+ mins = secs.to_i / 60
+ time =
+ if mins == 0
+ "#{secs.to_i} seconds"
+ else
+ "#{mins} minutes"
+ end
+
+ <<EOS
+Error: the sup index is locked by another process! User '#{e.user}' on
+host '#{e.host}' is running #{e.pname} with pid #{e.pid}. The process was alive
+as of #{time} ago.
+EOS
+ end
+
+ def lock_or_die
+ begin
+ lock
+ rescue LockError => e
+ $stderr.puts fancy_lock_error_message_for(e)
+ $stderr.puts <<EOS
+
+You can wait for the process to finish, or, if it crashed and left a
+stale lock file behind, you can manually delete #{@lock.path}.
+EOS
+ exit
+ end
+ end
+
+ def unlock
+ if @lock && @lock.locked?
+ Redwood::log "unlocking #{lockfile}..."
+ @lock.unlock
+ end
+ end
+
def load
load_sources
load_index
end
@@ -114,11 +187,11 @@
raise "just added message #{m.id} but couldn't find it in a search" unless docid
true
end
def save_index fn=File.join(@dir, "ferret")
- # don't have to do anything, apparently
+ # don't have to do anything, apparently
end
def contains_id? id
@index.search(Ferret::Search::TermQuery.new(:message_id, id)).total_hits > 0
end
@@ -141,29 +214,29 @@
end
end
def num_results_for opts={}
return 0 if @index.size == 0 # otherwise ferret barfs ###TODO: remove this once my ferret patch is accepted
+
q = build_query opts
- index.search(q).total_hits
+ index.search(q, :limit => 1).total_hits
end
## yield all messages in the thread containing 'm' by repeatedly
- ## querying the index. uields pairs of message ids and
+ ## querying the index. yields pairs of message ids and
## message-building lambdas, so that building an unwanted message
## can be skipped in the block if desired.
##
## stops loading any thread if a message with a :killed flag is found.
-
SAME_SUBJECT_DATE_LIMIT = 7
def each_message_in_thread_for m, opts={}
+ Redwood::log "Building thread for #{m.id}: #{m.subj}"
messages = {}
searched = {}
num_queries = 0
- ## todo: make subject querying configurable
- if true # do subject queries
+ if $config[:thread_by_subject] # do subject queries
date_min = m.date - (SAME_SUBJECT_DATE_LIMIT * 12 * 3600)
date_max = m.date + (SAME_SUBJECT_DATE_LIMIT * 12 * 3600)
q = Ferret::Search::BooleanQuery.new true
sq = Ferret::Search::PhraseQuery.new(:subject)
@@ -194,18 +267,19 @@
num_queries += 1
@index.search_each(q, :limit => :all) do |docid, score|
break if opts[:limit] && messages.size >= opts[:limit]
break if @index[docid][:label].split(/\s+/).include? "killed" unless opts[:load_killed]
mid = @index[docid][:message_id]
- unless messages.member? mid
+ unless messages.member?(mid)
+ Redwood::log "got #{mid} as a child of #{id}"
messages[mid] ||= lambda { build_message docid }
refs = @index[docid][:refs].split(" ")
pending += refs
end
end
end
- Redwood::log "ran #{num_queries} queries to build thread of #{messages.size} messages for #{m.id}" if num_queries > 0
+ Redwood::log "ran #{num_queries} queries to build thread of #{messages.size + 1} messages for #{m.id}" if num_queries > 0
messages.each { |mid, builder| yield mid, builder }
end
## builds a message object from a ferret result
def build_message docid
@@ -259,15 +333,13 @@
#Redwood::log "got message with to: #{@index[docid][:to].inspect} and from: #{@index[docid][:from].inspect}"
f = @index[docid][:from]
t = @index[docid][:to]
if AccountManager.is_account_email? f
- t.split(" ").each { |e| #Redwood::log "adding #{e} because there's a message to him from account email #{f}";
- contacts[Person.for(e)] = true }
+ t.split(" ").each { |e| contacts[PersonManager.person_for(e)] = true }
else
- #Redwood::log "adding from #{f} because there's a message from him to #{t}"
- contacts[Person.for(f)] = true
+ contacts[PersonManager.person_for(f)] = true
end
end
contacts.keys.compact
end
@@ -276,10 +348,17 @@
source_array = (Redwood::load_yaml_obj(fn) || []).map { |o| Recoverable.new o }
@sources = Hash[*(source_array).map { |s| [s.id, s] }.flatten]
@sources_dirty = false
end
+ def has_any_from_source_with_label? source, label
+ q = Ferret::Search::BooleanQuery.new
+ q.add_query Ferret::Search::TermQuery.new("source_id", source.id.to_s), :must
+ q.add_query Ferret::Search::TermQuery.new("label", label.to_s), :must
+ index.search(q, :limit => 1).total_hits > 0
+ end
+
protected
def parse_user_query_string str; @qparser.parse str; end
def build_query opts
query = Ferret::Search::BooleanQuery.new
@@ -304,12 +383,12 @@
def save_sources fn=Redwood::SOURCE_FN
if @sources_dirty || @sources.any? { |id, s| s.dirty? }
bakfn = fn + ".bak"
if File.exists? fn
File.chmod 0600, fn
- FileUtils.mv fn, bakfn, :force => true unless File.exists?(bakfn) && File.size(bakfn) > File.size(fn)
+ FileUtils.mv fn, bakfn, :force => true unless File.exists?(bakfn) && File.size(fn) == 0
end
- Redwood::save_yaml_obj @sources.values, fn
+ Redwood::save_yaml_obj @sources.values.sort_by { |s| s.id.to_i }, fn, true
File.chmod 0600, fn
end
@sources_dirty = false
end
end