diff --git a/ui/src/sqlite3.rs b/ui/src/sqlite3.rs index 17d37ff55..526c4c517 100644 --- a/ui/src/sqlite3.rs +++ b/ui/src/sqlite3.rs @@ -269,32 +269,30 @@ pub fn remove(env_hash: EnvelopeHash) -> Result<()> { Ok(()) } -pub fn index(context: &mut crate::state::Context) -> Result<()> { +pub fn index(context: &mut crate::state::Context, account_name: &str) -> Result<()> { + let account = if let Some(a) = context.accounts.iter().find(|acc| acc.name() == account_name) { + a} else { + return Err(MeliError::new(format!("Account {} was not found.", account_name))); + }; + + let (acc_name, acc_mutex, backend_mutex):( String, Arc>, Arc<_>) = if *account.settings.conf.cache_type() != crate::conf::CacheType::Sqlite3 { + return Err(MeliError::new(format!("Account {} doesn't have an sqlite3 search backend.", account_name))); + } else { + ( + account.name().to_string(), + account.collection.envelopes.clone(), + account.backend.clone(), + )}; let conn = open_or_create_db()?; let work_context = context.work_controller().get_context(); - let mutexes = context - .accounts - .iter() - .filter(|acc| *acc.settings.conf.cache_type() == crate::conf::CacheType::Sqlite3) - .map(|acc| { - ( - acc.name().to_string(), - acc.collection.envelopes.clone(), - acc.backend.clone(), - ) - }) - .collect::>, Arc<_>)>>(); - let env_hashes = mutexes - .iter() - .map(|m| m.1.read().unwrap().keys().cloned().collect::>()) - .collect::>>(); + let env_hashes = + acc_mutex.read().unwrap().keys().cloned().collect::>(); /* Sleep, index and repeat in order not to block the main process */ let handle = std::thread::Builder::new().name(String::from("rebuilding index")).spawn(move || { let thread_id = std::thread::current().id(); let sleep_dur = std::time::Duration::from_millis(20); - for ((acc_name, acc_mutex, backend_mutex), env_hashes) in mutexes.into_iter().zip(env_hashes.into_iter()) { if let Err(err) = conn.execute( "INSERT OR REPLACE INTO accounts (name) VALUES (?1)", params![acc_name.as_str(),],).map_err(|e| MeliError::new(e.to_string())) { debug!("{}", @@ -377,7 +375,6 @@ pub fn index(context: &mut crate::state::Context) -> Result<()> { .send((thread_id, format!("Rebuilding {} index. {}/{}", acc_name, ctr, env_hashes.len()))) .unwrap(); std::thread::sleep(sleep_dur); - } } work_context.finished.send(thread_id).unwrap(); })?; diff --git a/ui/src/state.rs b/ui/src/state.rs index ab5f84dd4..d1237faf1 100644 --- a/ui/src/state.rs +++ b/ui/src/state.rs @@ -584,22 +584,24 @@ impl State { )); } } - AccountAction(_, ReIndex) => match crate::sqlite3::index(&mut self.context) { - Ok(()) => { - self.context.replies.push_back(UIEvent::Notification( - None, - "Message index rebuild started.".to_string(), - Some(NotificationType::INFO), - )); + AccountAction(ref account_name, ReIndex) => { + match crate::sqlite3::index(&mut self.context, account_name) { + Ok(()) => { + self.context.replies.push_back(UIEvent::Notification( + None, + "Message index rebuild started.".to_string(), + Some(NotificationType::INFO), + )); + } + Err(e) => { + self.context.replies.push_back(UIEvent::Notification( + None, + format!("Message index rebuild failed: {}.", e), + Some(NotificationType::ERROR), + )); + } } - Err(e) => { - self.context.replies.push_back(UIEvent::Notification( - None, - format!("Message index rebuild failed: {}.", e), - Some(NotificationType::ERROR), - )); - } - }, + } v => { self.rcv_event(UIEvent::Action(v)); }