/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 breezy/graph.py

  • Committer: Jelmer Vernooij
  • Date: 2018-06-14 17:59:16 UTC
  • mto: This revision was merged to the branch mainline in revision 7065.
  • Revision ID: jelmer@jelmer.uk-20180614175916-a2e2xh5k533guq1x
Move breezy.plugins.git to breezy.git.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
from __future__ import absolute_import
 
18
 
17
19
import time
18
20
 
19
21
from . import (
23
25
    revision,
24
26
    trace,
25
27
    )
 
28
from .sixish import (
 
29
    viewitems,
 
30
    viewvalues,
 
31
    )
26
32
 
27
33
STEP_UNIQUE_SEARCHER_EVERY = 5
28
34
 
135
141
 
136
142
    The cache is enabled by default, but may be disabled and re-enabled.
137
143
    """
138
 
 
139
144
    def __init__(self, parent_provider=None, get_parent_map=None):
140
145
        """Constructor.
141
146
 
337
342
        """
338
343
        parent_map = self._parents_provider.get_parent_map(keys)
339
344
        parent_child = {}
340
 
        for child, parents in sorted(parent_map.items()):
 
345
        for child, parents in sorted(viewitems(parent_map)):
341
346
            for parent in parents:
342
347
                parent_child.setdefault(parent, []).append(child)
343
348
        return parent_child
371
376
            to_search.update(searching_known_tips)
372
377
            parent_map = self.get_parent_map(to_search)
373
378
            parents = parent_map.get(cur_tip, None)
374
 
            if not parents:  # An empty list or None is a ghost
 
379
            if not parents: # An empty list or None is a ghost
375
380
                raise errors.GhostRevisionsHaveNoRevno(target_revision_id,
376
381
                                                       cur_tip)
377
382
            cur_tip = parents[0]
403
408
        """
404
409
        # Optimisable by concurrent searching, but a random spread should get
405
410
        # some sort of hit rate.
 
411
        result = {}
406
412
        known_revnos = []
407
413
        ghosts = []
408
414
        for key in keys:
459
465
            return unique_nodes
460
466
 
461
467
        (all_unique_searcher,
462
 
         unique_tip_searchers) = self._make_unique_searchers(
463
 
             unique_nodes, unique_searcher, common_searcher)
 
468
         unique_tip_searchers) = self._make_unique_searchers(unique_nodes,
 
469
                                    unique_searcher, common_searcher)
464
470
 
465
471
        self._refine_unique_nodes(unique_searcher, all_unique_searcher,
466
472
                                  unique_tip_searchers, common_searcher)
498
504
                next_common_nodes.intersection(unique_searcher.seen))
499
505
            if unique_are_common_nodes:
500
506
                ancestors = unique_searcher.find_seen_ancestors(
501
 
                    unique_are_common_nodes)
 
507
                                unique_are_common_nodes)
502
508
                # TODO: This is a bit overboard, we only really care about
503
509
                #       the ancestors of the tips because the rest we
504
510
                #       already know. This is *correct* but causes us to
505
511
                #       search too much ancestry.
506
 
                ancestors.update(
507
 
                    common_searcher.find_seen_ancestors(ancestors))
 
512
                ancestors.update(common_searcher.find_seen_ancestors(ancestors))
508
513
                unique_searcher.stop_searching_any(ancestors)
509
514
                common_searcher.start_searching(ancestors)
510
515
 
519
524
 
520
525
        :return: (all_unique_searcher, unique_tip_searchers)
