/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 docstring.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Map from Git sha's to Bazaar objects."""
 
18
 
 
19
from dulwich.objects import (
 
20
    sha_to_hex,
 
21
    hex_to_sha,
 
22
    )
 
23
import os
 
24
import threading
 
25
 
 
26
import bzrlib
 
27
from bzrlib import (
 
28
    trace,
 
29
    )
 
30
 
 
31
 
 
32
def get_cache_dir():
 
33
    try:
 
34
        from xdg.BaseDirectory import xdg_cache_home
 
35
    except ImportError:
 
36
        from bzrlib.config import config_dir
 
37
        ret = os.path.join(config_dir(), "git")
 
38
    else:
 
39
        ret = os.path.join(xdg_cache_home, "bazaar", "git")
 
40
    if not os.path.isdir(ret):
 
41
        os.makedirs(ret)
 
42
    return ret
 
43
 
 
44
 
 
45
def check_pysqlite_version(sqlite3):
 
46
    """Check that sqlite library is compatible.
 
47
 
 
48
    """
 
49
    if (sqlite3.sqlite_version_info[0] < 3 or
 
50
            (sqlite3.sqlite_version_info[0] == 3 and
 
51
             sqlite3.sqlite_version_info[1] < 3)):
 
52
        trace.warning('Needs at least sqlite 3.3.x')
 
53
        raise bzrlib.errors.BzrError("incompatible sqlite library")
 
54
 
 
55
try:
 
56
    try:
 
57
        import sqlite3
 
58
        check_pysqlite_version(sqlite3)
 
59
    except (ImportError, bzrlib.errors.BzrError), e:
 
60
        from pysqlite2 import dbapi2 as sqlite3
 
61
        check_pysqlite_version(sqlite3)
 
62
except:
 
63
    trace.warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
 
64
            'module')
 
65
    raise bzrlib.errors.BzrError("missing sqlite library")
 
66
 
 
67
 
 
68
_mapdbs = threading.local()
 
69
def mapdbs():
 
70
    """Get a cache for this thread's db connections."""
 
71
    try:
 
72
        return _mapdbs.cache
 
73
    except AttributeError:
 
74
        _mapdbs.cache = {}
 
75
        return _mapdbs.cache
 
76
 
 
77
 
 
78
class InventorySHAMap(object):
 
79
    """Maps inventory file ids to Git SHAs."""
 
80
 
 
81
    def lookup_blob(self, file_id, revision_hint=None):
 
82
        """Retrieve a Git blob SHA by file id.
 
83
 
 
84
        :param file_id: File id of the file/symlink
 
85
        :param revision_hint: Optional revision in which the file was last
 
86
            changed.
 
87
        """
 
88
        raise NotImplementedError(self.lookup_blob)
 
89
 
 
90
    def lookup_tree(self, file_id):
 
91
        """Retrieve a Git tree SHA by file id.
 
92
        """
 
93
        raise NotImplementedError(self.lookup_tree)
 
94
 
 
95
 
 
96
class GitShaMap(object):
 
97
    """Git<->Bzr revision id mapping database."""
 
98
 
 
99
    def _add_entry(self, sha, type, type_data):
 
100
        """Add a new entry to the database.
 
101
        """
 
102
        raise NotImplementedError(self._add_entry)
 
103
 
 
104
    def add_entries(self, revid, parent_revids, commit_sha, root_tree_sha, 
 
105
                    entries):
 
106
        """Add multiple new entries to the database.
 
107
        """
 
108
        self._add_entry(commit_sha, "commit", (revid, root_tree_sha))
 
109
        for (fileid, kind, hexsha, revision) in entries:
 
110
            self._add_entry(hexsha, kind, (fileid, revision))
 
111
 
 
112
    def get_inventory_sha_map(self, revid):
 
113
        """Return the inventory SHA map for a revision.
 
114
 
 
115
        :param revid: Revision to fetch the map for
 
116
        :return: A `InventorySHAMap`
 
117
        """
 
118
        raise NotImplementedError(self.get_inventory_sha_map)
 
119
 
 
120
    def lookup_git_sha(self, sha):
 
121
        """Lookup a Git sha in the database.
 
122
        :param sha: Git object sha
 
123
        :return: (type, type_data) with type_data:
 
124
            revision: revid, tree sha
 
125
        """
 
126
        raise NotImplementedError(self.lookup_git_sha)
 
