summaryrefslogtreecommitdiff
path: root/client/src/leap/soledad/client/mp_safe_db.py
blob: 9ed0bef48fb0d9bd5441ed17a71e58d2e93b6941 (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
# -*- coding: utf-8 -*-
# mp_safe_db.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/>.


"""
Multiprocessing-safe SQLite database.
"""


from threading import Thread
from Queue import Queue
from pysqlcipher import dbapi2


# Thanks to http://code.activestate.com/recipes/526618/

class MPSafeSQLiteDB(Thread):
    """
    A multiprocessing-safe SQLite database accessor.
    """

    CLOSE = "--close--"
    NO_MORE = "--no more--"

    def __init__(self, db_path):
        """
        Initialize the process
        """
        Thread.__init__(self)
        self._db_path = db_path
        self._requests = Queue()
        self.start()

    def run(self):
        """
        Run the multiprocessing-safe database accessor.
        """
        conn = dbapi2.connect(self._db_path)
        while True:
            req, arg, res = self._requests.get()
            if req == self.CLOSE:
                break
            with conn:
                cursor = conn.cursor()
                cursor.execute(req, arg)
                if res:
                    for rec in cursor.fetchall():
                        res.put(rec)
                    res.put(self.NO_MORE)
        conn.close()

    def execute(self, req, arg=None, res=None):
        """
        Execute a request on the database.

        :param req: The request to be executed.
        :type req: str
        :param arg: The arguments for the request.
        :type arg: tuple
        :param res: A queue to write request results.
        :type res: multiprocessing.Queue
        """
        self._requests.put((req, arg or tuple(), res))

    def select(self, req, arg=None):
        """
        Run a select query on the database and yield results.

        :param req: The request to be executed.
        :type req: str
        :param arg: The arguments for the request.
        :type arg: tuple
        """
        res = Queue()
        self.execute(req, arg, res)
        while True:
            rec = res.get()
            if rec == self.NO_MORE:
                break
            yield rec

    def close(self):
        """
        Close the database connection.
        """
        self.execute(self.CLOSE)
        self.join()

    def cursor(self):
        """
        Return a fake cursor object.

        Not really a cursor, but allows for calling db.cursor().execute().

        :return: Self.
        :rtype: MPSafeSQLiteDatabase
        """
        return self