~bzr-pqm/bzr/bzr.dev

2490.2.5 by Aaron Bentley
Use GraphWalker.unique_ancestor to determine merge base
1
# Copyright (C) 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
2490.2.30 by Aaron Bentley
Add functionality for tsorting graphs
17
from bzrlib import (
18
    errors,
19
    tsort,
20
    )
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
21
from bzrlib.deprecated_graph import (node_distances, select_farthest)
2490.2.1 by Aaron Bentley
Start work on GraphWalker
22
from bzrlib.revision import NULL_REVISION
23
2490.2.25 by Aaron Bentley
Update from review
24
# DIAGRAM of terminology
25
#       A
26
#       /\
27
#      B  C
28
#      |  |\
29
#      D  E F
30
#      |\/| |
31
#      |/\|/
32
#      G  H
33
#
34
# In this diagram, relative to G and H:
35
# A, B, C, D, E are common ancestors.
36
# C, D and E are border ancestors, because each has a non-common descendant.
37
# D and E are least common ancestors because none of their descendants are
38
# common ancestors.
39
# C is not a least common ancestor because its descendant, E, is a common
40
# ancestor.
41
#
42
# The find_unique_lca algorithm will pick A in two steps:
43
# 1. find_lca('G', 'H') => ['D', 'E']
44
# 2. Since len(['D', 'E']) > 1, find_lca('D', 'E') => ['A']
45
46
2490.2.5 by Aaron Bentley
Use GraphWalker.unique_ancestor to determine merge base
47
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
48
class _StackedParentsProvider(object):
49
50
    def __init__(self, parent_providers):
51
        self._parent_providers = parent_providers
52
2490.2.28 by Aaron Bentley
Fix handling of null revision
53
    def __repr__(self):
54
        return "_StackedParentsProvider(%r)" % self._parent_providers
55
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
56
    def get_parents(self, revision_ids):
2490.2.25 by Aaron Bentley
Update from review
57
        """Find revision ids of the parents of a list of revisions
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
58
59
        A list is returned of the same length as the input.  Each entry
60
        is a list of parent ids for the corresponding input revision.
61
62
        [NULL_REVISION] is used as the parent of the first user-committed
63
        revision.  Its parent list is empty.
64
65
        If the revision is not present (i.e. a ghost), None is used in place
66
        of the list of parents.
67
        """
68
        found = {}
69
        for parents_provider in self._parent_providers:
2490.2.25 by Aaron Bentley
Update from review
70
            pending_revisions = [r for r in revision_ids if r not in found]
71
            parent_list = parents_provider.get_parents(pending_revisions)
72
            new_found = dict((k, v) for k, v in zip(pending_revisions,
73
                             parent_list) if v is not None)
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
74
            found.update(new_found)
75
            if len(found) == len(revision_ids):
76
                break
2490.2.25 by Aaron Bentley
Update from review
77
        return [found.get(r, None) for r in revision_ids]
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
78
79
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
80
class Graph(object):
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
81
    """Provide incremental access to revision graphs.
82
83
    This is the generic implementation; it is intended to be subclassed to
84
    specialize it for other repository types.
85
    """
2490.2.1 by Aaron Bentley
Start work on GraphWalker
86
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
87
    def __init__(self, parents_provider):
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
88
        """Construct a Graph that uses several graphs as its input
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
89
90
        This should not normally be invoked directly, because there may be
91
        specialized implementations for particular repository types.  See
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
92
        Repository.get_graph()
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
93
2921.3.4 by Robert Collins
Review feedback.
94
        :param parents_provider: An object providing a get_parents call
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
95
            conforming to the behavior of StackedParentsProvider.get_parents
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
96
        """
97
        self.get_parents = parents_provider.get_parents
2490.2.29 by Aaron Bentley
Make parents provider private
98
        self._parents_provider = parents_provider
2490.2.28 by Aaron Bentley
Fix handling of null revision
99
100
    def __repr__(self):
2490.2.29 by Aaron Bentley
Make parents provider private
101
        return 'Graph(%r)' % self._parents_provider
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
102
103
    def find_lca(self, *revisions):
