lib/sup/index.rb in sup-0.1 vs lib/sup/index.rb in sup-0.2

- old
+ new

@@ -152,15 +152,14 @@ ## docid and entry can be specified if they're already known. def sync_message m, docid=nil, entry=nil docid, entry = load_entry_for_id m.id unless docid && entry raise "no source info for message #{m.id}" unless m.source && m.source_info - raise "trying deleting non-corresponding entry #{docid}" if docid && @index[docid][:message_id] != m.id + raise "trying to delete non-corresponding entry #{docid} with index message-id #{@index[docid][:message_id].inspect} and parameter message id #{m.id.inspect}" if docid && @index[docid][:message_id] != m.id source_id = if m.source.is_a? Integer - raise "Debugging: integer source set" m.source else m.source.id or raise "unregistered source #{m.source} (id #{m.source.id.inspect})" end @@ -182,11 +181,11 @@ @index.delete docid if docid @index.add_document d docid, entry = load_entry_for_id m.id ## this hasn't been triggered in a long time. TODO: decide whether it's still a problem. - raise "just added message #{m.id} but couldn't find it in a search" unless docid + raise "just added message #{m.id.inspect} 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 @@ -224,14 +223,16 @@ ## 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. ## - ## stops loading any thread if a message with a :killed flag is found. + ## only two options, :limit and :skip_killed. if :skip_killed is + ## true, 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}" + #Redwood::log "Building thread for #{m.id}: #{m.subj}" messages = {} searched = {} num_queries = 0 if $config[:thread_by_subject] # do subject queries @@ -260,27 +261,37 @@ searched[id] = true q = Ferret::Search::BooleanQuery.new true q.add_query Ferret::Search::TermQuery.new(:message_id, id), :should q.add_query Ferret::Search::TermQuery.new(:refs, id), :should - q = build_query :qobj => q, :load_killed => true + q = build_query :qobj => q num_queries += 1 + killed = false @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] + if @index[docid][:label].split(/\s+/).include?("killed") && opts[:skip_killed] + killed = true + break + end mid = @index[docid][:message_id] unless messages.member?(mid) - Redwood::log "got #{mid} as a child of #{id}" + #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 + 1} messages for #{m.id}" if num_queries > 0 - messages.each { |mid, builder| yield mid, builder } + if killed + Redwood::log "thread for #{m.id} is killed, ignoring" + false + else + Redwood::log "ran #{num_queries} queries to build thread of #{messages.size + 1} messages for #{m.id}: #{m.subj}" if num_queries > 0 + messages.each { |mid, builder| yield mid, builder } + true + end end ## builds a message object from a ferret result def build_message docid doc = @index[docid] @@ -292,11 +303,11 @@ "date" => Time.at(doc[:date].to_i), "subject" => unwrap_subj(doc[:subject]), "from" => doc[:from], "to" => doc[:to], "message-id" => doc[:message_id], - "references" => doc[:refs], + "references" => doc[:refs].split(/\s+/).map { |x| "<#{x}>" }.join(" "), } 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 @@ -328,11 +339,11 @@ Redwood::log "contact search: #{q}" contacts = {} num = h[:num] || 20 @index.search_each(q, :sort => "date DESC", :limit => :all) do |docid, score| break if contacts.size >= num - #Redwood::log "got message with to: #{@index[docid][:to].inspect} and from: #{@index[docid][:from].inspect}" + #Redwood::log "got message #{docid} 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| contacts[PersonManager.person_for(e)] = true } @@ -357,11 +368,24 @@ index.search(q, :limit => 1).total_hits > 0 end protected - def parse_user_query_string str; @qparser.parse str; end + def parse_user_query_string str + str2 = str.gsub(/(to|from):(\S+)/) do + field, name = $1, $2 + if(p = ContactManager.contact_for(name)) + [field, p.email] + else + [field, name] + end.join(":") + end + + Redwood::log "translated #{str} to #{str2}" unless str2 == str + @qparser.parse str2 + 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 labels.each { |t| query.add_query Ferret::Search::TermQuery.new("label", t.to_s), :must } @@ -374,10 +398,10 @@ query.add_query q2, :must end query.add_query Ferret::Search::TermQuery.new("label", "spam"), :must_not unless opts[:load_spam] || labels.include?(:spam) query.add_query Ferret::Search::TermQuery.new("label", "deleted"), :must_not unless opts[:load_deleted] || labels.include?(:deleted) - query.add_query Ferret::Search::TermQuery.new("label", "killed"), :must_not unless opts[:load_killed] || labels.include?(:killed) + query.add_query Ferret::Search::TermQuery.new("label", "killed"), :must_not if opts[:skip_killed] query end def save_sources fn=Redwood::SOURCE_FN if @sources_dirty || @sources.any? { |id, s| s.dirty? }