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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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.
36
from sqlite3 import dbapi2
38
from pysqlite2 import dbapi2
40
# We take an optimistic approach to concurrency here: we might do work twice
41
# in the case of races, but not crash or corrupt data.
43
def safe_init_db(filename, init_sql):
44
# To avoid races around creating the database, we create the db in
45
# a temporary file and rename it into the ultimate location.
46
fd, temp_path = tempfile.mkstemp(dir=os.path.dirname(filename))
48
con = dbapi2.connect(temp_path)
53
os.rename(temp_path, filename)
55
class FakeShelf(object):
57
def __init__(self, filename):
58
create_table = not os.path.exists(filename)
61
filename, "create table RevisionData "
62
"(revid binary primary key, data binary)")
63
self.connection = dbapi2.connect(filename)
64
self.cursor = self.connection.cursor()
66
def _create_table(self, filename):
67
con = dbapi2.connect(filename)
70
"create table RevisionData "
71
"(revid binary primary key, data binary)")
75
def _serialize(self, obj):
76
return dbapi2.Binary(cPickle.dumps(obj, protocol=2))
78
def _unserialize(self, data):
79
return cPickle.loads(str(data))
83
"select data from revisiondata where revid = ?", (revid, ))
84
filechange = self.cursor.fetchone()
85
if filechange is None:
88
return self._unserialize(filechange[0])
90
def add(self, revid, object):
93
"insert into revisiondata (revid, data) values (?, ?)",
94
(revid, self._serialize(object)))
95
self.connection.commit()
96
except dbapi2.IntegrityError:
97
# If another thread or process attempted to set the same key, we
98
# assume it set it to the same value and carry on with our day.
102
class FileChangeCache(object):
104
def __init__(self, cache_path):
106
if not os.path.exists(cache_path):
109
self._changes_filename = os.path.join(cache_path, 'filechanges.sql')
111
def get_file_changes(self, entry):
112
cache = FakeShelf(self._changes_filename)
113
changes = cache.get(entry.revid)
115
changes = self.history.get_file_changes_uncached(entry)
116
cache.add(entry.revid, changes)
120
class RevInfoDiskCache(object):
121
"""Like `RevInfoMemoryCache` but backed in a sqlite DB."""
123
def __init__(self, cache_path):
124
if not os.path.exists(cache_path):
126
filename = os.path.join(cache_path, 'revinfo.sql')
127
create_table = not os.path.exists(filename)
130
filename, "create table Data "
131
"(key binary primary key, revid binary, data binary)")
132
self.connection = dbapi2.connect(filename)
133
self.cursor = self.connection.cursor()
135
def get(self, key, revid):
137
"select revid, data from data where key = ?", (dbapi2.Binary(key),))
138
row = self.cursor.fetchone()
141
elif str(row[0]) != revid:
144
return marshal.loads(zlib.decompress(row[1]))
146
def set(self, key, revid, data):
149
'delete from data where key = ?', (dbapi2.Binary(key), ))
150
blob = zlib.compress(marshal.dumps(data))
152
"insert into data (key, revid, data) values (?, ?, ?)",
153
map(dbapi2.Binary, [key, revid, blob]))
154
self.connection.commit()
155
except dbapi2.IntegrityError:
156
# If another thread or process attempted to set the same key, we
157
# don't care too much -- it's only a cache after all!