59
60
def __repr__(self):
60
61
return 'DictParentsProvider(%r)' % self.ancestry
63
# Note: DictParentsProvider does not implement get_cached_parent_map
64
# Arguably, the data is clearly cached in memory. However, this class
65
# is mostly used for testing, and it keeps the tests clean to not
62
68
def get_parent_map(self, keys):
63
69
"""See StackedParentsProvider.get_parent_map"""
64
70
ancestry = self.ancestry
65
return dict((k, ancestry[k]) for k in keys if k in ancestry)
71
return dict([(k, ancestry[k]) for k in keys if k in ancestry])
67
@deprecated_function(deprecated_in((1, 16, 0)))
68
def _StackedParentsProvider(*args, **kwargs):
69
return StackedParentsProvider(*args, **kwargs)
71
74
class StackedParentsProvider(object):
72
75
"""A parents provider which stacks (or unions) multiple providers.
74
77
The providers are queries in the order of the provided parent_providers.
77
80
def __init__(self, parent_providers):
78
81
self._parent_providers = parent_providers
258
306
right = searchers[1].seen
259
307
return (left.difference(right), right.difference(left))
309
def find_descendants(self, old_key, new_key):
310
"""Find descendants of old_key that are ancestors of new_key."""
311
child_map = self.get_child_map(self._find_descendant_ancestors(
313
graph = Graph(DictParentsProvider(child_map))
314
searcher = graph._make_breadth_first_searcher([old_key])
318
def _find_descendant_ancestors(self, old_key, new_key):
319
"""Find ancestors of new_key that may be descendants of old_key."""
320
stop = self._make_breadth_first_searcher([old_key])
321
descendants = self._make_breadth_first_searcher([new_key])
322
for revisions in descendants:
323
old_stop = stop.seen.intersection(revisions)
324
descendants.stop_searching_any(old_stop)
325
seen_stop = descendants.find_seen_ancestors(stop.step())
326
descendants.stop_searching_any(seen_stop)
327
return descendants.seen.difference(stop.seen)
329
def get_child_map(self, keys):
330
"""Get a mapping from parents to children of the specified keys.
332
This is simply the inversion of get_parent_map. Only supplied keys
333
will be discovered as children.
334
:return: a dict of key:child_list for keys.
336
parent_map = self._parents_provider.get_parent_map(keys)
338
for child, parents in sorted(parent_map.items()):
339
for parent in parents:
340
parent_child.setdefault(parent, []).append(child)
261
343
def find_distance_to_null(self, target_revision_id, known_revision_ids):
262
344
"""Find the left-hand distance to the NULL_REVISION.
1441
1594
return revs, ghosts
1444
class SearchResult(object):
1445
"""The result of a breadth first search.
1447
A SearchResult provides the ability to reconstruct the search or access a
1448
set of the keys the search found.
1451
def __init__(self, start_keys, exclude_keys, key_count, keys):
1452
"""Create a SearchResult.
1454
:param start_keys: The keys the search started at.
1455
:param exclude_keys: The keys the search excludes.
1456
:param key_count: The total number of keys (from start to but not
1458
:param keys: The keys the search found. Note that in future we may get
1459
a SearchResult from a smart server, in which case the keys list is
1460
not necessarily immediately available.
1462
self._recipe = ('search', start_keys, exclude_keys, key_count)
1463
self._keys = frozenset(keys)
1465
def get_recipe(self):
1466
"""Return a recipe that can be used to replay this search.
1468
The recipe allows reconstruction of the same results at a later date
1469
without knowing all the found keys. The essential elements are a list
1470
of keys to start and to stop at. In order to give reproducible
1471
results when ghosts are encountered by a search they are automatically
1472
added to the exclude list (or else ghost filling may alter the
1475
:return: A tuple ('search', start_keys_set, exclude_keys_set,
1476
revision_count). To recreate the results of this search, create a
1477
breadth first searcher on the same graph starting at start_keys.
1478
Then call next() (or next_with_ghosts()) repeatedly, and on every
1479
result, call stop_searching_any on any keys from the exclude_keys
1480
set. The revision_count value acts as a trivial cross-check - the
1481
found revisions of the new search should have as many elements as
1482
revision_count. If it does not, then additional revisions have been
1483
ghosted since the search was executed the first time and the second
1489
"""Return the keys found in this search.
1491
:return: A set of keys.
1496
"""Return false if the search lists 1 or more revisions."""
1497
return self._recipe[3] == 0
1499
def refine(self, seen, referenced):
1500
"""Create a new search by refining this search.
1502
:param seen: Revisions that have been satisfied.
1503
:param referenced: Revision references observed while satisfying some
1506
start = self._recipe[1]
1507
exclude = self._recipe[2]
1508
count = self._recipe[3]
1509
keys = self.get_keys()
1510
# New heads = referenced + old heads - seen things - exclude
1511
pending_refs = set(referenced)
1512
pending_refs.update(start)
1513
pending_refs.difference_update(seen)
1514
pending_refs.difference_update(exclude)
1515
# New exclude = old exclude + satisfied heads
1516
seen_heads = start.intersection(seen)
1517
exclude.update(seen_heads)
1518
# keys gets seen removed
1520
# length is reduced by len(seen)
1522
return SearchResult(pending_refs, exclude, count, keys)
1525
class PendingAncestryResult(object):
1526
"""A search result that will reconstruct the ancestry for some graph heads.
1528
Unlike SearchResult, this doesn't hold the complete search result in
1529
memory, it just holds a description of how to generate it.
1532
def __init__(self, heads, repo):
1535
:param heads: an iterable of graph heads.
1536
:param repo: a repository to use to generate the ancestry for the given
1539
self.heads = frozenset(heads)
1542
def get_recipe(self):
1543
"""Return a recipe that can be used to replay this search.
1545
The recipe allows reconstruction of the same results at a later date.
1547
:seealso SearchResult.get_recipe:
1549
:return: A tuple ('proxy-search', start_keys_set, set(), -1)
1550
To recreate this result, create a PendingAncestryResult with the
1553
return ('proxy-search', self.heads, set(), -1)
1556
"""See SearchResult.get_keys.
1558
Returns all the keys for the ancestry of the heads, excluding
1561
return self._get_keys(self.repo.get_graph())
1563
def _get_keys(self, graph):
1564
NULL_REVISION = revision.NULL_REVISION
1565
keys = [key for (key, parents) in graph.iter_ancestry(self.heads)
1566
if key != NULL_REVISION and parents is not None]
1570
"""Return false if the search lists 1 or more revisions."""
1571
if revision.NULL_REVISION in self.heads:
1572
return len(self.heads) == 1
1574
return len(self.heads) == 0
1576
def refine(self, seen, referenced):
1577
"""Create a new search by refining this search.
1579
:param seen: Revisions that have been satisfied.
1580
:param referenced: Revision references observed while satisfying some
1583
referenced = self.heads.union(referenced)
1584
return PendingAncestryResult(referenced - seen, self.repo)
1597
def invert_parent_map(parent_map):
1598
"""Given a map from child => parents, create a map of parent=>children"""
1600
for child, parents in parent_map.iteritems():
1602
# Any given parent is likely to have only a small handful
1603
# of children, many will have only one. So we avoid mem overhead of
1604
# a list, in exchange for extra copying of tuples
1605
if p not in child_map:
1606
child_map[p] = (child,)
1608
child_map[p] = child_map[p] + (child,)
1587
1612
def collapse_linear_regions(parent_map):
1685
class GraphThunkIdsToKeys(object):
1686
"""Forwards calls about 'ids' to be about keys internally."""
1688
def __init__(self, graph):
1691
def topo_sort(self):
1692
return [r for (r,) in self._graph.topo_sort()]
1694
def heads(self, ids):
1695
"""See Graph.heads()"""
1696
as_keys = [(i,) for i in ids]
1697
head_keys = self._graph.heads(as_keys)
1698
return set([h[0] for h in head_keys])
1700
def merge_sort(self, tip_revision):
1701
nodes = self._graph.merge_sort((tip_revision,))
1703
node.key = node.key[0]
1706
def add_node(self, revision, parents):
1707
self._graph.add_node((revision,), [(p,) for p in parents])
1660
1710
_counters = [0,0,0,0,0,0,0]
1662
1712
from bzrlib._known_graph_pyx import KnownGraph
1713
except ImportError, e:
1714
osutils.failed_to_load_extension(e)
1664
1715
from bzrlib._known_graph_py import KnownGraph