/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

Trivial cleanups.

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
    registry,
 
29
    trace,
 
30
    )
 
31
from bzrlib.transport import (
 
32
    get_transport,
 
33
    )
 
34
 
 
35
 
 
36
def get_cache_dir():
 
37
    try:
 
38
        from xdg.BaseDirectory import xdg_cache_home
 
39
    except ImportError:
 
40
        from bzrlib.config import config_dir
 
41
        ret = os.path.join(config_dir(), "git")
 
42
    else:
 
43
        ret = os.path.join(xdg_cache_home, "bazaar", "git")
 
44
    if not os.path.isdir(ret):
 
45
        os.makedirs(ret)
 
46
    return ret
 
47
 
 
48
 
 
49
def get_remote_cache_transport():
 
50
    return get_transport(get_cache_dir())
 
51
 
 
52
 
 
53
def check_pysqlite_version(sqlite3):
 
54
    """Check that sqlite library is compatible.
 
55
 
 
56
    """
 
57
    if (sqlite3.sqlite_version_info[0] < 3 or
 
58
            (sqlite3.sqlite_version_info[0] == 3 and
 
59
             sqlite3.sqlite_version_info[1] < 3)):
 
60
        trace.warning('Needs at least sqlite 3.3.x')
 
61
        raise bzrlib.errors.BzrError("incompatible sqlite library")
 
62
 
 
63
try:
 
64
    try:
 
65
        import sqlite3
 
66
        check_pysqlite_version(sqlite3)
 
67
    except (ImportError, bzrlib.errors.BzrError), e:
 
68
        from pysqlite2 import dbapi2 as sqlite3
 
69
        check_pysqlite_version(sqlite3)
 
70
except:
 
71
    trace.warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
 
72
            'module')
 
73
    raise bzrlib.errors.BzrError("missing sqlite library")
 
74
 
 
75
 
 
76
_mapdbs = threading.local()
 
77
def mapdbs():
 
78
    """Get a cache for this thread's db connections."""
 
79
    try:
 
80
        return _mapdbs.cache
 
81
    except AttributeError:
 
82
        _mapdbs.cache = {}
 
83
        return _mapdbs.cache
 
84
 
 
85
 
 
86
class GitShaMap(object):
 
87
    """Git<->Bzr revision id mapping database."""
 
88
 
 
89
    def lookup_git_sha(self, sha):
 
90
        """Lookup a Git sha in the database.
 
91
        :param sha: Git object sha
 
92
        :return: (type, type_data) with type_data:
 
93
            revision: revid, tree sha
 
94
        """
 
95
        raise NotImplementedError(self.lookup_git_sha)
 
96
 
 
97
    def lookup_blob_id(self, file_id, revision):
 
98
        """Retrieve a Git blob SHA by file id.
 
99
 
 
100
        :param file_id: File id of the file/symlink
 
101
        :param revision: revision in which the file was last changed.
 
102
        """
 
103
        raise NotImplementedError(self.lookup_blob_id)
 
104
 
 
105
    def lookup_tree_id(self, file_id, revision):
 
106
        """Retrieve a Git tree SHA by file id.
 
107
        """
 
108
        raise NotImplementedError(self.lookup_tree_id)
 
109
 
 
110
    def revids(self):
 
111
        """List the revision ids known."""
 
112
        raise NotImplementedError(self.revids)
 
113
 
 
114
    def missing_revisions(self, revids):
 
115
        """Return set of all the revisions that are not present."""
 
116
        present_revids = set(self.revids())
 
117
        if not isinstance(revids, set):
 
118
            revids = set(revids)
 
119
        return revids - present_revids
 
120
 
 
121
    def sha1s(self):
 
122
        """List the SHA1s."""
 
123
        raise NotImplementedError(self.sha1s)
 
124
 
 
125
    def start_write_group(self):
 
126
        """Start writing changes."""
 
127
 
 
128
    def commit_write_group(self):
 
129
        """Commit any pending changes."""
 
130
 
 
131
    def abort_write_group(self):
 
132
        """Abort any pending changes."""
 
133
 
 
134
 
 
135
class ContentCache(object):
 
136
    """Object that can cache Git objects."""
 
137
 
 
138
    def __getitem__(self, sha):
 
139
        """Retrieve an item, by SHA."""
 
140
        raise NotImplementedError(self.__getitem__)
 
