/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

Implement sha cache.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
from bzrlib.errors import NoSuchRevision
22
22
 
 
23
import os
 
24
 
23
25
 
24
26
def check_pysqlite_version(sqlite3):
25
27
    """Check that sqlite library is compatible.
48
50
 
49
51
    def __init__(self, transport):
50
52
        self.transport = transport
 
53
        self.db = sqlite3.connect(
 
54
            os.path.join(self.transport.local_abspath("."), "git.db"))
 
55
        self.db.executescript("""
 
56
        create table if not exists commits(sha1 text, revid text, tree_sha text);
 
57
        create index if not exists commit_sha1 on commits(sha1);
 
58
        create table if not exists blobs(sha1 text, fileid text, revid text);
 
59
        create index if not exists blobs_sha1 on blobs(sha1);
 
60
        create table if not exists trees(sha1 text, fileid text, revid text);
 
61
        create index if not exists trees_sha1 on trees(sha1);
 
62
""")
51
63
 
52
64
    def add_entry(self, sha, type, type_data):
53
65
        """Add a new entry to the database.
54
66
        """
55
 
        raise NotImplementedError(self.add_entry)
 
67
        if type == "commit":
 
68
            self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
 
69
        elif type == "blob":
 
70
            self.db.execute("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
 
71
        elif type == "tree":
 
72
            self.db.execute("replace into trees (sha1, fileid, revid) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
 
73
        else:
 
74
            raise AssertionError("Unknown type %s" % type)
56
75
 
57
76
    def lookup_git_sha(self, sha):
58
77
        """Lookup a Git sha in the database.
61
80
        :return: (type, type_data) with type_data:
62
81
            revision: revid, tree sha
63
82
        """
64
 
        raise NotImplementedError(self.lookup_git_sha)
 
83
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
 
84
        if row is not None:
 
85
            return ("commit", row)
 
86
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
 
87
        if row is not None:
 
88
            return ("blob", row)
 
89
        row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
 
90
        if row is not None:
 
91
            return ("tree", row)
 
92
        raise KeyError(sha)
 
93
 
 
94
    def revids(self):
 
95
        for row in self.db.execute("select revid from commits").fetchall():
 
96
            yield row[0]