From f6566fe83c93625b918664526e8858f7be667354 Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Tue, 11 Feb 2014 16:20:26 -0400 Subject: defer appends too and cut some more time by firing the callback as soon as we've got an UID. --- src/leap/mail/imap/mailbox.py | 22 +++++++++++----------- src/leap/mail/imap/memorystore.py | 32 ++++++++++++-------------------- src/leap/mail/imap/messages.py | 19 +++++++++---------- 3 files changed, 32 insertions(+), 41 deletions(-) diff --git a/src/leap/mail/imap/mailbox.py b/src/leap/mail/imap/mailbox.py index 21f0554..7083316 100644 --- a/src/leap/mail/imap/mailbox.py +++ b/src/leap/mail/imap/mailbox.py @@ -111,6 +111,7 @@ class SoledadMailbox(WithMsgFields, MBoxParser): last_uid_lock = threading.Lock() _fdoc_primed = {} + _last_uid_primed = {} def __init__(self, mbox, soledad, memstore, rw=1): """ @@ -294,10 +295,13 @@ class SoledadMailbox(WithMsgFields, MBoxParser): """ Prime memstore with last_uid value """ - mbox = self._get_mbox_doc() - last = mbox.content.get('lastuid', 0) - logger.info("Priming Soledad last_uid to %s" % (last,)) - self._memstore.set_last_soledad_uid(self.mbox, last) + primed = self._last_uid_primed.get(self.mbox, False) + if not primed: + mbox = self._get_mbox_doc() + last = mbox.content.get('lastuid', 0) + logger.info("Priming Soledad last_uid to %s" % (last,)) + self._memstore.set_last_soledad_uid(self.mbox, last) + self._last_uid_primed[self.mbox] = True def prime_known_uids_to_memstore(self): """ @@ -459,6 +463,9 @@ class SoledadMailbox(WithMsgFields, MBoxParser): flags = tuple(str(flag) for flag in flags) d = self._do_add_message(message, flags=flags, date=date) + if PROFILE_CMD: + do_profile_cmd(d, "APPEND") + # XXX should notify here probably return d def _do_add_message(self, message, flags, date): @@ -467,13 +474,6 @@ class SoledadMailbox(WithMsgFields, MBoxParser): Invoked from addMessage. """ d = self.messages.add_msg(message, flags=flags, date=date) - # XXX Removing notify temporarily. - # This is interfering with imaptest results. I'm not clear if it's - # because we clutter the logging or because the set of listeners is - # ever-growing. We should come up with some smart way of dealing with - # it, or maybe just disabling it using an environmental variable since - # we will only have just a few listeners in the regular desktop case. - #d.addCallback(self.notify_new) return d def notify_new(self, *args): diff --git a/src/leap/mail/imap/memorystore.py b/src/leap/mail/imap/memorystore.py index e8e8152..423b891 100644 --- a/src/leap/mail/imap/memorystore.py +++ b/src/leap/mail/imap/memorystore.py @@ -274,30 +274,24 @@ class MemoryStore(object): be fired. :type notify_on_disk: bool """ - from twisted.internet import reactor - log.msg("Adding new doc to memstore %r (%r)" % (mbox, uid)) key = mbox, uid self._add_message(mbox, uid, message, notify_on_disk) self._new.add(key) - # XXX use this while debugging the callback firing, - # remove after unittesting this. - #def log_add(result): - #return result - #observer.addCallback(log_add) - - if notify_on_disk: - # We store this deferred so we can keep track of the pending - # operations internally. - # TODO this should fire with the UID !!! -- change that in - # the soledad store code. - self._new_deferreds[key] = observer - if not notify_on_disk: - # Caller does not care, just fired and forgot, so we pass - # a defer that will inmediately have its callback triggered. - reactor.callLater(0, observer.callback, uid) + if observer is not None: + if notify_on_disk: + # We store this deferred so we can keep track of the pending + # operations internally. + # TODO this should fire with the UID !!! -- change that in + # the soledad store code. + self._new_deferreds[key] = observer + + else: + # Caller does not care, just fired and forgot, so we pass + # a defer that will inmediately have its callback triggered. + self.reactor.callFromThread(observer.callback, uid) def put_message(self, mbox, uid, message, notify_on_disk=True): """ @@ -722,8 +716,6 @@ class MemoryStore(object): headers_dict[uid] = hdoc except KeyError: continue - - import pprint; pprint.pprint(headers_dict) return headers_dict # Counting sheeps... diff --git a/src/leap/mail/imap/messages.py b/src/leap/mail/imap/messages.py index 7884fb0..c133a6d 100644 --- a/src/leap/mail/imap/messages.py +++ b/src/leap/mail/imap/messages.py @@ -879,19 +879,18 @@ class MessageCollection(WithMsgFields, IndexedDB, MailParser, MBoxParser): uid when the adding succeed. :rtype: deferred """ - logger.debug('Adding message') if flags is None: flags = tuple() leap_assert_type(flags, tuple) observer = defer.Deferred() d = self._do_parse(raw) - d.addCallback(self._do_add_msg, flags, subject, date, - notify_on_disk, observer) + d.addCallback(lambda result: self.reactor.callInThread( + self._do_add_msg, result, flags, subject, date, + notify_on_disk, observer)) return observer - # We SHOULD defer the heavy load here) to the thread pool, - # but it gives troubles with the QSocketNotifier used by Qt... + # Called in thread def _do_add_msg(self, parse_result, flags, subject, date, notify_on_disk, observer): """ @@ -912,7 +911,6 @@ class MessageCollection(WithMsgFields, IndexedDB, MailParser, MBoxParser): # TODO add the linked-from info ! # TODO add reference to the original message - from twisted.internet import reactor msg, parts, chash, size, multi = parse_result # check for uniqueness -------------------------------- @@ -922,13 +920,14 @@ class MessageCollection(WithMsgFields, IndexedDB, MailParser, MBoxParser): uid = existing_uid msg = self.get_msg_by_uid(uid) - # TODO this cannot be deferred, this has to block. + # We can say the observer that we're done + self.reactor.callFromThread(observer.callback, uid) msg.setFlags((fields.DELETED_FLAG,), -1) - reactor.callLater(0, observer.callback, uid) return uid = self.memstore.increment_last_soledad_uid(self.mbox) - logger.info("ADDING MSG WITH UID: %s" % uid) + # We can say the observer that we're done + self.reactor.callFromThread(observer.callback, uid) fd = self._populate_flags(flags, uid, chash, size, multi) hd = self._populate_headr(msg, chash, subject, date) @@ -953,7 +952,7 @@ class MessageCollection(WithMsgFields, IndexedDB, MailParser, MBoxParser): msg_container = MessageWrapper(fd, hd, cdocs) self.memstore.create_message( self.mbox, uid, msg_container, - observer=observer, notify_on_disk=notify_on_disk) + observer=None, notify_on_disk=notify_on_disk) # # getters: specific queries -- cgit v1.2.3