/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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,
3052.1.3 by John Arbash Meinel
deprecate revision.is_ancestor, update the callers and the tests.
19
    revision,
3099.3.3 by John Arbash Meinel
Deprecate get_parents() in favor of get_parent_map()
20
    symbol_versioning,
3377.3.10 by John Arbash Meinel
Tweak _BreadthFirstSearcher.find_seen_ancestors()
21
    trace,
2490.2.30 by Aaron Bentley
Add functionality for tsorting graphs
22
    tsort,
23
    )
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
24
from bzrlib.deprecated_graph import (node_distances, select_farthest)
2490.2.1 by Aaron Bentley
Start work on GraphWalker
25
2490.2.25 by Aaron Bentley
Update from review
26
# DIAGRAM of terminology
27
#       A
28
#       /\
29
#      B  C
30
#      |  |\
31
#      D  E F
32
#      |\/| |
33
#      |/\|/
34
#      G  H
35
#
36
# In this diagram, relative to G and H:
37
# A, B, C, D, E are common ancestors.
38
# C, D and E are border ancestors, because each has a non-common descendant.
39
# D and E are least common ancestors because none of their descendants are
40
# common ancestors.
41
# C is not a least common ancestor because its descendant, E, is a common
42
# ancestor.
43
#
44
# The find_unique_lca algorithm will pick A in two steps:
45
# 1. find_lca('G', 'H') => ['D', 'E']
46
# 2. Since len(['D', 'E']) > 1, find_lca('D', 'E') => ['A']
47
48
2988.1.3 by Robert Collins
Add a new repositoy method _generate_text_key_index for use by reconcile/check.
49
class DictParentsProvider(object):
3172.1.2 by Robert Collins
Parent Providers should now implement ``get_parent_map`` returning a
50
    """A parents provider for Graph objects."""
2988.1.3 by Robert Collins
Add a new repositoy method _generate_text_key_index for use by reconcile/check.
51
52
    def __init__(self, ancestry):
53
        self.ancestry = ancestry
54
55
    def __repr__(self):
56
        return 'DictParentsProvider(%r)' % self.ancestry
57
3099.3.1 by John Arbash Meinel
Implement get_parent_map for ParentProviders
58
    def get_parent_map(self, keys):
59
        """See _StackedParentsProvider.get_parent_map"""
60
        ancestry = self.ancestry
61
        return dict((k, ancestry[k]) for k in keys if k in ancestry)
62
2490.2.5 by Aaron Bentley
Use GraphWalker.unique_ancestor to determine merge base
63
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
64
class _StackedParentsProvider(object):
65
66
    def __init__(self, parent_providers):
67
        self._parent_providers = parent_providers
68
2490.2.28 by Aaron Bentley
Fix handling of null revision
69
    def __repr__(self):
70
        return "_StackedParentsProvider(%r)" % self._parent_providers
71
3099.3.1 by John Arbash Meinel
Implement get_parent_map for ParentProviders
72
    def get_parent_map(self, keys):
73
        """Get a mapping of keys => parents
74
75
        A dictionary is returned with an entry for each key present in this
76
        source. If this source doesn't have information about a key, it should
77
        not include an entry.
78
79
        [NULL_REVISION] is used as the parent of the first user-committed
80
        revision.  Its parent list is empty.
81
82
        :param keys: An iterable returning keys to check (eg revision_ids)
83
        :return: A dictionary mapping each key to its parents
84
        """
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
85
        found = {}
3099.3.1 by John Arbash Meinel
Implement get_parent_map for ParentProviders
86
        remaining = set(keys)
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
87
        for parents_provider in self._parent_providers:
3099.3.1 by John Arbash Meinel
Implement get_parent_map for ParentProviders
88
            new_found = parents_provider.get_parent_map(remaining)
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
89
            found.update(new_found)
3099.3.1 by John Arbash Meinel
Implement get_parent_map for ParentProviders
90
            remaining.difference_update(new_found)
91
            if not remaining:
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
92
                break
3099.3.1 by John Arbash Meinel
Implement get_parent_map for ParentProviders
93
        return found
94
95
96
class CachingParentsProvider(object):
97
    """A parents provider which will cache the revision => parents in a dict.
98
99
    This is useful for providers that have an expensive lookup.
100
    """
101
102
    def __init__(self, parent_provider):
103
        self._real_provider = parent_provider
104
        # Theoretically we could use an LRUCache here
105
        self._cache = {}
106
107
    def __repr__(self):
108
        return "%s(%r)" % (self.__class__.__name__, self._real_provider)
109
110
    def get_parent_map(self, keys):
111
        """See _StackedParentsProvider.get_parent_map"""
112
        needed = set()
113
        # If the _real_provider doesn't have a key, we cache a value of None,
114
        # which we then later use to realize we cannot provide a value for that
115
        # key.
116
        parent_map = {}
117
        cache = self._cache
118
        for key in keys:
119
            if key in cache:
120
                value = cache[key]
121
                if value is not None:
122
                    parent_map[key] = value
123
            else:
124
                needed.add(key)
125
126
        if needed:
127
            new_parents = self._real_provider.get_parent_map(needed)
128
            cache.update(new_parents)
129
            parent_map.update(new_parents)
130
            needed.difference_update(new_parents)
131
            cache.update(dict.fromkeys(needed, None))
132
        return parent_map
133
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
134
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
135
class Graph(object):
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
136
    """Provide incremental access to revision graphs.
137
138
    This is the generic implementation; it is intended to be subclassed to
139
    specialize it for other repository types.
140
    """
2490.2.1 by Aaron Bentley
Start work on GraphWalker
141
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
142
    def __init__(self, parents_provider):
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
143
        """Construct a Graph that uses several graphs as its input
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
144
145
        This should not normally be invoked directly, because there may be
146
        specialized implementations for particular repository types.  See
3172.1.2 by Robert Collins
Parent Providers should now implement ``get_parent_map`` returning a
147
        Repository.get_graph().
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
148
3172.1.2 by Robert Collins
Parent Providers should now implement ``get_parent_map`` returning a
149
        :param parents_provider: An object providing a get_parent_map call
150
            conforming to the behavior of
151
            StackedParentsProvider.get_parent_map.
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
152
        """
3172.1.2 by Robert Collins
Parent Providers should now implement ``get_parent_map`` returning a
153
        if getattr(parents_provider, 'get_parents', None) is not None:
154
            self.get_parents = parents_provider.get_parents
155
        if getattr(parents_provider, 'get_parent_map', None) is not None:
156
            self.get_parent_map = parents_provider.get_parent_map
2490.2.29 by Aaron Bentley
Make parents provider private
157
        self._parents_provider = parents_provider
2490.2.28 by Aaron Bentley
Fix handling of null revision
158
159
    def __repr__(self):
2490.2.29 by Aaron Bentley
Make parents provider private
160
        return 'Graph(%r)' % self._parents_provider
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
161
162
    def find_lca(self, *revisions):
163
        """Determine the lowest common ancestors of the provided revisions
164
165
        A lowest common ancestor is a common ancestor none of whose
166
        descendants are common ancestors.  In graphs, unlike trees, there may
167
        be multiple lowest common ancestors.
2490.2.12 by Aaron Bentley
Improve documentation
168
169
        This algorithm has two phases.  Phase 1 identifies border ancestors,
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
170
        and phase 2 filters border ancestors to determine lowest common
171
        ancestors.
2490.2.12 by Aaron Bentley
Improve documentation
172
173
        In phase 1, border ancestors are identified, using a breadth-first
174
        search starting at the bottom of the graph.  Searches are stopped
175
        whenever a node or one of its descendants is determined to be common
176
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
177
        In phase 2, the border ancestors are filtered to find the least
2490.2.12 by Aaron Bentley
Improve documentation
178
        common ancestors.  This is done by searching the ancestries of each
179
        border ancestor.
180
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
181
        Phase 2 is perfomed on the principle that a border ancestor that is
182
        not an ancestor of any other border ancestor is a least common
183
        ancestor.
2490.2.12 by Aaron Bentley
Improve documentation
184
185
        Searches are stopped when they find a node that is determined to be a
186
        common ancestor of all border ancestors, because this shows that it
187
        cannot be a descendant of any border ancestor.
188
189
        The scaling of this operation should be proportional to
190
        1. The number of uncommon ancestors
191
        2. The number of border ancestors
192
        3. The length of the shortest path between a border ancestor and an
193
           ancestor of all border ancestors.
2490.2.3 by Aaron Bentley
Implement new merge base picker
194
        """
