/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 cache.py

Support read locking object stores.

Show diffs side-by-side

added added

removed removed

Lines of Context:
100
100
    def lookup_git_sha(self, sha):
101
101
        """Lookup a Git sha in the database.
102
102
        :param sha: Git object sha
103
 
        :return: (type, type_data) with type_data:
 
103
        :return: list with (type, type_data) tuples with type_data:
104
104
            commit: revid, tree_sha, verifiers
105
105
            blob: fileid, revid
106
106
            tree: fileid, revid
120
120
        """
121
121
        raise NotImplementedError(self.lookup_tree_id)
122
122
 
 
123
    def lookup_commit(self, revid):
 
124
        """Retrieve a Git commit SHA by Bazaar revision id.
 
125
        """
 
126
        raise NotImplementedError(self.lookup_commit)
 
127
 
123
128
    def revids(self):
124
129
        """List the revision ids known."""
125
130
        raise NotImplementedError(self.revids)
267
272
        if obj.type_name == "commit":
268
273
            self._commit = obj
269
274
            assert type(ie) is dict
 
275
            key = self.revid
270
276
            type_data = (self.revid, self._commit.tree, ie)
271
277
            self.cache.idmap._by_revid[self.revid] = obj.id
272
278
        elif obj.type_name in ("blob", "tree"):
275
281
                    revision = ie.revision
276
282
                else:
277
283
                    revision = self.revid
278
 
                type_data = (ie.file_id, revision)
 
284
                key = type_data = (ie.file_id, revision)
279
285
                self.cache.idmap._by_fileid.setdefault(type_data[1], {})[type_data[0]] = obj.id
280
286
        else:
281
287
            raise AssertionError
282
 
        self.cache.idmap._by_sha[obj.id] = (obj.type_name, type_data)
 
288
        entry = (obj.type_name, type_data)
 
289
        self.cache.idmap._by_sha.setdefault(obj.id, {})[key] = entry
283
290
 
284
291
    def finish(self):
285
292
        if self._commit is None:
299
306
        return self._by_fileid[revision][fileid]
300
307
 
301
308
    def lookup_git_sha(self, sha):
302
 
        return self._by_sha[sha]
 
309
        for entry in self._by_sha[sha].itervalues():
 
310
            yield entry
303
311
 
304
312
    def lookup_tree_id(self, fileid, revision):
305
313
        return self._by_fileid[revision][fileid]
308
316
        return self._by_revid[revid]
309
317
 
310
318
    def revids(self):
311
 
        for key, (type, type_data) in self._by_sha.iteritems():
312
 
            if type == "commit":
313
 
                yield type_data[0]
 
319
        for key, entries in self._by_sha.iteritems():
 
320
            for (type, type_data) in entries.values():
 
321
                if type == "commit":
 
322
                    yield type_data[0]
314
323
 
315
324
    def sha1s(self):
316
325
        return self._by_sha.iterkeys()
329
338
    def add_object(self, obj, ie, path):
330
339
        if obj.type_name == "commit":
331
340
            self._commit = obj
332
 
            self._testament3_sha1 = ie["testament3-sha1"]
 
341
            self._testament3_sha1 = ie.get("testament3-sha1")
333
342
            assert type(ie) is dict
334
343
        elif obj.type_name == "tree":
335
344
            if ie is not None:
447
456
            tree: fileid, revid
448
457
            blob: fileid, revid
449
458
        """
450
 
        row = self.db.execute("select revid, tree_sha, testament3_sha1 from commits where sha1 = ?", (sha,)).fetchone()
451
 
        if row is not None:
452
 
            return ("commit", (row[0], row[1], {"testament3-sha1": row[2]}))
453
 
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
454
 
        if row is not None:
455
 
            return ("blob", row)
456
 
        row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
457
 
        if row is not None:
458
 
            return ("tree", row)
459
 
        raise KeyError(sha)
 
459
        found = False
 
460
        cursor = self.db.execute("select revid, tree_sha, testament3_sha1 from commits where sha1 = ?", (sha,))
 
461
        for row in cursor.fetchall():
 
462
            found = True
 
463
            if row[2] is not None:
 
464
                verifiers = {"testament3-sha1": row[2]}
 
465
            else:
 
466
                verifiers = {}
 
467
            yield ("commit", (row[0], row[1], verifiers))
 
468
        cursor = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,))
 
469
        for row in cursor.fetchall():
 
470
            found = True
 
471
            yield ("blob", row)
 
472
        cursor = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,))
 
473
        for row in cursor.fetchall():
 
474
            found = True
 
475
            yield ("tree", row)
 
476
        if not found:
 
477
            raise KeyError(sha)
460
478
 
461
479
    def revids(self):
462
480
        """List the revision ids known."""
485
503
        if obj.type_name == "commit":
486
504
            self.db["commit\0" + self.revid] = "\0".join((sha, obj.tree))
487
505
            assert type(ie) is dict, "was %r" % ie
488
 
            type_data = (self.revid, obj.tree, ie["testament3-sha1"])
 
506
            type_data = (self.revid, obj.tree)
 
507
            try:
 
508
                type_data += (ie["testament3-sha1"],)
 
509
            except KeyError:
 
510
                pass
489
511
            self._commit = obj
490
512
        elif obj.type_name == "blob":
491
513
            if ie is None:
498
520
            type_data = (ie.file_id, self.revid)
499
521
        else:
500
522
            raise AssertionError
501
 
        self.db["git\0" + sha] = "\0".join((obj.type_name, ) + type_data)
 
523
        entry = "\0".join((obj.type_name, ) + type_data) + "\n"
 
524
        key = "git\0" + sha
 
525
        try:
 
526
            oldval = self.db[key]
 
527
        except KeyError:
 
528
            self.db[key] = entry
 
529
        else:
 
530
            if oldval[-1] != "\n":
 
531
                self.db[key] = "".join([oldval, "\n", entry])
 
532
            else:
 
533
                self.db[key] = "".join([oldval, entry])
502
534
 
503
535
    def finish(self):
504
536
        if self._commit is None:
508
540
 
509
541
TdbBzrGitCache = lambda p: BzrGitCache(TdbGitShaMap(p), None, TdbCacheUpdater)
510
542
 
 
543
 
511
544
class TdbGitCacheFormat(BzrGitCacheFormat):
512
545
    """Cache format for tdb-based caches."""
513
546
 
516
549
 
517
550
    def open(self, transport):
518
551
        try:
519
 
            basepath = transport.local_abspath(".")
 
552
            basepath = transport.local_abspath(".").encode(osutils._fs_enc)
520
553
        except bzrlib.errors.NotLocalUrl:
521
554
            basepath = get_cache_dir()
 
555
        assert isinstance(basepath, str)
522
556
        try:
523
557
            return TdbBzrGitCache(os.path.join(basepath, "idmap.tdb"))
524
558
        except ImportError:
547
581
        if path is None:
548
582
            self.db = {}
549
583
        else:
 
584
            assert isinstance(path, str)
550
585
            if not mapdbs().has_key(path):
551
586
                mapdbs()[path] = tdb.Tdb(path, self.TDB_HASH_SIZE, tdb.DEFAULT,
552
587
                                          os.O_RDWR|os.O_CREAT)
592
627
        """
