/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: 2010-02-12 13:31:58 UTC
  • mto: (0.200.718 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100212133158-3nazi7x9y8c13mg1
Cope with empty lines in packs info file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
from bzrlib import (
28
28
    trace,
29
29
    )
30
 
from bzrlib.errors import (
31
 
    NoSuchRevision,
32
 
    )
33
30
 
34
31
 
35
32
def get_cache_dir():
49
46
    """Check that sqlite library is compatible.
50
47
 
51
48
    """
52
 
    if (sqlite3.sqlite_version_info[0] < 3 or 
53
 
            (sqlite3.sqlite_version_info[0] == 3 and 
 
49
    if (sqlite3.sqlite_version_info[0] < 3 or
 
50
            (sqlite3.sqlite_version_info[0] == 3 and
54
51
             sqlite3.sqlite_version_info[1] < 3)):
55
 
        warning('Needs at least sqlite 3.3.x')
 
52
        trace.warning('Needs at least sqlite 3.3.x')
56
53
        raise bzrlib.errors.BzrError("incompatible sqlite library")
57
54
 
58
55
try:
59
56
    try:
60
57
        import sqlite3
61
58
        check_pysqlite_version(sqlite3)
62
 
    except (ImportError, bzrlib.errors.BzrError), e: 
 
59
    except (ImportError, bzrlib.errors.BzrError), e:
63
60
        from pysqlite2 import dbapi2 as sqlite3
64
61
        check_pysqlite_version(sqlite3)
65
62
except:
66
 
    warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
 
63
    trace.warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
67
64
            'module')
68
65
    raise bzrlib.errors.BzrError("missing sqlite library")
69
66
 
113
110
        """List the revision ids known."""
114
111
        raise NotImplementedError(self.revids)
115
112
 
116
 
    def sha1s(Self):
 
113
    def missing_revisions(self, revids):
 
114
        """Return set of all the revisions that are not present."""
 
115
        present_revids = set(self.revids())
 
116
        if not isinstance(revids, set):
 
117
            revids = set(revids)
 
118
        return revids - present_revids
 
119
 
 
120
    def sha1s(self):
117
121
        """List the SHA1s."""
118
122
        raise NotImplementedError(self.sha1s)
119
123
 
120
 
    def commit(self):
 
124
    def start_write_group(self):
 
125
        """Start writing changes."""
 
126
 
 
127
    def commit_write_group(self):
121
128
        """Commit any pending changes."""
122
129
 
 
130
    def abort_write_group(self):
 
131
        """Abort any pending changes."""
 
132
 
123
133
 
124
134
class DictGitShaMap(GitShaMap):
125
135
 
162
172
        else:
163
173
            if not mapdbs().has_key(path):
164
174
                mapdbs()[path] = sqlite3.connect(path)
165
 
            self.db = mapdbs()[path]    
 
175
            self.db = mapdbs()[path]
 
176
        self.db.text_factory = str
166
177
        self.db.executescript("""
167
 
        create table if not exists commits(sha1 text, revid text, tree_sha text);
 
178
        create table if not exists commits(
 
179
            sha1 text not null check(length(sha1) == 40),
 
180
            revid text not null,
 
181
            tree_sha text not null check(length(tree_sha) == 40)
 
182
        );
168
183
        create index if not exists commit_sha1 on commits(sha1);
169
184
        create unique index if not exists commit_revid on commits(revid);
170
 
        create table if not exists blobs(sha1 text, fileid text, revid text);
 
185
        create table if not exists blobs(
 
186
            sha1 text not null check(length(sha1) == 40),
 
187
            fileid text not null,
 
188
            revid text not null
 
189
        );
171
190
        create index if not exists blobs_sha1 on blobs(sha1);
172
191
        create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
173
 
        create table if not exists trees(sha1 text, fileid text, revid text);
 
192
        create table if not exists trees(
 
193
            sha1 text not null check(length(sha1) == 40),
 
194
            fileid text not null,
 
195
            revid text not null
 
196
        );
174
197
        create index if not exists trees_sha1 on trees(sha1);
175
198
        create unique index if not exists trees_fileid_revid on trees(fileid, revid);
176
199
""")
188
211
    def lookup_commit(self, revid):
189
212
        row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
190
213
        if row is not None:
191
 
            return row[0].encode("utf-8")
 
214
            return row[0]
192
215
        raise KeyError
193
216
 
194
 
    def commit(self):
 
217
    def commit_write_group(self):
195
218
        self.db.commit()
196
219
 
197
220
    def add_entries(self, entries):
200
223
        for sha, type, type_data in entries:
201
224
            assert isinstance(type_data[0], str)
202
225
            assert isinstance(type_data[1], str)
203
 
            entry = (sha.decode("utf-8"), type_data[0].decode("utf-8"), 
204
 
                     type_data[1].decode("utf-8"))
 
226
            entry = (sha, type_data[0], type_data[1])
205
227
            if type == "tree":
206
228
                trees.append(entry)
207
229
            elif type == "blob":
218
240
        """Add a new entry to the database.
219
241
        """
220
242
        assert isinstance(type_data, tuple)
 
243
        if sha is None:
 
244
            return
221
245
        assert isinstance(sha, str), "type was %r" % sha
222
246
        if type == "commit":
223
247
            self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
230
254
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
231
255
        if row is None:
232
256
            raise KeyError((fileid, revid))
233
 
        return row[0].encode("utf-8")
 
257
        return row[0]
234
258
 
235
259
    def lookup_blob(self, fileid, revid):
236
260
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
237
261
        if row is None:
238
262
            raise KeyError((fileid, revid))
239
 
        return row[0].encode("utf-8")
 
263
        return row[0]
240
264
 
241
265
    def lookup_git_sha(self, sha):
242
266
        """Lookup a Git sha in the database.
246
270
            revision: revid, tree sha
247
271
        """
248
272
        def format(type, row):
249
 
            return (type, (row[0].encode("utf-8"), row[1].encode("utf-8")))
 
273
            return (type, (row[0], row[1]))
250
274
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
251
275
        if row is not None:
252
276
            return format("commit", row)
261
285
    def revids(self):
262
286
        """List the revision ids known."""
263
287
        for row in self.db.execute("select revid from commits").fetchall():
264
 
            yield row[0].encode("utf-8")
 
288
            yield row[0]
265
289
 
266
290
    def sha1s(self):
267
291
        """List the SHA1s."""
268
292
        for table in ("blobs", "commits", "trees"):
269
293
            for row in self.db.execute("select sha1 from %s" % table).fetchall():
270
 
                yield row[0].encode("utf-8")
 
294
                yield row[0]
271
295
 
272
296
 
273
297
TDB_MAP_VERSION = 2
274
 
TDB_HASH_SIZE = 10000
 
298
TDB_HASH_SIZE = 50000
275
299
 
276
300
 
277
301
class TdbGitShaMap(GitShaMap):
292
316
            self.db = {}
293
317
        else:
294
318
            if not mapdbs().has_key(path):
295
 
                mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT, 
 
319
                mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT,
296
320
                                          os.O_RDWR|os.O_CREAT)
297
 
            self.db = mapdbs()[path]    
298
 
        if not "version" in self.db:
299
 
            self.db["version"] = str(TDB_MAP_VERSION)
300
 
        else:
 
321
            self.db = mapdbs()[path]
 
322
        try:
301
323
            if int(self.db["version"]) != TDB_MAP_VERSION:
302
324
                trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
303
325
                              self.db["version"], TDB_MAP_VERSION)
