/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

  • Committer: Robert Collins
  • Date: 2008-02-13 03:30:01 UTC
  • mfrom: (3221 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3224.
  • Revision ID: robertc@robertcollins.net-20080213033001-rw70ul0zb02ph856
Merge to fix conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
from bzrlib import (
18
18
    errors,
 
19
    revision,
 
20
    symbol_versioning,
19
21
    tsort,
20
22
    )
21
23
from bzrlib.deprecated_graph import (node_distances, select_farthest)
22
 
from bzrlib.revision import NULL_REVISION
23
24
 
24
25
# DIAGRAM of terminology
25
26
#       A
44
45
# 2. Since len(['D', 'E']) > 1, find_lca('D', 'E') => ['A']
45
46
 
46
47
 
 
48
class DictParentsProvider(object):
 
49
    """A parents provider for Graph objects."""
 
50
 
 
51
    def __init__(self, ancestry):
 
52
        self.ancestry = ancestry
 
53
 
 
54
    def __repr__(self):
 
55
        return 'DictParentsProvider(%r)' % self.ancestry
 
56
 
 
57
    def get_parent_map(self, keys):
 
58
        """See _StackedParentsProvider.get_parent_map"""
 
59
        ancestry = self.ancestry
 
60
        return dict((k, ancestry[k]) for k in keys if k in ancestry)
 
61
 
47
62
 
48
63
class _StackedParentsProvider(object):
49
64
 
53
68
    def __repr__(self):
54
69
        return "_StackedParentsProvider(%r)" % self._parent_providers
55
70
 
56
 
    def get_parents(self, revision_ids):
57
 
        """Find revision ids of the parents of a list of revisions
 
71
    def get_parent_map(self, keys):
 
72
        """Get a mapping of keys => parents
58
73
 
59
 
        A list is returned of the same length as the input.  Each entry
60
 
        is a list of parent ids for the corresponding input revision.
 
74
        A dictionary is returned with an entry for each key present in this
 
75
        source. If this source doesn't have information about a key, it should
 
76
        not include an entry.
61
77
 
62
78
        [NULL_REVISION] is used as the parent of the first user-committed
63
79
        revision.  Its parent list is empty.
64
80
 
65
 
        If the revision is not present (i.e. a ghost), None is used in place
66
 
        of the list of parents.
 
81
        :param keys: An iterable returning keys to check (eg revision_ids)
 
82
        :return: A dictionary mapping each key to its parents
67
83
        """
68
84
        found = {}
 
85
        remaining = set(keys)
69
86
        for parents_provider in self._parent_providers:
70
 
            pending_revisions = [r for r in revision_ids if r not in found]
71
 
            parent_list = parents_provider.get_parents(pending_revisions)
72
 
            new_found = dict((k, v) for k, v in zip(pending_revisions,
73
 
                             parent_list) if v is not None)
 
87
            new_found = parents_provider.get_parent_map(remaining)
74
88
            found.update(new_found)
75
 
            if len(found) == len(revision_ids):
 
89
            remaining.difference_update(new_found)
 
90
            if not remaining:
76
91
                break
77
 
        return [found.get(r, None) for r in revision_ids]
 
92
        return found
 
93
 
 
94
 
 
95
class CachingParentsProvider(object):
 
96
    """A parents provider which will cache the revision => parents in a dict.
 
97
 
 
98
    This is useful for providers that have an expensive lookup.
 
99
    """
 
100
 
 
101
    def __init__(self, parent_provider):
 
102
        self._real_provider = parent_provider
 
103
        # Theoretically we could use an LRUCache here
 
104
        self._cache = {}
 
105
 
 
106
    def __repr__(self):
 
107
        return "%s(%r)" % (self.__class__.__name__, self._real_provider)
 
108
 
 
109
    def get_parent_map(self, keys):
 
110
        """See _StackedParentsProvider.get_parent_map"""
 
111
        needed = set()
 
112
        # If the _real_provider doesn't have a key, we cache a value of None,
 
113
        # which we then later use to realize we cannot provide a value for that
 
114
        # key.
 
115
        parent_map = {}
 
116
        cache = self._cache
 
117
        for key in keys:
 
118
            if key in cache:
 
119
                value = cache[key]
 
120
                if value is not None:
 
121
                    parent_map[key] = value
 
122
            else:
 
123
                needed.add(key)
 
124
 
 
125
        if needed:
 
126
            new_parents = self._real_provider.get_parent_map(needed)
 
127
            cache.update(new_parents)
 
128
            parent_map.update(new_parents)
 
129
            needed.difference_update(new_parents)
 
130
            cache.update(dict.fromkeys(needed, None))
 
131
        return parent_map
78
132
 
79
133
 
80
134
class Graph(object):
89
143
 
90
144
        This should not normally be invoked directly, because there may be
91
145
        specialized implementations for particular repository types.  See
92
 
        Repository.get_graph()
 
146
        Repository.get_graph().
93
147
 
94
 
        :param parents_provider: An object providing a get_parents call
95
 
            conforming to the behavior of StackedParentsProvider.get_parents
 
148
        :param parents_provider: An object providing a get_parent_map call
 
149
            conforming to the behavior of
 
150
            StackedParentsProvider.get_parent_map.
96
151
        """
97
 
        self.get_parents = parents_provider.get_parents
 
152
        if getattr(parents_provider, 'get_parents', None) is not None:
 
153
            self.get_parents = parents_provider.get_parents
 
154
        if getattr(parents_provider, 'get_parent_map', None) is not None:
 
155
            self.get_parent_map = parents_provider.get_parent_map
98
156
        self._parents_provider = parents_provider
99
157
 
100
158
    def __repr__(self):
146
204
        return (left.difference(right).difference(common),
147
205
                right.difference(left).difference(common))
148
206
 
 
207
    @symbol_versioning.deprecated_method(symbol_versioning.one_one)
 
208
    def get_parents(self, revisions):
 
209
        """Find revision ids of the parents of a list of revisions
 
210
 
 
211
        A list is returned of the same length as the input.  Each entry
 
212
        is a list of parent ids for the corresponding input revision.
 
213
 
 
214
        [NULL_REVISION] is used as the parent of the first user-committed
 
215
        revision.  Its parent list is empty.
 
216
 
 
217
        If the revision is not present (i.e. a ghost), None is used in place
 
218
        of the list of parents.
 
219
 
 
220
        Deprecated in bzr 1.2 - please see get_parent_map.
 
221
        """
 
222
        parents = self.get_parent_map(revisions)
 
223
        return [parent.get(r, None) for r in revisions]
 
224
 
 
225
    def get_parent_map(self, revisions):
 
226
        """Get a map of key:parent_list for revisions.
 
227
 
 
228
        This implementation delegates to get_parents, for old parent_providers
 
229
        that do not supply get_parent_map.
 
230
        """
 
231
        result = {}
 
232
        for rev, parents in self.get_parents(revisions):
 
233
            if parents is not None:
 
234
                result[rev] = parents
 
235
        return result
 
236
 
149
237
    def _make_breadth_first_searcher(self, revisions):
150
238
        return _BreadthFirstSearcher(revisions, self)
151
239
 
231
319
            order if they need it.
232
320
        """
233
321
        candidate_heads = set(keys)
 
322
        if revision.NULL_REVISION in candidate_heads:
 
323
            # NULL_REVISION is only a head if it is the only entry
 
324
            candidate_heads.remove(revision.NULL_REVISION)
 
325
            if not candidate_heads:
 
326
                return set([revision.NULL_REVISION])
234
327
        if len(candidate_heads) < 2:
235
328
            return candidate_heads
236
329
        searchers = dict((c, self._make_breadth_first_searcher([c]))
299
392
            common_walker.start_searching(new_common)
300
393
        return candidate_heads
301
394
 
302
 
    def find_unique_lca(self, left_revision, right_revision):
 
395
    def find_unique_lca(self, left_revision, right_revision,
 
396
                        count_steps=False):
303
397
        """Find a unique LCA.
304
398
 
305
399
        Find lowest common ancestors.  If there is no unique  common
310
404
 
311
405
        Note that None is not an acceptable substitute for NULL_REVISION.
312
406
        in the input for this method.
 
407
 
 
408
        :param count_steps: If True, the return value will be a tuple of
 
409
            (unique_lca, steps) where steps is the number of times that
 
410
            find_lca was run.  If False, only unique_lca is returned.
313
411
        """
314
412
        revisions = [left_revision, right_revision]
 
413
        steps = 0
315
414
        while True:
 
415
            steps += 1
316
416
            lca = self.find_lca(*revisions)
317
417
            if len(lca) == 1:
318
 
                return lca.pop()
 
418
                result = lca.pop()
 
419
                if count_steps:
 
420
                    return result, steps
 
421
                else:
 
422
                    return result
319
423
            if len(lca) == 0:
320
424
                raise errors.NoCommonAncestor(left_revision, right_revision)
321
425
            revisions = lca
327
431
        An ancestor may sort after a descendant if the relationship is not
328
432
        visible in the supplied list of revisions.
329
433
        """
330
 
        sorter = tsort.TopoSorter(zip(revisions, self.get_parents(revisions)))
 
434
        sorter = tsort.TopoSorter(self.get_parent_map(revisions))
331
435
        return sorter.iter_topo_order()
332
436
 
333
437
    def is_ancestor(self, candidate_ancestor, candidate_descendant):
334
438
        """Determine whether a revision is an ancestor of another.
335
439
 
336
440
        We answer this using heads() as heads() has the logic to perform the
337
 
        smallest number of parent looksup to determine the ancestral
 
441
        smallest number of parent lookups to determine the ancestral
338
442
        relationship between N revisions.
339
443
        """
340
444
        return set([candidate_descendant]) == self.heads(
369
473
            return set(heads)
370
474
 
371
475
 
372
 
class HeadsCache(object):
373
 
    """A cache of results for graph heads calls."""
374
 
 
375
 
    def __init__(self, graph):
376
 
        self.graph = graph
377
 
        self._heads = {}
378
 
 
379
 
    def heads(self, keys):
380
 
        """Return the heads of keys.
381
 
 
382
 
        :see also: Graph.heads.
383
 
        :param keys: The keys to calculate heads for.
384
 
        :return: A set containing the heads, which may be mutated without
385
 
            affecting future lookups.
386
 
        """
387
 
        keys = frozenset(keys)
388
 
        try:
389
 
            return set(self._heads[keys])
390
 
        except KeyError:
391
 
            heads = self.graph.heads(keys)
392
 
            self._heads[keys] = heads
393
 
            return set(heads)
394
 
 
395
 
 
396
476
class _BreadthFirstSearcher(object):
397
477
    """Parallel search breadth-first the ancestry of revisions.
398
478
 
402
482
    """
403
483
 
404
484
    def __init__(self, revisions, parents_provider):
405
 
        self._start = set(revisions)
406
 
        self._search_revisions = None
407
 
        self.seen = set(revisions)
408
 
        self._parents_provider = parents_provider 
 
485
        self._iterations = 0
 
486
        self._next_query = set(revisions)
 
487
        self.seen = set()
 
488
        self._started_keys = set(self._next_query)
 
489
        self._stopped_keys = set()
 
490
        self._parents_provider = parents_provider
 
491
        self._returning = 'next_with_ghosts'
 
492
        self._current_present = set()
 
493
        self._current_ghosts = set()
 
494
        self._current_parents = {}
409
495
 
410
496
    def __repr__(self):
411
 
        return ('_BreadthFirstSearcher(self._search_revisions=%r,'
412
 
                ' self.seen=%r)' % (self._search_revisions, self.seen))
 
497
        if self._iterations:
 
498
            prefix = "searching"
 
499
        else:
 
500
            prefix = "starting"
 
501
        search = '%s=%r' % (prefix, list(self._next_query))
 
502
        return ('_BreadthFirstSearcher(iterations=%d, %s,'
 
503
                ' seen=%r)' % (self._iterations, search, list(self.seen)))
 
504
 
 
505
    def get_result(self):
 
506
        """Get a SearchResult for the current state of this searcher.
 
507
        
 
508
        :return: A SearchResult for this search so far. The SearchResult is
 
509
            static - the search can be advanced and the search result will not
 
510
            be invalidated or altered.
 
511
        """
 
512
        if self._returning == 'next':
 
513
            # We have to know the current nodes children to be able to list the
 
514
            # exclude keys for them. However, while we could have a second
 
515
            # look-ahead result buffer and shuffle things around, this method
 
516
            # is typically only called once per search - when memoising the
 
517
            # results of the search. 
 
518
            found, ghosts, next, parents = self._do_query(self._next_query)
 
519
            # pretend we didn't query: perhaps we should tweak _do_query to be
 
520
            # entirely stateless?
 
521
            self.seen.difference_update(next)
 
522
            next_query = next.union(ghosts)
 
523
        else:
 
524
            next_query = self._next_query
 
525
        excludes = self._stopped_keys.union(next_query)
 
526
        included_keys = self.seen.difference(excludes)
 
527
        return SearchResult(self._started_keys, excludes, len(included_keys),
 
528
            included_keys)
413
529
 
414
530
    def next(self):
415
531
        """Return the next ancestors of this revision.
416
532
 
417
533
        Ancestors are returned in the order they are seen in a breadth-first
418
 
        traversal.  No ancestor will be returned more than once.
 
534
        traversal.  No ancestor will be returned more than once. Ancestors are
 
535
        returned before their parentage is queried, so ghosts and missing
 
536
        revisions (including the start revisions) are included in the result.
 
537
        This can save a round trip in LCA style calculation by allowing
 
538
        convergence to be detected without reading the data for the revision
 
539
        the convergence occurs on.
 
540
 
 
541
        :return: A set of revision_ids.
419
542
        """
420
 
        if self._search_revisions is None:
421
 
            self._search_revisions = self._start
 
543
        if self._returning != 'next':
 
544
            # switch to returning the query, not the results.
 
545
            self._returning = 'next'
 
546
            self._iterations += 1
422
547
        else:
423
 
            new_search_revisions = set()
424
 
            for parents in self._parents_provider.get_parents(
425
 
                self._search_revisions):
426
 
                if parents is None:
427
 
                    continue
428
 
                new_search_revisions.update(p for p in parents if
429
 
                                            p not in self.seen)
430
 
            self._search_revisions = new_search_revisions
431
 
        if len(self._search_revisions) == 0:
432
 
            raise StopIteration()
433
 
        self.seen.update(self._search_revisions)
434
 
        return self._search_revisions
 
548
            self._advance()
 
549
        if len(self._next_query) == 0:
 
550
            raise StopIteration()
 
551
        # We have seen what we're querying at this point as we are returning
 
552
        # the query, not the results.
 
553
        self.seen.update(self._next_query)
 
554
        return self._next_query
 
555
 
 
556
    def next_with_ghosts(self):
 
557
        """Return the next found ancestors, with ghosts split out.
 
558
        
 
559
        Ancestors are returned in the order they are seen in a breadth-first
 
560
        traversal.  No ancestor will be returned more than once. Ancestors are
 
561
        returned only after asking for their parents, which allows us to detect
 
562
        which revisions are ghosts and which are not.
 
563
 
 
564
        :return: A tuple with (present ancestors, ghost ancestors) sets.
 
565
        """
 
566
        if self._returning != 'next_with_ghosts':
 
567
            # switch to returning the results, not the current query.
 
568
            self._returning = 'next_with_ghosts'
 
569
            self._advance()
 
570
        if len(self._next_query) == 0:
 
571
            raise StopIteration()
 
572
        self._advance()
 
573
        return self._current_present, self._current_ghosts
 
574
 
 
575
    def _advance(self):
 
576
        """Advance the search.
 
577
 
 
578
        Updates self.seen, self._next_query, self._current_present,
 
579
        self._current_ghosts, self._current_parents and self._iterations.
 
580
        """
 
581
        self._iterations += 1
 
582
        found, ghosts, next, parents = self._do_query(self._next_query)
 
583
        self._current_present = found
 
584
        self._current_ghosts = ghosts
 
585
        self._next_query = next
 
586
        self._current_parents = parents
 
587
        # ghosts are implicit stop points, otherwise the search cannot be
 
588
        # repeated when ghosts are filled.
 
589
        self._stopped_keys.update(ghosts)
 
590
 
 
591
    def _do_query(self, revisions):
 
592
        """Query for revisions.
 
593
 
 
594
        Adds revisions to the seen set.
 
595
 
 
596
        :param revisions: Revisions to query.
 
597
        :return: A tuple: (set(found_revisions), set(ghost_revisions),
 
598
           set(parents_of_found_revisions), dict(found_revisions:parents)).
 
599
        """
 
600
        found_parents = set()
 
601
        parents_of_found = set()
 
602
        # revisions may contain nodes that point to other nodes in revisions:
 
603
        # we want to filter them out.
 
604
        self.seen.update(revisions)
 
605
        parent_map = self._parents_provider.get_parent_map(revisions)
 
606
        for rev_id, parents in parent_map.iteritems():
 
607
            found_parents.add(rev_id)
 
608
            parents_of_found.update(p for p in parents if p not in self.seen)
 
609
        ghost_parents = revisions - found_parents
 
610
        return found_parents, ghost_parents, parents_of_found, parent_map
435
611
 
436
612
    def __iter__(self):
437
613
        return self
455
631
        None of the specified revisions are required to be present in the
456
632
        search list.  In this case, the call is a no-op.
457
633
        """
458
 
        stopped = self._search_revisions.intersection(revisions)
459
 
        self._search_revisions = self._search_revisions.difference(revisions)
 
634
        revisions = frozenset(revisions)
 
635
        if self._returning == 'next':
 
636
            stopped = self._next_query.intersection(revisions)
 
637
            self._next_query = self._next_query.difference(revisions)
 
638
        else:
 
639
            stopped_present = self._current_present.intersection(revisions)
 
640
            stopped = stopped_present.union(
 
641
                self._current_ghosts.intersection(revisions))
 
642
            self._current_present.difference_update(stopped)
 
643
            self._current_ghosts.difference_update(stopped)
 
644
            # stopping 'x' should stop returning parents of 'x', but 
 
645
            # not if 'y' always references those same parents
 
646
            stop_rev_references = {}
 
647
            for rev in stopped_present:
 
648
                for parent_id in self._current_parents[rev]:
 
649
                    if parent_id not in stop_rev_references:
 
650
                        stop_rev_references[parent_id] = 0
 
651
                    stop_rev_references[parent_id] += 1
 
652
            # if only the stopped revisions reference it, the ref count will be
 
653
            # 0 after this loop
 
654
            for parents in self._current_parents.itervalues():
 
655
                for parent_id in parents:
 
656
                    try:
 
657
                        stop_rev_references[parent_id] -= 1
 
658
                    except KeyError:
 
659
                        pass
 
660
            stop_parents = set()
 
661
            for rev_id, refs in stop_rev_references.iteritems():
 
662
                if refs == 0:
 
663
                    stop_parents.add(rev_id)
 
664
            self._next_query.difference_update(stop_parents)
 
665
        self._stopped_keys.update(stopped)
460
666
        return stopped
461
667
 
462
668
    def start_searching(self, revisions):
463
 
        if self._search_revisions is None:
464
 
            self._start = set(revisions)
 
669
        """Add revisions to the search.
 
670
 
 
671
        The parents of revisions will be returned from the next call to next()
 
672
        or next_with_ghosts(). If next_with_ghosts was the most recently used
 
673
        next* call then the return value is the result of looking up the
 
674
        ghost/not ghost status of revisions. (A tuple (present, ghosted)).
 
675
        """
 
676
        revisions = frozenset(revisions)
 
677
        self._started_keys.update(revisions)
 
678
        new_revisions = revisions.difference(self.seen)
 
679
        revs, ghosts, query, parents = self._do_query(revisions)
 
680
        self._stopped_keys.update(ghosts)
 
681
        if self._returning == 'next':
 
682
            self._next_query.update(new_revisions)
465
683
        else:
466
 
            self._search_revisions.update(revisions.difference(self.seen))
467
 
        self.seen.update(revisions)
 
684
            # perform a query on revisions
 
685
            self._current_present.update(revs)
 
686
            self._current_ghosts.update(ghosts)
 
687
            self._next_query.update(query)
 
688
            self._current_parents.update(parents)
 
689
            return revs, ghosts
 
690
 
 
691
 
 
692
class SearchResult(object):
 
693
    """The result of a breadth first search.
 
694
 
 
695
    A SearchResult provides the ability to reconstruct the search or access a
 
696
    set of the keys the search found.
 
697
    """
 
698
 
 
699
    def __init__(self, start_keys, exclude_keys, key_count, keys):
 
700
        """Create a SearchResult.
 
701
 
 
702
        :param start_keys: The keys the search started at.
 
703
        :param exclude_keys: The keys the search excludes.
 
704
        :param key_count: The total number of keys (from start to but not
 
705
            including exclude).
 
706
        :param keys: The keys the search found. Note that in future we may get
 
707
            a SearchResult from a smart server, in which case the keys list is
 
708
            not necessarily immediately available.
 
709
        """
 
710
        self._recipe = (start_keys, exclude_keys, key_count)
 
711
        self._keys = frozenset(keys)
 
712
 
 
713
    def get_recipe(self):
 
714
        """Return a recipe that can be used to replay this search.
 
715
        
 
716
        The recipe allows reconstruction of the same results at a later date
 
717
        without knowing all the found keys. The essential elements are a list
 
718
        of keys to start and and to stop at. In order to give reproducible
 
719
        results when ghosts are encountered by a search they are automatically
 
720
        added to the exclude list (or else ghost filling may alter the
 
721
        results).
 
722
 
 
723
        :return: A tuple (start_keys_set, exclude_keys_set, revision_count). To
 
724
            recreate the results of this search, create a breadth first
 
725
            searcher on the same graph starting at start_keys. Then call next()
 
726
            (or next_with_ghosts()) repeatedly, and on every result, call
 
727
            stop_searching_any on any keys from the exclude_keys set. The
 
728
            revision_count value acts as a trivial cross-check - the found
 
729
            revisions of the new search should have as many elements as
 
730
            revision_count. If it does not, then additional revisions have been
 
731
            ghosted since the search was executed the first time and the second
 
732
            time.
 
733
        """
 
734
        return self._recipe
 
735
 
 
736
    def get_keys(self):
 
737
        """Return the keys found in this search.
 
738
 
 
739
        :return: A set of keys.
 
740
        """
 
741
        return self._keys
 
742