From cd59be3a007822637a8b48efe988fb052d58a756 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 13 Jul 2022 14:21:14 +0200 Subject: Implement opportunistic sync based on watch value, and use it --- src/mail/mailbox.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'src/mail/mailbox.rs') diff --git a/src/mail/mailbox.rs b/src/mail/mailbox.rs index 4b84cf2..19a95e0 100644 --- a/src/mail/mailbox.rs +++ b/src/mail/mailbox.rs @@ -60,8 +60,14 @@ impl Mailbox { } /// Sync data with backing store - pub async fn sync(&self) -> Result<()> { - self.mbox.write().await.sync().await + pub async fn force_sync(&self) -> Result<()> { + self.mbox.write().await.force_sync().await + } + + /// Sync data with backing store only if changes are detected + /// or last sync is too old + pub async fn opportunistic_sync(&self) -> Result<()> { + self.mbox.write().await.opportunistic_sync().await } // ---- Functions for reading the mailbox ---- @@ -184,11 +190,16 @@ struct MailboxInternal { } impl MailboxInternal { - async fn sync(&mut self) -> Result<()> { + async fn force_sync(&mut self) -> Result<()> { self.uid_index.sync().await?; Ok(()) } + async fn opportunistic_sync(&mut self) -> Result<()> { + self.uid_index.opportunistic_sync().await?; + Ok(()) + } + // ---- Functions for reading the mailbox ---- async fn fetch_meta(&self, ids: &[UniqueIdent]) -> Result> { @@ -308,7 +319,8 @@ impl MailboxInternal { .insert_item(&self.mail_path, &ident.to_string(), meta_blob, None) .await?; Ok::<_, anyhow::Error>(()) - } + }, + self.uid_index.opportunistic_sync() )?; // Add mail to Bayou mail index @@ -357,7 +369,8 @@ impl MailboxInternal { .insert_item(&self.mail_path, &ident.to_string(), meta_blob, None) .await?; Ok::<_, anyhow::Error>(()) - } + }, + self.uid_index.opportunistic_sync() )?; // Add mail to Bayou mail index @@ -449,6 +462,7 @@ impl MailboxInternal { .await?; Ok::<_, anyhow::Error>(()) }, + self.uid_index.opportunistic_sync(), )?; // Add mail to Bayou mail index -- cgit v1.2.3