/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 Tdb database backend.

Show diffs side-by-side

added added

removed removed

Lines of Context:
241
241
        for table in ("blobs", "commits", "trees"):
242
242
            for row in self.db.execute("select sha1 from %s" % table).fetchall():
243
243
                yield row[0].encode("utf-8")
 
244
 
 
245
 
 
246
class TdbGitShaMap(GitShaMap):
 
247
    """SHA Map that uses a TDB database.
 
248
 
 
249
    Entries:
 
250
 
 
251
    commit/sha1 -> revid tree-id
 
252
    tree/sha1 -> revid file-id
 
253
    blob/sha1 -> revid file-id
 
254
    """
 
255
 
 
256
    def __init__(self, path=None):
 
257
        import tdb
 
258
        self.path = path
 
259
        if path is None:
 
260
            self.db = {}
 
261
        else:
 
262
            if not mapdbs().has_key(path):
 
263
                mapdbs()[path] = tdb.open(path, 0, tdb.DEFAULT, 
 
264
                                          os.O_RDWR|os.O_CREAT)
 
265
            self.db = mapdbs()[path]    
 
266
 
 
267
    @classmethod
 
268
    def from_repository(cls, repository):
 
269
        return cls(os.path.join(repository._transport.local_abspath("."), "git.tdb"))
 
270
 
 
271
    def _parent_lookup(self, revid):
 
272
        row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
 
273
        if row is not None:
 
274
            return row[0].encode("utf-8")
 
275
        raise KeyError
 
276
 
 
277
    def commit(self):
 
278
        pass
 
279
 
 
280
    def add_entry(self, sha, type, type_data):
 
281
        """Add a new entry to the database.
 
282
        """
 
283
        self.db["%s/%s" % (type, sha)] = "%s %s" % type_data
 
284
 
 
285
    def lookup_tree(self, fileid, revid):
 
286
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
 
287
        if row is None:
 
288
            raise KeyError((fileid, revid))
 
289
        return row[0].encode("utf-8")
 
290
 
 
291
    def lookup_blob(self, fileid, revid):
 
292
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
 
293
        if row is None:
 
294
            raise KeyError((fileid, revid))
 
295
        return row[0].encode("utf-8")
 
296
 
 
297
    def lookup_git_sha(self, sha):
 
298
        """Lookup a Git sha in the database.
 
299
 
 
300
        :param sha: Git object sha
 
301
        :return: (type, type_data) with type_data:
 
302
            revision: revid, tree sha
 
303
        """
 
304
        for type in ("commit", "blob", "tree"):
 
305
            try:
 
306
                return (type, tuple(self.db["%s/%s" % (type, sha)].split(" ")))
 
307
            except KeyError:
 
308
                pass
 
309
        raise KeyError(sha)
 
310
 
 
311
    def revids(self):
 
312
        """List the revision ids known."""
 
313
        for key in self.db.iterkeys():
 
314
            if key.startswith("commit/"):
 
315
                yield self.db[key].split(" ")[0]
 
316
 
 
317
    def sha1s(self):
 
318
        """List the SHA1s."""
 
319
        for key in self.db.iterkeys():
 
320
            if (key.startswith("tree/") or key.startswith("blob/") or 
 
321
                key.startswith("commit/")):
 
322
                yield key.split("/")[1]