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.
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):
44
def __init__(self, history, cache_path):
45
self.history = history
46
self.log = history.log
48
if not os.path.exists(cache_path):
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)
94
for revid in revid_list:
95
# if the revid is in unicode, use the utf-8 encoding as the key
96
srevid = util.to_utf8(revid)
99
out.append(cache[srevid])
101
#self.log.debug('Entry cache miss: %r' % (revid,))
103
fetch_list.append(revid)
104
sfetch_list.append(srevid)
106
if len(fetch_list) > 0:
107
# some revisions weren't in the cache; fetch them
108
changes = self.history.get_changes_uncached(fetch_list, get_diffs)
111
for i in xrange(len(revid_list)):
113
cache[sfetch_list.pop(0)] = out[i] = changes.pop(0)
119
def full(self, get_diffs=False):
121
cache = shelve.open(self._changes_diffs_filename, 'c', protocol=2)
123
cache = shelve.open(self._changes_filename, 'c', protocol=2)
125
return (len(cache) >= len(self.history.get_revision_history())) and (util.to_utf8(self.history.last_revid) in cache)
131
cache = shelve.open(self._changes_filename, 'c', protocol=2)
134
cache = shelve.open(self._changes_diffs_filename, 'c', protocol=2)
139
def check_rebuild(self, max_time=3600):
141
check if we need to fill in any missing pieces of the cache. pull in
142
any missing changes, but don't work any longer than C{max_time}
145
if self.closed() or self.full():
148
self.log.info('Building revision cache...')
149
start_time = time.time()
150
last_update = time.time()
153
work = list(self.history.get_revision_history())
155
for i in xrange(0, len(work), jump):
157
# must call into history so we grab the branch lock (otherwise, lock inversion)
158
self.history.get_changes(r)
164
if now - start_time > max_time:
165
self.log.info('Cache rebuilding will pause for now.')
168
if now - last_update > 60:
169
self.log.info('Revision cache rebuilding continues: %d/%d' % (min(count, len(work)), len(work)))
170
last_update = time.time()
172
# give someone else a chance at the lock
174
self.log.info('Revision cache rebuild completed.')