Browse Source

melib: take MailboxHash instead of &Mailbox in fetch*()

async
Manos Pitsidianakis 2 years ago
parent
commit
fadb3634e0
Signed by: epilys GPG Key ID: 73627C2F690DF710
  1. 4
      melib/src/backends.rs
  2. 5
      melib/src/backends/imap.rs
  3. 3
      melib/src/backends/jmap.rs
  4. 22
      melib/src/backends/maildir/backend.rs
  5. 3
      melib/src/backends/mbox.rs
  6. 3
      melib/src/backends/notmuch.rs
  7. 24
      src/conf/accounts.rs
  8. 3
      src/plugins/backend.rs

4
melib/src/backends.rs

@ -294,10 +294,10 @@ pub trait MailBackend: ::std::fmt::Debug + Send + Sync {
fn is_online_async(&self) -> ResultFuture<()> {
Err(MeliError::new("Unimplemented."))
}
fn fetch(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>>;
fn fetch(&mut self, mailbox_hash: MailboxHash) -> Result<Async<Result<Vec<Envelope>>>>;
fn fetch_async(
&mut self,
_mailbox: &Mailbox,
_mailbox_hash: MailboxHash,
) -> Result<Pin<Box<dyn Stream<Item = Result<Vec<Envelope>>> + Send + 'static>>> {
Err(MeliError::new("Unimplemented."))
}

5
melib/src/backends/imap.rs

@ -190,11 +190,10 @@ impl MailBackend for ImapType {
fn fetch_async(
&mut self,
mailbox: &Mailbox,
mailbox_hash: MailboxHash,
) -> Result<Pin<Box<dyn Stream<Item = Result<Vec<Envelope>>> + Send + 'static>>> {
let uid_store = self.uid_store.clone();
let can_create_flags = self.can_create_flags.clone();
let mailbox_hash = mailbox.hash();
let connection = self.connection.clone();
let mut max_uid: Option<usize> = None;
let mut valid_hash_set: HashSet<EnvelopeHash> = HashSet::default();
@ -297,7 +296,7 @@ impl MailBackend for ImapType {
}))
}
fn fetch(&mut self, _mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
fn fetch(&mut self, _mailbox_hash: MailboxHash) -> Result<Async<Result<Vec<Envelope>>>> {
Err(MeliError::new("Unimplemented."))
}

3
melib/src/backends/jmap.rs

@ -212,13 +212,12 @@ impl MailBackend for JmapType {
self.online.lock().unwrap().1.clone()
}
fn fetch(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
fn fetch(&mut self, mailbox_hash: MailboxHash) -> Result<Async<Result<Vec<Envelope>>>> {
let mut w = AsyncBuilder::new();
let mailboxes = self.mailboxes.clone();
let store = self.store.clone();
let tag_index = self.tag_index.clone();
let connection = self.connection.clone();
let mailbox_hash = mailbox.hash();
let handle = {
let tx = w.tx();
let closure = move |_work_context| {

22
melib/src/backends/maildir/backend.rs

@ -207,17 +207,16 @@ impl MailBackend for MaildirType {
Ok(Box::pin(async { res }))
}
fn fetch(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
Ok(self.multicore(4, mailbox))
fn fetch(&mut self, mailbox_hash: MailboxHash) -> Result<Async<Result<Vec<Envelope>>>> {
Ok(self.multicore(4, mailbox_hash))
}
fn fetch_async(
&mut self,
mailbox: &Mailbox,
mailbox_hash: MailboxHash,
) -> Result<core::pin::Pin<Box<dyn Stream<Item = Result<Vec<Envelope>>> + Send + 'static>>>
{
let mailbox: &MaildirMailbox = &self.mailboxes[&self.owned_mailbox_idx(mailbox)];
let mailbox_hash = mailbox.hash();
let mailbox: &MaildirMailbox = &self.mailboxes[&mailbox_hash];
let unseen = mailbox.unseen.clone();
let total = mailbox.total.clone();
let path: PathBuf = mailbox.fs_path().into();
@ -928,23 +927,14 @@ impl MaildirType {
path: root_path,
}))
}
fn owned_mailbox_idx(&self, mailbox: &Mailbox) -> MailboxHash {
*self
.mailboxes
.iter()
.find(|(_, f)| f.hash() == mailbox.hash())
.unwrap()
.0
}
pub fn multicore(&mut self, cores: usize, mailbox: &Mailbox) -> Async<Result<Vec<Envelope>>> {
pub fn multicore(&mut self, cores: usize, mailbox_hash: MailboxHash) -> Async<Result<Vec<Envelope>>> {
let mut w = AsyncBuilder::new();
let cache_dir = xdg::BaseDirectories::with_profile("meli", &self.name).unwrap();
let handle = {
let tx = w.tx();
let mailbox: &MaildirMailbox = &self.mailboxes[&self.owned_mailbox_idx(mailbox)];
let mailbox_hash = mailbox.hash();
let mailbox: &MaildirMailbox = &self.mailboxes[&mailbox_hash];
let unseen = mailbox.unseen.clone();
let total = mailbox.total.clone();
let tx_final = w.tx();

3
melib/src/backends/mbox.rs

@ -718,11 +718,10 @@ impl MailBackend for MboxType {
Ok(())
}
fn fetch(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
fn fetch(&mut self, mailbox_hash: MailboxHash) -> Result<Async<Result<Vec<Envelope>>>> {
let mut w = AsyncBuilder::new();
let handle = {
let tx = w.tx();
let mailbox_hash = mailbox.hash();
let mailbox_index = self.mailbox_index.clone();
let mailboxes = self.mailboxes.clone();
let mailbox_path = mailboxes.lock().unwrap()[&mailbox_hash].fs_path.clone();

3
melib/src/backends/notmuch.rs

@ -334,9 +334,8 @@ impl MailBackend for NotmuchDb {
Ok(())
}
fn fetch(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
fn fetch(&mut self, mailbox_hash: MailboxHash) -> Result<Async<Result<Vec<Envelope>>>> {
let mut w = AsyncBuilder::new();
let mailbox_hash = mailbox.hash();
let database = NotmuchDb::new_connection(self.path.as_path(), self.lib.clone(), false);
let index = self.index.clone();
let mailbox_index = self.mailbox_index.clone();

24
src/conf/accounts.rs

@ -531,7 +531,7 @@ impl Account {
if entry.conf.mailbox_conf.autoload {
entry.status = MailboxStatus::Parsing(0, 0);
if self.is_async {
if let Ok(mailbox_job) = self.backend.write().unwrap().fetch_async(&f) {
if let Ok(mailbox_job) = self.backend.write().unwrap().fetch_async(*h) {
let mailbox_job = mailbox_job.into_future();
let (rcvr, handle, job_id) =
self.job_executor.spawn_specialized(mailbox_job);
@ -545,7 +545,7 @@ impl Account {
}
} else {
entry.worker = match Account::new_worker(
f.clone(),
&f,
&mut self.backend,
&self.work_context,
self.notify_fn.clone(),
@ -571,15 +571,15 @@ impl Account {
}
fn new_worker(
mailbox: Mailbox,
mailbox: &Mailbox,
backend: &Arc<RwLock<Box<dyn MailBackend>>>,
work_context: &WorkContext,
notify_fn: Arc<NotifyFn>,
) -> Result<Worker> {
let mut mailbox_handle = backend.write().unwrap().fetch(&mailbox)?;
let mailbox_hash = mailbox.hash();
let mut mailbox_handle = backend.write().unwrap().fetch(mailbox_hash)?;
let mut builder = AsyncBuilder::new();
let our_tx = builder.tx();
let mailbox_hash = mailbox.hash();
let priority = match mailbox.special_usage() {
SpecialUsageMailbox::Inbox => 0,
SpecialUsageMailbox::Sent => 1,
@ -601,9 +601,9 @@ impl Account {
* workers causing no actual parsing to be done. If we could yield from within the worker
* threads' closures this could be avoided, but it requires green threads.
*/
let name = format!("Parsing {}", mailbox.path());
builder.set_priority(priority).set_is_static(true);
let mut w = builder.build(Box::new(move |work_context| {
let name = format!("Parsing {}", mailbox.path());
let work = mailbox_handle.work().unwrap();
work_context.new_work.send(work).unwrap();
let thread_id = std::thread::current().id();
@ -821,7 +821,7 @@ impl Account {
}
RefreshEventKind::Rescan => {
let handle = match Account::new_worker(
self.mailbox_entries[&mailbox_hash].ref_mailbox.clone(),
&self.mailbox_entries[&mailbox_hash].ref_mailbox,
&mut self.backend,
&self.work_context,
self.notify_fn.clone(),
@ -1013,9 +1013,9 @@ impl Account {
MailboxStatus::None => {
if self.is_async {
if !self.active_jobs.values().any(|j| j.is_fetch(mailbox_hash)) {
match self.backend.write().unwrap().fetch_async(
&&self.mailbox_entries[&mailbox_hash].ref_mailbox,
) {
let mailbox_job =
self.backend.write().unwrap().fetch_async(mailbox_hash);
match mailbox_job {
Ok(mailbox_job) => {
let mailbox_job = mailbox_job.into_future();
let (rcvr, handle, job_id) =
@ -1046,7 +1046,7 @@ impl Account {
}
} else if self.mailbox_entries[&mailbox_hash].worker.is_none() {
let handle = match Account::new_worker(
self.mailbox_entries[&mailbox_hash].ref_mailbox.clone(),
&self.mailbox_entries[&mailbox_hash].ref_mailbox,
&mut self.backend,
&self.work_context,
self.notify_fn.clone(),
@ -1396,7 +1396,7 @@ impl Account {
});
}
let (status, worker) = match Account::new_worker(
mailboxes[&mailbox_hash].clone(),
&mailboxes[&mailbox_hash],
&mut self.backend,
&self.work_context,
self.notify_fn.clone(),

3
src/plugins/backend.rs

@ -98,9 +98,8 @@ impl MailBackend for PluginBackend {
}
}
fn fetch(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
fn fetch(&mut self, mailbox_hash: MailboxHash) -> Result<Async<Result<Vec<Envelope>>>> {
let mut w = AsyncBuilder::new();
let _mailbox_hash = mailbox.hash();
let channel = self.channel.clone();
let handle = {
let tx = w.tx();

Loading…
Cancel
Save