/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 tests for revspec.

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
        return _mapdbs.cache
76
76
 
77
77
 
 
78
class InventorySHAMap(object):
 
79
    """Maps inventory file ids to Git SHAs."""
 
80
 
 
81
    def lookup_blob(self, file_id, revision):
 
82
        """Retrieve a Git blob SHA by file id.
 
83
 
 
84
        :param file_id: File id of the file/symlink
 
85
        :param revision: revision in which the file was last changed.
 
86
        """
 
87
        raise NotImplementedError(self.lookup_blob)
 
88
 
 
89
    def lookup_tree(self, file_id):
 
90
        """Retrieve a Git tree SHA by file id.
 
91
        """
 
92
        raise NotImplementedError(self.lookup_tree)
 
93
 
 
94
 
78
95
class GitShaMap(object):
79
96
    """Git<->Bzr revision id mapping database."""
80
97
 
81
 
    def add_entry(self, sha, type, type_data):
 
98
    def _add_entry(self, sha, type, type_data):
82
99
        """Add a new entry to the database.
83
100
        """
84
 
        raise NotImplementedError(self.add_entry)
 
101
        raise NotImplementedError(self._add_entry)
85
102
 
86
 
    def add_entries(self, entries):
 
103
    def add_entries(self, revid, parent_revids, commit_sha, root_tree_sha, 
 
104
                    entries):
87
105
        """Add multiple new entries to the database.
88
106
        """
89
 
        for e in entries:
90
 
            self.add_entry(*e)
91
 
 
92
 
    def lookup_tree(self, fileid, revid):
93
 
        """Lookup the SHA of a git tree."""
94
 
        raise NotImplementedError(self.lookup_tree)
95
 
 
96
 
    def lookup_blob(self, fileid, revid):
97
 
        """Lookup a blob by the fileid it has in a bzr revision."""
98
 
        raise NotImplementedError(self.lookup_blob)
 
107
        for (fileid, kind, hexsha, revision) in entries:
 
108
            self._add_entry(hexsha, kind, (fileid, revision))
 
109
        self._add_entry(commit_sha, "commit", (revid, root_tree_sha))
 
110
 
 
111
    def get_inventory_sha_map(self, revid):
 
112
        """Return the inventory SHA map for a revision.
 
113
 
 
114
        :param revid: Revision to fetch the map for
 
115
        :return: A `InventorySHAMap`
 
116
        """
 
117
        raise NotImplementedError(self.get_inventory_sha_map)
99
118
 
100
119
    def lookup_git_sha(self, sha):
101
120
        """Lookup a Git sha in the database.
102
 
 
103
121
        :param sha: Git object sha
104
122
        :return: (type, type_data) with type_data:
105
123
            revision: revid, tree sha
134
152
class DictGitShaMap(GitShaMap):
135
153
 
136
154
    def __init__(self):
137
 
        self.dict = {}
138
 
 
139
 
    def add_entry(self, sha, type, type_data):
140
 
        self.dict[sha] = (type, type_data)
 
155
        self._by_sha = {}
 
156
        self._by_fileid = {}
 
157
 
 
158
    def _add_entry(self, sha, type, type_data):
 
159
        self._by_sha[sha] = (type, type_data)
 
160
        if type in ("blob", "tree"):
 
161
            self._by_fileid.setdefault(type_data[1], {})[type_data[0]] = sha
 
162
 
 
163
    def get_inventory_sha_map(self, revid):
 
164
 
 
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):
 
172
                return self._base._by_fileid[revision][fileid]
 
173
 
 
174
            def lookup_tree(self, fileid):
 
175
                return self._base._by_fileid[self.revid][fileid]
 
176
 
 
177
        return DictInventorySHAMap(self, revid)
141
178
 
142
179
    def lookup_git_sha(self, sha):
143
 
        return self.dict[sha]
144
 
 
145
 
    def lookup_tree(self, fileid, revid):
146
 
        for k, v in self.dict.iteritems():
147
 
            if v == ("tree", (fileid, revid)):
148
 
                return k
149
 
        raise KeyError((fileid, revid))
150
 
 
151
 
    def lookup_blob(self, fileid, revid):
152
 
        for k, v in self.dict.iteritems():
153
 
            if v == ("blob", (fileid, revid)):
154
 
                return k
155
 
        raise KeyError((fileid, revid))
 
180
        return self._by_sha[sha]
156
181
 
157
182
    def revids(self):
158
 
        for key, (type, type_data) in self.dict.iteritems():
 
183
        for key, (type, type_data) in self._by_sha.iteritems():
159
184
            if type == "commit":
160
185
                yield type_data[0]
161
186
 
162
187
    def sha1s(self):
163
 
        return self.dict.iterkeys()
 
188
        return self._by_sha.iterkeys()
164
189
 
165
190
 
166
191
class SqliteGitShaMap(GitShaMap):
190
215
        create index if not exists blobs_sha1 on blobs(sha1);
191
216
        create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
192
217
        create table if not exists trees(
193
 
            sha1 text not null check(length(sha1) == 40),
 
218
            sha1 text unique not null check(length(sha1) == 40),
194
219
            fileid text not null,
195
220
            revid text not null
196
221
        );
197
 
        create index if not exists trees_sha1 on trees(sha1);
 
222
        create unique index if not exists trees_sha1 on trees(sha1);
198
223
        create unique index if not exists trees_fileid_revid on trees(fileid, revid);
199
224
""")
200
225
 
 
226
    def __repr__(self):
 