304
326
                self.db.clear()
 
327
                self.db["version"] = str(TDB_MAP_VERSION)
 
328
        except KeyError:
305
329
            self.db["version"] = str(TDB_MAP_VERSION)
306
330
 
307
331
    @classmethod
317
341
    def lookup_commit(self, revid):
318
342
        return sha_to_hex(self.db["commit\0" + revid][:20])
319
343
 
320
 
    def commit(self):
321
 
        pass
322
 
 
323
 
    def add_entry(self, sha, type, type_data):
 
344
    def add_entry(self, hexsha, type, type_data):
324
345
        """Add a new entry to the database.
325
346
        """
326
 
        self.db["git\0" + hex_to_sha(sha)] = "\0".join((type, type_data[0], type_data[1]))
 
347
        if hexsha is None:
 
348
            sha = ""
 
349
        else:
 
350
            sha = hex_to_sha(hexsha)
 
351
            self.db["git\0" + sha] = "\0".join((type, type_data[0], type_data[1]))
327
352
        if type == "commit":
328
 
            self.db["commit\0" + type_data[0]] = "\0".join((hex_to_sha(sha), type_data[1]))
 
353
            self.db["commit\0" + type_data[0]] = "\0".join((sha, type_data[1]))
329
354
        else:
330
 
            self.db["\0".join((type, type_data[0], type_data[1]))] = hex_to_sha(sha)
 
355
            self.db["\0".join((type, type_data[0], type_data[1]))] = sha
331
356
 
332
357
    def lookup_tree(self, fileid, revid):
333
 
        return sha_to_hex(self.db["\0".join(("tree", fileid, revid))])
 
358
        sha = self.db["\0".join(("tree", fileid, revid))]
 
359
        if sha == "":
 
360
            return None
 
361
        else:
 
362
            return sha_to_hex(sha)
334
363
 
335
364
    def lookup_blob(self, fileid, revid):
336
365
        return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
342
371
        :return: (type, type_data) with type_data:
343
372
            revision: revid, tree sha
344
373
        """
345
 
        data = self.db["git\0" + hex_to_sha(sha)].split("\0")
 
374
        if len(sha) == 40:
 
375
            sha = hex_to_sha(sha)
 
376
        data = self.db["git\0" + sha].split("\0")
346
377
        return (data[0], (data[1], data[2]))
347
378
 
 
379
    def missing_revisions(self, revids):
 
380
        ret = set()
 
381
        for revid in revids:
 
382
            if self.db.get("commit\0" + revid) is None:
 
383
                ret.add(revid)
 
384
        return ret
 
385
 
348
386
    def revids(self):
349
387
        """List the revision ids known."""
350
388
        for key in self.db.iterkeys():