419
425
inventory_keys = source_vf.keys()
420
426
missing_inventories = set(self.revision_keys).difference(inventory_keys)
421
427
if missing_inventories:
422
missing_inventories = sorted(missing_inventories)
423
raise ValueError('We are missing inventories for revisions: %s'
424
% (missing_inventories,))
428
# Go back to the original repo, to see if these are really missing
429
# https://bugs.launchpad.net/bzr/+bug/437003
430
# If we are packing a subset of the repo, it is fine to just have
431
# the data in another Pack file, which is not included in this pack
433
inv_index = self._pack_collection.repo.inventories._index
434
pmap = inv_index.get_parent_map(missing_inventories)
435
really_missing = missing_inventories.difference(pmap)
437
missing_inventories = sorted(really_missing)
438
raise ValueError('We are missing inventories for revisions: %s'
439
% (missing_inventories,))
425
440
self._copy_stream(source_vf, target_vf, inventory_keys,
426
441
'inventories', self._get_filtered_inv_stream, 2)
443
def _get_chk_vfs_for_copy(self):
444
return self._build_vfs('chk', False, False)
428
446
def _copy_chk_texts(self):
429
source_vf, target_vf = self._build_vfs('chk', False, False)
447
source_vf, target_vf = self._get_chk_vfs_for_copy()
430
448
# TODO: This is technically spurious... if it is a performance issue,
432
450
total_keys = source_vf.keys()
578
596
return new_pack.data_inserted() and self._data_changed
599
class GCCHKCanonicalizingPacker(GCCHKPacker):
600
"""A packer that ensures inventories have canonical-form CHK maps.
602
Ideally this would be part of reconcile, but it's very slow and rarely
603
needed. (It repairs repositories affected by
604
https://bugs.launchpad.net/bzr/+bug/522637).
607
def __init__(self, *args, **kwargs):
608
super(GCCHKCanonicalizingPacker, self).__init__(*args, **kwargs)
609
self._data_changed = False
611
def _exhaust_stream(self, source_vf, keys, message, vf_to_stream, pb_offset):
612
"""Create and exhaust a stream, but don't insert it.
614
This is useful to get the side-effects of generating a stream.
616
self.pb.update('scanning %s' % (message,), pb_offset)
617
child_pb = ui.ui_factory.nested_progress_bar()
619
list(vf_to_stream(source_vf, keys, message, child_pb))
623
def _copy_inventory_texts(self):
624
source_vf, target_vf = self._build_vfs('inventory', True, True)
625
source_chk_vf, target_chk_vf = self._get_chk_vfs_for_copy()
626
inventory_keys = source_vf.keys()
627
# First, copy the existing CHKs on the assumption that most of them
628
# will be correct. This will save us from having to reinsert (and
629
# recompress) these records later at the cost of perhaps preserving a
631
# (Iterate but don't insert _get_filtered_inv_stream to populate the
632
# variables needed by GCCHKPacker._copy_chk_texts.)
633
self._exhaust_stream(source_vf, inventory_keys, 'inventories',
634
self._get_filtered_inv_stream, 2)
635
GCCHKPacker._copy_chk_texts(self)
636
# Now copy and fix the inventories, and any regenerated CHKs.
637
def chk_canonicalizing_inv_stream(source_vf, keys, message, pb=None):
638
return self._get_filtered_canonicalizing_inv_stream(
639
source_vf, keys, message, pb, source_chk_vf, target_chk_vf)
640
self._copy_stream(source_vf, target_vf, inventory_keys,
641
'inventories', chk_canonicalizing_inv_stream, 4)
643
def _copy_chk_texts(self):
644
# No-op; in this class this happens during _copy_inventory_texts.
647
def _get_filtered_canonicalizing_inv_stream(self, source_vf, keys, message,
648
pb=None, source_chk_vf=None, target_chk_vf=None):
649
"""Filter the texts of inventories, regenerating CHKs to make sure they
652
total_keys = len(keys)
653
target_chk_vf = versionedfile.NoDupeAddLinesDecorator(target_chk_vf)
654
def _filtered_inv_stream():
655
stream = source_vf.get_record_stream(keys, 'groupcompress', True)
656
search_key_name = None
657
for idx, record in enumerate(stream):
658
# Inventories should always be with revisions; assume success.
659
bytes = record.get_bytes_as('fulltext')
660
chk_inv = inventory.CHKInventory.deserialise(
661
source_chk_vf, bytes, record.key)
663
pb.update('inv', idx, total_keys)
664
chk_inv.id_to_entry._ensure_root()
665
if search_key_name is None:
666
# Find the name corresponding to the search_key_func
667
search_key_reg = chk_map.search_key_registry
668
for search_key_name, func in search_key_reg.iteritems():
669
if func == chk_inv.id_to_entry._search_key_func:
671
canonical_inv = inventory.CHKInventory.from_inventory(
672
target_chk_vf, chk_inv,
673
maximum_size=chk_inv.id_to_entry._root_node._maximum_size,
674
search_key_name=search_key_name)
675
if chk_inv.id_to_entry.key() != canonical_inv.id_to_entry.key():
677
'Non-canonical CHK map for id_to_entry of inv: %s '
678
'(root is %s, should be %s)' % (chk_inv.revision_id,
679
chk_inv.id_to_entry.key()[0],
680
canonical_inv.id_to_entry.key()[0]))
681
self._data_changed = True
682
p_id_map = chk_inv.parent_id_basename_to_file_id
683
p_id_map._ensure_root()
684
canon_p_id_map = canonical_inv.parent_id_basename_to_file_id
685
if p_id_map.key() != canon_p_id_map.key():
687
'Non-canonical CHK map for parent_id_to_basename of '
688
'inv: %s (root is %s, should be %s)'
689
% (chk_inv.revision_id, p_id_map.key()[0],
690
canon_p_id_map.key()[0]))
691
self._data_changed = True
692
yield versionedfile.ChunkedContentFactory(record.key,
693
record.parents, record.sha1,
694
canonical_inv.to_lines())
695
# We have finished processing all of the inventory records, we
696
# don't need these sets anymore
697
return _filtered_inv_stream()
699
def _use_pack(self, new_pack):
700
"""Override _use_pack to check for reconcile having changed content."""
701
return new_pack.data_inserted() and self._data_changed
581
704
class GCRepositoryPackCollection(RepositoryPackCollection):
583
706
pack_factory = GCPack
584
707
resumed_pack_factory = ResumedGCPack
708
normal_packer_class = GCCHKPacker
709
optimising_packer_class = GCCHKPacker
586
711
def _check_new_inventories(self):
587
712
"""Detect missing inventories or chk root entries for the new revisions
669
797
% (sorted(missing_text_keys),))
672
def _execute_pack_operations(self, pack_operations,
673
_packer_class=GCCHKPacker,
675
"""Execute a series of pack operations.
677
:param pack_operations: A list of [revision_count, packs_to_combine].
678
:param _packer_class: The class of packer to use (default: Packer).
681
# XXX: Copied across from RepositoryPackCollection simply because we
682
# want to override the _packer_class ... :(
683
for revision_count, packs in pack_operations:
684
# we may have no-ops from the setup logic
687
packer = GCCHKPacker(self, packs, '.autopack',
688
reload_func=reload_func)
690
result = packer.pack()
691
except errors.RetryWithNewPacks:
692
# An exception is propagating out of this context, make sure
693
# this packer has cleaned up. Packer() doesn't set its new_pack
694
# state into the RepositoryPackCollection object, so we only
695
# have access to it directly here.
696
if packer.new_pack is not None:
697
packer.new_pack.abort()
702
self._remove_pack_from_memory(pack)
703
# record the newly available packs and stop advertising the old
706
for _, packs in pack_operations:
707
to_be_obsoleted.extend(packs)
708
result = self._save_pack_names(clear_obsolete_packs=True,
709
obsolete_packs=to_be_obsoleted)
713
class CHKInventoryRepository(KnitPackRepository):
714
"""subclass of KnitPackRepository that uses CHK based inventories."""
801
class CHKInventoryRepository(PackRepository):
802
"""subclass of PackRepository that uses CHK based inventories."""
716
804
def __init__(self, _format, a_bzrdir, control_files, _commit_builder_class,
718
806
"""Overridden to change pack collection class."""
719
KnitPackRepository.__init__(self, _format, a_bzrdir, control_files,
720
_commit_builder_class, _serializer)
721
# and now replace everything it did :)
807
super(CHKInventoryRepository, self).__init__(_format, a_bzrdir,
808
control_files, _commit_builder_class, _serializer)
722
809
index_transport = self._transport.clone('indices')
723
810
self._pack_collection = GCRepositoryPackCollection(self,
724
811
self._transport, index_transport,
896
983
if record.storage_kind != 'absent':
897
984
texts[record.key] = record.get_bytes_as('fulltext')
899
raise errors.NoSuchRevision(self, record.key)
986
texts[record.key] = None
901
yield inventory.CHKInventory.deserialise(self.chk_bytes, texts[key], key)
990
yield (None, key[-1])
992
yield (inventory.CHKInventory.deserialise(
993
self.chk_bytes, bytes, key), key[-1])
903
def _iter_inventory_xmls(self, revision_ids, ordering):
995
def _get_inventory_xml(self, revision_id):
996
"""Get serialized inventory as a string."""
904
997
# Without a native 'xml' inventory, this method doesn't make sense.
905
998
# However older working trees, and older bundles want it - so we supply
906
999
# it allowing _get_inventory_xml to work. Bundles currently use the
907
1000
# 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
1001
# iteration interface offered at all for repositories.
1002
return self._serializer.write_inventory_to_string(
1003
self.get_inventory(revision_id))
914
1005
def _find_present_inventory_keys(self, revision_keys):
915
1006
parent_map = self.inventories.get_parent_map(revision_keys)
1013
1118
return GroupCHKStreamSource(self, to_format)
1014
1119
return super(CHKInventoryRepository, self)._get_source(to_format)
1017
class GroupCHKStreamSource(KnitPackStreamSource):
1121
def _find_inconsistent_revision_parents(self, revisions_iterator=None):
1122
"""Find revisions with different parent lists in the revision object
1123
and in the index graph.
1125
:param revisions_iterator: None, or an iterator of (revid,
1126
Revision-or-None). This iterator controls the revisions checked.
1127
:returns: an iterator yielding tuples of (revison-id, parents-in-index,
1128
parents-in-revision).
1130
if not self.is_locked():
1131
raise AssertionError()
1133
if revisions_iterator is None:
1134
revisions_iterator = self._iter_revisions(None)
1135
for revid, revision in revisions_iterator:
1136
if revision is None:
1138
parent_map = vf.get_parent_map([(revid,)])
1139
parents_according_to_index = tuple(parent[-1] for parent in
1140
parent_map[(revid,)])
1141
parents_according_to_revision = tuple(revision.parent_ids)
1142
if parents_according_to_index != parents_according_to_revision:
1143
yield (revid, parents_according_to_index,
1144
parents_according_to_revision)
1146
def _check_for_inconsistent_revision_parents(self):
1147
inconsistencies = list(self._find_inconsistent_revision_parents())
1149
raise errors.BzrCheckError(
1150
"Revision index has inconsistent parents.")
1153
class GroupCHKStreamSource(StreamSource):
1018
1154
"""Used when both the source and target repo are GroupCHK repos."""
1020
1156
def __init__(self, from_repository, to_format):
1127
1270
yield (stream_info[0],
1128
1271
wrap_and_count(pb, rc, stream_info[1]))
1129
1272
self._revision_keys = [(rev_id,) for rev_id in revision_ids]
1130
self.from_repository.revisions.clear_cache()
1131
self.from_repository.signatures.clear_cache()
1132
s = self._get_inventory_stream(self._revision_keys)
1133
yield (s[0], wrap_and_count(pb, rc, s[1]))
1134
self.from_repository.inventories.clear_cache()
1135
1273
# TODO: The keys to exclude might be part of the search recipe
1136
1274
# For now, exclude all parents that are at the edge of ancestry, for
1137
1275
# which we have inventories
1138
1276
from_repo = self.from_repository
1139
1277
parent_keys = from_repo._find_parent_keys_of_revisions(
1140
1278
self._revision_keys)
1279
self.from_repository.revisions.clear_cache()
1280
self.from_repository.signatures.clear_cache()
1281
# Clear the repo's get_parent_map cache too.
1282
self.from_repository._unstacked_provider.disable_cache()
1283
self.from_repository._unstacked_provider.enable_cache()
1284
s = self._get_inventory_stream(self._revision_keys)
1285
yield (s[0], wrap_and_count(pb, rc, s[1]))
1286
self.from_repository.inventories.clear_cache()
1141
1287
for stream_info in self._get_filtered_chk_streams(parent_keys):
1142
1288
yield (stream_info[0], wrap_and_count(pb, rc, stream_info[1]))
1143
1289
self.from_repository.chk_bytes.clear_cache()
1244
1386
pack_compresses = True
1246
1388
def _get_matching_bzrdir(self):
1247
return bzrdir.format_registry.make_bzrdir('development6-rich-root')
1249
def _ignore_setting_bzrdir(self, format):
1252
_matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
1254
def get_format_string(self):
1255
"""See RepositoryFormat.get_format_string()."""
1256
return ('Bazaar development format - group compression and chk inventory'
1257
' (needs bzr.dev from 1.14)\n')
1259
def get_format_description(self):
1260
"""See RepositoryFormat.get_format_description()."""
1261
return ("Development repository format - rich roots, group compression"
1262
" and chk inventories")
1265
class RepositoryFormatCHK2(RepositoryFormatCHK1):
1266
"""A CHK repository that uses the bencode revision serializer."""
1268
_serializer = chk_serializer.chk_bencode_serializer
1270
def _get_matching_bzrdir(self):
1271
return bzrdir.format_registry.make_bzrdir('development7-rich-root')
1273
def _ignore_setting_bzrdir(self, format):
1276
_matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
1278
def get_format_string(self):
1279
"""See RepositoryFormat.get_format_string()."""
1280
return ('Bazaar development format - chk repository with bencode '
1281
'revision serialization (needs bzr.dev from 1.16)\n')
1284
class RepositoryFormat2a(RepositoryFormatCHK2):
1285
"""A CHK repository that uses the bencode revision serializer.
1287
This is the same as RepositoryFormatCHK2 but with a public name.
1290
_serializer = chk_serializer.chk_bencode_serializer
1292
def _get_matching_bzrdir(self):
1293
return bzrdir.format_registry.make_bzrdir('2a')
1295
def _ignore_setting_bzrdir(self, format):
1298
_matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
1300
def get_format_string(self):
1389
return controldir.format_registry.make_bzrdir('2a')
1391
def _ignore_setting_bzrdir(self, format):
1394
_matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
1397
def get_format_string(cls):
1301
1398
return ('Bazaar repository format 2a (needs bzr 1.16 or later)\n')
1303
1400
def get_format_description(self):
1304
1401
"""See RepositoryFormat.get_format_description()."""
1305
1402
return ("Repository format 2a - rich roots, group compression"
1306
1403
" and chk inventories")
1406
class RepositoryFormat2aSubtree(RepositoryFormat2a):
1407
"""A 2a repository format that supports nested trees.
1411
def _get_matching_bzrdir(self):
1412
return controldir.format_registry.make_bzrdir('development-subtree')
1414
def _ignore_setting_bzrdir(self, format):
1417
_matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
1420
def get_format_string(cls):
1421
return ('Bazaar development format 8\n')
1423
def get_format_description(self):
1424
"""See RepositoryFormat.get_format_description()."""
1425
return ("Development repository format 8 - nested trees, "
1426
"group compression and chk inventories")
1429
supports_tree_reference = True