104
        """Determine the lowest common ancestors of the provided revisions
105
106
        A lowest common ancestor is a common ancestor none of whose
107
        descendants are common ancestors.  In graphs, unlike trees, there may
108
        be multiple lowest common ancestors.
2490.2.12 by Aaron Bentley
Improve documentation
109
110
        This algorithm has two phases.  Phase 1 identifies border ancestors,
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
111
        and phase 2 filters border ancestors to determine lowest common
112
        ancestors.
2490.2.12 by Aaron Bentley
Improve documentation
113
114
        In phase 1, border ancestors are identified, using a breadth-first
115
        search starting at the bottom of the graph.  Searches are stopped
116
        whenever a node or one of its descendants is determined to be common
117
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
118
        In phase 2, the border ancestors are filtered to find the least
2490.2.12 by Aaron Bentley
Improve documentation
119
        common ancestors.  This is done by searching the ancestries of each
120
        border ancestor.
121
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
122
        Phase 2 is perfomed on the principle that a border ancestor that is
123
        not an ancestor of any other border ancestor is a least common
124
        ancestor.
2490.2.12 by Aaron Bentley
Improve documentation
125
126
        Searches are stopped when they find a node that is determined to be a
127
        common ancestor of all border ancestors, because this shows that it
128
        cannot be a descendant of any border ancestor.
129
130
        The scaling of this operation should be proportional to
131
        1. The number of uncommon ancestors
132
        2. The number of border ancestors
133
        3. The length of the shortest path between a border ancestor and an
134
           ancestor of all border ancestors.
2490.2.3 by Aaron Bentley
Implement new merge base picker
135
        """
2490.2.23 by Aaron Bentley
Adapt find_borders to produce a graph difference
136
        border_common, common, sides = self._find_border_ancestors(revisions)
2776.3.1 by Robert Collins
* Deprecated method ``find_previous_heads`` on
137
        # We may have common ancestors that can be reached from each other.
138
        # - ask for the heads of them to filter it down to only ones that
139
        # cannot be reached from each other - phase 2.
140
        return self.heads(border_common)
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
141
2490.2.23 by Aaron Bentley
Adapt find_borders to produce a graph difference
142
    def find_difference(self, left_revision, right_revision):
2490.2.25 by Aaron Bentley
Update from review
143
        """Determine the graph difference between two revisions"""
2490.2.23 by Aaron Bentley
Adapt find_borders to produce a graph difference
144
        border, common, (left, right) = self._find_border_ancestors(
145
            [left_revision, right_revision])
146
        return (left.difference(right).difference(common),
147
                right.difference(left).difference(common))
148
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
149
    def _make_breadth_first_searcher(self, revisions):
150
        return _BreadthFirstSearcher(revisions, self)
151
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
152
    def _find_border_ancestors(self, revisions):
2490.2.12 by Aaron Bentley
Improve documentation
153
        """Find common ancestors with at least one uncommon descendant.
154
155
        Border ancestors are identified using a breadth-first
156
        search starting at the bottom of the graph.  Searches are stopped
157
        whenever a node or one of its descendants is determined to be common.
158
159
        This will scale with the number of uncommon ancestors.
2490.2.25 by Aaron Bentley
Update from review
160
161
        As well as the border ancestors, a set of seen common ancestors and a
162
        list of sets of seen ancestors for each input revision is returned.
163
        This allows calculation of graph difference from the results of this
164
        operation.
2490.2.12 by Aaron Bentley
Improve documentation
165
        """
2490.2.28 by Aaron Bentley
Fix handling of null revision
166
        if None in revisions:
167
            raise errors.InvalidRevisionId(None, self)
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
168
        common_searcher = self._make_breadth_first_searcher([])
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
169
        common_ancestors = set()
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
170
        searchers = [self._make_breadth_first_searcher([r])
171
                     for r in revisions]
172
        active_searchers = searchers[:]
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
173
        border_ancestors = set()
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
174
        def update_common(searcher, revisions):
175
            w_seen_ancestors = searcher.find_seen_ancestors(
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
176
                revision)
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
177
            stopped = searcher.stop_searching_any(w_seen_ancestors)
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
178
            common_ancestors.update(w_seen_ancestors)
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
179
            common_searcher.start_searching(stopped)
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
180
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
181
        while True:
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
182
            if len(active_searchers) == 0:
2490.2.23 by Aaron Bentley
Adapt find_borders to produce a graph difference
183
                return border_ancestors, common_ancestors, [s.seen for s in
184
                                                            searchers]
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
185
            try:
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
186
                new_common = common_searcher.next()
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
187
                common_ancestors.update(new_common)
188
            except StopIteration:
189
                pass
190
            else:
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
191
                for searcher in active_searchers:
192
                    for revision in new_common.intersection(searcher.seen):
