]> git.notmuchmail.org Git - sup/blobdiff - lib/sup/index.rb
added back thread-by-subject
[sup] / lib / sup / index.rb
index 5c1e2e4090c5694eb1f64e6fa6e95f4f3e95ea5f..69aed22b8885e5631093c34630806b9612736870 100644 (file)
@@ -7,20 +7,10 @@ require 'ferret'
 
 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 = {}
@@ -57,13 +47,15 @@ class Index
 
   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
@@ -125,12 +117,15 @@ class Index
   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 = {}
@@ -141,7 +136,7 @@ class Index
     ## 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)
 
@@ -198,35 +193,9 @@ class Index
       "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
@@ -240,7 +209,7 @@ EOS
       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(" ")
@@ -310,7 +279,6 @@ protected
 
   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