127
 
 
128
    def revids(self):
 
129
        """List the revision ids known."""
 
130
        raise NotImplementedError(self.revids)
 
131
 
 
132
    def missing_revisions(self, revids):
 
133
        """Return set of all the revisions that are not present."""
 
134
        present_revids = set(self.revids())
 
135
        if not isinstance(revids, set):
 
136
            revids = set(revids)
 
137
        return revids - present_revids
 
138
 
 
139
    def sha1s(self):
 
140
        """List the SHA1s."""
 
141
        raise NotImplementedError(self.sha1s)
 
142
 
 
143
    def start_write_group(self):
 
144
        """Start writing changes."""
 
145
 
 
146
    def commit_write_group(self):
 
147
        """Commit any pending changes."""
 
148
 
 
149
    def abort_write_group(self):
 
150
        """Abort any pending changes."""
 
151
 
 
152
 
 
153
class DictGitShaMap(GitShaMap):
 
154
 
 
155
    def __init__(self):
 
156
        self._by_sha = {}
 
157
        self._by_fileid = {}
 
158
 
 
159
    def _add_entry(self, sha, type, type_data):
 
160
        self._by_sha[sha] = (type, type_data)
 
161
        if type in ("blob", "tree"):
 
162
            self._by_fileid.setdefault(type_data[1], {})[type_data[0]] = sha
 
163
 
 
164
    def get_inventory_sha_map(self, revid):
 
165
        class DictInventorySHAMap(InventorySHAMap):
 
166
 
 
167
            def __init__(self, base, revid):
 
168
                self._base = base
 
169
                self.revid = revid
 
170
 
 
171
            def lookup_blob(self, fileid, revision_hint=None):
 
172
                if revision_hint is not None:
 
173
                    revid = revision_hint
 
174
                else:
 
175
                    revid = self.revid
 
176
                return self._base._by_fileid[revid][fileid]
 
177
 
 
178
            def lookup_tree(self, fileid):
 
179
                return self._base._by_fileid[self.revid][fileid]
 
180
 
 
181
        return DictInventorySHAMap(self, revid)
 
182
 
 
183
    def lookup_git_sha(self, sha):
 
184
        return self._by_sha[sha]
 
185
 
 
186
    def revids(self):
 
187
        for key, (type, type_data) in self._by_sha.iteritems():
 
188
            if type == "commit":
 
189
                yield type_data[0]
 
190
 
 
191
    def sha1s(self):
 
192
        return self._by_sha.iterkeys()
 
193
 
 
194
 
 
195
class SqliteGitShaMap(GitShaMap):
 
196
 
 
197
    def __init__(self, path=None):
 
198
        self.path = path
 
199
        if path is None:
 
200
            self.db = sqlite3.connect(":memory:")
 
201
        else:
 
202
            if not mapdbs().has_key(path):
 
203
                mapdbs()[path] = sqlite3.connect(path)
 
204
            self.db = mapdbs()[path]
 
205
        self.db.text_factory = str
 
206
        self.db.executescript("""
 
207
        create table if not exists commits(
 
208
            sha1 text not null check(length(sha1) == 40),
 
209
            revid text not null,
 
210
            tree_sha text not null check(length(tree_sha) == 40)
 
211
        );
 
212
        create index if not exists commit_sha1 on commits(sha1);
 
213
        create unique index if not exists commit_revid on commits(revid);
 
214
        create table if not exists blobs(
 
215
            sha1 text not null check(length(sha1) == 40),
 
216
            fileid text not null,
 
217
            revid text not null
 
218
        );
 
219
        create index if not exists blobs_sha1 on blobs(sha1);
 
220
        create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
 
221
        create table if not exists trees(
 
222
            sha1 text unique not null check(length(sha1) == 40),
 
223
            fileid text not null,
 
224
            revid text not null
 
225
        );
 
226
        create unique index if not exists trees_sha1 on trees(sha1);
 
227
        create unique index if not exists trees_fileid_revid on trees(fileid, revid);
 
228
""")
 
229
 
 
230
    def __repr__(self):
 
231
        return "%s(%r)" % (self.__class__.__name__, self.path)
 
232
    
 
233
    @classmethod
 
234
    def from_repository(cls, repository):
 
235
        try:
 
236
            transport = getattr(repository, "_transport", None)
 
237
            if transport is not None:
 