193
                        update_common(searcher, revision)
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
194
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
195
            newly_seen = set()
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
196
            new_active_searchers = []
197
            for searcher in active_searchers:
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
198
                try:
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
199
                    newly_seen.update(searcher.next())
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
200
                except StopIteration:
201
                    pass
202
                else:
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
203
                    new_active_searchers.append(searcher)
204
            active_searchers = new_active_searchers
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
205
            for revision in newly_seen:
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
206
                if revision in common_ancestors:
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
207
                    for searcher in searchers:
208
                        update_common(searcher, revision)
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
209
                    continue
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
210
                for searcher in searchers:
211
                    if revision not in searcher.seen:
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
212
                        break
213
                else:
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
214
                    border_ancestors.add(revision)
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
215
                    for searcher in searchers:
216
                        update_common(searcher, revision)
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
217
2776.3.1 by Robert Collins
* Deprecated method ``find_previous_heads`` on
218
    def heads(self, keys):
219
        """Return the heads from amongst keys.
220
221
        This is done by searching the ancestries of each key.  Any key that is
222
        reachable from another key is not returned; all the others are.
223
224
        This operation scales with the relative depth between any two keys. If
225
        any two keys are completely disconnected all ancestry of both sides
226
        will be retrieved.
227
228
        :param keys: An iterable of keys.
2776.1.4 by Robert Collins
Trivial review feedback changes.
229
        :return: A set of the heads. Note that as a set there is no ordering
230
            information. Callers will need to filter their input to create
231
            order if they need it.
2490.2.12 by Aaron Bentley
Improve documentation
232
        """
2776.1.4 by Robert Collins
Trivial review feedback changes.
233
        candidate_heads = set(keys)
2850.2.1 by Robert Collins
(robertc) Special case the zero-or-no-heads case for Graph.heads(). (Robert Collins)
234
        if len(candidate_heads) < 2:
235
            return candidate_heads
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
236
        searchers = dict((c, self._make_breadth_first_searcher([c]))
2776.1.4 by Robert Collins
Trivial review feedback changes.
237
                          for c in candidate_heads)
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
238
        active_searchers = dict(searchers)
239
        # skip over the actual candidate for each searcher
240
        for searcher in active_searchers.itervalues():
1551.15.81 by Aaron Bentley
Remove testing code
241
            searcher.next()
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
242
        # The common walker finds nodes that are common to two or more of the
243
        # input keys, so that we don't access all history when a currently
244
        # uncommon search point actually meets up with something behind a
245
        # common search point. Common search points do not keep searches
246
        # active; they just allow us to make searches inactive without
247
        # accessing all history.
248
        common_walker = self._make_breadth_first_searcher([])
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
249
        while len(active_searchers) > 0:
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
250
            ancestors = set()
251
            # advance searches
252
            try:
253
                common_walker.next()
254
            except StopIteration:
2921.3.4 by Robert Collins
Review feedback.
255
                # No common points being searched at this time.
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
256
                pass
1551.15.78 by Aaron Bentley
Fix KeyError in filter_candidate_lca
257
            for candidate in active_searchers.keys():
258
                try:
259
                    searcher = active_searchers[candidate]
260
                except KeyError:
261
                    # rare case: we deleted candidate in a previous iteration
262
                    # through this for loop, because it was determined to be
263
                    # a descendant of another candidate.
264
                    continue
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
265
                try:
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
266
                    ancestors.update(searcher.next())
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
267
                except StopIteration:
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
268
                    del active_searchers[candidate]
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
269
                    continue
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
270
            # process found nodes
271
            new_common = set()
272
            for ancestor in ancestors:
273
                if ancestor in candidate_heads:
274
                    candidate_heads.remove(ancestor)
275
                    del searchers[ancestor]
276
                    if ancestor in active_searchers:
277
                        del active_searchers[ancestor]
278
                # it may meet up with a known common node
2921.3.4 by Robert Collins
Review feedback.
279
                if ancestor in common_walker.seen:
280
                    # some searcher has encountered our known common nodes:
281
                    # just stop it
282
                    ancestor_set = set([ancestor])
283
                    for searcher in searchers.itervalues():
284
                        searcher.stop_searching_any(ancestor_set)
285
                else:
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
286
                    # or it may have been just reached by all the searchers:
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
287
                    for searcher in searchers.itervalues():
288
                        if ancestor not in searcher.seen:
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
289
                            break
290
                    else:
