87
class _KnitsParentsProvider(object):
89
def __init__(self, knit, prefix=()):
90
"""Create a parent provider for string keys mapped to tuple keys."""
95
return 'KnitsParentsProvider(%r)' % self._knit
97
def get_parent_map(self, keys):
98
"""See graph._StackedParentsProvider.get_parent_map"""
99
parent_map = self._knit.get_parent_map(
100
[self._prefix + (key,) for key in keys])
102
for key, parents in parent_map.items():
104
if len(parents) == 0:
105
parents = (_mod_revision.NULL_REVISION,)
107
parents = tuple(parent[-1] for parent in parents)
108
result[revid] = parents
109
for revision_id in keys:
110
if revision_id == _mod_revision.NULL_REVISION:
111
result[revision_id] = ()
86
115
class KnitRepository(MetaDirRepository):
87
116
"""Knit format repository."""
93
122
_commit_builder_class = None
94
123
_serializer = None
96
def __init__(self, _format, a_bzrdir, control_files, _revision_store,
97
control_store, text_store, _commit_builder_class, _serializer):
98
MetaDirRepository.__init__(self, _format, a_bzrdir, control_files,
99
_revision_store, control_store, text_store)
125
def __init__(self, _format, a_bzrdir, control_files, _commit_builder_class,
127
MetaDirRepository.__init__(self, _format, a_bzrdir, control_files)
100
128
self._commit_builder_class = _commit_builder_class
101
129
self._serializer = _serializer
102
130
self._reconcile_fixes_text_parents = True
103
control_store.get_scope = self.get_transaction
104
text_store.get_scope = self.get_transaction
105
_revision_store.get_scope = self.get_transaction
107
132
def _warn_if_deprecated(self):
108
133
# This class isn't deprecated
111
def _inventory_add_lines(self, inv_vf, revid, parents, lines, check_content):
112
return inv_vf.add_lines_with_ghosts(revid, parents, lines,
113
check_content=check_content)[0]
116
137
def _all_revision_ids(self):
117
138
"""See Repository.all_revision_ids()."""
118
# Knits get the revision graph from the index of the revision knit, so
119
# it's always possible even if they're on an unlistable transport.
120
return self._revision_store.all_revision_ids(self.get_transaction())
139
return [key[0] for key in self.revisions.keys()]
141
def _activate_new_inventory(self):
142
"""Put a replacement inventory.new into use as inventories."""
143
# Copy the content across
145
t.copy('inventory.new.kndx', 'inventory.kndx')
147
t.copy('inventory.new.knit', 'inventory.knit')
148
except errors.NoSuchFile:
149
# empty inventories knit
150
t.delete('inventory.knit')
151
# delete the temp inventory
152
t.delete('inventory.new.kndx')
154
t.delete('inventory.new.knit')
155
except errors.NoSuchFile:
156
# empty inventories knit
158
# Force index reload (sanity check)
159
self.inventories._index._reset_cache()
160
self.inventories.keys()
162
def _backup_inventory(self):
164
t.copy('inventory.kndx', 'inventory.backup.kndx')
165
t.copy('inventory.knit', 'inventory.backup.knit')
167
def _move_file_id(self, from_id, to_id):
168
t = self._transport.clone('knits')
169
from_rel_url = self.texts._index._mapper.map((from_id, None))
170
to_rel_url = self.texts._index._mapper.map((to_id, None))
171
# We expect both files to always exist in this case.
172
for suffix in ('.knit', '.kndx'):
173
t.rename(from_rel_url + suffix, to_rel_url + suffix)
175
def _remove_file_id(self, file_id):
176
t = self._transport.clone('knits')
177
rel_url = self.texts._index._mapper.map((file_id, None))
178
for suffix in ('.kndx', '.knit'):
180
t.delete(rel_url + suffix)
181
except errors.NoSuchFile:
184
def _temp_inventories(self):
185
result = self._format._get_inventories(self._transport, self,
187
# Reconciling when the output has no revisions would result in no
188
# writes - but we want to ensure there is an inventory for
189
# compatibility with older clients that don't lazy-load.
190
result.get_parent_map([('A',)])
122
193
def fileid_involved_between_revs(self, from_revid, to_revid):
123
194
"""Find file_id(s) which are involved in the changes between revisions.
145
216
return self._fileid_involved_by_set(changed)
148
def get_ancestry(self, revision_id, topo_sorted=True):
149
"""Return a list of revision-ids integrated by a revision.
151
This is topologically sorted, unless 'topo_sorted' is specified as
154
if _mod_revision.is_null(revision_id):
156
vf = self._get_revision_vf()
158
return [None] + vf.get_ancestry(revision_id, topo_sorted)
159
except errors.RevisionNotPresent:
160
raise errors.NoSuchRevision(self, revision_id)
162
@symbol_versioning.deprecated_method(symbol_versioning.one_two)
163
def get_data_stream(self, revision_ids):
164
"""See Repository.get_data_stream.
166
Deprecated in 1.2 for get_data_stream_for_search.
168
search_result = self.revision_ids_to_search_result(set(revision_ids))
169
return self.get_data_stream_for_search(search_result)
171
def get_data_stream_for_search(self, search):
172
"""See Repository.get_data_stream_for_search."""
173
item_keys = self.item_keys_introduced_by(search.get_keys())
174
for knit_kind, file_id, versions in item_keys:
176
if knit_kind == 'file':
177
name = ('file', file_id)
178
knit = self.weave_store.get_weave_or_empty(
179
file_id, self.get_transaction())
180
elif knit_kind == 'inventory':
181
knit = self.get_inventory_weave()
182
elif knit_kind == 'revisions':
183
knit = self._revision_store.get_revision_file(
184
self.get_transaction())
185
elif knit_kind == 'signatures':
186
knit = self._revision_store.get_signature_file(
187
self.get_transaction())
189
raise AssertionError('Unknown knit kind %r' % (knit_kind,))
190
yield name, _get_stream_as_bytes(knit, versions)
193
219
def get_revision(self, revision_id):
194
220
"""Return the Revision object for a named revision"""
195
221
revision_id = osutils.safe_revision_id(revision_id)
196
222
return self.get_revision_reconcile(revision_id)
198
def _get_revision_vf(self):
199
""":return: a versioned file containing the revisions."""
200
vf = self._revision_store.get_revision_file(self.get_transaction())
203
def has_revisions(self, revision_ids):
204
"""See Repository.has_revisions()."""
206
transaction = self.get_transaction()
207
for revision_id in revision_ids:
208
if self._revision_store.has_revision_id(revision_id, transaction):
209
result.add(revision_id)
212
224
@needs_write_lock
213
225
def reconcile(self, other=None, thorough=False):
214
226
"""Reconcile this repository."""
234
246
if not self.is_locked():
235
247
raise AssertionError()
236
vf = self._get_revision_vf()
237
for index_version in vf.versions():
238
parents_according_to_index = tuple(vf.get_parents_with_ghosts(
240
revision = self.get_revision(index_version)
249
for index_version in vf.keys():
250
parent_map = vf.get_parent_map([index_version])
251
parents_according_to_index = tuple(parent[-1] for parent in
252
parent_map[index_version])
253
revision = self.get_revision(index_version[-1])
241
254
parents_according_to_revision = tuple(revision.parent_ids)
242
255
if parents_according_to_index != parents_according_to_revision:
243
yield (index_version, parents_according_to_index,
256
yield (index_version[-1], parents_according_to_index,
244
257
parents_according_to_revision)
246
259
def _check_for_inconsistent_revision_parents(self):
284
297
# External lookups are not supported in this format.
285
298
supports_external_lookups = False
287
def _get_control_store(self, repo_transport, control_files):
288
"""Return the control store for this repository."""
289
return VersionedFileStore(
292
file_mode=control_files._file_mode,
293
versionedfile_class=knit.make_file_knit,
294
versionedfile_kwargs={'factory':knit.KnitPlainFactory()},
297
def _get_revision_store(self, repo_transport, control_files):
298
"""See RepositoryFormat._get_revision_store()."""
299
versioned_file_store = VersionedFileStore(
301
file_mode=control_files._file_mode,
304
versionedfile_class=knit.make_file_knit,
305
versionedfile_kwargs={'delta':False,
306
'factory':knit.KnitPlainFactory(),
310
return KnitRevisionStore(versioned_file_store)
312
def _get_text_store(self, transport, control_files):
313
"""See RepositoryFormat._get_text_store()."""
314
return self._get_versioned_file_store('knits',
317
versionedfile_class=knit.make_file_knit,
318
versionedfile_kwargs={
319
'create_parent_dir':True,
321
'dir_mode':control_files._dir_mode,
300
def _get_inventories(self, repo_transport, repo, name='inventory'):
301
mapper = ConstantMapper(name)
302
index = _KndxIndex(repo_transport, mapper, repo.get_transaction,
303
repo.is_write_locked, repo.is_locked)
304
access = _KnitKeyAccess(repo_transport, mapper)
305
return KnitVersionedFiles(index, access, annotated=False)
307
def _get_revisions(self, repo_transport, repo):
308
mapper = ConstantMapper('revisions')
309
index = _KndxIndex(repo_transport, mapper, repo.get_transaction,
310
repo.is_write_locked, repo.is_locked)
311
access = _KnitKeyAccess(repo_transport, mapper)
312
return KnitVersionedFiles(index, access, max_delta_chain=0,
315
def _get_signatures(self, repo_transport, repo):
316
mapper = ConstantMapper('signatures')
317
index = _KndxIndex(repo_transport, mapper, repo.get_transaction,
318
repo.is_write_locked, repo.is_locked)
319
access = _KnitKeyAccess(repo_transport, mapper)
320
return KnitVersionedFiles(index, access, max_delta_chain=0,
323
def _get_texts(self, repo_transport, repo):
324
mapper = HashEscapedPrefixMapper()
325
base_transport = repo_transport.clone('knits')
326
index = _KndxIndex(base_transport, mapper, repo.get_transaction,
327
repo.is_write_locked, repo.is_locked)
328
access = _KnitKeyAccess(base_transport, mapper)
329
return KnitVersionedFiles(index, access, max_delta_chain=200,
325
332
def initialize(self, a_bzrdir, shared=False):
326
333
"""Create a knit format 1 repository.
339
346
repo_transport = a_bzrdir.get_repository_transport(None)
340
347
control_files = lockable_files.LockableFiles(repo_transport,
341
348
'lock', lockdir.LockDir)
342
control_store = self._get_control_store(repo_transport, control_files)
343
349
transaction = transactions.WriteTransaction()
344
# trigger a write of the inventory store.
345
control_store.get_weave_or_empty('inventory', transaction)
346
_revision_store = self._get_revision_store(repo_transport, control_files)
350
result = self.open(a_bzrdir=a_bzrdir, _found=True)
347
352
# the revision id here is irrelevant: it will not be stored, and cannot
349
_revision_store.has_revision_id('A', transaction)
350
_revision_store.get_signature_file(transaction)
351
return self.open(a_bzrdir=a_bzrdir, _found=True)
353
# already exist, we do this to create files on disk for older clients.
354
result.inventories.get_parent_map([('A',)])
355
result.revisions.get_parent_map([('A',)])
356
result.signatures.get_parent_map([('A',)])
353
360
def open(self, a_bzrdir, _found=False, _override_transport=None):
354
361
"""See RepositoryFormat.open().
365
372
repo_transport = a_bzrdir.get_repository_transport(None)
366
373
control_files = lockable_files.LockableFiles(repo_transport,
367
374
'lock', lockdir.LockDir)
368
text_store = self._get_text_store(repo_transport, control_files)
369
control_store = self._get_control_store(repo_transport, control_files)
370
_revision_store = self._get_revision_store(repo_transport, control_files)
371
return self.repository_class(_format=self,
375
repo = self.repository_class(_format=self,
372
376
a_bzrdir=a_bzrdir,
373
377
control_files=control_files,
374
_revision_store=_revision_store,
375
control_store=control_store,
376
text_store=text_store,
377
378
_commit_builder_class=self._commit_builder_class,
378
379
_serializer=self._serializer)
380
repo.revisions = self._get_revisions(repo_transport, repo)
381
repo.signatures = self._get_signatures(repo_transport, repo)
382
repo.inventories = self._get_inventories(repo_transport, repo)
383
repo.texts = self._get_texts(repo_transport, repo)
384
repo._transport = repo_transport
381
388
class RepositoryFormatKnit1(RepositoryFormatKnit):
502
509
def get_format_description(self):
503
510
"""See RepositoryFormat.get_format_description()."""
504
511
return "Knit repository format 4"
507
def _get_stream_as_bytes(knit, required_versions):
508
"""Generate a serialised data stream.
510
The format is a bencoding of a list. The first element of the list is a
511
string of the format signature, then each subsequent element is a list
512
corresponding to a record. Those lists contain:
519
:returns: a bencoded list.
521
knit_stream = knit.get_data_stream(required_versions)
522
format_signature, data_list, callable = knit_stream
524
data.append(format_signature)
525
for version, options, length, parents in data_list:
526
data.append([version, options, parents, callable(length)])
527
return bencode.bencode(data)