2490.2.23 by Aaron Bentley
Adapt find_borders to produce a graph difference
195
        border_common, common, sides = self._find_border_ancestors(revisions)
2776.3.1 by Robert Collins
* Deprecated method ``find_previous_heads`` on
196
        # We may have common ancestors that can be reached from each other.
197
        # - ask for the heads of them to filter it down to only ones that
198
        # cannot be reached from each other - phase 2.
199
        return self.heads(border_common)
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
200
2490.2.23 by Aaron Bentley
Adapt find_borders to produce a graph difference
201
    def find_difference(self, left_revision, right_revision):
2490.2.25 by Aaron Bentley
Update from review
202
        """Determine the graph difference between two revisions"""
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
203
        border, common, searchers = self._find_border_ancestors(
2490.2.23 by Aaron Bentley
Adapt find_borders to produce a graph difference
204
            [left_revision, right_revision])
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
205
        self._search_for_extra_common(common, searchers)
206
        left = searchers[0].seen
207
        right = searchers[1].seen
208
        return (left.difference(right), right.difference(left))
2490.2.23 by Aaron Bentley
Adapt find_borders to produce a graph difference
209
3377.3.21 by John Arbash Meinel
Simple brute-force implementation of find_unique_ancestors
210
    def find_unique_ancestors(self, unique_revision, common_revisions):
211
        """Find the unique ancestors for a revision versus others.
212
213
        This returns the ancestry of unique_revision, excluding all revisions
214
        in the ancestry of common_revisions. If unique_revision is in the
215
        ancestry, then the empty set will be returned.
216
217
        :param unique_revision: The revision_id whose ancestry we are
218
            interested in.
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
219
            XXX: Would this API be better if we allowed multiple revisions on
220
                 to be searched here?
3377.3.21 by John Arbash Meinel
Simple brute-force implementation of find_unique_ancestors
221
        :param common_revisions: Revision_ids of ancestries to exclude.
222
        :return: A set of revisions in the ancestry of unique_revision
223
        """
224
        if unique_revision in common_revisions:
225
            return set()
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
226
227
        # Algorithm description
228
        # 1) Walk backwards from the unique node and all common nodes.
229
        # 2) When a node is seen by both sides, stop searching it in the unique
230
        #    walker, include it in the common walker.
231
        # 3) Stop searching when there are no nodes left for the unique walker.
232
        #    At this point, you have a maximal set of unique nodes. Some of
233
        #    them may actually be common, and you haven't reached them yet.
234
        # 4) Start new searchers for the unique nodes, seeded with the
235
        #    information you have so far.
236
        # 5) Continue searching, stopping the common searches when the search
237
        #    tip is an ancestor of all unique nodes.
238
        # 6) Search is done when all common searchers have completed.
239
240
        unique_searcher = self._make_breadth_first_searcher([unique_revision])
3377.3.27 by John Arbash Meinel
some simple updates
241
        # we know that unique_revision isn't in common_revisions
242
        unique_searcher.next()
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
243
        common_searcher = self._make_breadth_first_searcher(common_revisions)
244
3377.3.27 by John Arbash Meinel
some simple updates
245
        while unique_searcher._next_query:
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
246
            next_unique_nodes = set(unique_searcher.step())
247
            next_common_nodes = set(common_searcher.step())
248
249
            # Check if either searcher encounters new nodes seen by the other
250
            # side.
251
            unique_are_common_nodes = next_unique_nodes.intersection(
252
                common_searcher.seen)
253
            unique_are_common_nodes.update(
254
                next_common_nodes.intersection(unique_searcher.seen))
255
            if unique_are_common_nodes:
3377.3.29 by John Arbash Meinel
Revert the _find_any_seen change.
256
                ancestors = unique_searcher.find_seen_ancestors(
257
                                unique_are_common_nodes)
258
                ancestors.update(common_searcher.find_seen_ancestors(ancestors))
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
259
                unique_searcher.stop_searching_any(ancestors)
260
                common_searcher.start_searching(ancestors)
261
262
        unique_nodes = unique_searcher.seen.difference(common_searcher.seen)
263
        if not unique_nodes:
264
            return unique_nodes
265
        unique_tips = self._remove_simple_descendants(unique_nodes,
266
                        self.get_parent_map(unique_nodes))
267
268
        unique_searchers = []
269
        for tip in unique_tips:
270
            revs_to_search = unique_searcher.find_seen_ancestors([tip])
271
            searcher = self._make_breadth_first_searcher(revs_to_search)
272
            # We don't care about the starting nodes.
273
            searcher.step()
274
            unique_searchers.append(searcher)
275
276
        ancestor_all_unique = None
277
        for searcher in unique_searchers:
278
            if ancestor_all_unique is None:
279
                ancestor_all_unique = set(searcher.seen)
280
            else:
281
                ancestor_all_unique = ancestor_all_unique.intersection(
282
                                            searcher.seen)
283
284
        # Stop any search tips that are already known as ancestors of the
285
        # unique nodes
3377.3.28 by John Arbash Meinel
Try using _find_any_seen_ancestors,
286
        common_searcher.stop_searching_any(
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
287
            common_searcher.find_seen_ancestors(ancestor_all_unique))
288
3377.3.32 by John Arbash Meinel
try collapsing the common revisions into a single searcher.
289
        # Collapse all the common nodes into a single searcher
290
        all_unique_searcher = self._make_breadth_first_searcher(ancestor_all_unique)
291
        next_unique_searchers = []
292
        for searcher in unique_searchers:
293
            searcher.stop_searching_any(
294
                searcher.find_seen_ancestors(ancestor_all_unique))
295
            if searcher._next_query:
296
                next_unique_searchers.append(searcher)
297
        trace.mutter('Collapsed %s unique searchers into 1 + %s',
298
                     len(unique_searchers), len(next_unique_searchers))
299
        unique_searchers = next_unique_searchers
300
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
301
        # While we still have common nodes to search
302
        while common_searcher._next_query:
303
            newly_seen_common = set(common_searcher.step())
304
            newly_seen_unique = set()
305
            for searcher in unique_searchers:
306
                newly_seen_unique.update(searcher.step())
307
            # These nodes are common ancestors of all unique nodes
308
            unique_are_common_nodes = newly_seen_unique.copy()
309
            for searcher in unique_searchers:
310
                unique_are_common_nodes = unique_are_common_nodes.intersection(
311
                                            searcher.seen)
3377.3.32 by John Arbash Meinel
try collapsing the common revisions into a single searcher.
312
            unique_are_common_nodes.update(all_unique_searcher.step())
313
            # TODO: I think we can just use ancestor_all_unique.seen, instead of
314
            #       ancestor_all_unique
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
315
            ancestor_all_unique.update(unique_are_common_nodes)
316
            if newly_seen_common:
317
                # If a 'common' node is an ancestor of all unique searchers, we
318
                # can stop searching it.
319
                common_searcher.stop_searching_any(
320
                    ancestor_all_unique.intersection(newly_seen_common))
321
            if unique_are_common_nodes:
322
                # We have new common-to-all-unique-searchers nodes
3377.3.32 by John Arbash Meinel
try collapsing the common revisions into a single searcher.
323
                unique_are_common_nodes.update(
324
                    all_unique_searcher.find_seen_ancestors(unique_are_common_nodes))