2921.3.4 by Robert Collins
Review feedback.
291
                        # The final active searcher has just reached this node,
292
                        # making it be known as a descendant of all candidates,
293
                        # so we can stop searching it, and any seen ancestors
294
                        new_common.add(ancestor)
295
                        for searcher in searchers.itervalues():
296
                            seen_ancestors =\
297
                                searcher.find_seen_ancestors(ancestor)
298
                            searcher.stop_searching_any(seen_ancestors)
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
299
            common_walker.start_searching(new_common)
2776.1.4 by Robert Collins
Trivial review feedback changes.
300
        return candidate_heads
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
301
302
    def find_unique_lca(self, left_revision, right_revision):
303
        """Find a unique LCA.
304
305
        Find lowest common ancestors.  If there is no unique  common
306
        ancestor, find the lowest common ancestors of those ancestors.
307
308
        Iteration stops when a unique lowest common ancestor is found.
309
        The graph origin is necessarily a unique lowest common ancestor.
2490.2.5 by Aaron Bentley
Use GraphWalker.unique_ancestor to determine merge base
310
311
        Note that None is not an acceptable substitute for NULL_REVISION.
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
312
        in the input for this method.
2490.2.3 by Aaron Bentley
Implement new merge base picker
313
        """
314
        revisions = [left_revision, right_revision]
315
        while True:
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
316
            lca = self.find_lca(*revisions)
317
            if len(lca) == 1:
318
                return lca.pop()
2520.4.104 by Aaron Bentley
Avoid infinite loop when there is no unique lca
319
            if len(lca) == 0:
320
                raise errors.NoCommonAncestor(left_revision, right_revision)
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
321
            revisions = lca
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
322
2490.2.31 by Aaron Bentley
Fix iter_topo_order to permit un-included parents
323
    def iter_topo_order(self, revisions):
2490.2.30 by Aaron Bentley
Add functionality for tsorting graphs
324
        """Iterate through the input revisions in topological order.
325
326
        This sorting only ensures that parents come before their children.
327
        An ancestor may sort after a descendant if the relationship is not
328
        visible in the supplied list of revisions.
329
        """
2490.2.34 by Aaron Bentley
Update NEWS and change implementation to return an iterator
330
        sorter = tsort.TopoSorter(zip(revisions, self.get_parents(revisions)))
331
        return sorter.iter_topo_order()
2490.2.30 by Aaron Bentley
Add functionality for tsorting graphs
332
2653.2.1 by Aaron Bentley
Implement Graph.is_ancestor
333
    def is_ancestor(self, candidate_ancestor, candidate_descendant):
2653.2.5 by Aaron Bentley
Update to clarify algorithm
334
        """Determine whether a revision is an ancestor of another.
335
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
336
        We answer this using heads() as heads() has the logic to perform the
337
        smallest number of parent looksup to determine the ancestral
338
        relationship between N revisions.
2653.2.5 by Aaron Bentley
Update to clarify algorithm
339
        """
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
340
        return set([candidate_descendant]) == self.heads(
341
            [candidate_ancestor, candidate_descendant])
2653.2.1 by Aaron Bentley
Implement Graph.is_ancestor
342
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
343
2911.4.1 by Robert Collins
Factor out the Graph.heads() cache from _RevisionTextVersionCache for reuse, and use it in commit.
344
class HeadsCache(object):
345
    """A cache of results for graph heads calls."""
346
347
    def __init__(self, graph):
348
        self.graph = graph
349
        self._heads = {}
350
351
    def heads(self, keys):
352
        """Return the heads of keys.
353
2911.4.3 by Robert Collins
Make the contract of HeadsCache.heads() more clear.
354
        This matches the API of Graph.heads(), specifically the return value is
355
        a set which can be mutated, and ordering of the input is not preserved
356
        in the output.
357
2911.4.1 by Robert Collins
Factor out the Graph.heads() cache from _RevisionTextVersionCache for reuse, and use it in commit.
358
        :see also: Graph.heads.
359
        :param keys: The keys to calculate heads for.
360
        :return: A set containing the heads, which may be mutated without
361
            affecting future lookups.
362
        """
2911.4.2 by Robert Collins
Make HeadsCache actually work.
363
        keys = frozenset(keys)
2911.4.1 by Robert Collins
Factor out the Graph.heads() cache from _RevisionTextVersionCache for reuse, and use it in commit.
364
        try:
365
            return set(self._heads[keys])
366
        except KeyError:
