From: rlane@club.cc.cmu.edu (Rich Lane)
Subject: [sup-talk] [PATCH 07/18] move source-related methods to SourceManager
Date: Sat, 20 Jun 2009 13:50:06 -0700 [thread overview]
Message-ID: <1245531017-9907-8-git-send-email-rlane@club.cc.cmu.edu> (raw)
In-Reply-To: <1245531017-9907-7-git-send-email-rlane@club.cc.cmu.edu>
---
bin/sup | 10 ++++----
bin/sup-add | 10 ++++----
bin/sup-config | 14 +++++-----
bin/sup-recover-sources | 7 +++--
bin/sup-sync | 6 ++--
bin/sup-sync-back | 4 +-
bin/sup-tweak-labels | 4 +-
lib/sup.rb | 5 ++-
lib/sup/index.rb | 52 ++----------------------------------------
lib/sup/poll.rb | 2 +-
lib/sup/source.rb | 57 +++++++++++++++++++++++++++++++++++++++++++++++
11 files changed, 92 insertions(+), 79 deletions(-)
diff --git a/bin/sup b/bin/sup
index 302ad7c..1febefd 100755
--- a/bin/sup
+++ b/bin/sup
@@ -160,17 +160,17 @@ begin
Redwood::start
Index.load
- if(s = Index.source_for DraftManager.source_name)
+ if(s = Redwood::SourceManager.source_for DraftManager.source_name)
DraftManager.source = s
else
Redwood::log "no draft source, auto-adding..."
- Index.add_source DraftManager.new_source
+ Redwood::SourceManager.add_source DraftManager.new_source
end
- if(s = Index.source_for SentManager.source_uri)
+ if(s = Redwood::SourceManager.source_for SentManager.source_uri)
SentManager.source = s
else
- Index.add_source SentManager.default_source
+ Redwood::SourceManager.add_source SentManager.default_source
end
HookManager.run "startup"
@@ -190,7 +190,7 @@ begin
bm.draw_screen
- Index.usual_sources.each do |s|
+ Redwood::SourceManager.usual_sources.each do |s|
next unless s.respond_to? :connect
reporting_thread("call #connect on #{s}") do
begin
diff --git a/bin/sup-add b/bin/sup-add
index 50bbb29..c491ca7 100755
--- a/bin/sup-add
+++ b/bin/sup-add
@@ -82,12 +82,12 @@ index = Redwood::Index.new
index.lock_or_die
begin
- index.load_sources
+ Redwood::SourceManager.load_sources
ARGV.each do |uri|
labels = $opts[:labels] ? $opts[:labels].split(/\s*,\s*/).uniq : []
- if !$opts[:force_new] && index.source_for(uri)
+ if !$opts[:force_new] && Redwood::SourceManager.source_for(uri)
say "Already know about #{uri}; skipping."
next
end
@@ -99,10 +99,10 @@ begin
when "mbox+ssh"
say "For SSH connections, if you will use public key authentication, you may leave the username and password blank."
say ""
- username, password = get_login_info uri, index.sources
+ username, password = get_login_info uri, Redwood::SourceManager.sources
Redwood::MBox::SSHLoader.new uri, username, password, nil, !$opts[:unusual], $opts[:archive], nil, labels
when "imap", "imaps"
- username, password = get_login_info uri, index.sources
+ username, password = get_login_info uri, Redwood::SourceManager.sources
Redwood::IMAP.new uri, username, password, nil, !$opts[:unusual], $opts[:archive], nil, labels
when "maildir"
Redwood::Maildir.new uri, nil, !$opts[:unusual], $opts[:archive], nil, labels
@@ -114,7 +114,7 @@ begin
Trollop::die "Unknown source type #{parsed_uri.scheme.inspect}"
end
say "Adding #{source}..."
- index.add_source source
+ Redwood::SourceManager.add_source source
end
ensure
index.save
diff --git a/bin/sup-config b/bin/sup-config
index 398197f..9fcbee6 100755
--- a/bin/sup-config
+++ b/bin/sup-config
@@ -152,7 +152,7 @@ end
$terminal.wrap_at = :auto
Redwood::start
index = Redwood::Index.new
-index.load_sources
+Redwood::SourceManager.load_sources
say <<EOS
Howdy neighbor! This here's sup-config, ready to help you jack in to
@@ -191,12 +191,12 @@ $config[:editor] = editor
done = false
until done
say "\nNow, we'll tell Sup where to find all your email."
- index.load_sources
+ Redwood::SourceManager.load_sources
say "Current sources:"
- if index.sources.empty?
+ if Redwood::SourceManager.sources.empty?
say " No sources!"
else
- index.sources.each { |s| puts "* #{s}" }
+ Redwood::SourceManager.sources.each { |s| puts "* #{s}" }
end
say "\n"
@@ -210,8 +210,8 @@ end
say "\nSup needs to know where to store your sent messages."
say "Only sources capable of storing mail will be listed.\n\n"
-index.load_sources
-if index.sources.empty?
+Redwood::SourceManager.load_sources
+if Redwood::SourceManager.sources.empty?
say "\nUsing the default sup://sent, since you haven't configured other sources yet."
$config[:sent_source] = 'sup://sent'
else
@@ -222,7 +222,7 @@ else
choose do |menu|
menu.prompt = "Store my sent mail in? "
- valid_sents = index.sources.each do |s|
+ valid_sents = Redwood::SourceManager.sources.each do |s|
have_sup_sent = true if s.to_s.eql?('sup://sent')
menu.choice(s.to_s) { $config[:sent_source] = s.to_s } if s.respond_to? :store_message
diff --git a/bin/sup-recover-sources b/bin/sup-recover-sources
index 6e3810c..db75b11 100755
--- a/bin/sup-recover-sources
+++ b/bin/sup-recover-sources
@@ -48,13 +48,14 @@ EOS
end.parse(ARGV)
require "sup"
+Redwood::start
puts "loading index..."
index = Redwood::Index.new
index.load
puts "loaded index of #{index.size} messages"
ARGV.each do |fn|
- next if index.source_for fn
+ next if Redwood::SourceManager.source_for fn
## TODO: merge this code with the same snippet in import
source =
@@ -74,7 +75,7 @@ ARGV.each do |fn|
source.each do |offset, labels|
m = Redwood::Message.new :source => source, :source_info => offset
m.load_from_source!
- source_id = index.source_for_id m.id
+ source_id = Redwood::SourceManager.source_for_id m.id
next unless source_id
source_ids[source_id] += 1
count += 1
@@ -85,7 +86,7 @@ ARGV.each do |fn|
id = source_ids.keys.first.to_i
puts "assigned #{source} to #{source_ids.keys.first}"
source.id = id
- index.add_source source
+ Redwood::SourceManager.add_source source
else
puts ">> unable to determine #{source}: #{source_ids.inspect}"
end
diff --git a/bin/sup-sync b/bin/sup-sync
index 18a3cab..270524a 100755
--- a/bin/sup-sync
+++ b/bin/sup-sync
@@ -116,11 +116,11 @@ begin
index.load
sources = ARGV.map do |uri|
- index.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
+ Redwood::SourceManager.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
end
- sources = index.usual_sources if sources.empty?
- sources = index.sources if opts[:all_sources]
+ sources = Redwood::SourceManager.usual_sources if sources.empty?
+ sources = Redwood::SourceManager.sources if opts[:all_sources]
unless target == :new
if opts[:start_at]
diff --git a/bin/sup-sync-back b/bin/sup-sync-back
index 05b9e8c..679e03a 100755
--- a/bin/sup-sync-back
+++ b/bin/sup-sync-back
@@ -80,13 +80,13 @@ begin
index.load
sources = ARGV.map do |uri|
- s = index.source_for(uri) or die "unknown source: #{uri}. Did you add it with sup-add first?"
+ s = Redwood::SourceManager.source_for(uri) or die "unknown source: #{uri}. Did you add it with sup-add first?"
s.is_a?(Redwood::MBox::Loader) or die "#{uri} is not an mbox source."
s
end
if sources.empty?
- sources = index.usual_sources.select { |s| s.is_a? Redwood::MBox::Loader }
+ sources = Redwood::SourceManager.usual_sources.select { |s| s.is_a? Redwood::MBox::Loader }
end
unless sources.all? { |s| s.file_path.nil? } || File.executable?(dotlockfile) || opts[:dont_use_dotlockfile]
diff --git a/bin/sup-tweak-labels b/bin/sup-tweak-labels
index 6f603e2..95a3b03 100755
--- a/bin/sup-tweak-labels
+++ b/bin/sup-tweak-labels
@@ -66,10 +66,10 @@ begin
source_ids =
if opts[:all_sources]
- index.sources
+ Redwood::SourceManager.sources
else
ARGV.map do |uri|
- index.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
+ Redwood::SourceManager.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
end
end.map { |s| s.id }
Trollop::die "nothing to do: no sources" if source_ids.empty?
diff --git a/lib/sup.rb b/lib/sup.rb
index 8373820..5689c2b 100644
--- a/lib/sup.rb
+++ b/lib/sup.rb
@@ -115,6 +115,7 @@ module Redwood
Redwood::SuicideManager.new Redwood::SUICIDE_FN
Redwood::CryptoManager.new
Redwood::UndoManager.new
+ Redwood::SourceManager.new
end
def finish
@@ -130,7 +131,7 @@ module Redwood
def report_broken_sources opts={}
return unless BufferManager.instantiated?
- broken_sources = Index.sources.select { |s| s.error.is_a? FatalSourceError }
+ broken_sources = SourceManager.sources.select { |s| s.error.is_a? FatalSourceError }
unless broken_sources.empty?
BufferManager.spawn_unless_exists("Broken source notification for #{broken_sources.join(',')}", opts) do
TextMode.new(<<EOM)
@@ -147,7 +148,7 @@ EOM
end
end
- desynced_sources = Index.sources.select { |s| s.error.is_a? OutOfSyncSourceError }
+ desynced_sources = SourceManager.sources.select { |s| s.error.is_a? OutOfSyncSourceError }
unless desynced_sources.empty?
BufferManager.spawn_unless_exists("Out-of-sync source notification for #{broken_sources.join(',')}", opts) do
TextMode.new(<<EOM)
diff --git a/lib/sup/index.rb b/lib/sup/index.rb
index b9f4b36..7d6258d 100644
--- a/lib/sup/index.rb
+++ b/lib/sup/index.rb
@@ -26,11 +26,7 @@ class Index
def initialize dir=BASE_DIR
@index_mutex = Monitor.new
-
@dir = dir
- @sources = {}
- @sources_dirty = false
- @source_mutex = Monitor.new
wsa = Ferret::Analysis::WhiteSpaceAnalyzer.new false
sa = Ferret::Analysis::StandardAnalyzer.new [], true
@@ -112,36 +108,17 @@ EOS
end
def load
- load_sources
+ SourceManager.load_sources
load_index
end
def save
Redwood::log "saving index and sources..."
FileUtils.mkdir_p @dir unless File.exists? @dir
- save_sources
+ SourceManager.save_sources
save_index
end
- def add_source source
- @source_mutex.synchronize do
- raise "duplicate source!" if @sources.include? source
- @sources_dirty = true
- max = @sources.max_of { |id, s| s.is_a?(DraftLoader) || s.is_a?(SentLoader) ? 0 : id }
- source.id ||= (max || 0) + 1
- ##source.id += 1 while @sources.member? source.id
- @sources[source.id] = source
- end
- end
-
- def sources
- ## favour the inbox by listing non-archived sources first
- @source_mutex.synchronize { @sources.values }.sort_by { |s| s.id }.partition { |s| !s.archived? }.flatten
- end
-
- def source_for uri; sources.find { |s| s.is_source_for? uri }; end
- def usual_sources; sources.find_all { |s| s.usual? }; end
-
def load_index dir=File.join(@dir, "ferret")
if File.exists? dir
Redwood::log "loading index..."
@@ -383,7 +360,7 @@ EOS
@index_mutex.synchronize do
doc = @index[docid] or return
- source = @source_mutex.synchronize { @sources[doc[:source_id].to_i] }
+ source = SourceManager[doc[:source_id].to_i]
raise "invalid source #{doc[:source_id]}" unless source
#puts "building message #{doc[:message_id]} (#{source}##{doc[:source_info]})"
@@ -442,14 +419,6 @@ EOS
contacts.keys.compact
end
- def load_sources fn=Redwood::SOURCE_FN
- source_array = (Redwood::load_yaml_obj(fn) || []).map { |o| Recoverable.new o }
- @source_mutex.synchronize do
- @sources = Hash[*(source_array).map { |s| [s.id, s] }.flatten]
- @sources_dirty = false
- end
- end
-
def each_docid query={}
ferret_query = build_ferret_query query
results = @index_mutex.synchronize { @index.search ferret_query, :limit => (query[:limit] || :all) }
@@ -604,21 +573,6 @@ private
q.add_query Ferret::Search::TermQuery.new("source_id", query[:source_id]), :must if query[:source_id]
q
end
-
- def save_sources fn=Redwood::SOURCE_FN
- @source_mutex.synchronize do
- if @sources_dirty || @sources.any? { |id, s| s.dirty? }
- bakfn = fn + ".bak"
- if File.exists? fn
- File.chmod 0600, fn
- FileUtils.mv fn, bakfn, :force => true unless File.exists?(bakfn) && File.size(fn) == 0
- end
- Redwood::save_yaml_obj sources.sort_by { |s| s.id.to_i }, fn, true
- File.chmod 0600, fn
- end
- @sources_dirty = false
- end
- end
end
end
diff --git a/lib/sup/poll.rb b/lib/sup/poll.rb
index bbad5f2..c83290c 100644
--- a/lib/sup/poll.rb
+++ b/lib/sup/poll.rb
@@ -83,7 +83,7 @@ EOS
from_and_subj_inbox = []
@mutex.synchronize do
- Index.usual_sources.each do |source|
+ SourceManager.usual_sources.each do |source|
# yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
begin
yield "Loading from #{source}... " unless source.done? || (source.respond_to?(:has_errors?) && source.has_errors?)
diff --git a/lib/sup/source.rb b/lib/sup/source.rb
index fb98dbc..1bb7797 100644
--- a/lib/sup/source.rb
+++ b/lib/sup/source.rb
@@ -155,4 +155,61 @@ protected
end
end
+class SourceManager
+ include Singleton
+
+ def initialize
+ @sources = {}
+ @sources_dirty = false
+ @source_mutex = Monitor.new
+ self.class.i_am_the_instance self
+ end
+
+ def [](id)
+ @source_mutex.synchronize { @sources[id] }
+ end
+
+ def add_source source
+ @source_mutex.synchronize do
+ raise "duplicate source!" if @sources.include? source
+ @sources_dirty = true
+ max = @sources.max_of { |id, s| s.is_a?(DraftLoader) || s.is_a?(SentLoader) ? 0 : id }
+ source.id ||= (max || 0) + 1
+ ##source.id += 1 while @sources.member? source.id
+ @sources[source.id] = source
+ end
+ end
+
+ def sources
+ ## favour the inbox by listing non-archived sources first
+ @source_mutex.synchronize { @sources.values }.sort_by { |s| s.id }.partition { |s| !s.archived? }.flatten
+ end
+
+ def source_for uri; sources.find { |s| s.is_source_for? uri }; end
+ def usual_sources; sources.find_all { |s| s.usual? }; end
+
+ def load_sources fn=Redwood::SOURCE_FN
+ source_array = (Redwood::load_yaml_obj(fn) || []).map { |o| Recoverable.new o }
+ @source_mutex.synchronize do
+ @sources = Hash[*(source_array).map { |s| [s.id, s] }.flatten]
+ @sources_dirty = false
+ end
+ end
+
+ def save_sources fn=Redwood::SOURCE_FN
+ @source_mutex.synchronize do
+ if @sources_dirty || @sources.any? { |id, s| s.dirty? }
+ bakfn = fn + ".bak"
+ if File.exists? fn
+ File.chmod 0600, fn
+ FileUtils.mv fn, bakfn, :force => true unless File.exists?(bakfn) && File.size(fn) == 0
+ end
+ Redwood::save_yaml_obj sources.sort_by { |s| s.id.to_i }, fn, true
+ File.chmod 0600, fn
+ end
+ @sources_dirty = false
+ end
+ end
+end
+
end
--
1.6.0.4
next prev parent reply other threads:[~2009-06-20 20:50 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-06-20 20:49 [sup-talk] [PATCH 0/18] Xapian-based index Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 01/18] remove load_entry_for_id call in sup-recover-sources Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 02/18] remove load_entry_for_id call in DraftManager.discard Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 03/18] remove ferret entry from poll/sync interface Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 04/18] index: remove unused method load_entry_for_id Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 05/18] switch DraftManager to use Message.build_from_source Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 06/18] index: move has_any_from_source_with_label? to sup-sync-back Rich Lane
2009-06-20 20:50 ` Rich Lane [this message]
2009-06-20 20:50 ` [sup-talk] [PATCH 08/18] index: remove unused method fresh_thread_id Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 09/18] index: revert overeager opts->query rename in each_message_in_thread_for Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 10/18] index: make wrap_subj methods private Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 11/18] index: move Ferret-specific code to ferret_index.rb Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 12/18] remove last external uses of ferret docid Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 13/18] add Message.indexable_{body, chunks, subject} Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 14/18] index: choose index implementation with config entry or environment variable Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 15/18] index: add xapian implementation Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 16/18] fix String#ord monkeypatch Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 17/18] add limit argument to author_names_and_newness_for_thread Rich Lane
2009-06-20 20:50 ` [sup-talk] [PATCH 18/18] dont using SavingHash#[] for membership test Rich Lane
2009-06-22 14:46 ` Andrei Thorp
2009-06-24 16:30 ` [sup-talk] [PATCH 0/18] Xapian-based index William Morgan
2009-06-24 17:33 ` William Morgan
2009-06-26 2:00 ` Olly Betts
2009-06-26 13:49 ` William Morgan
2009-07-17 23:42 ` Richard Heycock
2009-07-23 10:23 ` Adeodato Simó
2009-07-25 4:53 ` Rich Lane
2009-07-25 9:21 ` Adeodato Simó
2009-07-25 19:59 ` Rich Lane
2009-07-25 23:28 ` Ingmar Vanhassel
2009-07-27 15:48 ` William Morgan
2009-07-27 16:56 ` Ingmar Vanhassel
2009-09-01 8:07 ` Ingmar Vanhassel
2009-09-03 16:52 ` Rich Lane
2009-07-27 17:06 ` Rich Lane
2009-07-31 16:20 ` Rich Lane
2009-08-12 13:05 ` Ingmar Vanhassel
2009-08-12 14:32 ` Nicolas Pouillard
2009-08-14 5:23 ` Rich Lane
2009-07-27 15:46 ` William Morgan
2009-07-28 16:53 ` Olly Betts
2009-07-28 17:01 ` William Morgan
2009-07-28 13:47 ` Olly Betts
2009-07-28 15:07 ` William Morgan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1245531017-9907-8-git-send-email-rlane@club.cc.cmu.edu \
--to=rlane@club.cc.cmu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox