3242
3424
annotator = _KnitAnnotator(knit)
3243
return iter(annotator.annotate(revision_id))
3246
class _KnitAnnotator(object):
3425
return iter(annotator.annotate_flat(revision_id))
3428
class _KnitAnnotator(annotate.Annotator):
3247
3429
"""Build up the annotations for a text."""
3249
def __init__(self, knit):
3252
# Content objects, differs from fulltexts because of how final newlines
3253
# are treated by knits. the content objects here will always have a
3255
self._fulltext_contents = {}
3257
# Annotated lines of specific revisions
3258
self._annotated_lines = {}
3260
# Track the raw data for nodes that we could not process yet.
3261
# This maps the revision_id of the base to a list of children that will
3262
# annotated from it.
3263
self._pending_children = {}
3265
# Nodes which cannot be extracted
3266
self._ghosts = set()
3268
# Track how many children this node has, so we know if we need to keep
3270
self._annotate_children = {}
3271
self._compression_children = {}
3431
def __init__(self, vf):
3432
annotate.Annotator.__init__(self, vf)
3434
# TODO: handle Nodes which cannot be extracted
3435
# self._ghosts = set()
3437
# Map from (key, parent_key) => matching_blocks, should be 'use once'
3438
self._matching_blocks = {}
3440
# KnitContent objects
3441
self._content_objects = {}
3442
# The number of children that depend on this fulltext content object
3443
self._num_compression_children = {}
3444
# Delta records that need their compression parent before they can be
3446
self._pending_deltas = {}
3447
# Fulltext records that are waiting for their parents fulltexts before
3448
# they can be yielded for annotation
3449
self._pending_annotation = {}
3273
3451
self._all_build_details = {}
3274
# The children => parent revision_id graph
3275
self._revision_id_graph = {}
3277
self._heads_provider = None
3279
self._nodes_to_keep_annotations = set()
3280
self._generations_until_keep = 100
3282
def set_generations_until_keep(self, value):
3283
"""Set the number of generations before caching a node.
3285
Setting this to -1 will cache every merge node, setting this higher
3286
will cache fewer nodes.
3288
self._generations_until_keep = value
3290
def _add_fulltext_content(self, revision_id, content_obj):
3291
self._fulltext_contents[revision_id] = content_obj
3292
# TODO: jam 20080305 It might be good to check the sha1digest here
3293
return content_obj.text()
3295
def _check_parents(self, child, nodes_to_annotate):
3296
"""Check if all parents have been processed.
3298
:param child: A tuple of (rev_id, parents, raw_content)
3299
:param nodes_to_annotate: If child is ready, add it to
3300
nodes_to_annotate, otherwise put it back in self._pending_children
3302
for parent_id in child[1]:
3303
if (parent_id not in self._annotated_lines):
3304
# This parent is present, but another parent is missing
3305
self._pending_children.setdefault(parent_id,
3309
# This one is ready to be processed
3310
nodes_to_annotate.append(child)
3312
def _add_annotation(self, revision_id, fulltext, parent_ids,
3313
left_matching_blocks=None):
3314
"""Add an annotation entry.
3316
All parents should already have been annotated.
3317
:return: A list of children that now have their parents satisfied.
3319
a = self._annotated_lines
3320
annotated_parent_lines = [a[p] for p in parent_ids]
3321
annotated_lines = list(annotate.reannotate(annotated_parent_lines,
3322
fulltext, revision_id, left_matching_blocks,
3323
heads_provider=self._get_heads_provider()))
3324
self._annotated_lines[revision_id] = annotated_lines
3325
for p in parent_ids:
3326
ann_children = self._annotate_children[p]
3327
ann_children.remove(revision_id)
3328
if (not ann_children
3329
and p not in self._nodes_to_keep_annotations):
3330
del self._annotated_lines[p]
3331
del self._all_build_details[p]
3332
if p in self._fulltext_contents:
3333
del self._fulltext_contents[p]
3334
# Now that we've added this one, see if there are any pending
3335
# deltas to be done, certainly this parent is finished
3336
nodes_to_annotate = []
3337
for child in self._pending_children.pop(revision_id, []):
3338
self._check_parents(child, nodes_to_annotate)
3339
return nodes_to_annotate
3341
3453
def _get_build_graph(self, key):
3342
3454
"""Get the graphs for building texts and annotations.
3349
3461
:return: A list of (key, index_memo) records, suitable for
3350
passing to read_records_iter to start reading in the raw data fro/
3462
passing to read_records_iter to start reading in the raw data from
3353
if key in self._annotated_lines:
3356
3465
pending = set([key])
3468
self._num_needed_children[key] = 1
3361
3470
# get all pending nodes
3363
3471
this_iteration = pending
3364
build_details = self._knit._index.get_build_details(this_iteration)
3472
build_details = self._vf._index.get_build_details(this_iteration)
3365
3473
self._all_build_details.update(build_details)
3366
# new_nodes = self._knit._index._get_entries(this_iteration)
3474
# new_nodes = self._vf._index._get_entries(this_iteration)
3367
3475
pending = set()
3368
3476
for key, details in build_details.iteritems():
3369
(index_memo, compression_parent, parents,
3477
(index_memo, compression_parent, parent_keys,
3370
3478
record_details) = details
3371
self._revision_id_graph[key] = parents
3479
self._parent_map[key] = parent_keys
3480
self._heads_provider = None
3372
3481
records.append((key, index_memo))
3373
3482
# Do we actually need to check _annotated_lines?
3374
pending.update(p for p in parents
3375
if p not in self._all_build_details)
3483
pending.update([p for p in parent_keys
3484
if p not in self._all_build_details])
3486
for parent_key in parent_keys:
3487
if parent_key in self._num_needed_children:
3488
self._num_needed_children[parent_key] += 1
3490
self._num_needed_children[parent_key] = 1
3376
3491
if compression_parent:
3377
self._compression_children.setdefault(compression_parent,
3380
for parent in parents:
3381
self._annotate_children.setdefault(parent,
3383
num_gens = generation - kept_generation
3384
if ((num_gens >= self._generations_until_keep)
3385
and len(parents) > 1):
3386
kept_generation = generation
3387
self._nodes_to_keep_annotations.add(key)
3492
if compression_parent in self._num_compression_children:
3493
self._num_compression_children[compression_parent] += 1
3495
self._num_compression_children[compression_parent] = 1
3389
3497
missing_versions = this_iteration.difference(build_details.keys())
3390
self._ghosts.update(missing_versions)
3391
for missing_version in missing_versions:
3392
# add a key, no parents
3393
self._revision_id_graph[missing_version] = ()
3394
pending.discard(missing_version) # don't look for it
3395
if self._ghosts.intersection(self._compression_children):
3397
"We cannot have nodes which have a ghost compression parent:\n"
3399
"compression children: %r"
3400
% (self._ghosts, self._compression_children))
3401
# Cleanout anything that depends on a ghost so that we don't wait for
3402
# the ghost to show up
3403
for node in self._ghosts:
3404
if node in self._annotate_children:
3405
# We won't be building this node
3406
del self._annotate_children[node]
3498
if missing_versions:
3499
for key in missing_versions:
3500
if key in self._parent_map and key in self._text_cache:
3501
# We already have this text ready, we just need to
3502
# yield it later so we get it annotated
3504
parent_keys = self._parent_map[key]
3505
for parent_key in parent_keys:
3506
if parent_key in self._num_needed_children:
3507
self._num_needed_children[parent_key] += 1
3509
self._num_needed_children[parent_key] = 1
3510
pending.update([p for p in parent_keys
3511
if p not in self._all_build_details])
3513
raise errors.RevisionNotPresent(key, self._vf)
3407
3514
# Generally we will want to read the records in reverse order, because
3408
3515
# we find the parent nodes after the children
3409
3516
records.reverse()
3412
def _annotate_records(self, records):
3413
"""Build the annotations for the listed records."""
3517
return records, ann_keys
3519
def _get_needed_texts(self, key, pb=None):
3520
# if True or len(self._vf._fallback_vfs) > 0:
3521
if len(self._vf._fallback_vfs) > 0:
3522
# If we have fallbacks, go to the generic path
3523
for v in annotate.Annotator._get_needed_texts(self, key, pb=pb):
3528
records, ann_keys = self._get_build_graph(key)
3529
for idx, (sub_key, text, num_lines) in enumerate(
3530
self._extract_texts(records)):
3532
pb.update('annotating', idx, len(records))
3533
yield sub_key, text, num_lines
3534
for sub_key in ann_keys:
3535
text = self._text_cache[sub_key]
3536
num_lines = len(text) # bad assumption
3537
yield sub_key, text, num_lines
3539
except errors.RetryWithNewPacks, e:
3540
self._vf._access.reload_or_raise(e)
3541
# The cached build_details are no longer valid
3542
self._all_build_details.clear()
3544
def _cache_delta_blocks(self, key, compression_parent, delta, lines):
3545
parent_lines = self._text_cache[compression_parent]
3546
blocks = list(KnitContent.get_line_delta_blocks(delta, parent_lines, lines))
3547
self._matching_blocks[(key, compression_parent)] = blocks
3549
def _expand_record(self, key, parent_keys, compression_parent, record,
3552
if compression_parent:
3553
if compression_parent not in self._content_objects:
3554
# Waiting for the parent
3555
self._pending_deltas.setdefault(compression_parent, []).append(
3556
(key, parent_keys, record, record_details))
3558
# We have the basis parent, so expand the delta
3559
num = self._num_compression_children[compression_parent]
3562
base_content = self._content_objects.pop(compression_parent)
3563
self._num_compression_children.pop(compression_parent)
3565
self._num_compression_children[compression_parent] = num
3566
base_content = self._content_objects[compression_parent]
3567
# It is tempting to want to copy_base_content=False for the last
3568
# child object. However, whenever noeol=False,
3569
# self._text_cache[parent_key] is content._lines. So mutating it
3570
# gives very bad results.
3571
# The alternative is to copy the lines into text cache, but then we
3572
# are copying anyway, so just do it here.
3573
content, delta = self._vf._factory.parse_record(
3574
key, record, record_details, base_content,
3575
copy_base_content=True)
3578
content, _ = self._vf._factory.parse_record(
3579
key, record, record_details, None)
3580
if self._num_compression_children.get(key, 0) > 0:
3581
self._content_objects[key] = content
3582
lines = content.text()
3583
self._text_cache[key] = lines
3584
if delta is not None:
3585
self._cache_delta_blocks(key, compression_parent, delta, lines)
3588
def _get_parent_annotations_and_matches(self, key, text, parent_key):
3589
"""Get the list of annotations for the parent, and the matching lines.
3591
:param text: The opaque value given by _get_needed_texts
3592
:param parent_key: The key for the parent text
3593
:return: (parent_annotations, matching_blocks)
3594
parent_annotations is a list as long as the number of lines in
3596
matching_blocks is a list of (parent_idx, text_idx, len) tuples
3597
indicating which lines match between the two texts
3599
block_key = (key, parent_key)
3600
if block_key in self._matching_blocks:
3601
blocks = self._matching_blocks.pop(block_key)
3602
parent_annotations = self._annotations_cache[parent_key]
3603
return parent_annotations, blocks
3604
return annotate.Annotator._get_parent_annotations_and_matches(self,
3605
key, text, parent_key)
3607
def _process_pending(self, key):
3608
"""The content for 'key' was just processed.
3610
Determine if there is any more pending work to be processed.
3613
if key in self._pending_deltas:
3614
compression_parent = key
3615
children = self._pending_deltas.pop(key)
3616
for child_key, parent_keys, record, record_details in children:
3617
lines = self._expand_record(child_key, parent_keys,
3619
record, record_details)
3620
if self._check_ready_for_annotations(child_key, parent_keys):
3621
to_return.append(child_key)
3622
# Also check any children that are waiting for this parent to be
3624
if key in self._pending_annotation:
3625
children = self._pending_annotation.pop(key)
3626
to_return.extend([c for c, p_keys in children
3627
if self._check_ready_for_annotations(c, p_keys)])
3630
def _check_ready_for_annotations(self, key, parent_keys):
3631
"""return true if this text is ready to be yielded.
3633
Otherwise, this will return False, and queue the text into
3634
self._pending_annotation
3636
for parent_key in parent_keys:
3637
if parent_key not in self._annotations_cache:
3638
# still waiting on at least one parent text, so queue it up
3639
# Note that if there are multiple parents, we need to wait
3641
self._pending_annotation.setdefault(parent_key,
3642
[]).append((key, parent_keys))
3646
def _extract_texts(self, records):
3647
"""Extract the various texts needed based on records"""
3414
3648
# We iterate in the order read, rather than a strict order requested
3415
3649
# However, process what we can, and put off to the side things that
3416
3650
# still need parents, cleaning them up when those parents are
3418
for (rev_id, record,
3419
digest) in self._knit._read_records_iter(records):
3420
if rev_id in self._annotated_lines:
3653
# 1) As 'records' are read, see if we can expand these records into
3654
# Content objects (and thus lines)
3655
# 2) If a given line-delta is waiting on its compression parent, it
3656
# gets queued up into self._pending_deltas, otherwise we expand
3657
# it, and put it into self._text_cache and self._content_objects
3658
# 3) If we expanded the text, we will then check to see if all
3659
# parents have also been processed. If so, this text gets yielded,
3660
# else this record gets set aside into pending_annotation
3661
# 4) Further, if we expanded the text in (2), we will then check to
3662
# see if there are any children in self._pending_deltas waiting to
3663
# also be processed. If so, we go back to (2) for those
3664
# 5) Further again, if we yielded the text, we can then check if that
3665
# 'unlocks' any of the texts in pending_annotations, which should
3666
# then get yielded as well
3667
# Note that both steps 4 and 5 are 'recursive' in that unlocking one
3668
# compression child could unlock yet another, and yielding a fulltext
3669
# will also 'unlock' the children that are waiting on that annotation.
3670
# (Though also, unlocking 1 parent's fulltext, does not unlock a child
3671
# if other parents are also waiting.)
3672
# We want to yield content before expanding child content objects, so
3673
# that we know when we can re-use the content lines, and the annotation
3674
# code can know when it can stop caching fulltexts, as well.
3676
# Children that are missing their compression parent
3678
for (key, record, digest) in self._vf._read_records_iter(records):
3680
details = self._all_build_details[key]
3681
(_, compression_parent, parent_keys, record_details) = details
3682
lines = self._expand_record(key, parent_keys, compression_parent,
3683
record, record_details)
3685
# Pending delta should be queued up
3422
parent_ids = self._revision_id_graph[rev_id]
3423
parent_ids = [p for p in parent_ids if p not in self._ghosts]
3424
details = self._all_build_details[rev_id]
3425
(index_memo, compression_parent, parents,
3426
record_details) = details
3427
nodes_to_annotate = []
3428
# TODO: Remove the punning between compression parents, and
3429
# parent_ids, we should be able to do this without assuming
3431
if len(parent_ids) == 0:
3432
# There are no parents for this node, so just add it
3433
# TODO: This probably needs to be decoupled
3434
fulltext_content, delta = self._knit._factory.parse_record(
3435
rev_id, record, record_details, None)
3436
fulltext = self._add_fulltext_content(rev_id, fulltext_content)
3437
nodes_to_annotate.extend(self._add_annotation(rev_id, fulltext,
3438
parent_ids, left_matching_blocks=None))
3440
child = (rev_id, parent_ids, record)
3441
# Check if all the parents are present
3442
self._check_parents(child, nodes_to_annotate)
3443
while nodes_to_annotate:
3444
# Should we use a queue here instead of a stack?
3445
(rev_id, parent_ids, record) = nodes_to_annotate.pop()
3446
(index_memo, compression_parent, parents,
3447
record_details) = self._all_build_details[rev_id]
3449
if compression_parent is not None:
3450
comp_children = self._compression_children[compression_parent]
3451
if rev_id not in comp_children:
3452
raise AssertionError("%r not in compression children %r"
3453
% (rev_id, comp_children))
3454
# If there is only 1 child, it is safe to reuse this
3456
reuse_content = (len(comp_children) == 1
3457
and compression_parent not in
3458
self._nodes_to_keep_annotations)
3460
# Remove it from the cache since it will be changing
3461
parent_fulltext_content = self._fulltext_contents.pop(compression_parent)
3462
# Make sure to copy the fulltext since it might be
3464
parent_fulltext = list(parent_fulltext_content.text())
3466
parent_fulltext_content = self._fulltext_contents[compression_parent]
3467
parent_fulltext = parent_fulltext_content.text()
3468
comp_children.remove(rev_id)
3469
fulltext_content, delta = self._knit._factory.parse_record(
3470
rev_id, record, record_details,
3471
parent_fulltext_content,
3472
copy_base_content=(not reuse_content))
3473
fulltext = self._add_fulltext_content(rev_id,
3475
if compression_parent == parent_ids[0]:
3476
# the compression_parent is the left parent, so we can
3478
blocks = KnitContent.get_line_delta_blocks(delta,
3479
parent_fulltext, fulltext)
3481
fulltext_content = self._knit._factory.parse_fulltext(
3483
fulltext = self._add_fulltext_content(rev_id,
3485
nodes_to_annotate.extend(
3486
self._add_annotation(rev_id, fulltext, parent_ids,
3487
left_matching_blocks=blocks))
3489
def _get_heads_provider(self):
3490
"""Create a heads provider for resolving ancestry issues."""
3491
if self._heads_provider is not None:
3492
return self._heads_provider
3493
parent_provider = _mod_graph.DictParentsProvider(
3494
self._revision_id_graph)
3495
graph_obj = _mod_graph.Graph(parent_provider)
3496
head_cache = _mod_graph.FrozenHeadsCache(graph_obj)
3497
self._heads_provider = head_cache
3500
def annotate(self, key):
3501
"""Return the annotated fulltext at the given key.
3503
:param key: The key to annotate.
3505
if len(self._knit._fallback_vfs) > 0:
3506
# stacked knits can't use the fast path at present.
3507
return self._simple_annotate(key)
3510
records = self._get_build_graph(key)
3511
if key in self._ghosts:
3512
raise errors.RevisionNotPresent(key, self._knit)
3513
self._annotate_records(records)
3514
return self._annotated_lines[key]
3515
except errors.RetryWithNewPacks, e:
3516
self._knit._access.reload_or_raise(e)
3517
# The cached build_details are no longer valid
3518
self._all_build_details.clear()
3520
def _simple_annotate(self, key):
3521
"""Return annotated fulltext, rediffing from the full texts.
3523
This is slow but makes no assumptions about the repository
3524
being able to produce line deltas.
3526
# TODO: this code generates a parent maps of present ancestors; it
3527
# could be split out into a separate method, and probably should use
3528
# iter_ancestry instead. -- mbp and robertc 20080704
3529
graph = _mod_graph.Graph(self._knit)
3530
head_cache = _mod_graph.FrozenHeadsCache(graph)
3531
search = graph._make_breadth_first_searcher([key])
3535
present, ghosts = search.next_with_ghosts()
3536
except StopIteration:
3538
keys.update(present)
3539
parent_map = self._knit.get_parent_map(keys)
3541
reannotate = annotate.reannotate
3542
for record in self._knit.get_record_stream(keys, 'topological', True):
3544
fulltext = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
3545
parents = parent_map[key]
3546
if parents is not None:
3547
parent_lines = [parent_cache[parent] for parent in parent_map[key]]
3550
parent_cache[key] = list(
3551
reannotate(parent_lines, fulltext, key, None, head_cache))
3553
return parent_cache[key]
3555
raise errors.RevisionNotPresent(key, self._knit)
3687
# At this point, we may be able to yield this content, if all
3688
# parents are also finished
3689
yield_this_text = self._check_ready_for_annotations(key,
3692
# All parents present
3693
yield key, lines, len(lines)
3694
to_process = self._process_pending(key)
3696
this_process = to_process
3698
for key in this_process:
3699
lines = self._text_cache[key]
3700
yield key, lines, len(lines)
3701
to_process.extend(self._process_pending(key))
3559
from bzrlib._knit_load_data_c import _load_data_c as _load_data
3704
from bzrlib._knit_load_data_pyx import _load_data_c as _load_data
3560
3705
except ImportError:
3561
3706
from bzrlib._knit_load_data_py import _load_data_py as _load_data