521
526
        """
522
 
        unique_tips = self._remove_simple_descendants(
523
 
            unique_nodes, self.get_parent_map(unique_nodes))
 
527
        unique_tips = self._remove_simple_descendants(unique_nodes,
 
528
                        self.get_parent_map(unique_nodes))
524
529
 
525
530
        if len(unique_tips) == 1:
526
531
            unique_tip_searchers = []
527
 
            ancestor_all_unique = unique_searcher.find_seen_ancestors(
528
 
                unique_tips)
 
532
            ancestor_all_unique = unique_searcher.find_seen_ancestors(unique_tips)
529
533
        else:
530
534
            unique_tip_searchers = []
531
535
            for tip in unique_tips:
544
548
                    ancestor_all_unique = set(searcher.seen)
545
549
                else:
546
550
                    ancestor_all_unique = ancestor_all_unique.intersection(
547
 
                        searcher.seen)
 
551
                                                searcher.seen)
548
552
        # Collapse all the common nodes into a single searcher
549
553
        all_unique_searcher = self._make_breadth_first_searcher(
550
 
            ancestor_all_unique)
 
554
                                ancestor_all_unique)
551
555
        if ancestor_all_unique:
552
556
            # We've seen these nodes in all the searchers, so we'll just go to
553
557
            # the next
601
605
        for searcher in unique_tip_searchers:
602
606
            common_to_all_unique_nodes.intersection_update(searcher.seen)
603
607
        common_to_all_unique_nodes.intersection_update(
604
 
            all_unique_searcher.seen)
 
608
                                    all_unique_searcher.seen)
605
609
        # Step all-unique less frequently than the other searchers.
606
610
        # In the common case, we don't need to spider out far here, so
607
611
        # avoid doing extra work.
608
612
        if step_all_unique:
609
 
            tstart = osutils.perf_counter()
 
613
            tstart = time.clock()
610
614
            nodes = all_unique_searcher.step()
611
615
            common_to_all_unique_nodes.update(nodes)
612
616
            if 'graph' in debug.debug_flags:
613
 
                tdelta = osutils.perf_counter() - tstart
 
617
                tdelta = time.clock() - tstart
614
618
                trace.mutter('all_unique_searcher step() took %.3fs'
615
619
                             'for %d nodes (%d total), iteration: %s',
616
620
                             tdelta, len(nodes), len(all_unique_searcher.seen),
648
652
        # TODO: it might be possible to collapse searchers faster when they
649
653
        #       only have *some* search tips in common.
650
654
        next_unique_searchers = []
651
 
        for searchers in unique_search_tips.values():
 
655
        for searchers in viewvalues(unique_search_tips):
652
656
            if len(searchers) == 1:
653
657
                # Searching unique tips, go for it
654
658
                next_unique_searchers.append(searchers[0])
688
692
            # These nodes are common ancestors of all unique nodes
689
693
            common_to_all_unique_nodes = self._find_nodes_common_to_all_unique(
690
694
                unique_tip_searchers, all_unique_searcher, newly_seen_unique,
691
 
                step_all_unique_counter == 0)
 
695
                step_all_unique_counter==0)
692
696
            step_all_unique_counter = ((step_all_unique_counter + 1)
693
697
                                       % STEP_UNIQUE_SEARCHER_EVERY)
694
698
 
756
760
        common_ancestors = set()
757
761
        searchers = [self._make_breadth_first_searcher([r])
758
762
                     for r in revisions]
 
763
        active_searchers = searchers[:]
759
764
        border_ancestors = set()
760
765
 
761
766
        while True:
834
839
        if len(candidate_heads) < 2:
835
840
            return candidate_heads
836
841
        searchers = dict((c, self._make_breadth_first_searcher([c]))
837
 
                         for c in candidate_heads)
 
842
                          for c in candidate_heads)
838
843
        active_searchers = dict(searchers)
839
844
        # skip over the actual candidate for each searcher
840
 
        for searcher in active_searchers.values():
 
845
        for searcher in viewvalues(active_searchers):
841
846
            next(searcher)
842
847
        # The common walker finds nodes that are common to two or more of the
843
848
        # input keys, so that we don't access all history when a currently
880
885
                    # some searcher has encountered our known common nodes:
881
886
                    # just stop it
882
887
                    ancestor_set = {ancestor}
883
 
                    for searcher in searchers.values():
 
888
                    for searcher in viewvalues(searchers):
884
889
                        searcher.stop_searching_any(ancestor_set)
885
890
                else:
886
891
                    # or it may have been just reached by all the searchers:
887
 
                    for searcher in searchers.values():
 
892
                    for searcher in viewvalues(searchers):
888
893
                        if ancestor not in searcher.seen:
889
894
                            break
890
895
                    else:
892
897
                        # making it be known as a descendant of all candidates,
893
898
                        # so we can stop searching it, and any seen ancestors
894
899
                        new_common.add(ancestor)
895
 
                        for searcher in searchers.values():
 
900
                        for searcher in viewvalues(searchers):
896
901
                            seen_ancestors =\
897
902
                                searcher.find_seen_ancestors([ancestor])
898
903
                            searcher.stop_searching_any(seen_ancestors)
930
935
                    break
931
936
                continue
932
937
            parent_ids = self.get_parent_map([next]).get(next, None)
933
 
            if not parent_ids:  # Ghost, nothing to search here
 
938
            if not parent_ids: # Ghost, nothing to search here
934
939
                continue
935
940
            for parent_id in reversed(parent_ids):
936
941
                # TODO: (performance) We see the parent at this point, but we
1015
1020
            processed.update(pending)
1016
1021
            next_map = self.get_parent_map(pending)
1017
1022
            next_pending = set()
1018
 
            for item in next_map.items():
 
1023
            for item in viewitems(next_map):
1019
1024
                yield item
1020
1025
                next_pending.update(p for p in item[1] if p not in processed)
1021
1026
            ghosts = pending.difference(next_map)
1027
1032
        if stop_keys is None:
1028
1033
            stop_keys = ()
1029
1034
        next_key = start_key
1030
 
 
1031
1035
        def get_parents(key):
1032
1036
            try:
1033
1037
                return self._parents_provider.get_parent_map([key])[key]
1071
1075
        lower_bound_revid <= revid <= upper_bound_revid
1072
1076
        """
