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

master
Manos Pitsidianakis 2020-07-20 15:25:35 +03:00
parent 9103d05617
commit fadb3634e0
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
8 changed files with 26 additions and 41 deletions

View File

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

View File

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

View File

@ -212,13 +212,12 @@ impl MailBackend for JmapType {
self.online.lock().unwrap().1.clone() 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 mut w = AsyncBuilder::new();
let mailboxes = self.mailboxes.clone(); let mailboxes = self.mailboxes.clone();
let store = self.store.clone(); let store = self.store.clone();
let tag_index = self.tag_index.clone(); let tag_index = self.tag_index.clone();
let connection = self.connection.clone(); let connection = self.connection.clone();
let mailbox_hash = mailbox.hash();
let handle = { let handle = {
let tx = w.tx(); let tx = w.tx();
let closure = move |_work_context| { let closure = move |_work_context| {

View File

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

View File

@ -718,11 +718,10 @@ impl MailBackend for MboxType {
Ok(()) 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 mut w = AsyncBuilder::new();
let handle = { let handle = {
let tx = w.tx(); let tx = w.tx();
let mailbox_hash = mailbox.hash();
let mailbox_index = self.mailbox_index.clone(); let mailbox_index = self.mailbox_index.clone();
let mailboxes = self.mailboxes.clone(); let mailboxes = self.mailboxes.clone();
let mailbox_path = mailboxes.lock().unwrap()[&mailbox_hash].fs_path.clone(); let mailbox_path = mailboxes.lock().unwrap()[&mailbox_hash].fs_path.clone();

View File

@ -334,9 +334,8 @@ impl MailBackend for NotmuchDb {
Ok(()) 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 mut w = AsyncBuilder::new();
let mailbox_hash = mailbox.hash();
let database = NotmuchDb::new_connection(self.path.as_path(), self.lib.clone(), false); let database = NotmuchDb::new_connection(self.path.as_path(), self.lib.clone(), false);
let index = self.index.clone(); let index = self.index.clone();
let mailbox_index = self.mailbox_index.clone(); let mailbox_index = self.mailbox_index.clone();

View File

@ -531,7 +531,7 @@ impl Account {
if entry.conf.mailbox_conf.autoload { if entry.conf.mailbox_conf.autoload {
entry.status = MailboxStatus::Parsing(0, 0); entry.status = MailboxStatus::Parsing(0, 0);
if self.is_async { 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 mailbox_job = mailbox_job.into_future();
let (rcvr, handle, job_id) = let (rcvr, handle, job_id) =
self.job_executor.spawn_specialized(mailbox_job); self.job_executor.spawn_specialized(mailbox_job);
@ -545,7 +545,7 @@ impl Account {
} }
} else { } else {
entry.worker = match Account::new_worker( entry.worker = match Account::new_worker(
f.clone(), &f,
&mut self.backend, &mut self.backend,
&self.work_context, &self.work_context,
self.notify_fn.clone(), self.notify_fn.clone(),
@ -571,15 +571,15 @@ impl Account {
} }
fn new_worker( fn new_worker(
mailbox: Mailbox, mailbox: &Mailbox,
backend: &Arc<RwLock<Box<dyn MailBackend>>>, backend: &Arc<RwLock<Box<dyn MailBackend>>>,
work_context: &WorkContext, work_context: &WorkContext,
notify_fn: Arc<NotifyFn>, notify_fn: Arc<NotifyFn>,
) -> Result<Worker> { ) -> 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 mut builder = AsyncBuilder::new();
let our_tx = builder.tx(); let our_tx = builder.tx();
let mailbox_hash = mailbox.hash();
let priority = match mailbox.special_usage() { let priority = match mailbox.special_usage() {
SpecialUsageMailbox::Inbox => 0, SpecialUsageMailbox::Inbox => 0,
SpecialUsageMailbox::Sent => 1, 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 * 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. * 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); builder.set_priority(priority).set_is_static(true);
let mut w = builder.build(Box::new(move |work_context| { let mut w = builder.build(Box::new(move |work_context| {
let name = format!("Parsing {}", mailbox.path());
let work = mailbox_handle.work().unwrap(); let work = mailbox_handle.work().unwrap();
work_context.new_work.send(work).unwrap(); work_context.new_work.send(work).unwrap();
let thread_id = std::thread::current().id(); let thread_id = std::thread::current().id();
@ -821,7 +821,7 @@ impl Account {
} }
RefreshEventKind::Rescan => { RefreshEventKind::Rescan => {
let handle = match Account::new_worker( let handle = match Account::new_worker(
self.mailbox_entries[&mailbox_hash].ref_mailbox.clone(), &self.mailbox_entries[&mailbox_hash].ref_mailbox,
&mut self.backend, &mut self.backend,
&self.work_context, &self.work_context,
self.notify_fn.clone(), self.notify_fn.clone(),
@ -1013,9 +1013,9 @@ impl Account {
MailboxStatus::None => { MailboxStatus::None => {
if self.is_async { if self.is_async {
if !self.active_jobs.values().any(|j| j.is_fetch(mailbox_hash)) { if !self.active_jobs.values().any(|j| j.is_fetch(mailbox_hash)) {
match self.backend.write().unwrap().fetch_async( let mailbox_job =
&&self.mailbox_entries[&mailbox_hash].ref_mailbox, self.backend.write().unwrap().fetch_async(mailbox_hash);
) { match mailbox_job {
Ok(mailbox_job) => { Ok(mailbox_job) => {
let mailbox_job = mailbox_job.into_future(); let mailbox_job = mailbox_job.into_future();
let (rcvr, handle, job_id) = let (rcvr, handle, job_id) =
@ -1046,7 +1046,7 @@ impl Account {
} }
} else if self.mailbox_entries[&mailbox_hash].worker.is_none() { } else if self.mailbox_entries[&mailbox_hash].worker.is_none() {
let handle = match Account::new_worker( let handle = match Account::new_worker(
self.mailbox_entries[&mailbox_hash].ref_mailbox.clone(), &self.mailbox_entries[&mailbox_hash].ref_mailbox,
&mut self.backend, &mut self.backend,
&self.work_context, &self.work_context,
self.notify_fn.clone(), self.notify_fn.clone(),
@ -1396,7 +1396,7 @@ impl Account {
}); });
} }
let (status, worker) = match Account::new_worker( let (status, worker) = match Account::new_worker(
mailboxes[&mailbox_hash].clone(), &mailboxes[&mailbox_hash],
&mut self.backend, &mut self.backend,
&self.work_context, &self.work_context,
self.notify_fn.clone(), self.notify_fn.clone(),

View File

@ -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 mut w = AsyncBuilder::new();
let _mailbox_hash = mailbox.hash();
let channel = self.channel.clone(); let channel = self.channel.clone();
let handle = { let handle = {
let tx = w.tx(); let tx = w.tx();