/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-09-10 13:13:15 UTC
  • mto: (0.200.602 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090910131315-6890xg58pl2jseml
Allow serving remote URLs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
    """Check that sqlite library is compatible.
47
47
 
48
48
    """
49
 
    if (sqlite3.sqlite_version_info[0] < 3 or
50
 
            (sqlite3.sqlite_version_info[0] == 3 and
 
49
    if (sqlite3.sqlite_version_info[0] < 3 or 
 
50
            (sqlite3.sqlite_version_info[0] == 3 and 
51
51
             sqlite3.sqlite_version_info[1] < 3)):
52
52
        trace.warning('Needs at least sqlite 3.3.x')
53
53
        raise bzrlib.errors.BzrError("incompatible sqlite library")
56
56
    try:
57
57
        import sqlite3
58
58
        check_pysqlite_version(sqlite3)
59
 
    except (ImportError, bzrlib.errors.BzrError), e:
 
59
    except (ImportError, bzrlib.errors.BzrError), e: 
60
60
        from pysqlite2 import dbapi2 as sqlite3
61
61
        check_pysqlite_version(sqlite3)
62
62
except:
110
110
        """List the revision ids known."""
111
111
        raise NotImplementedError(self.revids)
112
112
 
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
113
    def sha1s(self):
121
114
        """List the SHA1s."""
122
115
        raise NotImplementedError(self.sha1s)
123
116
 
124
 
    def start_write_group(self):
125
 
        """Start writing changes."""
126
 
 
127
 
    def commit_write_group(self):
 
117
    def commit(self):
128
118
        """Commit any pending changes."""
129
119
 
130
 
    def abort_write_group(self):
131
 
        """Abort any pending changes."""
132
 
 
133
120
 
134
121
class DictGitShaMap(GitShaMap):
135
122
 
172
159
        else:
173
160
            if not mapdbs().has_key(path):
174
161
                mapdbs()[path] = sqlite3.connect(path)
175
 
            self.db = mapdbs()[path]
176
 
        self.db.text_factory = str
 
162
            self.db = mapdbs()[path]    
177
163
        self.db.executescript("""
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
 
        );
 
164
        create table if not exists commits(sha1 text, revid text, tree_sha text);
183
165
        create index if not exists commit_sha1 on commits(sha1);
184
166
        create unique index if not exists commit_revid on commits(revid);
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
 
        );
 
167
        create table if not exists blobs(sha1 text, fileid text, revid text);
190
168
        create index if not exists blobs_sha1 on blobs(sha1);
191
169
        create unique index if not exists blobs_fileid_revid on blobs(fileid, revid);
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
 
        );
 
170
        create table if not exists trees(sha1 text, fileid text, revid text);
197
171
        create index if not exists trees_sha1 on trees(sha1);
198
172
        create unique index if not exists trees_fileid_revid on trees(fileid, revid);
199
173
""")
211
185
    def lookup_commit(self, revid):
212
186
        row = self.db.execute("select sha1 from commits where revid = ?", (revid,)).fetchone()
213
187
        if row is not None:
214
 
            return row[0]
 
188
            return row[0].encode("utf-8")
215
189
        raise KeyError
216
190
 
217
 
    def commit_write_group(self):
 
191
    def commit(self):
218
192
        self.db.commit()
219
193
 
220
194
    def add_entries(self, entries):
223
197
        for sha, type, type_data in entries:
224
198
            assert isinstance(type_data[0], str)
225
199
            assert isinstance(type_data[1], str)
226
 
            entry = (sha, type_data[0], type_data[1])
 
200
            entry = (sha.decode("utf-8"), type_data[0].decode("utf-8"), 
 
201
                     type_data[1].decode("utf-8"))
227
202
            if type == "tree":
228
203
                trees.append(entry)
229
204
            elif type == "blob":
240
215
        """Add a new entry to the database.
241
216
        """
242
217
        assert isinstance(type_data, tuple)
243
 
        if sha is None:
244
 
            return
245
218
        assert isinstance(sha, str), "type was %r" % sha
246
219
        if type == "commit":
247
220
            self.db.execute("replace into commits (sha1, revid, tree_sha) values (?, ?, ?)", (sha, type_data[0], type_data[1]))
254
227
        row = self.db.execute("select sha1 from trees where fileid = ? and revid = ?", (fileid,revid)).fetchone()
255
228
        if row is None:
256
229
            raise KeyError((fileid, revid))
257
 
        return row[0]
 
230
        return row[0].encode("utf-8")
258
231
 
259
232
    def lookup_blob(self, fileid, revid):
260
233
        row = self.db.execute("select sha1 from blobs where fileid = ? and revid = ?", (fileid, revid)).fetchone()
261
234
        if row is None:
262
235
            raise KeyError((fileid, revid))
263
 
        return row[0]
 
236
        return row[0].encode("utf-8")
264
237
 
265
238
    def lookup_git_sha(self, sha):
266
239
        """Lookup a Git sha in the database.
270
243
            revision: revid, tree sha
271
244
        """
272
245
        def format(type, row):
273
 
            return (type, (row[0], row[1]))
 
246
            return (type, (row[0].encode("utf-8"), row[1].encode("utf-8")))
274
247
        row = self.db.execute("select revid, tree_sha from commits where sha1 = ?", (sha,)).fetchone()
275
248
        if row is not None:
276
249
            return format("commit", row)
285
258
    def revids(self):
286
259
        """List the revision ids known."""
287
260
        for row in self.db.execute("select revid from commits").fetchall():
288
 
            yield row[0]
 
261
            yield row[0].encode("utf-8")
289
262
 
290
263
    def sha1s(self):
291
264
        """List the SHA1s."""
292
265
        for table in ("blobs", "commits", "trees"):
293
266
            for row in self.db.execute("select sha1 from %s" % table).fetchall():
294
 
                yield row[0]
 
267
                yield row[0].encode("utf-8")
295
268
 
296
269
 
297
270
TDB_MAP_VERSION = 2
316
289
            self.db = {}
317
290
        else:
318
291
            if not mapdbs().has_key(path):
319
 
                mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT,
 
292
                mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT, 
320
293
                                          os.O_RDWR|os.O_CREAT)
321
 
            self.db = mapdbs()[path]
322
 
        try:
 
294
            self.db = mapdbs()[path]    
 
295
        if not "version" in self.db:
 
296
            self.db["version"] = str(TDB_MAP_VERSION)
 
297
        else:
323
298
            if int(self.db["version"]) != TDB_MAP_VERSION:
324
299
                trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
325
300
                              self.db["version"], TDB_MAP_VERSION)
326
301
                self.db.clear()
327
 
                self.db["version"] = str(TDB_MAP_VERSION)
328
 
        except KeyError:
329
302
            self.db["version"] = str(TDB_MAP_VERSION)
330
303
 
331
304
    @classmethod
341
314
    def lookup_commit(self, revid):
342
315
        return sha_to_hex(self.db["commit\0" + revid][:20])
343
316
 
 
317
    def commit(self):
 
318
        pass
 
319
 
344
320
    def add_entry(self, hexsha, type, type_data):
345
321
        """Add a new entry to the database.
346
322
        """
376
352
        data = self.db["git\0" + sha].split("\0")
377
353
        return (data[0], (data[1], data[2]))
378
354
 
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
 
 
386
355
    def revids(self):
387
356
        """List the revision ids known."""
388
357
        for key in self.db.iterkeys():