/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/graph.py

Add simple tests and docstrings for GraphWalker.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
17
 
from bzrlib import (
18
 
    errors,
19
 
    revision,
20
 
    symbol_versioning,
21
 
    tsort,
22
 
    )
23
 
from bzrlib.deprecated_graph import (node_distances, select_farthest)
24
 
 
25
 
# DIAGRAM of terminology
26
 
#       A
27
 
#       /\
28
 
#      B  C
29
 
#      |  |\
30
 
#      D  E F
31
 
#      |\/| |
32
 
#      |/\|/
33
 
#      G  H
34
 
#
35
 
# In this diagram, relative to G and H:
36
 
# A, B, C, D, E are common ancestors.
37
 
# C, D and E are border ancestors, because each has a non-common descendant.
38
 
# D and E are least common ancestors because none of their descendants are
39
 
# common ancestors.
40
 
# C is not a least common ancestor because its descendant, E, is a common
41
 
# ancestor.
42
 
#
43
 
# The find_unique_lca algorithm will pick A in two steps:
44
 
# 1. find_lca('G', 'H') => ['D', 'E']
45
 
# 2. Since len(['D', 'E']) > 1, find_lca('D', 'E') => ['A']
46
 
 
47
 
 
48
 
class DictParentsProvider(object):
49
 
 
50
 
    def __init__(self, ancestry):
51
 
        self.ancestry = ancestry
52
 
 
53
 
    def __repr__(self):
54
 
        return 'DictParentsProvider(%r)' % self.ancestry
55
 
 
56
 
    @symbol_versioning.deprecated_method(symbol_versioning.one_one)
57
 
    def get_parents(self, revisions):
58
 
        return [self.ancestry.get(r, None) for r in revisions]
59
 
 
60
 
    def get_parent_map(self, keys):
61
 
        """See _StackedParentsProvider.get_parent_map"""
62
 
        ancestry = self.ancestry
63
 
        return dict((k, ancestry[k]) for k in keys if k in ancestry)
64
 
 
65
 
 
66
 
class _StackedParentsProvider(object):
67
 
 
68
 
    def __init__(self, parent_providers):
69
 
        self._parent_providers = parent_providers
70
 
 
71
 
    def __repr__(self):
72
 
        return "_StackedParentsProvider(%r)" % self._parent_providers
73
 
 
74
 
    @symbol_versioning.deprecated_method(symbol_versioning.one_one)
75
 
    def get_parents(self, revision_ids):
76
 
        """Find revision ids of the parents of a list of revisions
77
 
 
78
 
        A list is returned of the same length as the input.  Each entry
79
 
        is a list of parent ids for the corresponding input revision.
80
 
 
81
 
        [NULL_REVISION] is used as the parent of the first user-committed
82
 
        revision.  Its parent list is empty.
83
 
 
84
 
        If the revision is not present (i.e. a ghost), None is used in place
85
 
        of the list of parents.
86
 
        """
87
 
        found = self.get_parent_map(revision_ids)
88
 
        return [found.get(r, None) for r in revision_ids]
89
 
 
90
 
    def get_parent_map(self, keys):
91
 
        """Get a mapping of keys => parents
92
 
 
93
 
        A dictionary is returned with an entry for each key present in this
94
 
        source. If this source doesn't have information about a key, it should
95
 
        not include an entry.
96
 
 
97
 
        [NULL_REVISION] is used as the parent of the first user-committed
98
 
        revision.  Its parent list is empty.
99
 
 
100
 
        :param keys: An iterable returning keys to check (eg revision_ids)
101
 
        :return: A dictionary mapping each key to its parents
102
 
        """
103
 
        found = {}
104
 
        remaining = set(keys)
105
 
        for parents_provider in self._parent_providers:
106
 
            new_found = parents_provider.get_parent_map(remaining)
107
 
            found.update(new_found)
108
 
            remaining.difference_update(new_found)
109
 
            if not remaining:
110
 
                break
111
 
        return found
112
 
 
113
 
 
114
 
class CachingParentsProvider(object):
115
 
    """A parents provider which will cache the revision => parents in a dict.
116
 
 
117
 
    This is useful for providers that have an expensive lookup.
118
 
    """
119
 
 
120
 
    def __init__(self, parent_provider):
121
 
        self._real_provider = parent_provider
122
 
        # Theoretically we could use an LRUCache here
123
 
        self._cache = {}
124
 
 
125
 
    def __repr__(self):
126
 
        return "%s(%r)" % (self.__class__.__name__, self._real_provider)
127
 
 
128
 
    @symbol_versioning.deprecated_method(symbol_versioning.one_one)
