/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: Martin Pool
  • Date: 2008-02-06 00:41:04 UTC
  • mfrom: (3215 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3219.
  • Revision ID: mbp@sourcefrog.net-20080206004104-mxtn32habuhjq6b8
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
473
473
            return set(heads)
474
474
 
475
475
 
476
 
class HeadsCache(object):
477
 
    """A cache of results for graph heads calls."""
478
 
 
479
 
    def __init__(self, graph):
480
 
        self.graph = graph
481
 
        self._heads = {}
482
 
 
483
 
    def heads(self, keys):
484
 
        """Return the heads of keys.
485
 
 
486
 
        :see also: Graph.heads.
487
 
        :param keys: The keys to calculate heads for.
488
 
        :return: A set containing the heads, which may be mutated without
489
 
            affecting future lookups.
490
 
        """
491
 
        keys = frozenset(keys)
492
 
        try:
493
 
            return set(self._heads[keys])
494
 
        except KeyError:
495
 
            heads = self.graph.heads(keys)
496
 
            self._heads[keys] = heads
497
 
            return set(heads)
498
 
 
499
 
 
500
476
class _BreadthFirstSearcher(object):
501
477
    """Parallel search breadth-first the ancestry of revisions.
502
478
 
506
482
    """
507
483
 
508
484
    def __init__(self, revisions, parents_provider):
509
 
        self._start = set(revisions)
510
 
        self._search_revisions = None
511
 
        self.seen = set(revisions)
 
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()
512
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 = {}
513
495
 
514
496
    def __repr__(self):
515
 
        if self._search_revisions is not None:
516
 
            search = 'searching=%r' % (list(self._search_revisions),)
517
 
        else:
518
 
            search = 'starting=%r' % (list(self._start),)
519
 
        return ('_BreadthFirstSearcher(%s,'
520
 
                ' seen=%r)' % (search, list(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)
521
529
 
522
530
    def next(self):
523
531
        """Return the next ancestors of this revision.
524
532
 
525
533
        Ancestors are returned in the order they are seen in a breadth-first
526
 
        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.
527
542
        """
528
 
        if self._search_revisions is None:
529
 
            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
530
547
        else:
531
 
            new_search_revisions = set()
532
 
            parent_map = self._parents_provider.get_parent_map(
533
 
                            self._search_revisions)
534
 
            for parents in parent_map.itervalues():
535
 
                new_search_revisions.update(p for p in parents if
536
 
                                            p not in self.seen)
537
 
            self._search_revisions = new_search_revisions
538
 
        if len(self._search_revisions) == 0:
539
 
            raise StopIteration()
540
 
        self.seen.update(self._search_revisions)
541
 
        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
542
611
 
543
612
    def __iter__(self):
544
613
        return self
562
631
        None of the specified revisions are required to be present in the
563
632
        search list.  In this case, the call is a no-op.
564
633
        """
565
 
        stopped = self._search_revisions.intersection(revisions)
566
 
        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)
567
666
        return stopped
568
667
 
569
668
    def start_searching(self, revisions):
570
 
        if self._search_revisions is None:
571
 
            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)
572
683
        else:
573
 
            self._search_revisions.update(revisions.difference(self.seen))
574
 
        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