25
25
cached a change, it's good forever.
32
34
from loggerhead import util
35
from loggerhead.util import decorator
33
36
from loggerhead.lockfile import LockFile
36
39
with_lock = util.with_lock('_lock', 'ChangeCache')
38
SQLITE_INTERFACE = os.environ.get('SQLITE_INTERFACE', 'sqlite')
40
if SQLITE_INTERFACE == 'pysqlite2':
41
from pysqlite2 import dbapi2
43
elif SQLITE_INTERFACE == 'sqlite':
44
import sqlite as dbapi2
47
raise AssertionError("bad sqlite interface %r!?"%SQLITE_INTERFACE)
49
_select_stmt = ("select data from revisiondata where revid = ?"
50
).replace('?', _param_marker)
51
_insert_stmt = ("insert into revisiondata (revid, data) "
52
"values (?, ?)").replace('?', _param_marker)
53
_update_stmt = ("update revisiondata set data = ? where revid = ?"
54
).replace('?', _param_marker)
59
class FakeShelf(object):
60
def __init__(self, filename):
61
create_table = not os.path.exists(filename)
62
self.connection = dbapi2.connect(filename)
63
self.cursor = self.connection.cursor()
66
def _create_table(self):
68
"create table RevisionData "
69
"(revid binary primary key, data binary)")
70
self.connection.commit()
71
def _serialize(self, obj):
72
r = dbapi2.Binary(cPickle.dumps(obj, protocol=2))
74
def _unserialize(self, data):
75
return cPickle.loads(str(data))
77
self.cursor.execute(_select_stmt, (revid,))
78
filechange = self.cursor.fetchone()
79
if filechange is None:
82
return self._unserialize(filechange[0])
83
def add(self, revid_obj_pairs, commit=True):
84
for (r, d) in revid_obj_pairs:
85
self.cursor.execute(_insert_stmt, (r, self._serialize(d)))
87
self.connection.commit()
88
def update(self, revid_obj_pairs, commit=True):
89
for (r, d) in revid_obj_pairs:
90
self.cursor.execute(_update_stmt, (self._serialize(d), r))
92
self.connection.commit()
95
"select count(*) from revisiondata")
96
return self.cursor.fetchone()[0]
97
def close(self, commit=False):
99
self.connection.commit()
100
self.connection.close()
102
42
class ChangeCache (object):
104
44
def __init__(self, history, cache_path):
105
45
self.history = history
106
46
self.log = history.log
108
48
if not os.path.exists(cache_path):
109
49
os.mkdir(cache_path)
111
self._changes_filename = os.path.join(cache_path, 'changes.sql')
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')
113
55
# use a lockfile since the cache folder could be shared across different processes.
114
56
self._lock = LockFile(os.path.join(cache_path, 'lock'))
115
57
self._closed = False
117
## # this is fluff; don't slow down startup time with it.
118
## # but it is racy in tests :(
120
## self.log.info('Using change cache %s; %d entries.' % (cache_path, self.size()))
121
## threading.Thread(target=log_sizes).start()
124
return FakeShelf(self._changes_filename)
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()
128
67
self.log.debug('Closing cache file.')
129
68
self._closed = True
133
72
return self._closed
140
def get_changes(self, revid_list):
79
def get_changes(self, revid_list, get_diffs=False):
142
81
get a list of changes by their revision_ids. any changes missing
143
82
from the cache are fetched by calling L{History.get_change_uncached}
144
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)
148
missing_revid_indices = []
149
cache = self._cache()
150
93
for revid in revid_list:
151
entry = cache.get(revid)
152
if entry is not None:
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])
155
missing_revids.append(revid)
156
missing_revid_indices.append(len(out))
100
#self.log.debug('Entry cache miss: %r' % (revid,))
159
missing_entries = self.history.get_changes_uncached(missing_revids)
160
missing_entry_dict = {}
161
for entry in missing_entries:
162
missing_entry_dict[entry.revid] = entry
163
revid_entry_pairs = []
164
for i, revid in zip(missing_revid_indices, missing_revids):
165
out[i] = entry = missing_entry_dict.get(revid)
166
if entry is not None:
167
revid_entry_pairs.append((revid, entry))
168
cache.add(revid_entry_pairs)
169
return filter(None, out)
173
cache = self._cache()
174
last_revid = util.to_utf8(self.history.last_revid)
175
revision_history = self.history.get_revision_history()
176
return (cache.count() >= len(revision_history)
177
and cache.get(last_revid) is not None)
181
return self._cache().count()
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)
183
138
def check_rebuild(self, max_time=3600):
185
140
check if we need to fill in any missing pieces of the cache. pull in
218
173
self.log.info('Revision cache rebuild completed.')
221
class FileChangeCache(object):
222
def __init__(self, history, cache_path):
223
self.history = history
225
if not os.path.exists(cache_path):
228
self._changes_filename = os.path.join(cache_path, 'filechanges.sql')
230
# use a lockfile since the cache folder could be shared across
231
# different processes.
232
self._lock = LockFile(os.path.join(cache_path, 'filechange-lock'))
235
def get_file_changes(self, entries):
238
missing_entry_indices = []
239
cache = FakeShelf(self._changes_filename)
240
for entry in entries:
241
changes = cache.get(entry.revid)
242
if changes is not None:
245
missing_entries.append(entry)
246
missing_entry_indices.append(len(out))
249
missing_changes = self.history.get_file_changes_uncached(missing_entries)
250
revid_changes_pairs = []
251
for i, entry, changes in zip(
252
missing_entry_indices, missing_entries, missing_changes):
253
revid_changes_pairs.append((entry.revid, changes))
255
cache.add(revid_changes_pairs)