141
 
 
142
 
 
143
class BzrGitCacheFormat(object):
 
144
 
 
145
    def get_format_string(self):
 
146
        raise NotImplementedError(self.get_format_string)
 
147
 
 
148
    def open(self, transport):
 
149
        raise NotImplementedError(self.open)
 
150
 
 
151
    def initialize(self, transport):
 
152
        transport.put_bytes('format', self.get_format_string())
 
153
 
 
154
    @classmethod
 
155
    def from_repository(self, repository):
 
156
        repo_transport = getattr(repository, "_transport", None)
 
157
        if repo_transport is not None:
 
158
            try:
 
159
                repo_transport.mkdir('git')
 
160
            except bzrlib.errors.FileExists:
 
161
                pass
 
162
            transport = repo_transport.clone('git')
 
163
        else:
 
164
            transport = get_remote_cache_transport()
 
165
        try:
 
166
            format_name = transport.get_bytes('format')
 
167
            format = formats.get(format_name)
 
168
        except bzrlib.errors.NoSuchFile:
 
169
            format = formats.get('default')
 
170
            format.initialize(transport)
 
171
        return format.open(transport)
 
172
 
 
173
 
 
174
class CacheUpdater(object):
 
175
 
 
176
    def add_object(self, obj, ie):
 
177
        raise NotImplementedError(self.add_object)
 
178
 
 
179
    def finish(self):
 
180
        raise NotImplementedError(self.finish)
 
181
 
 
182
 
 
183
class BzrGitCache(object):
 
184
    """Caching backend."""
 
185
 
 
186
    def __init__(self, idmap, content_cache, cache_updater_klass):
 
187
        self.idmap = idmap
 
188
        self.content_cache = content_cache
 
189
        self._cache_updater_klass = cache_updater_klass
 
190
 
 
191
    def get_updater(self, rev):
 
192
        return self._cache_updater_klass(self, rev)
 
193
 
 
194
 
 
195
DictBzrGitCache = lambda: BzrGitCache(DictGitShaMap(), None, DictCacheUpdater)
 
196
 
 
197
 
 
198
class DictCacheUpdater(CacheUpdater):
 
199
 
 
200
    def __init__(self, cache, rev):
 
201
        self.cache = cache
 
202
        self.revid = rev.revision_id
 
203
        self.parent_revids = rev.parent_ids
 
204
        self._commit = None
 
205
        self._entries = []
 
206
 
 
207
    def add_object(self, obj, ie):
 
208
        if obj.type_name == "commit":
 
209
            self._commit = obj
 
210
            assert ie is None
 
211
            type_data = (self.revid, self._commit.tree)
 
212
            self.cache.idmap._by_revid[self.revid] = obj.id
 
213
        elif obj.type_name in ("blob", "tree"):
 
214
            if obj.type_name == "blob":
 
215
                revision = ie.revision
 
216
            else:
 
217
                revision = self.revid
 
218
            type_data = (ie.file_id, revision)
 
219
            self.cache.idmap._by_fileid.setdefault(type_data[1], {})[type_data[0]] = obj.id
 
220
        else:
 
221
            raise AssertionError
 
222
        self.cache.idmap._by_sha[obj.id] = (obj.type_name, type_data)
 
223
 
 
224
    def finish(self):
 
225
        if self._commit is None:
 
226
            raise AssertionError("No commit object added")
 
227
        return self._commit
 
228
 
 
229
 
 
230
class DictGitShaMap(GitShaMap):
 
231
 
 
232
    def __init__(self):
 
233
        self._by_sha = {}
 
234
        self._by_fileid = {}
 
235
        self._by_revid = {}
 
236
 
 
237
    def lookup_blob_id(self, fileid, revision):
 
238
        return self._by_fileid[revision][fileid]
 
239
 
 
240
    def lookup_git_sha(self, sha):
 
241
        return self._by_sha[sha]
 
242
 
 
243
    def lookup_tree_id(self, fileid, revision):
 
244
        return self._base._by_fileid[revision][fileid]
 
245
 
 
246
    def lookup_commit(self, revid):
 
247
        return self._by_revid[revid]
 
248
 
 
249
    def revids(self):
 
250
        for key, (type, type_data) in self._by_sha.iteritems():
 
251
            if type == "commit":
 
252
                yield type_data[0]
 
253
 
 
254
    def sha1s(self):
 
255
        return self._by_sha.iterkeys()
 
