1592
1550
return revs, ghosts
1595
def invert_parent_map(parent_map):
1596
"""Given a map from child => parents, create a map of parent=>children"""
1598
for child, parents in parent_map.iteritems():
1600
# Any given parent is likely to have only a small handful
1601
# of children, many will have only one. So we avoid mem overhead of
1602
# a list, in exchange for extra copying of tuples
1603
if p not in child_map:
1604
child_map[p] = (child,)
1606
child_map[p] = child_map[p] + (child,)
1553
class AbstractSearchResult(object):
1554
"""The result of a search, describing a set of keys.
1556
Search results are typically used as the 'fetch_spec' parameter when
1559
:seealso: AbstractSearch
1562
def get_recipe(self):
1563
"""Return a recipe that can be used to replay this search.
1565
The recipe allows reconstruction of the same results at a later date.
1567
:return: A tuple of `(search_kind_str, *details)`. The details vary by
1568
kind of search result.
1570
raise NotImplementedError(self.get_recipe)
1572
def get_network_struct(self):
1573
"""Return a tuple that can be transmitted via the HPSS protocol."""
1574
raise NotImplementedError(self.get_network_struct)
1577
"""Return the keys found in this search.
1579
:return: A set of keys.
1581
raise NotImplementedError(self.get_keys)
1584
"""Return false if the search lists 1 or more revisions."""
1585
raise NotImplementedError(self.is_empty)
1587
def refine(self, seen, referenced):
1588
"""Create a new search by refining this search.
1590
:param seen: Revisions that have been satisfied.
1591
:param referenced: Revision references observed while satisfying some
1593
:return: A search result.
1595
raise NotImplementedError(self.refine)
1598
class AbstractSearch(object):
1599
"""A search that can be executed, producing a search result.
1601
:seealso: AbstractSearchResult
1605
"""Construct a network-ready search result from this search description.
1607
This may take some time to search repositories, etc.
1609
:return: A search result (an object that implements
1610
AbstractSearchResult's API).
1612
raise NotImplementedError(self.execute)
1615
class SearchResult(AbstractSearchResult):
1616
"""The result of a breadth first search.
1618
A SearchResult provides the ability to reconstruct the search or access a
1619
set of the keys the search found.
1622
def __init__(self, start_keys, exclude_keys, key_count, keys):
1623
"""Create a SearchResult.
1625
:param start_keys: The keys the search started at.
1626
:param exclude_keys: The keys the search excludes.
1627
:param key_count: The total number of keys (from start to but not
1629
:param keys: The keys the search found. Note that in future we may get
1630
a SearchResult from a smart server, in which case the keys list is
1631
not necessarily immediately available.
1633
self._recipe = ('search', start_keys, exclude_keys, key_count)
1634
self._keys = frozenset(keys)
1637
kind, start_keys, exclude_keys, key_count = self._recipe
1638
if len(start_keys) > 5:
1639
start_keys_repr = repr(list(start_keys)[:5])[:-1] + ', ...]'
1641
start_keys_repr = repr(start_keys)
1642
if len(exclude_keys) > 5:
1643
exclude_keys_repr = repr(list(exclude_keys)[:5])[:-1] + ', ...]'
1645
exclude_keys_repr = repr(exclude_keys)
1646
return '<%s %s:(%s, %s, %d)>' % (self.__class__.__name__,
1647
kind, start_keys_repr, exclude_keys_repr, key_count)
1649
def get_recipe(self):
1650
"""Return a recipe that can be used to replay this search.
1652
The recipe allows reconstruction of the same results at a later date
1653
without knowing all the found keys. The essential elements are a list
1654
of keys to start and to stop at. In order to give reproducible
1655
results when ghosts are encountered by a search they are automatically
1656
added to the exclude list (or else ghost filling may alter the
1659
:return: A tuple ('search', start_keys_set, exclude_keys_set,
1660
revision_count). To recreate the results of this search, create a
1661
breadth first searcher on the same graph starting at start_keys.
1662
Then call next() (or next_with_ghosts()) repeatedly, and on every
1663
result, call stop_searching_any on any keys from the exclude_keys
1664
set. The revision_count value acts as a trivial cross-check - the
1665
found revisions of the new search should have as many elements as
1666
revision_count. If it does not, then additional revisions have been
1667
ghosted since the search was executed the first time and the second
1672
def get_network_struct(self):
1673
start_keys = ' '.join(self._recipe[1])
1674
stop_keys = ' '.join(self._recipe[2])
1675
count = str(self._recipe[3])
1676
return (self._recipe[0], '\n'.join((start_keys, stop_keys, count)))
1679
"""Return the keys found in this search.
1681
:return: A set of keys.
1686
"""Return false if the search lists 1 or more revisions."""
1687
return self._recipe[3] == 0
1689
def refine(self, seen, referenced):
1690
"""Create a new search by refining this search.
1692
:param seen: Revisions that have been satisfied.
1693
:param referenced: Revision references observed while satisfying some
1696
start = self._recipe[1]
1697
exclude = self._recipe[2]
1698
count = self._recipe[3]
1699
keys = self.get_keys()
1700
# New heads = referenced + old heads - seen things - exclude
1701
pending_refs = set(referenced)
1702
pending_refs.update(start)
1703
pending_refs.difference_update(seen)
1704
pending_refs.difference_update(exclude)
1705
# New exclude = old exclude + satisfied heads
1706
seen_heads = start.intersection(seen)
1707
exclude.update(seen_heads)
1708
# keys gets seen removed
1710
# length is reduced by len(seen)
1712
return SearchResult(pending_refs, exclude, count, keys)
1715
class PendingAncestryResult(AbstractSearchResult):
1716
"""A search result that will reconstruct the ancestry for some graph heads.
1718
Unlike SearchResult, this doesn't hold the complete search result in
1719
memory, it just holds a description of how to generate it.
1722
def __init__(self, heads, repo):
1725
:param heads: an iterable of graph heads.
1726
:param repo: a repository to use to generate the ancestry for the given
1729
self.heads = frozenset(heads)
1733
if len(self.heads) > 5:
1734
heads_repr = repr(list(self.heads)[:5])[:-1]
1735
heads_repr += ', <%d more>...]' % (len(self.heads) - 5,)
1737
heads_repr = repr(self.heads)
1738
return '<%s heads:%s repo:%r>' % (
1739
self.__class__.__name__, heads_repr, self.repo)
1741
def get_recipe(self):
1742
"""Return a recipe that can be used to replay this search.
1744
The recipe allows reconstruction of the same results at a later date.
1746
:seealso SearchResult.get_recipe:
1748
:return: A tuple ('proxy-search', start_keys_set, set(), -1)
1749
To recreate this result, create a PendingAncestryResult with the
1752
return ('proxy-search', self.heads, set(), -1)
1754
def get_network_struct(self):
1755
parts = ['ancestry-of']
1756
parts.extend(self.heads)
1760
"""See SearchResult.get_keys.
1762
Returns all the keys for the ancestry of the heads, excluding
1765
return self._get_keys(self.repo.get_graph())
1767
def _get_keys(self, graph):
1768
NULL_REVISION = revision.NULL_REVISION
1769
keys = [key for (key, parents) in graph.iter_ancestry(self.heads)
1770
if key != NULL_REVISION and parents is not None]
1774
"""Return false if the search lists 1 or more revisions."""
1775
if revision.NULL_REVISION in self.heads:
1776
return len(self.heads) == 1
1778
return len(self.heads) == 0
1780
def refine(self, seen, referenced):
1781
"""Create a new search by refining this search.
1783
:param seen: Revisions that have been satisfied.
1784
:param referenced: Revision references observed while satisfying some
1787
referenced = self.heads.union(referenced)
1788
return PendingAncestryResult(referenced - seen, self.repo)
1791
class EmptySearchResult(AbstractSearchResult):
1792
"""An empty search result."""
1798
class EverythingResult(AbstractSearchResult):
1799
"""A search result that simply requests everything in the repository."""
1801
def __init__(self, repo):
1805
return '%s(%r)' % (self.__class__.__name__, self._repo)
1807
def get_recipe(self):
1808
raise NotImplementedError(self.get_recipe)
1810
def get_network_struct(self):
1811
return ('everything',)
1814
if 'evil' in debug.debug_flags:
1815
from bzrlib import remote
1816
if isinstance(self._repo, remote.RemoteRepository):
1817
# warn developers (not users) not to do this
1818
trace.mutter_callsite(
1819
2, "EverythingResult(RemoteRepository).get_keys() is slow.")
1820
return self._repo.all_revision_ids()
1823
# It's ok for this to wrongly return False: the worst that can happen
1824
# is that RemoteStreamSource will initiate a get_stream on an empty
1825
# repository. And almost all repositories are non-empty.
1828
def refine(self, seen, referenced):
1829
heads = set(self._repo.all_revision_ids())
1830
heads.difference_update(seen)
1831
heads.update(referenced)
1832
return PendingAncestryResult(heads, self._repo)
1835
class EverythingNotInOther(AbstractSearch):
1836
"""Find all revisions in that are in one repo but not the other."""
1838
def __init__(self, to_repo, from_repo, find_ghosts=False):
1839
self.to_repo = to_repo
1840
self.from_repo = from_repo
1841
self.find_ghosts = find_ghosts
1844
return self.to_repo.search_missing_revision_ids(
1845
self.from_repo, find_ghosts=self.find_ghosts)
1848
class NotInOtherForRevs(AbstractSearch):
1849
"""Find all revisions missing in one repo for a some specific heads."""
1851
def __init__(self, to_repo, from_repo, required_ids, if_present_ids=None,
1852
find_ghosts=False, limit=None):
1855
:param required_ids: revision IDs of heads that must be found, or else
1856
the search will fail with NoSuchRevision. All revisions in their
1857
ancestry not already in the other repository will be included in
1859
:param if_present_ids: revision IDs of heads that may be absent in the
1860
source repository. If present, then their ancestry not already
1861
found in other will be included in the search result.
1862
:param limit: maximum number of revisions to fetch
1864
self.to_repo = to_repo
1865
self.from_repo = from_repo
1866
self.find_ghosts = find_ghosts
1867
self.required_ids = required_ids
1868
self.if_present_ids = if_present_ids
1872
if len(self.required_ids) > 5:
1873
reqd_revs_repr = repr(list(self.required_ids)[:5])[:-1] + ', ...]'
1875
reqd_revs_repr = repr(self.required_ids)
1876
if self.if_present_ids and len(self.if_present_ids) > 5:
1877
ifp_revs_repr = repr(list(self.if_present_ids)[:5])[:-1] + ', ...]'
1879
ifp_revs_repr = repr(self.if_present_ids)
1881
return ("<%s from:%r to:%r find_ghosts:%r req'd:%r if-present:%r"
1883
self.__class__.__name__, self.from_repo, self.to_repo,
1884
self.find_ghosts, reqd_revs_repr, ifp_revs_repr,
1888
return self.to_repo.search_missing_revision_ids(
1889
self.from_repo, revision_ids=self.required_ids,
1890
if_present_ids=self.if_present_ids, find_ghosts=self.find_ghosts,
1610
1894
def collapse_linear_regions(parent_map):