~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repofmt/groupcompress_repo.py

(gz) Backslash escape selftest output when printing to non-unicode consoles
 (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    knit,
30
30
    osutils,
31
31
    pack,
32
 
    remote,
33
32
    revision as _mod_revision,
34
33
    trace,
35
34
    ui,
 
35
    versionedfile,
36
36
    )
37
37
from bzrlib.btree_index import (
38
38
    BTreeGraphIndex,
39
39
    BTreeBuilder,
40
40
    )
 
41
from bzrlib.decorators import needs_write_lock
41
42
from bzrlib.groupcompress import (
42
43
    _GCGraphIndex,
43
44
    GroupCompressVersionedFiles,
263
264
        remaining_keys = set(keys)
264
265
        counter = [0]
265
266
        if self._gather_text_refs:
266
 
            bytes_to_info = inventory.CHKInventory._bytes_to_utf8name_key
267
267
            self._text_refs = set()
268
268
        def _get_referenced_stream(root_keys, parse_leaf_nodes=False):
269
269
            cur_keys = root_keys
290
290
                    # Store is None, because we know we have a LeafNode, and we
291
291
                    # just want its entries
292
292
                    for file_id, bytes in node.iteritems(None):
293
 
                        name_utf8, file_id, revision_id = bytes_to_info(bytes)
294
 
                        self._text_refs.add((file_id, revision_id))
 
293
                        self._text_refs.add(chk_map._bytes_to_text_key(bytes))
295
294
                def next_stream():
296
295
                    stream = source_vf.get_record_stream(cur_keys,
297
296
                                                         'as-requested', True)
428
427
        self._copy_stream(source_vf, target_vf, inventory_keys,
429
428
                          'inventories', self._get_filtered_inv_stream, 2)
430
429
 
 
430
    def _get_chk_vfs_for_copy(self):
 
431
        return self._build_vfs('chk', False, False)
 
432
 
431
433
    def _copy_chk_texts(self):
432
 
        source_vf, target_vf = self._build_vfs('chk', False, False)
 
434
        source_vf, target_vf = self._get_chk_vfs_for_copy()
433
435
        # TODO: This is technically spurious... if it is a performance issue,
434
436
        #       remove it
435
437
        total_keys = source_vf.keys()
581
583
        return new_pack.data_inserted() and self._data_changed
582
584
 
583
585
 
 
586
class GCCHKCanonicalizingPacker(GCCHKPacker):
 
587
    """A packer that ensures inventories have canonical-form CHK maps.
 
588
    
 
589
    Ideally this would be part of reconcile, but it's very slow and rarely
 
590
    needed.  (It repairs repositories affected by
 
591
    https://bugs.launchpad.net/bzr/+bug/522637).
 
592
    """
 
593
 
 
594
    def __init__(self, *args, **kwargs):
 
595
        super(GCCHKCanonicalizingPacker, self).__init__(*args, **kwargs)
 
596
        self._data_changed = False
 
597
    
 
598
    def _exhaust_stream(self, source_vf, keys, message, vf_to_stream, pb_offset):
 
599
        """Create and exhaust a stream, but don't insert it.
 
600
        
 
601
        This is useful to get the side-effects of generating a stream.
 
602
        """
 
603
        self.pb.update('scanning %s' % (message,), pb_offset)
 
604
        child_pb = ui.ui_factory.nested_progress_bar()
 
605
        try:
 
606
            list(vf_to_stream(source_vf, keys, message, child_pb))
 
607
        finally:
 
608
            child_pb.finished()
 
609
 
 
610
    def _copy_inventory_texts(self):
 
611
        source_vf, target_vf = self._build_vfs('inventory', True, True)
 
612
        source_chk_vf, target_chk_vf = self._get_chk_vfs_for_copy()
 
613
        inventory_keys = source_vf.keys()
 
614
        # First, copy the existing CHKs on the assumption that most of them
 
615
        # will be correct.  This will save us from having to reinsert (and
 
616
        # recompress) these records later at the cost of perhaps preserving a
 
617
        # few unused CHKs. 
 
618
        # (Iterate but don't insert _get_filtered_inv_stream to populate the
 
619
        # variables needed by GCCHKPacker._copy_chk_texts.)
 
620
        self._exhaust_stream(source_vf, inventory_keys, 'inventories',
 
621
                self._get_filtered_inv_stream, 2)
 
622
        GCCHKPacker._copy_chk_texts(self)
 
623
        # Now copy and fix the inventories, and any regenerated CHKs.
 
624
        def chk_canonicalizing_inv_stream(source_vf, keys, message, pb=None):
 
625
            return self._get_filtered_canonicalizing_inv_stream(
 
626
                source_vf, keys, message, pb, source_chk_vf, target_chk_vf)
 
627
        self._copy_stream(source_vf, target_vf, inventory_keys,
 
628
                          'inventories', chk_canonicalizing_inv_stream, 4)
 
629
 
 
630
    def _copy_chk_texts(self):
 
631
        # No-op; in this class this happens during _copy_inventory_texts.
 
632
        pass
 
633
 
 
634
    def _get_filtered_canonicalizing_inv_stream(self, source_vf, keys, message,
 
635
            pb=None, source_chk_vf=None, target_chk_vf=None):
 
636
        """Filter the texts of inventories, regenerating CHKs to make sure they
 
637
        are canonical.
 
638
        """
 
639
        total_keys = len(keys)
 
640
        target_chk_vf = versionedfile.NoDupeAddLinesDecorator(target_chk_vf)
 
641
        def _filtered_inv_stream():
 
642
            stream = source_vf.get_record_stream(keys, 'groupcompress', True)
 
643
            search_key_name = None
 
644
            for idx, record in enumerate(stream):
 
645
                # Inventories should always be with revisions; assume success.
 
646
                bytes = record.get_bytes_as('fulltext')
 
647
                chk_inv = inventory.CHKInventory.deserialise(
 
648
                    source_chk_vf, bytes, record.key)
 
649
                if pb is not None:
 
650
                    pb.update('inv', idx, total_keys)
 
651
                chk_inv.id_to_entry._ensure_root()
 
652
                if search_key_name is None:
 
653
                    # Find the name corresponding to the search_key_func
 
654
                    search_key_reg = chk_map.search_key_registry
 
655
                    for search_key_name, func in search_key_reg.iteritems():
 
656
                        if func == chk_inv.id_to_entry._search_key_func:
 
657
                            break
 
658
                canonical_inv = inventory.CHKInventory.from_inventory(
 
659
                    target_chk_vf, chk_inv,
 
660
                    maximum_size=chk_inv.id_to_entry._root_node._maximum_size,
 
661
                    search_key_name=search_key_name)
 
662
                if chk_inv.id_to_entry.key() != canonical_inv.id_to_entry.key():
 
663
                    trace.mutter(
 
664
                        'Non-canonical CHK map for id_to_entry of inv: %s '
 
665
                        '(root is %s, should be %s)' % (chk_inv.revision_id,
 
666
                        chk_inv.id_to_entry.key()[0],
 
667
                        canonical_inv.id_to_entry.key()[0]))
 
668
                    self._data_changed = True
 
669
                p_id_map = chk_inv.parent_id_basename_to_file_id
 
670
                p_id_map._ensure_root()
 
671
                canon_p_id_map = canonical_inv.parent_id_basename_to_file_id
 
672
                if p_id_map.key() != canon_p_id_map.key():
 
673
                    trace.mutter(
 
674
                        'Non-canonical CHK map for parent_id_to_basename of '
 
675
                        'inv: %s (root is %s, should be %s)'
 
676
                        % (chk_inv.revision_id, p_id_map.key()[0],
 
677
                           canon_p_id_map.key()[0]))
 
678
                    self._data_changed = True
 
679
                yield versionedfile.ChunkedContentFactory(record.key,
 
680
                        record.parents, record.sha1,
 
681
                        canonical_inv.to_lines())
 
682
            # We have finished processing all of the inventory records, we
 
683
            # don't need these sets anymore
 
684
        return _filtered_inv_stream()
 
685
 
 
686
    def _use_pack(self, new_pack):
 
687
        """Override _use_pack to check for reconcile having changed content."""
 
688
        return new_pack.data_inserted() and self._data_changed
 
689
 
 
690
 
584
691
class GCRepositoryPackCollection(RepositoryPackCollection):
585
692
 
586
693
    pack_factory = GCPack
648
755
        chk_diff = chk_map.iter_interesting_nodes(
649
756
            chk_bytes_no_fallbacks, root_key_info.interesting_root_keys,
650
757
            root_key_info.uninteresting_root_keys)
651
 
        bytes_to_info = inventory.CHKInventory._bytes_to_utf8name_key
652
758
        text_keys = set()
653
759
        try:
654
 
            for record in _filter_text_keys(chk_diff, text_keys, bytes_to_info):
 
760
            for record in _filter_text_keys(chk_diff, text_keys,
 
761
                                            chk_map._bytes_to_text_key):
655
762
                pass
656
763
        except errors.NoSuchRevision, e:
657
764
            # XXX: It would be nice if we could give a more precise error here.
865
972
        if basis_inv is None:
866
973
            if basis_revision_id == _mod_revision.NULL_REVISION:
867
974
                new_inv = self._create_inv_from_null(delta, new_revision_id)
 
975
                if new_inv.root_id is None:
 
976
                    raise errors.RootMissing()
868
977
                inv_lines = new_inv.to_lines()
869
978
                return self._inventory_add_lines(new_revision_id, parents,
870
979
                    inv_lines, check_content=False), new_inv
1000
1109
        finally:
1001
1110
            pb.finished()
1002
1111
 
 
1112
    @needs_write_lock
 
1113
    def reconcile_canonicalize_chks(self):
 
1114
        """Reconcile this repository to make sure all CHKs are in canonical
 
1115
        form.
 
1116
        """
 
1117
        from bzrlib.reconcile import PackReconciler
 
1118
        reconciler = PackReconciler(self, thorough=True, canonicalize_chks=True)
 
1119
        reconciler.reconcile()
 
1120
        return reconciler
 
1121
 
1003
1122
    def _reconcile_pack(self, collection, packs, extension, revs, pb):
1004
1123
        packer = GCCHKReconcilePacker(collection, packs, extension)
1005
1124
        return packer.pack(pb)
1006
1125
 
 
1126
    def _canonicalize_chks_pack(self, collection, packs, extension, revs, pb):
 
1127
        packer = GCCHKCanonicalizingPacker(collection, packs, extension, revs)
 
1128
        return packer.pack(pb)
 
1129
 
1007
1130
    def _get_source(self, to_format):
1008
1131
        """Return a source for streaming from this repository."""
1009
1132
        if self._format._serializer == to_format._serializer:
1088
1211
                uninteresting_root_keys.add(inv.id_to_entry.key())
1089
1212
                uninteresting_pid_root_keys.add(
1090
1213
                    inv.parent_id_basename_to_file_id.key())
1091
 
        bytes_to_info = inventory.CHKInventory._bytes_to_utf8name_key
1092
1214
        chk_bytes = self.from_repository.chk_bytes
1093
1215
        def _filter_id_to_entry():
1094
1216
            interesting_nodes = chk_map.iter_interesting_nodes(chk_bytes,
1095
1217
                        self._chk_id_roots, uninteresting_root_keys)
1096
1218
            for record in _filter_text_keys(interesting_nodes, self._text_keys,
1097
 
                    bytes_to_info):
 
1219
                    chk_map._bytes_to_text_key):