3377.3.29 by John Arbash Meinel
Revert the _find_any_seen change.
325
                for searcher in unique_searchers:
326
                    unique_are_common_nodes.update(
327
                        searcher.find_seen_ancestors(unique_are_common_nodes))
328
                # Since these are common, we can grab another set of ancestors
329
                # that we have seen
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
330
                unique_are_common_nodes.update(
3377.3.29 by John Arbash Meinel
Revert the _find_any_seen change.
331
                    common_searcher.find_seen_ancestors(unique_are_common_nodes))
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
332
3377.3.32 by John Arbash Meinel
try collapsing the common revisions into a single searcher.
333
                # The all_unique searcher can start searching the common nodes
334
                # but everyone else can stop.
335
                all_unique_searcher.start_searching(unique_are_common_nodes)
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
336
                for searcher in unique_searchers:
3377.3.32 by John Arbash Meinel
try collapsing the common revisions into a single searcher.
337
                    searcher.stop_searching_any(unique_are_common_nodes)
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
338
                common_searcher.stop_searching_any(unique_are_common_nodes)
339
340
                # Filter out searchers that don't actually search different
341
                # nodes. We already have the ancestry intersection for them
342
                next_unique_searchers = []
343
                unique_search_sets = set()
344
                for searcher in unique_searchers:
345
                    will_search_set = frozenset(searcher._next_query)
3377.3.32 by John Arbash Meinel
try collapsing the common revisions into a single searcher.
346
                    if (will_search_set
347
                        and will_search_set not in unique_search_sets):
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
348
                        # This searcher is searching a unique set of nodes, let it
349
                        unique_search_sets.add(will_search_set)
350
                        next_unique_searchers.append(searcher)
351
                unique_searchers = next_unique_searchers
352
        return unique_nodes.difference(common_searcher.seen)
3377.3.21 by John Arbash Meinel
Simple brute-force implementation of find_unique_ancestors
353
3172.1.2 by Robert Collins
Parent Providers should now implement ``get_parent_map`` returning a
354
    @symbol_versioning.deprecated_method(symbol_versioning.one_one)
355
    def get_parents(self, revisions):
356
        """Find revision ids of the parents of a list of revisions
357
358
        A list is returned of the same length as the input.  Each entry
359
        is a list of parent ids for the corresponding input revision.
360
361
        [NULL_REVISION] is used as the parent of the first user-committed
362
        revision.  Its parent list is empty.
363
364
        If the revision is not present (i.e. a ghost), None is used in place
365
        of the list of parents.
366
367
        Deprecated in bzr 1.2 - please see get_parent_map.
368
        """
369
        parents = self.get_parent_map(revisions)
3377.3.5 by John Arbash Meinel
Fix a latent bug in Graph.get_parents()
370
        return [parents.get(r, None) for r in revisions]
3172.1.2 by Robert Collins
Parent Providers should now implement ``get_parent_map`` returning a
371
372
    def get_parent_map(self, revisions):
373
        """Get a map of key:parent_list for revisions.
374
375
        This implementation delegates to get_parents, for old parent_providers
376
        that do not supply get_parent_map.
377
        """
378
        result = {}
379
        for rev, parents in self.get_parents(revisions):
380
            if parents is not None:
381
                result[rev] = parents
382
        return result
383
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
384
    def _make_breadth_first_searcher(self, revisions):
385
        return _BreadthFirstSearcher(revisions, self)
386
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
387
    def _find_border_ancestors(self, revisions):
2490.2.12 by Aaron Bentley
Improve documentation
388
        """Find common ancestors with at least one uncommon descendant.
389
390
        Border ancestors are identified using a breadth-first
391
        search starting at the bottom of the graph.  Searches are stopped
392
        whenever a node or one of its descendants is determined to be common.
393
394
        This will scale with the number of uncommon ancestors.
2490.2.25 by Aaron Bentley
Update from review
395
396
        As well as the border ancestors, a set of seen common ancestors and a
397
        list of sets of seen ancestors for each input revision is returned.
398
        This allows calculation of graph difference from the results of this
399
        operation.
2490.2.12 by Aaron Bentley
Improve documentation
400
        """
2490.2.28 by Aaron Bentley
Fix handling of null revision
401
        if None in revisions:
402
            raise errors.InvalidRevisionId(None, self)
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
403
        common_ancestors = set()
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
404
        searchers = [self._make_breadth_first_searcher([r])
405
                     for r in revisions]
406
        active_searchers = searchers[:]
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
407
        border_ancestors = set()
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
408
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
409
        while True:
410
            newly_seen = set()
3377.3.2 by John Arbash Meinel
find_difference is fixed by updating _find_border_ancestors.... is that reasonable?
411
            for searcher in searchers:
412
                new_ancestors = searcher.step()
413
                if new_ancestors:
414
                    newly_seen.update(new_ancestors)
415
            new_common = set()
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
416
            for revision in newly_seen:
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
417
                if revision in common_ancestors:
3377.3.2 by John Arbash Meinel
find_difference is fixed by updating _find_border_ancestors.... is that reasonable?
418
                    # Not a border ancestor because it was seen as common
419
                    # already
420
                    new_common.add(revision)
2490.2.19 by Aaron Bentley
Implement common-ancestor-based culling
421
                    continue
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
422
                for searcher in searchers:
423
                    if revision not in searcher.seen:
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
424
                        break
425
                else:
3377.3.2 by John Arbash Meinel
find_difference is fixed by updating _find_border_ancestors.... is that reasonable?
426
                    # This is a border because it is a first common that we see
427
                    # after walking for a while.
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
428
                    border_ancestors.add(revision)
3377.3.2 by John Arbash Meinel
find_difference is fixed by updating _find_border_ancestors.... is that reasonable?
429
                    new_common.add(revision)
430
            if new_common:
431
                for searcher in searchers:
432
                    new_common.update(searcher.find_seen_ancestors(new_common))
433
                for searcher in searchers:
434
                    searcher.start_searching(new_common)
435
                common_ancestors.update(new_common)
436
437
            # Figure out what the searchers will be searching next, and if
438
            # there is only 1 set being searched, then we are done searching,
439
            # since all searchers would have to be searching the same data,
440
            # thus it *must* be in common.
441
            unique_search_sets = set()
442
            for searcher in searchers:
443
                will_search_set = frozenset(searcher._next_query)
444
                if will_search_set not in unique_search_sets:
445
                    # This searcher is searching a unique set of nodes, let it
446
                    unique_search_sets.add(will_search_set)
447
448
            if len(unique_search_sets) == 1:
449
                nodes = unique_search_sets.pop()
450
                uncommon_nodes = nodes.difference(common_ancestors)
451
                assert not uncommon_nodes, ("Somehow we ended up converging"
452
                                            " without actually marking them as"
453
                                            " in common."
454
                                            "\nStart_nodes: %s"
455
                                            "\nuncommon_nodes: %s"
456
                                            % (revisions, uncommon_nodes))
457
                break
458
        return border_ancestors, common_ancestors, searchers
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
459
2776.3.1 by Robert Collins
* Deprecated method ``find_previous_heads`` on
460
    def heads(self, keys):
461
        """Return the heads from amongst keys.
462
463
        This is done by searching the ancestries of each key.  Any key that is
464
        reachable from another key is not returned; all the others are.
465
466
        This operation scales with the relative depth between any two keys. If
467
        any two keys are completely disconnected all ancestry of both sides
468
        will be retrieved.
469
470
        :param keys: An iterable of keys.
2776.1.4 by Robert Collins
Trivial review feedback changes.
471
        :return: A set of the heads. Note that as a set there is no ordering
472
            information. Callers will need to filter their input to create
473
            order if they need it.
2490.2.12 by Aaron Bentley
Improve documentation
474
        """
2776.1.4 by Robert Collins
Trivial review feedback changes.
475
        candidate_heads = set(keys)