256
 
 
257
 
 
258
class SqliteCacheUpdater(CacheUpdater):
 
259
 
 
260
    def __init__(self, cache, rev):
 
261
        self.cache = cache
 
262
        self.db = self.cache.idmap.db
 
263
        self.revid = rev.revision_id
 
264
        self._commit = None
 
265
        self._trees = []
 
266
        self._blobs = []
 
267
 
 
268
    def add_object(self, obj, ie):
 
269
        if obj.type_name == "commit":
 
270
            self._commit = obj
 
271
            assert ie is None
 
272
        elif obj.type_name == "tree":
 
273
            self._trees.append((obj.id, ie.file_id, self.revid))
 
274
        elif obj.type_name == "blob":
 
275
            self._blobs.append((obj.id, ie.file_id, ie.revision))
 
276
        else:
 
277
            raise AssertionError
 
278
 
 
279
    def finish(self):
 
280
        if self._commit is None:
 
281
            raise AssertionError("No commit object added")
 
282
        self.db.executemany(
 
283
            "replace into trees (sha1, fileid, revid) values (?, ?, ?)",
 
284
            self._trees)
 
285
        self.db.executemany(
 
286
            "replace into blobs (sha1, fileid, revid) values (?, ?, ?)",
 
287
            self._blobs)
 
288
        self.db.execute(
 
289
            "replace into commits (sha1, revid, tree_sha) values (?, ?, ?)",
 
290
            (self._commit.id, self.revid, self._commit.tree))
 
291
        return self._commit
 
292
 
 
293
 
 
294
SqliteBzrGitCache = lambda p: BzrGitCache(SqliteGitShaMap(p), None, SqliteCacheUpdater)
 
295
 
 
296
 
 
297
class SqliteGitCacheFormat(BzrGitCacheFormat):
 
298
 
 
299
    def get_format_string(self):
 
300
        return 'bzr-git sha map version 1 using sqlite\n'
 
301
 
 
302
    def open(self, transport):
 
303
        try:
 
304
            basepath = transport.local_abspath(".")
 
305
        except bzrlib.errors.NotLocalUrl:
 
306
            basepath = get_cache_dir()
 
307
        return SqliteBzrGitCache(os.path.join(basepath, "idmap.db"))
 
308
 
 
309
 
 
310
class SqliteGitShaMap(GitShaMap):
 
311
 
 
312
    def __init__(self, path=None):
 
313
        self.path = path
 
314
        if path is None:
 
315
            self.db = sqlite3.connect(":memory:")
 
316
        else:
 
317
            if not mapdbs().has_key(path):
 
318
                mapdbs()[path] = sqlite3.connect(path)
 
319
            self.db = mapdbs()[path]
 
320
        self.db.text_factory = str
 
321
        self.db.executescript("""
 
322
        create table if not exists commits(
 
323
            sha1 text not null check(length(sha1) == 40),
 
324
            revid text not null,
 
325
            tree_sha text not null check(length(tree_sha) == 40)
 
326
        );
 
327
        create index if not exists commit_sha1 on commits(sha1);
 
328
        create unique index if not exists commit_revid on commits(revid);
 
329
        create table if not exists blobs(
 
330
            sha1 text not null check(length(sha1) == 40),
 
331
            fileid text not null,
 
332
            revid text not null
 
333
        );
 
334
        create index if not exists blobs_sha1 on blobs(sha1);
 
335
        create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
 
336
        create table if not exists trees(
 
337
            sha1 text unique not null check(length(sha1) == 40),
 
338
            fileid text not null,
 
339
            revid text not null
 
340
        );
 
341
        create unique index if not exists trees_sha1 on trees(sha1);
 
342
        create unique index if not exists trees_fileid_revid on trees(fileid, revid);
 
343
""")
 
344
 
 
345
    def __repr__(self):
 
346
        return "%s(%r)" % (self.__class__.__name__, self.path)
 
347
    
 
348
    def lookup_commit(self, revid):
 
349
        row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
 
350
        if row is not None:
 
351
            return row[0]
 
352
        raise KeyError
 
353
 
 
354
    def commit_write_group(self):
 
355
        self.db.commit()
 
356
 
 
357
    def lookup_blob_id(self, fileid, revision):
 
358
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revision)).fetchone()
 
359
        if row is not None:
 
360
            return row[0]
 