238
                return cls(os.path.join(transport.local_abspath("."), "git.db"))
 
239
        except bzrlib.errors.NotLocalUrl:
 
240
            pass
 
241
        return cls(os.path.join(get_cache_dir(), "remote.db"))
 
242
 
 
243
    def lookup_commit(self, revid):
 
244
        row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
 
245
        if row is not None:
 
246
            return row[0]
 
247
        raise KeyError
 
248
 
 
249
    def commit_write_group(self):
 
250
        self.db.commit()
 
251
 
 
252
    def add_entries(self, revid, parent_revids, commit_sha, root_tree_sha,
 
253
                    entries):
 
254
        self._add_entry(commit_sha, "commit", (revid, root_tree_sha))
 
255
        trees = []
 
256
        blobs = []
 
257
        for (fileid, kind, hexsha, revision) in entries:
 
258
            if kind is None:
 
259
                continue
 
260
            if kind == "tree":
 
261
                trees.append((hexsha, "tree", (fileid, revid)))
 
262
            elif kind == "blob":
 
263
                blobs.append((hexsha, (fileid, revision)))
 
264
            else:
 
265
                raise AssertionError
 
266
        if trees:
 
267
            self.db.executemany("replace into trees (sha1, fileid, revid) values (?, ?, ?)", trees)
 
268
        if blobs:
 
269
            self.db.executemany("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", blobs)
 
270
 
 
271
 
 
272
    def _add_entry(self, sha, type, type_data):
 
273
        """Add a new entry to the database.
 
274
        """
 
275
        assert isinstance(type_data, tuple)
 
276
        if sha is None:
 
277
            return
 
278
        assert isinstance(sha, str), "type was %r" % sha
 
279
        if type == "commit":
 
280
            self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
 
281
        elif type in ("blob", "tree"):
 
282
            self.db.execute("replace into %ss (sha1, fileid, revid) values (?, ?, ?)" % type, (sha, type_data[0], type_data[1]))
 
283
        else:
 
284
            raise AssertionError("Unknown type %s" % type)
 
285
 
 
286
    def get_inventory_sha_map(self, revid):
 
287
        class SqliteInventorySHAMap(InventorySHAMap):
 
288
 
 
289
            def __init__(self, db, revid):
 
290
                self.db = db
 
291
                self.revid = revid
 
292
 
 
293
            def lookup_blob(self, fileid, revision_hint=None):
 
294
                if revision_hint is not None:
 
295
                    revid = revision_hint
 
296
                else:
 
297
                    revid = self.revid
 
298
                row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
 
299
                if row is not None:
 
300
                    return row[0]
 
301
                raise KeyError(fileid)
 
302
 
 
303
            def lookup_tree(self, fileid):
 
304
                row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid, self.revid)).fetchone()
 
305
                if row is not None:
 
306
                    return row[0]
 
307
                raise KeyError(fileid)
 
308
 
 
309
        return SqliteInventorySHAMap(self.db, revid)
 
310
 
 
311
    def lookup_git_sha(self, sha):
 
312
        """Lookup a Git sha in the database.
 
313
 
 
314
        :param sha: Git object sha
 
315
        :return: (type, type_data) with type_data:
 
316
            revision: revid, tree sha
 
317
        """
 
318
        def format(type, row):
 
319
            return (type, (row[0], row[1]))
 
320
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
 
321
        if row is not None:
 
322
            return format("commit", row)
 
323
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
 
324
        if row is not None:
 
325
            return format("blob", row)
 
326
        row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
 
327
        if row is not None:
 
328
            return format("tree", row)
 
329
        raise KeyError(sha)
 
330
 
 
331
    def revids(self):
 
332
        """List the revision ids known."""
 
333
        return (row for (row,) in self.db.execute("select revid from commits"))
 
334
 
 
335
    def sha1s(self):
 
336
        """List the SHA1s."""
 
337
        for table in ("blobs", "commits", "trees"):
 
338
            trace.note(table)
 
339
            for (row,) in self.db.execute("select sha1 from %s" % table):
 
340
                yield row
 
341
 
 
342
 
 
343
TDB_MAP_VERSION = 3
 
344
TDB_HASH_SIZE = 50000
 
345
 
 
346
 
 
347
class TdbGitShaMap(GitShaMap):
 
