64
59
def __repr__(self):
65
60
return 'DictParentsProvider(%r)' % self.ancestry
67
# Note: DictParentsProvider does not implement get_cached_parent_map
68
# Arguably, the data is clearly cached in memory. However, this class
69
# is mostly used for testing, and it keeps the tests clean to not
72
62
def get_parent_map(self, keys):
73
63
"""See StackedParentsProvider.get_parent_map"""
74
64
ancestry = self.ancestry
75
return dict([(k, ancestry[k]) for k in keys if k in ancestry])
65
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)
78
71
class StackedParentsProvider(object):
79
72
"""A parents provider which stacks (or unions) multiple providers.
81
74
The providers are queries in the order of the provided parent_providers.
84
77
def __init__(self, parent_providers):
85
78
self._parent_providers = parent_providers
314
258
right = searchers[1].seen
315
259
return (left.difference(right), right.difference(left))
317
def find_descendants(self, old_key, new_key):
318
"""Find descendants of old_key that are ancestors of new_key."""
319
child_map = self.get_child_map(self._find_descendant_ancestors(
321
graph = Graph(DictParentsProvider(child_map))
322
searcher = graph._make_breadth_first_searcher([old_key])
326
def _find_descendant_ancestors(self, old_key, new_key):
327
"""Find ancestors of new_key that may be descendants of old_key."""
328
stop = self._make_breadth_first_searcher([old_key])
329
descendants = self._make_breadth_first_searcher([new_key])
330
for revisions in descendants:
331
old_stop = stop.seen.intersection(revisions)
332
descendants.stop_searching_any(old_stop)
333
seen_stop = descendants.find_seen_ancestors(stop.step())
334
descendants.stop_searching_any(seen_stop)
335
return descendants.seen.difference(stop.seen)
337
def get_child_map(self, keys):
338
"""Get a mapping from parents to children of the specified keys.
340
This is simply the inversion of get_parent_map. Only supplied keys
341
will be discovered as children.
342
:return: a dict of key:child_list for keys.
344
parent_map = self._parents_provider.get_parent_map(keys)
346
for child, parents in sorted(viewitems(parent_map)):
347
for parent in parents:
348
parent_child.setdefault(parent, []).append(child)
351
261
def find_distance_to_null(self, target_revision_id, known_revision_ids):
352
262
"""Find the left-hand distance to the NULL_REVISION.
607
516
for searcher in unique_tip_searchers:
608
517
common_to_all_unique_nodes.intersection_update(searcher.seen)
609
518
common_to_all_unique_nodes.intersection_update(
610
all_unique_searcher.seen)
519
all_unique_searcher.seen)
611
520
# Step all-unique less frequently than the other searchers.
612
521
# In the common case, we don't need to spider out far here, so
613
522
# avoid doing extra work.
614
523
if step_all_unique:
615
tstart = osutils.perf_counter()
524
tstart = time.clock()
616
525
nodes = all_unique_searcher.step()
617
526
common_to_all_unique_nodes.update(nodes)
618
527
if 'graph' in debug.debug_flags:
619
tdelta = osutils.perf_counter() - tstart
528
tdelta = time.clock() - tstart
620
529
trace.mutter('all_unique_searcher step() took %.3fs'
621
530
'for %d nodes (%d total), iteration: %s',
622
531
tdelta, len(nodes), len(all_unique_searcher.seen),
1248
1117
# This is the same as the following loop. I don't know that it is any
1250
# simple_ancestors.difference_update(r for r, p_ids in parent_map.iteritems()
1251
# if p_ids is not None and revisions.intersection(p_ids))
1252
# return simple_ancestors
1119
## simple_ancestors.difference_update(r for r, p_ids in parent_map.iteritems()
1120
## if p_ids is not None and revisions.intersection(p_ids))
1121
## return simple_ancestors
1254
1123
# Yet Another Way, invert the parent map (which can be cached)
1255
1124
## descendants = {}
1256
# for revision_id, parent_ids in parent_map.iteritems():
1257
# for p_id in parent_ids:
1125
## for revision_id, parent_ids in parent_map.iteritems():
1126
## for p_id in parent_ids:
1258
1127
## descendants.setdefault(p_id, []).append(revision_id)
1259
# for revision in revisions.intersection(descendants):
1260
# simple_ancestors.difference_update(descendants[revision])
1261
# return simple_ancestors
1262
for revision, parent_ids in viewitems(parent_map):
1128
## for revision in revisions.intersection(descendants):
1129
## simple_ancestors.difference_update(descendants[revision])
1130
## return simple_ancestors
1131
for revision, parent_ids in parent_map.iteritems():
1263
1132
if parent_ids is None:
1265
1134
for parent_id in parent_ids:
1595
1463
return revs, ghosts
1598
def invert_parent_map(parent_map):
1599
"""Given a map from child => parents, create a map of parent=>children"""
1601
for child, parents in viewitems(parent_map):
1603
# Any given parent is likely to have only a small handful
1604
# of children, many will have only one. So we avoid mem overhead of
1605
# a list, in exchange for extra copying of tuples
1606
if p not in child_map:
1607
child_map[p] = (child,)
1609
child_map[p] = child_map[p] + (child,)
1466
class SearchResult(object):
1467
"""The result of a breadth first search.
1469
A SearchResult provides the ability to reconstruct the search or access a
1470
set of the keys the search found.
1473
def __init__(self, start_keys, exclude_keys, key_count, keys):
1474
"""Create a SearchResult.
1476
:param start_keys: The keys the search started at.
1477
:param exclude_keys: The keys the search excludes.
1478
:param key_count: The total number of keys (from start to but not
1480
:param keys: The keys the search found. Note that in future we may get
1481
a SearchResult from a smart server, in which case the keys list is
1482
not necessarily immediately available.
1484
self._recipe = ('search', start_keys, exclude_keys, key_count)
1485
self._keys = frozenset(keys)
1487
def get_recipe(self):
1488
"""Return a recipe that can be used to replay this search.
1490
The recipe allows reconstruction of the same results at a later date
1491
without knowing all the found keys. The essential elements are a list
1492
of keys to start and to stop at. In order to give reproducible
1493
results when ghosts are encountered by a search they are automatically
1494
added to the exclude list (or else ghost filling may alter the
1497
:return: A tuple ('search', start_keys_set, exclude_keys_set,
1498
revision_count). To recreate the results of this search, create a
1499
breadth first searcher on the same graph starting at start_keys.
1500
Then call next() (or next_with_ghosts()) repeatedly, and on every
1501
result, call stop_searching_any on any keys from the exclude_keys
1502
set. The revision_count value acts as a trivial cross-check - the
1503
found revisions of the new search should have as many elements as
1504
revision_count. If it does not, then additional revisions have been
1505
ghosted since the search was executed the first time and the second
1511
"""Return the keys found in this search.
1513
:return: A set of keys.
1518
"""Return false if the search lists 1 or more revisions."""
1519
return self._recipe[3] == 0
1521
def refine(self, seen, referenced):
1522
"""Create a new search by refining this search.
1524
:param seen: Revisions that have been satisfied.
1525
:param referenced: Revision references observed while satisfying some
1528
start = self._recipe[1]
1529
exclude = self._recipe[2]
1530
count = self._recipe[3]
1531
keys = self.get_keys()
1532
# New heads = referenced + old heads - seen things - exclude
1533
pending_refs = set(referenced)
1534
pending_refs.update(start)
1535
pending_refs.difference_update(seen)
1536
pending_refs.difference_update(exclude)
1537
# New exclude = old exclude + satisfied heads
1538
seen_heads = start.intersection(seen)
1539
exclude.update(seen_heads)
1540
# keys gets seen removed
1542
# length is reduced by len(seen)
1544
return SearchResult(pending_refs, exclude, count, keys)
1547
class PendingAncestryResult(object):
1548
"""A search result that will reconstruct the ancestry for some graph heads.
1550
Unlike SearchResult, this doesn't hold the complete search result in
1551
memory, it just holds a description of how to generate it.
1554
def __init__(self, heads, repo):
1557
:param heads: an iterable of graph heads.
1558
:param repo: a repository to use to generate the ancestry for the given
1561
self.heads = frozenset(heads)
1564
def get_recipe(self):
1565
"""Return a recipe that can be used to replay this search.
1567
The recipe allows reconstruction of the same results at a later date.
1569
:seealso SearchResult.get_recipe:
1571
:return: A tuple ('proxy-search', start_keys_set, set(), -1)
1572
To recreate this result, create a PendingAncestryResult with the
1575
return ('proxy-search', self.heads, set(), -1)
1578
"""See SearchResult.get_keys.
1580
Returns all the keys for the ancestry of the heads, excluding
1583
return self._get_keys(self.repo.get_graph())
1585
def _get_keys(self, graph):
1586
NULL_REVISION = revision.NULL_REVISION
1587
keys = [key for (key, parents) in graph.iter_ancestry(self.heads)
1588
if key != NULL_REVISION and parents is not None]
1592
"""Return false if the search lists 1 or more revisions."""
1593
if revision.NULL_REVISION in self.heads:
1594
return len(self.heads) == 1
1596
return len(self.heads) == 0
1598
def refine(self, seen, referenced):
1599
"""Create a new search by refining this search.
1601
:param seen: Revisions that have been satisfied.
1602
:param referenced: Revision references observed while satisfying some
1605
referenced = self.heads.union(referenced)
1606
return PendingAncestryResult(referenced - seen, self.repo)
1613
1609
def collapse_linear_regions(parent_map):
1695
1692
"""See Graph.heads()"""
1696
1693
as_keys = [(i,) for i in ids]
1697
1694
head_keys = self._graph.heads(as_keys)
1698
return {h[0] for h in head_keys}
1695
return set([h[0] for h in head_keys])
1700
1697
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])
1710
_counters = [0, 0, 0, 0, 0, 0, 0]
1698
return self._graph.merge_sort((tip_revision,))
1701
_counters = [0,0,0,0,0,0,0]
1712
from ._known_graph_pyx import KnownGraph
1713
except ImportError as e:
1703
from bzrlib._known_graph_pyx import KnownGraph
1704
except ImportError, e:
1714
1705
osutils.failed_to_load_extension(e)
1715
from ._known_graph_py import KnownGraph # noqa: F401
1706
from bzrlib._known_graph_py import KnownGraph