2
# Copyright (C) 2006 Robey Pointer <robey@lag.net>
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.
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.
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
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
25
once a revision is committed in bazaar, it never changes, so once we have
26
cached a change, it's good forever.
35
from sqlite3 import dbapi2
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.
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))
45
con = dbapi2.connect(temp_path)
50
os.rename(temp_path, filename)
52
class FakeShelf(object):
54
def __init__(self, filename):
55
create_table = not os.path.exists(filename)
58
filename, "create table RevisionData "
59
"(revid binary primary key, data binary)")
60
self.connection = dbapi2.connect(filename)
61
self.cursor = self.connection.cursor()
63
def _create_table(self, filename):
64
con = dbapi2.connect(filename)
67
"create table RevisionData "
68
"(revid binary primary key, data binary)")
72
def _serialize(self, obj):
73
return dbapi2.Binary(pickle.dumps(obj, protocol=2))
75
def _unserialize(self, data):
76
return pickle.loads(str(data))
80
"select data from revisiondata where revid = ?", (revid, ))
81
filechange = self.cursor.fetchone()
82
if filechange is None:
85
return self._unserialize(filechange[0])
87
def add(self, revid, object):
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.
99
class RevInfoDiskCache(object):
100
"""Like `RevInfoMemoryCache` but backed in a sqlite DB."""
102
def __init__(self, cache_path):
103
if not os.path.exists(cache_path):
105
filename = os.path.join(cache_path, 'revinfo.sql')
106
create_table = not os.path.exists(filename)
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()
114
def get(self, key, revid):
115
if not isinstance(key, bytes):
117
if not isinstance(revid, bytes):
118
raise TypeError(revid)
120
"select revid, data from data where key = ?", (dbapi2.Binary(key),))
121
row = self.cursor.fetchone()
124
elif str(row[0]) != revid:
128
return marshal.loads(zlib.decompress(row[1]))
129
except (EOFError, ValueError, TypeError):
132
def set(self, key, revid, data):
133
if not isinstance(key, bytes):
135
if not isinstance(revid, bytes):
136
raise TypeError(revid)
139
'delete from data where key = ?', (dbapi2.Binary(key), ))
140
blob = zlib.compress(marshal.dumps(data, 2))
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!