lib/sup/index.rb in sup-0.0.7 vs lib/sup/index.rb in sup-0.0.8
- old
+ new
@@ -28,10 +28,11 @@
load_sources
load_index
end
def save
+ Redwood::log "saving index and sources..."
FileUtils.mkdir_p @dir unless File.exists? @dir
save_sources
save_index
end
@@ -70,29 +71,50 @@
field_infos.create_index dir
@index = Ferret::Index::Index.new(:path => dir, :analyzer => @analyzer)
end
end
- ## Update the message state on disk, by deleting and re-adding it.
- ## The message must exist in the index. docid and entry are found
- ## unless given.
+ ## Syncs the message to the index: deleting if it's already there,
+ ## and adding either way. Index state will be determined by m.labels.
##
- ## Overwrites the labels on disk with the new labels in 'm', so that
- ## we can actually change message state.
- def update_message m, docid=nil, entry=nil
- unless docid && entry
- docid, entry = load_entry_for_id m.id
- raise ArgumentError, "cannot find #{m.id} in the index" unless entry
- end
+ ## 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 entry and no source info for message #{m.id}" unless m.source && m.source_info
+ 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 "deleting non-corresponding entry #{docid}" unless @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
- @index.delete docid
- add_message m
+ to = (m.to + m.cc + m.bcc).map { |x| x.email }.join(" ")
+ d = {
+ :message_id => m.id,
+ :source_id => source_id,
+ :source_info => m.source_info,
+ :date => m.date.to_indexable_s,
+ :body => m.content,
+ :snippet => m.snippet,
+ :label => m.labels.join(" "),
+ :from => m.from ? m.from.email : "",
+ :to => (m.to + m.cc + m.bcc).map { |x| x.email }.join(" "),
+ :subject => wrap_subj(Message.normalize_subj(m.subj)),
+ :refs => (m.refs + m.replytos).uniq.join(" "),
+ }
+
+ @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
+ true
end
def save_index fn=File.join(@dir, "ferret")
# don't have to do anything, apparently
end
@@ -208,45 +230,10 @@
def fresh_thread_id; @next_thread_id += 1; end
def wrap_subj subj; "__START_SUBJECT__ #{subj} __END_SUBJECT__"; end
def unwrap_subj subj; subj =~ /__START_SUBJECT__ (.*?) __END_SUBJECT__/ && $1; end
- ## Adds a message to the index. The message cannot already exist in
- ## the index.
- def add_message m
- raise ArgumentError, "index already contains #{m.id}" if contains? m
-
- 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
-
- to = (m.to + m.cc + m.bcc).map { |x| x.email }.join(" ")
- d = {
- :message_id => m.id,
- :source_id => source_id,
- :source_info => m.source_info,
- :date => m.date.to_indexable_s,
- :body => m.content,
- :snippet => m.snippet,
- :label => m.labels.join(" "),
- :from => m.from ? m.from.email : "",
- :to => (m.to + m.cc + m.bcc).map { |x| x.email }.join(" "),
- :subject => wrap_subj(Message.normalize_subj(m.subj)),
- :refs => (m.refs + m.replytos).uniq.join(" "),
- }
-
- @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
- true
- end
-
def drop_entry docno; @index.delete docno; end
def load_entry_for_id mid
results = @index.search(Ferret::Search::TermQuery.new(:message_id, mid))
return if results.total_hits == 0
@@ -283,10 +270,16 @@
end
contacts.keys.compact
end
+ def load_sources fn=Redwood::SOURCE_FN
+ 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
+
protected
def parse_user_query_string str; @qparser.parse str; end
def build_query opts
query = Ferret::Search::BooleanQuery.new
@@ -304,14 +297,9 @@
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
- end
-
- def load_sources fn=Redwood::SOURCE_FN
- @sources = Hash[*(Redwood::load_yaml_obj(fn) || []).map { |s| [s.id, s] }.flatten]
- @sources_dirty = false
end
def save_sources fn=Redwood::SOURCE_FN
if @sources_dirty || @sources.any? { |id, s| s.dirty? }
bakfn = fn + ".bak"