/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/changecache.py

merge my less-file-change-access branch, which mainly only computes expensive
information (the set of files changed in a revision) when it's actually needed,
but also a bunch of other streamlining and efficiency improvements.

Show diffs side-by-side

added added

removed removed

Lines of Context:
163
163
        self.flush()
164
164
 
165
165
 
 
166
class FileChangeCache(object):
 
167
    def __init__(self, history, cache_path):
 
168
        self.history = history
 
169
        self.log = history.log
 
170
 
 
171
        if not os.path.exists(cache_path):
 
172
            os.mkdir(cache_path)
 
173
 
 
174
        self._changes_filename = os.path.join(cache_path, 'filechanges')
 
175
 
 
176
        # use a lockfile since the cache folder could be shared across
 
177
        # different processes.
 
178
        self._lock = LockFile(os.path.join(cache_path, 'filechange-lock'))
 
179
 
 
180
        self._closed = False
 
181
 
 
182
    @with_lock
 
183
    def close(self):
 
184
        self.log.debug('Closing cache file.')
 
185
        self._closed = True
 
186
 
 
187
    @with_lock
 
188
    def closed(self):
 
189
        return self._closed
 
190
 
 
191
    @with_lock
 
192
    def flush(self):
 
193
        pass
 
194
 
 
195
    @with_lock
 
196
    def get_file_changes(self, entries):
 
197
        cache = shelve.open(self._changes_filename, 'c', protocol=2)
 
198
        try:
 
199
            out = []
 
200
            missing_entries = []
 
201
            missing_entry_indices = []
 
202
            for entry in entries:
 
203
                changes = cache.get(entry.revid)
 
204
                if changes is not None:
 
205
                    out.append(changes)
 
206
                else:
 
207
                    missing_entries.append(entry)
 
208
                    missing_entry_indices.append(len(out))
 
209
                    out.append(None)
 
210
            missing_changes = self.history.get_file_changes_uncached(missing_entries)
 
211
            for i, entry, changes in zip(
 
212
                missing_entry_indices, missing_entries, missing_changes):
 
213
                cache[entry.revid] = out[i] = changes
 
214
            return out
 
215
        finally:
 
216
            cache.close()