summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKali Kaneko <kali@leap.se>2015-03-05 12:18:11 -0400
committerKali Kaneko <kali@leap.se>2015-03-05 15:18:19 -0400
commit8603664204f9d760c1e332e70686e4dd63108091 (patch)
treee56dfd30f344fc92d25b45ffe618ff69896589ab
parentbd5f777ff5e9f4fd81afeebb17074326b2671cd1 (diff)
[feature] Keep mapping of collections
it is a weakref dictionary so that the collections can be garbage collected when out of scope. Releases: 0.4.0
-rw-r--r--src/leap/mail/mail.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/leap/mail/mail.py b/src/leap/mail/mail.py
index ef9a0d9..57d96ef 100644
--- a/src/leap/mail/mail.py
+++ b/src/leap/mail/mail.py
@@ -20,6 +20,8 @@ Generic Access to Mail objects: Public LEAP Mail API.
import uuid
import logging
import StringIO
+import weakref
+
from twisted.internet import defer
from leap.common.check import leap_assert_type
@@ -732,6 +734,14 @@ class Account(object):
adaptor_class = SoledadMailAdaptor
+ # This is a mapping to collection instances so that we always
+ # return a reference to them instead of creating new ones. However, being a
+ # dictionary of weakrefs values, they automagically vanish from the dict
+ # when no hard refs is left to them (so they can be garbage collected)
+ # This is important because the different wrappers rely on several
+ # kinds of deferredLocks that are kept as class or instance variables
+ _collection_mapping = weakref.WeakValueDictionary()
+
def __init__(self, store, ready_cb=None):
self.store = store
self.adaptor = self.adaptor_class()
@@ -835,12 +845,19 @@ class Account(object):
def get_collection_by_mailbox(self, name):
"""
- :rtype: MessageCollection
+ :rtype: deferred
+ :return: a deferred that will fire with a MessageCollection
"""
+ collection = self._collection_mapping.get(name, None)
+ if collection:
+ return defer.succeed(collection)
+
# imap select will use this, passing the collection to SoledadMailbox
def get_collection_for_mailbox(mbox_wrapper):
- return MessageCollection(
+ collection = MessageCollection(
self.adaptor, self.store, self.mbox_indexer, mbox_wrapper)
+ self._collection_mapping[name] = collection
+ return collection
d = self.adaptor.get_or_create_mbox(self.store, name)
d.addCallback(get_collection_for_mailbox)