421
427
inventory_keys = source_vf.keys()
422
428
missing_inventories = set(self.revision_keys).difference(inventory_keys)
423
429
if missing_inventories:
424
missing_inventories = sorted(missing_inventories)
425
raise ValueError('We are missing inventories for revisions: %s'
426
% (missing_inventories,))
430
# Go back to the original repo, to see if these are really missing
431
# https://bugs.launchpad.net/bzr/+bug/437003
432
# If we are packing a subset of the repo, it is fine to just have
433
# the data in another Pack file, which is not included in this pack
435
inv_index = self._pack_collection.repo.inventories._index
436
pmap = inv_index.get_parent_map(missing_inventories)
437
really_missing = missing_inventories.difference(pmap)
439
missing_inventories = sorted(really_missing)
440
raise ValueError('We are missing inventories for revisions: %s'
441
% (missing_inventories,))
427
442
self._copy_stream(source_vf, target_vf, inventory_keys,
428
443
'inventories', self._get_filtered_inv_stream, 2)
445
def _get_chk_vfs_for_copy(self):
446
return self._build_vfs('chk', False, False)
430
448
def _copy_chk_texts(self):
431
source_vf, target_vf = self._build_vfs('chk', False, False)
449
source_vf, target_vf = self._get_chk_vfs_for_copy()
432
450
# TODO: This is technically spurious... if it is a performance issue,
434
452
total_keys = source_vf.keys()
580
595
return new_pack.data_inserted() and self._data_changed
598
class GCCHKCanonicalizingPacker(GCCHKPacker):
599
"""A packer that ensures inventories have canonical-form CHK maps.
601
Ideally this would be part of reconcile, but it's very slow and rarely
602
needed. (It repairs repositories affected by
603
https://bugs.launchpad.net/bzr/+bug/522637).
606
def __init__(self, *args, **kwargs):
607
super(GCCHKCanonicalizingPacker, self).__init__(*args, **kwargs)
608
self._data_changed = False
610
def _exhaust_stream(self, source_vf, keys, message, vf_to_stream, pb_offset):
611
"""Create and exhaust a stream, but don't insert it.
613
This is useful to get the side-effects of generating a stream.
615
self.pb.update('scanning %s' % (message,), pb_offset)
616
with ui.ui_factory.nested_progress_bar() as child_pb:
617
list(vf_to_stream(source_vf, keys, message, child_pb))
619
def _copy_inventory_texts(self):
620
source_vf, target_vf = self._build_vfs('inventory', True, True)
621
source_chk_vf, target_chk_vf = self._get_chk_vfs_for_copy()
622
inventory_keys = source_vf.keys()
623
# First, copy the existing CHKs on the assumption that most of them
624
# will be correct. This will save us from having to reinsert (and
625
# recompress) these records later at the cost of perhaps preserving a
627
# (Iterate but don't insert _get_filtered_inv_stream to populate the
628
# variables needed by GCCHKPacker._copy_chk_texts.)
629
self._exhaust_stream(source_vf, inventory_keys, 'inventories',
630
self._get_filtered_inv_stream, 2)
631
GCCHKPacker._copy_chk_texts(self)
632
# Now copy and fix the inventories, and any regenerated CHKs.
633
def chk_canonicalizing_inv_stream(source_vf, keys, message, pb=None):
634
return self._get_filtered_canonicalizing_inv_stream(
635
source_vf, keys, message, pb, source_chk_vf, target_chk_vf)
636
self._copy_stream(source_vf, target_vf, inventory_keys,
637
'inventories', chk_canonicalizing_inv_stream, 4)
639
def _copy_chk_texts(self):
640
# No-op; in this class this happens during _copy_inventory_texts.
643
def _get_filtered_canonicalizing_inv_stream(self, source_vf, keys, message,
644
pb=None, source_chk_vf=None, target_chk_vf=None):
645
"""Filter the texts of inventories, regenerating CHKs to make sure they
648
total_keys = len(keys)
649
target_chk_vf = versionedfile.NoDupeAddLinesDecorator(target_chk_vf)
650
def _filtered_inv_stream():
651
stream = source_vf.get_record_stream(keys, 'groupcompress', True)
652
search_key_name = None
653
for idx, record in enumerate(stream):
654
# Inventories should always be with revisions; assume success.
655
bytes = record.get_bytes_as('fulltext')
656
chk_inv = inventory.CHKInventory.deserialise(
657
source_chk_vf, bytes, record.key)
659
pb.update('inv', idx, total_keys)
660
chk_inv.id_to_entry._ensure_root()
661
if search_key_name is None:
662
# Find the name corresponding to the search_key_func
663
search_key_reg = chk_map.search_key_registry
664
for search_key_name, func in viewitems(search_key_reg):
665
if func == chk_inv.id_to_entry._search_key_func:
667
canonical_inv = inventory.CHKInventory.from_inventory(
668
target_chk_vf, chk_inv,
669
maximum_size=chk_inv.id_to_entry._root_node._maximum_size,
670
search_key_name=search_key_name)
671
if chk_inv.id_to_entry.key() != canonical_inv.id_to_entry.key():
673
'Non-canonical CHK map for id_to_entry of inv: %s '
674
'(root is %s, should be %s)' % (chk_inv.revision_id,
675
chk_inv.id_to_entry.key()[0],
676
canonical_inv.id_to_entry.key()[0]))
677
self._data_changed = True
678
p_id_map = chk_inv.parent_id_basename_to_file_id
679
p_id_map._ensure_root()
680
canon_p_id_map = canonical_inv.parent_id_basename_to_file_id
681
if p_id_map.key() != canon_p_id_map.key():
683
'Non-canonical CHK map for parent_id_to_basename of '
684
'inv: %s (root is %s, should be %s)'
685
% (chk_inv.revision_id, p_id_map.key()[0],
686
canon_p_id_map.key()[0]))
687
self._data_changed = True
688
yield versionedfile.ChunkedContentFactory(record.key,
689
record.parents, record.sha1,
690
canonical_inv.to_lines())
691
# We have finished processing all of the inventory records, we
692
# don't need these sets anymore
693
return _filtered_inv_stream()
695
def _use_pack(self, new_pack):
696
"""Override _use_pack to check for reconcile having changed content."""
697
return new_pack.data_inserted() and self._data_changed
583
700
class GCRepositoryPackCollection(RepositoryPackCollection):
585
702
pack_factory = GCPack
586
703
resumed_pack_factory = ResumedGCPack
704
normal_packer_class = GCCHKPacker
705
optimising_packer_class = GCCHKPacker
588
707
def _check_new_inventories(self):
589
708
"""Detect missing inventories or chk root entries for the new revisions
671
793
% (sorted(missing_text_keys),))
674
def _execute_pack_operations(self, pack_operations,
675
_packer_class=GCCHKPacker,
677
"""Execute a series of pack operations.
679
:param pack_operations: A list of [revision_count, packs_to_combine].
680
:param _packer_class: The class of packer to use (default: Packer).
683
# XXX: Copied across from RepositoryPackCollection simply because we
684
# want to override the _packer_class ... :(
685
for revision_count, packs in pack_operations:
686
# we may have no-ops from the setup logic
689
packer = GCCHKPacker(self, packs, '.autopack',
690
reload_func=reload_func)
692
result = packer.pack()
693
except errors.RetryWithNewPacks:
694
# An exception is propagating out of this context, make sure
695
# this packer has cleaned up. Packer() doesn't set its new_pack
696
# state into the RepositoryPackCollection object, so we only
697
# have access to it directly here.
698
if packer.new_pack is not None:
699
packer.new_pack.abort()
704
self._remove_pack_from_memory(pack)
705
# record the newly available packs and stop advertising the old
708
for _, packs in pack_operations:
709
to_be_obsoleted.extend(packs)
710
result = self._save_pack_names(clear_obsolete_packs=True,
711
obsolete_packs=to_be_obsoleted)
715
class CHKInventoryRepository(KnitPackRepository):
716
"""subclass of KnitPackRepository that uses CHK based inventories."""
718
def __init__(self, _format, a_bzrdir, control_files, _commit_builder_class,
797
class CHKInventoryRepository(PackRepository):
798
"""subclass of PackRepository that uses CHK based inventories."""
800
def __init__(self, _format, a_controldir, control_files, _commit_builder_class,
720
802
"""Overridden to change pack collection class."""
721
KnitPackRepository.__init__(self, _format, a_bzrdir, control_files,
722
_commit_builder_class, _serializer)
723
# and now replace everything it did :)
803
super(CHKInventoryRepository, self).__init__(_format, a_controldir,
804
control_files, _commit_builder_class, _serializer)
724
805
index_transport = self._transport.clone('indices')
725
806
self._pack_collection = GCRepositoryPackCollection(self,
726
807
self._transport, index_transport,
896
979
if record.storage_kind != 'absent':
897
980
texts[record.key] = record.get_bytes_as('fulltext')
899
raise errors.NoSuchRevision(self, record.key)
982
texts[record.key] = None
901
yield inventory.CHKInventory.deserialise(self.chk_bytes, texts[key], key)
986
yield (None, key[-1])
988
yield (inventory.CHKInventory.deserialise(
989
self.chk_bytes, bytes, key), key[-1])
903
def _iter_inventory_xmls(self, revision_ids, ordering):
991
def _get_inventory_xml(self, revision_id):
992
"""Get serialized inventory as a string."""
904
993
# Without a native 'xml' inventory, this method doesn't make sense.
905
994
# However older working trees, and older bundles want it - so we supply
906
995
# it allowing _get_inventory_xml to work. Bundles currently use the
907
996
# serializer directly; this also isn't ideal, but there isn't an xml
908
# iteration interface offered at all for repositories. We could make
909
# _iter_inventory_xmls be part of the contract, even if kept private.
910
inv_to_str = self._serializer.write_inventory_to_string
911
for inv in self.iter_inventories(revision_ids, ordering=ordering):
912
yield inv_to_str(inv), inv.revision_id
997
# iteration interface offered at all for repositories.
998
return self._serializer.write_inventory_to_string(
999
self.get_inventory(revision_id))
914
1001
def _find_present_inventory_keys(self, revision_keys):
915
1002
parent_map = self.inventories.get_parent_map(revision_keys)
996
1078
if entry.revision == inv.revision_id:
997
1079
result[key] = True
1082
def reconcile_canonicalize_chks(self):
1083
"""Reconcile this repository to make sure all CHKs are in canonical
1086
from breezy.reconcile import PackReconciler
1087
with self.lock_write():
1088
reconciler = PackReconciler(self, thorough=True, canonicalize_chks=True)
1089
reconciler.reconcile()
1002
1092
def _reconcile_pack(self, collection, packs, extension, revs, pb):
1003
1093
packer = GCCHKReconcilePacker(collection, packs, extension)
1004
1094
return packer.pack(pb)
1096
def _canonicalize_chks_pack(self, collection, packs, extension, revs, pb):
1097
packer = GCCHKCanonicalizingPacker(collection, packs, extension, revs)
1098
return packer.pack(pb)
1006
1100
def _get_source(self, to_format):
1007
1101
"""Return a source for streaming from this repository."""
1008
1102
if self._format._serializer == to_format._serializer:
1013
1107
return GroupCHKStreamSource(self, to_format)
1014
1108
return super(CHKInventoryRepository, self)._get_source(to_format)
1017
class GroupCHKStreamSource(KnitPackStreamSource):
1110
def _find_inconsistent_revision_parents(self, revisions_iterator=None):
1111
"""Find revisions with different parent lists in the revision object
1112
and in the index graph.
1114
:param revisions_iterator: None, or an iterator of (revid,
1115
Revision-or-None). This iterator controls the revisions checked.
1116
:returns: an iterator yielding tuples of (revison-id, parents-in-index,
1117
parents-in-revision).
1119
if not self.is_locked():
1120
raise AssertionError()
1122
if revisions_iterator is None:
1123
revisions_iterator = self.iter_revisions(self.all_revision_ids())
1124
for revid, revision in revisions_iterator:
1125
if revision is None:
1127
parent_map = vf.get_parent_map([(revid,)])
1128
parents_according_to_index = tuple(parent[-1] for parent in
1129
parent_map[(revid,)])
1130
parents_according_to_revision = tuple(revision.parent_ids)
1131
if parents_according_to_index != parents_according_to_revision:
1132
yield (revid, parents_according_to_index,
1133
parents_according_to_revision)
1135
def _check_for_inconsistent_revision_parents(self):
1136
inconsistencies = list(self._find_inconsistent_revision_parents())
1138
raise errors.BzrCheckError(
1139
"Revision index has inconsistent parents.")
1142
class GroupCHKStreamSource(StreamSource):
1018
1143
"""Used when both the source and target repo are GroupCHK repos."""
1020
1145
def __init__(self, from_repository, to_format):
1108
1232
self._chk_p_id_roots = None
1109
1233
yield 'chk_bytes', _get_parent_id_basename_to_file_id_pages()
1235
def _get_text_stream(self):
1236
# Note: We know we don't have to handle adding root keys, because both
1237
# the source and target are the identical network name.
1238
text_stream = self.from_repository.texts.get_record_stream(
1239
self._text_keys, self._text_fetch_order, False)
1240
return ('texts', text_stream)
1111
1242
def get_stream(self, search):
1243
def wrap_and_count(pb, rc, stream):
1244
"""Yield records from stream while showing progress."""
1246
for record in stream:
1247
if count == rc.STEP:
1249
pb.update('Estimate', rc.current, rc.max)
1112
1254
revision_ids = search.get_keys()
1255
pb = ui.ui_factory.nested_progress_bar()
1256
rc = self._record_counter
1257
self._record_counter.setup(len(revision_ids))
1113
1258
for stream_info in self._fetch_revision_texts(revision_ids):
1259
yield (stream_info[0],
1260
wrap_and_count(pb, rc, stream_info[1]))
1115
1261
self._revision_keys = [(rev_id,) for rev_id in revision_ids]
1116
self.from_repository.revisions.clear_cache()
1117
self.from_repository.signatures.clear_cache()
1118
yield self._get_inventory_stream(self._revision_keys)
1119
self.from_repository.inventories.clear_cache()
1120
1262
# TODO: The keys to exclude might be part of the search recipe
1121
1263
# For now, exclude all parents that are at the edge of ancestry, for
1122
1264
# which we have inventories
1123
1265
from_repo = self.from_repository
1124
1266
parent_keys = from_repo._find_parent_keys_of_revisions(
1125
1267
self._revision_keys)
1268
self.from_repository.revisions.clear_cache()
1269
self.from_repository.signatures.clear_cache()
1270
# Clear the repo's get_parent_map cache too.
1271
self.from_repository._unstacked_provider.disable_cache()
1272
self.from_repository._unstacked_provider.enable_cache()
1273
s = self._get_inventory_stream(self._revision_keys)
1274
yield (s[0], wrap_and_count(pb, rc, s[1]))
1275
self.from_repository.inventories.clear_cache()
1126
1276
for stream_info in self._get_filtered_chk_streams(parent_keys):
1277
yield (stream_info[0], wrap_and_count(pb, rc, stream_info[1]))
1128
1278
self.from_repository.chk_bytes.clear_cache()
1129
yield self._get_text_stream()
1279
s = self._get_text_stream()
1280
yield (s[0], wrap_and_count(pb, rc, s[1]))
1130
1281
self.from_repository.texts.clear_cache()
1282
pb.update('Done', rc.max, rc.max)
1132
1285
def get_stream_for_missing_keys(self, missing_keys):
1133
1286
# missing keys can only occur when we are byte copying and not
1190
def _filter_text_keys(interesting_nodes_iterable, text_keys, bytes_to_info):
1343
def _filter_text_keys(interesting_nodes_iterable, text_keys, bytes_to_text_key):
1191
1344
"""Iterate the result of iter_interesting_nodes, yielding the records
1192
1345
and adding to text_keys.
1347
text_keys_update = text_keys.update
1194
1348
for record, items in interesting_nodes_iterable:
1195
for name, bytes in items:
1196
# Note: we don't care about name_utf8, because groupcompress repos
1197
# are always rich-root, so there are no synthesised root records to
1199
_, file_id, revision_id = bytes_to_info(bytes)
1200
file_id = intern(file_id)
1201
revision_id = intern(revision_id)
1202
text_keys.add(StaticTuple(file_id, revision_id).intern())
1349
text_keys_update([bytes_to_text_key(b) for n, b in items])
1208
class RepositoryFormatCHK1(RepositoryFormatPack):
1209
"""A hashed CHK+group compress pack repository."""
1353
class RepositoryFormat2a(RepositoryFormatPack):
1354
"""A CHK repository that uses the bencode revision serializer."""
1211
1356
repository_class = CHKInventoryRepository
1212
1357
supports_external_lookups = True
1213
1358
supports_chks = True
1214
# For right now, setting this to True gives us InterModel1And2 rather
1215
# than InterDifferingSerializer
1216
_commit_builder_class = PackRootCommitBuilder
1359
_commit_builder_class = PackCommitBuilder
1217
1360
rich_root_data = True
1218
_serializer = chk_serializer.chk_serializer_255_bigpage
1361
_serializer = chk_serializer.chk_bencode_serializer
1219
1362
_commit_inv_deltas = True
1220
1363
# What index classes to use
1221
1364
index_builder_class = BTreeBuilder
1232
1375
pack_compresses = True
1234
1377
def _get_matching_bzrdir(self):
1235
return bzrdir.format_registry.make_bzrdir('development6-rich-root')
1237
def _ignore_setting_bzrdir(self, format):
1240
_matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
1242
def get_format_string(self):
1243
"""See RepositoryFormat.get_format_string()."""
1244
return ('Bazaar development format - group compression and chk inventory'
1245
' (needs bzr.dev from 1.14)\n')
1247
def get_format_description(self):
1248
"""See RepositoryFormat.get_format_description()."""
1249
return ("Development repository format - rich roots, group compression"
1250
" and chk inventories")
1253
class RepositoryFormatCHK2(RepositoryFormatCHK1):
1254
"""A CHK repository that uses the bencode revision serializer."""
1256
_serializer = chk_serializer.chk_bencode_serializer
1258
def _get_matching_bzrdir(self):
1259
return bzrdir.format_registry.make_bzrdir('development7-rich-root')
1261
def _ignore_setting_bzrdir(self, format):
1264
_matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
1266
def get_format_string(self):
1267
"""See RepositoryFormat.get_format_string()."""
1268
return ('Bazaar development format - chk repository with bencode '
1269
'revision serialization (needs bzr.dev from 1.16)\n')
1272
class RepositoryFormat2a(RepositoryFormatCHK2):
1273
"""A CHK repository that uses the bencode revision serializer.
1275
This is the same as RepositoryFormatCHK2 but with a public name.
1278
_serializer = chk_serializer.chk_bencode_serializer
1280
def _get_matching_bzrdir(self):
1281
return bzrdir.format_registry.make_bzrdir('2a')
1283
def _ignore_setting_bzrdir(self, format):
1286
_matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
1288
def get_format_string(self):
1289
return ('Bazaar repository format 2a (needs bzr 1.16 or later)\n')
1378
return controldir.format_registry.make_controldir('2a')
1380
def _ignore_setting_bzrdir(self, format):
1383
_matchingcontroldir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
1386
def get_format_string(cls):
1387
return b'Bazaar repository format 2a (needs bzr 1.16 or later)\n'
1291
1389
def get_format_description(self):
1292
1390
"""See RepositoryFormat.get_format_description()."""
1293
1391
return ("Repository format 2a - rich roots, group compression"
1294
1392
" and chk inventories")
1395
class RepositoryFormat2aSubtree(RepositoryFormat2a):
1396
"""A 2a repository format that supports nested trees.
1400
def _get_matching_bzrdir(self):
1401
return controldir.format_registry.make_controldir('development-subtree')
1403
def _ignore_setting_bzrdir(self, format):
1406
_matchingcontroldir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
1409
def get_format_string(cls):
1410
return b'Bazaar development format 8\n'
1412
def get_format_description(self):
1413
"""See RepositoryFormat.get_format_description()."""
1414
return ("Development repository format 8 - nested trees, "
1415
"group compression and chk inventories")
1418
supports_tree_reference = True