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