227
        return "%s(%r)" % (self.__class__.__name__, self.path)
 
228
    
201
229
    @classmethod
202
230
    def from_repository(cls, repository):
203
231
        try:
217
245
    def commit_write_group(self):
218
246
        self.db.commit()
219
247
 
220
 
    def add_entries(self, entries):
 
248
    def add_entries(self, revid, parent_revids, commit_sha, root_tree_sha,
 
249
                    entries):
221
250
        trees = []
222
251
        blobs = []
223
 
        for sha, type, type_data in entries:
224
 
            assert isinstance(type_data[0], str)
225
 
            assert isinstance(type_data[1], str)
226
 
            entry = (sha, type_data[0], type_data[1])
227
 
            if type == "tree":
228
 
                trees.append(entry)
229
 
            elif type == "blob":
230
 
                blobs.append(entry)
 
252
        for (fileid, kind, hexsha, revision) in entries:
 
253
            if kind is None:
 
254
                continue
 
255
            if kind == "tree":
 
256
                trees.append((hexsha, fileid, revid))
 
257
            elif kind == "blob":
 
258
                blobs.append((hexsha, fileid, revision))
231
259
            else:
232
260
                raise AssertionError
233
261
        if trees:
234
262
            self.db.executemany("replace into trees (sha1, fileid, revid) values (?, ?, ?)", trees)
235
263
        if blobs:
236
264
            self.db.executemany("replace into blobs (sha1, fileid, revid) values (?, ?, ?)", blobs)
237
 
 
238
 
 
239
 
    def add_entry(self, sha, type, type_data):
 
265
        self._add_entry(commit_sha, "commit", (revid, root_tree_sha))
 
266
 
 
267
    def _add_entry(self, sha, type, type_data):
240
268
        """Add a new entry to the database.
241
269
        """
242
270
        assert isinstance(type_data, tuple)
 
271
        if sha is None:
 
272
            return
243
273
        assert isinstance(sha, str), "type was %r" % sha
244
274
        if type == "commit":
245
275
            self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
248
278
        else:
249
279
            raise AssertionError("Unknown type %s" % type)
250
280
 
251
 
    def lookup_tree(self, fileid, revid):
252
 
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
253
 
        if row is None:
254
 
            raise KeyError((fileid, revid))
255
 
        return row[0]
256
 
 
257
 
    def lookup_blob(self, fileid, revid):
258
 
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
259
 
        if row is None:
260
 
            raise KeyError((fileid, revid))
261
 
        return row[0]
 
281
    def get_inventory_sha_map(self, revid):
 
282
        class SqliteInventorySHAMap(InventorySHAMap):
 
283
 
 
284
            def __init__(self, db, revid):
 
285
                self.db = db
 
286
                self.revid = revid
 
287
 
 
288
            def lookup_blob(self, fileid, revision):
 
289
                row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revision)).fetchone()
 
290
                if row is not None:
 
291
                    return row[0]
 
292
                raise KeyError(fileid)
 
293
 
 
294
            def lookup_tree(self, fileid):
 
295
                row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid, self.revid)).fetchone()
 
296
                if row is not None:
 
297
                    return row[0]
 
298
                raise KeyError(fileid)
 
299
 
 
300
        return SqliteInventorySHAMap(self.db, revid)
262
301
 
263
302
    def lookup_git_sha(self, sha):