129
 
    def get_parents(self, revision_ids):
130
 
        """See _StackedParentsProvider.get_parents"""
131
 
        found = self.get_parent_map(revision_ids)
132
 
        return [found.get(r, None) for r in revision_ids]
133
 
 
134
 
    def get_parent_map(self, keys):
135
 
        """See _StackedParentsProvider.get_parent_map"""
136
 
        needed = set()
137
 
        # If the _real_provider doesn't have a key, we cache a value of None,
138
 
        # which we then later use to realize we cannot provide a value for that
139
 
        # key.
140
 
        parent_map = {}
141
 
        cache = self._cache
142
 
        for key in keys:
143
 
            if key in cache:
144
 
                value = cache[key]
145
 
                if value is not None:
146
 
                    parent_map[key] = value
147
 
            else:
148
 
                needed.add(key)
149
 
 
150
 
        if needed:
151
 
            new_parents = self._real_provider.get_parent_map(needed)
152
 
            cache.update(new_parents)
153
 
            parent_map.update(new_parents)
154
 
            needed.difference_update(new_parents)
155
 
            cache.update(dict.fromkeys(needed, None))
156
 
        return parent_map
157
 
 
158
 
 
159
 
class Graph(object):
160
 
    """Provide incremental access to revision graphs.
161
 
 
162
 
    This is the generic implementation; it is intended to be subclassed to
163
 
    specialize it for other repository types.
164
 
    """
165
 
 
166
 
    def __init__(self, parents_provider):
167
 
        """Construct a Graph that uses several graphs as its input
168
 
 
169
 
        This should not normally be invoked directly, because there may be
170
 
        specialized implementations for particular repository types.  See
171
 
        Repository.get_graph()
172
 
 
173
 
        :param parents_provider: An object providing a get_parents call
174
 
            conforming to the behavior of StackedParentsProvider.get_parents
175
 
        """
176
 
        self.get_parents = parents_provider.get_parents
177
 
        self.get_parent_map = parents_provider.get_parent_map
178
 
        self._parents_provider = parents_provider
179
 
 
180
 
    def __repr__(self):
181
 
        return 'Graph(%r)' % self._parents_provider
182
 
 
183
 
    def find_lca(self, *revisions):
184
 
        """Determine the lowest common ancestors of the provided revisions
185
 
 
186
 
        A lowest common ancestor is a common ancestor none of whose
187
 
        descendants are common ancestors.  In graphs, unlike trees, there may
188
 
        be multiple lowest common ancestors.
189
 
 
190
 
        This algorithm has two phases.  Phase 1 identifies border ancestors,
191
 
        and phase 2 filters border ancestors to determine lowest common
192
 
        ancestors.
193
 
 
194
 
        In phase 1, border ancestors are identified, using a breadth-first
195
 
        search starting at the bottom of the graph.  Searches are stopped
196
 
        whenever a node or one of its descendants is determined to be common
197
 
 
198
 
        In phase 2, the border ancestors are filtered to find the least
199
 
        common ancestors.  This is done by searching the ancestries of each
200
 
        border ancestor.
201
 
 
202
 
        Phase 2 is perfomed on the principle that a border ancestor that is
203
 
        not an ancestor of any other border ancestor is a least common
204
 
        ancestor.
205
 
 
206
 
        Searches are stopped when they find a node that is determined to be a
207
 
        common ancestor of all border ancestors, because this shows that it
208
 
        cannot be a descendant of any border ancestor.
209
 
 
210
 
        The scaling of this operation should be proportional to
211
 
        1. The number of uncommon ancestors
212
 
        2. The number of border ancestors
213
 
        3. The length of the shortest path between a border ancestor and an
214
 
           ancestor of all border ancestors.
215
 
        """
216
 
        border_common, common, sides = self._find_border_ancestors(revisions)
217
 
        # We may have common ancestors that can be reached from each other.
218
 
        # - ask for the heads of them to filter it down to only ones that
219
 
        # cannot be reached from each other - phase 2.
220
 
        return self.heads(border_common)
221
 
 
222
 
    def find_difference(self, left_revision, right_revision):
223
 
        """Determine the graph difference between two revisions"""
224
 
        border, common, (left, right) = self._find_border_ancestors(
225
 
            [left_revision, right_revision])
226
 
        return (left.difference(right).difference(common),
227
 
                right.difference(left).difference(common))
228
 
 
229
 
    def _make_breadth_first_searcher(self, revisions):
230
 
        return _BreadthFirstSearcher(revisions, self)
231
 
 
232
 
    def _find_border_ancestors(self, revisions):
