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 "change" data structures, which are basically just a
21
different way of storing a revision. the cache improves lookup times 10x
22
over bazaar's xml revision structure, though, so currently still worth doing.
24
once a revision is committed in bazaar, it never changes, so once we have
25
cached a change, it's good forever.
31
from loggerhead import util
32
from loggerhead.lockfile import LockFile
34
with_lock = util.with_lock('_lock', 'ChangeCache')
37
from sqlite3 import dbapi2
39
from pysqlite2 import dbapi2
42
class FakeShelf(object):
44
def __init__(self, filename):
45
create_table = not os.path.exists(filename)
46
self.connection = dbapi2.connect(filename)
47
self.cursor = self.connection.cursor()
51
def _create_table(self):
53
"create table RevisionData "
54
"(revid binary primary key, data binary)")
55
self.connection.commit()
57
def _serialize(self, obj):
58
r = dbapi2.Binary(cPickle.dumps(obj, protocol=2))
61
def _unserialize(self, data):
62
return cPickle.loads(str(data))
66
"select data from revisiondata where revid = ?", (revid, ))
67
filechange = self.cursor.fetchone()
68
if filechange is None:
71
return self._unserialize(filechange[0])
73
def add(self, revid_obj_pairs):
74
for (r, d) in revid_obj_pairs:
76
"insert into revisiondata (revid, data) values (?, ?)",
77
(r, self._serialize(d)))
78
self.connection.commit()
81
class FileChangeCache(object):
83
def __init__(self, history, cache_path):
84
self.history = history
86
if not os.path.exists(cache_path):
89
self._changes_filename = os.path.join(cache_path, 'filechanges.sql')
91
# use a lockfile since the cache folder could be shared across
92
# different processes.
93
self._lock = LockFile(os.path.join(cache_path, 'filechange-lock'))
96
def get_file_changes(self, entries):
99
missing_entry_indices = []
100
cache = FakeShelf(self._changes_filename)
101
for entry in entries:
102
changes = cache.get(entry.revid)
103
if changes is not None:
106
missing_entries.append(entry)
107
missing_entry_indices.append(len(out))
110
missing_changes = self.history.get_file_changes_uncached(
112
revid_changes_pairs = []
113
for i, entry, changes in zip(
114
missing_entry_indices, missing_entries, missing_changes):
115
revid_changes_pairs.append((entry.revid, changes))
117
cache.add(revid_changes_pairs)