3052.5.5 by John Arbash Meinel
Special case Graph.heads() for NULL_REVISION rather than is_ancestor.
476
        if revision.NULL_REVISION in candidate_heads:
477
            # NULL_REVISION is only a head if it is the only entry
478
            candidate_heads.remove(revision.NULL_REVISION)
479
            if not candidate_heads:
480
                return set([revision.NULL_REVISION])
2850.2.1 by Robert Collins
(robertc) Special case the zero-or-no-heads case for Graph.heads(). (Robert Collins)
481
        if len(candidate_heads) < 2:
482
            return candidate_heads
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
483
        searchers = dict((c, self._make_breadth_first_searcher([c]))
2776.1.4 by Robert Collins
Trivial review feedback changes.
484
                          for c in candidate_heads)
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
485
        active_searchers = dict(searchers)
486
        # skip over the actual candidate for each searcher
487
        for searcher in active_searchers.itervalues():
1551.15.81 by Aaron Bentley
Remove testing code
488
            searcher.next()
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
489
        # The common walker finds nodes that are common to two or more of the
490
        # input keys, so that we don't access all history when a currently
491
        # uncommon search point actually meets up with something behind a
492
        # common search point. Common search points do not keep searches
493
        # active; they just allow us to make searches inactive without
494
        # accessing all history.
495
        common_walker = self._make_breadth_first_searcher([])
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
496
        while len(active_searchers) > 0:
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
497
            ancestors = set()
498
            # advance searches
499
            try:
500
                common_walker.next()
501
            except StopIteration:
2921.3.4 by Robert Collins
Review feedback.
502
                # 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
503
                pass
1551.15.78 by Aaron Bentley
Fix KeyError in filter_candidate_lca
504
            for candidate in active_searchers.keys():
505
                try:
506
                    searcher = active_searchers[candidate]
507
                except KeyError:
508
                    # rare case: we deleted candidate in a previous iteration
509
                    # through this for loop, because it was determined to be
510
                    # a descendant of another candidate.
511
                    continue
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
512
                try:
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
513
                    ancestors.update(searcher.next())
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
514
                except StopIteration:
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
515
                    del active_searchers[candidate]
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
516
                    continue
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
517
            # process found nodes
518
            new_common = set()
519
            for ancestor in ancestors:
520
                if ancestor in candidate_heads:
521
                    candidate_heads.remove(ancestor)
522
                    del searchers[ancestor]
523
                    if ancestor in active_searchers:
524
                        del active_searchers[ancestor]
525
                # it may meet up with a known common node
2921.3.4 by Robert Collins
Review feedback.
526
                if ancestor in common_walker.seen:
527
                    # some searcher has encountered our known common nodes:
528
                    # just stop it
529
                    ancestor_set = set([ancestor])
530
                    for searcher in searchers.itervalues():
531
                        searcher.stop_searching_any(ancestor_set)
532
                else:
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
533
                    # or it may have been just reached by all the searchers:
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
534
                    for searcher in searchers.itervalues():
535
                        if ancestor not in searcher.seen:
2490.2.9 by Aaron Bentley
Fix minimal common ancestor algorithm for non-minimal perhipheral ancestors
536
                            break
537
                    else:
2921.3.4 by Robert Collins
Review feedback.
538
                        # The final active searcher has just reached this node,
539
                        # making it be known as a descendant of all candidates,
540
                        # so we can stop searching it, and any seen ancestors
541
                        new_common.add(ancestor)
542
                        for searcher in searchers.itervalues():
543
                            seen_ancestors =\
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
544
                                searcher.find_seen_ancestors([ancestor])
2921.3.4 by Robert Collins
Review feedback.
545
                            searcher.stop_searching_any(seen_ancestors)
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
546
            common_walker.start_searching(new_common)
2776.1.4 by Robert Collins
Trivial review feedback changes.
547
        return candidate_heads
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
548
1551.19.10 by Aaron Bentley
Merge now warns when it encounters a criss-cross
549
    def find_unique_lca(self, left_revision, right_revision,
550
                        count_steps=False):
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
551
        """Find a unique LCA.
552
553
        Find lowest common ancestors.  If there is no unique  common
554
        ancestor, find the lowest common ancestors of those ancestors.
555
556
        Iteration stops when a unique lowest common ancestor is found.
557
        The graph origin is necessarily a unique lowest common ancestor.
2490.2.5 by Aaron Bentley
Use GraphWalker.unique_ancestor to determine merge base
558
559
        Note that None is not an acceptable substitute for NULL_REVISION.
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
560
        in the input for this method.
1551.19.12 by Aaron Bentley
Add documentation for the count_steps parameter of Graph.find_unique_lca
561
562
        :param count_steps: If True, the return value will be a tuple of
563
            (unique_lca, steps) where steps is the number of times that
564
            find_lca was run.  If False, only unique_lca is returned.
2490.2.3 by Aaron Bentley
Implement new merge base picker
565
        """
566
        revisions = [left_revision, right_revision]
1551.19.10 by Aaron Bentley
Merge now warns when it encounters a criss-cross
567
        steps = 0
2490.2.3 by Aaron Bentley
Implement new merge base picker
568
        while True:
1551.19.10 by Aaron Bentley
Merge now warns when it encounters a criss-cross
569
            steps += 1
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
570
            lca = self.find_lca(*revisions)
571
            if len(lca) == 1:
1551.19.10 by Aaron Bentley
Merge now warns when it encounters a criss-cross
572
                result = lca.pop()
573
                if count_steps:
574
                    return result, steps
575
                else:
576
                    return result
2520.4.104 by Aaron Bentley
Avoid infinite loop when there is no unique lca
577
            if len(lca) == 0:
578
                raise errors.NoCommonAncestor(left_revision, right_revision)
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
579
            revisions = lca
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
580
3228.4.4 by John Arbash Meinel
Change iter_ancestry to take a group instead of a single node,
581
    def iter_ancestry(self, revision_ids):
3228.4.2 by John Arbash Meinel
Add a Graph.iter_ancestry()
582
        """Iterate the ancestry of this revision.
583
3228.4.4 by John Arbash Meinel
Change iter_ancestry to take a group instead of a single node,
584
        :param revision_ids: Nodes to start the search
3228.4.2 by John Arbash Meinel
Add a Graph.iter_ancestry()
585
        :return: Yield tuples mapping a revision_id to its parents for the
586
            ancestry of revision_id.
3228.4.10 by John Arbash Meinel
Respond to abentley's review comments.
587
            Ghosts will be returned with None as their parents, and nodes
3228.4.4 by John Arbash Meinel
Change iter_ancestry to take a group instead of a single node,
588
            with no parents will have NULL_REVISION as their only parent. (As
589
            defined by get_parent_map.)
3228.4.10 by John Arbash Meinel
Respond to abentley's review comments.
590
            There will also be a node for (NULL_REVISION, ())
3228.4.2 by John Arbash Meinel
Add a Graph.iter_ancestry()
591
        """
3228.4.4 by John Arbash Meinel
Change iter_ancestry to take a group instead of a single node,
592
        pending = set(revision_ids)
3228.4.2 by John Arbash Meinel
Add a Graph.iter_ancestry()
593
        processed = set()
594
        while pending:
595
            processed.update(pending)
596
            next_map = self.get_parent_map(pending)
597
            next_pending = set()
598
            for item in next_map.iteritems():
599
                yield item
600
                next_pending.update(p for p in item[1] if p not in processed)
601
            ghosts = pending.difference(next_map)
602
            for ghost in ghosts:
3228.4.10 by John Arbash Meinel
Respond to abentley's review comments.
603
                yield (ghost, None)
3228.4.2 by John Arbash Meinel
Add a Graph.iter_ancestry()
604
            pending = next_pending
605
2490.2.31 by Aaron Bentley
Fix iter_topo_order to permit un-included parents
606
    def iter_topo_order(self, revisions):
