[PATCH] Optimise 'Removed' flagging of messages deleted from the IMAP server

Neil Jerram neil at ossau.homelinux.net
Fri Dec 28 12:52:46 CET 2012


On each IMAP server sync, if there are messages that exist on the
phone but have since been deleted from the IMAP server, or moved to a
different folder on the IMAP server, QtMoko adds the
QMailMessage::Removed flag to those messages.  Note that we don't
automatically delete those messages from the phone.

I once - not using QtMoko - moved several thousand older messages to
an "OLD" folder on my IMAP server.  After that I found that each
QtMoko IMAP server sync operation would take a lot of time and CPU,
and discovered this was because it was reflagging all of those moved
messages every time.  If we optimize the process by skipping messages
that already have the 'Removed' flag, it goes massively faster.
---
 src/tools/messageserver/imapclient.cpp |   23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/tools/messageserver/imapclient.cpp b/src/tools/messageserver/imapclient.cpp
index 25536d0..9f10f5b 100644
--- a/src/tools/messageserver/imapclient.cpp
+++ b/src/tools/messageserver/imapclient.cpp
@@ -601,8 +601,15 @@ void ImapClient::searchCompleted()
     QMailAccount account(accountId);
     QStringList readElsewhereUids = account.serverUids(boxId, QMailMessage::ReadElsewhere);
     QStringList unreadElsewhereUids = account.serverUids(boxId, QMailMessage::ReadElsewhere, false);
+
+    // "deleted" here means messages that have been deleted from the
+    // phone (i.e. from the Qtopia database) but may still exist on
+    // the IMAP server.  Code below will delete these from the IMAP
+    // server too.
     QStringList deletedUids = account.deletedMessages(boxId);
 
+    // The following generates a list of all the UIDs that have ever
+    // previously been seen on the phone.
     QStringList storedUids = readElsewhereUids + unreadElsewhereUids + deletedUids;
 
     // New messages reported by the server that we don't yet have
@@ -623,8 +630,20 @@ void ImapClient::searchCompleted()
         // Messages marked read locally that the server reports are unseen
         _readUids = inFirstAndSecond(account.serverUids(boxId, QMailMessage::Read), _unseenUids);
 
-        // Report any messages that are no longer returned by the server
-        foreach (const QString &uid, inFirstButNotSecond(storedUids, reportedUids))
+        // If there are messages that exist on the phone but have
+        // since been deleted from the IMAP server, add the
+        // QMailMessage::Removed flag to those messages.  Note that we
+        // don't automatically delete those messages from the phone.
+	//
+	// Optimize this a bit by skipping phone messages that already
+	// have the QMailMessage::Removed flag.  Otherwise, in a
+	// scenario where a lot of messages are deleted from the IMAP
+	// server - but still exist on the phone - all of those
+	// messages are reprocessed every time we sync with the IMAP
+	// server, and that can take significant time and CPU.
+        foreach (const QString &uid,
+		 inFirstButNotSecond(inFirstButNotSecond(storedUids, reportedUids),
+				     account.serverUids(boxId, QMailMessage::Removed)))
             emit nonexistentMessage(uid, Client::Removed);
 
         // Update any messages that are reported read-elsewhere, that we didn't previously know about
-- 
1.7.10.4


--=-=-=--



More information about the community mailing list