49
46
"""Check that sqlite library is compatible.
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")
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)
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 '
68
65
raise bzrlib.errors.BzrError("missing sqlite library")
113
110
"""List the revision ids known."""
114
111
raise NotImplementedError(self.revids)
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):
118
return revids - present_revids
117
121
"""List the SHA1s."""
118
122
raise NotImplementedError(self.sha1s)
124
def start_write_group(self):
125
"""Start writing changes."""
127
def commit_write_group(self):
121
128
"""Commit any pending changes."""
130
def abort_write_group(self):
131
"""Abort any pending changes."""
124
134
class DictGitShaMap(GitShaMap):
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),
181
tree_sha text not null check(length(tree_sha) == 40)
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,
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,
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);
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")
217
def commit_write_group(self):
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()
232
254
raise KeyError((fileid, revid))
233
return row[0].encode("utf-8")
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()
238
260
raise KeyError((fileid, revid))
239
return row[0].encode("utf-8")
241
263
def lookup_git_sha(self, sha):
242
264
"""Lookup a Git sha in the database.
246
268
revision: revid, tree sha
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")
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")
273
295
TDB_MAP_VERSION = 2
274
TDB_HASH_SIZE = 10000
296
TDB_HASH_SIZE = 50000
277
299
class TdbGitShaMap(GitShaMap):
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)
319
self.db = mapdbs()[path]
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)
325
self.db["version"] = str(TDB_MAP_VERSION)
305
327
self.db["version"] = str(TDB_MAP_VERSION)
317
339
def lookup_commit(self, revid):
318
340
return sha_to_hex(self.db["commit\0" + revid][:20])
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.
326
self.db["git\0" + hex_to_sha(sha)] = "\0".join((type, type_data[0], type_data[1]))
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]))
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
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))]
360
return sha_to_hex(sha)
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
345
data = self.db["git\0" + hex_to_sha(sha)].split("\0")
373
sha = hex_to_sha(sha)
374
data = self.db["git\0" + sha].split("\0")
346
375
return (data[0], (data[1], data[2]))
377
def missing_revisions(self, revids):
380
if self.db.get("commit\0" + revid) is None:
348
384
def revids(self):
349
385
"""List the revision ids known."""
350
386
for key in self.db.iterkeys():