-
Notifications
You must be signed in to change notification settings - Fork 1
/
idmap.py
413 lines (329 loc) · 13.6 KB
/
idmap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# Copyright (C) 2009 Jelmer Vernooij <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Access to a map between Bazaar and Mercurial ids."""
from collections import defaultdict
import mercurial.node
import os
import threading
from breezy import (
errors,
registry,
trace,
)
from breezy.transport import (
get_transport,
)
from breezy.plugins.hg.mapping import (
mapping_registry,
)
def get_cache_dir():
try:
from xdg.BaseDirectory import xdg_cache_home
except ImportError:
from breezy.config import config_dir
ret = os.path.join(config_dir(), "hg")
else:
ret = os.path.join(xdg_cache_home, "bazaar", "hg")
if not os.path.isdir(ret):
os.makedirs(ret)
return ret
_mapdbs = threading.local()
def mapdbs():
"""Get a cache for this thread's db connections."""
try:
return _mapdbs.cache
except AttributeError:
_mapdbs.cache = {}
return _mapdbs.cache
TDB_MAP_VERSION = 1
TDB_HASH_SIZE = 50000
def check_pysqlite_version(sqlite3):
"""Check that sqlite library is compatible.
"""
if (sqlite3.sqlite_version_info[0] < 3 or
(sqlite3.sqlite_version_info[0] == 3 and
sqlite3.sqlite_version_info[1] < 3)):
trace.warning('Needs at least sqlite 3.3.x')
raise errors.BzrError("incompatible sqlite library")
try:
try:
import sqlite3
check_pysqlite_version(sqlite3)
except (ImportError, errors.BzrError) as e:
from pysqlite2 import dbapi2 as sqlite3
check_pysqlite_version(sqlite3)
except:
trace.warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 '
'module')
raise errors.BzrError("missing sqlite library")
class BzrHgIdmap(object):
"""Caching backend."""
def lookup_text_by_path_and_node(self, path, node):
raise NotImplementedError(self.lookup_text_by_path_and_node)
def lookup_revision_by_manifest_id(self):
raise NotImplementedError(self.lookup_revision_by_manifest_id)
def lookup_changeset_id_by_revid(self, revid):
raise NotImplementedError(self.lookup_changeset_id_by_revid)
def get_files_by_revid(self, revid):
raise NotImplementedError(self.get_files_by_revid)
def revids(self):
raise NotImplementedError(self.revids)
def insert_revision(self, revid, manifest_id, changeset_id, mapping):
raise NotImplementedError(self.insert_revision)
def insert_text(self, path, node, fileid, revision):
raise NotImplementedError(self.insert_text)
class MemoryIdmap(BzrHgIdmap):
"""In-memory idmap implementation."""
def __init__(self):
self._manifest_to_revid = {}
self._revid_to_changeset_id = {}
self._path_node_text_id = defaultdict(set)
def lookup_text_by_path_and_node(self, path, node):
return self._path_node_text_id[(path, node)]
def get_files_by_revid(self, revid):
raise KeyError(revid)
def lookup_revision_by_manifest_id(self, manifest_id):
return self._manifest_to_revid[manifest_id]
def lookup_changeset_id_by_revid(self, revid):
return self._revid_to_changeset_id[revid]
def revids(self):
return set(self._manifest_to_revid.values())
def insert_text(self, path, node, fileid, revision):
self._path_node_text_id[(path, node)].add((fileid, revision))
def insert_revision(self, revid, manifest_id, changeset_id, mapping):
if len(manifest_id) == 40:
manifest_id = mercurial.node.bin(manifest_id)
if len(changeset_id) == 40:
changeset_id = mercurial.node.bin(changeset_id)
self._manifest_to_revid[manifest_id] = revid
self._revid_to_changeset_id[revid] = changeset_id, mapping
class TdbIdmap(BzrHgIdmap):
"""Idmap that stores in tdb.
format:
manifest/<manifest_id> -> revid
"""
def __init__(self, path=None):
self.path = path
if path is None:
self.db = {}
else:
import tdb
if not mapdbs().has_key(path):
mapdbs()[path] = tdb.Tdb(path, TDB_HASH_SIZE, tdb.DEFAULT,
os.O_RDWR|os.O_CREAT)
self.db = mapdbs()[path]
try:
if int(self.db["version"]) != TDB_MAP_VERSION:
trace.warning("SHA Map is incompatible (%s -> %d), rebuilding database.",
self.db["version"], TDB_MAP_VERSION)
self.db.clear()
self.db["version"] = str(TDB_MAP_VERSION)
except KeyError:
self.db["version"] = str(TDB_MAP_VERSION)
def get_files_by_revid(self, revid):
raise KeyError(revid)
def lookup_revision_by_manifest_id(self, manifest_id):
return self.db["manifest/" + manifest_id]
def lookup_changeset_id_by_revid(self, revid):
text = self.db["revid/" + revid]
csid = text[:20]
return csid, mapping_registry.get(text[20:])
def revids(self):
ret = set()
for k in self.db.iterkeys():
if k.startswith("manifest/"):
ret.add(self.db[k])
return ret
def insert_revision(self, revid, manifest_id, changeset_id, mapping):
if len(manifest_id) == 40:
manifest_id = mercurial.node.bin(manifest_id)
if len(changeset_id) == 40:
changeset_id = mercurial.node.bin(changeset_id)
self.db["manifest/" + manifest_id] = revid
self.db["revid/" + revid] = changeset_id + str(mapping)
def lookup_text_by_path_and_node(self, path, node):
try:
for l in self.db["text/" + node + path].splitlines():
yield tuple(l.split(" "))
except KeyError:
return
def insert_text(self, path, node, fileid, revid):
self.db["text/" + node + path] = "%s %s\n" % (fileid, revid)
class SqliteIdmap(BzrHgIdmap):
"""Idmap that stores in SQLite.
"""
def __init__(self, path=None):
if path is None:
self.db = sqlite3.connect(":memory:")
self.db.text_factory = str
else:
if not mapdbs().has_key(path):
mapdbs()[path] = sqlite3.connect(path)
mapdbs()[path].text_factory = str
self.db = mapdbs()[path]
self.db.executescript("""
create table if not exists revision (
revid text not null,
csid text not null check(length(csid) == 40),
manifest_id text not null check(length(manifest_id) == 40),
mapping text not null
);
create unique index if not exists revision_revid on revision(revid);
create unique index if not exists revision_csid on revision(csid, mapping);
create index if not exists revision_manifest on revision(manifest_id);
create table if not exists text_map (
path blob not null,
node blob not null,
fileid blob not null,
revid blob not null
);
create unique index if not exists text_map_bzr_id on text_map (fileid, revid);
create index if not exists text_map_hg_id on text_map (path, node);
""")
def get_files_by_revid(self, revid):
raise KeyError(revid)
def lookup_revision_by_manifest_id(self, manifest_id):
if len(manifest_id) == 20:
manifest_id = mercurial.node.hex(manifest_id)
row = self.db.execute("select revid from revision where manifest_id = ?", (manifest_id,)).fetchone()
if row is not None:
return row[0]
raise KeyError
def lookup_changeset_id_by_revid(self, revid):
row = self.db.execute("select csid, mapping from revision where revid = ?").fetchone()
if row is not None:
return row[0], mapping_registry.get(row[1])
raise KeyError
def revids(self):
ret = set()
ret.update((row for
(row,) in self.db.execute("select revid from revision")))
return ret
def insert_revision(self, revid, manifest_id, changeset_id, mapping):
if len(manifest_id) == 20:
manifest_id = mercurial.node.hex(manifest_id)
if len(changeset_id) == 20:
changeset_id = mercurial.node.hex(changeset_id)
if len(changeset_id) != 40:
raise AssertionError
if len(manifest_id) != 40:
raise AssertionError
self.db.execute("insert into revision (revid, csid, manifest_id, mapping) values (?, ?, ?, ?)", (revid, changeset_id, manifest_id, str(mapping)))
def lookup_text_by_path_and_node(self, path, node):
cursor = self.db.execute("select fileid, revid from text_map where path = ? and node = ?", (path, node))
return cursor.fetchall()
def insert_text(self, path, node, fileid, revid):
self.db.execute("replace into text_map (path, node, fileid, revid) values (?, ?, ?, ?)", (path, node, fileid, revid))
class BzrHgCacheFormat(object):
"""Bazaar-Hg Cache Format."""
def get_format_string(self):
"""Return a single-line unique format string for this cache format."""
raise NotImplementedError(self.get_format_string)
def open(self, transport):
"""Open this format on a transport."""
raise NotImplementedError(self.open)
def initialize(self, transport):
"""Create a new instance of this cache format at transport."""
transport.put_bytes('format', self.get_format_string())
@classmethod
def from_transport(self, transport):
"""Open a cache file present on a transport, or initialize one.
:param transport: Transport to use
:return: A BzHgCache instance
"""
try:
format_name = transport.get_bytes('format')
format = formats.get(format_name)
except errors.NoSuchFile:
format = formats.get('default')
format.initialize(transport)
return format.open(transport)
@classmethod
def from_repository(cls, repository):
"""Open a cache file for a repository.
This will use the repository's transport to store the cache file, or
use the users global cache directory if the repository has no
transport associated with it.
:param repository: Repository to open the cache for
:return: A `BzrHgCache`
"""
repo_transport = getattr(repository, "_transport", None)
if repo_transport is not None:
# Even if we don't write to this repo, we should be able
# to update its cache.
repo_transport = remove_readonly_transport_decorator(repo_transport)
try:
repo_transport.mkdir('hg')
except errors.FileExists:
pass
transport = repo_transport.clone('hg')
else:
transport = get_remote_cache_transport()
return cls.from_transport(transport)
class SqliteBzrHgCacheFormat(BzrHgCacheFormat):
def get_format_string(self):
return 'bzr-hg sqlite cache v1\n'
def open(self, transport):
return SqliteIdmap(transport.local_abspath('idmap.db'))
class TdbBzrHgCacheFormat(BzrHgCacheFormat):
def get_format_string(self):
return 'bzr-hg tdb cache v1\n'
def open(self, transport):
return TdbIdmap(transport.local_abspath('idmap.tdb'))
formats = registry.Registry()
formats.register(TdbBzrHgCacheFormat().get_format_string(),
TdbBzrHgCacheFormat())
formats.register(SqliteBzrHgCacheFormat().get_format_string(),
SqliteBzrHgCacheFormat())
try:
import tdb
except ImportError:
formats.register('default', SqliteBzrHgCacheFormat())
else:
formats.register('default', TdbBzrHgCacheFormat())
def get_remote_cache_transport():
return get_transport(get_cache_dir())
def remove_readonly_transport_decorator(transport):
if transport.is_readonly():
return transport._decorated
return transport
def migrate_ancient_formats(repo_transport):
# Prefer migrating hg-v2.db over hg.tdb, since the latter may not
# be openable on some platforms.
if repo_transport.has("hg-v2.db"):
SqliteBzrHgCacheFormat().initialize(repo_transport.clone("hg"))
repo_transport.rename("hg-v2.db", "hg/idmap.db")
elif repo_transport.has("hg.tdb"):
TdbBzrHgCacheFormat().initialize(repo_transport.clone("hg"))
repo_transport.rename("hg.tdb", "hg/idmap.tdb")
def from_repository(repository):
"""Open a cache file for a repository.
If the repository is remote and there is no transport available from it
this will use a local file in the users cache directory
(typically ~/.cache/bazaar/hg/)
:param repository: A repository object
"""
repo_transport = getattr(repository, "_transport", None)
if repo_transport is not None:
# Migrate older cache formats
repo_transport = remove_readonly_transport_decorator(repo_transport)
try:
repo_transport.mkdir("hg")
except errors.FileExists:
pass
else:
migrate_ancient_formats(repo_transport)
return BzrHgCacheFormat.from_repository(repository)