361
        raise KeyError(fileid)
 
362
 
 
363
    def lookup_tree_id(self, fileid, revision):
 
364
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid, self.revid)).fetchone()
 
365
        if row is not None:
 
366
            return row[0]
 
367
        raise KeyError(fileid)
 
368
 
 
369
    def lookup_git_sha(self, sha):
 
370
        """Lookup a Git sha in the database.
 
371
 
 
372
        :param sha: Git object sha
 
373
        :return: (type, type_data) with type_data:
 
374
            revision: revid, tree sha
 
375
        """
 
376
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
 
377
        if row is not None:
 
378
            return ("commit", row)
 
379
        row = self.db.execute("select fileid, revid from blobs where sha1 = ?", (sha,)).fetchone()
 
380
        if row is not None:
 
381
            return ("blob", row)
 
382
        row = self.db.execute("select fileid, revid from trees where sha1 = ?", (sha,)).fetchone()
 
383
        if row is not None:
 
384
            return ("tree", row)
 
385
        raise KeyError(sha)
 
386
 
 
387
    def revids(self):
 
388
        """List the revision ids known."""
 
389
        return (row for (row,) in self.db.execute("select revid from commits"))
 
390
 
 
391
    def sha1s(self):
 
392
        """List the SHA1s."""
 
393
        for table in ("blobs", "commits", "trees"):
 
394
            for (sha,) in self.db.execute("select sha1 from %s" % table):
 
395
                yield sha
 
396
 
 
397
 
 
398
class TdbCacheUpdater(CacheUpdater):
 
399
 
 
400
    def __init__(self, cache, rev):
 
401
        self.cache = cache
 
402
        self.db = cache.idmap.db
 
403
        self.revid = rev.revision_id
 
404
        self.parent_revids = rev.parent_ids
 
405
        self._commit = None
 
406
        self._entries = []
 
407
 
 
408
    def add_object(self, obj, ie):
 
409
        sha = obj.sha().digest()
 
410
        if obj.type_name == "commit":
 
411
            self.db["commit\0" + self.revid] = "\0".join((sha, obj.tree))
 
412
            type_data = (self.revid, obj.tree)
 
413
            self._commit = obj
 
414
            assert ie is None
 
415
        elif obj.type_name == "blob":
 
416
            self.db["\0".join(("blob", ie.file_id, ie.revision))] = sha
 
417
            type_data = (ie.file_id, ie.revision)
 
418
        elif obj.type_name == "tree":
 
419
            type_data = (ie.file_id, self.revid)
 
420
        else:
 
421
            raise AssertionError
 
422
        self.db["git\0" + sha] = "\0".join((obj.type_name, ) + type_data)
 
423
 
 
424
    def finish(self):
 
425
        if self._commit is None:
 
426
            raise AssertionError("No commit object added")
 
427
        return self._commit
 
428
 
 
429
 
 
430
TdbBzrGitCache = lambda p: BzrGitCache(TdbGitShaMap(p), None, TdbCacheUpdater)
 
431
 
 
432
class TdbGitCacheFormat(BzrGitCacheFormat):
 
433
 
 
434
    def get_format_string(self):
 
435
        return 'bzr-git sha map version 3 using tdb\n'
 
436
 
 
437
    def open(self, transport):
 
438
        try:
 
439
            basepath = transport.local_abspath(".")
 
440
        except bzrlib.errors.NotLocalUrl:
 
441
            basepath = get_cache_dir()
 
442
        try:
 
443
            return TdbBzrGitCache(os.path.join(basepath, "idmap.tdb"))
 
444
        except ImportError:
 
445
            raise ImportError(
 
446
                "Unable to open existing bzr-git cache because 'tdb' is not "
 
447
                "installed.")
 
448
 
 
449
 
 
450
class TdbGitShaMap(GitShaMap):
 
451
    """SHA Map that uses a TDB database.
 
452
 
 
453
    Entries:
 
454
 
 
455
    "git <sha1>" -> "<type> <type-data1> <type-data2>"
 
456
    "commit revid" -> "<sha1> <tree-id>"
 
457
    "tree fileid revid" -> "<sha1>"
 
458
    "blob fileid revid" -> "<sha1>"
 
459
    """
 
460
 
 
461
    TDB_MAP_VERSION = 3
 
462
    TDB_HASH_SIZE = 50000
 
463
 
 
464
    def __init__(self, path=None):
 