233
 
        """Find common ancestors with at least one uncommon descendant.
234
 
 
235
 
        Border ancestors are identified using a breadth-first
236
 
        search starting at the bottom of the graph.  Searches are stopped
237
 
        whenever a node or one of its descendants is determined to be common.
238
 
 
239
 
        This will scale with the number of uncommon ancestors.
240
 
 
241
 
        As well as the border ancestors, a set of seen common ancestors and a
242
 
        list of sets of seen ancestors for each input revision is returned.
243
 
        This allows calculation of graph difference from the results of this
244
 
        operation.
245
 
        """
246
 
        if None in revisions:
247
 
            raise errors.InvalidRevisionId(None, self)
248
 
        common_searcher = self._make_breadth_first_searcher([])
249
 
        common_ancestors = set()
250
 
        searchers = [self._make_breadth_first_searcher([r])
251
 
                     for r in revisions]
252
 
        active_searchers = searchers[:]
253
 
        border_ancestors = set()
254
 
        def update_common(searcher, revisions):
255
 
            w_seen_ancestors = searcher.find_seen_ancestors(
256
 
                revision)
257
 
            stopped = searcher.stop_searching_any(w_seen_ancestors)
258
 
            common_ancestors.update(w_seen_ancestors)
259
 
            common_searcher.start_searching(stopped)
260
 
 
261
 
        while True:
262
 
            if len(active_searchers) == 0:
263
 
                return border_ancestors, common_ancestors, [s.seen for s in
264
 
                                                            searchers]
265
 
            try:
266
 
                new_common = common_searcher.next()
267
 
                common_ancestors.update(new_common)
268
 
            except StopIteration:
269
 
                pass
270
 
            else:
271
 
                for searcher in active_searchers:
272
 
                    for revision in new_common.intersection(searcher.seen):
273
 
                        update_common(searcher, revision)
274
 
 
275
 
            newly_seen = set()
276
 
            new_active_searchers = []
277
 
            for searcher in active_searchers:
278
 
                try:
279
 
                    newly_seen.update(searcher.next())
280
 
                except StopIteration:
281
 
                    pass
282
 
                else:
283
 
                    new_active_searchers.append(searcher)
284
 
            active_searchers = new_active_searchers
285
 
            for revision in newly_seen:
286
 
                if revision in common_ancestors:
287
 
                    for searcher in searchers:
288
 
                        update_common(searcher, revision)
289
 
                    continue
290
 
                for searcher in searchers:
291
 
                    if revision not in searcher.seen:
292
 
                        break
293
 
                else:
294
 
                    border_ancestors.add(revision)
295
 
                    for searcher in searchers:
296
 
                        update_common(searcher, revision)
297
 
 
298
 
    def heads(self, keys):
299
 
        """Return the heads from amongst keys.
300
 
 
301
 
        This is done by searching the ancestries of each key.  Any key that is
302
 
        reachable from another key is not returned; all the others are.
303
 
 
304
 
        This operation scales with the relative depth between any two keys. If
305
 
        any two keys are completely disconnected all ancestry of both sides
306
 
        will be retrieved.
307
 
 
308
 
        :param keys: An iterable of keys.
309
 
        :return: A set of the heads. Note that as a set there is no ordering
310
 
            information. Callers will need to filter their input to create
311
 
            order if they need it.
312
 
        """
313
 
        candidate_heads = set(keys)
314
 
        if revision.NULL_REVISION in candidate_heads:
315
 
            # NULL_REVISION is only a head if it is the only entry
316
 
            candidate_heads.remove(revision.NULL_REVISION)
317
 
            if not candidate_heads:
318
 
                return set([revision.NULL_REVISION])
319
 
        if len(candidate_heads) < 2:
320
 
            return candidate_heads
321
 
        searchers = dict((c, self._make_breadth_first_searcher([c]))
322
 
                          for c in candidate_heads)
323
 
        active_searchers = dict(searchers)
324
 
        # skip over the actual candidate for each searcher
325
 
        for searcher in active_searchers.itervalues():
326
 
            searcher.next()
327
 
        # The common walker finds nodes that are common to two or more of the
328
 
        # input keys, so that we don't access all history when a currently
329
 
        # uncommon search point actually meets up with something behind a
330
 
        # common search point. Common search points do not keep searches
331
 
        # active; they just allow us to make searches inactive without
332
 
        # accessing all history.
333
 
        common_walker = self._make_breadth_first_searcher([])
334
 
        while len(active_searchers) > 0:
335
 
            ancestors = set()
336
 
            # advance searches
337
 
            try:
338
 
                common_walker.next()
339
 
            except StopIteration:
340
 
                # No common points being searched at this time.
341
 
                pass
