From 218ee900ae4d16e3c46a9a5a0d81b2cbca056afe Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Sun, 19 Dec 2010 01:52:43 +0000 Subject: [PATCH] Execute LocalMessage.appendMessage() & LocalMessage.setFlag() in the same transaction for small message storing in order to speed up DB update. This is a per message basis optimization. More improved speed could be attained by batching several messages in the same transaction. --- .../k9/controller/MessagingController.java | 15 +++++---- src/com/fsck/k9/mail/store/LocalStore.java | 33 +++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/com/fsck/k9/controller/MessagingController.java b/src/com/fsck/k9/controller/MessagingController.java index bdba6646f..38c01e2b6 100644 --- a/src/com/fsck/k9/controller/MessagingController.java +++ b/src/com/fsck/k9/controller/MessagingController.java @@ -1783,13 +1783,14 @@ public class MessagingController implements Runnable } // Store the updated message locally - localFolder.appendMessages(new Message[] { message }); - - Message localMessage = localFolder.getMessage(message.getUid()); - progress.incrementAndGet(); - - // Set a flag indicating this message has now be fully downloaded - localMessage.setFlag(Flag.X_DOWNLOADED_FULL, true); + final Message localMessage = localFolder.storeSmallMessage(message, new Runnable() + { + @Override + public void run() + { + progress.incrementAndGet(); + } + }); if (K9.DEBUG) Log.v(K9.LOG_TAG, "About to notify listeners that we got a new small message " + account + ":" + folder + ":" + message.getUid()); diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index 23dfcfff4..b9d2f65e5 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -2842,6 +2842,39 @@ public class LocalStore extends Store implements Serializable, LocalStoreMigrati } + /** + * Convenience transaction wrapper for storing a message and set it as fully downloaded. Implemented mainly to speed up DB transaction commit. + * + * @param message Message to store. Never null. + * @param runnable What to do before setting {@link Flag#X_DOWNLOADED_FULL}. Never null. + * @return The local version of the message. Never null. + * @throws MessagingException + */ + public Message storeSmallMessage(final Message message, final Runnable runnable) throws MessagingException + { + return execute(true, new DbCallback() + { + @Override + public Message doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException + { + try + { + appendMessages(new Message[] { message }); + final String uid = message.getUid(); + final Message result = getMessage(uid); + runnable.run(); + // Set a flag indicating this message has now be fully downloaded + result.setFlag(Flag.X_DOWNLOADED_FULL, true); + return result; + } + catch (MessagingException e) + { + throw new WrappedException(e); + } + } + }); + } + /** * The method differs slightly from the contract; If an incoming message already has a uid * assigned and it matches the uid of an existing message then this message will replace the