2490.2.30 by Aaron Bentley
Add functionality for tsorting graphs
607
        """Iterate through the input revisions in topological order.
608
609
        This sorting only ensures that parents come before their children.
610
        An ancestor may sort after a descendant if the relationship is not
611
        visible in the supplied list of revisions.
612
        """
3099.3.3 by John Arbash Meinel
Deprecate get_parents() in favor of get_parent_map()
613
        sorter = tsort.TopoSorter(self.get_parent_map(revisions))
2490.2.34 by Aaron Bentley
Update NEWS and change implementation to return an iterator
614
        return sorter.iter_topo_order()
2490.2.30 by Aaron Bentley
Add functionality for tsorting graphs
615
2653.2.1 by Aaron Bentley
Implement Graph.is_ancestor
616
    def is_ancestor(self, candidate_ancestor, candidate_descendant):
2653.2.5 by Aaron Bentley
Update to clarify algorithm
617
        """Determine whether a revision is an ancestor of another.
618
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
619
        We answer this using heads() as heads() has the logic to perform the
3078.2.6 by Ian Clatworthy
fix efficiency of local commit detection as recommended by jameinel's review
620
        smallest number of parent lookups to determine the ancestral
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
621
        relationship between N revisions.
2653.2.5 by Aaron Bentley
Update to clarify algorithm
622
        """
2921.3.1 by Robert Collins
* Graph ``heads()`` queries have been bugfixed to no longer access all
623
        return set([candidate_descendant]) == self.heads(
624
            [candidate_ancestor, candidate_descendant])
2653.2.1 by Aaron Bentley
Implement Graph.is_ancestor
625
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
626
    def _search_for_extra_common(self, common, searchers):
627
        """Make sure that unique nodes are genuinely unique.
628
3377.3.10 by John Arbash Meinel
Tweak _BreadthFirstSearcher.find_seen_ancestors()
629
        After _find_border_ancestors, all nodes marked "common" are indeed
630
        common. Some of the nodes considered unique are not, due to history
631
        shortcuts stopping the searches early.
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
632
633
        We know that we have searched enough when all common search tips are
634
        descended from all unique (uncommon) nodes because we know that a node
635
        cannot be an ancestor of its own ancestor.
636
637
        :param common: A set of common nodes
638
        :param searchers: The searchers returned from _find_border_ancestors
639
        :return: None
640
        """
641
        # Basic algorithm...
642
        #   A) The passed in searchers should all be on the same tips, thus
643
        #      they should be considered the "common" searchers.
644
        #   B) We find the difference between the searchers, these are the
645
        #      "unique" nodes for each side.
646
        #   C) We do a quick culling so that we only start searching from the
647
        #      more interesting unique nodes. (A unique ancestor is more
3377.3.10 by John Arbash Meinel
Tweak _BreadthFirstSearcher.find_seen_ancestors()
648
        #      interesting than any of its children.)
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
649
        #   D) We start searching for ancestors common to all unique nodes.
650
        #   E) We have the common searchers stop searching any ancestors of
651
        #      nodes found by (D)
652
        #   F) When there are no more common search tips, we stop
3377.3.10 by John Arbash Meinel
Tweak _BreadthFirstSearcher.find_seen_ancestors()
653
654
        # TODO: We need a way to remove unique_searchers when they overlap with
655
        #       other unique searchers.
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
656
        assert len(searchers) == 2, (
657
            "Algorithm not yet implemented for > 2 searchers")
658
        common_searchers = searchers
659
        left_searcher = searchers[0]
660
        right_searcher = searchers[1]
3377.3.15 by John Arbash Meinel
minor update
661
        unique = left_searcher.seen.symmetric_difference(right_searcher.seen)
3377.3.17 by John Arbash Meinel
Keep track of the intersection of unique ancestry,
662
        if not unique: # No unique nodes, nothing to do
663
            return
3377.3.10 by John Arbash Meinel
Tweak _BreadthFirstSearcher.find_seen_ancestors()
664
        total_unique = len(unique)
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
665
        unique = self._remove_simple_descendants(unique,
666
                    self.get_parent_map(unique))
3377.3.10 by John Arbash Meinel
Tweak _BreadthFirstSearcher.find_seen_ancestors()
667
        simple_unique = len(unique)
3377.3.14 by John Arbash Meinel
Take another tack on _search_for_extra
668
669
        unique_searchers = []
670
        for revision_id in unique:
3377.3.15 by John Arbash Meinel
minor update
671
            if revision_id in left_searcher.seen:
3377.3.14 by John Arbash Meinel
Take another tack on _search_for_extra
672
                parent_searcher = left_searcher
673
            else:
674
                parent_searcher = right_searcher
675
            revs_to_search = parent_searcher.find_seen_ancestors([revision_id])
676
            if not revs_to_search: # XXX: This shouldn't be possible
677
                revs_to_search = [revision_id]
3377.3.15 by John Arbash Meinel
minor update
678
            searcher = self._make_breadth_first_searcher(revs_to_search)
679
            # We don't care about the starting nodes.
680
            searcher.step()
681
            unique_searchers.append(searcher)
3377.3.14 by John Arbash Meinel
Take another tack on _search_for_extra
682
3377.3.16 by John Arbash Meinel
small cleanups
683
        # possible todo: aggregate the common searchers into a single common
684
        #   searcher, just make sure that we include the nodes into the .seen
685
        #   properties of the original searchers
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
686
3377.3.17 by John Arbash Meinel
Keep track of the intersection of unique ancestry,
687
        ancestor_all_unique = None
688
        for searcher in unique_searchers:
689
            if ancestor_all_unique is None:
690
                ancestor_all_unique = set(searcher.seen)
691
            else:
692
                ancestor_all_unique = ancestor_all_unique.intersection(
693
                                            searcher.seen)
694
3377.3.23 by John Arbash Meinel
Implement find_unique_ancestors using more explicit graph searching.
695
        trace.mutter('Started %s unique searchers for %s unique revisions',
696
                     simple_unique, total_unique)
3377.3.19 by John Arbash Meinel
Start culling unique searchers once they converge.
697
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
698
        while True: # If we have no more nodes we have nothing to do
699
            newly_seen_common = set()
700
            for searcher in common_searchers:
701
                newly_seen_common.update(searcher.step())
702
            newly_seen_unique = set()
703
            for searcher in unique_searchers:
704
                newly_seen_unique.update(searcher.step())
705
            new_common_unique = set()
706
            for revision in newly_seen_unique:
707
                for searcher in unique_searchers:
708
                    if revision not in searcher.seen:
709
                        break
710
                else:
711
                    # This is a border because it is a first common that we see
712
                    # after walking for a while.
713
                    new_common_unique.add(revision)
714
            if newly_seen_common:
715
                # These are nodes descended from one of the 'common' searchers.
716
                # Make sure all searchers are on the same page
717
                for searcher in common_searchers:
3377.3.16 by John Arbash Meinel
small cleanups
718
                    newly_seen_common.update(
719
                        searcher.find_seen_ancestors(newly_seen_common))
3377.3.14 by John Arbash Meinel
Take another tack on _search_for_extra
720
                # We start searching the whole ancestry. It is a bit wasteful,
721
                # though. We really just want to mark all of these nodes as
722
                # 'seen' and then start just the tips. However, it requires a
723
                # get_parent_map() call to figure out the tips anyway, and all
724
                # redundant requests should be fairly fast.
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
725
                for searcher in common_searchers:
726
                    searcher.start_searching(newly_seen_common)
3377.3.13 by John Arbash Meinel
Change _search_for_extra_common slightly.
727
3377.3.17 by John Arbash Meinel
Keep track of the intersection of unique ancestry,
728
                # If a 'common' node is an ancestor of all unique searchers, we
3377.3.13 by John Arbash Meinel
Change _search_for_extra_common slightly.
729
                # can stop searching it.
3377.3.17 by John Arbash Meinel
Keep track of the intersection of unique ancestry,
730
                stop_searching_common = ancestor_all_unique.intersection(
731
                                            newly_seen_common)
