1
# Copyright (C) 2005, 2006 by Canonical Ltd
2
# Written by Martin Pool.
3
# Modified by Johan Rydberg <jrydberg@gnu.org>
4
# Modified by Robert Collins <robert.collins@canonical.com>
5
# Modified by Aaron Bentley <aaron.bentley@utoronto.ca>
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
"""Knit versionedfile implementation.
23
A knit is a versioned file implementation that supports efficient append only
27
lifeless: the data file is made up of "delta records". each delta record has a delta header
28
that contains; (1) a version id, (2) the size of the delta (in lines), and (3) the digest of
29
the -expanded data- (ie, the delta applied to the parent). the delta also ends with a
30
end-marker; simply "end VERSION"
32
delta can be line or full contents.a
33
... the 8's there are the index number of the annotation.
34
version robertc@robertcollins.net-20051003014215-ee2990904cc4c7ad 7 c7d23b2a5bd6ca00e8e266cec0ec228158ee9f9e
38
8 e.set('executable', 'yes')
40
8 if elt.get('executable') == 'yes':
41
8 ie.executable = True
42
end robertc@robertcollins.net-20051003014215-ee2990904cc4c7ad
46
09:33 < jrydberg> lifeless: each index is made up of a tuple of; version id, options, position, size, parents
47
09:33 < jrydberg> lifeless: the parents are currently dictionary compressed
48
09:33 < jrydberg> lifeless: (meaning it currently does not support ghosts)
49
09:33 < lifeless> right
50
09:33 < jrydberg> lifeless: the position and size is the range in the data file
53
so the index sequence is the dictionary compressed sequence number used
54
in the deltas to provide line annotation
59
# 10:16 < lifeless> make partial index writes safe
60
# 10:16 < lifeless> implement 'knit.check()' like weave.check()
61
# 10:17 < lifeless> record known ghosts so we can detect when they are filled in rather than the current 'reweave
63
# move sha1 out of the content so that join is faster at verifying parents
64
# record content length ?
68
from cStringIO import StringIO
70
from itertools import izip, chain
75
import bzrlib.errors as errors
76
from bzrlib.errors import FileExists, NoSuchFile, KnitError, \
77
InvalidRevisionId, KnitCorrupt, KnitHeaderError, \
78
RevisionNotPresent, RevisionAlreadyPresent
79
from bzrlib.tuned_gzip import *
80
from bzrlib.trace import mutter
81
from bzrlib.osutils import contains_whitespace, contains_linebreaks, \
83
from bzrlib.versionedfile import VersionedFile, InterVersionedFile
84
from bzrlib.tsort import topo_sort
88
# TODO: Split out code specific to this format into an associated object.
90
# TODO: Can we put in some kind of value to check that the index and data
91
# files belong together?
93
# TODO: accommodate binaries, perhaps by storing a byte count
95
# TODO: function to check whole file
97
# TODO: atomically append data, then measure backwards from the cursor
98
# position after writing to work out where it was located. we may need to
99
# bypass python file buffering.
101
DATA_SUFFIX = '.knit'
102
INDEX_SUFFIX = '.kndx'
105
class KnitContent(object):
106
"""Content of a knit version to which deltas can be applied."""
108
def __init__(self, lines):
111
def annotate_iter(self):
112
"""Yield tuples of (origin, text) for each content line."""
113
for origin, text in self._lines:
117
"""Return a list of (origin, text) tuples."""
118
return list(self.annotate_iter())
120
def line_delta_iter(self, new_lines):
121
"""Generate line-based delta from this content to new_lines."""
122
new_texts = [text for origin, text in new_lines._lines]
123
old_texts = [text for origin, text in self._lines]
124
s = KnitSequenceMatcher(None, old_texts, new_texts)
125
for op in s.get_opcodes():
128
# ofrom oto length data
129
yield (op[1], op[2], op[4]-op[3], new_lines._lines[op[3]:op[4]])
131
def line_delta(self, new_lines):
132
return list(self.line_delta_iter(new_lines))
135
return [text for origin, text in self._lines]
138
class _KnitFactory(object):
139
"""Base factory for creating content objects."""
141
def make(self, lines, version):
142
num_lines = len(lines)
143
return KnitContent(zip([version] * num_lines, lines))
146
class KnitAnnotateFactory(_KnitFactory):
147
"""Factory for creating annotated Content objects."""
151
def parse_fulltext(self, content, version):
152
"""Convert fulltext to internal representation
154
fulltext content is of the format
155
revid(utf8) plaintext\n
156
internal representation is of the format:
161
origin, text = line.split(' ', 1)
162
lines.append((origin.decode('utf-8'), text))
163
return KnitContent(lines)
165
def parse_line_delta_iter(self, lines):
166
for result_item in self.parse_line_delta[lines]:
169
def parse_line_delta(self, lines, version):
170
"""Convert a line based delta into internal representation.
172
line delta is in the form of:
173
intstart intend intcount
175
revid(utf8) newline\n
176
internal representation is
177
(start, end, count, [1..count tuples (revid, newline)])
182
# walk through the lines parsing.
184
start, end, count = [int(n) for n in header.split(',')]
188
origin, text = next().split(' ', 1)
190
contents.append((origin.decode('utf-8'), text))
191
result.append((start, end, count, contents))
194
def lower_fulltext(self, content):
195
"""convert a fulltext content record into a serializable form.
197
see parse_fulltext which this inverts.
199
return ['%s %s' % (o.encode('utf-8'), t) for o, t in content._lines]
201
def lower_line_delta(self, delta):
202
"""convert a delta into a serializable form.
204
See parse_line_delta which this inverts.
207
for start, end, c, lines in delta:
208
out.append('%d,%d,%d\n' % (start, end, c))
209
for origin, text in lines:
210
out.append('%s %s' % (origin.encode('utf-8'), text))
214
class KnitPlainFactory(_KnitFactory):
215
"""Factory for creating plain Content objects."""
219
def parse_fulltext(self, content, version):
220
"""This parses an unannotated fulltext.
222
Note that this is not a noop - the internal representation
223
has (versionid, line) - its just a constant versionid.
225
return self.make(content, version)
227
def parse_line_delta_iter(self, lines, version):
229
header = lines.pop(0)
230
start, end, c = [int(n) for n in header.split(',')]
231
yield start, end, c, zip([version] * c, lines[:c])
234
def parse_line_delta(self, lines, version):
235
return list(self.parse_line_delta_iter(lines, version))
237
def lower_fulltext(self, content):
238
return content.text()
240
def lower_line_delta(self, delta):
242
for start, end, c, lines in delta:
243
out.append('%d,%d,%d\n' % (start, end, c))
244
out.extend([text for origin, text in lines])
248
def make_empty_knit(transport, relpath):
249
"""Construct a empty knit at the specified location."""
250
k = KnitVersionedFile(transport, relpath, 'w', KnitPlainFactory)
254
class KnitVersionedFile(VersionedFile):
255
"""Weave-like structure with faster random access.
257
A knit stores a number of texts and a summary of the relationships
258
between them. Texts are identified by a string version-id. Texts
259
are normally stored and retrieved as a series of lines, but can
260
also be passed as single strings.
262
Lines are stored with the trailing newline (if any) included, to
263
avoid special cases for files with no final newline. Lines are
264
composed of 8-bit characters, not unicode. The combination of
265
these approaches should mean any 'binary' file can be safely
266
stored and retrieved.
269
def __init__(self, relpath, transport, file_mode=None, access_mode=None,
270
factory=None, basis_knit=None, delta=True, create=False):
271
"""Construct a knit at location specified by relpath.
273
:param create: If not True, only open an existing knit.
275
if access_mode is None:
277
super(KnitVersionedFile, self).__init__(access_mode)
278
assert access_mode in ('r', 'w'), "invalid mode specified %r" % access_mode
279
assert not basis_knit or isinstance(basis_knit, KnitVersionedFile), \
282
self.transport = transport
283
self.filename = relpath
284
self.basis_knit = basis_knit
285
self.factory = factory or KnitAnnotateFactory()
286
self.writable = (access_mode == 'w')
289
self._index = _KnitIndex(transport, relpath + INDEX_SUFFIX,
290
access_mode, create=create, file_mode=file_mode)
291
self._data = _KnitData(transport, relpath + DATA_SUFFIX,
292
access_mode, create=create and not len(self), file_mode=file_mode)
295
return '%s(%s)' % (self.__class__.__name__,
296
self.transport.abspath(self.filename))
298
def _add_delta(self, version_id, parents, delta_parent, sha1, noeol, delta):
299
"""See VersionedFile._add_delta()."""
300
self._check_add(version_id, []) # should we check the lines ?
301
self._check_versions_present(parents)
305
for parent in parents:
306
if not self.has_version(parent):
307
ghosts.append(parent)
309
present_parents.append(parent)
311
if delta_parent is None:
312
# reconstitute as full text.
313
assert len(delta) == 1 or len(delta) == 0
315
assert delta[0][0] == 0
316
assert delta[0][1] == 0, delta[0][1]
317
return super(KnitVersionedFile, self)._add_delta(version_id,
328
options.append('no-eol')
330
if delta_parent is not None:
331
# determine the current delta chain length.
332
# To speed the extract of texts the delta chain is limited
333
# to a fixed number of deltas. This should minimize both
334
# I/O and the time spend applying deltas.
336
delta_parents = [delta_parent]
338
parent = delta_parents[0]
339
method = self._index.get_method(parent)
340
if method == 'fulltext':
342
delta_parents = self._index.get_parents(parent)
344
if method == 'line-delta':
345
# did not find a fulltext in the delta limit.
346
# just do a normal insertion.
347
return super(KnitVersionedFile, self)._add_delta(version_id,
354
options.append('line-delta')
355
store_lines = self.factory.lower_line_delta(delta)
357
where, size = self._data.add_record(version_id, digest, store_lines)
358
self._index.add_version(version_id, options, where, size, parents)
360
def _add_raw_records(self, records, data):
361
"""Add all the records 'records' with data pre-joined in 'data'.
363
:param records: A list of tuples(version_id, options, parents, size).
364
:param data: The data for the records. When it is written, the records
365
are adjusted to have pos pointing into data by the sum of
366
the preceding records sizes.
369
pos = self._data.add_raw_record(data)
371
for (version_id, options, parents, size) in records:
372
index_entries.append((version_id, options, pos, size, parents))
374
self._index.add_versions(index_entries)
376
def clear_cache(self):
377
"""Clear the data cache only."""
378
self._data.clear_cache()
380
def copy_to(self, name, transport):
381
"""See VersionedFile.copy_to()."""
382
# copy the current index to a temp index to avoid racing with local
384
transport.put(name + INDEX_SUFFIX + '.tmp', self.transport.get(self._index._filename),)
386
transport.put(name + DATA_SUFFIX, self._data._open_file())
387
# rename the copied index into place
388
transport.rename(name + INDEX_SUFFIX + '.tmp', name + INDEX_SUFFIX)
390
def create_empty(self, name, transport, mode=None):
391
return KnitVersionedFile(name, transport, factory=self.factory, delta=self.delta, create=True)
393
def _fix_parents(self, version, new_parents):
394
"""Fix the parents list for version.
396
This is done by appending a new version to the index
397
with identical data except for the parents list.
398
the parents list must be a superset of the current
401
current_values = self._index._cache[version]
402
assert set(current_values[4]).difference(set(new_parents)) == set()
403
self._index.add_version(version,
409
def get_delta(self, version_id):
410
"""Get a delta for constructing version from some other version."""
411
if not self.has_version(version_id):
412
raise RevisionNotPresent(version_id, self.filename)
414
parents = self.get_parents(version_id)
419
data_pos, data_size = self._index.get_position(version_id)
420
data, sha1 = self._data.read_records(((version_id, data_pos, data_size),))[version_id]
421
version_idx = self._index.lookup(version_id)
422
noeol = 'no-eol' in self._index.get_options(version_id)
423
if 'fulltext' == self._index.get_method(version_id):
424
new_content = self.factory.parse_fulltext(data, version_idx)
425
if parent is not None:
426
reference_content = self._get_content(parent)
427
old_texts = reference_content.text()
430
new_texts = new_content.text()
431
delta_seq = KnitSequenceMatcher(None, old_texts, new_texts)
432
return parent, sha1, noeol, self._make_line_delta(delta_seq, new_content)
434
delta = self.factory.parse_line_delta(data, version_idx)
435
return parent, sha1, noeol, delta
437
def get_graph_with_ghosts(self):
438
"""See VersionedFile.get_graph_with_ghosts()."""
439
graph_items = self._index.get_graph()
440
return dict(graph_items)
442
def get_sha1(self, version_id):
443
"""See VersionedFile.get_sha1()."""
444
components = self._get_components(version_id)
445
return components[-1][-1][-1]
449
"""See VersionedFile.get_suffixes()."""
450
return [DATA_SUFFIX, INDEX_SUFFIX]
452
def has_ghost(self, version_id):
453
"""True if there is a ghost reference in the file to version_id."""
455
if self.has_version(version_id):
457
# optimisable if needed by memoising the _ghosts set.
458
items = self._index.get_graph()
459
for node, parents in items:
460
for parent in parents:
461
if parent not in self._index._cache:
462
if parent == version_id:
467
"""See VersionedFile.versions."""
468
return self._index.get_versions()
470
def has_version(self, version_id):
471
"""See VersionedFile.has_version."""
472
return self._index.has_version(version_id)
474
__contains__ = has_version
476
def _merge_annotations(self, content, parents, parent_texts={},
477
delta=None, annotated=None):
478
"""Merge annotations for content. This is done by comparing
479
the annotations based on changed to the text.
483
for parent_id in parents:
484
merge_content = self._get_content(parent_id, parent_texts)
485
seq = KnitSequenceMatcher(None, merge_content.text(), content.text())
486
if delta_seq is None:
487
# setup a delta seq to reuse.
489
for i, j, n in seq.get_matching_blocks():
492
# this appears to copy (origin, text) pairs across to the new
493
# content for any line that matches the last-checked parent.
494
# FIXME: save the sequence control data for delta compression
495
# against the most relevant parent rather than rediffing.
496
content._lines[j:j+n] = merge_content._lines[i:i+n]
499
reference_content = self._get_content(parents[0], parent_texts)
500
new_texts = content.text()
501
old_texts = reference_content.text()
502
delta_seq = KnitSequenceMatcher(None, old_texts, new_texts)
503
return self._make_line_delta(delta_seq, content)
505
def _make_line_delta(self, delta_seq, new_content):
506
"""Generate a line delta from delta_seq and new_content."""
508
for op in delta_seq.get_opcodes():
511
diff_hunks.append((op[1], op[2], op[4]-op[3], new_content._lines[op[3]:op[4]]))
514
def _get_component_versions(self, version_id):
515
basis = self.basis_knit
522
if basis and basis._index.has_version(cursor):
524
basis_versions.append(cursor)
525
method = picked_knit._index.get_method(cursor)
526
needed_versions.append((method, cursor))
527
if method == 'fulltext':
529
cursor = picked_knit.get_parents(cursor)[0]
530
return needed_versions, basis_versions
532
def _get_component_positions(self, version_id):
533
needed_versions, basis_versions = \
534
self._get_component_versions(version_id)
535
assert len(basis_versions) == 0
537
for method, comp_id in needed_versions:
538
data_pos, data_size = self._index.get_position(comp_id)
539
positions.append((method, comp_id, data_pos, data_size))
542
def _get_components(self, version_id):
543
"""Return a list of (version_id, method, data) tuples that
544
makes up version specified by version_id of the knit.
546
The components should be applied in the order of the returned
549
The basis knit will be used to the largest extent possible
550
since it is assumed that accesses to it is faster.
553
# 4168 calls in 14912, 2289 internal
554
# 4168 in 9711 to read_records
555
# 52554 in 1250 to get_parents
556
# 170166 in 865 to list.append
558
# needed_revisions holds a list of (method, version_id) of
559
# versions that is needed to be fetched to construct the final
560
# version of the file.
562
# basis_revisions is a list of versions that needs to be
563
# fetched but exists in the basis knit.
565
needed_versions, basis_versions = \
566
self._get_component_versions(version_id)
570
assert True, "I am broken"
571
basis = self.basis_knit
573
for comp_id in basis_versions:
574
data_pos, data_size = basis._index.get_data_position(comp_id)
575
records.append((piece_id, data_pos, data_size))
576
components.update(basis._data.read_records(records))
579
for comp_id in [vid for method, vid in needed_versions
580
if vid not in basis_versions]:
581
data_pos, data_size = self._index.get_position(comp_id)
582
records.append((comp_id, data_pos, data_size))
583
components.update(self._data.read_records(records))
585
# get_data_records returns a mapping with the version id as
586
# index and the value as data. The order the components need
587
# to be applied is held by needed_versions (reversed).
589
for method, comp_id in reversed(needed_versions):
590
out.append((comp_id, method, components[comp_id]))
594
def _get_content(self, version_id, parent_texts={}):
595
"""Returns a content object that makes up the specified
597
if not self.has_version(version_id):
598
raise RevisionNotPresent(version_id, self.filename)
600
cached_version = parent_texts.get(version_id, None)
601
if cached_version is not None:
602
return cached_version
604
if self.basis_knit and version_id in self.basis_knit:
605
return self.basis_knit._get_content(version_id)
608
components = self._get_components(version_id)
609
for component_id, method, (data, digest) in components:
610
version_idx = self._index.lookup(component_id)
611
if method == 'fulltext':
612
assert content is None
613
content = self.factory.parse_fulltext(data, version_idx)
614
elif method == 'line-delta':
615
delta = self.factory.parse_line_delta(data, version_idx)
616
content._lines = self._apply_delta(content._lines, delta)
618
if 'no-eol' in self._index.get_options(version_id):
619
line = content._lines[-1][1].rstrip('\n')
620
content._lines[-1] = (content._lines[-1][0], line)
622
# digest here is the digest from the last applied component.
623
if sha_strings(content.text()) != digest:
624
raise KnitCorrupt(self.filename, 'sha-1 does not match %s' % version_id)
628
def _check_versions_present(self, version_ids):
629
"""Check that all specified versions are present."""
630
version_ids = set(version_ids)
631
for r in list(version_ids):
632
if self._index.has_version(r):
633
version_ids.remove(r)
635
raise RevisionNotPresent(list(version_ids)[0], self.filename)
637
def _add_lines_with_ghosts(self, version_id, parents, lines, parent_texts):
638
"""See VersionedFile.add_lines_with_ghosts()."""
639
self._check_add(version_id, lines)
640
return self._add(version_id, lines[:], parents, self.delta, parent_texts)
642
def _add_lines(self, version_id, parents, lines, parent_texts):
643
"""See VersionedFile.add_lines."""
644
self._check_add(version_id, lines)
645
self._check_versions_present(parents)
646
return self._add(version_id, lines[:], parents, self.delta, parent_texts)
648
def _check_add(self, version_id, lines):
649
"""check that version_id and lines are safe to add."""
650
assert self.writable, "knit is not opened for write"
651
### FIXME escape. RBC 20060228
652
if contains_whitespace(version_id):
653
raise InvalidRevisionId(version_id, self.filename)
654
if self.has_version(version_id):
655
raise RevisionAlreadyPresent(version_id, self.filename)
656
self._check_lines_not_unicode(lines)
657
self._check_lines_are_lines(lines)
659
def _add(self, version_id, lines, parents, delta, parent_texts):
660
"""Add a set of lines on top of version specified by parents.
662
If delta is true, compress the text as a line-delta against
665
Any versions not present will be converted into ghosts.
667
# 461 0 6546.0390 43.9100 bzrlib.knit:489(_add)
668
# +400 0 889.4890 418.9790 +bzrlib.knit:192(lower_fulltext)
669
# +461 0 1364.8070 108.8030 +bzrlib.knit:996(add_record)
670
# +461 0 193.3940 41.5720 +bzrlib.knit:898(add_version)
671
# +461 0 134.0590 18.3810 +bzrlib.osutils:361(sha_strings)
672
# +461 0 36.3420 15.4540 +bzrlib.knit:146(make)
673
# +1383 0 8.0370 8.0370 +<len>
674
# +61 0 13.5770 7.9190 +bzrlib.knit:199(lower_line_delta)
675
# +61 0 963.3470 7.8740 +bzrlib.knit:427(_get_content)
676
# +61 0 973.9950 5.2950 +bzrlib.knit:136(line_delta)
677
# +61 0 1918.1800 5.2640 +bzrlib.knit:359(_merge_annotations)
681
if parent_texts is None:
683
for parent in parents:
684
if not self.has_version(parent):
685
ghosts.append(parent)
687
present_parents.append(parent)
689
if delta and not len(present_parents):
692
digest = sha_strings(lines)
695
if lines[-1][-1] != '\n':
696
options.append('no-eol')
697
lines[-1] = lines[-1] + '\n'
699
if len(present_parents) and delta:
700
# To speed the extract of texts the delta chain is limited
701
# to a fixed number of deltas. This should minimize both
702
# I/O and the time spend applying deltas.
704
delta_parents = present_parents
706
parent = delta_parents[0]
707
method = self._index.get_method(parent)
708
if method == 'fulltext':
710
delta_parents = self._index.get_parents(parent)
712
if method == 'line-delta':
715
lines = self.factory.make(lines, version_id)
716
if delta or (self.factory.annotated and len(present_parents) > 0):
717
# Merge annotations from parent texts if so is needed.
718
delta_hunks = self._merge_annotations(lines, present_parents, parent_texts,
719
delta, self.factory.annotated)
722
options.append('line-delta')
723
store_lines = self.factory.lower_line_delta(delta_hunks)
725
options.append('fulltext')
726
store_lines = self.factory.lower_fulltext(lines)
728
where, size = self._data.add_record(version_id, digest, store_lines)
729
self._index.add_version(version_id, options, where, size, parents)
732
def check(self, progress_bar=None):
733
"""See VersionedFile.check()."""
735
def _clone_text(self, new_version_id, old_version_id, parents):
736
"""See VersionedFile.clone_text()."""
737
# FIXME RBC 20060228 make fast by only inserting an index with null
739
self.add_lines(new_version_id, parents, self.get_lines(old_version_id))
741
def get_lines(self, version_id):
742
"""See VersionedFile.get_lines()."""
743
return self.get_line_list([version_id])[0]
745
def _get_version_components(self, position_map):
747
for version_id, positions in position_map.iteritems():
748
for method, comp_id, position, size in positions:
749
records.append((comp_id, position, size))
750
record_map = self._data.read_records(records)
753
for version_id, positions in position_map.iteritems():
755
for method, comp_id, position, size in positions:
756
data, digest = record_map[comp_id]
757
components.append((comp_id, method, data, digest))
758
component_map[version_id] = components
761
def get_text(self, version_id):
762
"""See VersionedFile.get_text"""
763
return self.get_texts([version_id])[0]
765
def get_texts(self, version_ids):
766
return [''.join(l) for l in self.get_line_list(version_ids)]
768
def get_line_list(self, version_ids):
769
"""Return the texts of listed versions as a list of strings."""
771
for version_id in version_ids:
772
if not self.has_version(version_id):
773
raise RevisionNotPresent(version_id, self.filename)
774
position_map[version_id] = \
775
self._get_component_positions(version_id)
777
version_components = self._get_version_components(position_map).items()
780
for version_id, components in version_components:
782
for component_id, method, data, digest in reversed(components):
783
version_idx = self._index.lookup(component_id)
784
if method == 'fulltext':
785
assert content is None
786
content = self.factory.parse_fulltext(data, version_idx)
787
elif method == 'line-delta':
788
delta = self.factory.parse_line_delta(data, version_idx)
789
content._lines = self._apply_delta(content._lines, delta)
791
if 'no-eol' in self._index.get_options(version_id):
792
line = content._lines[-1][1].rstrip('\n')
793
content._lines[-1] = (content._lines[-1][0], line)
795
# digest here is the digest from the last applied component.
796
if sha_strings(content.text()) != digest:
797
raise KnitCorrupt(self.filename,
798
'sha-1 does not match %s' % version_id)
800
text_map[version_id] = content.text()
801
return [text_map[v] for v in version_ids]
803
def iter_lines_added_or_present_in_versions(self, version_ids=None):
804
"""See VersionedFile.iter_lines_added_or_present_in_versions()."""
805
if version_ids is None:
806
version_ids = self.versions()
807
# we don't care about inclusions, the caller cares.
808
# but we need to setup a list of records to visit.
809
# we need version_id, position, length
810
version_id_records = []
811
requested_versions = list(version_ids)
812
# filter for available versions
813
for version_id in requested_versions:
814
if not self.has_version(version_id):
815
raise RevisionNotPresent(version_id, self.filename)
816
# get a in-component-order queue:
818
for version_id in self.versions():
819
if version_id in requested_versions:
820
version_ids.append(version_id)
821
data_pos, length = self._index.get_position(version_id)
822
version_id_records.append((version_id, data_pos, length))
824
pb = bzrlib.ui.ui_factory.nested_progress_bar()
826
total = len(version_id_records)
828
pb.update('Walking content.', count, total)
829
for version_id, data, sha_value in \
830
self._data.read_records_iter(version_id_records):
831
pb.update('Walking content.', count, total)
832
method = self._index.get_method(version_id)
833
version_idx = self._index.lookup(version_id)
834
assert method in ('fulltext', 'line-delta')
835
if method == 'fulltext':
836
content = self.factory.parse_fulltext(data, version_idx)
837
for line in content.text():
840
delta = self.factory.parse_line_delta(data, version_idx)
841
for start, end, count, lines in delta:
842
for origin, line in lines:
845
pb.update('Walking content.', total, total)
848
pb.update('Walking content.', total, total)
852
def num_versions(self):
853
"""See VersionedFile.num_versions()."""
854
return self._index.num_versions()
856
__len__ = num_versions
858
def annotate_iter(self, version_id):
859
"""See VersionedFile.annotate_iter."""
860
content = self._get_content(version_id)
861
for origin, text in content.annotate_iter():
864
def get_parents(self, version_id):
865
"""See VersionedFile.get_parents."""
868
# 52554 calls in 1264 872 internal down from 3674
870
return self._index.get_parents(version_id)
872
raise RevisionNotPresent(version_id, self.filename)
874
def get_parents_with_ghosts(self, version_id):
875
"""See VersionedFile.get_parents."""
877
return self._index.get_parents_with_ghosts(version_id)
879
raise RevisionNotPresent(version_id, self.filename)
881
def get_ancestry(self, versions):
882
"""See VersionedFile.get_ancestry."""
883
if isinstance(versions, basestring):
884
versions = [versions]
887
self._check_versions_present(versions)
888
return self._index.get_ancestry(versions)
890
def get_ancestry_with_ghosts(self, versions):
891
"""See VersionedFile.get_ancestry_with_ghosts."""
892
if isinstance(versions, basestring):
893
versions = [versions]
896
self._check_versions_present(versions)
897
return self._index.get_ancestry_with_ghosts(versions)
899
#@deprecated_method(zero_eight)
900
def walk(self, version_ids):
901
"""See VersionedFile.walk."""
902
# We take the short path here, and extract all relevant texts
903
# and put them in a weave and let that do all the work. Far
904
# from optimal, but is much simpler.
905
# FIXME RB 20060228 this really is inefficient!
906
from bzrlib.weave import Weave
908
w = Weave(self.filename)
909
ancestry = self.get_ancestry(version_ids)
910
sorted_graph = topo_sort(self._index.get_graph())
911
version_list = [vid for vid in sorted_graph if vid in ancestry]
913
for version_id in version_list:
914
lines = self.get_lines(version_id)
915
w.add_lines(version_id, self.get_parents(version_id), lines)
917
for lineno, insert_id, dset, line in w.walk(version_ids):
918
yield lineno, insert_id, dset, line
920
def plan_merge(self, ver_a, ver_b):
921
"""See VersionedFile.plan_merge."""
922
ancestors_b = set(self.get_ancestry(ver_b))
923
def status_a(revision, text):
924
if revision in ancestors_b:
925
return 'killed-b', text
929
ancestors_a = set(self.get_ancestry(ver_a))
930
def status_b(revision, text):
931
if revision in ancestors_a:
932
return 'killed-a', text
936
annotated_a = self.annotate(ver_a)
937
annotated_b = self.annotate(ver_b)
938
plain_a = [t for (a, t) in annotated_a]
939
plain_b = [t for (a, t) in annotated_b]
940
blocks = KnitSequenceMatcher(None, plain_a, plain_b).get_matching_blocks()
943
for ai, bi, l in blocks:
944
# process all mismatched sections
945
# (last mismatched section is handled because blocks always
946
# includes a 0-length last block)
947
for revision, text in annotated_a[a_cur:ai]:
948
yield status_a(revision, text)
949
for revision, text in annotated_b[b_cur:bi]:
950
yield status_b(revision, text)
952
# and now the matched section
955
for text_a, text_b in zip(plain_a[ai:a_cur], plain_b[bi:b_cur]):
956
assert text_a == text_b
957
yield "unchanged", text_a
960
class _KnitComponentFile(object):
961
"""One of the files used to implement a knit database"""
963
def __init__(self, transport, filename, mode, file_mode=None):
964
self._transport = transport
965
self._filename = filename
967
self._file_mode=file_mode
969
def write_header(self):
970
if self._transport.append(self._filename, StringIO(self.HEADER),
971
mode=self._file_mode):
972
raise KnitCorrupt(self._filename, 'misaligned after writing header')
974
def check_header(self, fp):
976
if line != self.HEADER:
977
raise KnitHeaderError(badline=line)
980
"""Commit is a nop."""
983
return '%s(%s)' % (self.__class__.__name__, self._filename)
986
class _KnitIndex(_KnitComponentFile):
987
"""Manages knit index file.
989
The index is already kept in memory and read on startup, to enable
990
fast lookups of revision information. The cursor of the index
991
file is always pointing to the end, making it easy to append
994
_cache is a cache for fast mapping from version id to a Index
997
_history is a cache for fast mapping from indexes to version ids.
999
The index data format is dictionary compressed when it comes to
1000
parent references; a index entry may only have parents that with a
1001
lover index number. As a result, the index is topological sorted.
1003
Duplicate entries may be written to the index for a single version id
1004
if this is done then the latter one completely replaces the former:
1005
this allows updates to correct version and parent information.
1006
Note that the two entries may share the delta, and that successive
1007
annotations and references MUST point to the first entry.
1009
The index file on disc contains a header, followed by one line per knit
1010
record. The same revision can be present in an index file more than once.
1011
The first occurrence gets assigned a sequence number starting from 0.
1013
The format of a single line is
1014
REVISION_ID FLAGS BYTE_OFFSET LENGTH( PARENT_ID|PARENT_SEQUENCE_ID)* :\n
1015
REVISION_ID is a utf8-encoded revision id
1016
FLAGS is a comma separated list of flags about the record. Values include
1017
no-eol, line-delta, fulltext.
1018
BYTE_OFFSET is the ascii representation of the byte offset in the data file
1019
that the the compressed data starts at.
1020
LENGTH is the ascii representation of the length of the data file.
1021
PARENT_ID a utf-8 revision id prefixed by a '.' that is a parent of
1023
PARENT_SEQUENCE_ID the ascii representation of the sequence number of a
1024
revision id already in the knit that is a parent of REVISION_ID.
1025
The ' :' marker is the end of record marker.
1028
when a write is interrupted to the index file, it will result in a line that
1029
does not end in ' :'. If the ' :' is not present at the end of a line, or at
1030
the end of the file, then the record that is missing it will be ignored by
1033
When writing new records to the index file, the data is preceded by '\n'
1034
to ensure that records always start on new lines even if the last write was
1035
interrupted. As a result its normal for the last line in the index to be
1036
missing a trailing newline. One can be added with no harmful effects.
1039
HEADER = "# bzr knit index 8\n"
1041
# speed of knit parsing went from 280 ms to 280 ms with slots addition.
1042
# __slots__ = ['_cache', '_history', '_transport', '_filename']
1044
def _cache_version(self, version_id, options, pos, size, parents):
1045
"""Cache a version record in the history array and index cache.
1047
This is inlined into __init__ for performance. KEEP IN SYNC.
1048
(It saves 60ms, 25% of the __init__ overhead on local 4000 record
1051
# only want the _history index to reference the 1st index entry
1053
if version_id not in self._cache:
1054
index = len(self._history)
1055
self._history.append(version_id)
1057
index = self._cache[version_id][5]
1058
self._cache[version_id] = (version_id,
1065
def __init__(self, transport, filename, mode, create=False, file_mode=None):
1066
_KnitComponentFile.__init__(self, transport, filename, mode, file_mode)
1068
# position in _history is the 'official' index for a revision
1069
# but the values may have come from a newer entry.
1070
# so - wc -l of a knit index is != the number of unique names
1073
pb = bzrlib.ui.ui_factory.nested_progress_bar()
1078
pb.update('read knit index', count, total)
1079
fp = self._transport.get(self._filename)
1080
self.check_header(fp)
1081
# readlines reads the whole file at once:
1082
# bad for transports like http, good for local disk
1083
# we save 60 ms doing this one change (
1084
# from calling readline each time to calling
1086
# probably what we want for nice behaviour on
1087
# http is a incremental readlines that yields, or
1088
# a check for local vs non local indexes,
1089
for l in fp.readlines():
1091
if len(rec) < 5 or rec[-1] != ':':
1093
# FIXME: in the future we should determine if its a
1094
# short write - and ignore it
1095
# or a different failure, and raise. RBC 20060407
1099
#pb.update('read knit index', count, total)
1100
# See self._parse_parents
1102
for value in rec[4:-1]:
1104
# uncompressed reference
1105
parents.append(value[1:])
1107
# this is 15/4000ms faster than isinstance,
1109
# this function is called thousands of times a
1110
# second so small variations add up.
1111
assert value.__class__ is str
1112
parents.append(self._history[int(value)])
1113
# end self._parse_parents
1114
# self._cache_version(rec[0],
1115
# rec[1].split(','),
1119
# --- self._cache_version
1120
# only want the _history index to reference the 1st
1121
# index entry for version_id
1123
if version_id not in self._cache:
1124
index = len(self._history)
1125
self._history.append(version_id)
1127
index = self._cache[version_id][5]
1128
self._cache[version_id] = (version_id,
1134
# --- self._cache_version
1135
except NoSuchFile, e:
1136
if mode != 'w' or not create:
1140
pb.update('read knit index', total, total)
1143
def _parse_parents(self, compressed_parents):
1144
"""convert a list of string parent values into version ids.
1146
ints are looked up in the index.
1147
.FOO values are ghosts and converted in to FOO.
1149
NOTE: the function is retained here for clarity, and for possible
1150
use in partial index reads. However bulk processing now has
1151
it inlined in __init__ for inner-loop optimisation.
1154
for value in compressed_parents:
1155
if value[-1] == '.':
1156
# uncompressed reference
1157
result.append(value[1:])
1159
# this is 15/4000ms faster than isinstance,
1160
# this function is called thousands of times a
1161
# second so small variations add up.
1162
assert value.__class__ is str
1163
result.append(self._history[int(value)])
1166
def get_graph(self):
1168
for version_id, index in self._cache.iteritems():
1169
graph.append((version_id, index[4]))
1172
def get_ancestry(self, versions):
1173
"""See VersionedFile.get_ancestry."""
1174
# get a graph of all the mentioned versions:
1176
pending = set(versions)
1178
version = pending.pop()
1179
parents = self._cache[version][4]
1180
# got the parents ok
1182
parents = [parent for parent in parents if parent in self._cache]
1183
for parent in parents:
1184
# if not completed and not a ghost
1185
if parent not in graph:
1187
graph[version] = parents
1188
return topo_sort(graph.items())
1190
def get_ancestry_with_ghosts(self, versions):
1191
"""See VersionedFile.get_ancestry_with_ghosts."""
1192
# get a graph of all the mentioned versions:
1194
pending = set(versions)
1196
version = pending.pop()
1198
parents = self._cache[version][4]
1204
# got the parents ok
1205
for parent in parents:
1206
if parent not in graph:
1208
graph[version] = parents
1209
return topo_sort(graph.items())
1211
def num_versions(self):
1212
return len(self._history)
1214
__len__ = num_versions
1216
def get_versions(self):
1217
return self._history
1219
def idx_to_name(self, idx):
1220
return self._history[idx]
1222
def lookup(self, version_id):
1223
assert version_id in self._cache
1224
return self._cache[version_id][5]
1226
def _version_list_to_index(self, versions):
1228
for version in versions:
1229
if version in self._cache:
1230
# -- inlined lookup() --
1231
result_list.append(str(self._cache[version][5]))
1232
# -- end lookup () --
1234
result_list.append('.' + version.encode('utf-8'))
1235
return ' '.join(result_list)
1237
def add_version(self, version_id, options, pos, size, parents):
1238
"""Add a version record to the index."""
1239
self.add_versions(((version_id, options, pos, size, parents),))
1241
def add_versions(self, versions):
1242
"""Add multiple versions to the index.
1244
:param versions: a list of tuples:
1245
(version_id, options, pos, size, parents).
1248
for version_id, options, pos, size, parents in versions:
1249
line = "\n%s %s %s %s %s :" % (version_id.encode('utf-8'),
1253
self._version_list_to_index(parents))
1254
assert isinstance(line, str), \
1255
'content must be utf-8 encoded: %r' % (line,)
1257
self._transport.append(self._filename, StringIO(''.join(lines)))
1258
# cache after writing, so that a failed write leads to missing cache
1259
# entries not extra ones. XXX TODO: RBC 20060502 in the event of a
1260
# failure, reload the index or flush it or some such, to prevent
1261
# writing records that did complete twice.
1262
for version_id, options, pos, size, parents in versions:
1263
self._cache_version(version_id, options, pos, size, parents)
1265
def has_version(self, version_id):
1266
"""True if the version is in the index."""
1267
return self._cache.has_key(version_id)
1269
def get_position(self, version_id):
1270
"""Return data position and size of specified version."""
1271
return (self._cache[version_id][2], \
1272
self._cache[version_id][3])
1274
def get_method(self, version_id):
1275
"""Return compression method of specified version."""
1276
options = self._cache[version_id][1]
1277
if 'fulltext' in options:
1280
assert 'line-delta' in options
1283
def get_options(self, version_id):
1284
return self._cache[version_id][1]
1286
def get_parents(self, version_id):
1287
"""Return parents of specified version ignoring ghosts."""
1288
return [parent for parent in self._cache[version_id][4]
1289
if parent in self._cache]
1291
def get_parents_with_ghosts(self, version_id):
1292
"""Return parents of specified version with ghosts."""
1293
return self._cache[version_id][4]
1295
def check_versions_present(self, version_ids):
1296
"""Check that all specified versions are present."""
1297
version_ids = set(version_ids)
1298
for version_id in list(version_ids):
1299
if version_id in self._cache:
1300
version_ids.remove(version_id)
1302
raise RevisionNotPresent(list(version_ids)[0], self.filename)
1305
class _KnitData(_KnitComponentFile):
1306
"""Contents of the knit data file"""
1308
HEADER = "# bzr knit data 8\n"
1310
def __init__(self, transport, filename, mode, create=False, file_mode=None):
1311
_KnitComponentFile.__init__(self, transport, filename, mode)
1313
self._checked = False
1315
self._transport.put(self._filename, StringIO(''), mode=file_mode)
1318
def clear_cache(self):
1319
"""Clear the record cache."""
1322
def _open_file(self):
1323
if self._file is None:
1325
self._file = self._transport.get(self._filename)
1330
def _record_to_data(self, version_id, digest, lines):
1331
"""Convert version_id, digest, lines into a raw data block.
1333
:return: (len, a StringIO instance with the raw data ready to read.)
1336
data_file = GzipFile(None, mode='wb', fileobj=sio)
1337
data_file.writelines(chain(
1338
["version %s %d %s\n" % (version_id.encode('utf-8'),
1342
["end %s\n" % version_id.encode('utf-8')]))
1349
def add_raw_record(self, raw_data):
1350
"""Append a prepared record to the data file.
1352
:return: the offset in the data file raw_data was written.
1354
assert isinstance(raw_data, str), 'data must be plain bytes'
1355
return self._transport.append(self._filename, StringIO(raw_data))
1357
def add_record(self, version_id, digest, lines):
1358
"""Write new text record to disk. Returns the position in the
1359
file where it was written."""
1360
size, sio = self._record_to_data(version_id, digest, lines)
1362
self._records[version_id] = (digest, lines)
1364
start_pos = self._transport.append(self._filename, sio)
1365
return start_pos, size
1367
def _parse_record_header(self, version_id, raw_data):
1368
"""Parse a record header for consistency.
1370
:return: the header and the decompressor stream.
1371
as (stream, header_record)
1373
df = GzipFile(mode='rb', fileobj=StringIO(raw_data))
1374
rec = df.readline().split()
1376
raise KnitCorrupt(self._filename, 'unexpected number of elements in record header')
1377
if rec[1].decode('utf-8')!= version_id:
1378
raise KnitCorrupt(self._filename,
1379
'unexpected version, wanted %r, got %r' % (
1380
version_id, rec[1]))
1383
def _parse_record(self, version_id, data):
1385
# 4168 calls in 2880 217 internal
1386
# 4168 calls to _parse_record_header in 2121
1387
# 4168 calls to readlines in 330
1388
df, rec = self._parse_record_header(version_id, data)
1389
record_contents = df.readlines()
1390
l = record_contents.pop()
1391
assert len(record_contents) == int(rec[2])
1392
if l.decode('utf-8') != 'end %s\n' % version_id:
1393
raise KnitCorrupt(self._filename, 'unexpected version end line %r, wanted %r'
1396
return record_contents, rec[3]
1398
def read_records_iter_raw(self, records):
1399
"""Read text records from data file and yield raw data.
1401
This unpacks enough of the text record to validate the id is
1402
as expected but thats all.
1404
It will actively recompress currently cached records on the
1405
basis that that is cheaper than I/O activity.
1408
for version_id, pos, size in records:
1409
if version_id not in self._records:
1410
needed_records.append((version_id, pos, size))
1412
# setup an iterator of the external records:
1413
# uses readv so nice and fast we hope.
1414
if len(needed_records):
1415
# grab the disk data needed.
1416
raw_records = self._transport.readv(self._filename,
1417
[(pos, size) for version_id, pos, size in needed_records])
1419
for version_id, pos, size in records:
1420
if version_id in self._records:
1421
# compress a new version
1422
size, sio = self._record_to_data(version_id,
1423
self._records[version_id][0],
1424
self._records[version_id][1])
1425
yield version_id, sio.getvalue()
1427
pos, data = raw_records.next()
1428
# validate the header
1429
df, rec = self._parse_record_header(version_id, data)
1431
yield version_id, data
1434
def read_records_iter(self, records):
1435
"""Read text records from data file and yield result.
1437
Each passed record is a tuple of (version_id, pos, len) and
1438
will be read in the given order. Yields (version_id,
1442
# 60890 calls for 4168 extractions in 5045, 683 internal.
1443
# 4168 calls to readv in 1411
1444
# 4168 calls to parse_record in 2880
1447
for version_id, pos, size in records:
1448
if version_id not in self._records:
1449
needed_records.append((version_id, pos, size))
1451
if len(needed_records):
1452
needed_records.sort(key=lambda x:x[1])
1453
# We take it that the transport optimizes the fetching as good
1454
# as possible (ie, reads continuous ranges.)
1455
response = self._transport.readv(self._filename,
1456
[(pos, size) for version_id, pos, size in needed_records])
1458
for (record_id, pos, size), (pos, data) in \
1459
izip(iter(needed_records), response):
1460
content, digest = self._parse_record(record_id, data)
1461
self._records[record_id] = (digest, content)
1463
for version_id, pos, size in records:
1464
yield version_id, list(self._records[version_id][1]), self._records[version_id][0]
1466
def read_records(self, records):
1467
"""Read records into a dictionary."""
1469
for record_id, content, digest in self.read_records_iter(records):
1470
components[record_id] = (content, digest)
1474
class InterKnit(InterVersionedFile):
1475
"""Optimised code paths for knit to knit operations."""
1477
_matching_file_from_factory = KnitVersionedFile
1478
_matching_file_to_factory = KnitVersionedFile
1481
def is_compatible(source, target):
1482
"""Be compatible with knits. """
1484
return (isinstance(source, KnitVersionedFile) and
1485
isinstance(target, KnitVersionedFile))
1486
except AttributeError:
1489
def join(self, pb=None, msg=None, version_ids=None, ignore_missing=False):
1490
"""See InterVersionedFile.join."""
1491
assert isinstance(self.source, KnitVersionedFile)
1492
assert isinstance(self.target, KnitVersionedFile)
1494
version_ids = self._get_source_version_ids(version_ids, ignore_missing)
1499
pb = bzrlib.ui.ui_factory.nested_progress_bar()
1501
version_ids = list(version_ids)
1502
if None in version_ids:
1503
version_ids.remove(None)
1505
self.source_ancestry = set(self.source.get_ancestry(version_ids))
1506
this_versions = set(self.target._index.get_versions())
1507
needed_versions = self.source_ancestry - this_versions
1508
cross_check_versions = self.source_ancestry.intersection(this_versions)
1509
mismatched_versions = set()
1510
for version in cross_check_versions:
1511
# scan to include needed parents.
1512
n1 = set(self.target.get_parents_with_ghosts(version))
1513
n2 = set(self.source.get_parents_with_ghosts(version))
1515
# FIXME TEST this check for cycles being introduced works
1516
# the logic is we have a cycle if in our graph we are an
1517
# ancestor of any of the n2 revisions.
1523
parent_ancestors = self.source.get_ancestry(parent)
1524
if version in parent_ancestors:
1525
raise errors.GraphCycleError([parent, version])
1526
# ensure this parent will be available later.
1527
new_parents = n2.difference(n1)
1528
needed_versions.update(new_parents.difference(this_versions))
1529
mismatched_versions.add(version)
1531
if not needed_versions and not mismatched_versions:
1533
full_list = topo_sort(self.source.get_graph())
1535
version_list = [i for i in full_list if (not self.target.has_version(i)
1536
and i in needed_versions)]
1540
copy_queue_records = []
1542
for version_id in version_list:
1543
options = self.source._index.get_options(version_id)
1544
parents = self.source._index.get_parents_with_ghosts(version_id)
1545
# check that its will be a consistent copy:
1546
for parent in parents:
1547
# if source has the parent, we must :
1548
# * already have it or
1549
# * have it scheduled already
1550
# otherwise we don't care
1551
assert (self.target.has_version(parent) or
1552
parent in copy_set or
1553
not self.source.has_version(parent))
1554
data_pos, data_size = self.source._index.get_position(version_id)
1555
copy_queue_records.append((version_id, data_pos, data_size))
1556
copy_queue.append((version_id, options, parents))
1557
copy_set.add(version_id)
1559
# data suck the join:
1561
total = len(version_list)
1564
for (version_id, raw_data), \
1565
(version_id2, options, parents) in \
1566
izip(self.source._data.read_records_iter_raw(copy_queue_records),
1568
assert version_id == version_id2, 'logic error, inconsistent results'
1570
pb.update("Joining knit", count, total)
1571
raw_records.append((version_id, options, parents, len(raw_data)))
1572
raw_datum.append(raw_data)
1573
self.target._add_raw_records(raw_records, ''.join(raw_datum))
1575
for version in mismatched_versions:
1576
# FIXME RBC 20060309 is this needed?
1577
n1 = set(self.target.get_parents_with_ghosts(version))
1578
n2 = set(self.source.get_parents_with_ghosts(version))
1579
# write a combined record to our history preserving the current
1580
# parents as first in the list
1581
new_parents = self.target.get_parents_with_ghosts(version) + list(n2.difference(n1))
1582
self.target.fix_parents(version, new_parents)
1588
InterVersionedFile.register_optimiser(InterKnit)
1591
class WeaveToKnit(InterVersionedFile):
1592
"""Optimised code paths for weave to knit operations."""
1594
_matching_file_from_factory = bzrlib.weave.WeaveFile
1595
_matching_file_to_factory = KnitVersionedFile
1598
def is_compatible(source, target):
1599
"""Be compatible with weaves to knits."""
1601
return (isinstance(source, bzrlib.weave.Weave) and
1602
isinstance(target, KnitVersionedFile))
1603
except AttributeError:
1606
def join(self, pb=None, msg=None, version_ids=None, ignore_missing=False):
1607
"""See InterVersionedFile.join."""
1608
assert isinstance(self.source, bzrlib.weave.Weave)
1609
assert isinstance(self.target, KnitVersionedFile)
1611
version_ids = self._get_source_version_ids(version_ids, ignore_missing)
1616
pb = bzrlib.ui.ui_factory.nested_progress_bar()
1618
version_ids = list(version_ids)
1620
self.source_ancestry = set(self.source.get_ancestry(version_ids))
1621
this_versions = set(self.target._index.get_versions())
1622
needed_versions = self.source_ancestry - this_versions
1623
cross_check_versions = self.source_ancestry.intersection(this_versions)
1624
mismatched_versions = set()
1625
for version in cross_check_versions:
1626
# scan to include needed parents.
1627
n1 = set(self.target.get_parents_with_ghosts(version))
1628
n2 = set(self.source.get_parents(version))
1629
# if all of n2's parents are in n1, then its fine.
1630
if n2.difference(n1):
1631
# FIXME TEST this check for cycles being introduced works
1632
# the logic is we have a cycle if in our graph we are an
1633
# ancestor of any of the n2 revisions.
1639
parent_ancestors = self.source.get_ancestry(parent)
1640
if version in parent_ancestors:
1641
raise errors.GraphCycleError([parent, version])
1642
# ensure this parent will be available later.
1643
new_parents = n2.difference(n1)
1644
needed_versions.update(new_parents.difference(this_versions))
1645
mismatched_versions.add(version)
1647
if not needed_versions and not mismatched_versions:
1649
full_list = topo_sort(self.source.get_graph())
1651
version_list = [i for i in full_list if (not self.target.has_version(i)
1652
and i in needed_versions)]
1656
total = len(version_list)
1657
for version_id in version_list:
1658
pb.update("Converting to knit", count, total)
1659
parents = self.source.get_parents(version_id)
1660
# check that its will be a consistent copy:
1661
for parent in parents:
1662
# if source has the parent, we must already have it
1663
assert (self.target.has_version(parent))
1664
self.target.add_lines(
1665
version_id, parents, self.source.get_lines(version_id))
1668
for version in mismatched_versions:
1669
# FIXME RBC 20060309 is this needed?
1670
n1 = set(self.target.get_parents_with_ghosts(version))
1671
n2 = set(self.source.get_parents(version))
1672
# write a combined record to our history preserving the current
1673
# parents as first in the list
1674
new_parents = self.target.get_parents_with_ghosts(version) + list(n2.difference(n1))
1675
self.target.fix_parents(version, new_parents)
1681
InterVersionedFile.register_optimiser(WeaveToKnit)
1684
class KnitSequenceMatcher(difflib.SequenceMatcher):
1685
"""Knit tuned sequence matcher.
1687
This is based on profiling of difflib which indicated some improvements
1688
for our usage pattern.
1691
def find_longest_match(self, alo, ahi, blo, bhi):
1692
"""Find longest matching block in a[alo:ahi] and b[blo:bhi].
1694
If isjunk is not defined:
1696
Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where
1697
alo <= i <= i+k <= ahi
1698
blo <= j <= j+k <= bhi
1699
and for all (i',j',k') meeting those conditions,
1702
and if i == i', j <= j'
1704
In other words, of all maximal matching blocks, return one that
1705
starts earliest in a, and of all those maximal matching blocks that
1706
start earliest in a, return the one that starts earliest in b.
1708
>>> s = SequenceMatcher(None, " abcd", "abcd abcd")
1709
>>> s.find_longest_match(0, 5, 0, 9)
1712
If isjunk is defined, first the longest matching block is
1713
determined as above, but with the additional restriction that no
1714
junk element appears in the block. Then that block is extended as
1715
far as possible by matching (only) junk elements on both sides. So
1716
the resulting block never matches on junk except as identical junk
1717
happens to be adjacent to an "interesting" match.
1719
Here's the same example as before, but considering blanks to be
1720
junk. That prevents " abcd" from matching the " abcd" at the tail
1721
end of the second sequence directly. Instead only the "abcd" can
1722
match, and matches the leftmost "abcd" in the second sequence:
1724
>>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
1725
>>> s.find_longest_match(0, 5, 0, 9)
1728
If no blocks match, return (alo, blo, 0).
1730
>>> s = SequenceMatcher(None, "ab", "c")
1731
>>> s.find_longest_match(0, 2, 0, 1)
1735
# CAUTION: stripping common prefix or suffix would be incorrect.
1739
# Longest matching block is "ab", but if common prefix is
1740
# stripped, it's "a" (tied with "b"). UNIX(tm) diff does so
1741
# strip, so ends up claiming that ab is changed to acab by
1742
# inserting "ca" in the middle. That's minimal but unintuitive:
1743
# "it's obvious" that someone inserted "ac" at the front.
1744
# Windiff ends up at the same place as diff, but by pairing up
1745
# the unique 'b's and then matching the first two 'a's.
1747
a, b, b2j, isbjunk = self.a, self.b, self.b2j, self.isbjunk
1748
besti, bestj, bestsize = alo, blo, 0
1749
# find longest junk-free match
1750
# during an iteration of the loop, j2len[j] = length of longest
1751
# junk-free match ending with a[i-1] and b[j]
1755
for i in xrange(alo, ahi):
1756
# look at all instances of a[i] in b; note that because
1757
# b2j has no junk keys, the loop is skipped if a[i] is junk
1758
j2lenget = j2len.get
1761
# changing b2j.get(a[i], nothing) to a try:KeyError pair produced the
1762
# following improvement
1763
# 704 0 4650.5320 2620.7410 bzrlib.knit:1336(find_longest_match)
1764
# +326674 0 1655.1210 1655.1210 +<method 'get' of 'dict' objects>
1765
# +76519 0 374.6700 374.6700 +<method 'has_key' of 'dict' objects>
1767
# 704 0 3733.2820 2209.6520 bzrlib.knit:1336(find_longest_match)
1768
# +211400 0 1147.3520 1147.3520 +<method 'get' of 'dict' objects>
1769
# +76519 0 376.2780 376.2780 +<method 'has_key' of 'dict' objects>
1781
k = newj2len[j] = 1 + j2lenget(-1 + j, 0)
1783
besti, bestj, bestsize = 1 + i-k, 1 + j-k, k
1786
# Extend the best by non-junk elements on each end. In particular,
1787
# "popular" non-junk elements aren't in b2j, which greatly speeds
1788
# the inner loop above, but also means "the best" match so far
1789
# doesn't contain any junk *or* popular non-junk elements.
1790
while besti > alo and bestj > blo and \
1791
not isbjunk(b[bestj-1]) and \
1792
a[besti-1] == b[bestj-1]:
1793
besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
1794
while besti+bestsize < ahi and bestj+bestsize < bhi and \
1795
not isbjunk(b[bestj+bestsize]) and \
1796
a[besti+bestsize] == b[bestj+bestsize]:
1799
# Now that we have a wholly interesting match (albeit possibly
1800
# empty!), we may as well suck up the matching junk on each
1801
# side of it too. Can't think of a good reason not to, and it
1802
# saves post-processing the (possibly considerable) expense of
1803
# figuring out what to do with it. In the case of an empty
1804
# interesting match, this is clearly the right thing to do,
1805
# because no other kind of match is possible in the regions.
1806
while besti > alo and bestj > blo and \
1807
isbjunk(b[bestj-1]) and \
1808
a[besti-1] == b[bestj-1]:
1809
besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
1810
while besti+bestsize < ahi and bestj+bestsize < bhi and \
1811
isbjunk(b[bestj+bestsize]) and \
1812
a[besti+bestsize] == b[bestj+bestsize]:
1813
bestsize = bestsize + 1
1815
return besti, bestj, bestsize