593
628
        if len(sha) == 40:
594
629
            sha = hex_to_sha(sha)
595
 
        data = self.db["git\0" + sha].split("\0")
596
 
        if data[0] == "commit":
597
 
            if len(data) == 3:
598
 
                return (data[0], (data[1], data[2], {}))
 
630
        value = self.db["git\0" + sha]
 
631
        for data in value.splitlines():
 
632
            data = data.split("\0")
 
633
            if data[0] == "commit":
 
634
                if len(data) == 3:
 
635
                    yield (data[0], (data[1], data[2], {}))
 
636
                else:
 
637
                    yield (data[0], (data[1], data[2], {"testament3-sha1": data[3]}))
 
638
            elif data[0] in ("tree", "blob"):
 
639
                yield (data[0], tuple(data[1:]))
599
640
            else:
600
 
                return (data[0], (data[1], data[2], {"testament3-sha1": data[3]}))
601
 
        else:
602
 
            return (data[0], tuple(data[1:]))
 
641
                raise AssertionError("unknown type %r" % data[0])
603
642
 
604
643
    def missing_revisions(self, revids):
605
644
        ret = set()
822
861
            except StopIteration:
823
862
                raise KeyError
824
863
 
825
 
    def _iter_keys_prefix(self, prefix):
 
864
    def _iter_entries_prefix(self, prefix):
826
865
        for entry in self._index.iter_entries_prefix([prefix]):
827
 
            yield entry[1]
 
866
            yield (entry[1], entry[2])
828
867
        if self._builder is not None:
829
868
            for entry in self._builder.iter_entries_prefix([prefix]):
830
 
                yield entry[1]
 
869
                yield (entry[1], entry[2])
831
870
 
832
871
    def lookup_commit(self, revid):
833
872
        return self._get_entry(("commit", revid, "X"))[:40]
836
875
        if hexsha is not None:
837
876
            self._name.update(hexsha)
838
877
            if type == "commit":
839
 
                td = (type_data[0], type_data[1], type_data[2]["testament3-sha1"])
 
878
                td = (type_data[0], type_data[1])
 
879
                try:
 
880
                    td += (type_data[2]["testament3-sha1"],)
 
881
                except KeyError:
 
882
                    pass
840
883
            else:
841
884
                td = type_data
842
885
            self._add_node(("git", hexsha, "X"), " ".join((type,) + td))
851
894
    def lookup_git_sha(self, sha):
852
895
        if len(sha) == 20:
853
896
            sha = sha_to_hex(sha)
854
 
        data = self._get_entry(("git", sha, "X")).split(" ", 3)
855
 
        if data[0] == "commit":
856
 
            return ("commit", (data[1], data[2], {"testament3-sha1": data[3]}))
857
 
        else:
858
 
            return (data[0], tuple(data[1:]))
 
897
        found = False
 
898
        for key, value in self._iter_entries_prefix(("git", sha, None)):
 
899
            found = True
 
900
            data = value.split(" ", 3)
 
901
            if data[0] == "commit":
 
902
                if data[3]:
 
903
                    verifiers = {"testament3-sha1": data[3]}
 
904
                else:
 
905
                    verifiers = {}
 
906
                yield ("commit", (data[1], data[2], verifiers))
 
907
            else:
 
908
                yield (data[0], tuple(data[1:]))
 
909
        if not found:
 
910
            raise KeyError(sha)
859
911
 
860
912
    def revids(self):
861
913
        """List the revision ids known."""
862
 
        for key in self._iter_keys_prefix(("commit", None, None)):
 
914
        for key, value in self._iter_entries_prefix(("commit", None, None)):
863
915
            yield key[1]
864
916
 
865
917
    def missing_revisions(self, revids):
872
924
 
873
925
    def sha1s(self):
874
926
        """List the SHA1s."""
875
 
        for key in self._iter_keys_prefix(("git", None, None)):
 
927
        for key, value in self._iter_entries_prefix(("git", None, None)):
876
928
            yield key[1]
877
929
 
878
930