465
        import tdb
 
466
        self.path = path
 
467
        if path is None:
 
468
            self.db = {}
 
469
        else:
 
470
            if not mapdbs().has_key(path):
 
471
                mapdbs()[path] = tdb.Tdb(path, self.TDB_HASH_SIZE, tdb.DEFAULT,
 
472
                                          os.O_RDWR|os.O_CREAT)
 
473
            self.db = mapdbs()[path]
 
474
        try:
 
475
            if int(self.db["version"]) not in (2, 3):
 
476
                trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
 
477
                              self.db["version"], self.TDB_MAP_VERSION)
 
478
                self.db.clear()
 
479
        except KeyError:
 
480
            pass
 
481
        self.db["version"] = str(self.TDB_MAP_VERSION)
 
482
 
 
483
    def start_write_group(self):
 
484
        """Start writing changes."""
 
485
        self.db.transaction_start()
 
486
 
 
487
    def commit_write_group(self):
 
488
        """Commit any pending changes."""
 
489
        self.db.transaction_commit()
 
490
 
 
491
    def abort_write_group(self):
 
492
        """Abort any pending changes."""
 
493
        self.db.transaction_cancel()
 
494
 
 
495
    def __repr__(self):
 
496
        return "%s(%r)" % (self.__class__.__name__, self.path)
 
497
 
 
498
    def lookup_commit(self, revid):
 
499
        return sha_to_hex(self.db["commit\0" + revid][:20])
 
500
 
 
501
    def lookup_blob_id(self, fileid, revision):
 
502
        return sha_to_hex(self.db["\0".join(("blob", fileid, revision))])
 
503
                
 
504
    def lookup_git_sha(self, sha):
 
505
        """Lookup a Git sha in the database.
 
506
 
 
507
        :param sha: Git object sha
 
508
        :return: (type, type_data) with type_data:
 
509
            revision: revid, tree sha
 
510
        """
 
511
        if len(sha) == 40:
 
512
            sha = hex_to_sha(sha)
 
513
        data = self.db["git\0" + sha].split("\0")
 
514
        return (data[0], (data[1], data[2]))
 
515
 
 
516
    def missing_revisions(self, revids):
 
517
        ret = set()
 
518
        for revid in revids:
 
519
            if self.db.get("commit\0" + revid) is None:
 
520
                ret.add(revid)
 
521
        return ret
 
522
 
 
523
    def revids(self):
 
524
        """List the revision ids known."""
 
525
        for key in self.db.iterkeys():
 
526
            if key.startswith("commit\0"):
 
527
                yield key[7:]
 
528
 
 
529
    def sha1s(self):
 
530
        """List the SHA1s."""
 
531
        for key in self.db.iterkeys():
 
532
            if key.startswith("git\0"):
 
533
                yield sha_to_hex(key[4:])
 
534
 
 
535
 
 
536
formats = registry.Registry()
 
537
formats.register(TdbGitCacheFormat().get_format_string(),
 
538
    TdbGitCacheFormat())
 
539
formats.register(SqliteGitCacheFormat().get_format_string(),
 
540
    SqliteGitCacheFormat())
 
541
try:
 
542
    import tdb
 
543
except ImportError:
 
544
    formats.register('default', SqliteGitCacheFormat())
 
545
else:
 
546
    formats.register('default', TdbGitCacheFormat())
 
547
 
 
548
 
 
549
def migrate_ancient_formats(repo_transport):
 
550
    if repo_transport.has("git.tdb"):
 
551
        TdbGitCacheFormat().initialize(repo_transport.clone("git"))
 
552
        repo_transport.rename("git.tdb", "git/idmap.tdb")
 
553
    elif repo_transport.has("git.db"):
 
554
        SqliteGitCacheFormat().initialize(repo_transport.clone("git"))
 
555
        repo_transport.rename("git.db", "git/idmap.db")
 
556
 
 
557
 
 
558
def from_repository(repository):
 
559
    repo_transport = getattr(repository, "_transport", None)
 
560
    if repo_transport is not None:
 
561
        # Migrate older cache formats
 
562
        try:
 
563
            repo_transport.mkdir("git")
 
564
        except bzrlib.errors.FileExists:
 
565
            pass
 
566
        else:
 
567
            migrate_ancient_formats(repo_transport)
 
568
    return BzrGitCacheFormat.from_repository(repository)