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>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
"""Knit versionedfile implementation.
22
A knit is a versioned file implementation that supports efficient append only
26
lifeless: the data file is made up of "delta records". each delta record has a delta header
27
that contains; (1) a version id, (2) the size of the delta (in lines), and (3) the digest of
28
the -expanded data- (ie, the delta applied to the parent). the delta also ends with a
29
end-marker; simply "end VERSION"
31
delta can be line or full contents.a
32
... the 8's there are the index number of the annotation.
33
version robertc@robertcollins.net-20051003014215-ee2990904cc4c7ad 7 c7d23b2a5bd6ca00e8e266cec0ec228158ee9f9e
37
8 e.set('executable', 'yes')
39
8 if elt.get('executable') == 'yes':
40
8 ie.executable = True
41
end robertc@robertcollins.net-20051003014215-ee2990904cc4c7ad
45
09:33 < jrydberg> lifeless: each index is made up of a tuple of; version id, options, position, size, parents
46
09:33 < jrydberg> lifeless: the parents are currently dictionary compressed
47
09:33 < jrydberg> lifeless: (meaning it currently does not support ghosts)
48
09:33 < lifeless> right
49
09:33 < jrydberg> lifeless: the position and size is the range in the data file
52
so the index sequence is the dictionary compressed sequence number used
53
in the deltas to provide line annotation
58
# 10:16 < lifeless> make partial index writes safe
59
# 10:16 < lifeless> implement 'knit.check()' like weave.check()
60
# 10:17 < lifeless> record known ghosts so we can detect when they are filled in rather than the current 'reweave
62
# move sha1 out of the content so that join is faster at verifying parents
63
# record content length ?
67
from cStringIO import StringIO
69
from difflib import SequenceMatcher
70
from gzip import GzipFile
71
from itertools import izip
76
import bzrlib.errors as errors
77
from bzrlib.errors import FileExists, NoSuchFile, KnitError, \
78
InvalidRevisionId, KnitCorrupt, KnitHeaderError, \
79
RevisionNotPresent, RevisionAlreadyPresent
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
87
# TODO: Split out code specific to this format into an associated object.
89
# TODO: Can we put in some kind of value to check that the index and data
90
# files belong together?
92
# TODO: accomodate binaries, perhaps by storing a byte count
94
# TODO: function to check whole file
96
# TODO: atomically append data, then measure backwards from the cursor
97
# position after writing to work out where it was located. we may need to
98
# bypass python file buffering.
100
DATA_SUFFIX = '.knit'
101
INDEX_SUFFIX = '.kndx'
104
class KnitContent(object):
105
"""Content of a knit version to which deltas can be applied."""
107
def __init__(self, lines):
110
def annotate_iter(self):
111
"""Yield tuples of (origin, text) for each content line."""
112
for origin, text in self._lines:
116
"""Return a list of (origin, text) tuples."""
117
return list(self.annotate_iter())
119
def apply_delta(self, delta):
120
"""Apply delta to this content."""
122
for start, end, count, lines in delta:
123
self._lines[offset+start:offset+end] = lines
124
offset = offset + (start - end) + count
126
def line_delta_iter(self, new_lines):
127
"""Generate line-based delta from new_lines to this content."""
128
new_texts = [text for origin, text in new_lines._lines]
129
old_texts = [text for origin, text in self._lines]
130
s = difflib.SequenceMatcher(None, old_texts, new_texts)
131
for op in s.get_opcodes():
134
yield (op[1], op[2], op[4]-op[3], new_lines._lines[op[3]:op[4]])
136
def line_delta(self, new_lines):
137
return list(self.line_delta_iter(new_lines))
140
return [text for origin, text in self._lines]
143
class _KnitFactory(object):
144
"""Base factory for creating content objects."""
146
def make(self, lines, version):
147
num_lines = len(lines)
148
return KnitContent(zip([version] * num_lines, lines))
151
class KnitAnnotateFactory(_KnitFactory):
152
"""Factory for creating annotated Content objects."""
156
def parse_fulltext(self, content, version):
159
origin, text = line.split(' ', 1)
160
lines.append((int(origin), text))
161
return KnitContent(lines)
163
def parse_line_delta_iter(self, lines):
165
header = lines.pop(0)
166
start, end, c = [int(n) for n in header.split(',')]
169
origin, text = lines.pop(0).split(' ', 1)
170
contents.append((int(origin), text))
171
yield start, end, c, contents
173
def parse_line_delta(self, lines, version):
174
return list(self.parse_line_delta_iter(lines))
176
def lower_fulltext(self, content):
177
return ['%d %s' % (o, t) for o, t in content._lines]
179
def lower_line_delta(self, delta):
181
for start, end, c, lines in delta:
182
out.append('%d,%d,%d\n' % (start, end, c))
183
for origin, text in lines:
184
out.append('%d %s' % (origin, text))
188
class KnitPlainFactory(_KnitFactory):
189
"""Factory for creating plain Content objects."""
193
def parse_fulltext(self, content, version):
194
return self.make(content, version)
196
def parse_line_delta_iter(self, lines, version):
198
header = lines.pop(0)
199
start, end, c = [int(n) for n in header.split(',')]
200
yield start, end, c, zip([version] * c, lines[:c])
203
def parse_line_delta(self, lines, version):
204
return list(self.parse_line_delta_iter(lines, version))
206
def lower_fulltext(self, content):
207
return content.text()
209
def lower_line_delta(self, delta):
211
for start, end, c, lines in delta:
212
out.append('%d,%d,%d\n' % (start, end, c))
213
out.extend([text for origin, text in lines])
217
def make_empty_knit(transport, relpath):
218
"""Construct a empty knit at the specified location."""
219
k = KnitVersionedFile(transport, relpath, 'w', KnitPlainFactory)
223
class KnitVersionedFile(VersionedFile):
224
"""Weave-like structure with faster random access.
226
A knit stores a number of texts and a summary of the relationships
227
between them. Texts are identified by a string version-id. Texts
228
are normally stored and retrieved as a series of lines, but can
229
also be passed as single strings.
231
Lines are stored with the trailing newline (if any) included, to
232
avoid special cases for files with no final newline. Lines are
233
composed of 8-bit characters, not unicode. The combination of
234
these approaches should mean any 'binary' file can be safely
235
stored and retrieved.
238
def __init__(self, relpath, transport, file_mode=None, access_mode=None, factory=None,
239
basis_knit=None, delta=True, create=False):
240
"""Construct a knit at location specified by relpath.
242
:param create: If not True, only open an existing knit.
244
if access_mode is None:
246
super(KnitVersionedFile, self).__init__(access_mode)
247
assert access_mode in ('r', 'w'), "invalid mode specified %r" % access_mode
248
assert not basis_knit or isinstance(basis_knit, KnitVersionedFile), \
251
self.transport = transport
252
self.filename = relpath
253
self.basis_knit = basis_knit
254
self.factory = factory or KnitAnnotateFactory()
255
self.writable = (access_mode == 'w')
258
self._index = _KnitIndex(transport, relpath + INDEX_SUFFIX,
259
access_mode, create=create)
260
self._data = _KnitData(transport, relpath + DATA_SUFFIX,
261
access_mode, create=not len(self.versions()))
263
def clear_cache(self):
264
"""Clear the data cache only."""
265
self._data.clear_cache()
267
def copy_to(self, name, transport):
268
"""See VersionedFile.copy_to()."""
269
# copy the current index to a temp index to avoid racing with local
271
transport.put(name + INDEX_SUFFIX + '.tmp', self.transport.get(self._index._filename))
273
transport.put(name + DATA_SUFFIX, self._data._open_file())
274
# rename the copied index into place
275
transport.rename(name + INDEX_SUFFIX + '.tmp', name + INDEX_SUFFIX)
277
def create_empty(self, name, transport, mode=None):
278
return KnitVersionedFile(name, transport, factory=self.factory, delta=self.delta, create=True)
280
def _fix_parents(self, version, new_parents):
281
"""Fix the parents list for version.
283
This is done by appending a new version to the index
284
with identical data except for the parents list.
285
the parents list must be a superset of the current
288
current_values = self._index._cache[version]
289
assert set(current_values[4]).difference(set(new_parents)) == set()
290
self._index.add_version(version,
296
def get_graph_with_ghosts(self):
297
"""See VersionedFile.get_graph_with_ghosts()."""
298
graph_items = self._index.get_graph()
299
return dict(graph_items)
303
"""See VersionedFile.get_suffixes()."""
304
return [DATA_SUFFIX, INDEX_SUFFIX]
306
def has_ghost(self, version_id):
307
"""True if there is a ghost reference in the file to version_id."""
309
if self.has_version(version_id):
311
# optimisable if needed by memoising the _ghosts set.
312
items = self._index.get_graph()
313
for node, parents in items:
314
for parent in parents:
315
if parent not in self._index._cache:
316
if parent == version_id:
321
"""See VersionedFile.versions."""
322
return self._index.get_versions()
324
def has_version(self, version_id):
325
"""See VersionedFile.has_version."""
326
return self._index.has_version(version_id)
328
__contains__ = has_version
330
def _merge_annotations(self, content, parents):
331
"""Merge annotations for content. This is done by comparing
332
the annotations based on changed to the text."""
333
for parent_id in parents:
334
merge_content = self._get_content(parent_id)
335
seq = SequenceMatcher(None, merge_content.text(), content.text())
336
for i, j, n in seq.get_matching_blocks():
339
content._lines[j:j+n] = merge_content._lines[i:i+n]
341
def _get_components(self, version_id):
342
"""Return a list of (version_id, method, data) tuples that
343
makes up version specified by version_id of the knit.
345
The components should be applied in the order of the returned
348
The basis knit will be used to the largest extent possible
349
since it is assumed that accesses to it is faster.
351
# needed_revisions holds a list of (method, version_id) of
352
# versions that is needed to be fetched to construct the final
353
# version of the file.
355
# basis_revisions is a list of versions that needs to be
356
# fetched but exists in the basis knit.
358
basis = self.basis_knit
365
if basis and basis._index.has_version(cursor):
367
basis_versions.append(cursor)
368
method = picked_knit._index.get_method(cursor)
369
needed_versions.append((method, cursor))
370
if method == 'fulltext':
372
cursor = picked_knit.get_parents(cursor)[0]
377
for comp_id in basis_versions:
378
data_pos, data_size = basis._index.get_data_position(comp_id)
379
records.append((piece_id, data_pos, data_size))
380
components.update(basis._data.read_records(records))
383
for comp_id in [vid for method, vid in needed_versions
384
if vid not in basis_versions]:
385
data_pos, data_size = self._index.get_position(comp_id)
386
records.append((comp_id, data_pos, data_size))
387
components.update(self._data.read_records(records))
389
# get_data_records returns a mapping with the version id as
390
# index and the value as data. The order the components need
391
# to be applied is held by needed_versions (reversed).
393
for method, comp_id in reversed(needed_versions):
394
out.append((comp_id, method, components[comp_id]))
398
def _get_content(self, version_id):
399
"""Returns a content object that makes up the specified
401
if not self.has_version(version_id):
402
raise RevisionNotPresent(version_id, self.filename)
404
if self.basis_knit and version_id in self.basis_knit:
405
return self.basis_knit._get_content(version_id)
408
components = self._get_components(version_id)
409
for component_id, method, (data, digest) in components:
410
version_idx = self._index.lookup(component_id)
411
if method == 'fulltext':
412
assert content is None
413
content = self.factory.parse_fulltext(data, version_idx)
414
elif method == 'line-delta':
415
delta = self.factory.parse_line_delta(data, version_idx)
416
content.apply_delta(delta)
418
if 'no-eol' in self._index.get_options(version_id):
419
line = content._lines[-1][1].rstrip('\n')
420
content._lines[-1] = (content._lines[-1][0], line)
422
if sha_strings(content.text()) != digest:
423
raise KnitCorrupt(self.filename, 'sha-1 does not match')
427
def _check_versions_present(self, version_ids):
428
"""Check that all specified versions are present."""
429
version_ids = set(version_ids)
430
for r in list(version_ids):
431
if self._index.has_version(r):
432
version_ids.remove(r)
434
raise RevisionNotPresent(list(version_ids)[0], self.filename)
436
def _add_lines_with_ghosts(self, version_id, parents, lines):
437
"""See VersionedFile.add_lines_with_ghosts()."""
438
self._check_add(version_id, lines)
439
return self._add(version_id, lines[:], parents, self.delta)
441
def _add_lines(self, version_id, parents, lines):
442
"""See VersionedFile.add_lines."""
443
self._check_add(version_id, lines)
444
self._check_versions_present(parents)
445
return self._add(version_id, lines[:], parents, self.delta)
447
def _check_add(self, version_id, lines):
448
"""check that version_id and lines are safe to add."""
449
assert self.writable, "knit is not opened for write"
450
### FIXME escape. RBC 20060228
451
if contains_whitespace(version_id):
452
raise InvalidRevisionId(version_id)
453
if self.has_version(version_id):
454
raise RevisionAlreadyPresent(version_id, self.filename)
456
if False or __debug__:
458
assert '\n' not in l[:-1]
460
def _add(self, version_id, lines, parents, delta):
461
"""Add a set of lines on top of version specified by parents.
463
If delta is true, compress the text as a line-delta against
466
Any versions not present will be converted into ghosts.
468
ghostless_parents = []
470
for parent in parents:
471
if not self.has_version(parent):
472
ghosts.append(parent)
474
ghostless_parents.append(parent)
476
if delta and not len(ghostless_parents):
479
digest = sha_strings(lines)
482
if lines[-1][-1] != '\n':
483
options.append('no-eol')
484
lines[-1] = lines[-1] + '\n'
486
lines = self.factory.make(lines, len(self._index))
487
if self.factory.annotated and len(ghostless_parents) > 0:
488
# Merge annotations from parent texts if so is needed.
489
self._merge_annotations(lines, ghostless_parents)
491
if len(ghostless_parents) and delta:
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.
496
delta_parents = ghostless_parents
498
parent = delta_parents[0]
499
method = self._index.get_method(parent)
500
if method == 'fulltext':
502
delta_parents = self._index.get_parents(parent)
504
if method == 'line-delta':
508
options.append('line-delta')
509
content = self._get_content(ghostless_parents[0])
510
delta_hunks = content.line_delta(lines)
511
store_lines = self.factory.lower_line_delta(delta_hunks)
513
options.append('fulltext')
514
store_lines = self.factory.lower_fulltext(lines)
516
where, size = self._data.add_record(version_id, digest, store_lines)
517
self._index.add_version(version_id, options, where, size, parents)
519
def check(self, progress_bar=None):
520
"""See VersionedFile.check()."""
522
def _clone_text(self, new_version_id, old_version_id, parents):
523
"""See VersionedFile.clone_text()."""
524
# FIXME RBC 20060228 make fast by only inserting an index with null delta.
525
self.add_lines(new_version_id, parents, self.get_lines(old_version_id))
527
def get_lines(self, version_id):
528
"""See VersionedFile.get_lines()."""
529
return self._get_content(version_id).text()
531
def iter_lines_added_or_present_in_versions(self, version_ids=None):
532
"""See VersionedFile.iter_lines_added_or_present_in_versions()."""
533
if version_ids is None:
534
version_ids = self.versions()
535
# we dont care about inclusions, the caller cares.
536
# but we need to setup a list of records to visit.
537
# we need version_id, position, length
538
version_id_records = []
539
for version_id in version_ids:
540
if not self.has_version(version_id):
541
raise RevisionNotPresent(version_id, self.filename)
542
data_pos, length = self._index.get_position(version_id)
543
version_id_records.append((version_id, data_pos, length))
544
pb = bzrlib.ui.ui_factory.nested_progress_bar()
546
total = len(version_id_records)
548
pb.update('Walking content.', count, total)
549
for version_id, data, sha_value in \
550
self._data.read_records_iter(version_id_records):
551
pb.update('Walking content.', count, total)
552
method = self._index.get_method(version_id)
553
version_idx = self._index.lookup(version_id)
554
assert method in ('fulltext', 'line-delta')
555
if method == 'fulltext':
556
content = self.factory.parse_fulltext(data, version_idx)
557
for line in content.text():
560
delta = self.factory.parse_line_delta(data, version_idx)
561
for start, end, count, lines in delta:
562
for origin, line in lines:
565
pb.update('Walking content.', total, total)
568
pb.update('Walking content.', total, total)
572
def num_versions(self):
573
"""See VersionedFile.num_versions()."""
574
return self._index.num_versions()
576
__len__ = num_versions
578
def annotate_iter(self, version_id):
579
"""See VersionedFile.annotate_iter."""
580
content = self._get_content(version_id)
581
for origin, text in content.annotate_iter():
582
yield self._index.idx_to_name(origin), text
584
def get_parents(self, version_id):
585
"""See VersionedFile.get_parents."""
586
self._check_versions_present([version_id])
587
return list(self._index.get_parents(version_id))
589
def get_parents_with_ghosts(self, version_id):
590
"""See VersionedFile.get_parents."""
591
self._check_versions_present([version_id])
592
return list(self._index.get_parents_with_ghosts(version_id))
594
def get_ancestry(self, versions):
595
"""See VersionedFile.get_ancestry."""
596
if isinstance(versions, basestring):
597
versions = [versions]
600
self._check_versions_present(versions)
601
return self._index.get_ancestry(versions)
603
def get_ancestry_with_ghosts(self, versions):
604
"""See VersionedFile.get_ancestry_with_ghosts."""
605
if isinstance(versions, basestring):
606
versions = [versions]
609
self._check_versions_present(versions)
610
return self._index.get_ancestry_with_ghosts(versions)
612
def _reannotate_line_delta(self, other, lines, new_version_id,
614
"""Re-annotate line-delta and return new delta."""
616
for start, end, count, contents \
617
in self.factory.parse_line_delta_iter(lines):
619
for origin, line in contents:
620
old_version_id = other._index.idx_to_name(origin)
621
if old_version_id == new_version_id:
622
idx = new_version_idx
624
idx = self._index.lookup(old_version_id)
625
new_lines.append((idx, line))
626
new_delta.append((start, end, count, new_lines))
628
return self.factory.lower_line_delta(new_delta)
630
def _reannotate_fulltext(self, other, lines, new_version_id,
632
"""Re-annotate fulltext and return new version."""
633
content = self.factory.parse_fulltext(lines, new_version_idx)
635
for origin, line in content.annotate_iter():
636
old_version_id = other._index.idx_to_name(origin)
637
if old_version_id == new_version_id:
638
idx = new_version_idx
640
idx = self._index.lookup(old_version_id)
641
new_lines.append((idx, line))
643
return self.factory.lower_fulltext(KnitContent(new_lines))
645
#@deprecated_method(zero_eight)
646
def walk(self, version_ids):
647
"""See VersionedFile.walk."""
648
# We take the short path here, and extract all relevant texts
649
# and put them in a weave and let that do all the work. Far
650
# from optimal, but is much simpler.
651
# FIXME RB 20060228 this really is inefficient!
652
from bzrlib.weave import Weave
654
w = Weave(self.filename)
655
ancestry = self.get_ancestry(version_ids)
656
sorted_graph = topo_sort(self._index.get_graph())
657
version_list = [vid for vid in sorted_graph if vid in ancestry]
659
for version_id in version_list:
660
lines = self.get_lines(version_id)
661
w.add_lines(version_id, self.get_parents(version_id), lines)
663
for lineno, insert_id, dset, line in w.walk(version_ids):
664
yield lineno, insert_id, dset, line
667
class _KnitComponentFile(object):
668
"""One of the files used to implement a knit database"""
670
def __init__(self, transport, filename, mode):
671
self._transport = transport
672
self._filename = filename
675
def write_header(self):
676
old_len = self._transport.append(self._filename, StringIO(self.HEADER))
678
raise KnitCorrupt(self._filename, 'misaligned after writing header')
680
def check_header(self, fp):
681
line = fp.read(len(self.HEADER))
682
if line != self.HEADER:
683
raise KnitHeaderError(badline=line)
686
"""Commit is a nop."""
689
return '%s(%s)' % (self.__class__.__name__, self._filename)
692
class _KnitIndex(_KnitComponentFile):
693
"""Manages knit index file.
695
The index is already kept in memory and read on startup, to enable
696
fast lookups of revision information. The cursor of the index
697
file is always pointing to the end, making it easy to append
700
_cache is a cache for fast mapping from version id to a Index
703
_history is a cache for fast mapping from indexes to version ids.
705
The index data format is dictionary compressed when it comes to
706
parent references; a index entry may only have parents that with a
707
lover index number. As a result, the index is topological sorted.
709
Duplicate entries may be written to the index for a single version id
710
if this is done then the latter one completely replaces the former:
711
this allows updates to correct version and parent information.
712
Note that the two entries may share the delta, and that successive
713
annotations and references MUST point to the first entry.
716
HEADER = "# bzr knit index 7\n"
718
def _cache_version(self, version_id, options, pos, size, parents):
719
val = (version_id, options, pos, size, parents)
720
self._cache[version_id] = val
721
if not version_id in self._history:
722
self._history.append(version_id)
724
def _iter_index(self, fp):
730
#for l in lines.splitlines(False):
733
def __init__(self, transport, filename, mode, create=False):
734
_KnitComponentFile.__init__(self, transport, filename, mode)
736
# position in _history is the 'official' index for a revision
737
# but the values may have come from a newer entry.
738
# so - wc -l of a knit index is != the number of uniqe names
741
pb = bzrlib.ui.ui_factory.nested_progress_bar()
746
pb.update('read knit index', count, total)
747
fp = self._transport.get(self._filename)
748
self.check_header(fp)
749
for rec in self._iter_index(fp):
752
pb.update('read knit index', count, total)
753
parents = self._parse_parents(rec[4:])
754
self._cache_version(rec[0], rec[1].split(','), int(rec[2]), int(rec[3]),
756
except NoSuchFile, e:
757
if mode != 'w' or not create:
761
pb.update('read knit index', total, total)
764
def _parse_parents(self, compressed_parents):
765
"""convert a list of string parent values into version ids.
767
ints are looked up in the index.
768
.FOO values are ghosts and converted in to FOO.
771
for value in compressed_parents:
772
if value.startswith('.'):
773
result.append(value[1:])
775
assert isinstance(value, str)
776
result.append(self._history[int(value)])
781
for version_id, index in self._cache.iteritems():
782
graph.append((version_id, index[4]))
785
def get_ancestry(self, versions):
786
"""See VersionedFile.get_ancestry."""
787
# get a graph of all the mentioned versions:
789
pending = set(versions)
791
version = pending.pop()
792
parents = self._cache[version][4]
795
parents = [parent for parent in parents if parent in self._cache]
796
for parent in parents:
797
# if not completed and not a ghost
798
if parent not in graph:
800
graph[version] = parents
801
return topo_sort(graph.items())
803
def get_ancestry_with_ghosts(self, versions):
804
"""See VersionedFile.get_ancestry_with_ghosts."""
805
# get a graph of all the mentioned versions:
807
pending = set(versions)
809
version = pending.pop()
811
parents = self._cache[version][4]
818
for parent in parents:
819
if parent not in graph:
821
graph[version] = parents
822
return topo_sort(graph.items())
824
def num_versions(self):
825
return len(self._history)
827
__len__ = num_versions
829
def get_versions(self):
832
def idx_to_name(self, idx):
833
return self._history[idx]
835
def lookup(self, version_id):
836
assert version_id in self._cache
837
return self._history.index(version_id)
839
def _version_list_to_index(self, versions):
841
for version in versions:
842
if version in self._cache:
843
result_list.append(str(self._history.index(version)))
845
result_list.append('.' + version.encode('utf-8'))
846
return ' '.join(result_list)
848
def add_version(self, version_id, options, pos, size, parents):
849
"""Add a version record to the index."""
850
self._cache_version(version_id, options, pos, size, parents)
852
content = "%s %s %s %s %s\n" % (version_id,
856
self._version_list_to_index(parents))
857
self._transport.append(self._filename, StringIO(content))
859
def has_version(self, version_id):
860
"""True if the version is in the index."""
861
return self._cache.has_key(version_id)
863
def get_position(self, version_id):
864
"""Return data position and size of specified version."""
865
return (self._cache[version_id][2], \
866
self._cache[version_id][3])
868
def get_method(self, version_id):
869
"""Return compression method of specified version."""
870
options = self._cache[version_id][1]
871
if 'fulltext' in options:
874
assert 'line-delta' in options
877
def get_options(self, version_id):
878
return self._cache[version_id][1]
880
def get_parents(self, version_id):
881
"""Return parents of specified version ignoring ghosts."""
882
return [parent for parent in self._cache[version_id][4]
883
if parent in self._cache]
885
def get_parents_with_ghosts(self, version_id):
886
"""Return parents of specified version wth ghosts."""
887
return self._cache[version_id][4]
889
def check_versions_present(self, version_ids):
890
"""Check that all specified versions are present."""
891
version_ids = set(version_ids)
892
for version_id in list(version_ids):
893
if version_id in self._cache:
894
version_ids.remove(version_id)
896
raise RevisionNotPresent(list(version_ids)[0], self.filename)
899
class _KnitData(_KnitComponentFile):
900
"""Contents of the knit data file"""
902
HEADER = "# bzr knit data 7\n"
904
def __init__(self, transport, filename, mode, create=False):
905
_KnitComponentFile.__init__(self, transport, filename, mode)
907
self._checked = False
909
self._transport.put(self._filename, StringIO(''))
912
def clear_cache(self):
913
"""Clear the record cache."""
916
def _open_file(self):
917
if self._file is None:
919
self._file = self._transport.get(self._filename)
924
def add_record(self, version_id, digest, lines):
925
"""Write new text record to disk. Returns the position in the
926
file where it was written."""
928
data_file = GzipFile(None, mode='wb', fileobj=sio)
929
print >>data_file, "version %s %d %s" % (version_id, len(lines), digest)
930
data_file.writelines(lines)
931
print >>data_file, "end %s\n" % version_id
935
self._records[version_id] = (digest, lines)
937
content = sio.getvalue()
939
start_pos = self._transport.append(self._filename, sio)
940
return start_pos, len(content)
942
def _parse_record(self, version_id, data):
943
df = GzipFile(mode='rb', fileobj=StringIO(data))
944
rec = df.readline().split()
946
raise KnitCorrupt(self._filename, 'unexpected number of records')
947
if rec[1] != version_id:
948
raise KnitCorrupt(self.file.name,
949
'unexpected version, wanted %r' % version_id)
951
record_contents = self._read_record_contents(df, lines)
953
if l != 'end %s\n' % version_id:
954
raise KnitCorrupt(self._filename, 'unexpected version end line %r, wanted %r'
956
return record_contents, rec[3]
958
def _read_record_contents(self, df, record_lines):
959
"""Read and return n lines from datafile."""
961
for i in range(record_lines):
962
r.append(df.readline())
965
def read_records_iter(self, records):
966
"""Read text records from data file and yield result.
968
Each passed record is a tuple of (version_id, pos, len) and
969
will be read in the given order. Yields (version_id,
974
for version_id, pos, size in records:
975
if version_id not in self._records:
976
needed_records.append((version_id, pos, size))
978
if len(needed_records):
979
# We take it that the transport optimizes the fetching as good
980
# as possible (ie, reads continous ranges.)
981
response = self._transport.readv(self._filename,
982
[(pos, size) for version_id, pos, size in needed_records])
984
for (record_id, pos, size), (pos, data) in izip(iter(records), response):
985
content, digest = self._parse_record(record_id, data)
986
self._records[record_id] = (digest, content)
988
for version_id, pos, size in records:
989
yield version_id, copy(self._records[version_id][1]), copy(self._records[version_id][0])
991
def read_records(self, records):
992
"""Read records into a dictionary."""
994
for record_id, content, digest in self.read_records_iter(records):
995
components[record_id] = (content, digest)
999
class InterKnit(InterVersionedFile):
1000
"""Optimised code paths for knit to knit operations."""
1002
_matching_file_factory = KnitVersionedFile
1005
def is_compatible(source, target):
1006
"""Be compatible with knits. """
1008
return (isinstance(source, KnitVersionedFile) and
1009
isinstance(target, KnitVersionedFile))
1010
except AttributeError:
1013
def join(self, pb=None, msg=None, version_ids=None, ignore_missing=False):
1014
"""See InterVersionedFile.join."""
1015
assert isinstance(self.source, KnitVersionedFile)
1016
assert isinstance(self.target, KnitVersionedFile)
1018
if version_ids is None:
1019
version_ids = self.source.versions()
1021
if not ignore_missing:
1022
self.source._check_versions_present(version_ids)
1024
version_ids = set(self.source.versions()).intersection(
1030
pb = bzrlib.ui.ui_factory.nested_progress_bar()
1032
version_ids = list(version_ids)
1033
if None in version_ids:
1034
version_ids.remove(None)
1036
self.source_ancestry = set(self.source.get_ancestry(version_ids))
1037
this_versions = set(self.target._index.get_versions())
1038
needed_versions = self.source_ancestry - this_versions
1039
cross_check_versions = self.source_ancestry.intersection(this_versions)
1040
mismatched_versions = set()
1041
for version in cross_check_versions:
1042
# scan to include needed parents.
1043
n1 = set(self.target.get_parents_with_ghosts(version))
1044
n2 = set(self.source.get_parents_with_ghosts(version))
1046
# FIXME TEST this check for cycles being introduced works
1047
# the logic is we have a cycle if in our graph we are an
1048
# ancestor of any of the n2 revisions.
1054
parent_ancestors = self.source.get_ancestry(parent)
1055
if version in parent_ancestors:
1056
raise errors.GraphCycleError([parent, version])
1057
# ensure this parent will be available later.
1058
new_parents = n2.difference(n1)
1059
needed_versions.update(new_parents.difference(this_versions))
1060
mismatched_versions.add(version)
1062
if not needed_versions and not cross_check_versions:
1064
full_list = topo_sort(self.source.get_graph())
1066
version_list = [i for i in full_list if (not self.target.has_version(i)
1067
and i in needed_versions)]
1070
for version_id in version_list:
1071
data_pos, data_size = self.source._index.get_position(version_id)
1072
records.append((version_id, data_pos, data_size))
1075
for version_id, lines, digest \
1076
in self.source._data.read_records_iter(records):
1077
options = self.source._index.get_options(version_id)
1078
parents = self.source._index.get_parents_with_ghosts(version_id)
1080
for parent in parents:
1081
# if source has the parent, we must hav grabbed it first.
1082
assert (self.target.has_version(parent) or not
1083
self.source.has_version(parent))
1085
if self.target.factory.annotated:
1086
# FIXME jrydberg: it should be possible to skip
1087
# re-annotating components if we know that we are
1088
# going to pull all revisions in the same order.
1089
new_version_id = version_id
1090
new_version_idx = self.target._index.num_versions()
1091
if 'fulltext' in options:
1092
lines = self.target._reannotate_fulltext(self.source, lines,
1093
new_version_id, new_version_idx)
1094
elif 'line-delta' in options:
1095
lines = self.target._reannotate_line_delta(self.source, lines,
1096
new_version_id, new_version_idx)
1099
pb.update("Joining knit", count, len(version_list))
1101
pos, size = self.target._data.add_record(version_id, digest, lines)
1102
self.target._index.add_version(version_id, options, pos, size, parents)
1104
for version in mismatched_versions:
1105
n1 = set(self.target.get_parents_with_ghosts(version))
1106
n2 = set(self.source.get_parents_with_ghosts(version))
1107
# write a combined record to our history preserving the current
1108
# parents as first in the list
1109
new_parents = self.target.get_parents_with_ghosts(version) + list(n2.difference(n1))
1110
self.target.fix_parents(version, new_parents)
1117
InterVersionedFile.register_optimiser(InterKnit)