348
    """SHA Map that uses a TDB database.
 
349
 
 
350
    Entries:
 
351
 
 
352
    "git <sha1>" -> "<type> <type-data1> <type-data2>"
 
353
    "commit revid" -> "<sha1> <tree-id>"
 
354
    "tree fileid revid" -> "<sha1>"
 
355
    "blob fileid revid" -> "<sha1>"
 
356
    """
 
357
 
 
358
    def __init__(self, path=None):
 
359
        import tdb
 
360
        self.path = path
 
361
        if path is None:
 
362
            self.db = {}
 
363
        else:
 
364
            if not mapdbs().has_key(path):
 
365
                mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT,
 
366
                                          os.O_RDWR|os.O_CREAT)
 
367
            self.db = mapdbs()[path]
 
368
        try:
 
369
            if int(self.db["version"]) not in (2, 3):
 
370
                trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
 
371
                              self.db["version"], TDB_MAP_VERSION)
 
372
                self.db.clear()
 
373
        except KeyError:
 
374
            pass
 
375
        self.db["version"] = str(TDB_MAP_VERSION)
 
376
 
 
377
    def __repr__(self):
 
378
        return "%s(%r)" % (self.__class__.__name__, self.path)
 
379
 
 
380
    @classmethod
 
381
    def from_repository(cls, repository):
 
382
        try:
 
383
            transport = getattr(repository, "_transport", None)
 
384
            if transport is not None:
 
385
                return cls(os.path.join(transport.local_abspath("."), "git.tdb"))
 
386
        except bzrlib.errors.NotLocalUrl:
 
387
            pass
 
388
        return cls(os.path.join(get_cache_dir(), "remote.tdb"))
 
389
 
 
390
    def lookup_commit(self, revid):
 
391
        return sha_to_hex(self.db["commit\0" + revid][:20])
 
392
 
 
393
    def _add_entry(self, hexsha, type, type_data):
 
394
        """Add a new entry to the database.
 
395
        """
 
396
        if hexsha is None:
 
397
            sha = ""
 
398
        else:
 
399
            sha = hex_to_sha(hexsha)
 
400
            self.db["git\0" + sha] = "\0".join((type, type_data[0], type_data[1]))
 
401
        if type == "commit":
 
402
            self.db["commit\0" + type_data[0]] = "\0".join((sha, type_data[1]))
 
403
        elif type == "blob":
 
404
            self.db["\0".join(("blob", type_data[0], type_data[1]))] = sha
 
405
 
 
406
    def get_inventory_sha_map(self, revid):
 
407
 
 
408
        class TdbInventorySHAMap(InventorySHAMap):
 
409
 
 
410
            def __init__(self, db, revid):
 
411
                self.db = db
 
412
                self.revid = revid
 
413
 
 
414
            def lookup_blob(self, fileid, revision_hint=None):
 
415
                if revision_hint is not None:
 
416
                    revid = revision_hint
 
417
                else:
 
418
                    revid = self.revid
 
419
                return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
 
420
                
 
421
        return TdbInventorySHAMap(self.db, revid)
 
422
 
 
423
    def lookup_git_sha(self, sha):
 
424
        """Lookup a Git sha in the database.
 
425
 
 
426
        :param sha: Git object sha
 
427
        :return: (type, type_data) with type_data:
 
428
            revision: revid, tree sha
 
429
        """
 
430
        if len(sha) == 40:
 
431
            sha = hex_to_sha(sha)
 
432
        data = self.db["git\0" + sha].split("\0")
 
433
        return (data[0], (data[1], data[2]))
 
434
 
 
435
    def missing_revisions(self, revids):
 
436
        ret = set()
 
437
        for revid in revids:
 
438
            if self.db.get("commit\0" + revid) is None:
 
439
                ret.add(revid)
 
440
        return ret
 
441
 
 
442
    def revids(self):
 
443
        """List the revision ids known."""
 
444
        for key in self.db.iterkeys():
 
445
            if key.startswith("commit\0"):
 
446
                yield key[7:]
 
447
 
 
448
    def sha1s(self):
 
449
        """List the SHA1s."""
 
450
        for key in self.db.iterkeys():
 
451
            if key.startswith("git\0"):
 
452
                yield sha_to_hex(key[4:])
 
453
 
 
454
 
 
455
def from_repository(repository):
 
456
    try:
 
457
        return TdbGitShaMap.from_repository(repository)
 
458
    except ImportError:
 
459
        return SqliteGitShaMap.from_repository(repository)