264
303
        """Lookup a Git sha in the database.
282
321
 
283
322
    def revids(self):
284
323
        """List the revision ids known."""
285
 
        for row in self.db.execute("select revid from commits").fetchall():
286
 
            yield row[0]
 
324
        return (row for (row,) in self.db.execute("select revid from commits"))
287
325
 
288
326
    def sha1s(self):
289
327
        """List the SHA1s."""
290
328
        for table in ("blobs", "commits", "trees"):
291
 
            for row in self.db.execute("select sha1 from %s" % table).fetchall():
292
 
                yield row[0]
293
 
 
294
 
 
295
 
TDB_MAP_VERSION = 2
 
329
            trace.note(table)
 
330
            for (row,) in self.db.execute("select sha1 from %s" % table):
 
331
                yield row
 
332
 
 
333
 
 
334
TDB_MAP_VERSION = 3
296
335
TDB_HASH_SIZE = 50000
297
336
 
298
337
 
318
357
                                          os.O_RDWR|os.O_CREAT)
319
358
            self.db = mapdbs()[path]
320
359
        try:
321
 
            if int(self.db["version"]) != TDB_MAP_VERSION:
 
360
            if int(self.db["version"]) not in (2, 3):
322
361
                trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
323
362
                              self.db["version"], TDB_MAP_VERSION)
324
363
                self.db.clear()
325
 
                self.db["version"] = str(TDB_MAP_VERSION)
326
364
        except KeyError:
327
 
            self.db["version"] = str(TDB_MAP_VERSION)
 
365
            pass
 
366
        self.db["version"] = str(TDB_MAP_VERSION)
 
367
 
 
368
    def start_write_group(self):
 
369
        """Start writing changes."""
 
370
        self.db.transaction_start()
 
371
 
 
372
    def commit_write_group(self):
 
373
        """Commit any pending changes."""
 
374
        self.db.transaction_commit()
 
375
 
 
376
    def abort_write_group(self):
 
377
        """Abort any pending changes."""
 
378
        self.db.transaction_cancel()
 
379
 
 
380
    def __repr__(self):
 
381
        return "%s(%r)" % (self.__class__.__name__, self.path)
328
382
 
329
383
    @classmethod
330
384
    def from_repository(cls, repository):
339
393
    def lookup_commit(self, revid):
340
394
        return sha_to_hex(self.db["commit\0" + revid][:20])
341
395
 
342
 
    def add_entry(self, hexsha, type, type_data):
 
396
    def _add_entry(self, hexsha, type, type_data):
343
397
        """Add a new entry to the database.
344
398
        """
345
399
        if hexsha is None:
349
403
            self.db["git\0" + sha] = "\0".join((type, type_data[0], type_data[1]))
350
404
        if type == "commit":
351
405
            self.db["commit\0" + type_data[0]] = "\0".join((sha, type_data[1]))
352
 
        else:
353
 
            self.db["\0".join((type, type_data[0], type_data[1]))] = sha
354
 
 
355
 
    def lookup_tree(self, fileid, revid):
356
 
        sha = self.db["\0".join(("tree", fileid, revid))]
357
 
        if sha == "":
358
 
            return None
359
 
        else:
360
 
            return sha_to_hex(sha)
361
 
 
362
 
    def lookup_blob(self, fileid, revid):
363
 
        return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
 
406
        elif type == "blob":
 
407
            self.db["\0".join(("blob", type_data[0], type_data[1]))] = sha
 
408
 
 
409
    def get_inventory_sha_map(self, revid):
 
410
 
 
411
        class TdbInventorySHAMap(InventorySHAMap):
 
412
 
 
413
            def __init__(self, db, revid):
 
414
                self.db = db
 
415
                self.revid = revid
 
416
 
 
417
            def lookup_blob(self, fileid, revision):
 
418
                return sha_to_hex(self.db["\0".join(("blob", fileid, revision))])
 
419
                
 
420
        return TdbInventorySHAMap(self.db, revid)
364
421
 
365
422
    def lookup_git_sha(self, sha):
366
423
        """Lookup a Git sha in the database.
392
449
        for key in self.db.iterkeys():
393
450
            if key.startswith("git\0"):
394
451
                yield sha_to_hex(key[4:])
 
452
 
 
453
 
 
454
def from_repository(repository):
 
455
    try:
 
456
        return TdbGitShaMap.from_repository(repository)
 
457
    except ImportError:
 
458
        return SqliteGitShaMap.from_repository(repository)