X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=lib%2Fsup%2Findex.rb;h=ca01ee76cc89c118933c5122b616b4b6f45a59bb;hb=0b5543a02c5d2dfc1a725e63e4a922bea9f903fd;hp=2bdf22b0c2b2ea22f904cc349672a1fe69fc20ad;hpb=4ea04212928446fb62c5fd5ecca9c18b0339edf9;p=sup diff --git a/lib/sup/index.rb b/lib/sup/index.rb index 2bdf22b..ca01ee7 100644 --- a/lib/sup/index.rb +++ b/lib/sup/index.rb @@ -2,7 +2,6 @@ require 'fileutils' require 'ferret' -require 'fastthread' begin require 'chronic' @@ -177,31 +176,31 @@ EOS end end - ## Syncs the message to the index: deleting if it's already there, - ## and adding either way. Index state will be determined by m.labels. + ## Syncs the message to the index, replacing any previous version. adding + ## either way. Index state will be determined by the message's #labels + ## accessor. ## - ## docid and entry can be specified if they're already known. - def sync_message m, docid=nil, entry=nil, opts={} - docid, entry = load_entry_for_id m.id unless docid && entry + ## if need_load is false, docid and entry are assumed to be set to the + ## result of load_entry_for_id (which can be nil). + def sync_message m, need_load=true, docid=nil, entry=nil, opts={} + docid, entry = load_entry_for_id m.id if need_load raise "no source info for message #{m.id}" unless m.source && m.source_info @index_mutex.synchronize do 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 end - source_id = - if m.source.is_a? Integer - m.source - else - m.source.id or raise "unregistered source #{m.source} (id #{m.source.id.inspect})" - end + source_id = if m.source.is_a? Integer + m.source + else + m.source.id or raise "unregistered source #{m.source} (id #{m.source.id.inspect})" + end - snippet = - if m.snippet_contains_encrypted_content? && $config[:discard_snippets_from_encrypted_messages] - "" - else - m.snippet - end + snippet = if m.snippet_contains_encrypted_content? && $config[:discard_snippets_from_encrypted_messages] + "" + else + m.snippet + end ## write the new document to the index. if the entry already exists in the ## index, reuse it (which avoids having to reload the entry from the source, @@ -261,15 +260,14 @@ EOS :refs => (entry[:refs] || (m.refs + m.replytos).uniq.join(" ")), } - @index_mutex.synchronize do + @index_mutex.synchronize do @index.delete docid if docid @index.add_document d end - 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.inspect} but couldn't find it in a search" unless docid - true + ## this hasn't been triggered in a long time. + ## docid, entry = load_entry_for_id m.id + ## raise "just added message #{m.id.inspect} but couldn't find it in a search" unless docid end def save_index fn=File.join(@dir, "ferret") @@ -333,7 +331,7 @@ EOS q = Ferret::Search::BooleanQuery.new true sq = Ferret::Search::PhraseQuery.new(:subject) - wrap_subj(Message.normalize_subj(m.subj)).split(/\s+/).each do |t| + wrap_subj(Message.normalize_subj(m.subj)).split.each do |t| sq.add_term t end q.add_query sq, :must @@ -378,7 +376,7 @@ EOS unless messages.member?(mid) #Redwood::log "got #{mid} as a child of #{id}" messages[mid] ||= lambda { build_message docid } - refs = @index[docid][:refs].split(" ") + refs = @index[docid][:refs].split pending += refs.select { |id| !searched[id] } end end @@ -389,7 +387,7 @@ EOS 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 + Redwood::log "ran #{num_queries} queries to build thread of #{messages.size} messages for #{m.id}: #{m.subj}" if num_queries > 0 messages.each { |mid, builder| yield mid, builder } true end @@ -409,14 +407,16 @@ EOS "date" => Time.at(doc[:date].to_i), "subject" => unwrap_subj(doc[:subject]), "from" => doc[:from], - "to" => doc[:to].split(/\s+/).join(", "), # reformat + "to" => doc[:to].split.join(", "), # reformat "message-id" => doc[:message_id], - "references" => doc[:refs].split(/\s+/).map { |x| "<#{x}>" }.join(" "), + "references" => doc[:refs].split.map { |x| "<#{x}>" }.join(" "), } - Message.new :source => source, :source_info => doc[:source_info].to_i, + m = Message.new :source => source, :source_info => doc[:source_info].to_i, :labels => doc[:label].symbolistize, - :snippet => doc[:snippet], :header => fake_header + :snippet => doc[:snippet] + m.parse_header fake_header + m end end @@ -483,10 +483,27 @@ EOS @index_mutex.synchronize { @index.search(q, :limit => 1).total_hits > 0 } end + ## takes a user query string and returns the list of docids for messages + ## that match the query. + ## + ## messages can then be loaded from the index with #build_message. + ## + ## raises a ParseError if the parsing failed. + def run_query query + qobj, opts = Redwood::Index.parse_user_query_string query + query = Redwood::Index.build_query opts.merge(:qobj => qobj) + results = @index.search query, :limit => (opts[:limit] || :all) + results.hits.map { |hit| hit.doc } + end + protected - ## do any specialized parsing - ## returns nil and flashes error message if parsing failed + class ParseError < StandardError; end + + ## parse a query string from the user. returns a query object and a set of + ## extra flags; both of these are meant to be passed to #build_query. + ## + ## raises a ParseError if something went wrong. def parse_user_query_string s extraopts = {} @@ -548,11 +565,9 @@ protected end if $have_chronic - chronic_failure = false subs = subs.gsub(/\b(before|on|in|during|after):(\((.+?)\)\B|(\S+)\b)/) do - break if chronic_failure field, datestr = $1, ($3 || $4) - realdate = Chronic.parse(datestr, :guess => false, :context => :none) + realdate = Chronic.parse datestr, :guess => false, :context => :past if realdate case field when "after" @@ -566,11 +581,9 @@ protected "date:(<= #{sprintf "%012d", realdate.end.to_i}) date:(>= #{sprintf "%012d", realdate.begin.to_i})" end else - BufferManager.flash "Can't understand date #{datestr.inspect}!" - chronic_failure = true + raise ParseError, "can't understand date #{datestr.inspect}" end end - subs = nil if chronic_failure end ## limit:42 restrict the search to 42 results @@ -580,15 +593,14 @@ protected extraopts[:limit] = lim.to_i '' else - BufferManager.flash "Can't understand limit #{lim.inspect}!" - subs = nil + raise ParseError, "non-numeric limit #{lim.inspect}" end end - if subs + begin [@qparser.parse(subs), extraopts] - else - nil + rescue Ferret::QueryParser::QueryParseException => e + raise ParseError, e.message end end