342
 
            for candidate in active_searchers.keys():
343
 
                try:
344
 
                    searcher = active_searchers[candidate]
345
 
                except KeyError:
346
 
                    # rare case: we deleted candidate in a previous iteration
347
 
                    # through this for loop, because it was determined to be
348
 
                    # a descendant of another candidate.
349
 
                    continue
350
 
                try:
351
 
                    ancestors.update(searcher.next())
352
 
                except StopIteration:
353
 
                    del active_searchers[candidate]
354
 
                    continue
355
 
            # process found nodes
356
 
            new_common = set()
357
 
            for ancestor in ancestors:
358
 
                if ancestor in candidate_heads:
359
 
                    candidate_heads.remove(ancestor)
360
 
                    del searchers[ancestor]
361
 
                    if ancestor in active_searchers:
362
 
                        del active_searchers[ancestor]
363
 
                # it may meet up with a known common node
364
 
                if ancestor in common_walker.seen:
365
 
                    # some searcher has encountered our known common nodes:
366
 
                    # just stop it
367
 
                    ancestor_set = set([ancestor])
368
 
                    for searcher in searchers.itervalues():
369
 
                        searcher.stop_searching_any(ancestor_set)
370
 
                else:
371
 
                    # or it may have been just reached by all the searchers:
372
 
                    for searcher in searchers.itervalues():
373
 
                        if ancestor not in searcher.seen:
374
 
                            break
375
 
                    else:
376
 
                        # The final active searcher has just reached this node,
377
 
                        # making it be known as a descendant of all candidates,
378
 
                        # so we can stop searching it, and any seen ancestors
379
 
                        new_common.add(ancestor)
380
 
                        for searcher in searchers.itervalues():
381
 
                            seen_ancestors =\
382
 
                                searcher.find_seen_ancestors(ancestor)
383
 
                            searcher.stop_searching_any(seen_ancestors)
384
 
            common_walker.start_searching(new_common)
385
 
        return candidate_heads
386
 
 
387
 
    def find_unique_lca(self, left_revision, right_revision,
388
 
                        count_steps=False):
389
 
        """Find a unique LCA.
390
 
 
391
 
        Find lowest common ancestors.  If there is no unique  common
392
 
        ancestor, find the lowest common ancestors of those ancestors.
393
 
 
394
 
        Iteration stops when a unique lowest common ancestor is found.
395
 
        The graph origin is necessarily a unique lowest common ancestor.
396
 
 
397
 
        Note that None is not an acceptable substitute for NULL_REVISION.
398
 
        in the input for this method.
399
 
 
400
 
        :param count_steps: If True, the return value will be a tuple of
401
 
            (unique_lca, steps) where steps is the number of times that
402
 
            find_lca was run.  If False, only unique_lca is returned.
403
 
        """
404
 
        revisions = [left_revision, right_revision]
405
 
        steps = 0
406
 
        while True:
407
 
            steps += 1
408
 
            lca = self.find_lca(*revisions)
409
 
            if len(lca) == 1:
410
 
                result = lca.pop()
411
 
                if count_steps:
412
 
                    return result, steps
413
 
                else:
414
 
                    return result
415
 
            if len(lca) == 0:
416
 
                raise errors.NoCommonAncestor(left_revision, right_revision)
417
 
            revisions = lca
418
 
 
419
 
    def iter_topo_order(self, revisions):
420
 
        """Iterate through the input revisions in topological order.
421
 
 
422
 
        This sorting only ensures that parents come before their children.
423
 
        An ancestor may sort after a descendant if the relationship is not
424
 
        visible in the supplied list of revisions.
425
 
        """
426
 
        sorter = tsort.TopoSorter(self.get_parent_map(revisions))
427
 
        return sorter.iter_topo_order()
428
 
 
429
 
    def is_ancestor(self, candidate_ancestor, candidate_descendant):
430
 
        """Determine whether a revision is an ancestor of another.
431
 
 
432
 
        We answer this using heads() as heads() has the logic to perform the
433
 
        smallest number of parent lookups to determine the ancestral
434
 
        relationship between N revisions.
435
 
        """
436
 
        return set([candidate_descendant]) == self.heads(
437
 
            [candidate_ancestor, candidate_descendant])
438
 
 
439
 
 
440
 
class HeadsCache(object):
441
 
    """A cache of results for graph heads calls."""
442
 
 
443
 
    def __init__(self, graph):
444
 
        self.graph = graph
445
 
        self._heads = {}
446
 
 
447
 
    def heads(self, keys):
