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
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.
25
24
once a revision is committed in bazaar, it never changes, so once we have
26
25
cached a change, it's good forever.
34
from sqlite3 import dbapi2
36
from pysqlite2 import dbapi2
38
# We take an optimistic approach to concurrency here: we might do work twice
39
# in the case of races, but not crash or corrupt data.
41
class FakeShelf(object):
43
def __init__(self, filename):
44
create_table = not os.path.exists(filename)
46
# To avoid races around creating the database, we create the db in
47
# a temporary file and rename it into the ultimate location.
48
fd, path = tempfile.mkstemp(dir=os.path.dirname(filename))
49
self._create_table(path)
50
os.rename(path, filename)
51
self.connection = dbapi2.connect(filename)
52
self.cursor = self.connection.cursor()
54
def _create_table(self, filename):
55
con = dbapi2.connect(filename)
58
"create table RevisionData "
59
"(revid binary primary key, data binary)")
63
def _serialize(self, obj):
64
return dbapi2.Binary(cPickle.dumps(obj, protocol=2))
66
def _unserialize(self, data):
67
return cPickle.loads(str(data))
71
"select data from revisiondata where revid = ?", (revid, ))
72
filechange = self.cursor.fetchone()
73
if filechange is None:
76
return self._unserialize(filechange[0])
78
def add(self, revid, object):
81
"insert into revisiondata (revid, data) values (?, ?)",
82
(revid, self._serialize(object)))
83
self.connection.commit()
84
except dbapi2.IntegrityError:
85
# If another thread or process attempted to set the same key, we
86
# assume it set it to the same value and carry on with our day.
90
class FileChangeCache(object):
34
from loggerhead import util
35
from loggerhead.util import decorator
36
from loggerhead.lockfile import LockFile
39
with_lock = util.with_lock('_lock', 'ChangeCache')
42
class ChangeCache (object):
92
44
def __init__(self, history, cache_path):
93
45
self.history = history
46
self.log = history.log
95
48
if not os.path.exists(cache_path):
96
49
os.mkdir(cache_path)
98
self._changes_filename = os.path.join(cache_path, 'filechanges.sql')
100
def get_file_changes(self, entry):
101
cache = FakeShelf(self._changes_filename)
102
changes = cache.get(entry.revid)
104
changes = self.history.get_file_changes_uncached(entry)
105
cache.add(entry.revid, changes)
51
# keep a separate cache for the diffs, because they're very time-consuming to fetch.
52
self._changes_filename = os.path.join(cache_path, 'changes')
53
self._changes_diffs_filename = os.path.join(cache_path, 'changes-diffs')
55
# use a lockfile since the cache folder could be shared across different processes.
56
self._lock = LockFile(os.path.join(cache_path, 'lock'))
59
# this is fluff; don't slow down startup time with it.
62
self.log.info('Using change cache %s; %d/%d entries.' % (cache_path, s1, s2))
63
threading.Thread(target=log_sizes).start()
67
self.log.debug('Closing cache file.')
79
def get_changes(self, revid_list, get_diffs=False):
81
get a list of changes by their revision_ids. any changes missing
82
from the cache are fetched by calling L{History.get_change_uncached}
83
and inserted into the cache before returning.
86
cache = shelve.open(self._changes_diffs_filename, 'c', protocol=2)
88
cache = shelve.open(self._changes_filename, 'c', protocol=2)
93
for revid in revid_list:
94
# if the revid is in unicode, use the utf-8 encoding as the key
95
srevid = util.to_utf8(revid)
98
out.append(cache[srevid])
100
#self.log.debug('Entry cache miss: %r' % (revid,))
102
fetch_list.append(revid)
103
sfetch_list.append(srevid)
105
if len(fetch_list) > 0:
106
# some revisions weren't in the cache; fetch them
107
changes = self.history.get_changes_uncached(fetch_list, get_diffs)
110
for i in xrange(len(revid_list)):
112
cache[sfetch_list.pop(0)] = out[i] = changes.pop(0)
118
def full(self, get_diffs=False):
120
cache = shelve.open(self._changes_diffs_filename, 'c', protocol=2)
122
cache = shelve.open(self._changes_filename, 'c', protocol=2)
124
return (len(cache) >= len(self.history.get_revision_history())) and (util.to_utf8(self.history.last_revid) in cache)
130
cache = shelve.open(self._changes_filename, 'c', protocol=2)
133
cache = shelve.open(self._changes_diffs_filename, 'c', protocol=2)
138
def check_rebuild(self, max_time=3600):
140
check if we need to fill in any missing pieces of the cache. pull in
141
any missing changes, but don't work any longer than C{max_time}
144
if self.closed() or self.full():
147
self.log.info('Building revision cache...')
148
start_time = time.time()
149
last_update = time.time()
152
work = list(self.history.get_revision_history())
154
for i in xrange(0, len(work), jump):
156
# must call into history so we grab the branch lock (otherwise, lock inversion)
157
self.history.get_changes(r)
163
if now - start_time > max_time:
164
self.log.info('Cache rebuilding will pause for now.')
167
if now - last_update > 60:
168
self.log.info('Revision cache rebuilding continues: %d/%d' % (min(count, len(work)), len(work)))
169
last_update = time.time()
171
# give someone else a chance at the lock
173
self.log.info('Revision cache rebuild completed.')