/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to shamap.py

Add BzrGitCache object.

Show diffs side-by-side

added added

removed removed

Lines of Context:
188
188
        return format.open(transport)
189
189
 
190
190
 
 
191
class CacheUpdater(object):
 
192
 
 
193
    def __init__(self, cache, rev, content_cache_types):
 
194
        self.cache = cache
 
195
        self.content_cache_types = content_cache_types
 
196
        self.revid = rev.revision_id
 
197
        self.parent_revids = rev.parent_ids
 
198
        self._commit = None
 
199
        self._entries = []
 
200
 
 
201
    def add_object(self, obj, ie):
 
202
        if obj.type_name == "commit":
 
203
            self._commit = obj
 
204
            assert ie is None
 
205
        elif obj.type_name in ("blob", "tree"):
 
206
            if obj.type_name == "blob":
 
207
                revision = ie.revision
 
208
            else:
 
209
                revision = self.revid
 
210
            self._entries.append((ie.file_id, obj.type_name, obj.id, revision))
 
211
        else:
 
212
            raise AssertionError
 
213
        if (self.cache.content_cache and 
 
214
            obj.type_name in self.content_cache_types):
 
215
            self.cache.content_cache.add(obj)
 
216
 
 
217
    def finish(self):
 
218
        if self._commit is None:
 
219
            raise AssertionError("No commit object added")
 
220
        self.cache.idmap.add_entries(self.revid, self.parent_revids,
 
221
            self._commit.id, self._commit.tree, self._entries)
 
222
        return self._commit
 
223
 
 
224
 
 
225
class BzrGitCache(object):
 
226
    """Caching backend."""
 
227
 
 
228
    def __init__(self, idmap, content_cache):
 
229
        self.idmap = idmap
 
230
        self.content_cache = content_cache
 
231
 
 
232
    def get_updater(self, rev, content_cache_types):
 
233
        return CacheUpdater(self, rev, content_cache_types)
 
234
 
 
235
 
191
236
class DictGitShaMap(GitShaMap):
192
237
 
193
238
    def __init__(self):
227
272
            basepath = transport.local_abspath(".")
228
273
        except bzrlib.errors.NotLocalUrl:
229
274
            basepath = get_cache_dir()
230
 
        return SqliteGitShaMap(os.path.join(get_cache_dir(), "idmap.db")), None
 
275
        return BzrGitCache(
 
276
            SqliteGitShaMap(os.path.join(get_cache_dir(), "idmap.db")),
 
277
            None)
231
278
 
232
279
 
233
280
class SqliteGitShaMap(GitShaMap):
364
411
        except bzrlib.errors.NotLocalUrl:
365
412
            basepath = get_cache_dir()
366
413
        try:
367
 
            return (TdbGitShaMap(os.path.join(get_cache_dir(), "idmap.tdb")),
 
414
            return BzrGitCache(
 
415
                    TdbGitShaMap(os.path.join(get_cache_dir(), "idmap.tdb")),
368
416
                    None)
369
417
        except ImportError:
370
418
            raise ImportError(
417
465
    def __repr__(self):
418
466
        return "%s(%r)" % (self.__class__.__name__, self.path)
419
467
 
420
 
    @classmethod
421
 
    def from_repository(cls, repository):
422
 
        try:
423
 
            transport = getattr(repository, "_transport", None)
424
 
            if transport is not None:
425
 
                return cls(os.path.join(transport.local_abspath("."), "shamap.tdb"))
426
 
        except bzrlib.errors.NotLocalUrl:
427
 
            pass
428
 
        return cls(os.path.join(get_cache_dir(), "remote.tdb"))
429
 
 
430
468
    def lookup_commit(self, revid):
431
469
        return sha_to_hex(self.db["commit\0" + revid][:20])
432
470