summaryrefslogtreecommitdiff
path: root/alot/db/manager.py
blob: c3d7507bc164d41ce33cd006b0e2c6a3c1e9eb8e (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
# Copyright (C) 2011-2012  Patrick Totzke <patricktotzke@gmail.com>
# This file is released under the GNU GPL, version 3 or a later revision.
# For further details see the COPYING file

import asyncio
from   concurrent.futures import ThreadPoolExecutor
from   contextlib import closing
from   functools  import partial, partialmethod
import logging

from notmuch2 import Database, NotmuchError

from .errors import (DatabaseROError, NonexistantObjectError,
                     QueryError)
from .sort   import ORDER
from .thread import Thread
from ..settings.const import settings

# DB write operations
class _DBWriteList:
    """
    A list of database write operations.
    """

    _dbman  = None
    _ops    = None
    _future = None

    def __init__(self, dbman):
        self._dbman = dbman
        self._ops   = []

    def _do_apply(self):
        logging.debug('Performing DB write: %s', self)

        try:
            with Database(path    = self._dbman._db_path,
                          config  = self._dbman._config_path,
                          mode    = Database.MODE.READ_WRITE) as db:
                logging.debug('got writeable DB')

                with db.atomic():
                    for op in self._ops:
                        op(db)
        except Exception as e:
            logging.exception(e)
            self._future.set_exception(e)
        else:
            logging.debug('DB write completed: %s', self)
            self._future.set_result(True)

    def _do_tag_add(self, db, tags, query):
        for msg in db.messages(query):
            msg_tags  = msg.tags
            op_tags   = tags - msg_tags
            msg_tags |= op_tags

    def _do_tag_remove(self, db, tags, query):
        for msg in db.messages(query):
            msg_tags  = msg.tags
            op_tags   = tags & msg_tags
            msg_tags -= op_tags

    def _do_tag_set(self, db, tags, query):
        for msg in db.messages(query):
            msg_tags      = msg.tags
            property_tags = msg_tags & self._dbman._property_tags

            msg_tags.clear()
            msg_tags |= tags | property_tags

    def _do_msg_add(self, db, tags, path):
        msg, _ = db.add(path, sync_flags = self._dbman._sync_flags)

        msg_tags  = msg.tags
        msg_tags |= tags

    def queue_tag_add(self, tags, query):
        if tags:
            self._ops.append(partial(self._do_tag_add, query = query, tags = tags))
    def queue_tag_remove(self, tags, query):
        if tags:
            self._ops.append(partial(self._do_tag_remove, query = query, tags = tags))
    def queue_tag_set(self, tags, query):
        if tags:
            self._ops.append(partial(self._do_tag_set, query = query, tags = tags))
    def queue_msg_add(self, path, tags):
        self._ops.append(partial(self._do_msg_add, path = path, tags = tags))

    def apply(self):
        if self._future:
            raise ValueError('Multiple applies on a write list')

        self._ops    = tuple(self._ops)
        self._future = self._dbman._loop.create_future()

        if self._ops:
            self._dbman._write_queue.put_nowait(self)
        else:
            self._future.set_result(True)

        return self._future

    def __str__(self):
        return '%s:%s' % (self.__class__.__name__, self._ops)

class DBManager:
    """
    Keeps track of your index parameters, maintains a write-queue and
    lets you look up threads and messages directly to the persistent wrapper
    classes.
    """

    _db_path       = None
    _config_path   = None

    _loop          = None

    _sync_flags    = None

    _exclude_tags = None
    _property_tags = None

    _write_task    = None
    _write_queue   = None

    def __init__(self, loop, ro = False,
                 db_path = None, config_path = Database.CONFIG.SEARCH):
        self.ro = ro

        self._db_path     = db_path
        self._config_path = config_path

        self._loop = loop

        # read notmuch's config regarding imap flag synchronization
        self._sync_flags    = settings.get_notmuch_setting('maildir', 'synchronize_flags')

        self._exclude_tags = frozenset(settings.get('exclude_tags'))
        self._property_tags = frozenset(settings.get('property_tags'))

    def _db_ro(self):
        return closing(Database(path = self._db_path, config = self._config_path,
                                mode = Database.MODE.READ_ONLY))

    def _count(self, what, querystring):
        try:
            with self._db_ro() as db:
                func = getattr(db, 'count_' +what)
                return func(querystring, exclude_tags = self._exclude_tags)
        except NotmuchError:
            return -1

    count_messages = partialmethod(_count, 'messages')
    """returns number of messages that match `querystring`"""

    count_threads  = partialmethod(_count, 'threads')
    """returns number of threads that match `querystring`"""

    def _get_notmuch_thread(self, db, tid):
        """returns :class:`notmuch.database.Thread` with given id"""
        querystr = 'thread:' + tid
        try:
            return next(db.threads(querystr, exclude_tags = self._exclude_tags))
        except StopIteration:
            errmsg = 'no thread with id %s exists!' % tid
            raise NonexistantObjectError(errmsg)

    def get_thread(self, tid):
        """returns :class:`Thread` with given thread id (str)"""
        with self._db_ro() as db:
            return Thread(self, self._get_notmuch_thread(db, tid))

    def get_all_tags(self):
        """
        returns all tagsstrings used in the database
        :rtype: set of str
        """
        with self._db_ro() as db:
            return set(db.tags)

    def get_named_queries(self):
        """
        returns the named queries stored in the database.
        :rtype: dict (str -> str) mapping alias to full query string
        """
        q_prefix = 'query.'

        with self._db_ro() as db:
            queries = filter(lambda k: k.startswith(q_prefix), db.config)
            return { q[len(q_prefix):] : db.config[q] for q in queries }

    def get_threads(self, querystring, sort = ORDER.NEWEST_FIRST,
                    exclude_tags = frozenset()):
        """
        asynchronously look up thread ids matching `querystring`.

        :param querystring: The query string to use for the lookup
        :type querystring: str.
        :param sort: Sort order.
        :type query: alot.db.sort.ORDER
        :param exclude_tags: Tags to exclude by default unless included in the
                             search
        :type exclude_tags: set of str
        :returns: iterator over thread ids
        """
        with self._db_ro() as db:
            exclude_tags = self._exclude_tags | exclude_tags

            try:
                for t in db.threads(querystring, sort = sort,
                                    exclude_tags = exclude_tags):
                    yield t.threadid
            except NotmuchError as e:
                raise QueryError from e

    async def startup(self):
        self._write_queue = asyncio.Queue()
        self._write_task  = asyncio.create_task(self._db_write_task())

    async def shutdown(self):
        if self._write_task:
            await self._write_queue.put(None)
            await self._write_task

    async def _db_write_task(self):
        # this task serialises write operations on the database and
        # sends them off to a thread so they do not block the event loop
        # one workers, as there can be only one DB writer at any moment
        with ThreadPoolExecutor(max_workers = 1) as executor:
            while True:
                cur_item = await self._write_queue.get()
                if cur_item is None:
                    self._write_queue.task_done()
                    break

                logging.debug('submitting write task: %s', cur_item)

                await self._loop.run_in_executor(executor, cur_item._do_apply)
                self._write_queue.task_done()

    def db_write_create(self):
        if self.ro:
            raise DatabaseROError()

        return _DBWriteList(self)

    def tags_add(self, query, tags):
        """
        Asynchronously add tags to messages matching `querystring`.

        :param querystring: notmuch search string
        :type querystring: str
        :param tags: a set of tags to be added
        :type tags: set of str
        """
        ret = self.db_write_create()
        ret.queue_tag_add(tags, query)
        return ret.apply()

    def tags_remove(self, query, tags):
        """
        Asynchronously remove tags to messages matching `querystring`.

        :param querystring: notmuch search string
        :type querystring: str
        :param tags: a set of tags to be added
        :type tags: set of str
        """
        ret = self.db_write_create()
        ret.queue_tag_remove(tags, query)
        return ret.apply()

    def tags_set(self, query, tags):
        """
        Asynchronously set tags to messages matching `querystring`.

        :param querystring: notmuch search string
        :type querystring: str
        :param tags: a set of tags to be added
        :type tags: set of str
        """
        ret = self.db_write_create()
        ret.queue_tag_set(tags, query)
        return ret.apply()

    def msg_add(self, path, tags):
        """
        Asynchronously add a file to the notmuch index.

        :param path: path to the file
        :type path: str
        :param tags: tagstrings to add
        :type tags: list of str
        """
        ret = self.db_write_create()
        ret.queue_msg_add(path, tags)
        return ret.apply()