1098
1220
                if record is not None:
1099
1221
                    yield record
1100
1222
            # Consumed
1110
1232
        yield 'chk_bytes', _get_parent_id_basename_to_file_id_pages()
1111
1233
 
1112
1234
    def get_stream(self, search):
 
1235
        def wrap_and_count(pb, rc, stream):
 
1236
            """Yield records from stream while showing progress."""
 
1237
            count = 0
 
1238
            for record in stream:
 
1239
                if count == rc.STEP:
 
1240
                    rc.increment(count)
 
1241
                    pb.update('Estimate', rc.current, rc.max)
 
1242
                    count = 0
 
1243
                count += 1
 
1244
                yield record
 
1245
 
1113
1246
        revision_ids = search.get_keys()
 
1247
        pb = ui.ui_factory.nested_progress_bar()
 
1248
        rc = self._record_counter
 
1249
        self._record_counter.setup(len(revision_ids))
1114
1250
        for stream_info in self._fetch_revision_texts(revision_ids):
1115
 
            yield stream_info
 
1251
            yield (stream_info[0],
 
1252
                wrap_and_count(pb, rc, stream_info[1]))
1116
1253
        self._revision_keys = [(rev_id,) for rev_id in revision_ids]
1117
1254
        self.from_repository.revisions.clear_cache()