3377.3.13 by John Arbash Meinel
Change _search_for_extra_common slightly.
732
                if stop_searching_common:
733
                    for searcher in common_searchers:
734
                        searcher.stop_searching_any(stop_searching_common)
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
735
            if new_common_unique:
3377.3.20 by John Arbash Meinel
comment cleanups.
736
                # We found some ancestors that are common
3377.3.10 by John Arbash Meinel
Tweak _BreadthFirstSearcher.find_seen_ancestors()
737
                for searcher in unique_searchers:
3377.3.16 by John Arbash Meinel
small cleanups
738
                    new_common_unique.update(
739
                        searcher.find_seen_ancestors(new_common_unique))
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
740
                # Since these are common, we can grab another set of ancestors
741
                # that we have seen
742
                for searcher in common_searchers:
3377.3.16 by John Arbash Meinel
small cleanups
743
                    new_common_unique.update(
744
                        searcher.find_seen_ancestors(new_common_unique))
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
745
746
                # We can tell all of the unique searchers to start at these
747
                # nodes, and tell all of the common searchers to *stop*
748
                # searching these nodes
749
                for searcher in unique_searchers:
750
                    searcher.start_searching(new_common_unique)
751
                for searcher in common_searchers:
752
                    searcher.stop_searching_any(new_common_unique)
3377.3.17 by John Arbash Meinel
Keep track of the intersection of unique ancestry,
753
                ancestor_all_unique.update(new_common_unique)
3377.3.19 by John Arbash Meinel
Start culling unique searchers once they converge.
754
3377.3.20 by John Arbash Meinel
comment cleanups.
755
                # Filter out searchers that don't actually search different
756
                # nodes. We already have the ancestry intersection for them
3377.3.19 by John Arbash Meinel
Start culling unique searchers once they converge.
757
                next_unique_searchers = []
758
                unique_search_sets = set()
759
                for searcher in unique_searchers:
760
                    will_search_set = frozenset(searcher._next_query)
761
                    if will_search_set not in unique_search_sets:
762
                        # This searcher is searching a unique set of nodes, let it
763
                        unique_search_sets.add(will_search_set)
764
                        next_unique_searchers.append(searcher)
765
                unique_searchers = next_unique_searchers
3377.3.2 by John Arbash Meinel
find_difference is fixed by updating _find_border_ancestors.... is that reasonable?
766
            for searcher in common_searchers:
767
                if searcher._next_query:
768
                    break
769
            else:
770
                # All common searcher have stopped searching
3377.3.16 by John Arbash Meinel
small cleanups
771
                return
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
772
773
    def _remove_simple_descendants(self, revisions, parent_map):
774
        """remove revisions which are children of other ones in the set
775
776
        This doesn't do any graph searching, it just checks the immediate
777
        parent_map to find if there are any children which can be removed.
778
779
        :param revisions: A set of revision_ids
780
        :return: A set of revision_ids with the children removed
781
        """
782
        simple_ancestors = revisions.copy()
783
        # TODO: jam 20071214 we *could* restrict it to searching only the
784
        #       parent_map of revisions already present in 'revisions', but
785
        #       considering the general use case, I think this is actually
786
        #       better.
787
788
        # This is the same as the following loop. I don't know that it is any
789
        # faster.
790
        ## simple_ancestors.difference_update(r for r, p_ids in parent_map.iteritems()
791
        ##     if p_ids is not None and revisions.intersection(p_ids))
792
        ## return simple_ancestors
793
794
        # Yet Another Way, invert the parent map (which can be cached)
795
        ## descendants = {}
796
        ## for revision_id, parent_ids in parent_map.iteritems():
797
        ##   for p_id in parent_ids:
798
        ##       descendants.setdefault(p_id, []).append(revision_id)
799
        ## for revision in revisions.intersection(descendants):
800
        ##   simple_ancestors.difference_update(descendants[revision])
801
        ## return simple_ancestors
802
        for revision, parent_ids in parent_map.iteritems():
803
            if parent_ids is None:
804
                continue
805
            for parent_id in parent_ids:
806
                if parent_id in revisions:
807
                    # This node has a parent present in the set, so we can
808
                    # remove it
809
                    simple_ancestors.discard(revision)
810
                    break
811
        return simple_ancestors
812
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
813
2911.4.1 by Robert Collins
Factor out the Graph.heads() cache from _RevisionTextVersionCache for reuse, and use it in commit.
814
class HeadsCache(object):
815
    """A cache of results for graph heads calls."""
816
817
    def __init__(self, graph):
818
        self.graph = graph
819
        self._heads = {}
820
821
    def heads(self, keys):
822
        """Return the heads of keys.
823
2911.4.3 by Robert Collins
Make the contract of HeadsCache.heads() more clear.
824
        This matches the API of Graph.heads(), specifically the return value is
825
        a set which can be mutated, and ordering of the input is not preserved
826
        in the output.
827
2911.4.1 by Robert Collins
Factor out the Graph.heads() cache from _RevisionTextVersionCache for reuse, and use it in commit.
828
        :see also: Graph.heads.
829
        :param keys: The keys to calculate heads for.
830
        :return: A set containing the heads, which may be mutated without
831
            affecting future lookups.
832
        """
2911.4.2 by Robert Collins
Make HeadsCache actually work.
833
        keys = frozenset(keys)
2911.4.1 by Robert Collins
Factor out the Graph.heads() cache from _RevisionTextVersionCache for reuse, and use it in commit.
834
        try:
835
            return set(self._heads[keys])
836
        except KeyError:
837
            heads = self.graph.heads(keys)
838
            self._heads[keys] = heads
839
            return set(heads)
840
841
3224.1.20 by John Arbash Meinel
Reduce the number of cache misses by caching known heads answers
842
class FrozenHeadsCache(object):
843
    """Cache heads() calls, assuming the caller won't modify them."""
844
845
    def __init__(self, graph):
846
        self.graph = graph
847
        self._heads = {}
848
849
    def heads(self, keys):
850
        """Return the heads of keys.
851
3224.1.24 by John Arbash Meinel
Fix up docstring since FrozenHeadsCache doesn't let you mutate the result.
852
        Similar to Graph.heads(). The main difference is that the return value
853
        is a frozen set which cannot be mutated.
3224.1.20 by John Arbash Meinel
Reduce the number of cache misses by caching known heads answers
854
855
        :see also: Graph.heads.
856
        :param keys: The keys to calculate heads for.
3224.1.24 by John Arbash Meinel
Fix up docstring since FrozenHeadsCache doesn't let you mutate the result.
857
        :return: A frozenset containing the heads.
3224.1.20 by John Arbash Meinel
Reduce the number of cache misses by caching known heads answers
858
        """
859
        keys = frozenset(keys)
860
        try:
861
            return self._heads[keys]
862
        except KeyError:
863
            heads = frozenset(self.graph.heads(keys))
864
            self._heads[keys] = heads
865
            return heads
866
867
    def cache(self, keys, heads):
868
        """Store a known value."""
869
        self._heads[frozenset(keys)] = frozenset(heads)
870
871
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
872
class _BreadthFirstSearcher(object):
2921.3.4 by Robert Collins
Review feedback.
873
    """Parallel search breadth-first the ancestry of revisions.
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
874
875
    This class implements the iterator protocol, but additionally
876
    1. provides a set of seen ancestors, and
877
    2. allows some ancestries to be unsearched, via stop_searching_any
878
    """
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
879
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
880
    def __init__(self, revisions, parents_provider):
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
881
        self._iterations = 0
882
        self._next_query = set(revisions)
883
        self.seen = set()
3184.1.1 by Robert Collins
Add basic get_recipe to the graph breadth first searcher.
884
        self._started_keys = set(self._next_query)
885
        self._stopped_keys = set()
3099.3.1 by John Arbash Meinel
Implement get_parent_map for ParentProviders
886
        self._parents_provider = parents_provider