1073
1077
        return ((upper_bound_revid is None or
1074
 
                 self.is_ancestor(revid, upper_bound_revid)) and
1075
 
                (lower_bound_revid is None or
1076
 
                 self.is_ancestor(lower_bound_revid, revid)))
 
1078
                    self.is_ancestor(revid, upper_bound_revid)) and
 
1079
               (lower_bound_revid is None or
 
1080
                    self.is_ancestor(lower_bound_revid, revid)))
1077
1081
 
1078
1082
    def _search_for_extra_common(self, common, searchers):
1079
1083
        """Make sure that unique nodes are genuinely unique.
1112
1116
        left_searcher = searchers[0]
1113
1117
        right_searcher = searchers[1]
1114
1118
        unique = left_searcher.seen.symmetric_difference(right_searcher.seen)
1115
 
        if not unique:  # No unique nodes, nothing to do
 
1119
        if not unique: # No unique nodes, nothing to do
1116
1120
            return
1117
1121
        total_unique = len(unique)
1118
1122
        unique = self._remove_simple_descendants(unique,
1119
 
                                                 self.get_parent_map(unique))
 
1123
                    self.get_parent_map(unique))
1120
1124
        simple_unique = len(unique)
1121
1125
 
1122
1126
        unique_searchers = []
1126
1130
            else:
1127
1131
                parent_searcher = right_searcher
1128
1132
            revs_to_search = parent_searcher.find_seen_ancestors([revision_id])
1129
 
            if not revs_to_search:  # XXX: This shouldn't be possible
 
1133
            if not revs_to_search: # XXX: This shouldn't be possible
1130
1134
                revs_to_search = [revision_id]
1131
1135
            searcher = self._make_breadth_first_searcher(revs_to_search)
1132
1136
            # We don't care about the starting nodes.
1143
1147
                ancestor_all_unique = set(searcher.seen)
1144
1148
            else:
1145
1149
                ancestor_all_unique = ancestor_all_unique.intersection(
1146
 
                    searcher.seen)
 
1150
                                            searcher.seen)
1147
1151
 
1148
1152
        trace.mutter('Started %d unique searchers for %d unique revisions',
1149
1153
                     simple_unique, total_unique)
1150
1154
 
1151
 
        while True:  # If we have no more nodes we have nothing to do
 
1155
        while True: # If we have no more nodes we have nothing to do
1152
1156
            newly_seen_common = set()
1153
1157
            for searcher in common_searchers:
1154
1158
                newly_seen_common.update(searcher.step())
1181
1185
                # If a 'common' node is an ancestor of all unique searchers, we
1182
1186
                # can stop searching it.
1183
1187
                stop_searching_common = ancestor_all_unique.intersection(
1184
 
                    newly_seen_common)
 
1188
                                            newly_seen_common)
1185
1189
                if stop_searching_common:
1186
1190
                    for searcher in common_searchers:
1187
1191
                        searcher.stop_searching_any(stop_searching_common)
1212
1216
                for searcher in unique_searchers:
1213
1217
                    will_search_set = frozenset(searcher._next_query)
1214
1218
                    if will_search_set not in unique_search_sets:
1215
 
                        # This searcher is searching a unique set of nodes, let
1216
 
                        # it
 
1219
                        # This searcher is searching a unique set of nodes, let it
1217
1220
                        unique_search_sets.add(will_search_set)
1218
1221
                        next_unique_searchers.append(searcher)
1219
1222
                unique_searchers = next_unique_searchers
1241
1244
 
1242
1245
        # This is the same as the following loop. I don't know that it is any
1243
1246
        # faster.
1244
 
        # simple_ancestors.difference_update(r for r, p_ids in parent_map.iteritems()
1245
 
        # if p_ids is not None and revisions.intersection(p_ids))
1246
 
        # return simple_ancestors
 
1247
        ## simple_ancestors.difference_update(r for r, p_ids in parent_map.iteritems()
 
1248
        ##     if p_ids is not None and revisions.intersection(p_ids))
 
1249
        ## return simple_ancestors
1247
1250
 
1248
1251
        # Yet Another Way, invert the parent map (which can be cached)
1249
1252
        ## descendants = {}
1250
 
        # for revision_id, parent_ids in parent_map.iteritems():
1251
 
        # for p_id in parent_ids:
 
1253
        ## for revision_id, parent_ids in parent_map.iteritems():
 
1254
        ##   for p_id in parent_ids:
1252
1255
        ##       descendants.setdefault(p_id, []).append(revision_id)
1253
 
        # for revision in revisions.intersection(descendants):
1254
 
        # simple_ancestors.difference_update(descendants[revision])
1255
 
        # return simple_ancestors
1256
 
        for revision, parent_ids in parent_map.items():
 
1256
        ## for revision in revisions.intersection(descendants):
 
1257
        ##   simple_ancestors.difference_update(descendants[revision])
 
1258
        ## return simple_ancestors
 
1259
        for revision, parent_ids in viewitems(parent_map):
1257
1260
            if parent_ids is None:
1258
1261
                continue
1259
1262
            for parent_id in parent_ids:
1460
1463
        seen.update(revisions)
1461
1464
        parent_map = self._parents_provider.get_parent_map(revisions)
1462
1465
        found_revisions.update(parent_map)
1463
 
        for rev_id, parents in parent_map.items():
 
1466
        for rev_id, parents in viewitems(parent_map):
1464
1467
            if parents is None:
1465
1468
                continue
1466
1469
            new_found_parents = [p for p in parents if p not in seen]
1503
1506
            all_parents = []
1504
1507
            # We don't care if it is a ghost, since it can't be seen if it is
1505
1508
            # a ghost
1506
 
            for parent_ids in parent_map.values():
 
1509
            for parent_ids in viewvalues(parent_map):
1507
1510
                all_parents.extend(parent_ids)
1508
 
            next_pending = all_seen.intersection(
1509
 
                all_parents).difference(seen_ancestors)
 
1511
            next_pending = all_seen.intersection(all_parents).difference(seen_ancestors)
1510
1512
            seen_ancestors.update(next_pending)
1511
1513
            next_pending.difference_update(not_searched_yet)
1512
1514
            pending = next_pending
1549
1551
                    stop_rev_references[parent_id] += 1
1550
1552
            # if only the stopped revisions reference it, the ref count will be
1551
1553
            # 0 after this loop
1552
 
            for parents in self._current_parents.values():
 
1554
            for parents in viewvalues(self._current_parents):
1553
1555
                for parent_id in parents:
1554
1556
                    try:
1555
1557
                        stop_rev_references[parent_id] -= 1
1556
1558
                    except KeyError:
1557
1559
                        pass
1558
1560
            stop_parents = set()
1559
 
            for rev_id, refs in stop_rev_references.items():
 
1561
            for rev_id, refs in viewitems(stop_rev_references):
1560
1562
                if refs == 0:
1561
1563
                    stop_parents.add(rev_id)
1562
1564
            self._next_query.difference_update(stop_parents)
1592
1594
def invert_parent_map(parent_map):
1593
1595
    """Given a map from child => parents, create a map of parent=>children"""
1594
1596
    child_map = {}
1595
 
    for child, parents in parent_map.items():
 
1597
    for child, parents in viewitems(parent_map):
1596
1598
        for p in parents:
1597
1599
            # Any given parent is likely to have only a small handful
1598
1600
            # of children, many will have only one. So we avoid mem overhead of
1644
1646
    # Will not have any nodes removed, even though you do have an
1645
1647
    # 'uninteresting' linear D->B and E->C
1646
1648
    children = {}
1647
 
    for child, parents in parent_map.items():
 
1649
    for child, parents in viewitems(parent_map):
1648
1650
        children.setdefault(child, [])
1649
1651
        for p in parents:
1650
1652
            children.setdefault(p, []).append(child)
1651
1653
 
 
1654
    orig_children = dict(children)
1652
1655
    removed = set()
1653
1656
    result = dict(parent_map)
1654
1657
    for node in parent_map:
1706
1709
    from ._known_graph_pyx import KnownGraph
1707
1710
except ImportError as e:
1708
1711
    osutils.failed_to_load_extension(e)
1709
 
    from ._known_graph_py import KnownGraph  # noqa: F401
 
1712
    from ._known_graph_py import KnownGraph