1118
1255
        self.from_repository.signatures.clear_cache()
1119
 
        yield self._get_inventory_stream(self._revision_keys)
 
1256
        s = self._get_inventory_stream(self._revision_keys)
 
1257
        yield (s[0], wrap_and_count(pb, rc, s[1]))
1120
1258
        self.from_repository.inventories.clear_cache()
1121
1259
        # TODO: The keys to exclude might be part of the search recipe
1122
1260
        # For now, exclude all parents that are at the edge of ancestry, for
1125
1263
        parent_keys = from_repo._find_parent_keys_of_revisions(
1126
1264
                        self._revision_keys)
1127
1265
        for stream_info in self._get_filtered_chk_streams(parent_keys):
1128
 
            yield stream_info
 
1266
            yield (stream_info[0], wrap_and_count(pb, rc, stream_info[1]))
1129
1267
        self.from_repository.chk_bytes.clear_cache()
1130
 
        yield self._get_text_stream()
 
1268
        s = self._get_text_stream()
 
1269
        yield (s[0], wrap_and_count(pb, rc, s[1]))
1131
1270
        self.from_repository.texts.clear_cache()
 
1271
        pb.update('Done', rc.max, rc.max)
 
1272
        pb.finished()
1132
1273
 
1133
1274
    def get_stream_for_missing_keys(self, missing_keys):
