1
# Copyright (C) 2005, 2006, 2007, 2008 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
from cStringIO import StringIO
56
from itertools import izip, chain
61
from bzrlib.lazy_import import lazy_import
62
lazy_import(globals(), """
82
from bzrlib.errors import (
90
RevisionAlreadyPresent,
93
from bzrlib.osutils import (
100
from bzrlib.versionedfile import (
101
AbsentContentFactory,
105
ChunkedContentFactory,
112
# TODO: Split out code specific to this format into an associated object.
114
# TODO: Can we put in some kind of value to check that the index and data
115
# files belong together?
117
# TODO: accommodate binaries, perhaps by storing a byte count
119
# TODO: function to check whole file
121
# TODO: atomically append data, then measure backwards from the cursor
122
# position after writing to work out where it was located. we may need to
123
# bypass python file buffering.
125
DATA_SUFFIX = '.knit'
126
INDEX_SUFFIX = '.kndx'
127
_STREAM_MIN_BUFFER_SIZE = 5*1024*1024
130
class KnitAdapter(object):
131
"""Base class for knit record adaption."""
133
def __init__(self, basis_vf):
134
"""Create an adapter which accesses full texts from basis_vf.
136
:param basis_vf: A versioned file to access basis texts of deltas from.
137
May be None for adapters that do not need to access basis texts.
139
self._data = KnitVersionedFiles(None, None)
140
self._annotate_factory = KnitAnnotateFactory()
141
self._plain_factory = KnitPlainFactory()
142
self._basis_vf = basis_vf
145
class FTAnnotatedToUnannotated(KnitAdapter):
146
"""An adapter from FT annotated knits to unannotated ones."""
148
def get_bytes(self, factory):
149
annotated_compressed_bytes = factory._raw_record
151
self._data._parse_record_unchecked(annotated_compressed_bytes)
152
content = self._annotate_factory.parse_fulltext(contents, rec[1])
153
size, bytes = self._data._record_to_data((rec[1],), rec[3], content.text())
157
class DeltaAnnotatedToUnannotated(KnitAdapter):
158
"""An adapter for deltas from annotated to unannotated."""
160
def get_bytes(self, factory):
161
annotated_compressed_bytes = factory._raw_record
163
self._data._parse_record_unchecked(annotated_compressed_bytes)
164
delta = self._annotate_factory.parse_line_delta(contents, rec[1],
166
contents = self._plain_factory.lower_line_delta(delta)
167
size, bytes = self._data._record_to_data((rec[1],), rec[3], contents)
171
class FTAnnotatedToFullText(KnitAdapter):
172
"""An adapter from FT annotated knits to unannotated ones."""
174
def get_bytes(self, factory):
175
annotated_compressed_bytes = factory._raw_record
177
self._data._parse_record_unchecked(annotated_compressed_bytes)
178
content, delta = self._annotate_factory.parse_record(factory.key[-1],
179
contents, factory._build_details, None)
180
return ''.join(content.text())
183
class DeltaAnnotatedToFullText(KnitAdapter):
184
"""An adapter for deltas from annotated to unannotated."""
186
def get_bytes(self, factory):
187
annotated_compressed_bytes = factory._raw_record
189
self._data._parse_record_unchecked(annotated_compressed_bytes)
190
delta = self._annotate_factory.parse_line_delta(contents, rec[1],
192
compression_parent = factory.parents[0]
193
basis_entry = self._basis_vf.get_record_stream(
194
[compression_parent], 'unordered', True).next()
195
if basis_entry.storage_kind == 'absent':
196
raise errors.RevisionNotPresent(compression_parent, self._basis_vf)
197
basis_chunks = basis_entry.get_bytes_as('chunked')
198
basis_lines = osutils.chunks_to_lines(basis_chunks)
199
# Manually apply the delta because we have one annotated content and
201
basis_content = PlainKnitContent(basis_lines, compression_parent)
202
basis_content.apply_delta(delta, rec[1])
203
basis_content._should_strip_eol = factory._build_details[1]
204
return ''.join(basis_content.text())
207
class FTPlainToFullText(KnitAdapter):
208
"""An adapter from FT plain knits to unannotated ones."""
210
def get_bytes(self, factory):
211
compressed_bytes = factory._raw_record
213
self._data._parse_record_unchecked(compressed_bytes)
214
content, delta = self._plain_factory.parse_record(factory.key[-1],
215
contents, factory._build_details, None)
216
return ''.join(content.text())
219
class DeltaPlainToFullText(KnitAdapter):
220
"""An adapter for deltas from annotated to unannotated."""
222
def get_bytes(self, factory):
223
compressed_bytes = factory._raw_record
225
self._data._parse_record_unchecked(compressed_bytes)
226
delta = self._plain_factory.parse_line_delta(contents, rec[1])
227
compression_parent = factory.parents[0]
228
# XXX: string splitting overhead.
229
basis_entry = self._basis_vf.get_record_stream(
230
[compression_parent], 'unordered', True).next()
231
if basis_entry.storage_kind == 'absent':
232
raise errors.RevisionNotPresent(compression_parent, self._basis_vf)
233
basis_chunks = basis_entry.get_bytes_as('chunked')
234
basis_lines = osutils.chunks_to_lines(basis_chunks)
235
basis_content = PlainKnitContent(basis_lines, compression_parent)
236
# Manually apply the delta because we have one annotated content and
238
content, _ = self._plain_factory.parse_record(rec[1], contents,
239
factory._build_details, basis_content)
240
return ''.join(content.text())
243
class KnitContentFactory(ContentFactory):
244
"""Content factory for streaming from knits.
246
:seealso ContentFactory:
249
def __init__(self, key, parents, build_details, sha1, raw_record,
250
annotated, knit=None, network_bytes=None):
251
"""Create a KnitContentFactory for key.
254
:param parents: The parents.
255
:param build_details: The build details as returned from
257
:param sha1: The sha1 expected from the full text of this object.
258
:param raw_record: The bytes of the knit data from disk.
259
:param annotated: True if the raw data is annotated.
260
:param network_bytes: None to calculate the network bytes on demand,
261
not-none if they are already known.
263
ContentFactory.__init__(self)
266
self.parents = parents
267
if build_details[0] == 'line-delta':
272
annotated_kind = 'annotated-'
275
self.storage_kind = 'knit-%s%s-gz' % (annotated_kind, kind)
276
self._raw_record = raw_record
277
self._network_bytes = network_bytes
278
self._build_details = build_details
281
def _create_network_bytes(self):
282
"""Create a fully serialised network version for transmission."""
283
# storage_kind, key, parents, Noeol, raw_record
284
key_bytes = '\x00'.join(self.key)
285
if self.parents is None:
286
parent_bytes = 'None:'
288
parent_bytes = '\t'.join('\x00'.join(key) for key in self.parents)
289
if self._build_details[1]:
293
network_bytes = "%s\n%s\n%s\n%s%s" % (self.storage_kind, key_bytes,
294
parent_bytes, noeol, self._raw_record)
295
self._network_bytes = network_bytes
297
def get_bytes_as(self, storage_kind):
298
if storage_kind == self.storage_kind:
299
if self._network_bytes is None:
300
self._create_network_bytes()
301
return self._network_bytes
302
if self._knit is not None:
303
if storage_kind == 'chunked':
304
return self._knit.get_lines(self.key[0])
305
elif storage_kind == 'fulltext':
306
return self._knit.get_text(self.key[0])
307
raise errors.UnavailableRepresentation(self.key, storage_kind,
311
class LazyKnitContentFactory(ContentFactory):
312
"""A ContentFactory which can either generate full text or a wire form.
314
:seealso ContentFactory:
317
def __init__(self, key, parents, generator, first):
318
"""Create a LazyKnitContentFactory.
320
:param key: The key of the record.
321
:param parents: The parents of the record.
322
:param generator: A _ContentMapGenerator containing the record for this
324
:param first: Is this the first content object returned from generator?
325
if it is, its storage kind is knit-delta-closure, otherwise it is
326
knit-delta-closure-ref
329
self.parents = parents
331
self._generator = generator
332
self.storage_kind = "knit-delta-closure"
334
self.storage_kind = self.storage_kind + "-ref"
337
def get_bytes_as(self, storage_kind):
338
if storage_kind == self.storage_kind:
340
return self._generator._wire_bytes()
342
# all the keys etc are contained in the bytes returned in the
345
if storage_kind in ('chunked', 'fulltext'):
346
chunks = self._generator._get_one_work(self.key).text()
347
if storage_kind == 'chunked':
350
return ''.join(chunks)
351
raise errors.UnavailableRepresentation(self.key, storage_kind,
355
def knit_delta_closure_to_records(storage_kind, bytes, line_end):
356
"""Convert a network record to a iterator over stream records.
358
:param storage_kind: The storage kind of the record.
359
Must be 'knit-delta-closure'.
360
:param bytes: The bytes of the record on the network.
362
generator = _NetworkContentMapGenerator(bytes, line_end)
363
return generator.get_record_stream()
366
def knit_network_to_record(storage_kind, bytes, line_end):
367
"""Convert a network record to a record object.
369
:param storage_kind: The storage kind of the record.
370
:param bytes: The bytes of the record on the network.
373
line_end = bytes.find('\n', start)
374
key = tuple(bytes[start:line_end].split('\x00'))
376
line_end = bytes.find('\n', start)
377
parent_line = bytes[start:line_end]
378
if parent_line == 'None:':
382
[tuple(segment.split('\x00')) for segment in parent_line.split('\t')
385
noeol = bytes[start] == 'N'
386
if 'ft' in storage_kind:
389
method = 'line-delta'
390
build_details = (method, noeol)
392
raw_record = bytes[start:]
393
annotated = 'annotated' in storage_kind
394
return [KnitContentFactory(key, parents, build_details, None, raw_record,
395
annotated, network_bytes=bytes)]
398
class KnitContent(object):
399
"""Content of a knit version to which deltas can be applied.
401
This is always stored in memory as a list of lines with \n at the end,
402
plus a flag saying if the final ending is really there or not, because that
403
corresponds to the on-disk knit representation.
407
self._should_strip_eol = False
409
def apply_delta(self, delta, new_version_id):
410
"""Apply delta to this object to become new_version_id."""
411
raise NotImplementedError(self.apply_delta)
413
def line_delta_iter(self, new_lines):
414
"""Generate line-based delta from this content to new_lines."""
415
new_texts = new_lines.text()
416
old_texts = self.text()
417
s = patiencediff.PatienceSequenceMatcher(None, old_texts, new_texts)
418
for tag, i1, i2, j1, j2 in s.get_opcodes():
421
# ofrom, oto, length, data
422
yield i1, i2, j2 - j1, new_lines._lines[j1:j2]
424
def line_delta(self, new_lines):
425
return list(self.line_delta_iter(new_lines))
428
def get_line_delta_blocks(knit_delta, source, target):
429
"""Extract SequenceMatcher.get_matching_blocks() from a knit delta"""
430
target_len = len(target)
433
for s_begin, s_end, t_len, new_text in knit_delta:
434
true_n = s_begin - s_pos
437
# knit deltas do not provide reliable info about whether the
438
# last line of a file matches, due to eol handling.
439
if source[s_pos + n -1] != target[t_pos + n -1]:
442
yield s_pos, t_pos, n
443
t_pos += t_len + true_n
445
n = target_len - t_pos
447
if source[s_pos + n -1] != target[t_pos + n -1]:
450
yield s_pos, t_pos, n
451
yield s_pos + (target_len - t_pos), target_len, 0
454
class AnnotatedKnitContent(KnitContent):
455
"""Annotated content."""
457
def __init__(self, lines):
458
KnitContent.__init__(self)
462
"""Return a list of (origin, text) for each content line."""
463
lines = self._lines[:]
464
if self._should_strip_eol:
465
origin, last_line = lines[-1]
466
lines[-1] = (origin, last_line.rstrip('\n'))
469
def apply_delta(self, delta, new_version_id):
470
"""Apply delta to this object to become new_version_id."""
473
for start, end, count, delta_lines in delta:
474
lines[offset+start:offset+end] = delta_lines
475
offset = offset + (start - end) + count
479
lines = [text for origin, text in self._lines]
480
except ValueError, e:
481
# most commonly (only?) caused by the internal form of the knit
482
# missing annotation information because of a bug - see thread
484
raise KnitCorrupt(self,
485
"line in annotated knit missing annotation information: %s"
487
if self._should_strip_eol:
488
lines[-1] = lines[-1].rstrip('\n')
492
return AnnotatedKnitContent(self._lines[:])
495
class PlainKnitContent(KnitContent):
496
"""Unannotated content.
498
When annotate[_iter] is called on this content, the same version is reported
499
for all lines. Generally, annotate[_iter] is not useful on PlainKnitContent
503
def __init__(self, lines, version_id):
504
KnitContent.__init__(self)
506
self._version_id = version_id
509
"""Return a list of (origin, text) for each content line."""
510
return [(self._version_id, line) for line in self._lines]
512
def apply_delta(self, delta, new_version_id):
513
"""Apply delta to this object to become new_version_id."""
516
for start, end, count, delta_lines in delta:
517
lines[offset+start:offset+end] = delta_lines
518
offset = offset + (start - end) + count
519
self._version_id = new_version_id
522
return PlainKnitContent(self._lines[:], self._version_id)
526
if self._should_strip_eol:
528
lines[-1] = lines[-1].rstrip('\n')
532
class _KnitFactory(object):
533
"""Base class for common Factory functions."""
535
def parse_record(self, version_id, record, record_details,
536
base_content, copy_base_content=True):
537
"""Parse a record into a full content object.
539
:param version_id: The official version id for this content
540
:param record: The data returned by read_records_iter()
541
:param record_details: Details about the record returned by
543
:param base_content: If get_build_details returns a compression_parent,
544
you must return a base_content here, else use None
545
:param copy_base_content: When building from the base_content, decide
546
you can either copy it and return a new object, or modify it in
548
:return: (content, delta) A Content object and possibly a line-delta,
551
method, noeol = record_details
552
if method == 'line-delta':
553
if copy_base_content:
554
content = base_content.copy()
556
content = base_content
557
delta = self.parse_line_delta(record, version_id)
558
content.apply_delta(delta, version_id)
560
content = self.parse_fulltext(record, version_id)
562
content._should_strip_eol = noeol
563
return (content, delta)
566
class KnitAnnotateFactory(_KnitFactory):
567
"""Factory for creating annotated Content objects."""
571
def make(self, lines, version_id):
572
num_lines = len(lines)
573
return AnnotatedKnitContent(zip([version_id] * num_lines, lines))
575
def parse_fulltext(self, content, version_id):
576
"""Convert fulltext to internal representation
578
fulltext content is of the format
579
revid(utf8) plaintext\n
580
internal representation is of the format:
583
# TODO: jam 20070209 The tests expect this to be returned as tuples,
584
# but the code itself doesn't really depend on that.
585
# Figure out a way to not require the overhead of turning the
586
# list back into tuples.
587
lines = [tuple(line.split(' ', 1)) for line in content]
588
return AnnotatedKnitContent(lines)
590
def parse_line_delta_iter(self, lines):
591
return iter(self.parse_line_delta(lines))
593
def parse_line_delta(self, lines, version_id, plain=False):
594
"""Convert a line based delta into internal representation.
596
line delta is in the form of:
597
intstart intend intcount
599
revid(utf8) newline\n
600
internal representation is
601
(start, end, count, [1..count tuples (revid, newline)])
603
:param plain: If True, the lines are returned as a plain
604
list without annotations, not as a list of (origin, content) tuples, i.e.
605
(start, end, count, [1..count newline])
612
def cache_and_return(line):
613
origin, text = line.split(' ', 1)
614
return cache.setdefault(origin, origin), text
616
# walk through the lines parsing.
617
# Note that the plain test is explicitly pulled out of the
618
# loop to minimise any performance impact
621
start, end, count = [int(n) for n in header.split(',')]
622
contents = [next().split(' ', 1)[1] for i in xrange(count)]
623
result.append((start, end, count, contents))
626
start, end, count = [int(n) for n in header.split(',')]
627
contents = [tuple(next().split(' ', 1)) for i in xrange(count)]
628
result.append((start, end, count, contents))
631
def get_fulltext_content(self, lines):
632
"""Extract just the content lines from a fulltext."""
633
return (line.split(' ', 1)[1] for line in lines)
635
def get_linedelta_content(self, lines):
636
"""Extract just the content from a line delta.
638
This doesn't return all of the extra information stored in a delta.
639
Only the actual content lines.
644
header = header.split(',')
645
count = int(header[2])
646
for i in xrange(count):
647
origin, text = next().split(' ', 1)
650
def lower_fulltext(self, content):
651
"""convert a fulltext content record into a serializable form.
653
see parse_fulltext which this inverts.
655
# TODO: jam 20070209 We only do the caching thing to make sure that
656
# the origin is a valid utf-8 line, eventually we could remove it
657
return ['%s %s' % (o, t) for o, t in content._lines]
659
def lower_line_delta(self, delta):
660
"""convert a delta into a serializable form.
662
See parse_line_delta which this inverts.
664
# TODO: jam 20070209 We only do the caching thing to make sure that
665
# the origin is a valid utf-8 line, eventually we could remove it
667
for start, end, c, lines in delta:
668
out.append('%d,%d,%d\n' % (start, end, c))
669
out.extend(origin + ' ' + text
670
for origin, text in lines)
673
def annotate(self, knit, key):
674
content = knit._get_content(key)
675
# adjust for the fact that serialised annotations are only key suffixes
677
if type(key) == tuple:
679
origins = content.annotate()
681
for origin, line in origins:
682
result.append((prefix + (origin,), line))
685
# XXX: This smells a bit. Why would key ever be a non-tuple here?
686
# Aren't keys defined to be tuples? -- spiv 20080618
687
return content.annotate()
690
class KnitPlainFactory(_KnitFactory):
691
"""Factory for creating plain Content objects."""
695
def make(self, lines, version_id):
696
return PlainKnitContent(lines, version_id)
698
def parse_fulltext(self, content, version_id):
699
"""This parses an unannotated fulltext.
701
Note that this is not a noop - the internal representation
702
has (versionid, line) - its just a constant versionid.
704
return self.make(content, version_id)
706
def parse_line_delta_iter(self, lines, version_id):
708
num_lines = len(lines)
709
while cur < num_lines:
712
start, end, c = [int(n) for n in header.split(',')]
713
yield start, end, c, lines[cur:cur+c]
716
def parse_line_delta(self, lines, version_id):
717
return list(self.parse_line_delta_iter(lines, version_id))
719
def get_fulltext_content(self, lines):
720
"""Extract just the content lines from a fulltext."""
723
def get_linedelta_content(self, lines):
724
"""Extract just the content from a line delta.
726
This doesn't return all of the extra information stored in a delta.
727
Only the actual content lines.
732
header = header.split(',')
733
count = int(header[2])
734
for i in xrange(count):
737
def lower_fulltext(self, content):
738
return content.text()
740
def lower_line_delta(self, delta):
742
for start, end, c, lines in delta:
743
out.append('%d,%d,%d\n' % (start, end, c))
747
def annotate(self, knit, key):
748
annotator = _KnitAnnotator(knit)
749
return annotator.annotate(key)
753
def make_file_factory(annotated, mapper):
754
"""Create a factory for creating a file based KnitVersionedFiles.
756
This is only functional enough to run interface tests, it doesn't try to
757
provide a full pack environment.
759
:param annotated: knit annotations are wanted.
760
:param mapper: The mapper from keys to paths.
762
def factory(transport):
763
index = _KndxIndex(transport, mapper, lambda:None, lambda:True, lambda:True)
764
access = _KnitKeyAccess(transport, mapper)
765
return KnitVersionedFiles(index, access, annotated=annotated)
769
def make_pack_factory(graph, delta, keylength):
770
"""Create a factory for creating a pack based VersionedFiles.
772
This is only functional enough to run interface tests, it doesn't try to
773
provide a full pack environment.
775
:param graph: Store a graph.
776
:param delta: Delta compress contents.
777
:param keylength: How long should keys be.
779
def factory(transport):
780
parents = graph or delta
786
max_delta_chain = 200
789
graph_index = _mod_index.InMemoryGraphIndex(reference_lists=ref_length,
790
key_elements=keylength)
791
stream = transport.open_write_stream('newpack')
792
writer = pack.ContainerWriter(stream.write)
794
index = _KnitGraphIndex(graph_index, lambda:True, parents=parents,
795
deltas=delta, add_callback=graph_index.add_nodes)
796
access = _DirectPackAccess({})
797
access.set_writer(writer, graph_index, (transport, 'newpack'))
798
result = KnitVersionedFiles(index, access,
799
max_delta_chain=max_delta_chain)
800
result.stream = stream
801
result.writer = writer
806
def cleanup_pack_knit(versioned_files):
807
versioned_files.stream.close()
808
versioned_files.writer.end()
811
def _get_total_build_size(self, keys, positions):
812
"""Determine the total bytes to build these keys.
814
(helper function because _KnitGraphIndex and _KndxIndex work the same, but
815
don't inherit from a common base.)
817
:param keys: Keys that we want to build
818
:param positions: dict of {key, (info, index_memo, comp_parent)} (such
819
as returned by _get_components_positions)
820
:return: Number of bytes to build those keys
822
all_build_index_memos = {}
826
for key in build_keys:
827
# This is mostly for the 'stacked' case
828
# Where we will be getting the data from a fallback
829
if key not in positions:
831
_, index_memo, compression_parent = positions[key]
832
all_build_index_memos[key] = index_memo
833
if compression_parent not in all_build_index_memos:
834
next_keys.add(compression_parent)
835
build_keys = next_keys
836
return sum([index_memo[2] for index_memo
837
in all_build_index_memos.itervalues()])
840
class KnitVersionedFiles(VersionedFiles):
841
"""Storage for many versioned files using knit compression.
843
Backend storage is managed by indices and data objects.
845
:ivar _index: A _KnitGraphIndex or similar that can describe the
846
parents, graph, compression and data location of entries in this
847
KnitVersionedFiles. Note that this is only the index for
848
*this* vfs; if there are fallbacks they must be queried separately.
851
def __init__(self, index, data_access, max_delta_chain=200,
852
annotated=False, reload_func=None):
853
"""Create a KnitVersionedFiles with index and data_access.
855
:param index: The index for the knit data.
856
:param data_access: The access object to store and retrieve knit
858
:param max_delta_chain: The maximum number of deltas to permit during
859
insertion. Set to 0 to prohibit the use of deltas.
860
:param annotated: Set to True to cause annotations to be calculated and
861
stored during insertion.
862
:param reload_func: An function that can be called if we think we need
863
to reload the pack listing and try again. See
864
'bzrlib.repofmt.pack_repo.AggregateIndex' for the signature.
867
self._access = data_access
868
self._max_delta_chain = max_delta_chain
870
self._factory = KnitAnnotateFactory()
872
self._factory = KnitPlainFactory()
873
self._fallback_vfs = []
874
self._reload_func = reload_func
877
return "%s(%r, %r)" % (
878
self.__class__.__name__,
882
def add_fallback_versioned_files(self, a_versioned_files):
883
"""Add a source of texts for texts not present in this knit.
885
:param a_versioned_files: A VersionedFiles object.
887
self._fallback_vfs.append(a_versioned_files)
889
def add_lines(self, key, parents, lines, parent_texts=None,
890
left_matching_blocks=None, nostore_sha=None, random_id=False,
892
"""See VersionedFiles.add_lines()."""
893
self._index._check_write_ok()
894
self._check_add(key, lines, random_id, check_content)
896
# The caller might pass None if there is no graph data, but kndx
897
# indexes can't directly store that, so we give them
898
# an empty tuple instead.
900
return self._add(key, lines, parents,
901
parent_texts, left_matching_blocks, nostore_sha, random_id)
903
def _add(self, key, lines, parents, parent_texts,
904
left_matching_blocks, nostore_sha, random_id):
905
"""Add a set of lines on top of version specified by parents.
907
Any versions not present will be converted into ghosts.
909
# first thing, if the content is something we don't need to store, find
911
line_bytes = ''.join(lines)
912
digest = sha_string(line_bytes)
913
if nostore_sha == digest:
914
raise errors.ExistingContent
917
if parent_texts is None:
919
# Do a single query to ascertain parent presence; we only compress
920
# against parents in the same kvf.
921
present_parent_map = self._index.get_parent_map(parents)
922
for parent in parents:
923
if parent in present_parent_map:
924
present_parents.append(parent)
926
# Currently we can only compress against the left most present parent.
927
if (len(present_parents) == 0 or
928
present_parents[0] != parents[0]):
931
# To speed the extract of texts the delta chain is limited
932
# to a fixed number of deltas. This should minimize both
933
# I/O and the time spend applying deltas.
934
delta = self._check_should_delta(present_parents[0])
936
text_length = len(line_bytes)
939
if lines[-1][-1] != '\n':
940
# copy the contents of lines.
942
options.append('no-eol')
943
lines[-1] = lines[-1] + '\n'
947
if type(element) != str:
948
raise TypeError("key contains non-strings: %r" % (key,))
949
# Knit hunks are still last-element only
951
content = self._factory.make(lines, version_id)
952
if 'no-eol' in options:
953
# Hint to the content object that its text() call should strip the
955
content._should_strip_eol = True
956
if delta or (self._factory.annotated and len(present_parents) > 0):
957
# Merge annotations from parent texts if needed.
958
delta_hunks = self._merge_annotations(content, present_parents,
959
parent_texts, delta, self._factory.annotated,
960
left_matching_blocks)
963
options.append('line-delta')
964
store_lines = self._factory.lower_line_delta(delta_hunks)
965
size, bytes = self._record_to_data(key, digest,
968
options.append('fulltext')
969
# isinstance is slower and we have no hierarchy.
970
if self._factory.__class__ is KnitPlainFactory:
971
# Use the already joined bytes saving iteration time in
973
size, bytes = self._record_to_data(key, digest,
976
# get mixed annotation + content and feed it into the
978
store_lines = self._factory.lower_fulltext(content)
979
size, bytes = self._record_to_data(key, digest,
982
access_memo = self._access.add_raw_records([(key, size)], bytes)[0]
983
self._index.add_records(
984
((key, options, access_memo, parents),),
986
return digest, text_length, content
988
def annotate(self, key):
989
"""See VersionedFiles.annotate."""
990
return self._factory.annotate(self, key)
992
def check(self, progress_bar=None):
993
"""See VersionedFiles.check()."""
994
# This doesn't actually test extraction of everything, but that will
995
# impact 'bzr check' substantially, and needs to be integrated with
996
# care. However, it does check for the obvious problem of a delta with
998
keys = self._index.keys()
999
parent_map = self.get_parent_map(keys)
1001
if self._index.get_method(key) != 'fulltext':
1002
compression_parent = parent_map[key][0]
1003
if compression_parent not in parent_map:
1004
raise errors.KnitCorrupt(self,
1005
"Missing basis parent %s for %s" % (
1006
compression_parent, key))
1007
for fallback_vfs in self._fallback_vfs:
1008
fallback_vfs.check()
1010
def _check_add(self, key, lines, random_id, check_content):
1011
"""check that version_id and lines are safe to add."""
1012
version_id = key[-1]
1013
if contains_whitespace(version_id):
1014
raise InvalidRevisionId(version_id, self)
1015
self.check_not_reserved_id(version_id)
1016
# TODO: If random_id==False and the key is already present, we should
1017
# probably check that the existing content is identical to what is
1018
# being inserted, and otherwise raise an exception. This would make
1019
# the bundle code simpler.
1021
self._check_lines_not_unicode(lines)
1022
self._check_lines_are_lines(lines)
1024
def _check_header(self, key, line):
1025
rec = self._split_header(line)
1026
self._check_header_version(rec, key[-1])
1029
def _check_header_version(self, rec, version_id):
1030
"""Checks the header version on original format knit records.
1032
These have the last component of the key embedded in the record.
1034
if rec[1] != version_id:
1035
raise KnitCorrupt(self,
1036
'unexpected version, wanted %r, got %r' % (version_id, rec[1]))
1038
def _check_should_delta(self, parent):
1039
"""Iterate back through the parent listing, looking for a fulltext.
1041
This is used when we want to decide whether to add a delta or a new
1042
fulltext. It searches for _max_delta_chain parents. When it finds a
1043
fulltext parent, it sees if the total size of the deltas leading up to
1044
it is large enough to indicate that we want a new full text anyway.
1046
Return True if we should create a new delta, False if we should use a
1050
fulltext_size = None
1051
for count in xrange(self._max_delta_chain):
1053
# Note that this only looks in the index of this particular
1054
# KnitVersionedFiles, not in the fallbacks. This ensures that
1055
# we won't store a delta spanning physical repository
1057
build_details = self._index.get_build_details([parent])
1058
parent_details = build_details[parent]
1059
except (RevisionNotPresent, KeyError), e:
1060
# Some basis is not locally present: always fulltext
1062
index_memo, compression_parent, _, _ = parent_details
1063
_, _, size = index_memo
1064
if compression_parent is None:
1065
fulltext_size = size
1068
# We don't explicitly check for presence because this is in an
1069
# inner loop, and if it's missing it'll fail anyhow.
1070
parent = compression_parent
1072
# We couldn't find a fulltext, so we must create a new one
1074
# Simple heuristic - if the total I/O wold be greater as a delta than
1075
# the originally installed fulltext, we create a new fulltext.
1076
return fulltext_size > delta_size
1078
def _build_details_to_components(self, build_details):
1079
"""Convert a build_details tuple to a position tuple."""
1080
# record_details, access_memo, compression_parent
1081
return build_details[3], build_details[0], build_details[1]
1083
def _get_components_positions(self, keys, allow_missing=False):
1084
"""Produce a map of position data for the components of keys.
1086
This data is intended to be used for retrieving the knit records.
1088
A dict of key to (record_details, index_memo, next, parents) is
1090
method is the way referenced data should be applied.
1091
index_memo is the handle to pass to the data access to actually get the
1093
next is the build-parent of the version, or None for fulltexts.
1094
parents is the version_ids of the parents of this version
1096
:param allow_missing: If True do not raise an error on a missing component,
1100
pending_components = keys
1101
while pending_components:
1102
build_details = self._index.get_build_details(pending_components)
1103
current_components = set(pending_components)
1104
pending_components = set()
1105
for key, details in build_details.iteritems():
1106
(index_memo, compression_parent, parents,
1107
record_details) = details
1108
method = record_details[0]
1109
if compression_parent is not None:
1110
pending_components.add(compression_parent)
1111
component_data[key] = self._build_details_to_components(details)
1112
missing = current_components.difference(build_details)
1113
if missing and not allow_missing:
1114
raise errors.RevisionNotPresent(missing.pop(), self)
1115
return component_data
1117
def _get_content(self, key, parent_texts={}):
1118
"""Returns a content object that makes up the specified
1120
cached_version = parent_texts.get(key, None)
1121
if cached_version is not None:
1122
# Ensure the cache dict is valid.
1123
if not self.get_parent_map([key]):
1124
raise RevisionNotPresent(key, self)
1125
return cached_version
1126
generator = _VFContentMapGenerator(self, [key])
1127
return generator._get_content(key)
1129
def get_parent_map(self, keys):
1130
"""Get a map of the graph parents of keys.
1132
:param keys: The keys to look up parents for.
1133
:return: A mapping from keys to parents. Absent keys are absent from
1136
return self._get_parent_map_with_sources(keys)[0]
1138
def _get_parent_map_with_sources(self, keys):
1139
"""Get a map of the parents of keys.
1141
:param keys: The keys to look up parents for.
1142
:return: A tuple. The first element is a mapping from keys to parents.
1143
Absent keys are absent from the mapping. The second element is a
1144
list with the locations each key was found in. The first element
1145
is the in-this-knit parents, the second the first fallback source,
1149
sources = [self._index] + self._fallback_vfs
1152
for source in sources:
1155
new_result = source.get_parent_map(missing)
1156
source_results.append(new_result)
1157
result.update(new_result)
1158
missing.difference_update(set(new_result))
1159
return result, source_results
1161
def _get_record_map(self, keys, allow_missing=False):
1162
"""Produce a dictionary of knit records.
1164
:return: {key:(record, record_details, digest, next)}
1166
data returned from read_records (a KnitContentobject)
1168
opaque information to pass to parse_record
1170
SHA1 digest of the full text after all steps are done
1172
build-parent of the version, i.e. the leftmost ancestor.
1173
Will be None if the record is not a delta.
1174
:param keys: The keys to build a map for
1175
:param allow_missing: If some records are missing, rather than
1176
error, just return the data that could be generated.
1178
raw_map = self._get_record_map_unparsed(keys,
1179
allow_missing=allow_missing)
1180
return self._raw_map_to_record_map(raw_map)
1182
def _raw_map_to_record_map(self, raw_map):
1183
"""Parse the contents of _get_record_map_unparsed.
1185
:return: see _get_record_map.
1189
data, record_details, next = raw_map[key]
1190
content, digest = self._parse_record(key[-1], data)
1191
result[key] = content, record_details, digest, next
1194
def _get_record_map_unparsed(self, keys, allow_missing=False):
1195
"""Get the raw data for reconstructing keys without parsing it.
1197
:return: A dict suitable for parsing via _raw_map_to_record_map.
1198
key-> raw_bytes, (method, noeol), compression_parent
1200
# This retries the whole request if anything fails. Potentially we
1201
# could be a bit more selective. We could track the keys whose records
1202
# we have successfully found, and then only request the new records
1203
# from there. However, _get_components_positions grabs the whole build
1204
# chain, which means we'll likely try to grab the same records again
1205
# anyway. Also, can the build chains change as part of a pack
1206
# operation? We wouldn't want to end up with a broken chain.
1209
position_map = self._get_components_positions(keys,
1210
allow_missing=allow_missing)
1211
# key = component_id, r = record_details, i_m = index_memo,
1213
records = [(key, i_m) for key, (r, i_m, n)
1214
in position_map.iteritems()]
1215
# Sort by the index memo, so that we request records from the
1216
# same pack file together, and in forward-sorted order
1217
records.sort(key=operator.itemgetter(1))
1219
for key, data in self._read_records_iter_unchecked(records):
1220
(record_details, index_memo, next) = position_map[key]
1221
raw_record_map[key] = data, record_details, next
1222
return raw_record_map
1223
except errors.RetryWithNewPacks, e:
1224
self._access.reload_or_raise(e)
1227
def _split_by_prefix(cls, keys):
1228
"""For the given keys, split them up based on their prefix.
1230
To keep memory pressure somewhat under control, split the
1231
requests back into per-file-id requests, otherwise "bzr co"
1232
extracts the full tree into memory before writing it to disk.
1233
This should be revisited if _get_content_maps() can ever cross
1236
The keys for a given file_id are kept in the same relative order.
1237
Ordering between file_ids is not, though prefix_order will return the
1238
order that the key was first seen.
1240
:param keys: An iterable of key tuples
1241
:return: (split_map, prefix_order)
1242
split_map A dictionary mapping prefix => keys
1243
prefix_order The order that we saw the various prefixes
1245
split_by_prefix = {}
1253
if prefix in split_by_prefix:
1254
split_by_prefix[prefix].append(key)
1256
split_by_prefix[prefix] = [key]
1257
prefix_order.append(prefix)
1258
return split_by_prefix, prefix_order
1260
def _group_keys_for_io(self, keys, non_local_keys, positions,
1261
_min_buffer_size=_STREAM_MIN_BUFFER_SIZE):
1262
"""For the given keys, group them into 'best-sized' requests.
1264
The idea is to avoid making 1 request per file, but to never try to
1265
unpack an entire 1.5GB source tree in a single pass. Also when
1266
possible, we should try to group requests to the same pack file
1269
:return: list of (keys, non_local) tuples that indicate what keys
1270
should be fetched next.
1272
# TODO: Ideally we would group on 2 factors. We want to extract texts
1273
# from the same pack file together, and we want to extract all
1274
# the texts for a given build-chain together. Ultimately it
1275
# probably needs a better global view.
1276
total_keys = len(keys)
1277
prefix_split_keys, prefix_order = self._split_by_prefix(keys)
1278
prefix_split_non_local_keys, _ = self._split_by_prefix(non_local_keys)
1280
cur_non_local = set()
1284
for prefix in prefix_order:
1285
keys = prefix_split_keys[prefix]
1286
non_local = prefix_split_non_local_keys.get(prefix, [])
1288
this_size = self._index._get_total_build_size(keys, positions)
1289
cur_size += this_size
1290
cur_keys.extend(keys)
1291
cur_non_local.update(non_local)
1292
if cur_size > _min_buffer_size:
1293
result.append((cur_keys, cur_non_local))
1294
sizes.append(cur_size)
1296
cur_non_local = set()
1299
result.append((cur_keys, cur_non_local))
1300
sizes.append(cur_size)
1303
def get_record_stream(self, keys, ordering, include_delta_closure):
1304
"""Get a stream of records for keys.
1306
:param keys: The keys to include.
1307
:param ordering: Either 'unordered' or 'topological'. A topologically
1308
sorted stream has compression parents strictly before their
1310
:param include_delta_closure: If True then the closure across any
1311
compression parents will be included (in the opaque data).
1312
:return: An iterator of ContentFactory objects, each of which is only
1313
valid until the iterator is advanced.
1315
# keys might be a generator
1319
if not self._index.has_graph:
1320
# Cannot sort when no graph has been stored.
1321
ordering = 'unordered'
1323
remaining_keys = keys
1326
keys = set(remaining_keys)
1327
for content_factory in self._get_remaining_record_stream(keys,
1328
ordering, include_delta_closure):
1329
remaining_keys.discard(content_factory.key)
1330
yield content_factory
1332
except errors.RetryWithNewPacks, e:
1333
self._access.reload_or_raise(e)
1335
def _get_remaining_record_stream(self, keys, ordering,
1336
include_delta_closure):
1337
"""This function is the 'retry' portion for get_record_stream."""
1338
if include_delta_closure:
1339
positions = self._get_components_positions(keys, allow_missing=True)
1341
build_details = self._index.get_build_details(keys)
1343
# (record_details, access_memo, compression_parent_key)
1344
positions = dict((key, self._build_details_to_components(details))
1345
for key, details in build_details.iteritems())
1346
absent_keys = keys.difference(set(positions))
1347
# There may be more absent keys : if we're missing the basis component
1348
# and are trying to include the delta closure.
1349
# XXX: We should not ever need to examine remote sources because we do
1350
# not permit deltas across versioned files boundaries.
1351
if include_delta_closure:
1352
needed_from_fallback = set()
1353
# Build up reconstructable_keys dict. key:True in this dict means
1354
# the key can be reconstructed.
1355
reconstructable_keys = {}
1359
chain = [key, positions[key][2]]
1361
needed_from_fallback.add(key)
1364
while chain[-1] is not None:
1365
if chain[-1] in reconstructable_keys:
1366
result = reconstructable_keys[chain[-1]]
1370
chain.append(positions[chain[-1]][2])
1372
# missing basis component
1373
needed_from_fallback.add(chain[-1])
1376
for chain_key in chain[:-1]:
1377
reconstructable_keys[chain_key] = result
1379
needed_from_fallback.add(key)
1380
# Double index lookups here : need a unified api ?
1381
global_map, parent_maps = self._get_parent_map_with_sources(keys)
1382
if ordering in ('topological', 'groupcompress'):
1383
if ordering == 'topological':
1384
# Global topological sort
1385
present_keys = tsort.topo_sort(global_map)
1387
present_keys = sort_groupcompress(global_map)
1388
# Now group by source:
1390
current_source = None
1391
for key in present_keys:
1392
for parent_map in parent_maps:
1393
if key in parent_map:
1394
key_source = parent_map
1396
if current_source is not key_source:
1397
source_keys.append((key_source, []))
1398
current_source = key_source
1399
source_keys[-1][1].append(key)
1401
if ordering != 'unordered':
1402
raise AssertionError('valid values for ordering are:'
1403
' "unordered", "groupcompress" or "topological" not: %r'
1405
# Just group by source; remote sources first.
1408
for parent_map in reversed(parent_maps):
1409
source_keys.append((parent_map, []))
1410
for key in parent_map:
1411
present_keys.append(key)
1412
source_keys[-1][1].append(key)
1413
# We have been requested to return these records in an order that
1414
# suits us. So we ask the index to give us an optimally sorted
1416
for source, sub_keys in source_keys:
1417
if source is parent_maps[0]:
1418
# Only sort the keys for this VF
1419
self._index._sort_keys_by_io(sub_keys, positions)
1420
absent_keys = keys - set(global_map)
1421
for key in absent_keys:
1422
yield AbsentContentFactory(key)
1423
# restrict our view to the keys we can answer.
1424
# XXX: Memory: TODO: batch data here to cap buffered data at (say) 1MB.
1425
# XXX: At that point we need to consider the impact of double reads by
1426
# utilising components multiple times.
1427
if include_delta_closure:
1428
# XXX: get_content_maps performs its own index queries; allow state
1430
non_local_keys = needed_from_fallback - absent_keys
1431
for keys, non_local_keys in self._group_keys_for_io(present_keys,
1434
generator = _VFContentMapGenerator(self, keys, non_local_keys,
1436
for record in generator.get_record_stream():
1439
for source, keys in source_keys:
1440
if source is parent_maps[0]:
1441
# this KnitVersionedFiles
1442
records = [(key, positions[key][1]) for key in keys]
1443
for key, raw_data, sha1 in self._read_records_iter_raw(records):
1444
(record_details, index_memo, _) = positions[key]
1445
yield KnitContentFactory(key, global_map[key],
1446
record_details, sha1, raw_data, self._factory.annotated, None)
1448
vf = self._fallback_vfs[parent_maps.index(source) - 1]
1449
for record in vf.get_record_stream(keys, ordering,
1450
include_delta_closure):
1453
def get_sha1s(self, keys):
1454
"""See VersionedFiles.get_sha1s()."""
1456
record_map = self._get_record_map(missing, allow_missing=True)
1458
for key, details in record_map.iteritems():
1459
if key not in missing:
1461
# record entry 2 is the 'digest'.
1462
result[key] = details[2]
1463
missing.difference_update(set(result))
1464
for source in self._fallback_vfs:
1467
new_result = source.get_sha1s(missing)
1468
result.update(new_result)
1469
missing.difference_update(set(new_result))
1472
def insert_record_stream(self, stream):
1473
"""Insert a record stream into this container.
1475
:param stream: A stream of records to insert.
1477
:seealso VersionedFiles.get_record_stream:
1479
def get_adapter(adapter_key):
1481
return adapters[adapter_key]
1483
adapter_factory = adapter_registry.get(adapter_key)
1484
adapter = adapter_factory(self)
1485
adapters[adapter_key] = adapter
1488
if self._factory.annotated:
1489
# self is annotated, we need annotated knits to use directly.
1490
annotated = "annotated-"
1493
# self is not annotated, but we can strip annotations cheaply.
1495
convertibles = set(["knit-annotated-ft-gz"])
1496
if self._max_delta_chain:
1497
delta_types.add("knit-annotated-delta-gz")
1498
convertibles.add("knit-annotated-delta-gz")
1499
# The set of types we can cheaply adapt without needing basis texts.
1500
native_types = set()
1501
if self._max_delta_chain:
1502
native_types.add("knit-%sdelta-gz" % annotated)
1503
delta_types.add("knit-%sdelta-gz" % annotated)
1504
native_types.add("knit-%sft-gz" % annotated)
1505
knit_types = native_types.union(convertibles)
1507
# Buffer all index entries that we can't add immediately because their
1508
# basis parent is missing. We don't buffer all because generating
1509
# annotations may require access to some of the new records. However we
1510
# can't generate annotations from new deltas until their basis parent
1511
# is present anyway, so we get away with not needing an index that
1512
# includes the new keys.
1514
# See <http://launchpad.net/bugs/300177> about ordering of compression
1515
# parents in the records - to be conservative, we insist that all
1516
# parents must be present to avoid expanding to a fulltext.
1518
# key = basis_parent, value = index entry to add
1519
buffered_index_entries = {}
1520
for record in stream:
1522
parents = record.parents
1523
if record.storage_kind in delta_types:
1524
# TODO: eventually the record itself should track
1525
# compression_parent
1526
compression_parent = parents[0]
1528
compression_parent = None
1529
# Raise an error when a record is missing.
1530
if record.storage_kind == 'absent':
1531
raise RevisionNotPresent([record.key], self)
1532
elif ((record.storage_kind in knit_types)
1533
and (compression_parent is None
1534
or not self._fallback_vfs
1535
or self._index.has_key(compression_parent)
1536
or not self.has_key(compression_parent))):
1537
# we can insert the knit record literally if either it has no
1538
# compression parent OR we already have its basis in this kvf
1539
# OR the basis is not present even in the fallbacks. In the
1540
# last case it will either turn up later in the stream and all
1541
# will be well, or it won't turn up at all and we'll raise an
1544
# TODO: self.has_key is somewhat redundant with
1545
# self._index.has_key; we really want something that directly
1546
# asks if it's only present in the fallbacks. -- mbp 20081119
1547
if record.storage_kind not in native_types:
1549
adapter_key = (record.storage_kind, "knit-delta-gz")
1550
adapter = get_adapter(adapter_key)
1552
adapter_key = (record.storage_kind, "knit-ft-gz")
1553
adapter = get_adapter(adapter_key)
1554
bytes = adapter.get_bytes(record)
1556
# It's a knit record, it has a _raw_record field (even if
1557
# it was reconstituted from a network stream).
1558
bytes = record._raw_record
1559
options = [record._build_details[0]]
1560
if record._build_details[1]:
1561
options.append('no-eol')
1562
# Just blat it across.
1563
# Note: This does end up adding data on duplicate keys. As
1564
# modern repositories use atomic insertions this should not
1565
# lead to excessive growth in the event of interrupted fetches.
1566
# 'knit' repositories may suffer excessive growth, but as a
1567
# deprecated format this is tolerable. It can be fixed if
1568
# needed by in the kndx index support raising on a duplicate
1569
# add with identical parents and options.
1570
access_memo = self._access.add_raw_records(
1571
[(record.key, len(bytes))], bytes)[0]
1572
index_entry = (record.key, options, access_memo, parents)
1573
if 'fulltext' not in options:
1574
# Not a fulltext, so we need to make sure the compression
1575
# parent will also be present.
1576
# Note that pack backed knits don't need to buffer here
1577
# because they buffer all writes to the transaction level,
1578
# but we don't expose that difference at the index level. If
1579
# the query here has sufficient cost to show up in
1580
# profiling we should do that.
1582
# They're required to be physically in this
1583
# KnitVersionedFiles, not in a fallback.
1584
if not self._index.has_key(compression_parent):
1585
pending = buffered_index_entries.setdefault(
1586
compression_parent, [])
1587
pending.append(index_entry)
1590
self._index.add_records([index_entry])
1591
elif record.storage_kind == 'chunked':
1592
self.add_lines(record.key, parents,
1593
osutils.chunks_to_lines(record.get_bytes_as('chunked')))
1595
# Not suitable for direct insertion as a
1596
# delta, either because it's not the right format, or this
1597
# KnitVersionedFiles doesn't permit deltas (_max_delta_chain ==
1598
# 0) or because it depends on a base only present in the
1601
# Try getting a fulltext directly from the record.
1602
bytes = record.get_bytes_as('fulltext')
1603
except errors.UnavailableRepresentation:
1604
adapter_key = record.storage_kind, 'fulltext'
1605
adapter = get_adapter(adapter_key)
1606
bytes = adapter.get_bytes(record)
1607
lines = split_lines(bytes)
1609
self.add_lines(record.key, parents, lines)
1610
except errors.RevisionAlreadyPresent:
1612
# Add any records whose basis parent is now available.
1614
added_keys = [record.key]
1616
key = added_keys.pop(0)
1617
if key in buffered_index_entries:
1618
index_entries = buffered_index_entries[key]
1619
self._index.add_records(index_entries)
1621
[index_entry[0] for index_entry in index_entries])
1622
del buffered_index_entries[key]
1623
if buffered_index_entries:
1624
# There were index entries buffered at the end of the stream,
1625
# So these need to be added (if the index supports holding such
1626
# entries for later insertion)
1627
for key in buffered_index_entries:
1628
index_entries = buffered_index_entries[key]
1629
self._index.add_records(index_entries,
1630
missing_compression_parents=True)
1632
def get_missing_compression_parent_keys(self):
1633
"""Return an iterable of keys of missing compression parents.
1635
Check this after calling insert_record_stream to find out if there are
1636
any missing compression parents. If there are, the records that
1637
depend on them are not able to be inserted safely. For atomic
1638
KnitVersionedFiles built on packs, the transaction should be aborted or
1639
suspended - commit will fail at this point. Nonatomic knits will error
1640
earlier because they have no staging area to put pending entries into.
1642
return self._index.get_missing_compression_parents()
1644
def iter_lines_added_or_present_in_keys(self, keys, pb=None):
1645
"""Iterate over the lines in the versioned files from keys.
1647
This may return lines from other keys. Each item the returned
1648
iterator yields is a tuple of a line and a text version that that line
1649
is present in (not introduced in).
1651
Ordering of results is in whatever order is most suitable for the
1652
underlying storage format.
1654
If a progress bar is supplied, it may be used to indicate progress.
1655
The caller is responsible for cleaning up progress bars (because this
1659
* Lines are normalised by the underlying store: they will all have \\n
1661
* Lines are returned in arbitrary order.
1662
* If a requested key did not change any lines (or didn't have any
1663
lines), it may not be mentioned at all in the result.
1665
:return: An iterator over (line, key).
1668
pb = progress.DummyProgress()
1674
# we don't care about inclusions, the caller cares.
1675
# but we need to setup a list of records to visit.
1676
# we need key, position, length
1678
build_details = self._index.get_build_details(keys)
1679
for key, details in build_details.iteritems():
1681
key_records.append((key, details[0]))
1682
records_iter = enumerate(self._read_records_iter(key_records))
1683
for (key_idx, (key, data, sha_value)) in records_iter:
1684
pb.update('Walking content.', key_idx, total)
1685
compression_parent = build_details[key][1]
1686
if compression_parent is None:
1688
line_iterator = self._factory.get_fulltext_content(data)
1691
line_iterator = self._factory.get_linedelta_content(data)
1692
# Now that we are yielding the data for this key, remove it
1695
# XXX: It might be more efficient to yield (key,
1696
# line_iterator) in the future. However for now, this is a
1697
# simpler change to integrate into the rest of the
1698
# codebase. RBC 20071110
1699
for line in line_iterator:
1702
except errors.RetryWithNewPacks, e:
1703
self._access.reload_or_raise(e)
1704
# If there are still keys we've not yet found, we look in the fallback
1705
# vfs, and hope to find them there. Note that if the keys are found
1706
# but had no changes or no content, the fallback may not return
1708
if keys and not self._fallback_vfs:
1709
# XXX: strictly the second parameter is meant to be the file id
1710
# but it's not easily accessible here.
1711
raise RevisionNotPresent(keys, repr(self))
1712
for source in self._fallback_vfs:
1716
for line, key in source.iter_lines_added_or_present_in_keys(keys):
1717
source_keys.add(key)
1719
keys.difference_update(source_keys)
1720
pb.update('Walking content.', total, total)
1722
def _make_line_delta(self, delta_seq, new_content):
1723
"""Generate a line delta from delta_seq and new_content."""
1725
for op in delta_seq.get_opcodes():
1726
if op[0] == 'equal':
1728
diff_hunks.append((op[1], op[2], op[4]-op[3], new_content._lines[op[3]:op[4]]))
1731
def _merge_annotations(self, content, parents, parent_texts={},
1732
delta=None, annotated=None,
1733
left_matching_blocks=None):
1734
"""Merge annotations for content and generate deltas.
1736
This is done by comparing the annotations based on changes to the text
1737
and generating a delta on the resulting full texts. If annotations are
1738
not being created then a simple delta is created.
1740
if left_matching_blocks is not None:
1741
delta_seq = diff._PrematchedMatcher(left_matching_blocks)
1745
for parent_key in parents:
1746
merge_content = self._get_content(parent_key, parent_texts)
1747
if (parent_key == parents[0] and delta_seq is not None):
1750
seq = patiencediff.PatienceSequenceMatcher(
1751
None, merge_content.text(), content.text())
1752
for i, j, n in seq.get_matching_blocks():
1755
# this copies (origin, text) pairs across to the new
1756
# content for any line that matches the last-checked
1758
content._lines[j:j+n] = merge_content._lines[i:i+n]
1759
# XXX: Robert says the following block is a workaround for a
1760
# now-fixed bug and it can probably be deleted. -- mbp 20080618
1761
if content._lines and content._lines[-1][1][-1] != '\n':
1762
# The copied annotation was from a line without a trailing EOL,
1763
# reinstate one for the content object, to ensure correct
1765
line = content._lines[-1][1] + '\n'
1766
content._lines[-1] = (content._lines[-1][0], line)
1768
if delta_seq is None:
1769
reference_content = self._get_content(parents[0], parent_texts)
1770
new_texts = content.text()
1771
old_texts = reference_content.text()
1772
delta_seq = patiencediff.PatienceSequenceMatcher(
1773
None, old_texts, new_texts)
1774
return self._make_line_delta(delta_seq, content)
1776
def _parse_record(self, version_id, data):
1777
"""Parse an original format knit record.
1779
These have the last element of the key only present in the stored data.
1781
rec, record_contents = self._parse_record_unchecked(data)
1782
self._check_header_version(rec, version_id)
1783
return record_contents, rec[3]
1785
def _parse_record_header(self, key, raw_data):
1786
"""Parse a record header for consistency.
1788
:return: the header and the decompressor stream.
1789
as (stream, header_record)
1791
df = tuned_gzip.GzipFile(mode='rb', fileobj=StringIO(raw_data))
1794
rec = self._check_header(key, df.readline())
1795
except Exception, e:
1796
raise KnitCorrupt(self,
1797
"While reading {%s} got %s(%s)"
1798
% (key, e.__class__.__name__, str(e)))
1801
def _parse_record_unchecked(self, data):
1803
# 4168 calls in 2880 217 internal
1804
# 4168 calls to _parse_record_header in 2121
1805
# 4168 calls to readlines in 330
1806
df = tuned_gzip.GzipFile(mode='rb', fileobj=StringIO(data))
1808
record_contents = df.readlines()
1809
except Exception, e:
1810
raise KnitCorrupt(self, "Corrupt compressed record %r, got %s(%s)" %
1811
(data, e.__class__.__name__, str(e)))
1812
header = record_contents.pop(0)
1813
rec = self._split_header(header)
1814
last_line = record_contents.pop()
1815
if len(record_contents) != int(rec[2]):
1816
raise KnitCorrupt(self,
1817
'incorrect number of lines %s != %s'
1818
' for version {%s} %s'
1819
% (len(record_contents), int(rec[2]),
1820
rec[1], record_contents))
1821
if last_line != 'end %s\n' % rec[1]:
1822
raise KnitCorrupt(self,
1823
'unexpected version end line %r, wanted %r'
1824
% (last_line, rec[1]))
1826
return rec, record_contents
1828
def _read_records_iter(self, records):
1829
"""Read text records from data file and yield result.
1831
The result will be returned in whatever is the fastest to read.
1832
Not by the order requested. Also, multiple requests for the same
1833
record will only yield 1 response.
1834
:param records: A list of (key, access_memo) entries
1835
:return: Yields (key, contents, digest) in the order
1836
read, not the order requested
1841
# XXX: This smells wrong, IO may not be getting ordered right.
1842
needed_records = sorted(set(records), key=operator.itemgetter(1))
1843
if not needed_records:
1846
# The transport optimizes the fetching as well
1847
# (ie, reads continuous ranges.)
1848
raw_data = self._access.get_raw_records(
1849
[index_memo for key, index_memo in needed_records])
1851
for (key, index_memo), data in \
1852
izip(iter(needed_records), raw_data):
1853
content, digest = self._parse_record(key[-1], data)
1854
yield key, content, digest
1856
def _read_records_iter_raw(self, records):
1857
"""Read text records from data file and yield raw data.
1859
This unpacks enough of the text record to validate the id is
1860
as expected but thats all.
1862
Each item the iterator yields is (key, bytes,
1863
expected_sha1_of_full_text).
1865
for key, data in self._read_records_iter_unchecked(records):
1866
# validate the header (note that we can only use the suffix in
1867
# current knit records).
1868
df, rec = self._parse_record_header(key, data)
1870
yield key, data, rec[3]
1872
def _read_records_iter_unchecked(self, records):
1873
"""Read text records from data file and yield raw data.
1875
No validation is done.
1877
Yields tuples of (key, data).
1879
# setup an iterator of the external records:
1880
# uses readv so nice and fast we hope.
1882
# grab the disk data needed.
1883
needed_offsets = [index_memo for key, index_memo
1885
raw_records = self._access.get_raw_records(needed_offsets)
1887
for key, index_memo in records:
1888
data = raw_records.next()
1891
def _record_to_data(self, key, digest, lines, dense_lines=None):
1892
"""Convert key, digest, lines into a raw data block.
1894
:param key: The key of the record. Currently keys are always serialised
1895
using just the trailing component.
1896
:param dense_lines: The bytes of lines but in a denser form. For
1897
instance, if lines is a list of 1000 bytestrings each ending in \n,
1898
dense_lines may be a list with one line in it, containing all the
1899
1000's lines and their \n's. Using dense_lines if it is already
1900
known is a win because the string join to create bytes in this
1901
function spends less time resizing the final string.
1902
:return: (len, a StringIO instance with the raw data ready to read.)
1904
# Note: using a string copy here increases memory pressure with e.g.
1905
# ISO's, but it is about 3 seconds faster on a 1.2Ghz intel machine
1906
# when doing the initial commit of a mozilla tree. RBC 20070921
1907
bytes = ''.join(chain(
1908
["version %s %d %s\n" % (key[-1],
1911
dense_lines or lines,
1912
["end %s\n" % key[-1]]))
1913
if type(bytes) != str:
1914
raise AssertionError(
1915
'data must be plain bytes was %s' % type(bytes))
1916
if lines and lines[-1][-1] != '\n':
1917
raise ValueError('corrupt lines value %r' % lines)
1918
compressed_bytes = tuned_gzip.bytes_to_gzip(bytes)
1919
return len(compressed_bytes), compressed_bytes
1921
def _split_header(self, line):
1924
raise KnitCorrupt(self,
1925
'unexpected number of elements in record header')
1929
"""See VersionedFiles.keys."""
1930
if 'evil' in debug.debug_flags:
1931
trace.mutter_callsite(2, "keys scales with size of history")
1932
sources = [self._index] + self._fallback_vfs
1934
for source in sources:
1935
result.update(source.keys())
1939
class _ContentMapGenerator(object):
1940
"""Generate texts or expose raw deltas for a set of texts."""
1942
def _get_content(self, key):
1943
"""Get the content object for key."""
1944
# Note that _get_content is only called when the _ContentMapGenerator
1945
# has been constructed with just one key requested for reconstruction.
1946
if key in self.nonlocal_keys:
1947
record = self.get_record_stream().next()
1948
# Create a content object on the fly
1949
lines = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
1950
return PlainKnitContent(lines, record.key)
1952
# local keys we can ask for directly
1953
return self._get_one_work(key)
1955
def get_record_stream(self):
1956
"""Get a record stream for the keys requested during __init__."""
1957
for record in self._work():
1961
"""Produce maps of text and KnitContents as dicts.
1963
:return: (text_map, content_map) where text_map contains the texts for
1964
the requested versions and content_map contains the KnitContents.
1966
# NB: By definition we never need to read remote sources unless texts
1967
# are requested from them: we don't delta across stores - and we
1968
# explicitly do not want to to prevent data loss situations.
1969
if self.global_map is None:
1970
self.global_map = self.vf.get_parent_map(self.keys)
1971
nonlocal_keys = self.nonlocal_keys
1973
missing_keys = set(nonlocal_keys)
1974
# Read from remote versioned file instances and provide to our caller.
1975
for source in self.vf._fallback_vfs:
1976
if not missing_keys:
1978
# Loop over fallback repositories asking them for texts - ignore
1979
# any missing from a particular fallback.
1980
for record in source.get_record_stream(missing_keys,
1982
if record.storage_kind == 'absent':
1983
# Not in thie particular stream, may be in one of the
1984
# other fallback vfs objects.
1986
missing_keys.remove(record.key)
1989
self._raw_record_map = self.vf._get_record_map_unparsed(self.keys,
1992
for key in self.keys:
1993
if key in self.nonlocal_keys:
1995
yield LazyKnitContentFactory(key, self.global_map[key], self, first)
1998
def _get_one_work(self, requested_key):
1999
# Now, if we have calculated everything already, just return the
2001
if requested_key in self._contents_map:
2002
return self._contents_map[requested_key]
2003
# To simplify things, parse everything at once - code that wants one text
2004
# probably wants them all.
2005
# FUTURE: This function could be improved for the 'extract many' case
2006
# by tracking each component and only doing the copy when the number of
2007
# children than need to apply delta's to it is > 1 or it is part of the
2009
multiple_versions = len(self.keys) != 1
2010
if self._record_map is None:
2011
self._record_map = self.vf._raw_map_to_record_map(
2012
self._raw_record_map)
2013
record_map = self._record_map
2014
# raw_record_map is key:
2015
# Have read and parsed records at this point.
2016
for key in self.keys:
2017
if key in self.nonlocal_keys:
2022
while cursor is not None:
2024
record, record_details, digest, next = record_map[cursor]
2026
raise RevisionNotPresent(cursor, self)
2027
components.append((cursor, record, record_details, digest))
2029
if cursor in self._contents_map:
2030
# no need to plan further back
2031
components.append((cursor, None, None, None))
2035
for (component_id, record, record_details,
2036
digest) in reversed(components):
2037
if component_id in self._contents_map:
2038
content = self._contents_map[component_id]
2040
content, delta = self._factory.parse_record(key[-1],
2041
record, record_details, content,
2042
copy_base_content=multiple_versions)
2043
if multiple_versions:
2044
self._contents_map[component_id] = content
2046
# digest here is the digest from the last applied component.
2047
text = content.text()
2048
actual_sha = sha_strings(text)
2049
if actual_sha != digest:
2050
raise SHA1KnitCorrupt(self, actual_sha, digest, key, text)
2051
if multiple_versions:
2052
return self._contents_map[requested_key]
2056
def _wire_bytes(self):
2057
"""Get the bytes to put on the wire for 'key'.
2059
The first collection of bytes asked for returns the serialised
2060
raw_record_map and the additional details (key, parent) for key.
2061
Subsequent calls return just the additional details (key, parent).
2062
The wire storage_kind given for the first key is 'knit-delta-closure',
2063
For subsequent keys it is 'knit-delta-closure-ref'.
2065
:param key: A key from the content generator.
2066
:return: Bytes to put on the wire.
2069
# kind marker for dispatch on the far side,
2070
lines.append('knit-delta-closure')
2072
if self.vf._factory.annotated:
2073
lines.append('annotated')
2076
# then the list of keys
2077
lines.append('\t'.join(['\x00'.join(key) for key in self.keys
2078
if key not in self.nonlocal_keys]))
2079
# then the _raw_record_map in serialised form:
2081
# for each item in the map:
2083
# 1 line with parents if the key is to be yielded (None: for None, '' for ())
2084
# one line with method
2085
# one line with noeol
2086
# one line with next ('' for None)
2087
# one line with byte count of the record bytes
2089
for key, (record_bytes, (method, noeol), next) in \
2090
self._raw_record_map.iteritems():
2091
key_bytes = '\x00'.join(key)
2092
parents = self.global_map.get(key, None)
2094
parent_bytes = 'None:'
2096
parent_bytes = '\t'.join('\x00'.join(key) for key in parents)
2097
method_bytes = method
2103
next_bytes = '\x00'.join(next)
2106
map_byte_list.append('%s\n%s\n%s\n%s\n%s\n%d\n%s' % (
2107
key_bytes, parent_bytes, method_bytes, noeol_bytes, next_bytes,
2108
len(record_bytes), record_bytes))
2109
map_bytes = ''.join(map_byte_list)
2110
lines.append(map_bytes)
2111
bytes = '\n'.join(lines)
2115
class _VFContentMapGenerator(_ContentMapGenerator):
2116
"""Content map generator reading from a VersionedFiles object."""
2118
def __init__(self, versioned_files, keys, nonlocal_keys=None,
2119
global_map=None, raw_record_map=None):
2120
"""Create a _ContentMapGenerator.
2122
:param versioned_files: The versioned files that the texts are being
2124
:param keys: The keys to produce content maps for.
2125
:param nonlocal_keys: An iterable of keys(possibly intersecting keys)
2126
which are known to not be in this knit, but rather in one of the
2128
:param global_map: The result of get_parent_map(keys) (or a supermap).
2129
This is required if get_record_stream() is to be used.
2130
:param raw_record_map: A unparsed raw record map to use for answering
2133
# The vf to source data from
2134
self.vf = versioned_files
2136
self.keys = list(keys)
2137
# Keys known to be in fallback vfs objects
2138
if nonlocal_keys is None:
2139
self.nonlocal_keys = set()
2141
self.nonlocal_keys = frozenset(nonlocal_keys)
2142
# Parents data for keys to be returned in get_record_stream
2143
self.global_map = global_map
2144
# The chunked lists for self.keys in text form
2146
# A cache of KnitContent objects used in extracting texts.
2147
self._contents_map = {}
2148
# All the knit records needed to assemble the requested keys as full
2150
self._record_map = None
2151
if raw_record_map is None:
2152
self._raw_record_map = self.vf._get_record_map_unparsed(keys,
2155
self._raw_record_map = raw_record_map
2156
# the factory for parsing records
2157
self._factory = self.vf._factory
2160
class _NetworkContentMapGenerator(_ContentMapGenerator):
2161
"""Content map generator sourced from a network stream."""
2163
def __init__(self, bytes, line_end):
2164
"""Construct a _NetworkContentMapGenerator from a bytes block."""
2166
self.global_map = {}
2167
self._raw_record_map = {}
2168
self._contents_map = {}
2169
self._record_map = None
2170
self.nonlocal_keys = []
2171
# Get access to record parsing facilities
2172
self.vf = KnitVersionedFiles(None, None)
2175
line_end = bytes.find('\n', start)
2176
line = bytes[start:line_end]
2177
start = line_end + 1
2178
if line == 'annotated':
2179
self._factory = KnitAnnotateFactory()
2181
self._factory = KnitPlainFactory()
2182
# list of keys to emit in get_record_stream
2183
line_end = bytes.find('\n', start)
2184
line = bytes[start:line_end]
2185
start = line_end + 1
2187
tuple(segment.split('\x00')) for segment in line.split('\t')
2189
# now a loop until the end. XXX: It would be nice if this was just a
2190
# bunch of the same records as get_record_stream(..., False) gives, but
2191
# there is a decent sized gap stopping that at the moment.
2195
line_end = bytes.find('\n', start)
2196
key = tuple(bytes[start:line_end].split('\x00'))
2197
start = line_end + 1
2198
# 1 line with parents (None: for None, '' for ())
2199
line_end = bytes.find('\n', start)
2200
line = bytes[start:line_end]
2205
[tuple(segment.split('\x00')) for segment in line.split('\t')
2207
self.global_map[key] = parents
2208
start = line_end + 1
2209
# one line with method
2210
line_end = bytes.find('\n', start)
2211
line = bytes[start:line_end]
2213
start = line_end + 1
2214
# one line with noeol
2215
line_end = bytes.find('\n', start)
2216
line = bytes[start:line_end]
2218
start = line_end + 1
2219
# one line with next ('' for None)
2220
line_end = bytes.find('\n', start)
2221
line = bytes[start:line_end]
2225
next = tuple(bytes[start:line_end].split('\x00'))
2226
start = line_end + 1
2227
# one line with byte count of the record bytes
2228
line_end = bytes.find('\n', start)
2229
line = bytes[start:line_end]
2231
start = line_end + 1
2233
record_bytes = bytes[start:start+count]
2234
start = start + count
2236
self._raw_record_map[key] = (record_bytes, (method, noeol), next)
2238
def get_record_stream(self):
2239
"""Get a record stream for for keys requested by the bytestream."""
2241
for key in self.keys:
2242
yield LazyKnitContentFactory(key, self.global_map[key], self, first)
2245
def _wire_bytes(self):
2249
class _KndxIndex(object):
2250
"""Manages knit index files
2252
The index is kept in memory and read on startup, to enable
2253
fast lookups of revision information. The cursor of the index
2254
file is always pointing to the end, making it easy to append
2257
_cache is a cache for fast mapping from version id to a Index
2260
_history is a cache for fast mapping from indexes to version ids.
2262
The index data format is dictionary compressed when it comes to
2263
parent references; a index entry may only have parents that with a
2264
lover index number. As a result, the index is topological sorted.
2266
Duplicate entries may be written to the index for a single version id
2267
if this is done then the latter one completely replaces the former:
2268
this allows updates to correct version and parent information.
2269
Note that the two entries may share the delta, and that successive
2270
annotations and references MUST point to the first entry.
2272
The index file on disc contains a header, followed by one line per knit
2273
record. The same revision can be present in an index file more than once.
2274
The first occurrence gets assigned a sequence number starting from 0.
2276
The format of a single line is
2277
REVISION_ID FLAGS BYTE_OFFSET LENGTH( PARENT_ID|PARENT_SEQUENCE_ID)* :\n
2278
REVISION_ID is a utf8-encoded revision id
2279
FLAGS is a comma separated list of flags about the record. Values include
2280
no-eol, line-delta, fulltext.
2281
BYTE_OFFSET is the ascii representation of the byte offset in the data file
2282
that the the compressed data starts at.
2283
LENGTH is the ascii representation of the length of the data file.
2284
PARENT_ID a utf-8 revision id prefixed by a '.' that is a parent of
2286
PARENT_SEQUENCE_ID the ascii representation of the sequence number of a
2287
revision id already in the knit that is a parent of REVISION_ID.
2288
The ' :' marker is the end of record marker.
2291
when a write is interrupted to the index file, it will result in a line
2292
that does not end in ' :'. If the ' :' is not present at the end of a line,
2293
or at the end of the file, then the record that is missing it will be
2294
ignored by the parser.
2296
When writing new records to the index file, the data is preceded by '\n'
2297
to ensure that records always start on new lines even if the last write was
2298
interrupted. As a result its normal for the last line in the index to be
2299
missing a trailing newline. One can be added with no harmful effects.
2301
:ivar _kndx_cache: dict from prefix to the old state of KnitIndex objects,
2302
where prefix is e.g. the (fileid,) for .texts instances or () for
2303
constant-mapped things like .revisions, and the old state is
2304
tuple(history_vector, cache_dict). This is used to prevent having an
2305
ABI change with the C extension that reads .kndx files.
2308
HEADER = "# bzr knit index 8\n"
2310
def __init__(self, transport, mapper, get_scope, allow_writes, is_locked):
2311
"""Create a _KndxIndex on transport using mapper."""
2312
self._transport = transport
2313
self._mapper = mapper
2314
self._get_scope = get_scope
2315
self._allow_writes = allow_writes
2316
self._is_locked = is_locked
2318
self.has_graph = True
2320
def add_records(self, records, random_id=False, missing_compression_parents=False):
2321
"""Add multiple records to the index.
2323
:param records: a list of tuples:
2324
(key, options, access_memo, parents).
2325
:param random_id: If True the ids being added were randomly generated
2326
and no check for existence will be performed.
2327
:param missing_compression_parents: If True the records being added are
2328
only compressed against texts already in the index (or inside
2329
records). If False the records all refer to unavailable texts (or
2330
texts inside records) as compression parents.
2332
if missing_compression_parents:
2333
# It might be nice to get the edge of the records. But keys isn't
2335
keys = sorted(record[0] for record in records)
2336
raise errors.RevisionNotPresent(keys, self)
2338
for record in records:
2341
path = self._mapper.map(key) + '.kndx'
2342
path_keys = paths.setdefault(path, (prefix, []))
2343
path_keys[1].append(record)
2344
for path in sorted(paths):
2345
prefix, path_keys = paths[path]
2346
self._load_prefixes([prefix])
2348
orig_history = self._kndx_cache[prefix][1][:]
2349
orig_cache = self._kndx_cache[prefix][0].copy()
2352
for key, options, (_, pos, size), parents in path_keys:
2354
# kndx indices cannot be parentless.
2356
line = "\n%s %s %s %s %s :" % (
2357
key[-1], ','.join(options), pos, size,
2358
self._dictionary_compress(parents))
2359
if type(line) != str:
2360
raise AssertionError(
2361
'data must be utf8 was %s' % type(line))
2363
self._cache_key(key, options, pos, size, parents)
2364
if len(orig_history):
2365
self._transport.append_bytes(path, ''.join(lines))
2367
self._init_index(path, lines)
2369
# If any problems happen, restore the original values and re-raise
2370
self._kndx_cache[prefix] = (orig_cache, orig_history)
2373
def scan_unvalidated_index(self, graph_index):
2374
"""See _KnitGraphIndex.scan_unvalidated_index."""
2375
# Because kndx files do not support atomic insertion via separate index
2376
# files, they do not support this method.
2377
raise NotImplementedError(self.scan_unvalidated_index)
2379
def get_missing_compression_parents(self):
2380
"""See _KnitGraphIndex.get_missing_compression_parents."""
2381
# Because kndx files do not support atomic insertion via separate index
2382
# files, they do not support this method.
2383
raise NotImplementedError(self.get_missing_compression_parents)
2385
def _cache_key(self, key, options, pos, size, parent_keys):
2386
"""Cache a version record in the history array and index cache.
2388
This is inlined into _load_data for performance. KEEP IN SYNC.
2389
(It saves 60ms, 25% of the __init__ overhead on local 4000 record
2393
version_id = key[-1]
2394
# last-element only for compatibilty with the C load_data.
2395
parents = tuple(parent[-1] for parent in parent_keys)
2396
for parent in parent_keys:
2397
if parent[:-1] != prefix:
2398
raise ValueError("mismatched prefixes for %r, %r" % (
2400
cache, history = self._kndx_cache[prefix]
2401
# only want the _history index to reference the 1st index entry
2403
if version_id not in cache:
2404
index = len(history)
2405
history.append(version_id)
2407
index = cache[version_id][5]
2408
cache[version_id] = (version_id,
2415
def check_header(self, fp):
2416
line = fp.readline()
2418
# An empty file can actually be treated as though the file doesn't
2420
raise errors.NoSuchFile(self)
2421
if line != self.HEADER:
2422
raise KnitHeaderError(badline=line, filename=self)
2424
def _check_read(self):
2425
if not self._is_locked():
2426
raise errors.ObjectNotLocked(self)
2427
if self._get_scope() != self._scope:
2430
def _check_write_ok(self):
2431
"""Assert if not writes are permitted."""
2432
if not self._is_locked():
2433
raise errors.ObjectNotLocked(self)
2434
if self._get_scope() != self._scope:
2436
if self._mode != 'w':
2437
raise errors.ReadOnlyObjectDirtiedError(self)
2439
def get_build_details(self, keys):
2440
"""Get the method, index_memo and compression parent for keys.
2442
Ghosts are omitted from the result.
2444
:param keys: An iterable of keys.
2445
:return: A dict of key:(index_memo, compression_parent, parents,
2448
opaque structure to pass to read_records to extract the raw
2451
Content that this record is built upon, may be None
2453
Logical parents of this node
2455
extra information about the content which needs to be passed to
2456
Factory.parse_record
2458
parent_map = self.get_parent_map(keys)
2461
if key not in parent_map:
2463
method = self.get_method(key)
2464
parents = parent_map[key]
2465
if method == 'fulltext':
2466
compression_parent = None
2468
compression_parent = parents[0]
2469
noeol = 'no-eol' in self.get_options(key)
2470
index_memo = self.get_position(key)
2471
result[key] = (index_memo, compression_parent,
2472
parents, (method, noeol))
2475
def get_method(self, key):
2476
"""Return compression method of specified key."""
2477
options = self.get_options(key)
2478
if 'fulltext' in options:
2480
elif 'line-delta' in options:
2483
raise errors.KnitIndexUnknownMethod(self, options)
2485
def get_options(self, key):
2486
"""Return a list representing options.
2490
prefix, suffix = self._split_key(key)
2491
self._load_prefixes([prefix])
2493
return self._kndx_cache[prefix][0][suffix][1]
2495
raise RevisionNotPresent(key, self)
2497
def get_parent_map(self, keys):
2498
"""Get a map of the parents of keys.
2500
:param keys: The keys to look up parents for.
2501
:return: A mapping from keys to parents. Absent keys are absent from
2504
# Parse what we need to up front, this potentially trades off I/O
2505
# locality (.kndx and .knit in the same block group for the same file
2506
# id) for less checking in inner loops.
2507
prefixes = set(key[:-1] for key in keys)
2508
self._load_prefixes(prefixes)
2513
suffix_parents = self._kndx_cache[prefix][0][key[-1]][4]
2517
result[key] = tuple(prefix + (suffix,) for
2518
suffix in suffix_parents)
2521
def get_position(self, key):
2522
"""Return details needed to access the version.
2524
:return: a tuple (key, data position, size) to hand to the access
2525
logic to get the record.
2527
prefix, suffix = self._split_key(key)
2528
self._load_prefixes([prefix])
2529
entry = self._kndx_cache[prefix][0][suffix]
2530
return key, entry[2], entry[3]
2532
has_key = _mod_index._has_key_from_parent_map
2534
def _init_index(self, path, extra_lines=[]):
2535
"""Initialize an index."""
2537
sio.write(self.HEADER)
2538
sio.writelines(extra_lines)
2540
self._transport.put_file_non_atomic(path, sio,
2541
create_parent_dir=True)
2542
# self._create_parent_dir)
2543
# mode=self._file_mode,
2544
# dir_mode=self._dir_mode)
2547
"""Get all the keys in the collection.
2549
The keys are not ordered.
2552
# Identify all key prefixes.
2553
# XXX: A bit hacky, needs polish.
2554
if type(self._mapper) == ConstantMapper:
2558
for quoted_relpath in self._transport.iter_files_recursive():
2559
path, ext = os.path.splitext(quoted_relpath)
2561
prefixes = [self._mapper.unmap(path) for path in relpaths]
2562
self._load_prefixes(prefixes)
2563
for prefix in prefixes:
2564
for suffix in self._kndx_cache[prefix][1]:
2565
result.add(prefix + (suffix,))
2568
def _load_prefixes(self, prefixes):
2569
"""Load the indices for prefixes."""
2571
for prefix in prefixes:
2572
if prefix not in self._kndx_cache:
2573
# the load_data interface writes to these variables.
2576
self._filename = prefix
2578
path = self._mapper.map(prefix) + '.kndx'
2579
fp = self._transport.get(path)
2581
# _load_data may raise NoSuchFile if the target knit is
2583
_load_data(self, fp)
2586
self._kndx_cache[prefix] = (self._cache, self._history)
2591
self._kndx_cache[prefix] = ({}, [])
2592
if type(self._mapper) == ConstantMapper:
2593
# preserve behaviour for revisions.kndx etc.
2594
self._init_index(path)
2599
missing_keys = _mod_index._missing_keys_from_parent_map
2601
def _partition_keys(self, keys):
2602
"""Turn keys into a dict of prefix:suffix_list."""
2605
prefix_keys = result.setdefault(key[:-1], [])
2606
prefix_keys.append(key[-1])
2609
def _dictionary_compress(self, keys):
2610
"""Dictionary compress keys.
2612
:param keys: The keys to generate references to.
2613
:return: A string representation of keys. keys which are present are
2614
dictionary compressed, and others are emitted as fulltext with a
2620
prefix = keys[0][:-1]
2621
cache = self._kndx_cache[prefix][0]
2623
if key[:-1] != prefix:
2624
# kndx indices cannot refer across partitioned storage.
2625
raise ValueError("mismatched prefixes for %r" % keys)
2626
if key[-1] in cache:
2627
# -- inlined lookup() --
2628
result_list.append(str(cache[key[-1]][5]))
2629
# -- end lookup () --
2631
result_list.append('.' + key[-1])
2632
return ' '.join(result_list)
2634
def _reset_cache(self):
2635
# Possibly this should be a LRU cache. A dictionary from key_prefix to
2636
# (cache_dict, history_vector) for parsed kndx files.
2637
self._kndx_cache = {}
2638
self._scope = self._get_scope()
2639
allow_writes = self._allow_writes()
2645
def _sort_keys_by_io(self, keys, positions):
2646
"""Figure out an optimal order to read the records for the given keys.
2648
Sort keys, grouped by index and sorted by position.
2650
:param keys: A list of keys whose records we want to read. This will be
2652
:param positions: A dict, such as the one returned by
2653
_get_components_positions()
2656
def get_sort_key(key):
2657
index_memo = positions[key][1]
2658
# Group by prefix and position. index_memo[0] is the key, so it is
2659
# (file_id, revision_id) and we don't want to sort on revision_id,
2660
# index_memo[1] is the position, and index_memo[2] is the size,
2661
# which doesn't matter for the sort
2662
return index_memo[0][:-1], index_memo[1]
2663
return keys.sort(key=get_sort_key)
2665
_get_total_build_size = _get_total_build_size
2667
def _split_key(self, key):
2668
"""Split key into a prefix and suffix."""
2669
return key[:-1], key[-1]
2672
class _KnitGraphIndex(object):
2673
"""A KnitVersionedFiles index layered on GraphIndex."""
2675
def __init__(self, graph_index, is_locked, deltas=False, parents=True,
2677
"""Construct a KnitGraphIndex on a graph_index.
2679
:param graph_index: An implementation of bzrlib.index.GraphIndex.
2680
:param is_locked: A callback to check whether the object should answer
2682
:param deltas: Allow delta-compressed records.
2683
:param parents: If True, record knits parents, if not do not record
2685
:param add_callback: If not None, allow additions to the index and call
2686
this callback with a list of added GraphIndex nodes:
2687
[(node, value, node_refs), ...]
2688
:param is_locked: A callback, returns True if the index is locked and
2691
self._add_callback = add_callback
2692
self._graph_index = graph_index
2693
self._deltas = deltas
2694
self._parents = parents
2695
if deltas and not parents:
2696
# XXX: TODO: Delta tree and parent graph should be conceptually
2698
raise KnitCorrupt(self, "Cannot do delta compression without "
2700
self.has_graph = parents
2701
self._is_locked = is_locked
2702
self._missing_compression_parents = set()
2705
return "%s(%r)" % (self.__class__.__name__, self._graph_index)
2707
def add_records(self, records, random_id=False,
2708
missing_compression_parents=False):
2709
"""Add multiple records to the index.
2711
This function does not insert data into the Immutable GraphIndex
2712
backing the KnitGraphIndex, instead it prepares data for insertion by
2713
the caller and checks that it is safe to insert then calls
2714
self._add_callback with the prepared GraphIndex nodes.
2716
:param records: a list of tuples:
2717
(key, options, access_memo, parents).
2718
:param random_id: If True the ids being added were randomly generated
2719
and no check for existence will be performed.
2720
:param missing_compression_parents: If True the records being added are
2721
only compressed against texts already in the index (or inside
2722
records). If False the records all refer to unavailable texts (or
2723
texts inside records) as compression parents.
2725
if not self._add_callback:
2726
raise errors.ReadOnlyError(self)
2727
# we hope there are no repositories with inconsistent parentage
2731
compression_parents = set()
2732
for (key, options, access_memo, parents) in records:
2734
parents = tuple(parents)
2735
index, pos, size = access_memo
2736
if 'no-eol' in options:
2740
value += "%d %d" % (pos, size)
2741
if not self._deltas:
2742
if 'line-delta' in options:
2743
raise KnitCorrupt(self, "attempt to add line-delta in non-delta knit")
2746
if 'line-delta' in options:
2747
node_refs = (parents, (parents[0],))
2748
if missing_compression_parents:
2749
compression_parents.add(parents[0])
2751
node_refs = (parents, ())
2753
node_refs = (parents, )
2756
raise KnitCorrupt(self, "attempt to add node with parents "
2757
"in parentless index.")
2759
keys[key] = (value, node_refs)
2762
present_nodes = self._get_entries(keys)
2763
for (index, key, value, node_refs) in present_nodes:
2764
if (value[0] != keys[key][0][0] or
2765
node_refs[:1] != keys[key][1][:1]):
2766
raise KnitCorrupt(self, "inconsistent details in add_records"
2767
": %s %s" % ((value, node_refs), keys[key]))
2771
for key, (value, node_refs) in keys.iteritems():
2772
result.append((key, value, node_refs))
2774
for key, (value, node_refs) in keys.iteritems():
2775
result.append((key, value))
2776
self._add_callback(result)
2777
if missing_compression_parents:
2778
# This may appear to be incorrect (it does not check for
2779
# compression parents that are in the existing graph index),
2780
# but such records won't have been buffered, so this is
2781
# actually correct: every entry when
2782
# missing_compression_parents==True either has a missing parent, or
2783
# a parent that is one of the keys in records.
2784
compression_parents.difference_update(keys)
2785
self._missing_compression_parents.update(compression_parents)
2786
# Adding records may have satisfied missing compression parents.
2787
self._missing_compression_parents.difference_update(keys)
2789
def scan_unvalidated_index(self, graph_index):
2790
"""Inform this _KnitGraphIndex that there is an unvalidated index.
2792
This allows this _KnitGraphIndex to keep track of any missing
2793
compression parents we may want to have filled in to make those
2796
:param graph_index: A GraphIndex
2799
new_missing = graph_index.external_references(ref_list_num=1)
2800
new_missing.difference_update(self.get_parent_map(new_missing))
2801
self._missing_compression_parents.update(new_missing)
2803
def get_missing_compression_parents(self):
2804
"""Return the keys of missing compression parents.
2806
Missing compression parents occur when a record stream was missing
2807
basis texts, or a index was scanned that had missing basis texts.
2809
return frozenset(self._missing_compression_parents)
2811
def _check_read(self):
2812
"""raise if reads are not permitted."""
2813
if not self._is_locked():
2814
raise errors.ObjectNotLocked(self)
2816
def _check_write_ok(self):
2817
"""Assert if writes are not permitted."""
2818
if not self._is_locked():
2819
raise errors.ObjectNotLocked(self)
2821
def _compression_parent(self, an_entry):
2822
# return the key that an_entry is compressed against, or None
2823
# Grab the second parent list (as deltas implies parents currently)
2824
compression_parents = an_entry[3][1]
2825
if not compression_parents:
2827
if len(compression_parents) != 1:
2828
raise AssertionError(
2829
"Too many compression parents: %r" % compression_parents)
2830
return compression_parents[0]
2832
def get_build_details(self, keys):
2833
"""Get the method, index_memo and compression parent for version_ids.
2835
Ghosts are omitted from the result.
2837
:param keys: An iterable of keys.
2838
:return: A dict of key:
2839
(index_memo, compression_parent, parents, record_details).
2841
opaque structure to pass to read_records to extract the raw
2844
Content that this record is built upon, may be None
2846
Logical parents of this node
2848
extra information about the content which needs to be passed to
2849
Factory.parse_record
2853
entries = self._get_entries(keys, False)
2854
for entry in entries:
2856
if not self._parents:
2859
parents = entry[3][0]
2860
if not self._deltas:
2861
compression_parent_key = None
2863
compression_parent_key = self._compression_parent(entry)
2864
noeol = (entry[2][0] == 'N')
2865
if compression_parent_key:
2866
method = 'line-delta'
2869
result[key] = (self._node_to_position(entry),
2870
compression_parent_key, parents,
2874
def _get_entries(self, keys, check_present=False):
2875
"""Get the entries for keys.
2877
:param keys: An iterable of index key tuples.
2882
for node in self._graph_index.iter_entries(keys):
2884
found_keys.add(node[1])
2886
# adapt parentless index to the rest of the code.
2887
for node in self._graph_index.iter_entries(keys):
2888
yield node[0], node[1], node[2], ()
2889
found_keys.add(node[1])
2891
missing_keys = keys.difference(found_keys)
2893
raise RevisionNotPresent(missing_keys.pop(), self)
2895
def get_method(self, key):
2896
"""Return compression method of specified key."""
2897
return self._get_method(self._get_node(key))
2899
def _get_method(self, node):
2900
if not self._deltas:
2902
if self._compression_parent(node):
2907
def _get_node(self, key):
2909
return list(self._get_entries([key]))[0]
2911
raise RevisionNotPresent(key, self)
2913
def get_options(self, key):
2914
"""Return a list representing options.
2918
node = self._get_node(key)
2919
options = [self._get_method(node)]
2920
if node[2][0] == 'N':
2921
options.append('no-eol')
2924
def get_parent_map(self, keys):
2925
"""Get a map of the parents of keys.
2927
:param keys: The keys to look up parents for.
2928
:return: A mapping from keys to parents. Absent keys are absent from
2932
nodes = self._get_entries(keys)
2936
result[node[1]] = node[3][0]
2939
result[node[1]] = None
2942
def get_position(self, key):
2943
"""Return details needed to access the version.
2945
:return: a tuple (index, data position, size) to hand to the access
2946
logic to get the record.
2948
node = self._get_node(key)
2949
return self._node_to_position(node)
2951
has_key = _mod_index._has_key_from_parent_map
2954
"""Get all the keys in the collection.
2956
The keys are not ordered.
2959
return [node[1] for node in self._graph_index.iter_all_entries()]
2961
missing_keys = _mod_index._missing_keys_from_parent_map
2963
def _node_to_position(self, node):
2964
"""Convert an index value to position details."""
2965
bits = node[2][1:].split(' ')
2966
return node[0], int(bits[0]), int(bits[1])
2968
def _sort_keys_by_io(self, keys, positions):
2969
"""Figure out an optimal order to read the records for the given keys.
2971
Sort keys, grouped by index and sorted by position.
2973
:param keys: A list of keys whose records we want to read. This will be
2975
:param positions: A dict, such as the one returned by
2976
_get_components_positions()
2979
def get_index_memo(key):
2980
# index_memo is at offset [1]. It is made up of (GraphIndex,
2981
# position, size). GI is an object, which will be unique for each
2982
# pack file. This causes us to group by pack file, then sort by
2983
# position. Size doesn't matter, but it isn't worth breaking up the
2985
return positions[key][1]
2986
return keys.sort(key=get_index_memo)
2988
_get_total_build_size = _get_total_build_size
2991
class _KnitKeyAccess(object):
2992
"""Access to records in .knit files."""
2994
def __init__(self, transport, mapper):
2995
"""Create a _KnitKeyAccess with transport and mapper.
2997
:param transport: The transport the access object is rooted at.
2998
:param mapper: The mapper used to map keys to .knit files.
3000
self._transport = transport
3001
self._mapper = mapper
3003
def add_raw_records(self, key_sizes, raw_data):
3004
"""Add raw knit bytes to a storage area.
3006
The data is spooled to the container writer in one bytes-record per
3009
:param sizes: An iterable of tuples containing the key and size of each
3011
:param raw_data: A bytestring containing the data.
3012
:return: A list of memos to retrieve the record later. Each memo is an
3013
opaque index memo. For _KnitKeyAccess the memo is (key, pos,
3014
length), where the key is the record key.
3016
if type(raw_data) != str:
3017
raise AssertionError(
3018
'data must be plain bytes was %s' % type(raw_data))
3021
# TODO: This can be tuned for writing to sftp and other servers where
3022
# append() is relatively expensive by grouping the writes to each key
3024
for key, size in key_sizes:
3025
path = self._mapper.map(key)
3027
base = self._transport.append_bytes(path + '.knit',
3028
raw_data[offset:offset+size])
3029
except errors.NoSuchFile:
3030
self._transport.mkdir(osutils.dirname(path))
3031
base = self._transport.append_bytes(path + '.knit',
3032
raw_data[offset:offset+size])
3036
result.append((key, base, size))
3039
def get_raw_records(self, memos_for_retrieval):
3040
"""Get the raw bytes for a records.
3042
:param memos_for_retrieval: An iterable containing the access memo for
3043
retrieving the bytes.
3044
:return: An iterator over the bytes of the records.
3046
# first pass, group into same-index request to minimise readv's issued.
3048
current_prefix = None
3049
for (key, offset, length) in memos_for_retrieval:
3050
if current_prefix == key[:-1]:
3051
current_list.append((offset, length))
3053
if current_prefix is not None:
3054
request_lists.append((current_prefix, current_list))
3055
current_prefix = key[:-1]
3056
current_list = [(offset, length)]
3057
# handle the last entry
3058
if current_prefix is not None:
3059
request_lists.append((current_prefix, current_list))
3060
for prefix, read_vector in request_lists:
3061
path = self._mapper.map(prefix) + '.knit'
3062
for pos, data in self._transport.readv(path, read_vector):
3066
class _DirectPackAccess(object):
3067
"""Access to data in one or more packs with less translation."""
3069
def __init__(self, index_to_packs, reload_func=None):
3070
"""Create a _DirectPackAccess object.
3072
:param index_to_packs: A dict mapping index objects to the transport
3073
and file names for obtaining data.
3074
:param reload_func: A function to call if we determine that the pack
3075
files have moved and we need to reload our caches. See
3076
bzrlib.repo_fmt.pack_repo.AggregateIndex for more details.
3078
self._container_writer = None
3079
self._write_index = None
3080
self._indices = index_to_packs
3081
self._reload_func = reload_func
3083
def add_raw_records(self, key_sizes, raw_data):
3084
"""Add raw knit bytes to a storage area.
3086
The data is spooled to the container writer in one bytes-record per
3089
:param sizes: An iterable of tuples containing the key and size of each
3091
:param raw_data: A bytestring containing the data.
3092
:return: A list of memos to retrieve the record later. Each memo is an
3093
opaque index memo. For _DirectPackAccess the memo is (index, pos,
3094
length), where the index field is the write_index object supplied
3095
to the PackAccess object.
3097
if type(raw_data) != str:
3098
raise AssertionError(
3099
'data must be plain bytes was %s' % type(raw_data))
3102
for key, size in key_sizes:
3103
p_offset, p_length = self._container_writer.add_bytes_record(
3104
raw_data[offset:offset+size], [])
3106
result.append((self._write_index, p_offset, p_length))
3109
def get_raw_records(self, memos_for_retrieval):
3110
"""Get the raw bytes for a records.
3112
:param memos_for_retrieval: An iterable containing the (index, pos,
3113
length) memo for retrieving the bytes. The Pack access method
3114
looks up the pack to use for a given record in its index_to_pack
3116
:return: An iterator over the bytes of the records.
3118
# first pass, group into same-index requests
3120
current_index = None
3121
for (index, offset, length) in memos_for_retrieval:
3122
if current_index == index:
3123
current_list.append((offset, length))
3125
if current_index is not None:
3126
request_lists.append((current_index, current_list))
3127
current_index = index
3128
current_list = [(offset, length)]
3129
# handle the last entry
3130
if current_index is not None:
3131
request_lists.append((current_index, current_list))
3132
for index, offsets in request_lists:
3134
transport, path = self._indices[index]
3136
# A KeyError here indicates that someone has triggered an index
3137
# reload, and this index has gone missing, we need to start
3139
if self._reload_func is None:
3140
# If we don't have a _reload_func there is nothing that can
3143
raise errors.RetryWithNewPacks(index,
3144
reload_occurred=True,
3145
exc_info=sys.exc_info())
3147
reader = pack.make_readv_reader(transport, path, offsets)
3148
for names, read_func in reader.iter_records():
3149
yield read_func(None)
3150
except errors.NoSuchFile:
3151
# A NoSuchFile error indicates that a pack file has gone
3152
# missing on disk, we need to trigger a reload, and start over.
3153
if self._reload_func is None:
3155
raise errors.RetryWithNewPacks(transport.abspath(path),
3156
reload_occurred=False,
3157
exc_info=sys.exc_info())
3159
def set_writer(self, writer, index, transport_packname):
3160
"""Set a writer to use for adding data."""
3161
if index is not None:
3162
self._indices[index] = transport_packname
3163
self._container_writer = writer
3164
self._write_index = index
3166
def reload_or_raise(self, retry_exc):
3167
"""Try calling the reload function, or re-raise the original exception.
3169
This should be called after _DirectPackAccess raises a
3170
RetryWithNewPacks exception. This function will handle the common logic
3171
of determining when the error is fatal versus being temporary.
3172
It will also make sure that the original exception is raised, rather
3173
than the RetryWithNewPacks exception.
3175
If this function returns, then the calling function should retry
3176
whatever operation was being performed. Otherwise an exception will
3179
:param retry_exc: A RetryWithNewPacks exception.
3182
if self._reload_func is None:
3184
elif not self._reload_func():
3185
# The reload claimed that nothing changed
3186
if not retry_exc.reload_occurred:
3187
# If there wasn't an earlier reload, then we really were
3188
# expecting to find changes. We didn't find them, so this is a
3192
exc_class, exc_value, exc_traceback = retry_exc.exc_info
3193
raise exc_class, exc_value, exc_traceback
3196
# Deprecated, use PatienceSequenceMatcher instead
3197
KnitSequenceMatcher = patiencediff.PatienceSequenceMatcher
3200
def annotate_knit(knit, revision_id):
3201
"""Annotate a knit with no cached annotations.
3203
This implementation is for knits with no cached annotations.
3204
It will work for knits with cached annotations, but this is not
3207
annotator = _KnitAnnotator(knit)
3208
return iter(annotator.annotate(revision_id))
3211
class _KnitAnnotator(object):
3212
"""Build up the annotations for a text."""
3214
def __init__(self, knit):
3217
# Content objects, differs from fulltexts because of how final newlines
3218
# are treated by knits. the content objects here will always have a
3220
self._fulltext_contents = {}
3222
# Annotated lines of specific revisions
3223
self._annotated_lines = {}
3225
# Track the raw data for nodes that we could not process yet.
3226
# This maps the revision_id of the base to a list of children that will
3227
# annotated from it.
3228
self._pending_children = {}
3230
# Nodes which cannot be extracted
3231
self._ghosts = set()
3233
# Track how many children this node has, so we know if we need to keep
3235
self._annotate_children = {}
3236
self._compression_children = {}
3238
self._all_build_details = {}
3239
# The children => parent revision_id graph
3240
self._revision_id_graph = {}
3242
self._heads_provider = None
3244
self._nodes_to_keep_annotations = set()
3245
self._generations_until_keep = 100
3247
def set_generations_until_keep(self, value):
3248
"""Set the number of generations before caching a node.
3250
Setting this to -1 will cache every merge node, setting this higher
3251
will cache fewer nodes.
3253
self._generations_until_keep = value
3255
def _add_fulltext_content(self, revision_id, content_obj):
3256
self._fulltext_contents[revision_id] = content_obj
3257
# TODO: jam 20080305 It might be good to check the sha1digest here
3258
return content_obj.text()
3260
def _check_parents(self, child, nodes_to_annotate):
3261
"""Check if all parents have been processed.
3263
:param child: A tuple of (rev_id, parents, raw_content)
3264
:param nodes_to_annotate: If child is ready, add it to
3265
nodes_to_annotate, otherwise put it back in self._pending_children
3267
for parent_id in child[1]:
3268
if (parent_id not in self._annotated_lines):
3269
# This parent is present, but another parent is missing
3270
self._pending_children.setdefault(parent_id,
3274
# This one is ready to be processed
3275
nodes_to_annotate.append(child)
3277
def _add_annotation(self, revision_id, fulltext, parent_ids,
3278
left_matching_blocks=None):
3279
"""Add an annotation entry.
3281
All parents should already have been annotated.
3282
:return: A list of children that now have their parents satisfied.
3284
a = self._annotated_lines
3285
annotated_parent_lines = [a[p] for p in parent_ids]
3286
annotated_lines = list(annotate.reannotate(annotated_parent_lines,
3287
fulltext, revision_id, left_matching_blocks,
3288
heads_provider=self._get_heads_provider()))
3289
self._annotated_lines[revision_id] = annotated_lines
3290
for p in parent_ids:
3291
ann_children = self._annotate_children[p]
3292
ann_children.remove(revision_id)
3293
if (not ann_children
3294
and p not in self._nodes_to_keep_annotations):
3295
del self._annotated_lines[p]
3296
del self._all_build_details[p]
3297
if p in self._fulltext_contents:
3298
del self._fulltext_contents[p]
3299
# Now that we've added this one, see if there are any pending
3300
# deltas to be done, certainly this parent is finished
3301
nodes_to_annotate = []
3302
for child in self._pending_children.pop(revision_id, []):
3303
self._check_parents(child, nodes_to_annotate)
3304
return nodes_to_annotate
3306
def _get_build_graph(self, key):
3307
"""Get the graphs for building texts and annotations.
3309
The data you need for creating a full text may be different than the
3310
data you need to annotate that text. (At a minimum, you need both
3311
parents to create an annotation, but only need 1 parent to generate the
3314
:return: A list of (key, index_memo) records, suitable for
3315
passing to read_records_iter to start reading in the raw data fro/
3318
if key in self._annotated_lines:
3321
pending = set([key])
3326
# get all pending nodes
3328
this_iteration = pending
3329
build_details = self._knit._index.get_build_details(this_iteration)
3330
self._all_build_details.update(build_details)
3331
# new_nodes = self._knit._index._get_entries(this_iteration)
3333
for key, details in build_details.iteritems():
3334
(index_memo, compression_parent, parents,
3335
record_details) = details
3336
self._revision_id_graph[key] = parents
3337
records.append((key, index_memo))
3338
# Do we actually need to check _annotated_lines?
3339
pending.update(p for p in parents
3340
if p not in self._all_build_details)
3341
if compression_parent:
3342
self._compression_children.setdefault(compression_parent,
3345
for parent in parents:
3346
self._annotate_children.setdefault(parent,
3348
num_gens = generation - kept_generation
3349
if ((num_gens >= self._generations_until_keep)
3350
and len(parents) > 1):
3351
kept_generation = generation
3352
self._nodes_to_keep_annotations.add(key)
3354
missing_versions = this_iteration.difference(build_details.keys())
3355
self._ghosts.update(missing_versions)
3356
for missing_version in missing_versions:
3357
# add a key, no parents
3358
self._revision_id_graph[missing_version] = ()
3359
pending.discard(missing_version) # don't look for it
3360
if self._ghosts.intersection(self._compression_children):
3362
"We cannot have nodes which have a ghost compression parent:\n"
3364
"compression children: %r"
3365
% (self._ghosts, self._compression_children))
3366
# Cleanout anything that depends on a ghost so that we don't wait for
3367
# the ghost to show up
3368
for node in self._ghosts:
3369
if node in self._annotate_children:
3370
# We won't be building this node
3371
del self._annotate_children[node]
3372
# Generally we will want to read the records in reverse order, because
3373
# we find the parent nodes after the children
3377
def _annotate_records(self, records):
3378
"""Build the annotations for the listed records."""
3379
# We iterate in the order read, rather than a strict order requested
3380
# However, process what we can, and put off to the side things that
3381
# still need parents, cleaning them up when those parents are
3383
for (rev_id, record,
3384
digest) in self._knit._read_records_iter(records):
3385
if rev_id in self._annotated_lines:
3387
parent_ids = self._revision_id_graph[rev_id]
3388
parent_ids = [p for p in parent_ids if p not in self._ghosts]
3389
details = self._all_build_details[rev_id]
3390
(index_memo, compression_parent, parents,
3391
record_details) = details
3392
nodes_to_annotate = []
3393
# TODO: Remove the punning between compression parents, and
3394
# parent_ids, we should be able to do this without assuming
3396
if len(parent_ids) == 0:
3397
# There are no parents for this node, so just add it
3398
# TODO: This probably needs to be decoupled
3399
fulltext_content, delta = self._knit._factory.parse_record(
3400
rev_id, record, record_details, None)
3401
fulltext = self._add_fulltext_content(rev_id, fulltext_content)
3402
nodes_to_annotate.extend(self._add_annotation(rev_id, fulltext,
3403
parent_ids, left_matching_blocks=None))
3405
child = (rev_id, parent_ids, record)
3406
# Check if all the parents are present
3407
self._check_parents(child, nodes_to_annotate)
3408
while nodes_to_annotate:
3409
# Should we use a queue here instead of a stack?
3410
(rev_id, parent_ids, record) = nodes_to_annotate.pop()
3411
(index_memo, compression_parent, parents,
3412
record_details) = self._all_build_details[rev_id]
3414
if compression_parent is not None:
3415
comp_children = self._compression_children[compression_parent]
3416
if rev_id not in comp_children:
3417
raise AssertionError("%r not in compression children %r"
3418
% (rev_id, comp_children))
3419
# If there is only 1 child, it is safe to reuse this
3421
reuse_content = (len(comp_children) == 1
3422
and compression_parent not in
3423
self._nodes_to_keep_annotations)
3425
# Remove it from the cache since it will be changing
3426
parent_fulltext_content = self._fulltext_contents.pop(compression_parent)
3427
# Make sure to copy the fulltext since it might be
3429
parent_fulltext = list(parent_fulltext_content.text())
3431
parent_fulltext_content = self._fulltext_contents[compression_parent]
3432
parent_fulltext = parent_fulltext_content.text()
3433
comp_children.remove(rev_id)
3434
fulltext_content, delta = self._knit._factory.parse_record(
3435
rev_id, record, record_details,
3436
parent_fulltext_content,
3437
copy_base_content=(not reuse_content))
3438
fulltext = self._add_fulltext_content(rev_id,
3440
if compression_parent == parent_ids[0]:
3441
# the compression_parent is the left parent, so we can
3443
blocks = KnitContent.get_line_delta_blocks(delta,
3444
parent_fulltext, fulltext)
3446
fulltext_content = self._knit._factory.parse_fulltext(
3448
fulltext = self._add_fulltext_content(rev_id,
3450
nodes_to_annotate.extend(
3451
self._add_annotation(rev_id, fulltext, parent_ids,
3452
left_matching_blocks=blocks))
3454
def _get_heads_provider(self):
3455
"""Create a heads provider for resolving ancestry issues."""
3456
if self._heads_provider is not None:
3457
return self._heads_provider
3458
parent_provider = _mod_graph.DictParentsProvider(
3459
self._revision_id_graph)
3460
graph_obj = _mod_graph.Graph(parent_provider)
3461
head_cache = _mod_graph.FrozenHeadsCache(graph_obj)
3462
self._heads_provider = head_cache
3465
def annotate(self, key):
3466
"""Return the annotated fulltext at the given key.
3468
:param key: The key to annotate.
3470
if len(self._knit._fallback_vfs) > 0:
3471
# stacked knits can't use the fast path at present.
3472
return self._simple_annotate(key)
3475
records = self._get_build_graph(key)
3476
if key in self._ghosts:
3477
raise errors.RevisionNotPresent(key, self._knit)
3478
self._annotate_records(records)
3479
return self._annotated_lines[key]
3480
except errors.RetryWithNewPacks, e:
3481
self._knit._access.reload_or_raise(e)
3482
# The cached build_details are no longer valid
3483
self._all_build_details.clear()
3485
def _simple_annotate(self, key):
3486
"""Return annotated fulltext, rediffing from the full texts.
3488
This is slow but makes no assumptions about the repository
3489
being able to produce line deltas.
3491
# TODO: this code generates a parent maps of present ancestors; it
3492
# could be split out into a separate method, and probably should use
3493
# iter_ancestry instead. -- mbp and robertc 20080704
3494
graph = _mod_graph.Graph(self._knit)
3495
head_cache = _mod_graph.FrozenHeadsCache(graph)
3496
search = graph._make_breadth_first_searcher([key])
3500
present, ghosts = search.next_with_ghosts()
3501
except StopIteration:
3503
keys.update(present)
3504
parent_map = self._knit.get_parent_map(keys)
3506
reannotate = annotate.reannotate
3507
for record in self._knit.get_record_stream(keys, 'topological', True):
3509
fulltext = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
3510
parents = parent_map[key]
3511
if parents is not None:
3512
parent_lines = [parent_cache[parent] for parent in parent_map[key]]
3515
parent_cache[key] = list(
3516
reannotate(parent_lines, fulltext, key, None, head_cache))
3518
return parent_cache[key]
3520
raise errors.RevisionNotPresent(key, self._knit)
3524
from bzrlib._knit_load_data_c import _load_data_c as _load_data
3526
from bzrlib._knit_load_data_py import _load_data_py as _load_data