summaryrefslogtreecommitdiff
path: root/src/leap/mail/imap/memorystore.py
blob: d383b79c9af4968a38ace435e01530f879d5825b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
# -*- coding: utf-8 -*-
# memorystore.py
# Copyright (C) 2014 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
In-memory transient store for a LEAPIMAPServer.
"""
import contextlib
import logging
import threading
import weakref

from collections import defaultdict
from copy import copy

from enum import Enum
from twisted.internet import defer
from twisted.internet.task import LoopingCall
from twisted.python import log
from zope.interface import implements

from leap.common.check import leap_assert_type
from leap.mail import size
from leap.mail.utils import empty, phash_iter
from leap.mail.messageflow import MessageProducer
from leap.mail.imap import interfaces
from leap.mail.imap.fields import fields
from leap.mail.imap.messageparts import MessagePartType, MessagePartDoc
from leap.mail.imap.messageparts import RecentFlagsDoc
from leap.mail.imap.messageparts import MessageWrapper
from leap.mail.imap.messageparts import ReferenciableDict

from leap.mail.decorators import deferred_to_thread

logger = logging.getLogger(__name__)


# The default period to do writebacks to the permanent
# soledad storage, in seconds.
SOLEDAD_WRITE_PERIOD = 15

FDOC = MessagePartType.fdoc.key
HDOC = MessagePartType.hdoc.key
CDOCS = MessagePartType.cdocs.key
DOCS_ID = MessagePartType.docs_id.key


@contextlib.contextmanager
def set_bool_flag(obj, att):
    """
    Set a boolean flag to True while we're doing our thing.
    Just to let the world know.
    """
    setattr(obj, att, True)
    try:
        yield True
    except RuntimeError as exc:
        logger.exception(exc)
    finally:
        setattr(obj, att, False)


DirtyState = Enum("none", "dirty", "new")


class MemoryStore(object):
    """
    An in-memory store to where we can write the different parts that
    we split the messages into and buffer them until we write them to the
    permanent storage.

    It uses MessageWrapper instances to represent the message-parts, which are
    indexed by mailbox name and UID.

    It also can be passed a permanent storage as a paremeter (any implementor
    of IMessageStore, in this case a SoledadStore). In this case, a periodic
    dump of the messages stored in memory will be done. The period of the
    writes to the permanent storage is controled by the write_period parameter
    in the constructor.
    """
    implements(interfaces.IMessageStore,
               interfaces.IMessageStoreWriter)

    # TODO We will want to index by chash when we transition to local-only
    # UIDs.

    WRITING_FLAG = "_writing"
    _last_uid_lock = threading.Lock()
    _fdoc_docid_lock = threading.Lock()

    def __init__(self, permanent_store=None,
                 write_period=SOLEDAD_WRITE_PERIOD):
        """
        Initialize a MemoryStore.

        :param permanent_store: a IMessageStore implementor to dump
                                messages to.
        :type permanent_store: IMessageStore
        :param write_period: the interval to dump messages to disk, in seconds.
        :type write_period: int
        """
        from twisted.internet import reactor
        self.reactor = reactor

        self._permanent_store = permanent_store
        self._write_period = write_period

        # Internal Storage: messages
        """
        flags document store.
        _fdoc_store[mbox][uid] = { 'content': 'aaa' }
        """
        self._fdoc_store = defaultdict(lambda: defaultdict(
            lambda: ReferenciableDict({})))

        # Sizes
        """
        {'mbox, uid': <int>}
        """
        self._sizes = {}

        # Internal Storage: payload-hash
        """
        fdocs:doc-id store, stores document IDs for putting
        the dirty flags-docs.
        """
        self._fdoc_id_store = defaultdict(lambda: defaultdict(
            lambda: ''))

        # Internal Storage: content-hash:hdoc
        """
        hdoc-store keeps references to
        the header-documents indexed by content-hash.

        {'chash': { dict-stuff }
        }
        """
        self._hdoc_store = defaultdict(lambda: ReferenciableDict({}))

        # Internal Storage: payload-hash:cdoc
        """
        content-docs stored by payload-hash
        {'phash': { dict-stuff } }
        """
        self._cdoc_store = defaultdict(lambda: ReferenciableDict({}))

        # Internal Storage: content-hash:fdoc
        """
        chash-fdoc-store keeps references to
        the flag-documents indexed by content-hash.

        {'chash': {'mbox-a': weakref.proxy(dict),
                   'mbox-b': weakref.proxy(dict)}
        }
        """
        self._chash_fdoc_store = defaultdict(lambda: defaultdict(lambda: None))

        # Internal Storage: recent-flags store
        """
        recent-flags store keeps one dict per mailbox,
        with the document-id of the u1db document
        and the set of the UIDs that have the recent flag.

        {'mbox-a': {'doc_id': 'deadbeef',
                    'set': {1,2,3,4}
                    }
        }
        """
        # TODO this will have to transition to content-hash
        # indexes after we move to local-only UIDs.

        self._rflags_store = defaultdict(
            lambda: {'doc_id': None, 'set': set([])})

        """
        last-uid store keeps the count of the highest UID
        per mailbox.

        {'mbox-a': 42,
         'mbox-b': 23}
        """
        self._last_uid = defaultdict(lambda: 0)

        """
        known-uids keeps a count of the uids that soledad knows for a given
        mailbox

        {'mbox-a': set([1,2,3])}
        """
        self._known_uids = defaultdict(set)

        # New and dirty flags, to set MessageWrapper State.
        self._new = set([])
        self._new_queue = set([])
        self._new_deferreds = {}

        self._dirty = set([])
        self._dirty_queue = set([])
        self._dirty_deferreds = {}

        self._rflags_dirty = set([])

        # Flag for signaling we're busy writing to the disk storage.
        setattr(self, self.WRITING_FLAG, False)

        if self._permanent_store is not None:
            # this producer spits its messages to the permanent store
            # consumer using a queue. We will use that to put
            # our messages to be written.
            self.producer = MessageProducer(permanent_store,
                                            period=0.1)
            # looping call for dumping to SoledadStore
            self._write_loop = LoopingCall(self.write_messages,
                                           permanent_store)

            # We can start the write loop right now, why wait?
            self._start_write_loop()
        else:
            # We have a memory-only store.
            self.producer = None
            self._write_loop = None

    def _start_write_loop(self):
        """
        Start loop for writing to disk database.
        """
        if self._write_loop is None:
            return
        if not self._write_loop.running:
            self._write_loop.start(self._write_period, now=True)

    def _stop_write_loop(self):
        """
        Stop loop for writing to disk database.
        """
        if self._write_loop is None:
            return
        if self._write_loop.running:
            self._write_loop.stop()

    # IMessageStore

    # XXX this would work well for whole message operations.
    # We would have to add a put_flags operation to modify only
    # the flags doc (and set the dirty flag accordingly)

    def create_message(self, mbox, uid, message, observer,
                       notify_on_disk=True):
        """
        Create the passed message into this MemoryStore.

        By default we consider that any message is a new message.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param uid: the UID for the message
        :type uid: int
        :param message: a message to be added
        :type message: MessageWrapper
        :param observer: the deferred that will fire with the
                         UID of the message. If notify_on_disk is True,
                         this will happen when the message is written to
                         Soledad. Otherwise it will fire as soon as we've
                         added the message to the memory store.
        :type observer: Deferred
        :param notify_on_disk: whether the `observer` deferred should
                               wait until the message is written to disk to
                               be fired.
        :type notify_on_disk: bool
        """
        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)

        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):
        """
        Put an existing message.

        This will also set the dirty flag on the MemoryStore.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param uid: the UID for the message
        :type uid: int
        :param message: a message to be added
        :type message: MessageWrapper
        :param notify_on_disk: whether the deferred that is returned should
                               wait until the message is written to disk to
                               be fired.
        :type notify_on_disk: bool

        :return: a Deferred. if notify_on_disk is True, will be fired
                 when written to the db on disk.
                 Otherwise will fire inmediately
        :rtype: Deferred
        """
        key = mbox, uid
        d = defer.Deferred()
        d.addCallback(lambda result: log.msg("message PUT save: %s" % result))

        self._dirty.add(key)
        self._dirty_deferreds[key] = d
        self._add_message(mbox, uid, message, notify_on_disk)
        return d

    def _add_message(self, mbox, uid, message, notify_on_disk=True):
        """
        Helper method, called by both create_message and put_message.
        See those for parameter documentation.
        """
        msg_dict = message.as_dict()

        fdoc = msg_dict.get(FDOC, None)
        if fdoc is not None:
            fdoc_store = self._fdoc_store[mbox][uid]
            fdoc_store.update(fdoc)
            chash_fdoc_store = self._chash_fdoc_store

            # content-hash indexing
            chash = fdoc.get(fields.CONTENT_HASH_KEY)
            chash_fdoc_store[chash][mbox] = weakref.proxy(
                self._fdoc_store[mbox][uid])

        hdoc = msg_dict.get(HDOC, None)
        if hdoc is not None:
            chash = hdoc.get(fields.CONTENT_HASH_KEY)
            hdoc_store = self._hdoc_store[chash]
            hdoc_store.update(hdoc)

        cdocs = message.cdocs
        for cdoc in cdocs.values():
            phash = cdoc.get(fields.PAYLOAD_HASH_KEY, None)
            if not phash:
                continue
            cdoc_store = self._cdoc_store[phash]
            cdoc_store.update(cdoc)

        # Update memory store size
        # XXX this should use [mbox][uid]
        # TODO --- this has to be deferred to thread,
        # TODO add hdoc and cdocs sizes too
        # it's slowing things down here.
        #key = mbox, uid
        #self._sizes[key] = size.get_size(self._fdoc_store[key])

    def purge_fdoc_store(self, mbox):
        """
        Purge the empty documents from a fdoc store.
        Called during initialization of the SoledadMailbox

        :param mbox: the mailbox
        :type mbox: str or unicode
        """
        # XXX This is really a workaround until I find the conditions
        # that are making the empty items remain there.
        # This happens, for instance, after running several times
        # the regression test, that issues a store deleted + expunge + select
        # The items are being correclty deleted, but in succesive appends
        # the empty items with previously deleted uids reappear as empty
        # documents. I suspect it's a timing condition with a previously
        # evaluated sequence being used after the items has been removed.

        for uid, value in self._fdoc_store[mbox].items():
            if empty(value):
                del self._fdoc_store[mbox][uid]

    def get_docid_for_fdoc(self, mbox, uid):
        """
        Return Soledad document id for the flags-doc for a given mbox and uid,
        or None of no flags document could be found.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param uid: the message UID
        :type uid: int
        :rtype: unicode or None
        """
        with self._fdoc_docid_lock:
            doc_id = self._fdoc_id_store[mbox][uid]

        if empty(doc_id):
            fdoc = self._permanent_store.get_flags_doc(mbox, uid)
            if empty(fdoc) or empty(fdoc.content):
                return None
            doc_id = fdoc.doc_id
            self._fdoc_id_store[mbox][uid] = doc_id

        return doc_id

    def get_message(self, mbox, uid, dirtystate=DirtyState.none,
                    flags_only=False):
        """
        Get a MessageWrapper for the given mbox and uid combination.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param uid: the message UID
        :type uid: int
        :param dirtystate: DirtyState enum: one of `dirty`, `new`
                           or `none` (default)
        :type dirtystate: enum
        :param flags_only: whether the message should carry only a reference
                           to the flags document.
        :type flags_only: bool
        :

        :return: MessageWrapper or None
        """
        if dirtystate == DirtyState.dirty:
            flags_only = True

        key = mbox, uid

        fdoc = self._fdoc_store[mbox][uid]
        if empty(fdoc):
            return None

        new, dirty = False, False
        if dirtystate == DirtyState.none:
            new, dirty = self._get_new_dirty_state(key)
        if dirtystate == DirtyState.dirty:
            new, dirty = False, True
        if dirtystate == DirtyState.new:
            new, dirty = True, False

        if flags_only:
            return MessageWrapper(fdoc=fdoc,
                                  new=new, dirty=dirty,
                                  memstore=weakref.proxy(self))
        else:
            chash = fdoc.get(fields.CONTENT_HASH_KEY)
            hdoc = self._hdoc_store[chash]
            if empty(hdoc):
                hdoc = self._permanent_store.get_headers_doc(chash)
                if empty(hdoc):
                    return None
                if not empty(hdoc.content):
                    self._hdoc_store[chash] = hdoc.content
                    hdoc = hdoc.content
            cdocs = None

            pmap = hdoc.get(fields.PARTS_MAP_KEY, None)
            if new and pmap is not None:
                # take the different cdocs for write...
                cdoc_store = self._cdoc_store
                cdocs_list = phash_iter(hdoc)
                cdocs = dict(enumerate(
                    [cdoc_store[phash] for phash in cdocs_list], 1))

            return MessageWrapper(fdoc=fdoc, hdoc=hdoc, cdocs=cdocs,
                                  new=new, dirty=dirty,
                                  memstore=weakref.proxy(self))

    def remove_message(self, mbox, uid):
        """
        Remove a Message from this MemoryStore.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param uid: the message UID
        :type uid: int
        """
        # XXX For the moment we are only removing the flags and headers
        # docs. The rest we leave there polluting your hard disk,
        # until we think about a good way of deorphaning.

        # XXX implement elijah's idea of using a PUT document as a
        # token to ensure consistency in the removal.

        try:
            del self._fdoc_store[mbox][uid]
        except KeyError:
            pass

        try:
            key = mbox, uid
            self._new.discard(key)
            self._dirty.discard(key)
            if key in self._sizes:
                del self._sizes[key]
            self._known_uids[mbox].discard(uid)
        except KeyError:
            pass
        except Exception as exc:
            logger.error("error while removing message!")
            logger.exception(exc)
        try:
            with self._fdoc_docid_lock:
                del self._fdoc_id_store[mbox][uid]
        except KeyError:
            pass
        except Exception as exc:
            logger.error("error while removing message!")
            logger.exception(exc)

    # IMessageStoreWriter

    @deferred_to_thread
    def write_messages(self, store):
        """
        Write the message documents in this MemoryStore to a different store.

        :param store: the IMessageStore to write to
        :rtype: False if queue is not empty, None otherwise.
        """
        # For now, we pass if the queue is not empty, to avoid duplicate
        # queuing.
        # We would better use a flag to know when we've already enqueued an
        # item.

        # XXX this could return the deferred for all the enqueued operations

        if not self.producer.is_queue_empty():
            return False

        if any(map(lambda i: not empty(i), (self._new, self._dirty))):
            logger.info("Writing messages to Soledad...")

        # TODO change for lock, and make the property access
        # is accquired
        with set_bool_flag(self, self.WRITING_FLAG):
            for rflags_doc_wrapper in self.all_rdocs_iter():
                self.producer.push(rflags_doc_wrapper,
                                   state=self.producer.STATE_DIRTY)
            for msg_wrapper in self.all_new_msg_iter():
                self.producer.push(msg_wrapper,
                                   state=self.producer.STATE_NEW)
            for msg_wrapper in self.all_dirty_msg_iter():
                self.producer.push(msg_wrapper,
                                   state=self.producer.STATE_DIRTY)

    # MemoryStore specific methods.

    def get_uids(self, mbox):
        """
        Get all uids for a given mbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :rtype: list
        """
        return self._fdoc_store[mbox].keys()

    def get_soledad_known_uids(self, mbox):
        """
        Get all uids that soledad knows about, from the memory cache.
        :param mbox: the mailbox
        :type mbox: str or unicode
        :rtype: list
        """
        return self._known_uids.get(mbox, [])

    # last_uid

    def get_last_uid(self, mbox):
        """
        Return the highest UID for a given mbox.
        It will be the highest between the highest uid in the message store for
        the mailbox, and the soledad integer cache.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :rtype: int
        """
        uids = self.get_uids(mbox)
        last_mem_uid = uids and max(uids) or 0
        last_soledad_uid = self.get_last_soledad_uid(mbox)
        return max(last_mem_uid, last_soledad_uid)

    def get_last_soledad_uid(self, mbox):
        """
        Get last uid for a given mbox from the soledad integer cache.

        :param mbox: the mailbox
        :type mbox: str or unicode
        """
        return self._last_uid.get(mbox, 0)

    def set_last_soledad_uid(self, mbox, value):
        """
        Set last uid for a given mbox in the soledad integer cache.
        SoledadMailbox should prime this value during initialization.
        Other methods (during message adding) SHOULD call
        `increment_last_soledad_uid` instead.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param value: the value to set
        :type value: int
        """
        # can be long???
        #leap_assert_type(value, int)
        logger.info("setting last soledad uid for %s to %s" %
                    (mbox, value))
        # if we already have a value here, don't do anything
        with self._last_uid_lock:
            if not self._last_uid.get(mbox, None):
                self._last_uid[mbox] = value

    def set_known_uids(self, mbox, value):
        """
        Set the value fo the known-uids set for this mbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param value: a sequence of integers to be added to the set.
        :type value: tuple
        """
        current = self._known_uids[mbox]
        self._known_uids[mbox] = current.union(set(value))

    def increment_last_soledad_uid(self, mbox):
        """
        Increment by one the soledad integer cache for the last_uid for
        this mbox, and fire a defer-to-thread to update the soledad value.
        The caller should lock the call tho this method.

        :param mbox: the mailbox
        :type mbox: str or unicode
        """
        with self._last_uid_lock:
            self._last_uid[mbox] += 1
            value = self._last_uid[mbox]
            self.reactor.callInThread(self.write_last_uid, mbox, value)
            return value

    def write_last_uid(self, mbox, value):
        """
        Increment the soledad integer cache for the highest uid value.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param value: the value to set
        :type value: int
        """
        leap_assert_type(value, int)
        if self._permanent_store:
            self._permanent_store.write_last_uid(mbox, value)

    def load_flag_docs(self, mbox, flag_docs):
        """
        Load the flag documents for the given mbox.
        Used during initial flag docs prefetch.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param flag_docs: a dict with the content for the flag docs, indexed
                          by uid.
        :type flag_docs: dict
        """
        # We can do direct assignments cause we know this will only
        # be called during initialization of the mailbox.
        # TODO could hook here a sanity-check
        # for duplicates

        fdoc_store = self._fdoc_store[mbox]
        chash_fdoc_store = self._chash_fdoc_store
        for uid in flag_docs:
            rdict = ReferenciableDict(flag_docs[uid])
            fdoc_store[uid] = rdict
            # populate chash dict too, to avoid fdoc duplication
            chash = flag_docs[uid]["chash"]
            chash_fdoc_store[chash][mbox] = weakref.proxy(
                self._fdoc_store[mbox][uid])

    def update_flags(self, mbox, uid, fdoc):
        """
        Update the flag document for a given mbox and uid combination,
        and set the dirty flag.
        We could use put_message, but this is faster.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param uid: the uid of the message
        :type uid: int

        :param fdoc: a dict with the content for the flag docs
        :type fdoc: dict
        """
        key = mbox, uid
        self._fdoc_store[mbox][uid].update(fdoc)
        self._dirty.add(key)

    def load_header_docs(self, header_docs):
        """
        Load the flag documents for the given mbox.
        Used during header docs prefetch, and during cache after
        a read from soledad if the hdoc property in message did not
        find its value in here.

        :param flag_docs: a dict with the content for the flag docs.
        :type flag_docs: dict
        """
        hdoc_store = self._hdoc_store
        for chash in header_docs:
            hdoc_store[chash] = ReferenciableDict(header_docs[chash])

    def all_flags(self, mbox):
        """
        Return a dictionary with all the flags for a given mbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :rtype: dict
        """
        fdict = {}
        uids = self.get_uids(mbox)
        fstore = self._fdoc_store[mbox]

        for uid in uids:
            try:
                fdict[uid] = fstore[uid][fields.FLAGS_KEY]
            except KeyError:
                continue
        return fdict

    def all_headers(self, mbox):
        """
        Return a dictionary with all the header docs for a given mbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :rtype: dict
        """
        headers_dict = {}
        uids = self.get_uids(mbox)
        fdoc_store = self._fdoc_store[mbox]
        hdoc_store = self._hdoc_store

        for uid in uids:
            try:
                chash = fdoc_store[uid][fields.CONTENT_HASH_KEY]
                hdoc = hdoc_store[chash]
                if not empty(hdoc):
                    headers_dict[uid] = hdoc
            except KeyError:
                continue
        return headers_dict

    # Counting sheeps...

    def count_new_mbox(self, mbox):
        """
        Count the new messages by mailbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :return: number of new messages
        :rtype: int
        """
        return len([(m, uid) for m, uid in self._new if mbox == mbox])

    # XXX used at all?
    def count_new(self):
        """
        Count all the new messages in the MemoryStore.

        :rtype: int
        """
        return len(self._new)

    def count(self, mbox):
        """
        Return the count of messages for a given mbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :return: number of messages
        :rtype: int
        """
        return len(self._fdoc_store[mbox])

    def unseen_iter(self, mbox):
        """
        Get an iterator for the message UIDs with no `seen` flag
        for a given mailbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :return: iterator through unseen message doc UIDs
        :rtype: iterable
        """
        fdocs = self._fdoc_store[mbox]

        return [uid for uid, value
                in fdocs.items()
                if fields.SEEN_FLAG not in value.get(fields.FLAGS_KEY, [])]

    def get_cdoc_from_phash(self, phash):
        """
        Return a content-document by its payload-hash.

        :param phash: the payload hash to check against
        :type phash: str or unicode
        :rtype: MessagePartDoc
        """
        doc = self._cdoc_store.get(phash, None)

        # XXX return None for consistency?

        # XXX have to keep a mapping between phash and its linkage
        # info, to know if this payload is been already saved or not.
        # We will be able to get this from the linkage-docs,
        # not yet implemented.
        new = True
        dirty = False
        return MessagePartDoc(
            new=new, dirty=dirty, store="mem",
            part=MessagePartType.cdoc,
            content=doc,
            doc_id=None)

    def get_fdoc_from_chash(self, chash, mbox):
        """
        Return a flags-document by its content-hash and a given mailbox.
        Used during content-duplication detection while copying or adding a
        message.

        :param chash: the content hash to check against
        :type chash: str or unicode
        :param mbox: the mailbox
        :type mbox: str or unicode

        :return: MessagePartDoc. It will return None if the flags document
                 has empty content or it is flagged as \\Deleted.
        """
        fdoc = self._chash_fdoc_store[chash][mbox]

        # a couple of special cases.
        # 1. We might have a doc with empty content...
        if empty(fdoc):
            return None

        # 2. ...Or the message could exist, but being flagged for deletion.
        # We want to create a new one in this case.
        # Hmmm what if the deletion is un-done?? We would end with a
        # duplicate...
        if fdoc and fields.DELETED_FLAG in fdoc.get(fields.FLAGS_KEY, []):
            return None

        uid = fdoc[fields.UID_KEY]
        key = mbox, uid
        new = key in self._new
        dirty = key in self._dirty

        return MessagePartDoc(
            new=new, dirty=dirty, store="mem",
            part=MessagePartType.fdoc,
            content=fdoc,
            doc_id=None)

    def iter_fdoc_keys(self):
        """
        Return a generator through all the mbox, uid keys in the flags-doc
        store.
        """
        fdoc_store = self._fdoc_store
        for mbox in fdoc_store:
            for uid in fdoc_store[mbox]:
                yield mbox, uid

    def all_new_msg_iter(self):
        """
        Return generator that iterates through all new messages.

        :return: generator of MessageWrappers
        :rtype: generator
        """
        gm = self.get_message
        # need to freeze, set can change during iteration
        new = [gm(*key, dirtystate=DirtyState.new) for key in tuple(self._new)]
        # move content from new set to the queue
        self._new_queue.update(self._new)
        self._new.difference_update(self._new)
        return new

    def all_dirty_msg_iter(self):
        """
        Return generator that iterates through all dirty messages.

        :return: generator of MessageWrappers
        :rtype: generator
        """
        gm = self.get_message
        # need to freeze, set can change during iteration
        dirty = [gm(*key, flags_only=True, dirtystate=DirtyState.dirty)
                 for key in tuple(self._dirty)]
        # move content from new and dirty sets to the queue

        self._dirty_queue.update(self._dirty)
        self._dirty.difference_update(self._dirty)
        return dirty

    def all_deleted_uid_iter(self, mbox):
        """
        Return a list with the UIDs for all messags
        with deleted flag in a given mailbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :return: list of integers
        :rtype: list
        """
        # This *needs* to return a fixed sequence. Otherwise the dictionary len
        # will change during iteration, when we modify it
        fdocs = self._fdoc_store[mbox]
        return [uid for uid, value
                in fdocs.items()
                if fields.DELETED_FLAG in value.get(fields.FLAGS_KEY, [])]

    # new, dirty flags

    def _get_new_dirty_state(self, key):
        """
        Return `new` and `dirty` flags for a given message.

        :param key: the key for the message, in the form mbox, uid
        :type key: tuple
        :return: tuple of bools
        :rtype: tuple
        """
        # TODO change indexing of sets to [mbox][key] too.
        # XXX should return *first* the news, and *then* the dirty...

        # TODO should query in queues too , true?
        #
        return map(lambda _set: key in _set, (self._new, self._dirty))

    def set_new_queued(self, key):
        """
        Add the key value to the `new-queue` set.

        :param key: the key for the message, in the form mbox, uid
        :type key: tuple
        """
        self._new_queue.add(key)

    def unset_new_queued(self, key):
        """
        Remove the key value from the `new-queue` set.

        :param key: the key for the message, in the form mbox, uid
        :type key: tuple
        """
        self._new_queue.discard(key)
        deferreds = self._new_deferreds
        d = deferreds.get(key, None)
        if d:
            # XXX use a namedtuple for passing the result
            # when we check it in the other side.
            d.callback('%s, ok' % str(key))
            deferreds.pop(key)

    def set_dirty_queued(self, key):
        """
        Add the key value to the `dirty-queue` set.

        :param key: the key for the message, in the form mbox, uid
        :type key: tuple
        """
        self._dirty_queue.add(key)

    def unset_dirty_queued(self, key):
        """
        Remove the key value from the `dirty-queue` set.

        :param key: the key for the message, in the form mbox, uid
        :type key: tuple
        """
        self._dirty_queue.discard(key)
        deferreds = self._dirty_deferreds
        d = deferreds.get(key, None)
        if d:
            # XXX use a namedtuple for passing the result
            # when we check it in the other side.
            d.callback('%s, ok' % str(key))
            deferreds.pop(key)

    # Recent Flags

    def set_recent_flag(self, mbox, uid):
        """
        Set the `Recent` flag for a given mailbox and UID.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param uid: the message UID
        :type uid: int
        """
        self._rflags_dirty.add(mbox)
        self._rflags_store[mbox]['set'].add(uid)

    # TODO --- nice but unused
    def unset_recent_flag(self, mbox, uid):
        """
        Unset the `Recent` flag for a given mailbox and UID.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param uid: the message UID
        :type uid: int
        """
        self._rflags_store[mbox]['set'].discard(uid)

    def set_recent_flags(self, mbox, value):
        """
        Set the value for the set of the recent flags.
        Used from the property in the MessageCollection.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param value: a sequence of flags to set
        :type value: sequence
        """
        self._rflags_dirty.add(mbox)
        self._rflags_store[mbox]['set'] = set(value)

    def load_recent_flags(self, mbox, flags_doc):
        """
        Load the passed flags document in the recent flags store, for a given
        mailbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param flags_doc: A dictionary containing the `doc_id` of the Soledad
                          flags-document for this mailbox, and the `set`
                          of uids marked with that flag.
        """
        self._rflags_store[mbox] = flags_doc

    def get_recent_flags(self, mbox):
        """
        Return the set of UIDs with the `Recent` flag for this mailbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :rtype: set, or None
        """
        rflag_for_mbox = self._rflags_store.get(mbox, None)
        if not rflag_for_mbox:
            return None
        return self._rflags_store[mbox]['set']

    def all_rdocs_iter(self):
        """
        Return an iterator through all in-memory recent flag dicts, wrapped
        under a RecentFlagsDoc namedtuple.
        Used for saving to disk.

        :return: a generator of RecentFlagDoc
        :rtype: generator
        """
        # XXX use enums
        DOC_ID = "doc_id"
        SET = "set"

        rflags_store = self._rflags_store

        def get_rdoc(mbox, rdict):
            mbox_rflag_set = rdict[SET]
            recent_set = copy(mbox_rflag_set)
            # zero it!
            mbox_rflag_set.difference_update(mbox_rflag_set)
            return RecentFlagsDoc(
                doc_id=rflags_store[mbox][DOC_ID],
                content={
                    fields.TYPE_KEY: fields.TYPE_RECENT_VAL,
                    fields.MBOX_KEY: mbox,
                    fields.RECENTFLAGS_KEY: list(recent_set)
                })

        return (get_rdoc(mbox, rdict) for mbox, rdict in rflags_store.items()
                if not empty(rdict[SET]))

    # Methods that mirror the IMailbox interface

    def remove_all_deleted(self, mbox):
        """
        Remove all messages flagged \\Deleted from this Memory Store only.
        Called from `expunge`

        :param mbox: the mailbox
        :type mbox: str or unicode
        :return: a list of UIDs
        :rtype: list
        """
        mem_deleted = self.all_deleted_uid_iter(mbox)
        for uid in mem_deleted:
            self.remove_message(mbox, uid)
        return mem_deleted

    def stop_and_flush(self):
        """
        Stop the write loop and trigger a write to the producer.
        """
        self._stop_write_loop()
        if self._permanent_store is not None:
            # XXX we should check if we did get a True value on this
            # operation. If we got False we should retry! (queue was not empty)
            self.write_messages(self._permanent_store)
            self.producer.flush()

    def expunge(self, mbox, observer):
        """
        Remove all messages flagged \\Deleted, from the Memory Store
        and from the permanent store also.

        It first queues up a last write, and wait for the deferreds to be done
        before continuing.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param observer: a deferred that will be fired when expunge is done
        :type observer: Deferred
        """
        soledad_store = self._permanent_store
        if soledad_store is None:
            # just-in memory store, easy then.
            self._delete_from_memory(mbox, observer)
            return

        # We have a soledad storage.
        try:
            # Stop and trigger last write
            self.stop_and_flush()
            # Wait on the writebacks to finish

            # XXX what if pending deferreds is empty?
            pending_deferreds = (self._new_deferreds.get(mbox, []) +
                                 self._dirty_deferreds.get(mbox, []))
            d1 = defer.gatherResults(pending_deferreds, consumeErrors=True)
            d1.addCallback(
                self._delete_from_soledad_and_memory, mbox, observer)
        except Exception as exc:
            logger.exception(exc)

    def _delete_from_memory(self, mbox, observer):
        """
        Remove all messages marked as deleted from soledad and memory.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :param observer: a deferred that will be fired when expunge is done
        :type observer: Deferred
        """
        mem_deleted = self.remove_all_deleted(mbox)
        observer.callback(mem_deleted)

    def _delete_from_soledad_and_memory(self, result, mbox, observer):
        """
        Remove all messages marked as deleted from soledad and memory.

        :param result: ignored. the result of the deferredList that triggers
                       this as a callback from `expunge`.
        :param mbox: the mailbox
        :type mbox: str or unicode
        :param observer: a deferred that will be fired when expunge is done
        :type observer: Deferred
        """
        all_deleted = []
        soledad_store = self._permanent_store

        try:
            # 1. Delete all messages marked as deleted in soledad.
            logger.debug("DELETING FROM SOLEDAD ALL FOR %r" % (mbox,))
            sol_deleted = soledad_store.remove_all_deleted(mbox)

            try:
                self._known_uids[mbox].difference_update(set(sol_deleted))
            except Exception as exc:
                logger.exception(exc)

            # 2. Delete all messages marked as deleted in memory.
            logger.debug("DELETING FROM MEM ALL FOR %r" % (mbox,))
            mem_deleted = self.remove_all_deleted(mbox)

            all_deleted = set(mem_deleted).union(set(sol_deleted))
            logger.debug("deleted %r" % all_deleted)
        except Exception as exc:
            logger.exception(exc)
        finally:
            self._start_write_loop()

        observer.callback(all_deleted)

    # Mailbox documents and attributes

    # This could be also be cached in memstore, but proxying directly
    # to soledad since it's not too performance-critical.

    def get_mbox_doc(self, mbox):
        """
        Return the soledad document for a given mailbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :rtype: SoledadDocument or None.
        """
        return self.permanent_store.get_mbox_document(mbox)

    def get_mbox_closed(self, mbox):
        """
        Return the closed attribute for a given mailbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        :rtype: bool
        """
        return self.permanent_store.get_mbox_closed(mbox)

    def set_mbox_closed(self, mbox, closed):
        """
        Set the closed attribute for a given mailbox.

        :param mbox: the mailbox
        :type mbox: str or unicode
        """
        self.permanent_store.set_mbox_closed(mbox, closed)

    # Rename flag-documents

    def rename_fdocs_mailbox(self, old_mbox, new_mbox):
        """
        Change the mailbox name for all flag documents in a given mailbox.
        Used from account.rename

        :param old_mbox: name for the old mbox
        :type old_mbox: str or unicode
        :param new_mbox: name for the new mbox
        :type new_mbox: str or unicode
        """
        fs = self._fdoc_store
        keys = fs[old_mbox].keys()
        for k in keys:
            fdoc = fs[old_mbox][k]
            fdoc['mbox'] = new_mbox
            fs[new_mbox][k] = fdoc
            fs[old_mbox].pop(k)
            self._dirty.add((new_mbox, k))

    # Dump-to-disk controls.

    @property
    def is_writing(self):
        """
        Property that returns whether the store is currently writing its
        internal state to a permanent storage.

        Used to evaluate whether the CHECK command can inform that the field
        is clear to proceed, or waiting for the write operations to complete
        is needed instead.

        :rtype: bool
        """
        # FIXME this should return a deferred !!!
        # XXX ----- can fire when all new + dirty deferreds
        # are done (gatherResults)
        return getattr(self, self.WRITING_FLAG)

    @property
    def permanent_store(self):
        return self._permanent_store

    # Memory management.

    def get_size(self):
        """
        Return the size of the internal storage.
        Use for calculating the limit beyond which we should flush the store.

        :rtype: int
        """
        return reduce(lambda x, y: x + y, self._sizes, 0)