1134
1275
        # missing keys can only occur when we are byte copying and not
1188
1329
    return result
1189
1330
 
1190
1331
 
1191
 
def _filter_text_keys(interesting_nodes_iterable, text_keys, bytes_to_info):
 
1332
def _filter_text_keys(interesting_nodes_iterable, text_keys, bytes_to_text_key):
1192
1333
    """Iterate the result of iter_interesting_nodes, yielding the records
1193
1334
    and adding to text_keys.
1194
1335
    """
 
1336
    text_keys_update = text_keys.update
1195
1337
    for record, items in interesting_nodes_iterable:
1196
 
        for name, bytes in items:
1197
 
            # Note: we don't care about name_utf8, because groupcompress repos
1198
 
            # are always rich-root, so there are no synthesised root records to
1199
 
            # ignore.
1200
 
            _, file_id, revision_id = bytes_to_info(bytes)
1201
 
            file_id = intern(file_id)
1202
 
            revision_id = intern(revision_id)
1203
 
            text_keys.add(StaticTuple(file_id, revision_id).intern())
 
1338
        text_keys_update([bytes_to_text_key(b) for n,b in items])
1204
1339
        yield record
1205
1340
 
1206
1341
 
1207
 
 
1208
 
 
1209
 
class RepositoryFormatCHK1(RepositoryFormatPack):
1210
 
    """A hashed CHK+group compress pack repository."""
 
1342
class RepositoryFormat2a(RepositoryFormatPack):
 
1343
    """A CHK repository that uses the bencode revision serializer."""
1211
1344
 
1212
1345
    repository_class = CHKInventoryRepository
1213
1346
    supports_external_lookups = True