448
 
        """Return the heads of keys.
449
 
 
450
 
        This matches the API of Graph.heads(), specifically the return value is
451
 
        a set which can be mutated, and ordering of the input is not preserved
452
 
        in the output.
453
 
 
454
 
        :see also: Graph.heads.
455
 
        :param keys: The keys to calculate heads for.
456
 
        :return: A set containing the heads, which may be mutated without
457
 
            affecting future lookups.
458
 
        """
459
 
        keys = frozenset(keys)
460
 
        try:
461
 
            return set(self._heads[keys])
462
 
        except KeyError:
463
 
            heads = self.graph.heads(keys)
464
 
            self._heads[keys] = heads
465
 
            return set(heads)
466
 
 
467
 
 
468
 
class HeadsCache(object):
469
 
    """A cache of results for graph heads calls."""
470
 
 
471
 
    def __init__(self, graph):
472
 
        self.graph = graph
473
 
        self._heads = {}
474
 
 
475
 
    def heads(self, keys):
476
 
        """Return the heads of keys.
477
 
 
478
 
        :see also: Graph.heads.
479
 
        :param keys: The keys to calculate heads for.
480
 
        :return: A set containing the heads, which may be mutated without
481
 
            affecting future lookups.
482
 
        """
483
 
        keys = frozenset(keys)
484
 
        try:
485
 
            return set(self._heads[keys])
486
 
        except KeyError:
487
 
            heads = self.graph.heads(keys)
488
 
            self._heads[keys] = heads
489
 
            return set(heads)
490
 
 
491
 
 
492
 
class _BreadthFirstSearcher(object):
493
 
    """Parallel search breadth-first the ancestry of revisions.
494
 
 
495
 
    This class implements the iterator protocol, but additionally
496
 
    1. provides a set of seen ancestors, and
497
 
    2. allows some ancestries to be unsearched, via stop_searching_any
498
 
    """
499
 
 
500
 
    def __init__(self, revisions, parents_provider):
501
 
        self._start = set(revisions)
502
 
        self._search_revisions = None
503
 
        self.seen = set(revisions)
504
 
        self._parents_provider = parents_provider
505
 
 
506
 
    def __repr__(self):
507
 
        if self._search_revisions is not None:
508
 
            search = 'searching=%r' % (list(self._search_revisions),)
509
 
        else:
510
 
            search = 'starting=%r' % (list(self._start),)
511
 
        return ('_BreadthFirstSearcher(%s,'
512
 
                ' seen=%r)' % (search, list(self.seen)))
513
 
 
514
 
    def next(self):
515
 
        """Return the next ancestors of this revision.
516
 
 
517
 
        Ancestors are returned in the order they are seen in a breadth-first
518
 
        traversal.  No ancestor will be returned more than once.
519
 
        """
520
 
        if self._search_revisions is None:
521
 
            self._search_revisions = self._start
522
 
        else:
523
 
            new_search_revisions = set()
524
 
            parent_map = self._parents_provider.get_parent_map(
525
 
                            self._search_revisions)
526
 
            for parents in parent_map.itervalues():
527
 
                new_search_revisions.update(p for p in parents if
528
 
                                            p not in self.seen)
529
 
            self._search_revisions = new_search_revisions
530
 
        if len(self._search_revisions) == 0:
531
 
            raise StopIteration()
532
 
        self.seen.update(self._search_revisions)
533
 
        return self._search_revisions
534
 
 
535
 
    def __iter__(self):
536
 
        return self
537
 
 
538
 
    def find_seen_ancestors(self, revision):
539
 
        """Find ancestors of this revision that have already been seen."""
540
 
        searcher = _BreadthFirstSearcher([revision], self._parents_provider)
541
 
        seen_ancestors = set()
542
 
        for ancestors in searcher:
543
 
            for ancestor in ancestors:
544
 
                if ancestor not in self.seen:
545
 
                    searcher.stop_searching_any([ancestor])
546
 
                else:
547
 
                    seen_ancestors.add(ancestor)
548
 
        return seen_ancestors
549
 
 
550
 
    def stop_searching_any(self, revisions):
551
 
        """
552
 
        Remove any of the specified revisions from the search list.
553
 
 
554
 
        None of the specified revisions are required to be present in the
555
 
        search list.  In this case, the call is a no-op.
556
 
        """
557
 
        stopped = self._search_revisions.intersection(revisions)
558
 
        self._search_revisions = self._search_revisions.difference(revisions)
559
 
        return stopped
560
 
 
561
 
    def start_searching(self, revisions):
562
 
        if self._search_revisions is None:
563
 
            self._start = set(revisions)
564
 
        else:
565
 
            self._search_revisions.update(revisions.difference(self.seen))
566
 
        self.seen.update(revisions)