/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

  • Committer: Jelmer Vernooij
  • Date: 2009-05-16 21:13:11 UTC
  • mto: (0.200.527 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090516211311-clcz6rvpnz82cgi4
Store sha map more efficiently.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Map from Git sha's to Bazaar objects."""
18
18
 
 
19
from dulwich.objects import (
 
20
    sha_to_hex,
 
21
    hex_to_sha,
 
22
    )
19
23
import os
20
24
import threading
21
25
 
244
248
                yield row[0].encode("utf-8")
245
249
 
246
250
 
247
 
TDB_MAP_VERSION = 1
 
251
TDB_MAP_VERSION = 2
248
252
 
249
253
 
250
254
class TdbGitShaMap(GitShaMap):
271
275
        if not "version" in self.db:
272
276
            self.db["version"] = str(TDB_MAP_VERSION)
273
277
        else:
274
 
            assert int(self.db["version"]) == TDB_MAP_VERSION
 
278
            if int(self.db["version"]) != TDB_MAP_VERSION:
 
279
                trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
 
280
                              self.db["version"], TDB_MAP_VERSION)
 
281
                self.db.clear()
 
282
            self.db["version"] = str(TDB_MAP_VERSION)
275
283
 
276
284
    @classmethod
277
285
    def from_repository(cls, repository):
285
293
        return cls(os.path.join(config_dir(), "remote-git.tdb"))
286
294
 
287
295
    def lookup_commit(self, revid):
288
 
        return self.db["commit %s" % revid].split(" ")[0]
 
296
        return sha_to_hex(self.db["commit\0" + revid][:20])
289
297
 
290
298
    def commit(self):
291
299
        pass
293
301
    def add_entry(self, sha, type, type_data):
294
302
        """Add a new entry to the database.
295
303
        """
296
 
        self.db["git %s" % sha] = "%s %s %s" % (type, type_data[0], type_data[1])
 
304
        self.db["git\0" + hex_to_sha(sha)] = "\0".join((type, type_data[0], type_data[1]))
297
305
        if type == "commit":
298
 
            self.db["commit %s" % type_data[0]] = "%s %s" % (sha, type_data[1])
 
306
            self.db["commit\0" + type_data[0]] = "\0".join((hex_to_sha(sha), type_data[1]))
299
307
        else:
300
 
            self.db["%s %s %s" % (type, type_data[0], type_data[1])] = sha
 
308
            self.db["\0".join((type, type_data[0], type_data[1]))] = hex_to_sha(sha)
301
309
 
302
310
    def lookup_tree(self, fileid, revid):
303
 
        return self.db["tree %s %s" % (fileid, revid)]
 
311
        return sha_to_hex(self.db["\0".join(("tree", fileid, revid))])
304
312
 
305
313
    def lookup_blob(self, fileid, revid):
306
 
        return self.db["blob %s %s" % (fileid, revid)]
 
314
        return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
307
315
 
308
316
    def lookup_git_sha(self, sha):
309
317
        """Lookup a Git sha in the database.
312
320
        :return: (type, type_data) with type_data:
313
321
            revision: revid, tree sha
314
322
        """
315
 
        data = self.db["git %s" % sha].split(" ")
 
323
        data = self.db["git\0" + hex_to_sha(sha)].split("\0")
316
324
        return (data[0], (data[1], data[2]))
317
325
 
318
326
    def revids(self):
319
327
        """List the revision ids known."""
320
328
        for key in self.db.iterkeys():
321
 
            if key.startswith("commit "):
322
 
                yield key.split(" ")[1]
 
329
            if key.startswith("commit\0"):
 
330
                yield key[7:]
323
331
 
324
332
    def sha1s(self):
325
333
        """List the SHA1s."""
326
334
        for key in self.db.iterkeys():
327
 
            if key.startswith("git "):
328
 
                yield key.split(" ")[1]
 
335
            if key.startswith("git\0"):
 
336
                yield sha_to_hex(key[4:])