module Redwood
-class IndexError < StandardError
- attr_reader :source
-
- def initialize source, s
- super s
- @source = source
- end
-end
-
class Index
include Singleton
- attr_reader :index # debugging only
-
+ attr_reader :index
def initialize dir=BASE_DIR
@dir = dir
@sources = {}
def source_for name; @sources.values.find { |s| s.is_source_for? name }; end
def usual_sources; @sources.values.find_all { |s| s.usual? }; end
+ def sources; @sources.values; end
def load_index dir=File.join(@dir, "ferret")
if File.exists? dir
- Redwood::log "loading index"
+ Redwood::log "loading index..."
@index = Ferret::Index::Index.new(:path => dir, :analyzer => @analyzer)
+ Redwood::log "loaded index of #{@index.size} messages"
else
- Redwood::log "creating index"
+ Redwood::log "creating index..."
field_infos = Ferret::Index::FieldInfos.new :store => :yes
field_infos.add_field :message_id
field_infos.add_field :source_id
end
def num_results_for opts={}
- query = build_query opts
- x = @index.search(query).total_hits
- Redwood::log "num_results_for: have #{x} for query #{query}"
- x
+ 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
end
+ ## yield all messages in the thread containing 'm' by repeatedly
+ ## 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.
SAME_SUBJECT_DATE_LIMIT = 7
def each_message_in_thread_for m, opts={}
messages = {}
## significant slowdown.
##
## TODO: make this configurable, i guess
- if false
+ if true
date_min = m.date - (SAME_SUBJECT_DATE_LIMIT * 12 * 3600)
date_max = m.date + (SAME_SUBJECT_DATE_LIMIT * 12 * 3600)
"references" => doc[:refs],
}
- m =
- if source.broken?
- nil
- else
- begin
- Message.new :source => source, :source_info => doc[:source_info].to_i,
- :labels => doc[:label].split(" ").map { |s| s.intern },
- :snippet => doc[:snippet], :header => fake_header
- rescue MessageFormatError => e
- raise IndexError.new(source, "error building message #{doc[:message_id]} at #{source}/#{doc[:source_info]}: #{e.message}")
- rescue SourceError => e
- nil
- end
- end
-
- unless m
- m = Message.new :labels => doc[:label].split(" ").map { |s| s.intern },
- :snippet => doc[:snippet], :header => fake_header,
- :body => <<EOS
-#{doc[:snippet]}...
-
-An error occurred while loading this message. It is possible that the source
-has changed, or (in the case of remote sources) is down.
-
-The error message was:
- #{source.broken_msg}
-EOS
- end
- m
+ Message.new :source => source, :source_info => doc[:source_info].to_i,
+ :labels => doc[:label].split(" ").map { |s| s.intern },
+ :snippet => doc[:snippet], :header => fake_header
end
def fresh_thread_id; @next_thread_id += 1; end
if m.source.is_a? Integer
m.source
else
- m.source.id or raise "unregistered source #{m.source}"
+ m.source.id or raise "unregistered source #{m.source} (id #{m.source.id.inspect})"
end
to = (m.to + m.cc + m.bcc).map { |x| x.email }.join(" ")
def parse_user_query_string str; @qparser.parse str; end
def build_query opts
-
query = Ferret::Search::BooleanQuery.new
query.add_query opts[:qobj], :must if opts[:qobj]
labels = ([opts[:label]] + (opts[:labels] || [])).compact