421
424
inventory_keys = source_vf.keys()
422
425
missing_inventories = set(self.revision_keys).difference(inventory_keys)
423
426
if missing_inventories:
424
missing_inventories = sorted(missing_inventories)
425
raise ValueError('We are missing inventories for revisions: %s'
426
% (missing_inventories,))
427
# Go back to the original repo, to see if these are really missing
428
# https://bugs.launchpad.net/bzr/+bug/437003
429
# If we are packing a subset of the repo, it is fine to just have
430
# the data in another Pack file, which is not included in this pack
432
inv_index = self._pack_collection.repo.inventories._index
433
pmap = inv_index.get_parent_map(missing_inventories)
434
really_missing = missing_inventories.difference(pmap)
436
missing_inventories = sorted(really_missing)
437
raise ValueError('We are missing inventories for revisions: %s'
438
% (missing_inventories,))
427
439
self._copy_stream(source_vf, target_vf, inventory_keys,
428
440
'inventories', self._get_filtered_inv_stream, 2)
442
def _get_chk_vfs_for_copy(self):
443
return self._build_vfs('chk', False, False)
430
445
def _copy_chk_texts(self):
431
source_vf, target_vf = self._build_vfs('chk', False, False)
446
source_vf, target_vf = self._get_chk_vfs_for_copy()
432
447
# TODO: This is technically spurious... if it is a performance issue,
434
449
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
child_pb = ui.ui_factory.nested_progress_bar()
618
list(vf_to_stream(source_vf, keys, message, child_pb))
622
def _copy_inventory_texts(self):
623
source_vf, target_vf = self._build_vfs('inventory', True, True)
624
source_chk_vf, target_chk_vf = self._get_chk_vfs_for_copy()
625
inventory_keys = source_vf.keys()
626
# First, copy the existing CHKs on the assumption that most of them
627
# will be correct. This will save us from having to reinsert (and
628
# recompress) these records later at the cost of perhaps preserving a
630
# (Iterate but don't insert _get_filtered_inv_stream to populate the
631
# variables needed by GCCHKPacker._copy_chk_texts.)
632
self._exhaust_stream(source_vf, inventory_keys, 'inventories',
633
self._get_filtered_inv_stream, 2)
634
GCCHKPacker._copy_chk_texts(self)
635
# Now copy and fix the inventories, and any regenerated CHKs.
636
def chk_canonicalizing_inv_stream(source_vf, keys, message, pb=None):
637
return self._get_filtered_canonicalizing_inv_stream(
638
source_vf, keys, message, pb, source_chk_vf, target_chk_vf)
639
self._copy_stream(source_vf, target_vf, inventory_keys,
640
'inventories', chk_canonicalizing_inv_stream, 4)
642
def _copy_chk_texts(self):
643
# No-op; in this class this happens during _copy_inventory_texts.
646
def _get_filtered_canonicalizing_inv_stream(self, source_vf, keys, message,
647
pb=None, source_chk_vf=None, target_chk_vf=None):
648
"""Filter the texts of inventories, regenerating CHKs to make sure they
651
total_keys = len(keys)
652
target_chk_vf = versionedfile.NoDupeAddLinesDecorator(target_chk_vf)
653
def _filtered_inv_stream():
654
stream = source_vf.get_record_stream(keys, 'groupcompress', True)
655
search_key_name = None
656
for idx, record in enumerate(stream):
657
# Inventories should always be with revisions; assume success.
658
bytes = record.get_bytes_as('fulltext')
659
chk_inv = inventory.CHKInventory.deserialise(
660
source_chk_vf, bytes, record.key)
662
pb.update('inv', idx, total_keys)
663
chk_inv.id_to_entry._ensure_root()
664
if search_key_name is None:
665
# Find the name corresponding to the search_key_func
666
search_key_reg = chk_map.search_key_registry
667
for search_key_name, func in search_key_reg.iteritems():
668
if func == chk_inv.id_to_entry._search_key_func:
670
canonical_inv = inventory.CHKInventory.from_inventory(
671
target_chk_vf, chk_inv,
672
maximum_size=chk_inv.id_to_entry._root_node._maximum_size,
673
search_key_name=search_key_name)
674
if chk_inv.id_to_entry.key() != canonical_inv.id_to_entry.key():
676
'Non-canonical CHK map for id_to_entry of inv: %s '
677
'(root is %s, should be %s)' % (chk_inv.revision_id,
678
chk_inv.id_to_entry.key()[0],
679
canonical_inv.id_to_entry.key()[0]))
680
self._data_changed = True
681
p_id_map = chk_inv.parent_id_basename_to_file_id
682
p_id_map._ensure_root()
683
canon_p_id_map = canonical_inv.parent_id_basename_to_file_id
684
if p_id_map.key() != canon_p_id_map.key():
686
'Non-canonical CHK map for parent_id_to_basename of '
687
'inv: %s (root is %s, should be %s)'
688
% (chk_inv.revision_id, p_id_map.key()[0],
689
canon_p_id_map.key()[0]))
690
self._data_changed = True
691
yield versionedfile.ChunkedContentFactory(record.key,
692
record.parents, record.sha1,
693
canonical_inv.to_lines())
694
# We have finished processing all of the inventory records, we
695
# don't need these sets anymore
696
return _filtered_inv_stream()
698
def _use_pack(self, new_pack):
699
"""Override _use_pack to check for reconcile having changed content."""
700
return new_pack.data_inserted() and self._data_changed
583
703
class GCRepositoryPackCollection(RepositoryPackCollection):
585
705
pack_factory = GCPack
715
class CHKInventoryRepository(KnitPackRepository):
716
"""subclass of KnitPackRepository that uses CHK based inventories."""
835
class CHKInventoryRepository(PackRepository):
836
"""subclass of PackRepository that uses CHK based inventories."""
718
838
def __init__(self, _format, a_bzrdir, control_files, _commit_builder_class,
720
840
"""Overridden to change pack collection class."""
721
KnitPackRepository.__init__(self, _format, a_bzrdir, control_files,
841
super(CHKInventoryRepository, self).__init__(_format, a_bzrdir, control_files,
722
842
_commit_builder_class, _serializer)
723
# and now replace everything it did :)
724
843
index_transport = self._transport.clone('indices')
725
844
self._pack_collection = GCRepositoryPackCollection(self,
726
845
self._transport, index_transport,
1124
def reconcile_canonicalize_chks(self):
1125
"""Reconcile this repository to make sure all CHKs are in canonical
1128
from bzrlib.reconcile import PackReconciler
1129
reconciler = PackReconciler(self, thorough=True, canonicalize_chks=True)
1130
reconciler.reconcile()
1002
1133
def _reconcile_pack(self, collection, packs, extension, revs, pb):
1003
1134
packer = GCCHKReconcilePacker(collection, packs, extension)
1004
1135
return packer.pack(pb)
1137
def _canonicalize_chks_pack(self, collection, packs, extension, revs, pb):
1138
packer = GCCHKCanonicalizingPacker(collection, packs, extension, revs)
1139
return packer.pack(pb)
1006
1141
def _get_source(self, to_format):
1007
1142
"""Return a source for streaming from this repository."""
1008
1143
if self._format._serializer == to_format._serializer:
1108
1242
self._chk_p_id_roots = None
1109
1243
yield 'chk_bytes', _get_parent_id_basename_to_file_id_pages()
1245
def _get_text_stream(self):
1246
# Note: We know we don't have to handle adding root keys, because both
1247
# the source and target are the identical network name.
1248
text_stream = self.from_repository.texts.get_record_stream(
1249
self._text_keys, self._text_fetch_order, False)
1250
return ('texts', text_stream)
1111
1252
def get_stream(self, search):
1253
def wrap_and_count(pb, rc, stream):
1254
"""Yield records from stream while showing progress."""
1256
for record in stream:
1257
if count == rc.STEP:
1259
pb.update('Estimate', rc.current, rc.max)
1112
1264
revision_ids = search.get_keys()
1265
pb = ui.ui_factory.nested_progress_bar()
1266
rc = self._record_counter
1267
self._record_counter.setup(len(revision_ids))
1113
1268
for stream_info in self._fetch_revision_texts(revision_ids):
1269
yield (stream_info[0],
1270
wrap_and_count(pb, rc, stream_info[1]))
1115
1271
self._revision_keys = [(rev_id,) for rev_id in revision_ids]
1116
1272
self.from_repository.revisions.clear_cache()
1117
1273
self.from_repository.signatures.clear_cache()
1118
yield self._get_inventory_stream(self._revision_keys)
1274
s = self._get_inventory_stream(self._revision_keys)
1275
yield (s[0], wrap_and_count(pb, rc, s[1]))
1119
1276
self.from_repository.inventories.clear_cache()
1120
1277
# TODO: The keys to exclude might be part of the search recipe
1121
1278
# For now, exclude all parents that are at the edge of ancestry, for
1190
def _filter_text_keys(interesting_nodes_iterable, text_keys, bytes_to_info):
1350
def _filter_text_keys(interesting_nodes_iterable, text_keys, bytes_to_text_key):
1191
1351
"""Iterate the result of iter_interesting_nodes, yielding the records
1192
1352
and adding to text_keys.
1354
text_keys_update = text_keys.update
1194
1355
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())
1356
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."""
1360
class RepositoryFormat2a(RepositoryFormatPack):
1361
"""A CHK repository that uses the bencode revision serializer."""
1211
1363
repository_class = CHKInventoryRepository
1212
1364
supports_external_lookups = True
1213
1365
supports_chks = True
1214
# For right now, setting this to True gives us InterModel1And2 rather
1215
# than InterDifferingSerializer
1216
1366
_commit_builder_class = PackRootCommitBuilder
1217
1367
rich_root_data = True
1218
_serializer = chk_serializer.chk_serializer_255_bigpage
1368
_serializer = chk_serializer.chk_bencode_serializer
1219
1369
_commit_inv_deltas = True
1220
1370
# What index classes to use
1221
1371
index_builder_class = BTreeBuilder
1232
1382
pack_compresses = True
1234
1384
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
1385
return bzrdir.format_registry.make_bzrdir('2a')
1283
1387
def _ignore_setting_bzrdir(self, format):