1214
1347
    supports_chks = True
1215
 
    # For right now, setting this to True gives us InterModel1And2 rather
1216
 
    # than InterDifferingSerializer
1217
1348
    _commit_builder_class = PackRootCommitBuilder
1218
1349
    rich_root_data = True
1219
 
    _serializer = chk_serializer.chk_serializer_255_bigpage
 
1350
    _serializer = chk_serializer.chk_bencode_serializer
1220
1351
    _commit_inv_deltas = True
1221
1352
    # What index classes to use
1222
1353
    index_builder_class = BTreeBuilder
1233
1364
    pack_compresses = True
1234
1365
 
1235
1366
    def _get_matching_bzrdir(self):
1236
 
        return bzrdir.format_registry.make_bzrdir('development6-rich-root')
1237
 
 
1238
 
    def _ignore_setting_bzrdir(self, format):
1239
 
        pass
1240
 
 
1241
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
1242
 
 
1243
 
    def get_format_string(self):
1244
 
        """See RepositoryFormat.get_format_string()."""
1245
 
        return ('Bazaar development format - group compression and chk inventory'
1246
 
                ' (needs bzr.dev from 1.14)\n')
1247
 
 
1248
 
    def get_format_description(self):
1249
 
        """See RepositoryFormat.get_format_description()."""
1250
 
        return ("Development repository format - rich roots, group compression"
1251
 
            " and chk inventories")
1252
 
 
1253
 
 
1254
 
class RepositoryFormatCHK2(RepositoryFormatCHK1):
1255
 
    """A CHK repository that uses the bencode revision serializer."""
1256
 
 
1257
 
    _serializer = chk_serializer.chk_bencode_serializer
1258
 
 
1259
 
    def _get_matching_bzrdir(self):
1260
 
        return bzrdir.format_registry.make_bzrdir('development7-rich-root')
1261
 
 
1262
 
    def _ignore_setting_bzrdir(self, format):
1263
 
        pass
1264
 
 
1265
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
1266
 
 
1267
 
    def get_format_string(self):
1268
 
        """See RepositoryFormat.get_format_string()."""
1269
 
        return ('Bazaar development format - chk repository with bencode '
1270
 
                'revision serialization (needs bzr.dev from 1.16)\n')
1271
 
 
1272
 
 
1273
 
class RepositoryFormat2a(RepositoryFormatCHK2):
1274
 
    """A CHK repository that uses the bencode revision serializer.
1275
 
 
1276
 
    This is the same as RepositoryFormatCHK2 but with a public name.
1277
 
    """
1278
 
 
1279
 
    _serializer = chk_serializer.chk_bencode_serializer
1280
 
 
1281
 
    def _get_matching_bzrdir(self):
1282
1367
        return bzrdir.format_registry.make_bzrdir('2a')
1283
1368
 
1284
1369
    def _ignore_setting_bzrdir(self, format):
1293
1378
        """See RepositoryFormat.get_format_description()."""
1294
1379
        return ("Repository format 2a - rich roots, group compression"
1295
1380
            " and chk inventories")
 
1381
 
 
1382
 
 
1383
class RepositoryFormat2aSubtree(RepositoryFormat2a):
 
1384
    """A 2a repository format that supports nested trees.
 
1385
 
 
1386
    """
 
1387
 
 
1388
    def _get_matching_bzrdir(self):
 
1389
        return bzrdir.format_registry.make_bzrdir('development-subtree')
 
1390
 
 
1391
    def _ignore_setting_bzrdir(self, format):
 
1392
        pass
 
1393
 
 
1394
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
 
1395
 
 
1396
    def get_format_string(self):
 
1397
        return ('Bazaar development format 8\n')
 
1398
 
 
1399
    def get_format_description(self):
 
1400
        """See RepositoryFormat.get_format_description()."""
 
1401
        return ("Development repository format 8 - nested trees, "
 
1402
                "group compression and chk inventories")
 
1403
 
 
1404
    experimental = True
 
1405
    supports_tree_reference = True