1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Knit versionedfile implementation.
19
A knit is a versioned file implementation that supports efficient append only
23
lifeless: the data file is made up of "delta records". each delta record has a delta header
24
that contains; (1) a version id, (2) the size of the delta (in lines), and (3) the digest of
25
the -expanded data- (ie, the delta applied to the parent). the delta also ends with a
26
end-marker; simply "end VERSION"
28
delta can be line or full contents.a
29
... the 8's there are the index number of the annotation.
30
version robertc@robertcollins.net-20051003014215-ee2990904cc4c7ad 7 c7d23b2a5bd6ca00e8e266cec0ec228158ee9f9e
34
8 e.set('executable', 'yes')
36
8 if elt.get('executable') == 'yes':
37
8 ie.executable = True
38
end robertc@robertcollins.net-20051003014215-ee2990904cc4c7ad
42
09:33 < jrydberg> lifeless: each index is made up of a tuple of; version id, options, position, size, parents
43
09:33 < jrydberg> lifeless: the parents are currently dictionary compressed
44
09:33 < jrydberg> lifeless: (meaning it currently does not support ghosts)
45
09:33 < lifeless> right
46
09:33 < jrydberg> lifeless: the position and size is the range in the data file
49
so the index sequence is the dictionary compressed sequence number used
50
in the deltas to provide line annotation
55
# 10:16 < lifeless> make partial index writes safe
56
# 10:16 < lifeless> implement 'knit.check()' like weave.check()
57
# 10:17 < lifeless> record known ghosts so we can detect when they are filled in rather than the current 'reweave
59
# move sha1 out of the content so that join is faster at verifying parents
60
# record content length ?
64
from cStringIO import StringIO
66
from itertools import izip, chain
73
from bzrlib.lazy_import import lazy_import
74
lazy_import(globals(), """
92
from bzrlib.errors import (
100
RevisionAlreadyPresent,
102
from bzrlib.tuned_gzip import GzipFile
103
from bzrlib.osutils import (
108
from bzrlib.symbol_versioning import DEPRECATED_PARAMETER, deprecated_passed
109
from bzrlib.tsort import topo_sort
112
from bzrlib.versionedfile import VersionedFile, InterVersionedFile
115
# TODO: Split out code specific to this format into an associated object.
117
# TODO: Can we put in some kind of value to check that the index and data
118
# files belong together?
120
# TODO: accommodate binaries, perhaps by storing a byte count
122
# TODO: function to check whole file
124
# TODO: atomically append data, then measure backwards from the cursor
125
# position after writing to work out where it was located. we may need to
126
# bypass python file buffering.
128
DATA_SUFFIX = '.knit'
129
INDEX_SUFFIX = '.kndx'
132
class KnitContent(object):
133
"""Content of a knit version to which deltas can be applied."""
135
def __init__(self, lines):
138
def annotate_iter(self):
139
"""Yield tuples of (origin, text) for each content line."""
140
return iter(self._lines)
143
"""Return a list of (origin, text) tuples."""
144
return list(self.annotate_iter())
146
def line_delta_iter(self, new_lines):
147
"""Generate line-based delta from this content to new_lines."""
148
new_texts = new_lines.text()
149
old_texts = self.text()
150
s = KnitSequenceMatcher(None, old_texts, new_texts)
151
for tag, i1, i2, j1, j2 in s.get_opcodes():
154
# ofrom, oto, length, data
155
yield i1, i2, j2 - j1, new_lines._lines[j1:j2]
157
def line_delta(self, new_lines):
158
return list(self.line_delta_iter(new_lines))
161
return [text for origin, text in self._lines]
164
return KnitContent(self._lines[:])
167
def get_line_delta_blocks(knit_delta, source, target):
168
"""Extract SequenceMatcher.get_matching_blocks() from a knit delta"""
169
target_len = len(target)
172
for s_begin, s_end, t_len, new_text in knit_delta:
173
true_n = s_begin - s_pos
176
# knit deltas do not provide reliable info about whether the
177
# last line of a file matches, due to eol handling.
178
if source[s_pos + n -1] != target[t_pos + n -1]:
181
yield s_pos, t_pos, n
182
t_pos += t_len + true_n
184
n = target_len - t_pos
186
if source[s_pos + n -1] != target[t_pos + n -1]:
189
yield s_pos, t_pos, n
190
yield s_pos + (target_len - t_pos), target_len, 0
193
class _KnitFactory(object):
194
"""Base factory for creating content objects."""
196
def make(self, lines, version_id):
197
num_lines = len(lines)
198
return KnitContent(zip([version_id] * num_lines, lines))
201
class KnitAnnotateFactory(_KnitFactory):
202
"""Factory for creating annotated Content objects."""
206
def parse_fulltext(self, content, version_id):
207
"""Convert fulltext to internal representation
209
fulltext content is of the format
210
revid(utf8) plaintext\n
211
internal representation is of the format:
214
# TODO: jam 20070209 The tests expect this to be returned as tuples,
215
# but the code itself doesn't really depend on that.
216
# Figure out a way to not require the overhead of turning the
217
# list back into tuples.
218
lines = [tuple(line.split(' ', 1)) for line in content]
219
return KnitContent(lines)
221
def parse_line_delta_iter(self, lines):
222
return iter(self.parse_line_delta(lines))
224
def parse_line_delta(self, lines, version_id):
225
"""Convert a line based delta into internal representation.
227
line delta is in the form of:
228
intstart intend intcount
230
revid(utf8) newline\n
231
internal representation is
232
(start, end, count, [1..count tuples (revid, newline)])
239
def cache_and_return(line):
240
origin, text = line.split(' ', 1)
241
return cache.setdefault(origin, origin), text
243
# walk through the lines parsing.
245
start, end, count = [int(n) for n in header.split(',')]
246
contents = [tuple(next().split(' ', 1)) for i in xrange(count)]
247
result.append((start, end, count, contents))
250
def get_fulltext_content(self, lines):
251
"""Extract just the content lines from a fulltext."""
252
return (line.split(' ', 1)[1] for line in lines)
254
def get_linedelta_content(self, lines):
255
"""Extract just the content from a line delta.
257
This doesn't return all of the extra information stored in a delta.
258
Only the actual content lines.
263
header = header.split(',')
264
count = int(header[2])
265
for i in xrange(count):
266
origin, text = next().split(' ', 1)
269
def lower_fulltext(self, content):
270
"""convert a fulltext content record into a serializable form.
272
see parse_fulltext which this inverts.
274
# TODO: jam 20070209 We only do the caching thing to make sure that
275
# the origin is a valid utf-8 line, eventually we could remove it
276
return ['%s %s' % (o, t) for o, t in content._lines]
278
def lower_line_delta(self, delta):
279
"""convert a delta into a serializable form.
281
See parse_line_delta which this inverts.
283
# TODO: jam 20070209 We only do the caching thing to make sure that
284
# the origin is a valid utf-8 line, eventually we could remove it
286
for start, end, c, lines in delta:
287
out.append('%d,%d,%d\n' % (start, end, c))
288
out.extend(origin + ' ' + text
289
for origin, text in lines)
292
def annotate_iter(self, knit, version_id):
293
content = knit._get_content(version_id)
294
for origin, text in content.annotate_iter():
297
def annotate_iter(self, knit, version_id):
298
return annotate_knit(knit, version_id)
301
class KnitPlainFactory(_KnitFactory):
302
"""Factory for creating plain Content objects."""
306
def parse_fulltext(self, content, version_id):
307
"""This parses an unannotated fulltext.
309
Note that this is not a noop - the internal representation
310
has (versionid, line) - its just a constant versionid.
312
return self.make(content, version_id)
314
def parse_line_delta_iter(self, lines, version_id):
316
num_lines = len(lines)
317
while cur < num_lines:
320
start, end, c = [int(n) for n in header.split(',')]
321
yield start, end, c, zip([version_id] * c, lines[cur:cur+c])
324
def parse_line_delta(self, lines, version_id):
325
return list(self.parse_line_delta_iter(lines, version_id))
327
def get_fulltext_content(self, lines):
328
"""Extract just the content lines from a fulltext."""
331
def get_linedelta_content(self, lines):
332
"""Extract just the content from a line delta.
334
This doesn't return all of the extra information stored in a delta.
335
Only the actual content lines.
340
header = header.split(',')
341
count = int(header[2])
342
for i in xrange(count):
345
def lower_fulltext(self, content):
346
return content.text()
348
def lower_line_delta(self, delta):
350
for start, end, c, lines in delta:
351
out.append('%d,%d,%d\n' % (start, end, c))
352
out.extend([text for origin, text in lines])
355
def annotate_iter(self, knit, version_id):
356
return annotate_knit(knit, version_id)
359
def make_empty_knit(transport, relpath):
360
"""Construct a empty knit at the specified location."""
361
k = KnitVersionedFile(transport, relpath, 'w', KnitPlainFactory)
364
class KnitVersionedFile(VersionedFile):
365
"""Weave-like structure with faster random access.
367
A knit stores a number of texts and a summary of the relationships
368
between them. Texts are identified by a string version-id. Texts
369
are normally stored and retrieved as a series of lines, but can
370
also be passed as single strings.
372
Lines are stored with the trailing newline (if any) included, to
373
avoid special cases for files with no final newline. Lines are
374
composed of 8-bit characters, not unicode. The combination of
375
these approaches should mean any 'binary' file can be safely
376
stored and retrieved.
379
def __init__(self, relpath, transport, file_mode=None, access_mode=None,
380
factory=None, basis_knit=DEPRECATED_PARAMETER, delta=True,
381
create=False, create_parent_dir=False, delay_create=False,
382
dir_mode=None, index=None, access_method=None):
383
"""Construct a knit at location specified by relpath.
385
:param create: If not True, only open an existing knit.
386
:param create_parent_dir: If True, create the parent directory if
387
creating the file fails. (This is used for stores with
388
hash-prefixes that may not exist yet)
389
:param delay_create: The calling code is aware that the knit won't
390
actually be created until the first data is stored.
391
:param index: An index to use for the knit.
393
if deprecated_passed(basis_knit):
394
warnings.warn("KnitVersionedFile.__(): The basis_knit parameter is"
395
" deprecated as of bzr 0.9.",
396
DeprecationWarning, stacklevel=2)
397
if access_mode is None:
399
super(KnitVersionedFile, self).__init__(access_mode)
400
assert access_mode in ('r', 'w'), "invalid mode specified %r" % access_mode
401
self.transport = transport
402
self.filename = relpath
403
self.factory = factory or KnitAnnotateFactory()
404
self.writable = (access_mode == 'w')
407
self._max_delta_chain = 200
410
self._index = _KnitIndex(transport, relpath + INDEX_SUFFIX,
411
access_mode, create=create, file_mode=file_mode,
412
create_parent_dir=create_parent_dir, delay_create=delay_create,
416
if access_method is None:
417
_access = _KnitAccess(transport, relpath + DATA_SUFFIX, file_mode, dir_mode,
418
((create and not len(self)) and delay_create), create_parent_dir)
420
_access = access_method
421
if create and not len(self) and not delay_create:
423
self._data = _KnitData(_access)
426
return '%s(%s)' % (self.__class__.__name__,
427
self.transport.abspath(self.filename))
429
def _check_should_delta(self, first_parents):
430
"""Iterate back through the parent listing, looking for a fulltext.
432
This is used when we want to decide whether to add a delta or a new
433
fulltext. It searches for _max_delta_chain parents. When it finds a
434
fulltext parent, it sees if the total size of the deltas leading up to
435
it is large enough to indicate that we want a new full text anyway.
437
Return True if we should create a new delta, False if we should use a
442
delta_parents = first_parents
443
for count in xrange(self._max_delta_chain):
444
parent = delta_parents[0]
445
method = self._index.get_method(parent)
446
index, pos, size = self._index.get_position(parent)
447
if method == 'fulltext':
451
delta_parents = self._index.get_parents(parent)
453
# We couldn't find a fulltext, so we must create a new one
456
return fulltext_size > delta_size
458
def _add_delta(self, version_id, parents, delta_parent, sha1, noeol, delta):
459
"""See VersionedFile._add_delta()."""
460
self._check_add(version_id, []) # should we check the lines ?
461
self._check_versions_present(parents)
465
for parent in parents:
466
if not self.has_version(parent):
467
ghosts.append(parent)
469
present_parents.append(parent)
471
if delta_parent is None:
472
# reconstitute as full text.
473
assert len(delta) == 1 or len(delta) == 0
475
assert delta[0][0] == 0
476
assert delta[0][1] == 0, delta[0][1]
477
return super(KnitVersionedFile, self)._add_delta(version_id,
488
options.append('no-eol')
490
if delta_parent is not None:
491
# determine the current delta chain length.
492
# To speed the extract of texts the delta chain is limited
493
# to a fixed number of deltas. This should minimize both
494
# I/O and the time spend applying deltas.
495
# The window was changed to a maximum of 200 deltas, but also added
496
# was a check that the total compressed size of the deltas is
497
# smaller than the compressed size of the fulltext.
498
if not self._check_should_delta([delta_parent]):
499
# We don't want a delta here, just do a normal insertion.
500
return super(KnitVersionedFile, self)._add_delta(version_id,
507
options.append('line-delta')
508
store_lines = self.factory.lower_line_delta(delta)
510
access_memo = self._data.add_record(version_id, digest, store_lines)
511
self._index.add_version(version_id, options, access_memo, parents)
513
def _add_raw_records(self, records, data):
514
"""Add all the records 'records' with data pre-joined in 'data'.
516
:param records: A list of tuples(version_id, options, parents, size).
517
:param data: The data for the records. When it is written, the records
518
are adjusted to have pos pointing into data by the sum of
519
the preceding records sizes.
522
raw_record_sizes = [record[3] for record in records]
523
positions = self._data.add_raw_records(raw_record_sizes, data)
526
for (version_id, options, parents, size), access_memo in zip(
528
index_entries.append((version_id, options, access_memo, parents))
529
if self._data._do_cache:
530
self._data._cache[version_id] = data[offset:offset+size]
532
self._index.add_versions(index_entries)
534
def enable_cache(self):
535
"""Start caching data for this knit"""
536
self._data.enable_cache()
538
def clear_cache(self):
539
"""Clear the data cache only."""
540
self._data.clear_cache()
542
def copy_to(self, name, transport):
543
"""See VersionedFile.copy_to()."""
544
# copy the current index to a temp index to avoid racing with local
546
transport.put_file_non_atomic(name + INDEX_SUFFIX + '.tmp',
547
self.transport.get(self._index._filename))
549
f = self._data._open_file()
551
transport.put_file(name + DATA_SUFFIX, f)
554
# move the copied index into place
555
transport.move(name + INDEX_SUFFIX + '.tmp', name + INDEX_SUFFIX)
557
def create_empty(self, name, transport, mode=None):
558
return KnitVersionedFile(name, transport, factory=self.factory,
559
delta=self.delta, create=True)
561
def _fix_parents(self, version_id, new_parents):
562
"""Fix the parents list for version.
564
This is done by appending a new version to the index
565
with identical data except for the parents list.
566
the parents list must be a superset of the current
569
current_values = self._index._cache[version_id]
570
assert set(current_values[4]).difference(set(new_parents)) == set()
571
self._index.add_version(version_id,
573
(None, current_values[2], current_values[3]),
576
def _extract_blocks(self, version_id, source, target):
577
if self._index.get_method(version_id) != 'line-delta':
579
parent, sha1, noeol, delta = self.get_delta(version_id)
580
return KnitContent.get_line_delta_blocks(delta, source, target)
582
def get_delta(self, version_id):
583
"""Get a delta for constructing version from some other version."""
584
version_id = osutils.safe_revision_id(version_id)
585
self.check_not_reserved_id(version_id)
586
if not self.has_version(version_id):
587
raise RevisionNotPresent(version_id, self.filename)
589
parents = self.get_parents(version_id)
594
index_memo = self._index.get_position(version_id)
595
data, sha1 = self._data.read_records(((version_id, index_memo),))[version_id]
596
noeol = 'no-eol' in self._index.get_options(version_id)
597
if 'fulltext' == self._index.get_method(version_id):
598
new_content = self.factory.parse_fulltext(data, version_id)
599
if parent is not None:
600
reference_content = self._get_content(parent)
601
old_texts = reference_content.text()
604
new_texts = new_content.text()
605
delta_seq = KnitSequenceMatcher(None, old_texts, new_texts)
606
return parent, sha1, noeol, self._make_line_delta(delta_seq, new_content)
608
delta = self.factory.parse_line_delta(data, version_id)
609
return parent, sha1, noeol, delta
611
def get_graph_with_ghosts(self):
612
"""See VersionedFile.get_graph_with_ghosts()."""
613
graph_items = self._index.get_graph()
614
return dict(graph_items)
616
def get_sha1(self, version_id):
617
return self.get_sha1s([version_id])[0]
619
def get_sha1s(self, version_ids):
620
"""See VersionedFile.get_sha1()."""
621
version_ids = [osutils.safe_revision_id(v) for v in version_ids]
622
record_map = self._get_record_map(version_ids)
623
# record entry 2 is the 'digest'.
624
return [record_map[v][2] for v in version_ids]
628
"""See VersionedFile.get_suffixes()."""
629
return [DATA_SUFFIX, INDEX_SUFFIX]
631
def has_ghost(self, version_id):
632
"""True if there is a ghost reference in the file to version_id."""
633
version_id = osutils.safe_revision_id(version_id)
635
if self.has_version(version_id):
637
# optimisable if needed by memoising the _ghosts set.
638
items = self._index.get_graph()
639
for node, parents in items:
640
for parent in parents:
641
if parent not in self._index._cache:
642
if parent == version_id:
647
"""See VersionedFile.versions."""
648
if 'evil' in debug.debug_flags:
649
trace.mutter_callsite(2, "versions scales with size of history")
650
return self._index.get_versions()
652
def has_version(self, version_id):
653
"""See VersionedFile.has_version."""
654
if 'evil' in debug.debug_flags:
655
trace.mutter_callsite(2, "has_version is a LBYL scenario")
656
version_id = osutils.safe_revision_id(version_id)
657
return self._index.has_version(version_id)
659
__contains__ = has_version
661
def _merge_annotations(self, content, parents, parent_texts={},
662
delta=None, annotated=None,
663
left_matching_blocks=None):
664
"""Merge annotations for content. This is done by comparing
665
the annotations based on changed to the text.
667
if left_matching_blocks is not None:
668
delta_seq = diff._PrematchedMatcher(left_matching_blocks)
672
for parent_id in parents:
673
merge_content = self._get_content(parent_id, parent_texts)
674
if (parent_id == parents[0] and delta_seq is not None):
677
seq = patiencediff.PatienceSequenceMatcher(
678
None, merge_content.text(), content.text())
679
for i, j, n in seq.get_matching_blocks():
682
# this appears to copy (origin, text) pairs across to the
683
# new content for any line that matches the last-checked
685
content._lines[j:j+n] = merge_content._lines[i:i+n]
687
if delta_seq is None:
688
reference_content = self._get_content(parents[0], parent_texts)
689
new_texts = content.text()
690
old_texts = reference_content.text()
691
delta_seq = patiencediff.PatienceSequenceMatcher(
692
None, old_texts, new_texts)
693
return self._make_line_delta(delta_seq, content)
695
def _make_line_delta(self, delta_seq, new_content):
696
"""Generate a line delta from delta_seq and new_content."""
698
for op in delta_seq.get_opcodes():
701
diff_hunks.append((op[1], op[2], op[4]-op[3], new_content._lines[op[3]:op[4]]))
704
def _get_components_positions(self, version_ids):
705
"""Produce a map of position data for the components of versions.
707
This data is intended to be used for retrieving the knit records.
709
A dict of version_id to (method, data_pos, data_size, next) is
711
method is the way referenced data should be applied.
712
data_pos is the position of the data in the knit.
713
data_size is the size of the data in the knit.
714
next is the build-parent of the version, or None for fulltexts.
717
for version_id in version_ids:
720
while cursor is not None and cursor not in component_data:
721
method = self._index.get_method(cursor)
722
if method == 'fulltext':
725
next = self.get_parents(cursor)[0]
726
index_memo = self._index.get_position(cursor)
727
component_data[cursor] = (method, index_memo, next)
729
return component_data
731
def _get_content(self, version_id, parent_texts={}):
732
"""Returns a content object that makes up the specified
734
if not self.has_version(version_id):
735
raise RevisionNotPresent(version_id, self.filename)
737
cached_version = parent_texts.get(version_id, None)
738
if cached_version is not None:
739
return cached_version
741
text_map, contents_map = self._get_content_maps([version_id])
742
return contents_map[version_id]
744
def _check_versions_present(self, version_ids):
745
"""Check that all specified versions are present."""
746
self._index.check_versions_present(version_ids)
748
def _add_lines_with_ghosts(self, version_id, parents, lines, parent_texts):
749
"""See VersionedFile.add_lines_with_ghosts()."""
750
self._check_add(version_id, lines)
751
return self._add(version_id, lines[:], parents, self.delta, parent_texts)
753
def _add_lines(self, version_id, parents, lines, parent_texts,
754
left_matching_blocks=None):
755
"""See VersionedFile.add_lines."""
756
self._check_add(version_id, lines)
757
self._check_versions_present(parents)
758
return self._add(version_id, lines[:], parents, self.delta,
759
parent_texts, left_matching_blocks)
761
def _check_add(self, version_id, lines):
762
"""check that version_id and lines are safe to add."""
763
assert self.writable, "knit is not opened for write"
764
### FIXME escape. RBC 20060228
765
if contains_whitespace(version_id):
766
raise InvalidRevisionId(version_id, self.filename)
767
self.check_not_reserved_id(version_id)
768
if self.has_version(version_id):
769
raise RevisionAlreadyPresent(version_id, self.filename)
770
self._check_lines_not_unicode(lines)
771
self._check_lines_are_lines(lines)
773
def _add(self, version_id, lines, parents, delta, parent_texts,
774
left_matching_blocks=None):
775
"""Add a set of lines on top of version specified by parents.
777
If delta is true, compress the text as a line-delta against
780
Any versions not present will be converted into ghosts.
782
# 461 0 6546.0390 43.9100 bzrlib.knit:489(_add)
783
# +400 0 889.4890 418.9790 +bzrlib.knit:192(lower_fulltext)
784
# +461 0 1364.8070 108.8030 +bzrlib.knit:996(add_record)
785
# +461 0 193.3940 41.5720 +bzrlib.knit:898(add_version)
786
# +461 0 134.0590 18.3810 +bzrlib.osutils:361(sha_strings)
787
# +461 0 36.3420 15.4540 +bzrlib.knit:146(make)
788
# +1383 0 8.0370 8.0370 +<len>
789
# +61 0 13.5770 7.9190 +bzrlib.knit:199(lower_line_delta)
790
# +61 0 963.3470 7.8740 +bzrlib.knit:427(_get_content)
791
# +61 0 973.9950 5.2950 +bzrlib.knit:136(line_delta)
792
# +61 0 1918.1800 5.2640 +bzrlib.knit:359(_merge_annotations)
796
if parent_texts is None:
798
for parent in parents:
799
if not self.has_version(parent):
800
ghosts.append(parent)
802
present_parents.append(parent)
804
if delta and not len(present_parents):
807
digest = sha_strings(lines)
810
if lines[-1][-1] != '\n':
811
options.append('no-eol')
812
lines[-1] = lines[-1] + '\n'
814
if len(present_parents) and delta:
815
# To speed the extract of texts the delta chain is limited
816
# to a fixed number of deltas. This should minimize both
817
# I/O and the time spend applying deltas.
818
delta = self._check_should_delta(present_parents)
820
assert isinstance(version_id, str)
821
lines = self.factory.make(lines, version_id)
822
if delta or (self.factory.annotated and len(present_parents) > 0):
823
# Merge annotations from parent texts if so is needed.
824
delta_hunks = self._merge_annotations(lines, present_parents,
825
parent_texts, delta, self.factory.annotated,
826
left_matching_blocks)
829
options.append('line-delta')
830
store_lines = self.factory.lower_line_delta(delta_hunks)
832
options.append('fulltext')
833
store_lines = self.factory.lower_fulltext(lines)
835
access_memo = self._data.add_record(version_id, digest, store_lines)
836
self._index.add_version(version_id, options, access_memo, parents)
839
def check(self, progress_bar=None):
840
"""See VersionedFile.check()."""
842
def _clone_text(self, new_version_id, old_version_id, parents):
843
"""See VersionedFile.clone_text()."""
844
# FIXME RBC 20060228 make fast by only inserting an index with null
846
self.add_lines(new_version_id, parents, self.get_lines(old_version_id))
848
def get_lines(self, version_id):
849
"""See VersionedFile.get_lines()."""
850
return self.get_line_list([version_id])[0]
852
def _get_record_map(self, version_ids):
853
"""Produce a dictionary of knit records.
855
The keys are version_ids, the values are tuples of (method, content,
857
method is the way the content should be applied.
858
content is a KnitContent object.
859
digest is the SHA1 digest of this version id after all steps are done
860
next is the build-parent of the version, i.e. the leftmost ancestor.
861
If the method is fulltext, next will be None.
863
position_map = self._get_components_positions(version_ids)
864
# c = component_id, m = method, i_m = index_memo, n = next
865
records = [(c, i_m) for c, (m, i_m, n) in position_map.iteritems()]
867
for component_id, content, digest in \
868
self._data.read_records_iter(records):
869
method, index_memo, next = position_map[component_id]
870
record_map[component_id] = method, content, digest, next
874
def get_text(self, version_id):
875
"""See VersionedFile.get_text"""
876
return self.get_texts([version_id])[0]
878
def get_texts(self, version_ids):
879
return [''.join(l) for l in self.get_line_list(version_ids)]
881
def get_line_list(self, version_ids):
882
"""Return the texts of listed versions as a list of strings."""
883
version_ids = [osutils.safe_revision_id(v) for v in version_ids]
884
for version_id in version_ids:
885
self.check_not_reserved_id(version_id)
886
text_map, content_map = self._get_content_maps(version_ids)
887
return [text_map[v] for v in version_ids]
889
_get_lf_split_line_list = get_line_list
891
def _get_content_maps(self, version_ids):
892
"""Produce maps of text and KnitContents
894
:return: (text_map, content_map) where text_map contains the texts for
895
the requested versions and content_map contains the KnitContents.
896
Both dicts take version_ids as their keys.
898
for version_id in version_ids:
899
if not self.has_version(version_id):
900
raise RevisionNotPresent(version_id, self.filename)
901
record_map = self._get_record_map(version_ids)
906
for version_id in version_ids:
909
while cursor is not None:
910
method, data, digest, next = record_map[cursor]
911
components.append((cursor, method, data, digest))
912
if cursor in content_map:
917
for component_id, method, data, digest in reversed(components):
918
if component_id in content_map:
919
content = content_map[component_id]
921
if method == 'fulltext':
922
assert content is None
923
content = self.factory.parse_fulltext(data, version_id)
924
elif method == 'line-delta':
925
delta = self.factory.parse_line_delta(data, version_id)
926
content = content.copy()
927
content._lines = self._apply_delta(content._lines,
929
content_map[component_id] = content
931
if 'no-eol' in self._index.get_options(version_id):
932
content = content.copy()
933
line = content._lines[-1][1].rstrip('\n')
934
content._lines[-1] = (content._lines[-1][0], line)
935
final_content[version_id] = content
937
# digest here is the digest from the last applied component.
938
text = content.text()
939
if sha_strings(text) != digest:
940
raise KnitCorrupt(self.filename,
941
'sha-1 does not match %s' % version_id)
943
text_map[version_id] = text
944
return text_map, final_content
946
def iter_lines_added_or_present_in_versions(self, version_ids=None,
948
"""See VersionedFile.iter_lines_added_or_present_in_versions()."""
949
if version_ids is None:
950
version_ids = self.versions()
952
version_ids = [osutils.safe_revision_id(v) for v in version_ids]
954
pb = progress.DummyProgress()
955
# we don't care about inclusions, the caller cares.
956
# but we need to setup a list of records to visit.
957
# we need version_id, position, length
958
version_id_records = []
959
requested_versions = set(version_ids)
960
# filter for available versions
961
for version_id in requested_versions:
962
if not self.has_version(version_id):
963
raise RevisionNotPresent(version_id, self.filename)
964
# get a in-component-order queue:
965
for version_id in self.versions():
966
if version_id in requested_versions:
967
index_memo = self._index.get_position(version_id)
968
version_id_records.append((version_id, index_memo))
970
total = len(version_id_records)
971
for version_idx, (version_id, data, sha_value) in \
972
enumerate(self._data.read_records_iter(version_id_records)):
973
pb.update('Walking content.', version_idx, total)
974
method = self._index.get_method(version_id)
976
assert method in ('fulltext', 'line-delta')
977
if method == 'fulltext':
978
line_iterator = self.factory.get_fulltext_content(data)
980
line_iterator = self.factory.get_linedelta_content(data)
981
for line in line_iterator:
984
pb.update('Walking content.', total, total)
986
def iter_parents(self, version_ids):
987
"""Iterate through the parents for many version ids.
989
:param version_ids: An iterable yielding version_ids.
990
:return: An iterator that yields (version_id, parents). Requested
991
version_ids not present in the versioned file are simply skipped.
992
The order is undefined, allowing for different optimisations in
993
the underlying implementation.
995
version_ids = [osutils.safe_revision_id(version_id) for
996
version_id in version_ids]
997
return self._index.iter_parents(version_ids)
999
def num_versions(self):
1000
"""See VersionedFile.num_versions()."""
1001
return self._index.num_versions()
1003
__len__ = num_versions
1005
def annotate_iter(self, version_id):
1006
"""See VersionedFile.annotate_iter."""
1007
version_id = osutils.safe_revision_id(version_id)
1008
return self.factory.annotate_iter(self, version_id)
1010
def get_parents(self, version_id):
1011
"""See VersionedFile.get_parents."""
1014
# 52554 calls in 1264 872 internal down from 3674
1015
version_id = osutils.safe_revision_id(version_id)
1017
return self._index.get_parents(version_id)
1019
raise RevisionNotPresent(version_id, self.filename)
1021
def get_parents_with_ghosts(self, version_id):
1022
"""See VersionedFile.get_parents."""
1023
version_id = osutils.safe_revision_id(version_id)
1025
return self._index.get_parents_with_ghosts(version_id)
1027
raise RevisionNotPresent(version_id, self.filename)
1029
def get_ancestry(self, versions, topo_sorted=True):
1030
"""See VersionedFile.get_ancestry."""
1031
if isinstance(versions, basestring):
1032
versions = [versions]
1035
versions = [osutils.safe_revision_id(v) for v in versions]
1036
return self._index.get_ancestry(versions, topo_sorted)
1038
def get_ancestry_with_ghosts(self, versions):
1039
"""See VersionedFile.get_ancestry_with_ghosts."""
1040
if isinstance(versions, basestring):
1041
versions = [versions]
1044
versions = [osutils.safe_revision_id(v) for v in versions]
1045
return self._index.get_ancestry_with_ghosts(versions)
1047
def plan_merge(self, ver_a, ver_b):
1048
"""See VersionedFile.plan_merge."""
1049
ver_a = osutils.safe_revision_id(ver_a)
1050
ver_b = osutils.safe_revision_id(ver_b)
1051
ancestors_b = set(self.get_ancestry(ver_b, topo_sorted=False))
1053
ancestors_a = set(self.get_ancestry(ver_a, topo_sorted=False))
1054
annotated_a = self.annotate(ver_a)
1055
annotated_b = self.annotate(ver_b)
1056
return merge._plan_annotate_merge(annotated_a, annotated_b,
1057
ancestors_a, ancestors_b)
1060
class _KnitComponentFile(object):
1061
"""One of the files used to implement a knit database"""
1063
def __init__(self, transport, filename, mode, file_mode=None,
1064
create_parent_dir=False, dir_mode=None):
1065
self._transport = transport
1066
self._filename = filename
1068
self._file_mode = file_mode
1069
self._dir_mode = dir_mode
1070
self._create_parent_dir = create_parent_dir
1071
self._need_to_create = False
1073
def _full_path(self):
1074
"""Return the full path to this file."""
1075
return self._transport.base + self._filename
1077
def check_header(self, fp):
1078
line = fp.readline()
1080
# An empty file can actually be treated as though the file doesn't
1082
raise errors.NoSuchFile(self._full_path())
1083
if line != self.HEADER:
1084
raise KnitHeaderError(badline=line,
1085
filename=self._transport.abspath(self._filename))
1088
return '%s(%s)' % (self.__class__.__name__, self._filename)
1091
class _KnitIndex(_KnitComponentFile):
1092
"""Manages knit index file.
1094
The index is already kept in memory and read on startup, to enable
1095
fast lookups of revision information. The cursor of the index
1096
file is always pointing to the end, making it easy to append
1099
_cache is a cache for fast mapping from version id to a Index
1102
_history is a cache for fast mapping from indexes to version ids.
1104
The index data format is dictionary compressed when it comes to
1105
parent references; a index entry may only have parents that with a
1106
lover index number. As a result, the index is topological sorted.
1108
Duplicate entries may be written to the index for a single version id
1109
if this is done then the latter one completely replaces the former:
1110
this allows updates to correct version and parent information.
1111
Note that the two entries may share the delta, and that successive
1112
annotations and references MUST point to the first entry.
1114
The index file on disc contains a header, followed by one line per knit
1115
record. The same revision can be present in an index file more than once.
1116
The first occurrence gets assigned a sequence number starting from 0.
1118
The format of a single line is
1119
REVISION_ID FLAGS BYTE_OFFSET LENGTH( PARENT_ID|PARENT_SEQUENCE_ID)* :\n
1120
REVISION_ID is a utf8-encoded revision id
1121
FLAGS is a comma separated list of flags about the record. Values include
1122
no-eol, line-delta, fulltext.
1123
BYTE_OFFSET is the ascii representation of the byte offset in the data file
1124
that the the compressed data starts at.
1125
LENGTH is the ascii representation of the length of the data file.
1126
PARENT_ID a utf-8 revision id prefixed by a '.' that is a parent of
1128
PARENT_SEQUENCE_ID the ascii representation of the sequence number of a
1129
revision id already in the knit that is a parent of REVISION_ID.
1130
The ' :' marker is the end of record marker.
1133
when a write is interrupted to the index file, it will result in a line
1134
that does not end in ' :'. If the ' :' is not present at the end of a line,
1135
or at the end of the file, then the record that is missing it will be
1136
ignored by the parser.
1138
When writing new records to the index file, the data is preceded by '\n'
1139
to ensure that records always start on new lines even if the last write was
1140
interrupted. As a result its normal for the last line in the index to be
1141
missing a trailing newline. One can be added with no harmful effects.
1144
HEADER = "# bzr knit index 8\n"
1146
# speed of knit parsing went from 280 ms to 280 ms with slots addition.
1147
# __slots__ = ['_cache', '_history', '_transport', '_filename']
1149
def _cache_version(self, version_id, options, pos, size, parents):
1150
"""Cache a version record in the history array and index cache.
1152
This is inlined into _load_data for performance. KEEP IN SYNC.
1153
(It saves 60ms, 25% of the __init__ overhead on local 4000 record
1156
# only want the _history index to reference the 1st index entry
1158
if version_id not in self._cache:
1159
index = len(self._history)
1160
self._history.append(version_id)
1162
index = self._cache[version_id][5]
1163
self._cache[version_id] = (version_id,
1170
def __init__(self, transport, filename, mode, create=False, file_mode=None,
1171
create_parent_dir=False, delay_create=False, dir_mode=None):
1172
_KnitComponentFile.__init__(self, transport, filename, mode,
1173
file_mode=file_mode,
1174
create_parent_dir=create_parent_dir,
1177
# position in _history is the 'official' index for a revision
1178
# but the values may have come from a newer entry.
1179
# so - wc -l of a knit index is != the number of unique names
1183
fp = self._transport.get(self._filename)
1185
# _load_data may raise NoSuchFile if the target knit is
1187
_load_data(self, fp)
1191
if mode != 'w' or not create:
1194
self._need_to_create = True
1196
self._transport.put_bytes_non_atomic(
1197
self._filename, self.HEADER, mode=self._file_mode)
1199
def get_graph(self):
1200
"""Return a list of the node:parents lists from this knit index."""
1201
return [(vid, idx[4]) for vid, idx in self._cache.iteritems()]
1203
def get_ancestry(self, versions, topo_sorted=True):
1204
"""See VersionedFile.get_ancestry."""
1205
# get a graph of all the mentioned versions:
1207
pending = set(versions)
1210
version = pending.pop()
1213
parents = [p for p in cache[version][4] if p in cache]
1215
raise RevisionNotPresent(version, self._filename)
1216
# if not completed and not a ghost
1217
pending.update([p for p in parents if p not in graph])
1218
graph[version] = parents
1221
return topo_sort(graph.items())
1223
def get_ancestry_with_ghosts(self, versions):
1224
"""See VersionedFile.get_ancestry_with_ghosts."""
1225
# get a graph of all the mentioned versions:
1226
self.check_versions_present(versions)
1229
pending = set(versions)
1231
version = pending.pop()
1233
parents = cache[version][4]
1239
pending.update([p for p in parents if p not in graph])
1240
graph[version] = parents
1241
return topo_sort(graph.items())
1243
def iter_parents(self, version_ids):
1244
"""Iterate through the parents for many version ids.
1246
:param version_ids: An iterable yielding version_ids.
1247
:return: An iterator that yields (version_id, parents). Requested
1248
version_ids not present in the versioned file are simply skipped.
1249
The order is undefined, allowing for different optimisations in
1250
the underlying implementation.
1252
for version_id in version_ids:
1254
yield version_id, tuple(self.get_parents(version_id))
1258
def num_versions(self):
1259
return len(self._history)
1261
__len__ = num_versions
1263
def get_versions(self):
1264
"""Get all the versions in the file. not topologically sorted."""
1265
return self._history
1267
def _version_list_to_index(self, versions):
1270
for version in versions:
1271
if version in cache:
1272
# -- inlined lookup() --
1273
result_list.append(str(cache[version][5]))
1274
# -- end lookup () --
1276
result_list.append('.' + version)
1277
return ' '.join(result_list)
1279
def add_version(self, version_id, options, index_memo, parents):
1280
"""Add a version record to the index."""
1281
self.add_versions(((version_id, options, index_memo, parents),))
1283
def add_versions(self, versions):
1284
"""Add multiple versions to the index.
1286
:param versions: a list of tuples:
1287
(version_id, options, pos, size, parents).
1290
orig_history = self._history[:]
1291
orig_cache = self._cache.copy()
1294
for version_id, options, (index, pos, size), parents in versions:
1295
line = "\n%s %s %s %s %s :" % (version_id,
1299
self._version_list_to_index(parents))
1300
assert isinstance(line, str), \
1301
'content must be utf-8 encoded: %r' % (line,)
1303
self._cache_version(version_id, options, pos, size, parents)
1304
if not self._need_to_create:
1305
self._transport.append_bytes(self._filename, ''.join(lines))
1308
sio.write(self.HEADER)
1309
sio.writelines(lines)
1311
self._transport.put_file_non_atomic(self._filename, sio,
1312
create_parent_dir=self._create_parent_dir,
1313
mode=self._file_mode,
1314
dir_mode=self._dir_mode)
1315
self._need_to_create = False
1317
# If any problems happen, restore the original values and re-raise
1318
self._history = orig_history
1319
self._cache = orig_cache
1322
def has_version(self, version_id):
1323
"""True if the version is in the index."""
1324
return version_id in self._cache
1326
def get_position(self, version_id):
1327
"""Return details needed to access the version.
1329
.kndx indices do not support split-out data, so return None for the
1332
:return: a tuple (None, data position, size) to hand to the access
1333
logic to get the record.
1335
entry = self._cache[version_id]
1336
return None, entry[2], entry[3]
1338
def get_method(self, version_id):
1339
"""Return compression method of specified version."""
1340
options = self._cache[version_id][1]
1341
if 'fulltext' in options:
1344
if 'line-delta' not in options:
1345
raise errors.KnitIndexUnknownMethod(self._full_path(), options)
1348
def get_options(self, version_id):
1349
"""Return a string represention options.
1353
return self._cache[version_id][1]
1355
def get_parents(self, version_id):
1356
"""Return parents of specified version ignoring ghosts."""
1357
return [parent for parent in self._cache[version_id][4]
1358
if parent in self._cache]
1360
def get_parents_with_ghosts(self, version_id):
1361
"""Return parents of specified version with ghosts."""
1362
return self._cache[version_id][4]
1364
def check_versions_present(self, version_ids):
1365
"""Check that all specified versions are present."""
1367
for version_id in version_ids:
1368
if version_id not in cache:
1369
raise RevisionNotPresent(version_id, self._filename)
1372
class KnitGraphIndex(object):
1373
"""A knit index that builds on GraphIndex."""
1375
def __init__(self, graph_index, deltas=False, parents=True, add_callback=None):
1376
"""Construct a KnitGraphIndex on a graph_index.
1378
:param graph_index: An implementation of bzrlib.index.GraphIndex.
1379
:param deltas: Allow delta-compressed records.
1380
:param add_callback: If not None, allow additions to the index and call
1381
this callback with a list of added GraphIndex nodes:
1382
[(node, value, node_refs), ...]
1383
:param parents: If True, record knits parents, if not do not record
1386
self._graph_index = graph_index
1387
self._deltas = deltas
1388
self._add_callback = add_callback
1389
self._parents = parents
1390
if deltas and not parents:
1391
raise KnitCorrupt(self, "Cannot do delta compression without "
1394
def _get_entries(self, keys, check_present=False):
1395
"""Get the entries for keys.
1397
:param keys: An iterable of index keys, - 1-tuples.
1402
for node in self._graph_index.iter_entries(keys):
1404
found_keys.add(node[1])
1406
# adapt parentless index to the rest of the code.
1407
for node in self._graph_index.iter_entries(keys):
1408
yield node[0], node[1], node[2], ()
1409
found_keys.add(node[1])
1411
missing_keys = keys.difference(found_keys)
1413
raise RevisionNotPresent(missing_keys.pop(), self)
1415
def _present_keys(self, version_ids):
1417
node[1] for node in self._get_entries(version_ids)])
1419
def _parentless_ancestry(self, versions):
1420
"""Honour the get_ancestry API for parentless knit indices."""
1421
wanted_keys = self._version_ids_to_keys(versions)
1422
present_keys = self._present_keys(wanted_keys)
1423
missing = set(wanted_keys).difference(present_keys)
1425
raise RevisionNotPresent(missing.pop(), self)
1426
return list(self._keys_to_version_ids(present_keys))
1428
def get_ancestry(self, versions, topo_sorted=True):
1429
"""See VersionedFile.get_ancestry."""
1430
if not self._parents:
1431
return self._parentless_ancestry(versions)
1432
# XXX: This will do len(history) index calls - perhaps
1433
# it should be altered to be a index core feature?
1434
# get a graph of all the mentioned versions:
1437
versions = self._version_ids_to_keys(versions)
1438
pending = set(versions)
1440
# get all pending nodes
1441
this_iteration = pending
1442
new_nodes = self._get_entries(this_iteration)
1445
for (index, key, value, node_refs) in new_nodes:
1446
# dont ask for ghosties - otherwise
1447
# we we can end up looping with pending
1448
# being entirely ghosted.
1449
graph[key] = [parent for parent in node_refs[0]
1450
if parent not in ghosts]
1452
for parent in graph[key]:
1453
# dont examine known nodes again
1458
ghosts.update(this_iteration.difference(found))
1459
if versions.difference(graph):
1460
raise RevisionNotPresent(versions.difference(graph).pop(), self)
1462
result_keys = topo_sort(graph.items())
1464
result_keys = graph.iterkeys()
1465
return [key[0] for key in result_keys]
1467
def get_ancestry_with_ghosts(self, versions):
1468
"""See VersionedFile.get_ancestry."""
1469
if not self._parents:
1470
return self._parentless_ancestry(versions)
1471
# XXX: This will do len(history) index calls - perhaps
1472
# it should be altered to be a index core feature?
1473
# get a graph of all the mentioned versions:
1475
versions = self._version_ids_to_keys(versions)
1476
pending = set(versions)
1478
# get all pending nodes
1479
this_iteration = pending
1480
new_nodes = self._get_entries(this_iteration)
1482
for (index, key, value, node_refs) in new_nodes:
1483
graph[key] = node_refs[0]
1485
for parent in graph[key]:
1486
# dont examine known nodes again
1490
missing_versions = this_iteration.difference(graph)
1491
missing_needed = versions.intersection(missing_versions)
1493
raise RevisionNotPresent(missing_needed.pop(), self)
1494
for missing_version in missing_versions:
1495
# add a key, no parents
1496
graph[missing_version] = []
1497
pending.discard(missing_version) # don't look for it
1498
result_keys = topo_sort(graph.items())
1499
return [key[0] for key in result_keys]
1501
def get_graph(self):
1502
"""Return a list of the node:parents lists from this knit index."""
1503
if not self._parents:
1504
return [(key, ()) for key in self.get_versions()]
1506
for index, key, value, refs in self._graph_index.iter_all_entries():
1507
result.append((key[0], tuple([ref[0] for ref in refs[0]])))
1510
def iter_parents(self, version_ids):
1511
"""Iterate through the parents for many version ids.
1513
:param version_ids: An iterable yielding version_ids.
1514
:return: An iterator that yields (version_id, parents). Requested
1515
version_ids not present in the versioned file are simply skipped.
1516
The order is undefined, allowing for different optimisations in
1517
the underlying implementation.
1520
all_nodes = set(self._get_entries(self._version_ids_to_keys(version_ids)))
1522
present_parents = set()
1523
for node in all_nodes:
1524
all_parents.update(node[3][0])
1525
# any node we are querying must be present
1526
present_parents.add(node[1])
1527
unknown_parents = all_parents.difference(present_parents)
1528
present_parents.update(self._present_keys(unknown_parents))
1529
for node in all_nodes:
1531
for parent in node[3][0]:
1532
if parent in present_parents:
1533
parents.append(parent[0])
1534
yield node[1][0], tuple(parents)
1536
for node in self._get_entries(self._version_ids_to_keys(version_ids)):
1537
yield node[1][0], ()
1539
def num_versions(self):
1540
return len(list(self._graph_index.iter_all_entries()))
1542
__len__ = num_versions
1544
def get_versions(self):
1545
"""Get all the versions in the file. not topologically sorted."""
1546
return [node[1][0] for node in self._graph_index.iter_all_entries()]
1548
def has_version(self, version_id):
1549
"""True if the version is in the index."""
1550
return len(self._present_keys(self._version_ids_to_keys([version_id]))) == 1
1552
def _keys_to_version_ids(self, keys):
1553
return tuple(key[0] for key in keys)
1555
def get_position(self, version_id):
1556
"""Return details needed to access the version.
1558
:return: a tuple (index, data position, size) to hand to the access
1559
logic to get the record.
1561
node = self._get_node(version_id)
1562
bits = node[2][1:].split(' ')
1563
return node[0], int(bits[0]), int(bits[1])
1565
def get_method(self, version_id):
1566
"""Return compression method of specified version."""
1567
if not self._deltas:
1569
return self._parent_compression(self._get_node(version_id)[3][1])
1571
def _parent_compression(self, reference_list):
1572
# use the second reference list to decide if this is delta'd or not.
1573
if len(reference_list):
1578
def _get_node(self, version_id):
1579
return list(self._get_entries(self._version_ids_to_keys([version_id])))[0]
1581
def get_options(self, version_id):
1582
"""Return a string represention options.
1586
node = self._get_node(version_id)
1587
if not self._deltas:
1588
options = ['fulltext']
1590
options = [self._parent_compression(node[3][1])]
1591
if node[2][0] == 'N':
1592
options.append('no-eol')
1595
def get_parents(self, version_id):
1596
"""Return parents of specified version ignoring ghosts."""
1597
parents = list(self.iter_parents([version_id]))
1600
raise errors.RevisionNotPresent(version_id, self)
1601
return parents[0][1]
1603
def get_parents_with_ghosts(self, version_id):
1604
"""Return parents of specified version with ghosts."""
1605
nodes = list(self._get_entries(self._version_ids_to_keys([version_id]),
1606
check_present=True))
1607
if not self._parents:
1609
return self._keys_to_version_ids(nodes[0][3][0])
1611
def check_versions_present(self, version_ids):
1612
"""Check that all specified versions are present."""
1613
keys = self._version_ids_to_keys(version_ids)
1614
present = self._present_keys(keys)
1615
missing = keys.difference(present)
1617
raise RevisionNotPresent(missing.pop(), self)
1619
def add_version(self, version_id, options, access_memo, parents):
1620
"""Add a version record to the index."""
1621
return self.add_versions(((version_id, options, access_memo, parents),))
1623
def add_versions(self, versions):
1624
"""Add multiple versions to the index.
1626
This function does not insert data into the Immutable GraphIndex
1627
backing the KnitGraphIndex, instead it prepares data for insertion by
1628
the caller and checks that it is safe to insert then calls
1629
self._add_callback with the prepared GraphIndex nodes.
1631
:param versions: a list of tuples:
1632
(version_id, options, pos, size, parents).
1634
if not self._add_callback:
1635
raise errors.ReadOnlyError(self)
1636
# we hope there are no repositories with inconsistent parentage
1641
for (version_id, options, access_memo, parents) in versions:
1642
index, pos, size = access_memo
1643
key = (version_id, )
1644
parents = tuple((parent, ) for parent in parents)
1645
if 'no-eol' in options:
1649
value += "%d %d" % (pos, size)
1650
if not self._deltas:
1651
if 'line-delta' in options:
1652
raise KnitCorrupt(self, "attempt to add line-delta in non-delta knit")
1655
if 'line-delta' in options:
1656
node_refs = (parents, (parents[0],))
1658
node_refs = (parents, ())
1660
node_refs = (parents, )
1663
raise KnitCorrupt(self, "attempt to add node with parents "
1664
"in parentless index.")
1666
keys[key] = (value, node_refs)
1667
present_nodes = self._get_entries(keys)
1668
for (index, key, value, node_refs) in present_nodes:
1669
if (value, node_refs) != keys[key]:
1670
raise KnitCorrupt(self, "inconsistent details in add_versions"
1671
": %s %s" % ((value, node_refs), keys[key]))
1675
for key, (value, node_refs) in keys.iteritems():
1676
result.append((key, value, node_refs))
1678
for key, (value, node_refs) in keys.iteritems():
1679
result.append((key, value))
1680
self._add_callback(result)
1682
def _version_ids_to_keys(self, version_ids):
1683
return set((version_id, ) for version_id in version_ids)
1686
class _KnitAccess(object):
1687
"""Access to knit records in a .knit file."""
1689
def __init__(self, transport, filename, _file_mode, _dir_mode,
1690
_need_to_create, _create_parent_dir):
1691
"""Create a _KnitAccess for accessing and inserting data.
1693
:param transport: The transport the .knit is located on.
1694
:param filename: The filename of the .knit.
1696
self._transport = transport
1697
self._filename = filename
1698
self._file_mode = _file_mode
1699
self._dir_mode = _dir_mode
1700
self._need_to_create = _need_to_create
1701
self._create_parent_dir = _create_parent_dir
1703
def add_raw_records(self, sizes, raw_data):
1704
"""Add raw knit bytes to a storage area.
1706
The data is spooled to whereever the access method is storing data.
1708
:param sizes: An iterable containing the size of each raw data segment.
1709
:param raw_data: A bytestring containing the data.
1710
:return: A list of memos to retrieve the record later. Each memo is a
1711
tuple - (index, pos, length), where the index field is always None
1712
for the .knit access method.
1714
assert type(raw_data) == str, \
1715
'data must be plain bytes was %s' % type(raw_data)
1716
if not self._need_to_create:
1717
base = self._transport.append_bytes(self._filename, raw_data)
1719
self._transport.put_bytes_non_atomic(self._filename, raw_data,
1720
create_parent_dir=self._create_parent_dir,
1721
mode=self._file_mode,
1722
dir_mode=self._dir_mode)
1723
self._need_to_create = False
1727
result.append((None, base, size))
1732
"""IFF this data access has its own storage area, initialise it.
1736
self._transport.put_bytes_non_atomic(self._filename, '',
1737
mode=self._file_mode)
1739
def open_file(self):
1740
"""IFF this data access can be represented as a single file, open it.
1742
For knits that are not mapped to a single file on disk this will
1745
:return: None or a file handle.
1748
return self._transport.get(self._filename)
1753
def get_raw_records(self, memos_for_retrieval):
1754
"""Get the raw bytes for a records.
1756
:param memos_for_retrieval: An iterable containing the (index, pos,
1757
length) memo for retrieving the bytes. The .knit method ignores
1758
the index as there is always only a single file.
1759
:return: An iterator over the bytes of the records.
1761
read_vector = [(pos, size) for (index, pos, size) in memos_for_retrieval]
1762
for pos, data in self._transport.readv(self._filename, read_vector):
1766
class _PackAccess(object):
1767
"""Access to knit records via a collection of packs."""
1769
def __init__(self, index_to_packs, writer=None):
1770
"""Create a _PackAccess object.
1772
:param index_to_packs: A dict mapping index objects to the transport
1773
and file names for obtaining data.
1774
:param writer: A tuple (pack.ContainerWriter, write_index) which
1775
contains the pack to write, and the index that reads from it will
1779
self.container_writer = writer[0]
1780
self.write_index = writer[1]
1782
self.container_writer = None
1783
self.write_index = None
1784
self.indices = index_to_packs
1786
def add_raw_records(self, sizes, raw_data):
1787
"""Add raw knit bytes to a storage area.
1789
The data is spooled to the container writer in one bytes-record per
1792
:param sizes: An iterable containing the size of each raw data segment.
1793
:param raw_data: A bytestring containing the data.
1794
:return: A list of memos to retrieve the record later. Each memo is a
1795
tuple - (index, pos, length), where the index field is the
1796
write_index object supplied to the PackAccess object.
1798
assert type(raw_data) == str, \
1799
'data must be plain bytes was %s' % type(raw_data)
1803
p_offset, p_length = self.container_writer.add_bytes_record(
1804
raw_data[offset:offset+size], [])
1806
result.append((self.write_index, p_offset, p_length))
1810
"""Pack based knits do not get individually created."""
1812
def get_raw_records(self, memos_for_retrieval):
1813
"""Get the raw bytes for a records.
1815
:param memos_for_retrieval: An iterable containing the (index, pos,
1816
length) memo for retrieving the bytes. The Pack access method
1817
looks up the pack to use for a given record in its index_to_pack
1819
:return: An iterator over the bytes of the records.
1821
# first pass, group into same-index requests
1823
current_index = None
1824
for (index, offset, length) in memos_for_retrieval:
1825
if current_index == index:
1826
current_list.append((offset, length))
1828
if current_index is not None:
1829
request_lists.append((current_index, current_list))
1830
current_index = index
1831
current_list = [(offset, length)]
1832
# handle the last entry
1833
if current_index is not None:
1834
request_lists.append((current_index, current_list))
1835
for index, offsets in request_lists:
1836
transport, path = self.indices[index]
1837
reader = pack.make_readv_reader(transport, path, offsets)
1838
for names, read_func in reader.iter_records():
1839
yield read_func(None)
1841
def open_file(self):
1842
"""Pack based knits have no single file."""
1845
def set_writer(self, writer, index, (transport, packname)):
1846
"""Set a writer to use for adding data."""
1847
self.indices[index] = (transport, packname)
1848
self.container_writer = writer
1849
self.write_index = index
1852
class _KnitData(object):
1853
"""Manage extraction of data from a KnitAccess, caching and decompressing.
1855
The KnitData class provides the logic for parsing and using knit records,
1856
making use of an access method for the low level read and write operations.
1859
def __init__(self, access):
1860
"""Create a KnitData object.
1862
:param access: The access method to use. Access methods such as
1863
_KnitAccess manage the insertion of raw records and the subsequent
1864
retrieval of the same.
1866
self._access = access
1867
self._checked = False
1868
# TODO: jam 20060713 conceptually, this could spill to disk
1869
# if the cached size gets larger than a certain amount
1870
# but it complicates the model a bit, so for now just use
1871
# a simple dictionary
1873
self._do_cache = False
1875
def enable_cache(self):
1876
"""Enable caching of reads."""
1877
self._do_cache = True
1879
def clear_cache(self):
1880
"""Clear the record cache."""
1881
self._do_cache = False
1884
def _open_file(self):
1885
return self._access.open_file()
1887
def _record_to_data(self, version_id, digest, lines):
1888
"""Convert version_id, digest, lines into a raw data block.
1890
:return: (len, a StringIO instance with the raw data ready to read.)
1893
data_file = GzipFile(None, mode='wb', fileobj=sio)
1895
assert isinstance(version_id, str)
1896
data_file.writelines(chain(
1897
["version %s %d %s\n" % (version_id,
1901
["end %s\n" % version_id]))
1908
def add_raw_records(self, sizes, raw_data):
1909
"""Append a prepared record to the data file.
1911
:param sizes: An iterable containing the size of each raw data segment.
1912
:param raw_data: A bytestring containing the data.
1913
:return: a list of index data for the way the data was stored.
1914
See the access method add_raw_records documentation for more
1917
return self._access.add_raw_records(sizes, raw_data)
1919
def add_record(self, version_id, digest, lines):
1920
"""Write new text record to disk.
1922
Returns index data for retrieving it later, as per add_raw_records.
1924
size, sio = self._record_to_data(version_id, digest, lines)
1925
result = self.add_raw_records([size], sio.getvalue())
1927
self._cache[version_id] = sio.getvalue()
1930
def _parse_record_header(self, version_id, raw_data):
1931
"""Parse a record header for consistency.
1933
:return: the header and the decompressor stream.
1934
as (stream, header_record)
1936
df = GzipFile(mode='rb', fileobj=StringIO(raw_data))
1938
rec = self._check_header(version_id, df.readline())
1939
except Exception, e:
1940
raise KnitCorrupt(self._access,
1941
"While reading {%s} got %s(%s)"
1942
% (version_id, e.__class__.__name__, str(e)))
1945
def _check_header(self, version_id, line):
1948
raise KnitCorrupt(self._access,
1949
'unexpected number of elements in record header')
1950
if rec[1] != version_id:
1951
raise KnitCorrupt(self._access,
1952
'unexpected version, wanted %r, got %r'
1953
% (version_id, rec[1]))
1956
def _parse_record(self, version_id, data):
1958
# 4168 calls in 2880 217 internal
1959
# 4168 calls to _parse_record_header in 2121
1960
# 4168 calls to readlines in 330
1961
df = GzipFile(mode='rb', fileobj=StringIO(data))
1964
record_contents = df.readlines()
1965
except Exception, e:
1966
raise KnitCorrupt(self._access,
1967
"While reading {%s} got %s(%s)"
1968
% (version_id, e.__class__.__name__, str(e)))
1969
header = record_contents.pop(0)
1970
rec = self._check_header(version_id, header)
1972
last_line = record_contents.pop()
1973
if len(record_contents) != int(rec[2]):
1974
raise KnitCorrupt(self._access,
1975
'incorrect number of lines %s != %s'
1977
% (len(record_contents), int(rec[2]),
1979
if last_line != 'end %s\n' % rec[1]:
1980
raise KnitCorrupt(self._access,
1981
'unexpected version end line %r, wanted %r'
1982
% (last_line, version_id))
1984
return record_contents, rec[3]
1986
def read_records_iter_raw(self, records):
1987
"""Read text records from data file and yield raw data.
1989
This unpacks enough of the text record to validate the id is
1990
as expected but thats all.
1992
# setup an iterator of the external records:
1993
# uses readv so nice and fast we hope.
1995
# grab the disk data needed.
1997
# Don't check _cache if it is empty
1998
needed_offsets = [index_memo for version_id, index_memo
2000
if version_id not in self._cache]
2002
needed_offsets = [index_memo for version_id, index_memo
2005
raw_records = self._access.get_raw_records(needed_offsets)
2007
for version_id, index_memo in records:
2008
if version_id in self._cache:
2009
# This data has already been validated
2010
data = self._cache[version_id]
2012
data = raw_records.next()
2014
self._cache[version_id] = data
2016
# validate the header
2017
df, rec = self._parse_record_header(version_id, data)
2019
yield version_id, data
2021
def read_records_iter(self, records):
2022
"""Read text records from data file and yield result.
2024
The result will be returned in whatever is the fastest to read.
2025
Not by the order requested. Also, multiple requests for the same
2026
record will only yield 1 response.
2027
:param records: A list of (version_id, pos, len) entries
2028
:return: Yields (version_id, contents, digest) in the order
2029
read, not the order requested
2035
# Skip records we have alread seen
2036
yielded_records = set()
2037
needed_records = set()
2038
for record in records:
2039
if record[0] in self._cache:
2040
if record[0] in yielded_records:
2042
yielded_records.add(record[0])
2043
data = self._cache[record[0]]
2044
content, digest = self._parse_record(record[0], data)
2045
yield (record[0], content, digest)
2047
needed_records.add(record)
2048
needed_records = sorted(needed_records, key=operator.itemgetter(1))
2050
needed_records = sorted(set(records), key=operator.itemgetter(1))
2052
if not needed_records:
2055
# The transport optimizes the fetching as well
2056
# (ie, reads continuous ranges.)
2057
raw_data = self._access.get_raw_records(
2058
[index_memo for version_id, index_memo in needed_records])
2060
for (version_id, index_memo), data in \
2061
izip(iter(needed_records), raw_data):
2062
content, digest = self._parse_record(version_id, data)
2064
self._cache[version_id] = data
2065
yield version_id, content, digest
2067
def read_records(self, records):
2068
"""Read records into a dictionary."""
2070
for record_id, content, digest in \
2071
self.read_records_iter(records):
2072
components[record_id] = (content, digest)
2076
class InterKnit(InterVersionedFile):
2077
"""Optimised code paths for knit to knit operations."""
2079
_matching_file_from_factory = KnitVersionedFile
2080
_matching_file_to_factory = KnitVersionedFile
2083
def is_compatible(source, target):
2084
"""Be compatible with knits. """
2086
return (isinstance(source, KnitVersionedFile) and
2087
isinstance(target, KnitVersionedFile))
2088
except AttributeError:
2091
def join(self, pb=None, msg=None, version_ids=None, ignore_missing=False):
2092
"""See InterVersionedFile.join."""
2093
assert isinstance(self.source, KnitVersionedFile)
2094
assert isinstance(self.target, KnitVersionedFile)
2096
version_ids = self._get_source_version_ids(version_ids, ignore_missing)
2101
pb = ui.ui_factory.nested_progress_bar()
2103
version_ids = list(version_ids)
2104
if None in version_ids:
2105
version_ids.remove(None)
2107
self.source_ancestry = set(self.source.get_ancestry(version_ids))
2108
this_versions = set(self.target._index.get_versions())
2109
needed_versions = self.source_ancestry - this_versions
2110
cross_check_versions = self.source_ancestry.intersection(this_versions)
2111
mismatched_versions = set()
2112
for version in cross_check_versions:
2113
# scan to include needed parents.
2114
n1 = set(self.target.get_parents_with_ghosts(version))
2115
n2 = set(self.source.get_parents_with_ghosts(version))
2117
# FIXME TEST this check for cycles being introduced works
2118
# the logic is we have a cycle if in our graph we are an
2119
# ancestor of any of the n2 revisions.
2125
parent_ancestors = self.source.get_ancestry(parent)
2126
if version in parent_ancestors:
2127
raise errors.GraphCycleError([parent, version])
2128
# ensure this parent will be available later.
2129
new_parents = n2.difference(n1)
2130
needed_versions.update(new_parents.difference(this_versions))
2131
mismatched_versions.add(version)
2133
if not needed_versions and not mismatched_versions:
2135
full_list = topo_sort(self.source.get_graph())
2137
version_list = [i for i in full_list if (not self.target.has_version(i)
2138
and i in needed_versions)]
2142
copy_queue_records = []
2144
for version_id in version_list:
2145
options = self.source._index.get_options(version_id)
2146
parents = self.source._index.get_parents_with_ghosts(version_id)
2147
# check that its will be a consistent copy:
2148
for parent in parents:
2149
# if source has the parent, we must :
2150
# * already have it or
2151
# * have it scheduled already
2152
# otherwise we don't care
2153
assert (self.target.has_version(parent) or
2154
parent in copy_set or
2155
not self.source.has_version(parent))
2156
index_memo = self.source._index.get_position(version_id)
2157
copy_queue_records.append((version_id, index_memo))
2158
copy_queue.append((version_id, options, parents))
2159
copy_set.add(version_id)
2161
# data suck the join:
2163
total = len(version_list)
2166
for (version_id, raw_data), \
2167
(version_id2, options, parents) in \
2168
izip(self.source._data.read_records_iter_raw(copy_queue_records),
2170
assert version_id == version_id2, 'logic error, inconsistent results'
2172
pb.update("Joining knit", count, total)
2173
raw_records.append((version_id, options, parents, len(raw_data)))
2174
raw_datum.append(raw_data)
2175
self.target._add_raw_records(raw_records, ''.join(raw_datum))
2177
for version in mismatched_versions:
2178
# FIXME RBC 20060309 is this needed?
2179
n1 = set(self.target.get_parents_with_ghosts(version))
2180
n2 = set(self.source.get_parents_with_ghosts(version))
2181
# write a combined record to our history preserving the current
2182
# parents as first in the list
2183
new_parents = self.target.get_parents_with_ghosts(version) + list(n2.difference(n1))
2184
self.target.fix_parents(version, new_parents)
2190
InterVersionedFile.register_optimiser(InterKnit)
2193
class WeaveToKnit(InterVersionedFile):
2194
"""Optimised code paths for weave to knit operations."""
2196
_matching_file_from_factory = bzrlib.weave.WeaveFile
2197
_matching_file_to_factory = KnitVersionedFile
2200
def is_compatible(source, target):
2201
"""Be compatible with weaves to knits."""
2203
return (isinstance(source, bzrlib.weave.Weave) and
2204
isinstance(target, KnitVersionedFile))
2205
except AttributeError:
2208
def join(self, pb=None, msg=None, version_ids=None, ignore_missing=False):
2209
"""See InterVersionedFile.join."""
2210
assert isinstance(self.source, bzrlib.weave.Weave)
2211
assert isinstance(self.target, KnitVersionedFile)
2213
version_ids = self._get_source_version_ids(version_ids, ignore_missing)
2218
pb = ui.ui_factory.nested_progress_bar()
2220
version_ids = list(version_ids)
2222
self.source_ancestry = set(self.source.get_ancestry(version_ids))
2223
this_versions = set(self.target._index.get_versions())
2224
needed_versions = self.source_ancestry - this_versions
2225
cross_check_versions = self.source_ancestry.intersection(this_versions)
2226
mismatched_versions = set()
2227
for version in cross_check_versions:
2228
# scan to include needed parents.
2229
n1 = set(self.target.get_parents_with_ghosts(version))
2230
n2 = set(self.source.get_parents(version))
2231
# if all of n2's parents are in n1, then its fine.
2232
if n2.difference(n1):
2233
# FIXME TEST this check for cycles being introduced works
2234
# the logic is we have a cycle if in our graph we are an
2235
# ancestor of any of the n2 revisions.
2241
parent_ancestors = self.source.get_ancestry(parent)
2242
if version in parent_ancestors:
2243
raise errors.GraphCycleError([parent, version])
2244
# ensure this parent will be available later.
2245
new_parents = n2.difference(n1)
2246
needed_versions.update(new_parents.difference(this_versions))
2247
mismatched_versions.add(version)
2249
if not needed_versions and not mismatched_versions:
2251
full_list = topo_sort(self.source.get_graph())
2253
version_list = [i for i in full_list if (not self.target.has_version(i)
2254
and i in needed_versions)]
2258
total = len(version_list)
2259
for version_id in version_list:
2260
pb.update("Converting to knit", count, total)
2261
parents = self.source.get_parents(version_id)
2262
# check that its will be a consistent copy:
2263
for parent in parents:
2264
# if source has the parent, we must already have it
2265
assert (self.target.has_version(parent))
2266
self.target.add_lines(
2267
version_id, parents, self.source.get_lines(version_id))
2270
for version in mismatched_versions:
2271
# FIXME RBC 20060309 is this needed?
2272
n1 = set(self.target.get_parents_with_ghosts(version))
2273
n2 = set(self.source.get_parents(version))
2274
# write a combined record to our history preserving the current
2275
# parents as first in the list
2276
new_parents = self.target.get_parents_with_ghosts(version) + list(n2.difference(n1))
2277
self.target.fix_parents(version, new_parents)
2283
InterVersionedFile.register_optimiser(WeaveToKnit)
2286
class KnitSequenceMatcher(difflib.SequenceMatcher):
2287
"""Knit tuned sequence matcher.
2289
This is based on profiling of difflib which indicated some improvements
2290
for our usage pattern.
2293
def find_longest_match(self, alo, ahi, blo, bhi):
2294
"""Find longest matching block in a[alo:ahi] and b[blo:bhi].
2296
If isjunk is not defined:
2298
Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where
2299
alo <= i <= i+k <= ahi
2300
blo <= j <= j+k <= bhi
2301
and for all (i',j',k') meeting those conditions,
2304
and if i == i', j <= j'
2306
In other words, of all maximal matching blocks, return one that
2307
starts earliest in a, and of all those maximal matching blocks that
2308
start earliest in a, return the one that starts earliest in b.
2310
>>> s = SequenceMatcher(None, " abcd", "abcd abcd")
2311
>>> s.find_longest_match(0, 5, 0, 9)
2314
If isjunk is defined, first the longest matching block is
2315
determined as above, but with the additional restriction that no
2316
junk element appears in the block. Then that block is extended as
2317
far as possible by matching (only) junk elements on both sides. So
2318
the resulting block never matches on junk except as identical junk
2319
happens to be adjacent to an "interesting" match.
2321
Here's the same example as before, but considering blanks to be
2322
junk. That prevents " abcd" from matching the " abcd" at the tail
2323
end of the second sequence directly. Instead only the "abcd" can
2324
match, and matches the leftmost "abcd" in the second sequence:
2326
>>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
2327
>>> s.find_longest_match(0, 5, 0, 9)
2330
If no blocks match, return (alo, blo, 0).
2332
>>> s = SequenceMatcher(None, "ab", "c")
2333
>>> s.find_longest_match(0, 2, 0, 1)
2337
# CAUTION: stripping common prefix or suffix would be incorrect.
2341
# Longest matching block is "ab", but if common prefix is
2342
# stripped, it's "a" (tied with "b"). UNIX(tm) diff does so
2343
# strip, so ends up claiming that ab is changed to acab by
2344
# inserting "ca" in the middle. That's minimal but unintuitive:
2345
# "it's obvious" that someone inserted "ac" at the front.
2346
# Windiff ends up at the same place as diff, but by pairing up
2347
# the unique 'b's and then matching the first two 'a's.
2349
a, b, b2j, isbjunk = self.a, self.b, self.b2j, self.isbjunk
2350
besti, bestj, bestsize = alo, blo, 0
2351
# find longest junk-free match
2352
# during an iteration of the loop, j2len[j] = length of longest
2353
# junk-free match ending with a[i-1] and b[j]
2357
for i in xrange(alo, ahi):
2358
# look at all instances of a[i] in b; note that because
2359
# b2j has no junk keys, the loop is skipped if a[i] is junk
2360
j2lenget = j2len.get
2363
# changing b2j.get(a[i], nothing) to a try:KeyError pair produced the
2364
# following improvement
2365
# 704 0 4650.5320 2620.7410 bzrlib.knit:1336(find_longest_match)
2366
# +326674 0 1655.1210 1655.1210 +<method 'get' of 'dict' objects>
2367
# +76519 0 374.6700 374.6700 +<method 'has_key' of 'dict' objects>
2369
# 704 0 3733.2820 2209.6520 bzrlib.knit:1336(find_longest_match)
2370
# +211400 0 1147.3520 1147.3520 +<method 'get' of 'dict' objects>
2371
# +76519 0 376.2780 376.2780 +<method 'has_key' of 'dict' objects>
2383
k = newj2len[j] = 1 + j2lenget(-1 + j, 0)
2385
besti, bestj, bestsize = 1 + i-k, 1 + j-k, k
2388
# Extend the best by non-junk elements on each end. In particular,
2389
# "popular" non-junk elements aren't in b2j, which greatly speeds
2390
# the inner loop above, but also means "the best" match so far
2391
# doesn't contain any junk *or* popular non-junk elements.
2392
while besti > alo and bestj > blo and \
2393
not isbjunk(b[bestj-1]) and \
2394
a[besti-1] == b[bestj-1]:
2395
besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
2396
while besti+bestsize < ahi and bestj+bestsize < bhi and \
2397
not isbjunk(b[bestj+bestsize]) and \
2398
a[besti+bestsize] == b[bestj+bestsize]:
2401
# Now that we have a wholly interesting match (albeit possibly
2402
# empty!), we may as well suck up the matching junk on each
2403
# side of it too. Can't think of a good reason not to, and it
2404
# saves post-processing the (possibly considerable) expense of
2405
# figuring out what to do with it. In the case of an empty
2406
# interesting match, this is clearly the right thing to do,
2407
# because no other kind of match is possible in the regions.
2408
while besti > alo and bestj > blo and \
2409
isbjunk(b[bestj-1]) and \
2410
a[besti-1] == b[bestj-1]:
2411
besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
2412
while besti+bestsize < ahi and bestj+bestsize < bhi and \
2413
isbjunk(b[bestj+bestsize]) and \
2414
a[besti+bestsize] == b[bestj+bestsize]:
2415
bestsize = bestsize + 1
2417
return besti, bestj, bestsize
2420
def annotate_knit(knit, revision_id):
2421
"""Annotate a knit with no cached annotations.
2423
This implementation is for knits with no cached annotations.
2424
It will work for knits with cached annotations, but this is not
2427
ancestry = knit.get_ancestry(revision_id, topo_sorted=False)
2428
fulltext = dict(zip(ancestry, knit.get_line_list(ancestry)))
2430
pending_annotation = [revision_id]
2431
while len(pending_annotation) > 0:
2432
candidate = pending_annotation.pop()
2433
if candidate in annotations:
2435
parents = knit.get_parents(candidate)
2436
pending_parents = [p for p in parents if p not in annotations]
2437
if len(pending_parents) > 0:
2438
pending_annotation.append(candidate)
2439
pending_annotation.extend(pending_parents)
2441
if len(parents) == 0:
2443
elif knit._index.get_method(candidate) != 'line-delta':
2446
parent, sha1, noeol, delta = knit.get_delta(candidate)
2447
blocks = KnitContent.get_line_delta_blocks(delta,
2448
fulltext[parents[0]], fulltext[candidate])
2449
annotations[candidate] = list(annotate.reannotate([annotations[p]
2450
for p in parents], fulltext[candidate], candidate, blocks))
2451
return iter(annotations[revision_id])
2455
from bzrlib._knit_load_data_c import _load_data_c as _load_data
2457
from bzrlib._knit_load_data_py import _load_data_py as _load_data