/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/changecache.py

  • Committer: Jelmer Vernooij
  • Date: 2021-12-17 21:39:45 UTC
  • mto: This revision was merged to the branch mainline in revision 520.
  • Revision ID: jelmer@jelmer.uk-20211217213945-a5zdr4tays3vl0h7
Keep python3.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (C) 2006  Robey Pointer <robey@lag.net>
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335  USA
 
17
#
 
18
 
 
19
"""
 
20
a cache for chewed-up 'file change' data structures, which are basically just
 
21
a different way of storing a revision delta.  the cache improves lookup times
 
22
10x over bazaar's xml revision structure, though, so currently still worth
 
23
doing.
 
24
 
 
25
once a revision is committed in bazaar, it never changes, so once we have
 
26
cached a change, it's good forever.
 
27
"""
 
28
 
 
29
import pickle
 
30
import marshal
 
31
import os
 
32
import tempfile
 
33
import zlib
 
34
 
 
35
from sqlite3 import dbapi2
 
36
 
 
37
# We take an optimistic approach to concurrency here: we might do work twice
 
38
# in the case of races, but not crash or corrupt data.
 
39
 
 
40
def safe_init_db(filename, init_sql):
 
41
    # To avoid races around creating the database, we create the db in
 
42
    # a temporary file and rename it into the ultimate location.
 
43
    fd, temp_path = tempfile.mkstemp(dir=os.path.dirname(filename))
 
44
    os.close(fd)
 
45
    con = dbapi2.connect(temp_path)
 
46
    cur = con.cursor()
 
47
    cur.execute(init_sql)
 
48
    con.commit()
 
49
    con.close()
 
50
    os.rename(temp_path, filename)
 
51
 
 
52
class FakeShelf(object):
 
53
 
 
54
    def __init__(self, filename):
 
55
        create_table = not os.path.exists(filename)
 
56
        if create_table:
 
57
            safe_init_db(
 
58
                filename, "create table RevisionData "
 
59
                "(revid binary primary key, data binary)")
 
60
        self.connection = dbapi2.connect(filename)
 
61
        self.cursor = self.connection.cursor()
 
62
 
 
63
    def _create_table(self, filename):
 
64
        con = dbapi2.connect(filename)
 
65
        cur = con.cursor()
 
66
        cur.execute(
 
67
            "create table RevisionData "
 
68
            "(revid binary primary key, data binary)")
 
69
        con.commit()
 
70
        con.close()
 
71
 
 
72
    def _serialize(self, obj):
 
73
        return dbapi2.Binary(pickle.dumps(obj, protocol=2))
 
74
 
 
75
    def _unserialize(self, data):
 
76
        return pickle.loads(str(data))
 
77
 
 
78
    def get(self, revid):
 
79
        self.cursor.execute(
 
80
            "select data from revisiondata where revid = ?", (revid, ))
 
81
        filechange = self.cursor.fetchone()
 
82
        if filechange is None:
 
83
            return None
 
84
        else:
 
85
            return self._unserialize(filechange[0])
 
86
 
 
87
    def add(self, revid, object):
 
88
        try:
 
89
            self.cursor.execute(
 
90
                "insert into revisiondata (revid, data) values (?, ?)",
 
91
                (revid, self._serialize(object)))
 
92
            self.connection.commit()
 
93
        except dbapi2.IntegrityError:
 
94
            # If another thread or process attempted to set the same key, we
 
95
            # assume it set it to the same value and carry on with our day.
 
96
            pass
 
97
 
 
98
 
 
99
class RevInfoDiskCache(object):
 
100
    """Like `RevInfoMemoryCache` but backed in a sqlite DB."""
 
101
 
 
102
    def __init__(self, cache_path):
 
103
        if not os.path.exists(cache_path):
 
104
            os.mkdir(cache_path)
 
105
        filename = os.path.join(cache_path, 'revinfo.sql')
 
106
        create_table = not os.path.exists(filename)
 
107
        if create_table:
 
108
            safe_init_db(
 
109
                filename, "create table Data "
 
110
                "(key binary primary key, revid binary, data binary)")
 
111
        self.connection = dbapi2.connect(filename)
 
112
        self.cursor = self.connection.cursor()
 
113
 
 
114
    def get(self, key, revid):
 
115
        if not isinstance(key, bytes):
 
116
            raise TypeError(key)
 
117
        if not isinstance(revid, bytes):
 
118
            raise TypeError(revid)
 
119
        self.cursor.execute(
 
120
            "select revid, data from data where key = ?", (dbapi2.Binary(key),))
 
121
        row = self.cursor.fetchone()
 
122
        if row is None:
 
123
            return None
 
124
        elif str(row[0]) != revid:
 
125
            return None
 
126
        else:
 
127
            try:
 
128
                return marshal.loads(zlib.decompress(row[1]))
 
129
            except (EOFError, ValueError, TypeError):
 
130
                return None
 
131
 
 
132
    def set(self, key, revid, data):
 
133
        if not isinstance(key, bytes):
 
134
            raise TypeError(key)
 
135
        if not isinstance(revid, bytes):
 
136
            raise TypeError(revid)
 
137
        try:
 
138
            self.cursor.execute(
 
139
                'delete from data where key = ?', (dbapi2.Binary(key), ))
 
140
            blob = zlib.compress(marshal.dumps(data, 2))
 
141
            self.cursor.execute(
 
142
                "insert into data (key, revid, data) values (?, ?, ?)",
 
143
                list(map(dbapi2.Binary, [key, revid, blob])))
 
144
            self.connection.commit()
 
145
        except dbapi2.IntegrityError:
 
146
            # If another thread or process attempted to set the same key, we
 
147
            # don't care too much -- it's only a cache after all!
 
148
            pass