/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

Clean up trailing whitespace.

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":
230
252
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
231
253
        if row is None:
232
254
            raise KeyError((fileid, revid))
233
 
        return row[0].encode("utf-8")
 
255
        return row[0]
234
256
 
235
257
    def lookup_blob(self, fileid, revid):
236
258
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
237
259
        if row is None:
238
260
            raise KeyError((fileid, revid))
239
 
        return row[0].encode("utf-8")
 
261
        return row[0]
240
262
 
241
263
    def lookup_git_sha(self, sha):
242
264
        """Lookup a Git sha in the database.
246
268
            revision: revid, tree sha
247
269
        """
248
270
        def format(type, row):
249
 
            return (type, (row[0].encode("utf-8"), row[1].encode("utf-8")))
 
271
            return (type, (row[0], row[1]))
250
272
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
251
273
        if row is not None:
252
274
            return format("commit", row)
261
283
    def revids(self):
262
284
        """List the revision ids known."""
263
285
        for row in self.db.execute("select revid from commits").fetchall():
264
 
            yield row[0].encode("utf-8")
 
286
            yield row[0]
265
287
 
266
288
    def sha1s(self):
267
289
        """List the SHA1s."""
268
290
        for table in ("blobs", "commits", "trees"):
269
291
            for row in self.db.execute("select sha1 from %s" % table).fetchall():
270
 
                yield row[0].encode("utf-8")
 
292
                yield row[0]
271
293
 
272
294
 
273
295
TDB_MAP_VERSION = 2
274
 
TDB_HASH_SIZE = 10000
 
296
TDB_HASH_SIZE = 50000
275
297
 
276
298
 
277
299
class TdbGitShaMap(GitShaMap):
292
314
            self.db = {}
293
315
        else:
294
316
            if not mapdbs().has_key(path):
295
 
                mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT, 
 
317
                mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT,
296
318
                                          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:
 
319
            self.db = mapdbs()[path]
 
320
        try:
301
321
            if int(self.db["version"]) != TDB_MAP_VERSION:
302
322
                trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
303
323
                              self.db["version"], TDB_MAP_VERSION)
304
324
                self.db.clear()
 
325
                self.db["version"] = str(TDB_MAP_VERSION)
 
326
        except KeyError:
305
327
            self.db["version"] = str(TDB_MAP_VERSION)
306
328
 
307
329
    @classmethod
317
339
    def lookup_commit(self, revid):
318
340
        return sha_to_hex(self.db["commit\0" + revid][:20])
319
341
 
320
 
    def commit(self):
321
 
        pass
322
 
 
323
 
    def add_entry(self, sha, type, type_data):
 
342
    def add_entry(self, hexsha, type, type_data):
324
343
        """Add a new entry to the database.
325
344
        """
326
 
        self.db["git\0" + hex_to_sha(sha)] = "\0".join((type, type_data[0], type_data[1]))
 
345
        if hexsha is None:
 
346
            sha = ""
 
347
        else:
 
348
            sha = hex_to_sha(hexsha)
 
349
            self.db["git\0" + sha] = "\0".join((type, type_data[0], type_data[1]))
327
350
        if type == "commit":
328
 
            self.db["commit\0" + type_data[0]] = "\0".join((hex_to_sha(sha), type_data[1]))
 
351
            self.db["commit\0" + type_data[0]] = "\0".join((sha, type_data[1]))
329
352
        else:
330
 
            self.db["\0".join((type, type_data[0], type_data[1]))] = hex_to_sha(sha)
 
353
            self.db["\0".join((type, type_data[0], type_data[1]))] = sha
331
354
 
332
355
    def lookup_tree(self, fileid, revid):
333
 
        return sha_to_hex(self.db["\0".join(("tree", 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)
334
361
 
335
362
    def lookup_blob(self, fileid, revid):
336
363
        return sha_to_hex(self.db["\0".join(("blob", fileid, revid))])
342
369
        :return: (type, type_data) with type_data:
343
370
            revision: revid, tree sha
344
371
        """
345
 
        data = self.db["git\0" + hex_to_sha(sha)].split("\0")
 
372
        if len(sha) == 40:
 
373
            sha = hex_to_sha(sha)
 
374
        data = self.db["git\0" + sha].split("\0")
346
375
        return (data[0], (data[1], data[2]))
347
376
 
 
377
    def missing_revisions(self, revids):
 
378
        ret = set()
 
379
        for revid in revids:
 
380
            if self.db.get("commit\0" + revid) is None:
 
381
                ret.add(revid)
 
382
        return ret
 
383
 
348
384
    def revids(self):
349
385
        """List the revision ids known."""
350
386
        for key in self.db.iterkeys():