3177.3.3 by Robert Collins
Review feedback.
887
        self._returning = 'next_with_ghosts'
3184.1.2 by Robert Collins
Add tests for starting and stopping searches in combination with get_recipe.
888
        self._current_present = set()
889
        self._current_ghosts = set()
890
        self._current_parents = {}
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
891
892
    def __repr__(self):
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
893
        if self._iterations:
894
            prefix = "searching"
3099.3.1 by John Arbash Meinel
Implement get_parent_map for ParentProviders
895
        else:
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
896
            prefix = "starting"
897
        search = '%s=%r' % (prefix, list(self._next_query))
898
        return ('_BreadthFirstSearcher(iterations=%d, %s,'
899
                ' seen=%r)' % (self._iterations, search, list(self.seen)))
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
900
3184.1.6 by Robert Collins
Create a SearchResult object which can be used as a replacement for sets.
901
    def get_result(self):
902
        """Get a SearchResult for the current state of this searcher.
3184.1.1 by Robert Collins
Add basic get_recipe to the graph breadth first searcher.
903
        
3184.1.6 by Robert Collins
Create a SearchResult object which can be used as a replacement for sets.
904
        :return: A SearchResult for this search so far. The SearchResult is
905
            static - the search can be advanced and the search result will not
906
            be invalidated or altered.
3184.1.1 by Robert Collins
Add basic get_recipe to the graph breadth first searcher.
907
        """
908
        if self._returning == 'next':
909
            # We have to know the current nodes children to be able to list the
910
            # exclude keys for them. However, while we could have a second
911
            # look-ahead result buffer and shuffle things around, this method
912
            # is typically only called once per search - when memoising the
3211.5.1 by Robert Collins
Change the smart server get_parents method to take a graph search to exclude already recieved parents from. This prevents history shortcuts causing huge numbers of duplicates.
913
            # results of the search. 
3184.1.1 by Robert Collins
Add basic get_recipe to the graph breadth first searcher.
914
            found, ghosts, next, parents = self._do_query(self._next_query)
915
            # pretend we didn't query: perhaps we should tweak _do_query to be
916
            # entirely stateless?
917
            self.seen.difference_update(next)
3184.1.3 by Robert Collins
Automatically exclude ghosts.
918
            next_query = next.union(ghosts)
3184.1.1 by Robert Collins
Add basic get_recipe to the graph breadth first searcher.
919
        else:
920
            next_query = self._next_query
3184.1.5 by Robert Collins
Record the number of found revisions for cross checking.
921
        excludes = self._stopped_keys.union(next_query)
3184.1.6 by Robert Collins
Create a SearchResult object which can be used as a replacement for sets.
922
        included_keys = self.seen.difference(excludes)
923
        return SearchResult(self._started_keys, excludes, len(included_keys),
924
            included_keys)
3184.1.1 by Robert Collins
Add basic get_recipe to the graph breadth first searcher.
925
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
926
    def step(self):
927
        try:
928
            return self.next()
929
        except StopIteration:
930
            return ()
931
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
932
    def next(self):
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
933
        """Return the next ancestors of this revision.
934
2490.2.12 by Aaron Bentley
Improve documentation
935
        Ancestors are returned in the order they are seen in a breadth-first
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
936
        traversal.  No ancestor will be returned more than once. Ancestors are
937
        returned before their parentage is queried, so ghosts and missing
938
        revisions (including the start revisions) are included in the result.
939
        This can save a round trip in LCA style calculation by allowing
940
        convergence to be detected without reading the data for the revision
941
        the convergence occurs on.
942
943
        :return: A set of revision_ids.
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
944
        """
3177.3.3 by Robert Collins
Review feedback.
945
        if self._returning != 'next':
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
946
            # switch to returning the query, not the results.
3177.3.3 by Robert Collins
Review feedback.
947
            self._returning = 'next'
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
948
            self._iterations += 1
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
949
        else:
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
950
            self._advance()
951
        if len(self._next_query) == 0:
952
            raise StopIteration()
3184.1.1 by Robert Collins
Add basic get_recipe to the graph breadth first searcher.
953
        # We have seen what we're querying at this point as we are returning
954
        # the query, not the results.
955
        self.seen.update(self._next_query)
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
956
        return self._next_query
957
958
    def next_with_ghosts(self):
959
        """Return the next found ancestors, with ghosts split out.
960
        
961
        Ancestors are returned in the order they are seen in a breadth-first
962
        traversal.  No ancestor will be returned more than once. Ancestors are
3177.3.3 by Robert Collins
Review feedback.
963
        returned only after asking for their parents, which allows us to detect
964
        which revisions are ghosts and which are not.
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
965
966
        :return: A tuple with (present ancestors, ghost ancestors) sets.
967
        """
3177.3.3 by Robert Collins
Review feedback.
968
        if self._returning != 'next_with_ghosts':
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
969
            # switch to returning the results, not the current query.
3177.3.3 by Robert Collins
Review feedback.
970
            self._returning = 'next_with_ghosts'
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
971
            self._advance()
972
        if len(self._next_query) == 0:
973
            raise StopIteration()
974
        self._advance()
975
        return self._current_present, self._current_ghosts
976
977
    def _advance(self):
978
        """Advance the search.
979
980
        Updates self.seen, self._next_query, self._current_present,
3177.3.3 by Robert Collins
Review feedback.
981
        self._current_ghosts, self._current_parents and self._iterations.
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
982
        """
983
        self._iterations += 1
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
984
        found, ghosts, next, parents = self._do_query(self._next_query)
985
        self._current_present = found
986
        self._current_ghosts = ghosts
987
        self._next_query = next
988
        self._current_parents = parents
3184.1.3 by Robert Collins
Automatically exclude ghosts.
989
        # ghosts are implicit stop points, otherwise the search cannot be
990
        # repeated when ghosts are filled.
991
        self._stopped_keys.update(ghosts)
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
992
993
    def _do_query(self, revisions):
994
        """Query for revisions.
995
3184.1.4 by Robert Collins
Correctly exclude ghosts when ghosts are started on an existing search.
996
        Adds revisions to the seen set.
997
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
998
        :param revisions: Revisions to query.
999
        :return: A tuple: (set(found_revisions), set(ghost_revisions),
1000
           set(parents_of_found_revisions), dict(found_revisions:parents)).
1001
        """
3377.3.9 by John Arbash Meinel
Small tweaks to _do_query
1002
        found_revisions = set()
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
1003
        parents_of_found = set()
3184.1.1 by Robert Collins
Add basic get_recipe to the graph breadth first searcher.
1004
        # revisions may contain nodes that point to other nodes in revisions:
1005
        # we want to filter them out.
1006
        self.seen.update(revisions)
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
1007
        parent_map = self._parents_provider.get_parent_map(revisions)
3377.3.9 by John Arbash Meinel
Small tweaks to _do_query
1008
        found_revisions.update(parent_map)
3177.3.1 by Robert Collins
* New method ``next_with_ghosts`` on the Graph breadth-first-search objects
1009
        for rev_id, parents in parent_map.iteritems():
3377.3.9 by John Arbash Meinel
Small tweaks to _do_query
1010
            new_found_parents = [p for p in parents if p not in self.seen]
1011
            if new_found_parents:
1012
                # Calling set.update() with an empty generator is actually
1013
                # rather expensive.
1014
                parents_of_found.update(new_found_parents)
1015
        ghost_revisions = revisions - found_revisions
1016
        return found_revisions, ghost_revisions, parents_of_found, parent_map
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
1017
2490.2.8 by Aaron Bentley
fix iteration stuff
1018
    def __iter__(self):
1019
        return self
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
1020
3377.3.1 by John Arbash Meinel
Bring in some of the changes from graph_update and graph_optimization
1021
    def find_seen_ancestors(self, revisions):
1022
        """Find ancestors of these revisions that have already been seen."""