367
            heads = self.graph.heads(keys)
368
            self._heads[keys] = heads
369
            return set(heads)
370
371
2592.3.223 by Robert Collins
Merge graph history-access bugfix.
372
class HeadsCache(object):
373
    """A cache of results for graph heads calls."""
374
375
    def __init__(self, graph):
376
        self.graph = graph
377
        self._heads = {}
378
379
    def heads(self, keys):
380
        """Return the heads of keys.
381
382
        :see also: Graph.heads.
383
        :param keys: The keys to calculate heads for.
384
        :return: A set containing the heads, which may be mutated without
385
            affecting future lookups.
386
        """
387
        keys = frozenset(keys)
388
        try:
389
            return set(self._heads[keys])
390
        except KeyError:
391
            heads = self.graph.heads(keys)
392
            self._heads[keys] = heads
393
            return set(heads)
394
395
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
396
class _BreadthFirstSearcher(object):
2921.3.4 by Robert Collins
Review feedback.
397
    """Parallel search breadth-first the ancestry of revisions.
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
398
399
    This class implements the iterator protocol, but additionally
400
    1. provides a set of seen ancestors, and
401
    2. allows some ancestries to be unsearched, via stop_searching_any
402
    """
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
403
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
404
    def __init__(self, revisions, parents_provider):
2490.2.18 by Aaron Bentley
Change AncestryWalker to take a set of ancestors
405
        self._start = set(revisions)
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
406
        self._search_revisions = None
2490.2.18 by Aaron Bentley
Change AncestryWalker to take a set of ancestors
407
        self.seen = set(revisions)
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
408
        self._parents_provider = parents_provider 
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
409
410
    def __repr__(self):
2490.2.25 by Aaron Bentley
Update from review
411
        return ('_BreadthFirstSearcher(self._search_revisions=%r,'
412
                ' self.seen=%r)' % (self._search_revisions, self.seen))
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
413
414
    def next(self):
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
415
        """Return the next ancestors of this revision.
416
2490.2.12 by Aaron Bentley
Improve documentation
417
        Ancestors are returned in the order they are seen in a breadth-first
418
        traversal.  No ancestor will be returned more than once.
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
419
        """
420
        if self._search_revisions is None:
421
            self._search_revisions = self._start
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
422
        else:
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
423
            new_search_revisions = set()
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
424
            for parents in self._parents_provider.get_parents(
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
425
                self._search_revisions):
426
                if parents is None:
427
                    continue
428
                new_search_revisions.update(p for p in parents if
429
                                            p not in self.seen)
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
430
            self._search_revisions = new_search_revisions
431
        if len(self._search_revisions) == 0:
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
432
            raise StopIteration()
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
433
        self.seen.update(self._search_revisions)
434
        return self._search_revisions
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
435
2490.2.8 by Aaron Bentley
fix iteration stuff
436
    def __iter__(self):
437
        return self
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
438
439
    def find_seen_ancestors(self, revision):
2490.2.25 by Aaron Bentley
Update from review
440
        """Find ancestors of this revision that have already been seen."""
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
441
        searcher = _BreadthFirstSearcher([revision], self._parents_provider)
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
442
        seen_ancestors = set()
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
443
        for ancestors in searcher:
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
444
            for ancestor in ancestors:
445
                if ancestor not in self.seen:
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
446
                    searcher.stop_searching_any([ancestor])
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
447
                else:
448
                    seen_ancestors.add(ancestor)
449
        return seen_ancestors
450
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
451
    def stop_searching_any(self, revisions):
452
        """
453
        Remove any of the specified revisions from the search list.
454
455
        None of the specified revisions are required to be present in the
2490.2.12 by Aaron Bentley
Improve documentation
456
        search list.  In this case, the call is a no-op.
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
457
        """
2490.2.25 by Aaron Bentley
Update from review
458
        stopped = self._search_revisions.intersection(revisions)
459
        self._search_revisions = self._search_revisions.difference(revisions)
460
        return stopped
2490.2.17 by Aaron Bentley
Add start_searching, tweak stop_searching_any
461
462
    def start_searching(self, revisions):
463
        if self._search_revisions is None:
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
464
            self._start = set(revisions)
2490.2.17 by Aaron Bentley
Add start_searching, tweak stop_searching_any
465
        else:
2490.2.25 by Aaron Bentley
Update from review
466
            self._search_revisions.update(revisions.difference(self.seen))
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
467
        self.seen.update(revisions)