/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/repofmt/groupcompress_repo.py

  • Committer: Martin Pool
  • Date: 2009-09-14 01:48:28 UTC
  • mfrom: (4685 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4688.
  • Revision ID: mbp@sourcefrog.net-20090914014828-ydr9rlkdfq2sv57z
Merge news

Show diffs side-by-side

added added

removed removed

Lines of Context:
154
154
        self._writer.begin()
155
155
        # what state is the pack in? (open, finished, aborted)
156
156
        self._state = 'open'
 
157
        # no name until we finish writing the content
 
158
        self.name = None
157
159
 
158
160
    def _check_references(self):
159
161
        """Make sure our external references are present.
410
412
 
411
413
    def _copy_inventory_texts(self):
412
414
        source_vf, target_vf = self._build_vfs('inventory', True, True)
413
 
        self._copy_stream(source_vf, target_vf, self.revision_keys,
 
415
        # It is not sufficient to just use self.revision_keys, as stacked
 
416
        # repositories can have more inventories than they have revisions.
 
417
        # One alternative would be to do something with
 
418
        # get_parent_map(self.revision_keys), but that shouldn't be any faster
 
419
        # than this.
 
420
        inventory_keys = source_vf.keys()
 
421
        missing_inventories = set(self.revision_keys).difference(inventory_keys)
 
422
        if missing_inventories:
 
423
            missing_inventories = sorted(missing_inventories)
 
424
            raise ValueError('We are missing inventories for revisions: %s'
 
425
                % (missing_inventories,))
 
426
        self._copy_stream(source_vf, target_vf, inventory_keys,
414
427
                          'inventories', self._get_filtered_inv_stream, 2)
415
428
 
416
429
    def _copy_chk_texts(self):
466
479
        if not self._use_pack(self.new_pack):
467
480
            self.new_pack.abort()
468
481
            return None
 
482
        self.new_pack.finish_content()
 
483
        if len(self.packs) == 1:
 
484
            old_pack = self.packs[0]
 
485
            if old_pack.name == self.new_pack._hash.hexdigest():
 
486
                # The single old pack was already optimally packed.
 
487
                trace.mutter('single pack %s was already optimally packed',
 
488
                    old_pack.name)
 
489
                self.new_pack.abort()
 
490
                return None
469
491
        self.pb.update('finishing repack', 6, 7)
470
492
        self.new_pack.finish()
471
493
        self._pack_collection.allocate(self.new_pack)
562
584
    pack_factory = GCPack
563
585
    resumed_pack_factory = ResumedGCPack
564
586
 
 
587
    def _check_new_inventories(self):
 
588
        """Detect missing inventories or chk root entries for the new revisions
 
589
        in this write group.
 
590
 
 
591
        :returns: list of strs, summarising any problems found.  If the list is
 
592
            empty no problems were found.
 
593
        """
 
594
        # Ensure that all revisions added in this write group have:
 
595
        #   - corresponding inventories,
 
596
        #   - chk root entries for those inventories,
 
597
        #   - and any present parent inventories have their chk root
 
598
        #     entries too.
 
599
        # And all this should be independent of any fallback repository.
 
600
        problems = []
 
601
        key_deps = self.repo.revisions._index._key_dependencies
 
602
        new_revisions_keys = key_deps.get_new_keys()
 
603
        no_fallback_inv_index = self.repo.inventories._index
 
604
        no_fallback_chk_bytes_index = self.repo.chk_bytes._index
 
605
        no_fallback_texts_index = self.repo.texts._index
 
606
        inv_parent_map = no_fallback_inv_index.get_parent_map(
 
607
            new_revisions_keys)
 
608
        # Are any inventories for corresponding to the new revisions missing?
 
609
        corresponding_invs = set(inv_parent_map)
 
610
        missing_corresponding = set(new_revisions_keys)
 
611
        missing_corresponding.difference_update(corresponding_invs)
 
612
        if missing_corresponding:
 
613
            problems.append("inventories missing for revisions %s" %
 
614
                (sorted(missing_corresponding),))
 
615
            return problems
 
616
        # Are any chk root entries missing for any inventories?  This includes
 
617
        # any present parent inventories, which may be used when calculating
 
618
        # deltas for streaming.
 
619
        all_inv_keys = set(corresponding_invs)
 
620
        for parent_inv_keys in inv_parent_map.itervalues():
 
621
            all_inv_keys.update(parent_inv_keys)
 
622
        # Filter out ghost parents.
 
623
        all_inv_keys.intersection_update(
 
624
            no_fallback_inv_index.get_parent_map(all_inv_keys))
 
625
        parent_invs_only_keys = all_inv_keys.symmetric_difference(
 
626
            corresponding_invs)
 
627
        all_missing = set()
 
628
        inv_ids = [key[-1] for key in all_inv_keys]
 
629
        parent_invs_only_ids = [key[-1] for key in parent_invs_only_keys]
 
630
        root_key_info = _build_interesting_key_sets(
 
631
            self.repo, inv_ids, parent_invs_only_ids)
 
632
        expected_chk_roots = root_key_info.all_keys()
 
633
        present_chk_roots = no_fallback_chk_bytes_index.get_parent_map(
 
634
            expected_chk_roots)
 
635
        missing_chk_roots = expected_chk_roots.difference(present_chk_roots)
 
636
        if missing_chk_roots:
 
637
            problems.append("missing referenced chk root keys: %s"
 
638
                % (sorted(missing_chk_roots),))
 
639
            # Don't bother checking any further.
 
640
            return problems
 
641
        # Find all interesting chk_bytes records, and make sure they are
 
642
        # present, as well as the text keys they reference.
 
643
        chk_bytes_no_fallbacks = self.repo.chk_bytes.without_fallbacks()
 
644
        chk_bytes_no_fallbacks._search_key_func = \
 
645
            self.repo.chk_bytes._search_key_func
 
646
        chk_diff = chk_map.iter_interesting_nodes(
 
647
            chk_bytes_no_fallbacks, root_key_info.interesting_root_keys,
 
648
            root_key_info.uninteresting_root_keys)
 
649
        bytes_to_info = inventory.CHKInventory._bytes_to_utf8name_key
 
650
        text_keys = set()
 
651
        try:
 
652
            for record in _filter_text_keys(chk_diff, text_keys, bytes_to_info):
 
653
                pass
 
654
        except errors.NoSuchRevision, e:
 
655
            # XXX: It would be nice if we could give a more precise error here.
 
656
            problems.append("missing chk node(s) for id_to_entry maps")
 
657
        chk_diff = chk_map.iter_interesting_nodes(
 
658
            chk_bytes_no_fallbacks, root_key_info.interesting_pid_root_keys,
 
659
            root_key_info.uninteresting_pid_root_keys)
 
660
        try:
 
661
            for interesting_rec, interesting_map in chk_diff:
 
662
                pass
 
663
        except errors.NoSuchRevision, e:
 
664
            problems.append(
 
665
                "missing chk node(s) for parent_id_basename_to_file_id maps")
 
666
        present_text_keys = no_fallback_texts_index.get_parent_map(text_keys)
 
667
        missing_text_keys = text_keys.difference(present_text_keys)
 
668
        if missing_text_keys:
 
669
            problems.append("missing text keys: %r"
 
670
                % (sorted(missing_text_keys),))
 
671
        return problems
 
672
 
565
673
    def _execute_pack_operations(self, pack_operations,
566
674
                                 _packer_class=GCCHKPacker,
567
675
                                 reload_func=None):
580
688
            packer = GCCHKPacker(self, packs, '.autopack',
581
689
                                 reload_func=reload_func)
582
690
            try:
583
 
                packer.pack()
 
691
                result = packer.pack()
584
692
            except errors.RetryWithNewPacks:
585
693
                # An exception is propagating out of this context, make sure
586
694
                # this packer has cleaned up. Packer() doesn't set its new_pack
589
697
                if packer.new_pack is not None:
590
698
                    packer.new_pack.abort()
591
699
                raise
 
700
            if result is None:
 
701
                return
592
702
            for pack in packs:
593
703
                self._remove_pack_from_memory(pack)
594
704
        # record the newly available packs and stop advertising the old
595
705
        # packs
596
 
        self._save_pack_names(clear_obsolete_packs=True)
 
706
        result = self._save_pack_names(clear_obsolete_packs=True)
597
707
        # Move the old packs out of the way now they are no longer referenced.
598
708
        for revision_count, packs in pack_operations:
599
709
            self._obsolete_packs(packs)
 
710
        return result
600
711
 
601
712
 
602
713
class CHKInventoryRepository(KnitPackRepository):
627
738
            _GCGraphIndex(self._pack_collection.revision_index.combined_index,
628
739
                add_callback=self._pack_collection.revision_index.add_callback,
629
740
                parents=True, is_locked=self.is_locked,
630
 
                track_external_parent_refs=True),
 
741
                track_external_parent_refs=True, track_new_keys=True),
631
742
            access=self._pack_collection.revision_index.data_access,
632
743
            delta=False)
633
744
        self.signatures = GroupCompressVersionedFiles(
766
877
            if basis_tree is not None:
767
878
                basis_tree.unlock()
768
879
 
769
 
    def _iter_inventories(self, revision_ids):
 
880
    def deserialise_inventory(self, revision_id, bytes):
 
881
        return inventory.CHKInventory.deserialise(self.chk_bytes, bytes,
 
882
            (revision_id,))
 
883
 
 
884
    def _iter_inventories(self, revision_ids, ordering):
770
885
        """Iterate over many inventory objects."""
 
886
        if ordering is None:
 
887
            ordering = 'unordered'
771
888
        keys = [(revision_id,) for revision_id in revision_ids]
772
 
        stream = self.inventories.get_record_stream(keys, 'unordered', True)
 
889
        stream = self.inventories.get_record_stream(keys, ordering, True)
773
890
        texts = {}
774
891
        for record in stream:
775
892
            if record.storage_kind != 'absent':
779
896
        for key in keys:
780
897
            yield inventory.CHKInventory.deserialise(self.chk_bytes, texts[key], key)
781
898
 
782
 
    def _iter_inventory_xmls(self, revision_ids):
783
 
        # Without a native 'xml' inventory, this method doesn't make sense, so
784
 
        # make it raise to trap naughty direct users.
785
 
        raise NotImplementedError(self._iter_inventory_xmls)
 
899
    def _iter_inventory_xmls(self, revision_ids, ordering):
 
900
        # Without a native 'xml' inventory, this method doesn't make sense.
 
901
        # However older working trees, and older bundles want it - so we supply
 
902
        # it allowing get_inventory_xml to work. Bundles currently use the
 
903
        # serializer directly; this also isn't ideal, but there isn't an xml
 
904
        # iteration interface offered at all for repositories. We could make
 
905
        # _iter_inventory_xmls be part of the contract, even if kept private.
 
906
        inv_to_str = self._serializer.write_inventory_to_string
 
907
        for inv in self.iter_inventories(revision_ids, ordering=ordering):
 
908
            yield inv_to_str(inv), inv.revision_id
786
909
 
787
910
    def _find_present_inventory_keys(self, revision_keys):
788
911
        parent_map = self.inventories.get_parent_map(revision_keys)
814
937
                                        parent_keys)
815
938
            present_parent_inv_ids = set(
816
939
                [k[-1] for k in present_parent_inv_keys])
817
 
            uninteresting_root_keys = set()
818
 
            interesting_root_keys = set()
819
940
            inventories_to_read = set(revision_ids)
820
941
            inventories_to_read.update(present_parent_inv_ids)
821
 
            for inv in self.iter_inventories(inventories_to_read):
822
 
                entry_chk_root_key = inv.id_to_entry.key()
823
 
                if inv.revision_id in present_parent_inv_ids:
824
 
                    uninteresting_root_keys.add(entry_chk_root_key)
825
 
                else:
826
 
                    interesting_root_keys.add(entry_chk_root_key)
827
 
 
 
942
            root_key_info = _build_interesting_key_sets(
 
943
                self, inventories_to_read, present_parent_inv_ids)
 
944
            interesting_root_keys = root_key_info.interesting_root_keys
 
945
            uninteresting_root_keys = root_key_info.uninteresting_root_keys
828
946
            chk_bytes = self.chk_bytes
829
947
            for record, items in chk_map.iter_interesting_nodes(chk_bytes,
830
948
                        interesting_root_keys, uninteresting_root_keys,
879
997
 
880
998
    def _get_source(self, to_format):
881
999
        """Return a source for streaming from this repository."""
882
 
        if isinstance(to_format, remote.RemoteRepositoryFormat):
883
 
            # Can't just check attributes on to_format with the current code,
884
 
            # work around this:
885
 
            to_format._ensure_real()
886
 
            to_format = to_format._custom_format
887
 
        if to_format.__class__ is self._format.__class__:
 
1000
        if self._format._serializer == to_format._serializer:
888
1001
            # We must be exactly the same format, otherwise stuff like the chk
889
 
            # page layout might be different
 
1002
            # page layout might be different.
 
1003
            # Actually, this test is just slightly looser than exact so that
 
1004
            # CHK2 <-> 2a transfers will work.
890
1005
            return GroupCHKStreamSource(self, to_format)
891
1006
        return super(CHKInventoryRepository, self)._get_source(to_format)
892
1007
 
967
1082
        bytes_to_info = inventory.CHKInventory._bytes_to_utf8name_key
968
1083
        chk_bytes = self.from_repository.chk_bytes
969
1084
        def _filter_id_to_entry():
970
 
            for record, items in chk_map.iter_interesting_nodes(chk_bytes,
971
 
                        self._chk_id_roots, uninteresting_root_keys):
972
 
                for name, bytes in items:
973
 
                    # Note: we don't care about name_utf8, because we are always
974
 
                    # rich-root = True
975
 
                    _, file_id, revision_id = bytes_to_info(bytes)
976
 
                    self._text_keys.add((file_id, revision_id))
 
1085
            interesting_nodes = chk_map.iter_interesting_nodes(chk_bytes,
 
1086
                        self._chk_id_roots, uninteresting_root_keys)
 
1087
            for record in _filter_text_keys(interesting_nodes, self._text_keys,
 
1088
                    bytes_to_info):
977
1089
                if record is not None:
978
1090
                    yield record
979
1091
            # Consumed
1017
1129
            missing_inventory_keys.add(key[1:])
1018
1130
        if self._chk_id_roots or self._chk_p_id_roots:
1019
1131
            raise AssertionError('Cannot call get_stream_for_missing_keys'
1020
 
                ' untill all of get_stream() has been consumed.')
 
1132
                ' until all of get_stream() has been consumed.')
1021
1133
        # Yield the inventory stream, so we can find the chk stream
1022
1134
        # Some of the missing_keys will be missing because they are ghosts.
1023
1135
        # As such, we can ignore them. The Sink is required to verify there are
1030
1142
            yield stream_info
1031
1143
 
1032
1144
 
 
1145
class _InterestingKeyInfo(object):
 
1146
    def __init__(self):
 
1147
        self.interesting_root_keys = set()
 
1148
        self.interesting_pid_root_keys = set()
 
1149
        self.uninteresting_root_keys = set()
 
1150
        self.uninteresting_pid_root_keys = set()
 
1151
 
 
1152
    def all_interesting(self):
 
1153
        return self.interesting_root_keys.union(self.interesting_pid_root_keys)
 
1154
 
 
1155
    def all_uninteresting(self):
 
1156
        return self.uninteresting_root_keys.union(
 
1157
            self.uninteresting_pid_root_keys)
 
1158
 
 
1159
    def all_keys(self):
 
1160
        return self.all_interesting().union(self.all_uninteresting())
 
1161
 
 
1162
 
 
1163
def _build_interesting_key_sets(repo, inventory_ids, parent_only_inv_ids):
 
1164
    result = _InterestingKeyInfo()
 
1165
    for inv in repo.iter_inventories(inventory_ids, 'unordered'):
 
1166
        root_key = inv.id_to_entry.key()
 
1167
        pid_root_key = inv.parent_id_basename_to_file_id.key()
 
1168
        if inv.revision_id in parent_only_inv_ids:
 
1169
            result.uninteresting_root_keys.add(root_key)
 
1170
            result.uninteresting_pid_root_keys.add(pid_root_key)
 
1171
        else:
 
1172
            result.interesting_root_keys.add(root_key)
 
1173
            result.interesting_pid_root_keys.add(pid_root_key)
 
1174
    return result
 
1175
 
 
1176
 
 
1177
def _filter_text_keys(interesting_nodes_iterable, text_keys, bytes_to_info):
 
1178
    """Iterate the result of iter_interesting_nodes, yielding the records
 
1179
    and adding to text_keys.
 
1180
    """
 
1181
    for record, items in interesting_nodes_iterable:
 
1182
        for name, bytes in items:
 
1183
            # Note: we don't care about name_utf8, because groupcompress repos
 
1184
            # are always rich-root, so there are no synthesised root records to
 
1185
            # ignore.
 
1186
            _, file_id, revision_id = bytes_to_info(bytes)
 
1187
            text_keys.add((file_id, revision_id))
 
1188
        yield record
 
1189
 
 
1190
 
 
1191
 
 
1192
 
1033
1193
class RepositoryFormatCHK1(RepositoryFormatPack):
1034
1194
    """A hashed CHK+group compress pack repository."""
1035
1195
 
1074
1234
        return ("Development repository format - rich roots, group compression"
1075
1235
            " and chk inventories")
1076
1236
 
1077
 
    def check_conversion_target(self, target_format):
1078
 
        if not target_format.rich_root_data:
1079
 
            raise errors.BadConversionTarget(
1080
 
                'Does not support rich root data.', target_format)
1081
 
        if (self.supports_tree_reference and 
1082
 
            not getattr(target_format, 'supports_tree_reference', False)):
1083
 
            raise errors.BadConversionTarget(
1084
 
                'Does not support nested trees', target_format)
1085
 
 
1086
 
 
1087
1237
 
1088
1238
class RepositoryFormatCHK2(RepositoryFormatCHK1):
1089
1239
    """A CHK repository that uses the bencode revision serializer."""
1106
1256
 
1107
1257
class RepositoryFormat2a(RepositoryFormatCHK2):
1108
1258
    """A CHK repository that uses the bencode revision serializer.
1109
 
    
 
1259
 
1110
1260
    This is the same as RepositoryFormatCHK2 but with a public name.
1111
1261
    """
1112
1262
 
1122
1272
 
1123
1273
    def get_format_string(self):
1124
1274
        return ('Bazaar repository format 2a (needs bzr 1.16 or later)\n')
 
1275
 
 
1276
    def get_format_description(self):
 
1277
        """See RepositoryFormat.get_format_description()."""
 
1278
        return ("Repository format 2a - rich roots, group compression"
 
1279
            " and chk inventories")