3377.3.10 by John Arbash Meinel
Tweak _BreadthFirstSearcher.find_seen_ancestors()
1023
        all_seen = self.seen
1024
        pending = set(revisions).intersection(all_seen)
1025
        seen_ancestors = set(pending)
1026
1027
        if self._returning == 'next':
1028
            # self.seen contains what nodes have been returned, not what nodes
1029
            # have been queried. We don't want to probe for nodes that haven't
1030
            # been searched yet.
1031
            not_searched_yet = self._next_query
1032
        else:
1033
            not_searched_yet = ()
3377.3.11 by John Arbash Meinel
Committing a debug thunk that was very helpful
1034
        pending.difference_update(not_searched_yet)
3377.3.10 by John Arbash Meinel
Tweak _BreadthFirstSearcher.find_seen_ancestors()
1035
        get_parent_map = self._parents_provider.get_parent_map
3377.3.12 by John Arbash Meinel
Remove the helpful but ugly thunk
1036
        while pending:
1037
            parent_map = get_parent_map(pending)
1038
            all_parents = []
1039
            # We don't care if it is a ghost, since it can't be seen if it is
1040
            # a ghost
1041
            for parent_ids in parent_map.itervalues():
1042
                all_parents.extend(parent_ids)
1043
            next_pending = all_seen.intersection(all_parents).difference(seen_ancestors)
1044
            seen_ancestors.update(next_pending)
1045
            next_pending.difference_update(not_searched_yet)
1046
            pending = next_pending
3377.3.10 by John Arbash Meinel
Tweak _BreadthFirstSearcher.find_seen_ancestors()
1047
2490.2.7 by Aaron Bentley
Start implementing mca that scales with number of uncommon ancestors
1048
        return seen_ancestors
1049
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
1050
    def stop_searching_any(self, revisions):
1051
        """
1052
        Remove any of the specified revisions from the search list.
1053
1054
        None of the specified revisions are required to be present in the
2490.2.12 by Aaron Bentley
Improve documentation
1055
        search list.  In this case, the call is a no-op.
2490.2.10 by Aaron Bentley
Clarify text, remove unused _get_ancestry method
1056
        """
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
1057
        revisions = frozenset(revisions)
3177.3.3 by Robert Collins
Review feedback.
1058
        if self._returning == 'next':
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
1059
            stopped = self._next_query.intersection(revisions)
1060
            self._next_query = self._next_query.difference(revisions)
1061
        else:
3184.2.1 by Robert Collins
Handle stopping ghosts in searches properly.
1062
            stopped_present = self._current_present.intersection(revisions)
1063
            stopped = stopped_present.union(
1064
                self._current_ghosts.intersection(revisions))
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
1065
            self._current_present.difference_update(stopped)
1066
            self._current_ghosts.difference_update(stopped)
1067
            # stopping 'x' should stop returning parents of 'x', but 
1068
            # not if 'y' always references those same parents
1069
            stop_rev_references = {}
3184.2.1 by Robert Collins
Handle stopping ghosts in searches properly.
1070
            for rev in stopped_present:
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
1071
                for parent_id in self._current_parents[rev]:
1072
                    if parent_id not in stop_rev_references:
1073
                        stop_rev_references[parent_id] = 0
1074
                    stop_rev_references[parent_id] += 1
1075
            # if only the stopped revisions reference it, the ref count will be
1076
            # 0 after this loop
3177.3.3 by Robert Collins
Review feedback.
1077
            for parents in self._current_parents.itervalues():
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
1078
                for parent_id in parents:
1079
                    try:
1080
                        stop_rev_references[parent_id] -= 1
1081
                    except KeyError:
1082
                        pass
1083
            stop_parents = set()
1084
            for rev_id, refs in stop_rev_references.iteritems():
1085
                if refs == 0:
1086
                    stop_parents.add(rev_id)
1087
            self._next_query.difference_update(stop_parents)
3184.1.2 by Robert Collins
Add tests for starting and stopping searches in combination with get_recipe.
1088
        self._stopped_keys.update(stopped)
2490.2.25 by Aaron Bentley
Update from review
1089
        return stopped
2490.2.17 by Aaron Bentley
Add start_searching, tweak stop_searching_any
1090
1091
    def start_searching(self, revisions):
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
1092
        """Add revisions to the search.
1093
1094
        The parents of revisions will be returned from the next call to next()
1095
        or next_with_ghosts(). If next_with_ghosts was the most recently used
1096
        next* call then the return value is the result of looking up the
1097
        ghost/not ghost status of revisions. (A tuple (present, ghosted)).
1098
        """
1099
        revisions = frozenset(revisions)
3184.1.2 by Robert Collins
Add tests for starting and stopping searches in combination with get_recipe.
1100
        self._started_keys.update(revisions)
3184.1.4 by Robert Collins
Correctly exclude ghosts when ghosts are started on an existing search.
1101
        new_revisions = revisions.difference(self.seen)
3177.3.3 by Robert Collins
Review feedback.
1102
        if self._returning == 'next':
3184.1.4 by Robert Collins
Correctly exclude ghosts when ghosts are started on an existing search.
1103
            self._next_query.update(new_revisions)
3377.3.30 by John Arbash Meinel
Can we avoid the extra _do_query in start_searching?
1104
            self.seen.update(new_revisions)
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
1105
        else:
1106
            # perform a query on revisions
3377.3.30 by John Arbash Meinel
Can we avoid the extra _do_query in start_searching?
1107
            revs, ghosts, query, parents = self._do_query(revisions)
1108
            self._stopped_keys.update(ghosts)
3177.3.2 by Robert Collins
Update graph searchers stop_searching_any and start_searching for next_with_ghosts.
1109
            self._current_present.update(revs)
1110
            self._current_ghosts.update(ghosts)
1111
            self._next_query.update(query)
1112
            self._current_parents.update(parents)
1113
            return revs, ghosts
3184.1.6 by Robert Collins
Create a SearchResult object which can be used as a replacement for sets.
1114
1115
1116
class SearchResult(object):
1117
    """The result of a breadth first search.
1118
1119
    A SearchResult provides the ability to reconstruct the search or access a
1120
    set of the keys the search found.
1121
    """
1122
1123
    def __init__(self, start_keys, exclude_keys, key_count, keys):
1124
        """Create a SearchResult.
1125
1126
        :param start_keys: The keys the search started at.
1127
        :param exclude_keys: The keys the search excludes.
1128
        :param key_count: The total number of keys (from start to but not
1129
            including exclude).
1130
        :param keys: The keys the search found. Note that in future we may get
1131
            a SearchResult from a smart server, in which case the keys list is
1132
            not necessarily immediately available.
1133
        """
1134
        self._recipe = (start_keys, exclude_keys, key_count)
1135
        self._keys = frozenset(keys)
1136
1137
    def get_recipe(self):
1138
        """Return a recipe that can be used to replay this search.
1139
        
1140
        The recipe allows reconstruction of the same results at a later date
1141
        without knowing all the found keys. The essential elements are a list
1142
        of keys to start and and to stop at. In order to give reproducible
1143
        results when ghosts are encountered by a search they are automatically
1144
        added to the exclude list (or else ghost filling may alter the
1145
        results).
1146
1147
        :return: A tuple (start_keys_set, exclude_keys_set, revision_count). To
1148
            recreate the results of this search, create a breadth first
1149
            searcher on the same graph starting at start_keys. Then call next()
1150
            (or next_with_ghosts()) repeatedly, and on every result, call
1151
            stop_searching_any on any keys from the exclude_keys set. The
1152
            revision_count value acts as a trivial cross-check - the found
1153
            revisions of the new search should have as many elements as
1154
            revision_count. If it does not, then additional revisions have been
1155
            ghosted since the search was executed the first time and the second
1156
            time.
1157
        """
1158
        return self._recipe
1159
1160
    def get_keys(self):
1161
        """Return the keys found in this search.
1162
1163
        :return: A set of keys.
1164
        """
1165
        return self._keys
1166