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,
111
# TODO: Split out code specific to this format into an associated object.
113
# TODO: Can we put in some kind of value to check that the index and data
114
# files belong together?
116
# TODO: accommodate binaries, perhaps by storing a byte count
118
# TODO: function to check whole file
120
# TODO: atomically append data, then measure backwards from the cursor
121
# position after writing to work out where it was located. we may need to
122
# bypass python file buffering.
124
DATA_SUFFIX = '.knit'
125
INDEX_SUFFIX = '.kndx'
126
_STREAM_MIN_BUFFER_SIZE = 5*1024*1024
129
class KnitAdapter(object):
130
"""Base class for knit record adaption."""
132
def __init__(self, basis_vf):
133
"""Create an adapter which accesses full texts from basis_vf.
135
:param basis_vf: A versioned file to access basis texts of deltas from.
136
May be None for adapters that do not need to access basis texts.
138
self._data = KnitVersionedFiles(None, None)
139
self._annotate_factory = KnitAnnotateFactory()
140
self._plain_factory = KnitPlainFactory()
141
self._basis_vf = basis_vf
144
class FTAnnotatedToUnannotated(KnitAdapter):
145
"""An adapter from FT annotated knits to unannotated ones."""
147
def get_bytes(self, factory):
148
annotated_compressed_bytes = factory._raw_record
150
self._data._parse_record_unchecked(annotated_compressed_bytes)
151
content = self._annotate_factory.parse_fulltext(contents, rec[1])
152
size, bytes = self._data._record_to_data((rec[1],), rec[3], content.text())
156
class DeltaAnnotatedToUnannotated(KnitAdapter):
157
"""An adapter for deltas from annotated to unannotated."""
159
def get_bytes(self, factory):
160
annotated_compressed_bytes = factory._raw_record
162
self._data._parse_record_unchecked(annotated_compressed_bytes)
163
delta = self._annotate_factory.parse_line_delta(contents, rec[1],
165
contents = self._plain_factory.lower_line_delta(delta)
166
size, bytes = self._data._record_to_data((rec[1],), rec[3], contents)
170
class FTAnnotatedToFullText(KnitAdapter):
171
"""An adapter from FT annotated knits to unannotated ones."""
173
def get_bytes(self, factory):
174
annotated_compressed_bytes = factory._raw_record
176
self._data._parse_record_unchecked(annotated_compressed_bytes)
177
content, delta = self._annotate_factory.parse_record(factory.key[-1],
178
contents, factory._build_details, None)
179
return ''.join(content.text())
182
class DeltaAnnotatedToFullText(KnitAdapter):
183
"""An adapter for deltas from annotated to unannotated."""
185
def get_bytes(self, factory):
186
annotated_compressed_bytes = factory._raw_record
188
self._data._parse_record_unchecked(annotated_compressed_bytes)
189
delta = self._annotate_factory.parse_line_delta(contents, rec[1],
191
compression_parent = factory.parents[0]
192
basis_entry = self._basis_vf.get_record_stream(
193
[compression_parent], 'unordered', True).next()
194
if basis_entry.storage_kind == 'absent':
195
raise errors.RevisionNotPresent(compression_parent, self._basis_vf)
196
basis_chunks = basis_entry.get_bytes_as('chunked')
197
basis_lines = osutils.chunks_to_lines(basis_chunks)
198
# Manually apply the delta because we have one annotated content and
200
basis_content = PlainKnitContent(basis_lines, compression_parent)
201
basis_content.apply_delta(delta, rec[1])
202
basis_content._should_strip_eol = factory._build_details[1]
203
return ''.join(basis_content.text())
206
class FTPlainToFullText(KnitAdapter):
207
"""An adapter from FT plain knits to unannotated ones."""
209
def get_bytes(self, factory):
210
compressed_bytes = factory._raw_record
212
self._data._parse_record_unchecked(compressed_bytes)
213
content, delta = self._plain_factory.parse_record(factory.key[-1],
214
contents, factory._build_details, None)
215
return ''.join(content.text())
218
class DeltaPlainToFullText(KnitAdapter):
219
"""An adapter for deltas from annotated to unannotated."""
221
def get_bytes(self, factory):
222
compressed_bytes = factory._raw_record
224
self._data._parse_record_unchecked(compressed_bytes)
225
delta = self._plain_factory.parse_line_delta(contents, rec[1])
226
compression_parent = factory.parents[0]
227
# XXX: string splitting overhead.
228
basis_entry = self._basis_vf.get_record_stream(
229
[compression_parent], 'unordered', True).next()
230
if basis_entry.storage_kind == 'absent':
231
raise errors.RevisionNotPresent(compression_parent, self._basis_vf)
232
basis_chunks = basis_entry.get_bytes_as('chunked')
233
basis_lines = osutils.chunks_to_lines(basis_chunks)
234
basis_content = PlainKnitContent(basis_lines, compression_parent)
235
# Manually apply the delta because we have one annotated content and
237
content, _ = self._plain_factory.parse_record(rec[1], contents,
238
factory._build_details, basis_content)
239
return ''.join(content.text())
242
class KnitContentFactory(ContentFactory):
243
"""Content factory for streaming from knits.
245
:seealso ContentFactory:
248
def __init__(self, key, parents, build_details, sha1, raw_record,
249
annotated, knit=None, network_bytes=None):
250
"""Create a KnitContentFactory for key.
253
:param parents: The parents.
254
:param build_details: The build details as returned from
256
:param sha1: The sha1 expected from the full text of this object.
257
:param raw_record: The bytes of the knit data from disk.
258
:param annotated: True if the raw data is annotated.
259
:param network_bytes: None to calculate the network bytes on demand,
260
not-none if they are already known.
262
ContentFactory.__init__(self)
265
self.parents = parents
266
if build_details[0] == 'line-delta':
271
annotated_kind = 'annotated-'
274
self.storage_kind = 'knit-%s%s-gz' % (annotated_kind, kind)
275
self._raw_record = raw_record
276
self._network_bytes = network_bytes
277
self._build_details = build_details
280
def _create_network_bytes(self):
281
"""Create a fully serialised network version for transmission."""
282
# storage_kind, key, parents, Noeol, raw_record
283
key_bytes = '\x00'.join(self.key)
284
if self.parents is None:
285
parent_bytes = 'None:'
287
parent_bytes = '\t'.join('\x00'.join(key) for key in self.parents)
288
if self._build_details[1]:
292
network_bytes = "%s\n%s\n%s\n%s%s" % (self.storage_kind, key_bytes,
293
parent_bytes, noeol, self._raw_record)
294
self._network_bytes = network_bytes
296
def get_bytes_as(self, storage_kind):
297
if storage_kind == self.storage_kind:
298
if self._network_bytes is None:
299
self._create_network_bytes()
300
return self._network_bytes
301
if self._knit is not None:
302
if storage_kind == 'chunked':
303
return self._knit.get_lines(self.key[0])
304
elif storage_kind == 'fulltext':
305
return self._knit.get_text(self.key[0])
306
raise errors.UnavailableRepresentation(self.key, storage_kind,
310
class LazyKnitContentFactory(ContentFactory):
311
"""A ContentFactory which can either generate full text or a wire form.
313
:seealso ContentFactory:
316
def __init__(self, key, parents, generator, first):
317
"""Create a LazyKnitContentFactory.
319
:param key: The key of the record.
320
:param parents: The parents of the record.
321
:param generator: A _ContentMapGenerator containing the record for this
323
:param first: Is this the first content object returned from generator?
324
if it is, its storage kind is knit-delta-closure, otherwise it is
325
knit-delta-closure-ref
328
self.parents = parents
330
self._generator = generator
331
self.storage_kind = "knit-delta-closure"
333
self.storage_kind = self.storage_kind + "-ref"
336
def get_bytes_as(self, storage_kind):
337
if storage_kind == self.storage_kind:
339
return self._generator._wire_bytes()
341
# all the keys etc are contained in the bytes returned in the
344
if storage_kind in ('chunked', 'fulltext'):
345
chunks = self._generator._get_one_work(self.key).text()
346
if storage_kind == 'chunked':
349
return ''.join(chunks)
350
raise errors.UnavailableRepresentation(self.key, storage_kind,
354
def knit_delta_closure_to_records(storage_kind, bytes, line_end):
355
"""Convert a network record to a iterator over stream records.
357
:param storage_kind: The storage kind of the record.
358
Must be 'knit-delta-closure'.
359
:param bytes: The bytes of the record on the network.
361
generator = _NetworkContentMapGenerator(bytes, line_end)
362
return generator.get_record_stream()
365
def knit_network_to_record(storage_kind, bytes, line_end):
366
"""Convert a network record to a record object.
368
:param storage_kind: The storage kind of the record.
369
:param bytes: The bytes of the record on the network.
372
line_end = bytes.find('\n', start)
373
key = tuple(bytes[start:line_end].split('\x00'))
375
line_end = bytes.find('\n', start)
376
parent_line = bytes[start:line_end]
377
if parent_line == 'None:':
381
[tuple(segment.split('\x00')) for segment in parent_line.split('\t')
384
noeol = bytes[start] == 'N'
385
if 'ft' in storage_kind:
388
method = 'line-delta'
389
build_details = (method, noeol)
391
raw_record = bytes[start:]
392
annotated = 'annotated' in storage_kind
393
return [KnitContentFactory(key, parents, build_details, None, raw_record,
394
annotated, network_bytes=bytes)]
397
class KnitContent(object):
398
"""Content of a knit version to which deltas can be applied.
400
This is always stored in memory as a list of lines with \n at the end,
401
plus a flag saying if the final ending is really there or not, because that
402
corresponds to the on-disk knit representation.
406
self._should_strip_eol = False
408
def apply_delta(self, delta, new_version_id):
409
"""Apply delta to this object to become new_version_id."""
410
raise NotImplementedError(self.apply_delta)
412
def line_delta_iter(self, new_lines):
413
"""Generate line-based delta from this content to new_lines."""
414
new_texts = new_lines.text()
415
old_texts = self.text()
416
s = patiencediff.PatienceSequenceMatcher(None, old_texts, new_texts)
417
for tag, i1, i2, j1, j2 in s.get_opcodes():
420
# ofrom, oto, length, data
421
yield i1, i2, j2 - j1, new_lines._lines[j1:j2]
423
def line_delta(self, new_lines):
424
return list(self.line_delta_iter(new_lines))
427
def get_line_delta_blocks(knit_delta, source, target):
428
"""Extract SequenceMatcher.get_matching_blocks() from a knit delta"""
429
target_len = len(target)
432
for s_begin, s_end, t_len, new_text in knit_delta:
433
true_n = s_begin - s_pos
436
# knit deltas do not provide reliable info about whether the
437
# last line of a file matches, due to eol handling.
438
if source[s_pos + n -1] != target[t_pos + n -1]:
441
yield s_pos, t_pos, n
442
t_pos += t_len + true_n
444
n = target_len - t_pos
446
if source[s_pos + n -1] != target[t_pos + n -1]:
449
yield s_pos, t_pos, n
450
yield s_pos + (target_len - t_pos), target_len, 0
453
class AnnotatedKnitContent(KnitContent):
454
"""Annotated content."""
456
def __init__(self, lines):
457
KnitContent.__init__(self)
461
"""Return a list of (origin, text) for each content line."""
462
lines = self._lines[:]
463
if self._should_strip_eol:
464
origin, last_line = lines[-1]
465
lines[-1] = (origin, last_line.rstrip('\n'))
468
def apply_delta(self, delta, new_version_id):
469
"""Apply delta to this object to become new_version_id."""
472
for start, end, count, delta_lines in delta:
473
lines[offset+start:offset+end] = delta_lines
474
offset = offset + (start - end) + count
478
lines = [text for origin, text in self._lines]
479
except ValueError, e:
480
# most commonly (only?) caused by the internal form of the knit
481
# missing annotation information because of a bug - see thread
483
raise KnitCorrupt(self,
484
"line in annotated knit missing annotation information: %s"
486
if self._should_strip_eol:
487
lines[-1] = lines[-1].rstrip('\n')
491
return AnnotatedKnitContent(self._lines[:])
494
class PlainKnitContent(KnitContent):
495
"""Unannotated content.
497
When annotate[_iter] is called on this content, the same version is reported
498
for all lines. Generally, annotate[_iter] is not useful on PlainKnitContent
502
def __init__(self, lines, version_id):
503
KnitContent.__init__(self)
505
self._version_id = version_id
508
"""Return a list of (origin, text) for each content line."""
509
return [(self._version_id, line) for line in self._lines]
511
def apply_delta(self, delta, new_version_id):
512
"""Apply delta to this object to become new_version_id."""
515
for start, end, count, delta_lines in delta:
516
lines[offset+start:offset+end] = delta_lines
517
offset = offset + (start - end) + count
518
self._version_id = new_version_id
521
return PlainKnitContent(self._lines[:], self._version_id)
525
if self._should_strip_eol:
527
lines[-1] = lines[-1].rstrip('\n')
531
class _KnitFactory(object):
532
"""Base class for common Factory functions."""
534
def parse_record(self, version_id, record, record_details,
535
base_content, copy_base_content=True):
536
"""Parse a record into a full content object.
538
:param version_id: The official version id for this content
539
:param record: The data returned by read_records_iter()
540
:param record_details: Details about the record returned by
542
:param base_content: If get_build_details returns a compression_parent,
543
you must return a base_content here, else use None
544
:param copy_base_content: When building from the base_content, decide
545
you can either copy it and return a new object, or modify it in
547
:return: (content, delta) A Content object and possibly a line-delta,
550
method, noeol = record_details
551
if method == 'line-delta':
552
if copy_base_content:
553
content = base_content.copy()
555
content = base_content
556
delta = self.parse_line_delta(record, version_id)
557
content.apply_delta(delta, version_id)
559
content = self.parse_fulltext(record, version_id)
561
content._should_strip_eol = noeol
562
return (content, delta)
565
class KnitAnnotateFactory(_KnitFactory):
566
"""Factory for creating annotated Content objects."""
570
def make(self, lines, version_id):
571
num_lines = len(lines)
572
return AnnotatedKnitContent(zip([version_id] * num_lines, lines))
574
def parse_fulltext(self, content, version_id):
575
"""Convert fulltext to internal representation
577
fulltext content is of the format
578
revid(utf8) plaintext\n
579
internal representation is of the format:
582
# TODO: jam 20070209 The tests expect this to be returned as tuples,
583
# but the code itself doesn't really depend on that.
584
# Figure out a way to not require the overhead of turning the
585
# list back into tuples.
586
lines = [tuple(line.split(' ', 1)) for line in content]
587
return AnnotatedKnitContent(lines)
589
def parse_line_delta_iter(self, lines):
590
return iter(self.parse_line_delta(lines))
592
def parse_line_delta(self, lines, version_id, plain=False):
593
"""Convert a line based delta into internal representation.
595
line delta is in the form of:
596
intstart intend intcount
598
revid(utf8) newline\n
599
internal representation is
600
(start, end, count, [1..count tuples (revid, newline)])
602
:param plain: If True, the lines are returned as a plain
603
list without annotations, not as a list of (origin, content) tuples, i.e.
604
(start, end, count, [1..count newline])
611
def cache_and_return(line):
612
origin, text = line.split(' ', 1)
613
return cache.setdefault(origin, origin), text
615
# walk through the lines parsing.
616
# Note that the plain test is explicitly pulled out of the
617
# loop to minimise any performance impact
620
start, end, count = [int(n) for n in header.split(',')]
621
contents = [next().split(' ', 1)[1] for i in xrange(count)]
622
result.append((start, end, count, contents))
625
start, end, count = [int(n) for n in header.split(',')]
626
contents = [tuple(next().split(' ', 1)) for i in xrange(count)]
627
result.append((start, end, count, contents))
630
def get_fulltext_content(self, lines):
631
"""Extract just the content lines from a fulltext."""
632
return (line.split(' ', 1)[1] for line in lines)
634
def get_linedelta_content(self, lines):
635
"""Extract just the content from a line delta.
637
This doesn't return all of the extra information stored in a delta.
638
Only the actual content lines.
643
header = header.split(',')
644
count = int(header[2])
645
for i in xrange(count):
646
origin, text = next().split(' ', 1)
649
def lower_fulltext(self, content):
650
"""convert a fulltext content record into a serializable form.
652
see parse_fulltext which this inverts.
654
# TODO: jam 20070209 We only do the caching thing to make sure that
655
# the origin is a valid utf-8 line, eventually we could remove it
656
return ['%s %s' % (o, t) for o, t in content._lines]
658
def lower_line_delta(self, delta):
659
"""convert a delta into a serializable form.
661
See parse_line_delta which this inverts.
663
# TODO: jam 20070209 We only do the caching thing to make sure that
664
# the origin is a valid utf-8 line, eventually we could remove it
666
for start, end, c, lines in delta:
667
out.append('%d,%d,%d\n' % (start, end, c))
668
out.extend(origin + ' ' + text
669
for origin, text in lines)
672
def annotate(self, knit, key):
673
content = knit._get_content(key)
674
# adjust for the fact that serialised annotations are only key suffixes
676
if type(key) == tuple:
678
origins = content.annotate()
680
for origin, line in origins:
681
result.append((prefix + (origin,), line))
684
# XXX: This smells a bit. Why would key ever be a non-tuple here?
685
# Aren't keys defined to be tuples? -- spiv 20080618
686
return content.annotate()
689
class KnitPlainFactory(_KnitFactory):
690
"""Factory for creating plain Content objects."""
694
def make(self, lines, version_id):
695
return PlainKnitContent(lines, version_id)
697
def parse_fulltext(self, content, version_id):
698
"""This parses an unannotated fulltext.
700
Note that this is not a noop - the internal representation
701
has (versionid, line) - its just a constant versionid.
703
return self.make(content, version_id)
705
def parse_line_delta_iter(self, lines, version_id):
707
num_lines = len(lines)
708
while cur < num_lines:
711
start, end, c = [int(n) for n in header.split(',')]
712
yield start, end, c, lines[cur:cur+c]
715
def parse_line_delta(self, lines, version_id):
716
return list(self.parse_line_delta_iter(lines, version_id))
718
def get_fulltext_content(self, lines):
719
"""Extract just the content lines from a fulltext."""
722
def get_linedelta_content(self, lines):
723
"""Extract just the content from a line delta.
725
This doesn't return all of the extra information stored in a delta.
726
Only the actual content lines.
731
header = header.split(',')
732
count = int(header[2])
733
for i in xrange(count):
736
def lower_fulltext(self, content):
737
return content.text()
739
def lower_line_delta(self, delta):
741
for start, end, c, lines in delta:
742
out.append('%d,%d,%d\n' % (start, end, c))
746
def annotate(self, knit, key):
747
annotator = _KnitAnnotator(knit)
748
return annotator.annotate(key)
752
def make_file_factory(annotated, mapper):
753
"""Create a factory for creating a file based KnitVersionedFiles.
755
This is only functional enough to run interface tests, it doesn't try to
756
provide a full pack environment.
758
:param annotated: knit annotations are wanted.
759
:param mapper: The mapper from keys to paths.
761
def factory(transport):
762
index = _KndxIndex(transport, mapper, lambda:None, lambda:True, lambda:True)
763
access = _KnitKeyAccess(transport, mapper)
764
return KnitVersionedFiles(index, access, annotated=annotated)
768
def make_pack_factory(graph, delta, keylength):
769
"""Create a factory for creating a pack based VersionedFiles.
771
This is only functional enough to run interface tests, it doesn't try to
772
provide a full pack environment.
774
:param graph: Store a graph.
775
:param delta: Delta compress contents.
776
:param keylength: How long should keys be.
778
def factory(transport):
779
parents = graph or delta
785
max_delta_chain = 200
788
graph_index = _mod_index.InMemoryGraphIndex(reference_lists=ref_length,
789
key_elements=keylength)
790
stream = transport.open_write_stream('newpack')
791
writer = pack.ContainerWriter(stream.write)
793
index = _KnitGraphIndex(graph_index, lambda:True, parents=parents,
794
deltas=delta, add_callback=graph_index.add_nodes)
795
access = _DirectPackAccess({})
796
access.set_writer(writer, graph_index, (transport, 'newpack'))
797
result = KnitVersionedFiles(index, access,
798
max_delta_chain=max_delta_chain)
799
result.stream = stream
800
result.writer = writer
805
def cleanup_pack_knit(versioned_files):
806
versioned_files.stream.close()
807
versioned_files.writer.end()
810
def _get_total_build_size(self, keys, positions):
811
"""Determine the total bytes to build these keys.
813
(helper function because _KnitGraphIndex and _KndxIndex work the same, but
814
don't inherit from a common base.)
816
:param keys: Keys that we want to build
817
:param positions: dict of {key, (info, index_memo, comp_parent)} (such
818
as returned by _get_components_positions)
819
:return: Number of bytes to build those keys
821
all_build_index_memos = {}
825
for key in build_keys:
826
# This is mostly for the 'stacked' case
827
# Where we will be getting the data from a fallback
828
if key not in positions:
830
_, index_memo, compression_parent = positions[key]
831
all_build_index_memos[key] = index_memo
832
if compression_parent not in all_build_index_memos:
833
next_keys.add(compression_parent)
834
build_keys = next_keys
835
return sum([index_memo[2] for index_memo
836
in all_build_index_memos.itervalues()])
839
class KnitVersionedFiles(VersionedFiles):
840
"""Storage for many versioned files using knit compression.
842
Backend storage is managed by indices and data objects.
844
:ivar _index: A _KnitGraphIndex or similar that can describe the
845
parents, graph, compression and data location of entries in this
846
KnitVersionedFiles. Note that this is only the index for
847
*this* vfs; if there are fallbacks they must be queried separately.
850
def __init__(self, index, data_access, max_delta_chain=200,
851
annotated=False, reload_func=None):
852
"""Create a KnitVersionedFiles with index and data_access.
854
:param index: The index for the knit data.
855
:param data_access: The access object to store and retrieve knit
857
:param max_delta_chain: The maximum number of deltas to permit during
858
insertion. Set to 0 to prohibit the use of deltas.
859
:param annotated: Set to True to cause annotations to be calculated and
860
stored during insertion.
861
:param reload_func: An function that can be called if we think we need
862
to reload the pack listing and try again. See
863
'bzrlib.repofmt.pack_repo.AggregateIndex' for the signature.
866
self._access = data_access
867
self._max_delta_chain = max_delta_chain
869
self._factory = KnitAnnotateFactory()
871
self._factory = KnitPlainFactory()
872
self._fallback_vfs = []
873
self._reload_func = reload_func
876
return "%s(%r, %r)" % (
877
self.__class__.__name__,
881
def add_fallback_versioned_files(self, a_versioned_files):
882
"""Add a source of texts for texts not present in this knit.
884
:param a_versioned_files: A VersionedFiles object.
886
self._fallback_vfs.append(a_versioned_files)
888
def add_lines(self, key, parents, lines, parent_texts=None,
889
left_matching_blocks=None, nostore_sha=None, random_id=False,
891
"""See VersionedFiles.add_lines()."""
892
self._index._check_write_ok()
893
self._check_add(key, lines, random_id, check_content)
895
# The caller might pass None if there is no graph data, but kndx
896
# indexes can't directly store that, so we give them
897
# an empty tuple instead.
899
return self._add(key, lines, parents,
900
parent_texts, left_matching_blocks, nostore_sha, random_id)
902
def _add(self, key, lines, parents, parent_texts,
903
left_matching_blocks, nostore_sha, random_id):
904
"""Add a set of lines on top of version specified by parents.
906
Any versions not present will be converted into ghosts.
908
# first thing, if the content is something we don't need to store, find
910
line_bytes = ''.join(lines)
911
digest = sha_string(line_bytes)
912
if nostore_sha == digest:
913
raise errors.ExistingContent
916
if parent_texts is None:
918
# Do a single query to ascertain parent presence; we only compress
919
# against parents in the same kvf.
920
present_parent_map = self._index.get_parent_map(parents)
921
for parent in parents:
922
if parent in present_parent_map:
923
present_parents.append(parent)
925
# Currently we can only compress against the left most present parent.
926
if (len(present_parents) == 0 or
927
present_parents[0] != parents[0]):
930
# To speed the extract of texts the delta chain is limited
931
# to a fixed number of deltas. This should minimize both
932
# I/O and the time spend applying deltas.
933
delta = self._check_should_delta(present_parents[0])
935
text_length = len(line_bytes)
938
if lines[-1][-1] != '\n':
939
# copy the contents of lines.
941
options.append('no-eol')
942
lines[-1] = lines[-1] + '\n'
946
if type(element) != str:
947
raise TypeError("key contains non-strings: %r" % (key,))
948
# Knit hunks are still last-element only
950
content = self._factory.make(lines, version_id)
951
if 'no-eol' in options:
952
# Hint to the content object that its text() call should strip the
954
content._should_strip_eol = True
955
if delta or (self._factory.annotated and len(present_parents) > 0):
956
# Merge annotations from parent texts if needed.
957
delta_hunks = self._merge_annotations(content, present_parents,
958
parent_texts, delta, self._factory.annotated,
959
left_matching_blocks)
962
options.append('line-delta')
963
store_lines = self._factory.lower_line_delta(delta_hunks)
964
size, bytes = self._record_to_data(key, digest,
967
options.append('fulltext')
968
# isinstance is slower and we have no hierarchy.
969
if self._factory.__class__ is KnitPlainFactory:
970
# Use the already joined bytes saving iteration time in
972
size, bytes = self._record_to_data(key, digest,
975
# get mixed annotation + content and feed it into the
977
store_lines = self._factory.lower_fulltext(content)
978
size, bytes = self._record_to_data(key, digest,
981
access_memo = self._access.add_raw_records([(key, size)], bytes)[0]
982
self._index.add_records(
983
((key, options, access_memo, parents),),
985
return digest, text_length, content
987
def annotate(self, key):
988
"""See VersionedFiles.annotate."""
989
return self._factory.annotate(self, key)
991
def check(self, progress_bar=None):
992
"""See VersionedFiles.check()."""
993
# This doesn't actually test extraction of everything, but that will
994
# impact 'bzr check' substantially, and needs to be integrated with
995
# care. However, it does check for the obvious problem of a delta with
997
keys = self._index.keys()
998
parent_map = self.get_parent_map(keys)
1000
if self._index.get_method(key) != 'fulltext':
1001
compression_parent = parent_map[key][0]
1002
if compression_parent not in parent_map:
1003
raise errors.KnitCorrupt(self,
1004
"Missing basis parent %s for %s" % (
1005
compression_parent, key))
1006
for fallback_vfs in self._fallback_vfs:
1007
fallback_vfs.check()
1009
def _check_add(self, key, lines, random_id, check_content):
1010
"""check that version_id and lines are safe to add."""
1011
version_id = key[-1]
1012
if contains_whitespace(version_id):
1013
raise InvalidRevisionId(version_id, self)
1014
self.check_not_reserved_id(version_id)
1015
# TODO: If random_id==False and the key is already present, we should
1016
# probably check that the existing content is identical to what is
1017
# being inserted, and otherwise raise an exception. This would make
1018
# the bundle code simpler.
1020
self._check_lines_not_unicode(lines)
1021
self._check_lines_are_lines(lines)
1023
def _check_header(self, key, line):
1024
rec = self._split_header(line)
1025
self._check_header_version(rec, key[-1])
1028
def _check_header_version(self, rec, version_id):
1029
"""Checks the header version on original format knit records.
1031
These have the last component of the key embedded in the record.
1033
if rec[1] != version_id:
1034
raise KnitCorrupt(self,
1035
'unexpected version, wanted %r, got %r' % (version_id, rec[1]))
1037
def _check_should_delta(self, parent):
1038
"""Iterate back through the parent listing, looking for a fulltext.
1040
This is used when we want to decide whether to add a delta or a new
1041
fulltext. It searches for _max_delta_chain parents. When it finds a
1042
fulltext parent, it sees if the total size of the deltas leading up to
1043
it is large enough to indicate that we want a new full text anyway.
1045
Return True if we should create a new delta, False if we should use a
1049
fulltext_size = None
1050
for count in xrange(self._max_delta_chain):
1052
# Note that this only looks in the index of this particular
1053
# KnitVersionedFiles, not in the fallbacks. This ensures that
1054
# we won't store a delta spanning physical repository
1056
build_details = self._index.get_build_details([parent])
1057
parent_details = build_details[parent]
1058
except (RevisionNotPresent, KeyError), e:
1059
# Some basis is not locally present: always fulltext
1061
index_memo, compression_parent, _, _ = parent_details
1062
_, _, size = index_memo
1063
if compression_parent is None:
1064
fulltext_size = size
1067
# We don't explicitly check for presence because this is in an
1068
# inner loop, and if it's missing it'll fail anyhow.
1069
parent = compression_parent
1071
# We couldn't find a fulltext, so we must create a new one
1073
# Simple heuristic - if the total I/O wold be greater as a delta than
1074
# the originally installed fulltext, we create a new fulltext.
1075
return fulltext_size > delta_size
1077
def _build_details_to_components(self, build_details):
1078
"""Convert a build_details tuple to a position tuple."""
1079
# record_details, access_memo, compression_parent
1080
return build_details[3], build_details[0], build_details[1]
1082
def _get_components_positions(self, keys, allow_missing=False):
1083
"""Produce a map of position data for the components of keys.
1085
This data is intended to be used for retrieving the knit records.
1087
A dict of key to (record_details, index_memo, next, parents) is
1089
method is the way referenced data should be applied.
1090
index_memo is the handle to pass to the data access to actually get the
1092
next is the build-parent of the version, or None for fulltexts.
1093
parents is the version_ids of the parents of this version
1095
:param allow_missing: If True do not raise an error on a missing component,
1099
pending_components = keys
1100
while pending_components:
1101
build_details = self._index.get_build_details(pending_components)
1102
current_components = set(pending_components)
1103
pending_components = set()
1104
for key, details in build_details.iteritems():
1105
(index_memo, compression_parent, parents,
1106
record_details) = details
1107
method = record_details[0]
1108
if compression_parent is not None:
1109
pending_components.add(compression_parent)
1110
component_data[key] = self._build_details_to_components(details)
1111
missing = current_components.difference(build_details)
1112
if missing and not allow_missing:
1113
raise errors.RevisionNotPresent(missing.pop(), self)
1114
return component_data
1116
def _get_content(self, key, parent_texts={}):
1117
"""Returns a content object that makes up the specified
1119
cached_version = parent_texts.get(key, None)
1120
if cached_version is not None:
1121
# Ensure the cache dict is valid.
1122
if not self.get_parent_map([key]):
1123
raise RevisionNotPresent(key, self)
1124
return cached_version
1125
generator = _VFContentMapGenerator(self, [key])
1126
return generator._get_content(key)
1128
def get_parent_map(self, keys):
1129
"""Get a map of the graph parents of keys.
1131
:param keys: The keys to look up parents for.
1132
:return: A mapping from keys to parents. Absent keys are absent from
1135
return self._get_parent_map_with_sources(keys)[0]
1137
def _get_parent_map_with_sources(self, keys):
1138
"""Get a map of the parents of keys.
1140
:param keys: The keys to look up parents for.
1141
:return: A tuple. The first element is a mapping from keys to parents.
1142
Absent keys are absent from the mapping. The second element is a
1143
list with the locations each key was found in. The first element
1144
is the in-this-knit parents, the second the first fallback source,
1148
sources = [self._index] + self._fallback_vfs
1151
for source in sources:
1154
new_result = source.get_parent_map(missing)
1155
source_results.append(new_result)
1156
result.update(new_result)
1157
missing.difference_update(set(new_result))
1158
return result, source_results
1160
def _get_record_map(self, keys, allow_missing=False):
1161
"""Produce a dictionary of knit records.
1163
:return: {key:(record, record_details, digest, next)}
1165
data returned from read_records (a KnitContentobject)
1167
opaque information to pass to parse_record
1169
SHA1 digest of the full text after all steps are done
1171
build-parent of the version, i.e. the leftmost ancestor.
1172
Will be None if the record is not a delta.
1173
:param keys: The keys to build a map for
1174
:param allow_missing: If some records are missing, rather than
1175
error, just return the data that could be generated.
1177
raw_map = self._get_record_map_unparsed(keys,
1178
allow_missing=allow_missing)
1179
return self._raw_map_to_record_map(raw_map)
1181
def _raw_map_to_record_map(self, raw_map):
1182
"""Parse the contents of _get_record_map_unparsed.
1184
:return: see _get_record_map.
1188
data, record_details, next = raw_map[key]
1189
content, digest = self._parse_record(key[-1], data)
1190
result[key] = content, record_details, digest, next
1193
def _get_record_map_unparsed(self, keys, allow_missing=False):
1194
"""Get the raw data for reconstructing keys without parsing it.
1196
:return: A dict suitable for parsing via _raw_map_to_record_map.
1197
key-> raw_bytes, (method, noeol), compression_parent
1199
# This retries the whole request if anything fails. Potentially we
1200
# could be a bit more selective. We could track the keys whose records
1201
# we have successfully found, and then only request the new records
1202
# from there. However, _get_components_positions grabs the whole build
1203
# chain, which means we'll likely try to grab the same records again
1204
# anyway. Also, can the build chains change as part of a pack
1205
# operation? We wouldn't want to end up with a broken chain.
1208
position_map = self._get_components_positions(keys,
1209
allow_missing=allow_missing)
1210
# key = component_id, r = record_details, i_m = index_memo,
1212
records = [(key, i_m) for key, (r, i_m, n)
1213
in position_map.iteritems()]
1214
# Sort by the index memo, so that we request records from the
1215
# same pack file together, and in forward-sorted order
1216
records.sort(key=operator.itemgetter(1))
1218
for key, data in self._read_records_iter_unchecked(records):
1219
(record_details, index_memo, next) = position_map[key]
1220
raw_record_map[key] = data, record_details, next
1221
return raw_record_map
1222
except errors.RetryWithNewPacks, e:
1223
self._access.reload_or_raise(e)
1226
def _split_by_prefix(cls, keys):
1227
"""For the given keys, split them up based on their prefix.
1229
To keep memory pressure somewhat under control, split the
1230
requests back into per-file-id requests, otherwise "bzr co"
1231
extracts the full tree into memory before writing it to disk.
1232
This should be revisited if _get_content_maps() can ever cross
1235
The keys for a given file_id are kept in the same relative order.
1236
Ordering between file_ids is not, though prefix_order will return the
1237
order that the key was first seen.
1239
:param keys: An iterable of key tuples
1240
:return: (split_map, prefix_order)
1241
split_map A dictionary mapping prefix => keys
1242
prefix_order The order that we saw the various prefixes
1244
split_by_prefix = {}
1252
if prefix in split_by_prefix:
1253
split_by_prefix[prefix].append(key)
1255
split_by_prefix[prefix] = [key]
1256
prefix_order.append(prefix)
1257
return split_by_prefix, prefix_order
1259
def _group_keys_for_io(self, keys, non_local_keys, positions,
1260
_min_buffer_size=_STREAM_MIN_BUFFER_SIZE):
1261
"""For the given keys, group them into 'best-sized' requests.
1263
The idea is to avoid making 1 request per file, but to never try to
1264
unpack an entire 1.5GB source tree in a single pass. Also when
1265
possible, we should try to group requests to the same pack file
1268
:return: list of (keys, non_local) tuples that indicate what keys
1269
should be fetched next.
1271
# TODO: Ideally we would group on 2 factors. We want to extract texts
1272
# from the same pack file together, and we want to extract all
1273
# the texts for a given build-chain together. Ultimately it
1274
# probably needs a better global view.
1275
total_keys = len(keys)
1276
prefix_split_keys, prefix_order = self._split_by_prefix(keys)
1277
prefix_split_non_local_keys, _ = self._split_by_prefix(non_local_keys)
1279
cur_non_local = set()
1283
for prefix in prefix_order:
1284
keys = prefix_split_keys[prefix]
1285
non_local = prefix_split_non_local_keys.get(prefix, [])
1287
this_size = self._index._get_total_build_size(keys, positions)
1288
cur_size += this_size
1289
cur_keys.extend(keys)
1290
cur_non_local.update(non_local)
1291
if cur_size > _min_buffer_size:
1292
result.append((cur_keys, cur_non_local))
1293
sizes.append(cur_size)
1295
cur_non_local = set()
1298
result.append((cur_keys, cur_non_local))
1299
sizes.append(cur_size)
1302
def get_record_stream(self, keys, ordering, include_delta_closure):
1303
"""Get a stream of records for keys.
1305
:param keys: The keys to include.
1306
:param ordering: Either 'unordered' or 'topological'. A topologically
1307
sorted stream has compression parents strictly before their
1309
:param include_delta_closure: If True then the closure across any
1310
compression parents will be included (in the opaque data).
1311
:return: An iterator of ContentFactory objects, each of which is only
1312
valid until the iterator is advanced.
1314
# keys might be a generator
1318
if not self._index.has_graph:
1319
# Cannot topological order when no graph has been stored.
1320
ordering = 'unordered'
1322
remaining_keys = keys
1325
keys = set(remaining_keys)
1326
for content_factory in self._get_remaining_record_stream(keys,
1327
ordering, include_delta_closure):
1328
remaining_keys.discard(content_factory.key)
1329
yield content_factory
1331
except errors.RetryWithNewPacks, e:
1332
self._access.reload_or_raise(e)
1334
def _get_remaining_record_stream(self, keys, ordering,
1335
include_delta_closure):
1336
"""This function is the 'retry' portion for get_record_stream."""
1337
if include_delta_closure:
1338
positions = self._get_components_positions(keys, allow_missing=True)
1340
build_details = self._index.get_build_details(keys)
1342
# (record_details, access_memo, compression_parent_key)
1343
positions = dict((key, self._build_details_to_components(details))
1344
for key, details in build_details.iteritems())
1345
absent_keys = keys.difference(set(positions))
1346
# There may be more absent keys : if we're missing the basis component
1347
# and are trying to include the delta closure.
1348
# XXX: We should not ever need to examine remote sources because we do
1349
# not permit deltas across versioned files boundaries.
1350
if include_delta_closure:
1351
needed_from_fallback = set()
1352
# Build up reconstructable_keys dict. key:True in this dict means
1353
# the key can be reconstructed.
1354
reconstructable_keys = {}
1358
chain = [key, positions[key][2]]
1360
needed_from_fallback.add(key)
1363
while chain[-1] is not None:
1364
if chain[-1] in reconstructable_keys:
1365
result = reconstructable_keys[chain[-1]]
1369
chain.append(positions[chain[-1]][2])
1371
# missing basis component
1372
needed_from_fallback.add(chain[-1])
1375
for chain_key in chain[:-1]:
1376
reconstructable_keys[chain_key] = result
1378
needed_from_fallback.add(key)
1379
# Double index lookups here : need a unified api ?
1380
global_map, parent_maps = self._get_parent_map_with_sources(keys)
1381
if ordering == 'topological':
1382
# Global topological sort
1383
present_keys = tsort.topo_sort(global_map)
1384
# Now group by source:
1386
current_source = None
1387
for key in present_keys:
1388
for parent_map in parent_maps:
1389
if key in parent_map:
1390
key_source = parent_map
1392
if current_source is not key_source:
1393
source_keys.append((key_source, []))
1394
current_source = key_source
1395
source_keys[-1][1].append(key)
1397
if ordering != 'unordered':
1398
raise AssertionError('valid values for ordering are:'
1399
' "unordered" or "topological" not: %r'
1401
# Just group by source; remote sources first.
1404
for parent_map in reversed(parent_maps):
1405
source_keys.append((parent_map, []))
1406
for key in parent_map:
1407
present_keys.append(key)
1408
source_keys[-1][1].append(key)
1409
# We have been requested to return these records in an order that
1410
# suits us. So we ask the index to give us an optimally sorted
1412
for source, sub_keys in source_keys:
1413
if source is parent_maps[0]:
1414
# Only sort the keys for this VF
1415
self._index._sort_keys_by_io(sub_keys, positions)
1416
absent_keys = keys - set(global_map)
1417
for key in absent_keys:
1418
yield AbsentContentFactory(key)
1419
# restrict our view to the keys we can answer.
1420
# XXX: Memory: TODO: batch data here to cap buffered data at (say) 1MB.
1421
# XXX: At that point we need to consider the impact of double reads by
1422
# utilising components multiple times.
1423
if include_delta_closure:
1424
# XXX: get_content_maps performs its own index queries; allow state
1426
non_local_keys = needed_from_fallback - absent_keys
1427
for keys, non_local_keys in self._group_keys_for_io(present_keys,
1430
generator = _VFContentMapGenerator(self, keys, non_local_keys,
1432
for record in generator.get_record_stream():
1435
for source, keys in source_keys:
1436
if source is parent_maps[0]:
1437
# this KnitVersionedFiles
1438
records = [(key, positions[key][1]) for key in keys]
1439
for key, raw_data, sha1 in self._read_records_iter_raw(records):
1440
(record_details, index_memo, _) = positions[key]
1441
yield KnitContentFactory(key, global_map[key],
1442
record_details, sha1, raw_data, self._factory.annotated, None)
1444
vf = self._fallback_vfs[parent_maps.index(source) - 1]
1445
for record in vf.get_record_stream(keys, ordering,
1446
include_delta_closure):
1449
def get_sha1s(self, keys):
1450
"""See VersionedFiles.get_sha1s()."""
1452
record_map = self._get_record_map(missing, allow_missing=True)
1454
for key, details in record_map.iteritems():
1455
if key not in missing:
1457
# record entry 2 is the 'digest'.
1458
result[key] = details[2]
1459
missing.difference_update(set(result))
1460
for source in self._fallback_vfs:
1463
new_result = source.get_sha1s(missing)
1464
result.update(new_result)
1465
missing.difference_update(set(new_result))
1468
def insert_record_stream(self, stream):
1469
"""Insert a record stream into this container.
1471
:param stream: A stream of records to insert.
1473
:seealso VersionedFiles.get_record_stream:
1475
def get_adapter(adapter_key):
1477
return adapters[adapter_key]
1479
adapter_factory = adapter_registry.get(adapter_key)
1480
adapter = adapter_factory(self)
1481
adapters[adapter_key] = adapter
1484
if self._factory.annotated:
1485
# self is annotated, we need annotated knits to use directly.
1486
annotated = "annotated-"
1489
# self is not annotated, but we can strip annotations cheaply.
1491
convertibles = set(["knit-annotated-ft-gz"])
1492
if self._max_delta_chain:
1493
delta_types.add("knit-annotated-delta-gz")
1494
convertibles.add("knit-annotated-delta-gz")
1495
# The set of types we can cheaply adapt without needing basis texts.
1496
native_types = set()
1497
if self._max_delta_chain:
1498
native_types.add("knit-%sdelta-gz" % annotated)
1499
delta_types.add("knit-%sdelta-gz" % annotated)
1500
native_types.add("knit-%sft-gz" % annotated)
1501
knit_types = native_types.union(convertibles)
1503
# Buffer all index entries that we can't add immediately because their
1504
# basis parent is missing. We don't buffer all because generating
1505
# annotations may require access to some of the new records. However we
1506
# can't generate annotations from new deltas until their basis parent
1507
# is present anyway, so we get away with not needing an index that
1508
# includes the new keys.
1510
# See <http://launchpad.net/bugs/300177> about ordering of compression
1511
# parents in the records - to be conservative, we insist that all
1512
# parents must be present to avoid expanding to a fulltext.
1514
# key = basis_parent, value = index entry to add
1515
buffered_index_entries = {}
1516
for record in stream:
1518
parents = record.parents
1519
if record.storage_kind in delta_types:
1520
# TODO: eventually the record itself should track
1521
# compression_parent
1522
compression_parent = parents[0]
1524
compression_parent = None
1525
# Raise an error when a record is missing.
1526
if record.storage_kind == 'absent':
1527
raise RevisionNotPresent([record.key], self)
1528
elif ((record.storage_kind in knit_types)
1529
and (compression_parent is None
1530
or not self._fallback_vfs
1531
or self._index.has_key(compression_parent)
1532
or not self.has_key(compression_parent))):
1533
# we can insert the knit record literally if either it has no
1534
# compression parent OR we already have its basis in this kvf
1535
# OR the basis is not present even in the fallbacks. In the
1536
# last case it will either turn up later in the stream and all
1537
# will be well, or it won't turn up at all and we'll raise an
1540
# TODO: self.has_key is somewhat redundant with
1541
# self._index.has_key; we really want something that directly
1542
# asks if it's only present in the fallbacks. -- mbp 20081119
1543
if record.storage_kind not in native_types:
1545
adapter_key = (record.storage_kind, "knit-delta-gz")
1546
adapter = get_adapter(adapter_key)
1548
adapter_key = (record.storage_kind, "knit-ft-gz")
1549
adapter = get_adapter(adapter_key)
1550
bytes = adapter.get_bytes(record)
1552
# It's a knit record, it has a _raw_record field (even if
1553
# it was reconstituted from a network stream).
1554
bytes = record._raw_record
1555
options = [record._build_details[0]]
1556
if record._build_details[1]:
1557
options.append('no-eol')
1558
# Just blat it across.
1559
# Note: This does end up adding data on duplicate keys. As
1560
# modern repositories use atomic insertions this should not
1561
# lead to excessive growth in the event of interrupted fetches.
1562
# 'knit' repositories may suffer excessive growth, but as a
1563
# deprecated format this is tolerable. It can be fixed if
1564
# needed by in the kndx index support raising on a duplicate
1565
# add with identical parents and options.
1566
access_memo = self._access.add_raw_records(
1567
[(record.key, len(bytes))], bytes)[0]
1568
index_entry = (record.key, options, access_memo, parents)
1569
if 'fulltext' not in options:
1570
# Not a fulltext, so we need to make sure the compression
1571
# parent will also be present.
1572
# Note that pack backed knits don't need to buffer here
1573
# because they buffer all writes to the transaction level,
1574
# but we don't expose that difference at the index level. If
1575
# the query here has sufficient cost to show up in
1576
# profiling we should do that.
1578
# They're required to be physically in this
1579
# KnitVersionedFiles, not in a fallback.
1580
if not self._index.has_key(compression_parent):
1581
pending = buffered_index_entries.setdefault(
1582
compression_parent, [])
1583
pending.append(index_entry)
1586
self._index.add_records([index_entry])
1587
elif record.storage_kind == 'chunked':
1588
self.add_lines(record.key, parents,
1589
osutils.chunks_to_lines(record.get_bytes_as('chunked')))
1591
# Not suitable for direct insertion as a
1592
# delta, either because it's not the right format, or this
1593
# KnitVersionedFiles doesn't permit deltas (_max_delta_chain ==
1594
# 0) or because it depends on a base only present in the
1597
# Try getting a fulltext directly from the record.
1598
bytes = record.get_bytes_as('fulltext')
1599
except errors.UnavailableRepresentation:
1600
adapter_key = record.storage_kind, 'fulltext'
1601
adapter = get_adapter(adapter_key)
1602
bytes = adapter.get_bytes(record)
1603
lines = split_lines(bytes)
1605
self.add_lines(record.key, parents, lines)
1606
except errors.RevisionAlreadyPresent:
1608
# Add any records whose basis parent is now available.
1610
added_keys = [record.key]
1612
key = added_keys.pop(0)
1613
if key in buffered_index_entries:
1614
index_entries = buffered_index_entries[key]
1615
self._index.add_records(index_entries)
1617
[index_entry[0] for index_entry in index_entries])
1618
del buffered_index_entries[key]
1619
if buffered_index_entries:
1620
# There were index entries buffered at the end of the stream,
1621
# So these need to be added (if the index supports holding such
1622
# entries for later insertion)
1623
for key in buffered_index_entries:
1624
index_entries = buffered_index_entries[key]
1625
self._index.add_records(index_entries,
1626
missing_compression_parents=True)
1628
def get_missing_compression_parent_keys(self):
1629
"""Return an iterable of keys of missing compression parents.
1631
Check this after calling insert_record_stream to find out if there are
1632
any missing compression parents. If there are, the records that
1633
depend on them are not able to be inserted safely. For atomic
1634
KnitVersionedFiles built on packs, the transaction should be aborted or
1635
suspended - commit will fail at this point. Nonatomic knits will error
1636
earlier because they have no staging area to put pending entries into.
1638
return self._index.get_missing_compression_parents()
1640
def iter_lines_added_or_present_in_keys(self, keys, pb=None):
1641
"""Iterate over the lines in the versioned files from keys.
1643
This may return lines from other keys. Each item the returned
1644
iterator yields is a tuple of a line and a text version that that line
1645
is present in (not introduced in).
1647
Ordering of results is in whatever order is most suitable for the
1648
underlying storage format.
1650
If a progress bar is supplied, it may be used to indicate progress.
1651
The caller is responsible for cleaning up progress bars (because this
1655
* Lines are normalised by the underlying store: they will all have \\n
1657
* Lines are returned in arbitrary order.
1658
* If a requested key did not change any lines (or didn't have any
1659
lines), it may not be mentioned at all in the result.
1661
:return: An iterator over (line, key).
1664
pb = progress.DummyProgress()
1670
# we don't care about inclusions, the caller cares.
1671
# but we need to setup a list of records to visit.
1672
# we need key, position, length
1674
build_details = self._index.get_build_details(keys)
1675
for key, details in build_details.iteritems():
1677
key_records.append((key, details[0]))
1678
records_iter = enumerate(self._read_records_iter(key_records))
1679
for (key_idx, (key, data, sha_value)) in records_iter:
1680
pb.update('Walking content.', key_idx, total)
1681
compression_parent = build_details[key][1]
1682
if compression_parent is None:
1684
line_iterator = self._factory.get_fulltext_content(data)
1687
line_iterator = self._factory.get_linedelta_content(data)
1688
# Now that we are yielding the data for this key, remove it
1691
# XXX: It might be more efficient to yield (key,
1692
# line_iterator) in the future. However for now, this is a
1693
# simpler change to integrate into the rest of the
1694
# codebase. RBC 20071110
1695
for line in line_iterator:
1698
except errors.RetryWithNewPacks, e:
1699
self._access.reload_or_raise(e)
1700
# If there are still keys we've not yet found, we look in the fallback
1701
# vfs, and hope to find them there. Note that if the keys are found
1702
# but had no changes or no content, the fallback may not return
1704
if keys and not self._fallback_vfs:
1705
# XXX: strictly the second parameter is meant to be the file id
1706
# but it's not easily accessible here.
1707
raise RevisionNotPresent(keys, repr(self))
1708
for source in self._fallback_vfs:
1712
for line, key in source.iter_lines_added_or_present_in_keys(keys):
1713
source_keys.add(key)
1715
keys.difference_update(source_keys)
1716
pb.update('Walking content.', total, total)
1718
def _make_line_delta(self, delta_seq, new_content):
1719
"""Generate a line delta from delta_seq and new_content."""
1721
for op in delta_seq.get_opcodes():
1722
if op[0] == 'equal':
1724
diff_hunks.append((op[1], op[2], op[4]-op[3], new_content._lines[op[3]:op[4]]))
1727
def _merge_annotations(self, content, parents, parent_texts={},
1728
delta=None, annotated=None,
1729
left_matching_blocks=None):
1730
"""Merge annotations for content and generate deltas.
1732
This is done by comparing the annotations based on changes to the text
1733
and generating a delta on the resulting full texts. If annotations are
1734
not being created then a simple delta is created.
1736
if left_matching_blocks is not None:
1737
delta_seq = diff._PrematchedMatcher(left_matching_blocks)
1741
for parent_key in parents:
1742
merge_content = self._get_content(parent_key, parent_texts)
1743
if (parent_key == parents[0] and delta_seq is not None):
1746
seq = patiencediff.PatienceSequenceMatcher(
1747
None, merge_content.text(), content.text())
1748
for i, j, n in seq.get_matching_blocks():
1751
# this copies (origin, text) pairs across to the new
1752
# content for any line that matches the last-checked
1754
content._lines[j:j+n] = merge_content._lines[i:i+n]
1755
# XXX: Robert says the following block is a workaround for a
1756
# now-fixed bug and it can probably be deleted. -- mbp 20080618
1757
if content._lines and content._lines[-1][1][-1] != '\n':
1758
# The copied annotation was from a line without a trailing EOL,
1759
# reinstate one for the content object, to ensure correct
1761
line = content._lines[-1][1] + '\n'
1762
content._lines[-1] = (content._lines[-1][0], line)
1764
if delta_seq is None:
1765
reference_content = self._get_content(parents[0], parent_texts)
1766
new_texts = content.text()
1767
old_texts = reference_content.text()
1768
delta_seq = patiencediff.PatienceSequenceMatcher(
1769
None, old_texts, new_texts)
1770
return self._make_line_delta(delta_seq, content)
1772
def _parse_record(self, version_id, data):
1773
"""Parse an original format knit record.
1775
These have the last element of the key only present in the stored data.
1777
rec, record_contents = self._parse_record_unchecked(data)
1778
self._check_header_version(rec, version_id)
1779
return record_contents, rec[3]
1781
def _parse_record_header(self, key, raw_data):
1782
"""Parse a record header for consistency.
1784
:return: the header and the decompressor stream.
1785
as (stream, header_record)
1787
df = tuned_gzip.GzipFile(mode='rb', fileobj=StringIO(raw_data))
1790
rec = self._check_header(key, df.readline())
1791
except Exception, e:
1792
raise KnitCorrupt(self,
1793
"While reading {%s} got %s(%s)"
1794
% (key, e.__class__.__name__, str(e)))
1797
def _parse_record_unchecked(self, data):
1799
# 4168 calls in 2880 217 internal
1800
# 4168 calls to _parse_record_header in 2121
1801
# 4168 calls to readlines in 330
1802
df = tuned_gzip.GzipFile(mode='rb', fileobj=StringIO(data))
1804
record_contents = df.readlines()
1805
except Exception, e:
1806
raise KnitCorrupt(self, "Corrupt compressed record %r, got %s(%s)" %
1807
(data, e.__class__.__name__, str(e)))
1808
header = record_contents.pop(0)
1809
rec = self._split_header(header)
1810
last_line = record_contents.pop()
1811
if len(record_contents) != int(rec[2]):
1812
raise KnitCorrupt(self,
1813
'incorrect number of lines %s != %s'
1814
' for version {%s} %s'
1815
% (len(record_contents), int(rec[2]),
1816
rec[1], record_contents))
1817
if last_line != 'end %s\n' % rec[1]:
1818
raise KnitCorrupt(self,
1819
'unexpected version end line %r, wanted %r'
1820
% (last_line, rec[1]))
1822
return rec, record_contents
1824
def _read_records_iter(self, records):
1825
"""Read text records from data file and yield result.
1827
The result will be returned in whatever is the fastest to read.
1828
Not by the order requested. Also, multiple requests for the same
1829
record will only yield 1 response.
1830
:param records: A list of (key, access_memo) entries
1831
:return: Yields (key, contents, digest) in the order
1832
read, not the order requested
1837
# XXX: This smells wrong, IO may not be getting ordered right.
1838
needed_records = sorted(set(records), key=operator.itemgetter(1))
1839
if not needed_records:
1842
# The transport optimizes the fetching as well
1843
# (ie, reads continuous ranges.)
1844
raw_data = self._access.get_raw_records(
1845
[index_memo for key, index_memo in needed_records])
1847
for (key, index_memo), data in \
1848
izip(iter(needed_records), raw_data):
1849
content, digest = self._parse_record(key[-1], data)
1850
yield key, content, digest
1852
def _read_records_iter_raw(self, records):
1853
"""Read text records from data file and yield raw data.
1855
This unpacks enough of the text record to validate the id is
1856
as expected but thats all.
1858
Each item the iterator yields is (key, bytes,
1859
expected_sha1_of_full_text).
1861
for key, data in self._read_records_iter_unchecked(records):
1862
# validate the header (note that we can only use the suffix in
1863
# current knit records).
1864
df, rec = self._parse_record_header(key, data)
1866
yield key, data, rec[3]
1868
def _read_records_iter_unchecked(self, records):
1869
"""Read text records from data file and yield raw data.
1871
No validation is done.
1873
Yields tuples of (key, data).
1875
# setup an iterator of the external records:
1876
# uses readv so nice and fast we hope.
1878
# grab the disk data needed.
1879
needed_offsets = [index_memo for key, index_memo
1881
raw_records = self._access.get_raw_records(needed_offsets)
1883
for key, index_memo in records:
1884
data = raw_records.next()
1887
def _record_to_data(self, key, digest, lines, dense_lines=None):
1888
"""Convert key, digest, lines into a raw data block.
1890
:param key: The key of the record. Currently keys are always serialised
1891
using just the trailing component.
1892
:param dense_lines: The bytes of lines but in a denser form. For
1893
instance, if lines is a list of 1000 bytestrings each ending in \n,
1894
dense_lines may be a list with one line in it, containing all the
1895
1000's lines and their \n's. Using dense_lines if it is already
1896
known is a win because the string join to create bytes in this
1897
function spends less time resizing the final string.
1898
:return: (len, a StringIO instance with the raw data ready to read.)
1900
# Note: using a string copy here increases memory pressure with e.g.
1901
# ISO's, but it is about 3 seconds faster on a 1.2Ghz intel machine
1902
# when doing the initial commit of a mozilla tree. RBC 20070921
1903
bytes = ''.join(chain(
1904
["version %s %d %s\n" % (key[-1],
1907
dense_lines or lines,
1908
["end %s\n" % key[-1]]))
1909
if type(bytes) != str:
1910
raise AssertionError(
1911
'data must be plain bytes was %s' % type(bytes))
1912
if lines and lines[-1][-1] != '\n':
1913
raise ValueError('corrupt lines value %r' % lines)
1914
compressed_bytes = tuned_gzip.bytes_to_gzip(bytes)
1915
return len(compressed_bytes), compressed_bytes
1917
def _split_header(self, line):
1920
raise KnitCorrupt(self,
1921
'unexpected number of elements in record header')
1925
"""See VersionedFiles.keys."""
1926
if 'evil' in debug.debug_flags:
1927
trace.mutter_callsite(2, "keys scales with size of history")
1928
sources = [self._index] + self._fallback_vfs
1930
for source in sources:
1931
result.update(source.keys())
1935
class _ContentMapGenerator(object):
1936
"""Generate texts or expose raw deltas for a set of texts."""
1938
def _get_content(self, key):
1939
"""Get the content object for key."""
1940
# Note that _get_content is only called when the _ContentMapGenerator
1941
# has been constructed with just one key requested for reconstruction.
1942
if key in self.nonlocal_keys:
1943
record = self.get_record_stream().next()
1944
# Create a content object on the fly
1945
lines = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
1946
return PlainKnitContent(lines, record.key)
1948
# local keys we can ask for directly
1949
return self._get_one_work(key)
1951
def get_record_stream(self):
1952
"""Get a record stream for the keys requested during __init__."""
1953
for record in self._work():
1957
"""Produce maps of text and KnitContents as dicts.
1959
:return: (text_map, content_map) where text_map contains the texts for
1960
the requested versions and content_map contains the KnitContents.
1962
# NB: By definition we never need to read remote sources unless texts
1963
# are requested from them: we don't delta across stores - and we
1964
# explicitly do not want to to prevent data loss situations.
1965
if self.global_map is None:
1966
self.global_map = self.vf.get_parent_map(self.keys)
1967
nonlocal_keys = self.nonlocal_keys
1969
missing_keys = set(nonlocal_keys)
1970
# Read from remote versioned file instances and provide to our caller.
1971
for source in self.vf._fallback_vfs:
1972
if not missing_keys:
1974
# Loop over fallback repositories asking them for texts - ignore
1975
# any missing from a particular fallback.
1976
for record in source.get_record_stream(missing_keys,
1978
if record.storage_kind == 'absent':
1979
# Not in thie particular stream, may be in one of the
1980
# other fallback vfs objects.
1982
missing_keys.remove(record.key)
1985
self._raw_record_map = self.vf._get_record_map_unparsed(self.keys,
1988
for key in self.keys:
1989
if key in self.nonlocal_keys:
1991
yield LazyKnitContentFactory(key, self.global_map[key], self, first)
1994
def _get_one_work(self, requested_key):
1995
# Now, if we have calculated everything already, just return the
1997
if requested_key in self._contents_map:
1998
return self._contents_map[requested_key]
1999
# To simplify things, parse everything at once - code that wants one text
2000
# probably wants them all.
2001
# FUTURE: This function could be improved for the 'extract many' case
2002
# by tracking each component and only doing the copy when the number of
2003
# children than need to apply delta's to it is > 1 or it is part of the
2005
multiple_versions = len(self.keys) != 1
2006
if self._record_map is None:
2007
self._record_map = self.vf._raw_map_to_record_map(
2008
self._raw_record_map)
2009
record_map = self._record_map
2010
# raw_record_map is key:
2011
# Have read and parsed records at this point.
2012
for key in self.keys:
2013
if key in self.nonlocal_keys:
2018
while cursor is not None:
2020
record, record_details, digest, next = record_map[cursor]
2022
raise RevisionNotPresent(cursor, self)
2023
components.append((cursor, record, record_details, digest))
2025
if cursor in self._contents_map:
2026
# no need to plan further back
2027
components.append((cursor, None, None, None))
2031
for (component_id, record, record_details,
2032
digest) in reversed(components):
2033
if component_id in self._contents_map:
2034
content = self._contents_map[component_id]
2036
content, delta = self._factory.parse_record(key[-1],
2037
record, record_details, content,
2038
copy_base_content=multiple_versions)
2039
if multiple_versions:
2040
self._contents_map[component_id] = content
2042
# digest here is the digest from the last applied component.
2043
text = content.text()
2044
actual_sha = sha_strings(text)
2045
if actual_sha != digest:
2046
raise SHA1KnitCorrupt(self, actual_sha, digest, key, text)
2047
if multiple_versions:
2048
return self._contents_map[requested_key]
2052
def _wire_bytes(self):
2053
"""Get the bytes to put on the wire for 'key'.
2055
The first collection of bytes asked for returns the serialised
2056
raw_record_map and the additional details (key, parent) for key.
2057
Subsequent calls return just the additional details (key, parent).
2058
The wire storage_kind given for the first key is 'knit-delta-closure',
2059
For subsequent keys it is 'knit-delta-closure-ref'.
2061
:param key: A key from the content generator.
2062
:return: Bytes to put on the wire.
2065
# kind marker for dispatch on the far side,
2066
lines.append('knit-delta-closure')
2068
if self.vf._factory.annotated:
2069
lines.append('annotated')
2072
# then the list of keys
2073
lines.append('\t'.join(['\x00'.join(key) for key in self.keys
2074
if key not in self.nonlocal_keys]))
2075
# then the _raw_record_map in serialised form:
2077
# for each item in the map:
2079
# 1 line with parents if the key is to be yielded (None: for None, '' for ())
2080
# one line with method
2081
# one line with noeol
2082
# one line with next ('' for None)
2083
# one line with byte count of the record bytes
2085
for key, (record_bytes, (method, noeol), next) in \
2086
self._raw_record_map.iteritems():
2087
key_bytes = '\x00'.join(key)
2088
parents = self.global_map.get(key, None)
2090
parent_bytes = 'None:'
2092
parent_bytes = '\t'.join('\x00'.join(key) for key in parents)
2093
method_bytes = method
2099
next_bytes = '\x00'.join(next)
2102
map_byte_list.append('%s\n%s\n%s\n%s\n%s\n%d\n%s' % (
2103
key_bytes, parent_bytes, method_bytes, noeol_bytes, next_bytes,
2104
len(record_bytes), record_bytes))
2105
map_bytes = ''.join(map_byte_list)
2106
lines.append(map_bytes)
2107
bytes = '\n'.join(lines)
2111
class _VFContentMapGenerator(_ContentMapGenerator):
2112
"""Content map generator reading from a VersionedFiles object."""
2114
def __init__(self, versioned_files, keys, nonlocal_keys=None,
2115
global_map=None, raw_record_map=None):
2116
"""Create a _ContentMapGenerator.
2118
:param versioned_files: The versioned files that the texts are being
2120
:param keys: The keys to produce content maps for.
2121
:param nonlocal_keys: An iterable of keys(possibly intersecting keys)
2122
which are known to not be in this knit, but rather in one of the
2124
:param global_map: The result of get_parent_map(keys) (or a supermap).
2125
This is required if get_record_stream() is to be used.
2126
:param raw_record_map: A unparsed raw record map to use for answering
2129
# The vf to source data from
2130
self.vf = versioned_files
2132
self.keys = list(keys)
2133
# Keys known to be in fallback vfs objects
2134
if nonlocal_keys is None:
2135
self.nonlocal_keys = set()
2137
self.nonlocal_keys = frozenset(nonlocal_keys)
2138
# Parents data for keys to be returned in get_record_stream
2139
self.global_map = global_map
2140
# The chunked lists for self.keys in text form
2142
# A cache of KnitContent objects used in extracting texts.
2143
self._contents_map = {}
2144
# All the knit records needed to assemble the requested keys as full
2146
self._record_map = None
2147
if raw_record_map is None:
2148
self._raw_record_map = self.vf._get_record_map_unparsed(keys,
2151
self._raw_record_map = raw_record_map
2152
# the factory for parsing records
2153
self._factory = self.vf._factory
2156
class _NetworkContentMapGenerator(_ContentMapGenerator):
2157
"""Content map generator sourced from a network stream."""
2159
def __init__(self, bytes, line_end):
2160
"""Construct a _NetworkContentMapGenerator from a bytes block."""
2162
self.global_map = {}
2163
self._raw_record_map = {}
2164
self._contents_map = {}
2165
self._record_map = None
2166
self.nonlocal_keys = []
2167
# Get access to record parsing facilities
2168
self.vf = KnitVersionedFiles(None, None)
2171
line_end = bytes.find('\n', start)
2172
line = bytes[start:line_end]
2173
start = line_end + 1
2174
if line == 'annotated':
2175
self._factory = KnitAnnotateFactory()
2177
self._factory = KnitPlainFactory()
2178
# list of keys to emit in get_record_stream
2179
line_end = bytes.find('\n', start)
2180
line = bytes[start:line_end]
2181
start = line_end + 1
2183
tuple(segment.split('\x00')) for segment in line.split('\t')
2185
# now a loop until the end. XXX: It would be nice if this was just a
2186
# bunch of the same records as get_record_stream(..., False) gives, but
2187
# there is a decent sized gap stopping that at the moment.
2191
line_end = bytes.find('\n', start)
2192
key = tuple(bytes[start:line_end].split('\x00'))
2193
start = line_end + 1
2194
# 1 line with parents (None: for None, '' for ())
2195
line_end = bytes.find('\n', start)
2196
line = bytes[start:line_end]
2201
[tuple(segment.split('\x00')) for segment in line.split('\t')
2203
self.global_map[key] = parents
2204
start = line_end + 1
2205
# one line with method
2206
line_end = bytes.find('\n', start)
2207
line = bytes[start:line_end]
2209
start = line_end + 1
2210
# one line with noeol
2211
line_end = bytes.find('\n', start)
2212
line = bytes[start:line_end]
2214
start = line_end + 1
2215
# one line with next ('' for None)
2216
line_end = bytes.find('\n', start)
2217
line = bytes[start:line_end]
2221
next = tuple(bytes[start:line_end].split('\x00'))
2222
start = line_end + 1
2223
# one line with byte count of the record bytes
2224
line_end = bytes.find('\n', start)
2225
line = bytes[start:line_end]
2227
start = line_end + 1
2229
record_bytes = bytes[start:start+count]
2230
start = start + count
2232
self._raw_record_map[key] = (record_bytes, (method, noeol), next)
2234
def get_record_stream(self):
2235
"""Get a record stream for for keys requested by the bytestream."""
2237
for key in self.keys:
2238
yield LazyKnitContentFactory(key, self.global_map[key], self, first)
2241
def _wire_bytes(self):
2245
class _KndxIndex(object):
2246
"""Manages knit index files
2248
The index is kept in memory and read on startup, to enable
2249
fast lookups of revision information. The cursor of the index
2250
file is always pointing to the end, making it easy to append
2253
_cache is a cache for fast mapping from version id to a Index
2256
_history is a cache for fast mapping from indexes to version ids.
2258
The index data format is dictionary compressed when it comes to
2259
parent references; a index entry may only have parents that with a
2260
lover index number. As a result, the index is topological sorted.
2262
Duplicate entries may be written to the index for a single version id
2263
if this is done then the latter one completely replaces the former:
2264
this allows updates to correct version and parent information.
2265
Note that the two entries may share the delta, and that successive
2266
annotations and references MUST point to the first entry.
2268
The index file on disc contains a header, followed by one line per knit
2269
record. The same revision can be present in an index file more than once.
2270
The first occurrence gets assigned a sequence number starting from 0.
2272
The format of a single line is
2273
REVISION_ID FLAGS BYTE_OFFSET LENGTH( PARENT_ID|PARENT_SEQUENCE_ID)* :\n
2274
REVISION_ID is a utf8-encoded revision id
2275
FLAGS is a comma separated list of flags about the record. Values include
2276
no-eol, line-delta, fulltext.
2277
BYTE_OFFSET is the ascii representation of the byte offset in the data file
2278
that the the compressed data starts at.
2279
LENGTH is the ascii representation of the length of the data file.
2280
PARENT_ID a utf-8 revision id prefixed by a '.' that is a parent of
2282
PARENT_SEQUENCE_ID the ascii representation of the sequence number of a
2283
revision id already in the knit that is a parent of REVISION_ID.
2284
The ' :' marker is the end of record marker.
2287
when a write is interrupted to the index file, it will result in a line
2288
that does not end in ' :'. If the ' :' is not present at the end of a line,
2289
or at the end of the file, then the record that is missing it will be
2290
ignored by the parser.
2292
When writing new records to the index file, the data is preceded by '\n'
2293
to ensure that records always start on new lines even if the last write was
2294
interrupted. As a result its normal for the last line in the index to be
2295
missing a trailing newline. One can be added with no harmful effects.
2297
:ivar _kndx_cache: dict from prefix to the old state of KnitIndex objects,
2298
where prefix is e.g. the (fileid,) for .texts instances or () for
2299
constant-mapped things like .revisions, and the old state is
2300
tuple(history_vector, cache_dict). This is used to prevent having an
2301
ABI change with the C extension that reads .kndx files.
2304
HEADER = "# bzr knit index 8\n"
2306
def __init__(self, transport, mapper, get_scope, allow_writes, is_locked):
2307
"""Create a _KndxIndex on transport using mapper."""
2308
self._transport = transport
2309
self._mapper = mapper
2310
self._get_scope = get_scope
2311
self._allow_writes = allow_writes
2312
self._is_locked = is_locked
2314
self.has_graph = True
2316
def add_records(self, records, random_id=False, missing_compression_parents=False):
2317
"""Add multiple records to the index.
2319
:param records: a list of tuples:
2320
(key, options, access_memo, parents).
2321
:param random_id: If True the ids being added were randomly generated
2322
and no check for existence will be performed.
2323
:param missing_compression_parents: If True the records being added are
2324
only compressed against texts already in the index (or inside
2325
records). If False the records all refer to unavailable texts (or
2326
texts inside records) as compression parents.
2328
if missing_compression_parents:
2329
# It might be nice to get the edge of the records. But keys isn't
2331
keys = sorted(record[0] for record in records)
2332
raise errors.RevisionNotPresent(keys, self)
2334
for record in records:
2337
path = self._mapper.map(key) + '.kndx'
2338
path_keys = paths.setdefault(path, (prefix, []))
2339
path_keys[1].append(record)
2340
for path in sorted(paths):
2341
prefix, path_keys = paths[path]
2342
self._load_prefixes([prefix])
2344
orig_history = self._kndx_cache[prefix][1][:]
2345
orig_cache = self._kndx_cache[prefix][0].copy()
2348
for key, options, (_, pos, size), parents in path_keys:
2350
# kndx indices cannot be parentless.
2352
line = "\n%s %s %s %s %s :" % (
2353
key[-1], ','.join(options), pos, size,
2354
self._dictionary_compress(parents))
2355
if type(line) != str:
2356
raise AssertionError(
2357
'data must be utf8 was %s' % type(line))
2359
self._cache_key(key, options, pos, size, parents)
2360
if len(orig_history):
2361
self._transport.append_bytes(path, ''.join(lines))
2363
self._init_index(path, lines)
2365
# If any problems happen, restore the original values and re-raise
2366
self._kndx_cache[prefix] = (orig_cache, orig_history)
2369
def scan_unvalidated_index(self, graph_index):
2370
"""See _KnitGraphIndex.scan_unvalidated_index."""
2371
# Because kndx files do not support atomic insertion via separate index
2372
# files, they do not support this method.
2373
raise NotImplementedError(self.scan_unvalidated_index)
2375
def get_missing_compression_parents(self):
2376
"""See _KnitGraphIndex.get_missing_compression_parents."""
2377
# Because kndx files do not support atomic insertion via separate index
2378
# files, they do not support this method.
2379
raise NotImplementedError(self.get_missing_compression_parents)
2381
def _cache_key(self, key, options, pos, size, parent_keys):
2382
"""Cache a version record in the history array and index cache.
2384
This is inlined into _load_data for performance. KEEP IN SYNC.
2385
(It saves 60ms, 25% of the __init__ overhead on local 4000 record
2389
version_id = key[-1]
2390
# last-element only for compatibilty with the C load_data.
2391
parents = tuple(parent[-1] for parent in parent_keys)
2392
for parent in parent_keys:
2393
if parent[:-1] != prefix:
2394
raise ValueError("mismatched prefixes for %r, %r" % (
2396
cache, history = self._kndx_cache[prefix]
2397
# only want the _history index to reference the 1st index entry
2399
if version_id not in cache:
2400
index = len(history)
2401
history.append(version_id)
2403
index = cache[version_id][5]
2404
cache[version_id] = (version_id,
2411
def check_header(self, fp):
2412
line = fp.readline()
2414
# An empty file can actually be treated as though the file doesn't
2416
raise errors.NoSuchFile(self)
2417
if line != self.HEADER:
2418
raise KnitHeaderError(badline=line, filename=self)
2420
def _check_read(self):
2421
if not self._is_locked():
2422
raise errors.ObjectNotLocked(self)
2423
if self._get_scope() != self._scope:
2426
def _check_write_ok(self):
2427
"""Assert if not writes are permitted."""
2428
if not self._is_locked():
2429
raise errors.ObjectNotLocked(self)
2430
if self._get_scope() != self._scope:
2432
if self._mode != 'w':
2433
raise errors.ReadOnlyObjectDirtiedError(self)
2435
def get_build_details(self, keys):
2436
"""Get the method, index_memo and compression parent for keys.
2438
Ghosts are omitted from the result.
2440
:param keys: An iterable of keys.
2441
:return: A dict of key:(index_memo, compression_parent, parents,
2444
opaque structure to pass to read_records to extract the raw
2447
Content that this record is built upon, may be None
2449
Logical parents of this node
2451
extra information about the content which needs to be passed to
2452
Factory.parse_record
2454
parent_map = self.get_parent_map(keys)
2457
if key not in parent_map:
2459
method = self.get_method(key)
2460
parents = parent_map[key]
2461
if method == 'fulltext':
2462
compression_parent = None
2464
compression_parent = parents[0]
2465
noeol = 'no-eol' in self.get_options(key)
2466
index_memo = self.get_position(key)
2467
result[key] = (index_memo, compression_parent,
2468
parents, (method, noeol))
2471
def get_method(self, key):
2472
"""Return compression method of specified key."""
2473
options = self.get_options(key)
2474
if 'fulltext' in options:
2476
elif 'line-delta' in options:
2479
raise errors.KnitIndexUnknownMethod(self, options)
2481
def get_options(self, key):
2482
"""Return a list representing options.
2486
prefix, suffix = self._split_key(key)
2487
self._load_prefixes([prefix])
2489
return self._kndx_cache[prefix][0][suffix][1]
2491
raise RevisionNotPresent(key, self)
2493
def get_parent_map(self, keys):
2494
"""Get a map of the parents of keys.
2496
:param keys: The keys to look up parents for.
2497
:return: A mapping from keys to parents. Absent keys are absent from
2500
# Parse what we need to up front, this potentially trades off I/O
2501
# locality (.kndx and .knit in the same block group for the same file
2502
# id) for less checking in inner loops.
2503
prefixes = set(key[:-1] for key in keys)
2504
self._load_prefixes(prefixes)
2509
suffix_parents = self._kndx_cache[prefix][0][key[-1]][4]
2513
result[key] = tuple(prefix + (suffix,) for
2514
suffix in suffix_parents)
2517
def get_position(self, key):
2518
"""Return details needed to access the version.
2520
:return: a tuple (key, data position, size) to hand to the access
2521
logic to get the record.
2523
prefix, suffix = self._split_key(key)
2524
self._load_prefixes([prefix])
2525
entry = self._kndx_cache[prefix][0][suffix]
2526
return key, entry[2], entry[3]
2528
has_key = _mod_index._has_key_from_parent_map
2530
def _init_index(self, path, extra_lines=[]):
2531
"""Initialize an index."""
2533
sio.write(self.HEADER)
2534
sio.writelines(extra_lines)
2536
self._transport.put_file_non_atomic(path, sio,
2537
create_parent_dir=True)
2538
# self._create_parent_dir)
2539
# mode=self._file_mode,
2540
# dir_mode=self._dir_mode)
2543
"""Get all the keys in the collection.
2545
The keys are not ordered.
2548
# Identify all key prefixes.
2549
# XXX: A bit hacky, needs polish.
2550
if type(self._mapper) == ConstantMapper:
2554
for quoted_relpath in self._transport.iter_files_recursive():
2555
path, ext = os.path.splitext(quoted_relpath)
2557
prefixes = [self._mapper.unmap(path) for path in relpaths]
2558
self._load_prefixes(prefixes)
2559
for prefix in prefixes:
2560
for suffix in self._kndx_cache[prefix][1]:
2561
result.add(prefix + (suffix,))
2564
def _load_prefixes(self, prefixes):
2565
"""Load the indices for prefixes."""
2567
for prefix in prefixes:
2568
if prefix not in self._kndx_cache:
2569
# the load_data interface writes to these variables.
2572
self._filename = prefix
2574
path = self._mapper.map(prefix) + '.kndx'
2575
fp = self._transport.get(path)
2577
# _load_data may raise NoSuchFile if the target knit is
2579
_load_data(self, fp)
2582
self._kndx_cache[prefix] = (self._cache, self._history)
2587
self._kndx_cache[prefix] = ({}, [])
2588
if type(self._mapper) == ConstantMapper:
2589
# preserve behaviour for revisions.kndx etc.
2590
self._init_index(path)
2595
missing_keys = _mod_index._missing_keys_from_parent_map
2597
def _partition_keys(self, keys):
2598
"""Turn keys into a dict of prefix:suffix_list."""
2601
prefix_keys = result.setdefault(key[:-1], [])
2602
prefix_keys.append(key[-1])
2605
def _dictionary_compress(self, keys):
2606
"""Dictionary compress keys.
2608
:param keys: The keys to generate references to.
2609
:return: A string representation of keys. keys which are present are
2610
dictionary compressed, and others are emitted as fulltext with a
2616
prefix = keys[0][:-1]
2617
cache = self._kndx_cache[prefix][0]
2619
if key[:-1] != prefix:
2620
# kndx indices cannot refer across partitioned storage.
2621
raise ValueError("mismatched prefixes for %r" % keys)
2622
if key[-1] in cache:
2623
# -- inlined lookup() --
2624
result_list.append(str(cache[key[-1]][5]))
2625
# -- end lookup () --
2627
result_list.append('.' + key[-1])
2628
return ' '.join(result_list)
2630
def _reset_cache(self):
2631
# Possibly this should be a LRU cache. A dictionary from key_prefix to
2632
# (cache_dict, history_vector) for parsed kndx files.
2633
self._kndx_cache = {}
2634
self._scope = self._get_scope()
2635
allow_writes = self._allow_writes()
2641
def _sort_keys_by_io(self, keys, positions):
2642
"""Figure out an optimal order to read the records for the given keys.
2644
Sort keys, grouped by index and sorted by position.
2646
:param keys: A list of keys whose records we want to read. This will be
2648
:param positions: A dict, such as the one returned by
2649
_get_components_positions()
2652
def get_sort_key(key):
2653
index_memo = positions[key][1]
2654
# Group by prefix and position. index_memo[0] is the key, so it is
2655
# (file_id, revision_id) and we don't want to sort on revision_id,
2656
# index_memo[1] is the position, and index_memo[2] is the size,
2657
# which doesn't matter for the sort
2658
return index_memo[0][:-1], index_memo[1]
2659
return keys.sort(key=get_sort_key)
2661
_get_total_build_size = _get_total_build_size
2663
def _split_key(self, key):
2664
"""Split key into a prefix and suffix."""
2665
return key[:-1], key[-1]
2668
class _KnitGraphIndex(object):
2669
"""A KnitVersionedFiles index layered on GraphIndex."""
2671
def __init__(self, graph_index, is_locked, deltas=False, parents=True,
2673
"""Construct a KnitGraphIndex on a graph_index.
2675
:param graph_index: An implementation of bzrlib.index.GraphIndex.
2676
:param is_locked: A callback to check whether the object should answer
2678
:param deltas: Allow delta-compressed records.
2679
:param parents: If True, record knits parents, if not do not record
2681
:param add_callback: If not None, allow additions to the index and call
2682
this callback with a list of added GraphIndex nodes:
2683
[(node, value, node_refs), ...]
2684
:param is_locked: A callback, returns True if the index is locked and
2687
self._add_callback = add_callback
2688
self._graph_index = graph_index
2689
self._deltas = deltas
2690
self._parents = parents
2691
if deltas and not parents:
2692
# XXX: TODO: Delta tree and parent graph should be conceptually
2694
raise KnitCorrupt(self, "Cannot do delta compression without "
2696
self.has_graph = parents
2697
self._is_locked = is_locked
2698
self._missing_compression_parents = set()
2701
return "%s(%r)" % (self.__class__.__name__, self._graph_index)
2703
def add_records(self, records, random_id=False,
2704
missing_compression_parents=False):
2705
"""Add multiple records to the index.
2707
This function does not insert data into the Immutable GraphIndex
2708
backing the KnitGraphIndex, instead it prepares data for insertion by
2709
the caller and checks that it is safe to insert then calls
2710
self._add_callback with the prepared GraphIndex nodes.
2712
:param records: a list of tuples:
2713
(key, options, access_memo, parents).
2714
:param random_id: If True the ids being added were randomly generated
2715
and no check for existence will be performed.
2716
:param missing_compression_parents: If True the records being added are
2717
only compressed against texts already in the index (or inside
2718
records). If False the records all refer to unavailable texts (or
2719
texts inside records) as compression parents.
2721
if not self._add_callback:
2722
raise errors.ReadOnlyError(self)
2723
# we hope there are no repositories with inconsistent parentage
2727
compression_parents = set()
2728
for (key, options, access_memo, parents) in records:
2730
parents = tuple(parents)
2731
index, pos, size = access_memo
2732
if 'no-eol' in options:
2736
value += "%d %d" % (pos, size)
2737
if not self._deltas:
2738
if 'line-delta' in options:
2739
raise KnitCorrupt(self, "attempt to add line-delta in non-delta knit")
2742
if 'line-delta' in options:
2743
node_refs = (parents, (parents[0],))
2744
if missing_compression_parents:
2745
compression_parents.add(parents[0])
2747
node_refs = (parents, ())
2749
node_refs = (parents, )
2752
raise KnitCorrupt(self, "attempt to add node with parents "
2753
"in parentless index.")
2755
keys[key] = (value, node_refs)
2758
present_nodes = self._get_entries(keys)
2759
for (index, key, value, node_refs) in present_nodes:
2760
if (value[0] != keys[key][0][0] or
2761
node_refs[:1] != keys[key][1][:1]):
2762
raise KnitCorrupt(self, "inconsistent details in add_records"
2763
": %s %s" % ((value, node_refs), keys[key]))
2767
for key, (value, node_refs) in keys.iteritems():
2768
result.append((key, value, node_refs))
2770
for key, (value, node_refs) in keys.iteritems():
2771
result.append((key, value))
2772
self._add_callback(result)
2773
if missing_compression_parents:
2774
# This may appear to be incorrect (it does not check for
2775
# compression parents that are in the existing graph index),
2776
# but such records won't have been buffered, so this is
2777
# actually correct: every entry when
2778
# missing_compression_parents==True either has a missing parent, or
2779
# a parent that is one of the keys in records.
2780
compression_parents.difference_update(keys)
2781
self._missing_compression_parents.update(compression_parents)
2782
# Adding records may have satisfied missing compression parents.
2783
self._missing_compression_parents.difference_update(keys)
2785
def scan_unvalidated_index(self, graph_index):
2786
"""Inform this _KnitGraphIndex that there is an unvalidated index.
2788
This allows this _KnitGraphIndex to keep track of any missing
2789
compression parents we may want to have filled in to make those
2792
:param graph_index: A GraphIndex
2795
new_missing = graph_index.external_references(ref_list_num=1)
2796
new_missing.difference_update(self.get_parent_map(new_missing))
2797
self._missing_compression_parents.update(new_missing)
2799
def get_missing_compression_parents(self):
2800
"""Return the keys of missing compression parents.
2802
Missing compression parents occur when a record stream was missing
2803
basis texts, or a index was scanned that had missing basis texts.
2805
return frozenset(self._missing_compression_parents)
2807
def _check_read(self):
2808
"""raise if reads are not permitted."""
2809
if not self._is_locked():
2810
raise errors.ObjectNotLocked(self)
2812
def _check_write_ok(self):
2813
"""Assert if writes are not permitted."""
2814
if not self._is_locked():
2815
raise errors.ObjectNotLocked(self)
2817
def _compression_parent(self, an_entry):
2818
# return the key that an_entry is compressed against, or None
2819
# Grab the second parent list (as deltas implies parents currently)
2820
compression_parents = an_entry[3][1]
2821
if not compression_parents:
2823
if len(compression_parents) != 1:
2824
raise AssertionError(
2825
"Too many compression parents: %r" % compression_parents)
2826
return compression_parents[0]
2828
def get_build_details(self, keys):
2829
"""Get the method, index_memo and compression parent for version_ids.
2831
Ghosts are omitted from the result.
2833
:param keys: An iterable of keys.
2834
:return: A dict of key:
2835
(index_memo, compression_parent, parents, record_details).
2837
opaque structure to pass to read_records to extract the raw
2840
Content that this record is built upon, may be None
2842
Logical parents of this node
2844
extra information about the content which needs to be passed to
2845
Factory.parse_record
2849
entries = self._get_entries(keys, False)
2850
for entry in entries:
2852
if not self._parents:
2855
parents = entry[3][0]
2856
if not self._deltas:
2857
compression_parent_key = None
2859
compression_parent_key = self._compression_parent(entry)
2860
noeol = (entry[2][0] == 'N')
2861
if compression_parent_key:
2862
method = 'line-delta'
2865
result[key] = (self._node_to_position(entry),
2866
compression_parent_key, parents,
2870
def _get_entries(self, keys, check_present=False):
2871
"""Get the entries for keys.
2873
:param keys: An iterable of index key tuples.
2878
for node in self._graph_index.iter_entries(keys):
2880
found_keys.add(node[1])
2882
# adapt parentless index to the rest of the code.
2883
for node in self._graph_index.iter_entries(keys):
2884
yield node[0], node[1], node[2], ()
2885
found_keys.add(node[1])
2887
missing_keys = keys.difference(found_keys)
2889
raise RevisionNotPresent(missing_keys.pop(), self)
2891
def get_method(self, key):
2892
"""Return compression method of specified key."""
2893
return self._get_method(self._get_node(key))
2895
def _get_method(self, node):
2896
if not self._deltas:
2898
if self._compression_parent(node):
2903
def _get_node(self, key):
2905
return list(self._get_entries([key]))[0]
2907
raise RevisionNotPresent(key, self)
2909
def get_options(self, key):
2910
"""Return a list representing options.
2914
node = self._get_node(key)
2915
options = [self._get_method(node)]
2916
if node[2][0] == 'N':
2917
options.append('no-eol')
2920
def get_parent_map(self, keys):
2921
"""Get a map of the parents of keys.
2923
:param keys: The keys to look up parents for.
2924
:return: A mapping from keys to parents. Absent keys are absent from
2928
nodes = self._get_entries(keys)
2932
result[node[1]] = node[3][0]
2935
result[node[1]] = None
2938
def get_position(self, key):
2939
"""Return details needed to access the version.
2941
:return: a tuple (index, data position, size) to hand to the access
2942
logic to get the record.
2944
node = self._get_node(key)
2945
return self._node_to_position(node)
2947
has_key = _mod_index._has_key_from_parent_map
2950
"""Get all the keys in the collection.
2952
The keys are not ordered.
2955
return [node[1] for node in self._graph_index.iter_all_entries()]
2957
missing_keys = _mod_index._missing_keys_from_parent_map
2959
def _node_to_position(self, node):
2960
"""Convert an index value to position details."""
2961
bits = node[2][1:].split(' ')
2962
return node[0], int(bits[0]), int(bits[1])
2964
def _sort_keys_by_io(self, keys, positions):
2965
"""Figure out an optimal order to read the records for the given keys.
2967
Sort keys, grouped by index and sorted by position.
2969
:param keys: A list of keys whose records we want to read. This will be
2971
:param positions: A dict, such as the one returned by
2972
_get_components_positions()
2975
def get_index_memo(key):
2976
# index_memo is at offset [1]. It is made up of (GraphIndex,
2977
# position, size). GI is an object, which will be unique for each
2978
# pack file. This causes us to group by pack file, then sort by
2979
# position. Size doesn't matter, but it isn't worth breaking up the
2981
return positions[key][1]
2982
return keys.sort(key=get_index_memo)
2984
_get_total_build_size = _get_total_build_size
2987
class _KnitKeyAccess(object):
2988
"""Access to records in .knit files."""
2990
def __init__(self, transport, mapper):
2991
"""Create a _KnitKeyAccess with transport and mapper.
2993
:param transport: The transport the access object is rooted at.
2994
:param mapper: The mapper used to map keys to .knit files.
2996
self._transport = transport
2997
self._mapper = mapper
2999
def add_raw_records(self, key_sizes, raw_data):
3000
"""Add raw knit bytes to a storage area.
3002
The data is spooled to the container writer in one bytes-record per
3005
:param sizes: An iterable of tuples containing the key and size of each
3007
:param raw_data: A bytestring containing the data.
3008
:return: A list of memos to retrieve the record later. Each memo is an
3009
opaque index memo. For _KnitKeyAccess the memo is (key, pos,
3010
length), where the key is the record key.
3012
if type(raw_data) != str:
3013
raise AssertionError(
3014
'data must be plain bytes was %s' % type(raw_data))
3017
# TODO: This can be tuned for writing to sftp and other servers where
3018
# append() is relatively expensive by grouping the writes to each key
3020
for key, size in key_sizes:
3021
path = self._mapper.map(key)
3023
base = self._transport.append_bytes(path + '.knit',
3024
raw_data[offset:offset+size])
3025
except errors.NoSuchFile:
3026
self._transport.mkdir(osutils.dirname(path))
3027
base = self._transport.append_bytes(path + '.knit',
3028
raw_data[offset:offset+size])
3032
result.append((key, base, size))
3035
def get_raw_records(self, memos_for_retrieval):
3036
"""Get the raw bytes for a records.
3038
:param memos_for_retrieval: An iterable containing the access memo for
3039
retrieving the bytes.
3040
:return: An iterator over the bytes of the records.
3042
# first pass, group into same-index request to minimise readv's issued.
3044
current_prefix = None
3045
for (key, offset, length) in memos_for_retrieval:
3046
if current_prefix == key[:-1]:
3047
current_list.append((offset, length))
3049
if current_prefix is not None:
3050
request_lists.append((current_prefix, current_list))
3051
current_prefix = key[:-1]
3052
current_list = [(offset, length)]
3053
# handle the last entry
3054
if current_prefix is not None:
3055
request_lists.append((current_prefix, current_list))
3056
for prefix, read_vector in request_lists:
3057
path = self._mapper.map(prefix) + '.knit'
3058
for pos, data in self._transport.readv(path, read_vector):
3062
class _DirectPackAccess(object):
3063
"""Access to data in one or more packs with less translation."""
3065
def __init__(self, index_to_packs, reload_func=None):
3066
"""Create a _DirectPackAccess object.
3068
:param index_to_packs: A dict mapping index objects to the transport
3069
and file names for obtaining data.
3070
:param reload_func: A function to call if we determine that the pack
3071
files have moved and we need to reload our caches. See
3072
bzrlib.repo_fmt.pack_repo.AggregateIndex for more details.
3074
self._container_writer = None
3075
self._write_index = None
3076
self._indices = index_to_packs
3077
self._reload_func = reload_func
3079
def add_raw_records(self, key_sizes, raw_data):
3080
"""Add raw knit bytes to a storage area.
3082
The data is spooled to the container writer in one bytes-record per
3085
:param sizes: An iterable of tuples containing the key and size of each
3087
:param raw_data: A bytestring containing the data.
3088
:return: A list of memos to retrieve the record later. Each memo is an
3089
opaque index memo. For _DirectPackAccess the memo is (index, pos,
3090
length), where the index field is the write_index object supplied
3091
to the PackAccess object.
3093
if type(raw_data) != str:
3094
raise AssertionError(
3095
'data must be plain bytes was %s' % type(raw_data))
3098
for key, size in key_sizes:
3099
p_offset, p_length = self._container_writer.add_bytes_record(
3100
raw_data[offset:offset+size], [])
3102
result.append((self._write_index, p_offset, p_length))
3105
def get_raw_records(self, memos_for_retrieval):
3106
"""Get the raw bytes for a records.
3108
:param memos_for_retrieval: An iterable containing the (index, pos,
3109
length) memo for retrieving the bytes. The Pack access method
3110
looks up the pack to use for a given record in its index_to_pack
3112
:return: An iterator over the bytes of the records.
3114
# first pass, group into same-index requests
3116
current_index = None
3117
for (index, offset, length) in memos_for_retrieval:
3118
if current_index == index:
3119
current_list.append((offset, length))
3121
if current_index is not None:
3122
request_lists.append((current_index, current_list))
3123
current_index = index
3124
current_list = [(offset, length)]
3125
# handle the last entry
3126
if current_index is not None:
3127
request_lists.append((current_index, current_list))
3128
for index, offsets in request_lists:
3130
transport, path = self._indices[index]
3132
# A KeyError here indicates that someone has triggered an index
3133
# reload, and this index has gone missing, we need to start
3135
if self._reload_func is None:
3136
# If we don't have a _reload_func there is nothing that can
3139
raise errors.RetryWithNewPacks(index,
3140
reload_occurred=True,
3141
exc_info=sys.exc_info())
3143
reader = pack.make_readv_reader(transport, path, offsets)
3144
for names, read_func in reader.iter_records():
3145
yield read_func(None)
3146
except errors.NoSuchFile:
3147
# A NoSuchFile error indicates that a pack file has gone
3148
# missing on disk, we need to trigger a reload, and start over.
3149
if self._reload_func is None:
3151
raise errors.RetryWithNewPacks(transport.abspath(path),
3152
reload_occurred=False,
3153
exc_info=sys.exc_info())
3155
def set_writer(self, writer, index, transport_packname):
3156
"""Set a writer to use for adding data."""
3157
if index is not None:
3158
self._indices[index] = transport_packname
3159
self._container_writer = writer
3160
self._write_index = index
3162
def reload_or_raise(self, retry_exc):
3163
"""Try calling the reload function, or re-raise the original exception.
3165
This should be called after _DirectPackAccess raises a
3166
RetryWithNewPacks exception. This function will handle the common logic
3167
of determining when the error is fatal versus being temporary.
3168
It will also make sure that the original exception is raised, rather
3169
than the RetryWithNewPacks exception.
3171
If this function returns, then the calling function should retry
3172
whatever operation was being performed. Otherwise an exception will
3175
:param retry_exc: A RetryWithNewPacks exception.
3178
if self._reload_func is None:
3180
elif not self._reload_func():
3181
# The reload claimed that nothing changed
3182
if not retry_exc.reload_occurred:
3183
# If there wasn't an earlier reload, then we really were
3184
# expecting to find changes. We didn't find them, so this is a
3188
exc_class, exc_value, exc_traceback = retry_exc.exc_info
3189
raise exc_class, exc_value, exc_traceback
3192
# Deprecated, use PatienceSequenceMatcher instead
3193
KnitSequenceMatcher = patiencediff.PatienceSequenceMatcher
3196
def annotate_knit(knit, revision_id):
3197
"""Annotate a knit with no cached annotations.
3199
This implementation is for knits with no cached annotations.
3200
It will work for knits with cached annotations, but this is not
3203
annotator = _KnitAnnotator(knit)
3204
return iter(annotator.annotate(revision_id))
3207
class _KnitAnnotator(object):
3208
"""Build up the annotations for a text."""
3210
def __init__(self, knit):
3213
# Content objects, differs from fulltexts because of how final newlines
3214
# are treated by knits. the content objects here will always have a
3216
self._fulltext_contents = {}
3218
# Annotated lines of specific revisions
3219
self._annotated_lines = {}
3221
# Track the raw data for nodes that we could not process yet.
3222
# This maps the revision_id of the base to a list of children that will
3223
# annotated from it.
3224
self._pending_children = {}
3226
# Nodes which cannot be extracted
3227
self._ghosts = set()
3229
# Track how many children this node has, so we know if we need to keep
3231
self._annotate_children = {}
3232
self._compression_children = {}
3234
self._all_build_details = {}
3235
# The children => parent revision_id graph
3236
self._revision_id_graph = {}
3238
self._heads_provider = None
3240
self._nodes_to_keep_annotations = set()
3241
self._generations_until_keep = 100
3243
def set_generations_until_keep(self, value):
3244
"""Set the number of generations before caching a node.
3246
Setting this to -1 will cache every merge node, setting this higher
3247
will cache fewer nodes.
3249
self._generations_until_keep = value
3251
def _add_fulltext_content(self, revision_id, content_obj):
3252
self._fulltext_contents[revision_id] = content_obj
3253
# TODO: jam 20080305 It might be good to check the sha1digest here
3254
return content_obj.text()
3256
def _check_parents(self, child, nodes_to_annotate):
3257
"""Check if all parents have been processed.
3259
:param child: A tuple of (rev_id, parents, raw_content)
3260
:param nodes_to_annotate: If child is ready, add it to
3261
nodes_to_annotate, otherwise put it back in self._pending_children
3263
for parent_id in child[1]:
3264
if (parent_id not in self._annotated_lines):
3265
# This parent is present, but another parent is missing
3266
self._pending_children.setdefault(parent_id,
3270
# This one is ready to be processed
3271
nodes_to_annotate.append(child)
3273
def _add_annotation(self, revision_id, fulltext, parent_ids,
3274
left_matching_blocks=None):
3275
"""Add an annotation entry.
3277
All parents should already have been annotated.
3278
:return: A list of children that now have their parents satisfied.
3280
a = self._annotated_lines
3281
annotated_parent_lines = [a[p] for p in parent_ids]
3282
annotated_lines = list(annotate.reannotate(annotated_parent_lines,
3283
fulltext, revision_id, left_matching_blocks,
3284
heads_provider=self._get_heads_provider()))
3285
self._annotated_lines[revision_id] = annotated_lines
3286
for p in parent_ids:
3287
ann_children = self._annotate_children[p]
3288
ann_children.remove(revision_id)
3289
if (not ann_children
3290
and p not in self._nodes_to_keep_annotations):
3291
del self._annotated_lines[p]
3292
del self._all_build_details[p]
3293
if p in self._fulltext_contents:
3294
del self._fulltext_contents[p]
3295
# Now that we've added this one, see if there are any pending
3296
# deltas to be done, certainly this parent is finished
3297
nodes_to_annotate = []
3298
for child in self._pending_children.pop(revision_id, []):
3299
self._check_parents(child, nodes_to_annotate)
3300
return nodes_to_annotate
3302
def _get_build_graph(self, key):
3303
"""Get the graphs for building texts and annotations.
3305
The data you need for creating a full text may be different than the
3306
data you need to annotate that text. (At a minimum, you need both
3307
parents to create an annotation, but only need 1 parent to generate the
3310
:return: A list of (key, index_memo) records, suitable for
3311
passing to read_records_iter to start reading in the raw data fro/
3314
if key in self._annotated_lines:
3317
pending = set([key])
3322
# get all pending nodes
3324
this_iteration = pending
3325
build_details = self._knit._index.get_build_details(this_iteration)
3326
self._all_build_details.update(build_details)
3327
# new_nodes = self._knit._index._get_entries(this_iteration)
3329
for key, details in build_details.iteritems():
3330
(index_memo, compression_parent, parents,
3331
record_details) = details
3332
self._revision_id_graph[key] = parents
3333
records.append((key, index_memo))
3334
# Do we actually need to check _annotated_lines?
3335
pending.update(p for p in parents
3336
if p not in self._all_build_details)
3337
if compression_parent:
3338
self._compression_children.setdefault(compression_parent,
3341
for parent in parents:
3342
self._annotate_children.setdefault(parent,
3344
num_gens = generation - kept_generation
3345
if ((num_gens >= self._generations_until_keep)
3346
and len(parents) > 1):
3347
kept_generation = generation
3348
self._nodes_to_keep_annotations.add(key)
3350
missing_versions = this_iteration.difference(build_details.keys())
3351
self._ghosts.update(missing_versions)
3352
for missing_version in missing_versions:
3353
# add a key, no parents
3354
self._revision_id_graph[missing_version] = ()
3355
pending.discard(missing_version) # don't look for it
3356
if self._ghosts.intersection(self._compression_children):
3358
"We cannot have nodes which have a ghost compression parent:\n"
3360
"compression children: %r"
3361
% (self._ghosts, self._compression_children))
3362
# Cleanout anything that depends on a ghost so that we don't wait for
3363
# the ghost to show up
3364
for node in self._ghosts:
3365
if node in self._annotate_children:
3366
# We won't be building this node
3367
del self._annotate_children[node]
3368
# Generally we will want to read the records in reverse order, because
3369
# we find the parent nodes after the children
3373
def _annotate_records(self, records):
3374
"""Build the annotations for the listed records."""
3375
# We iterate in the order read, rather than a strict order requested
3376
# However, process what we can, and put off to the side things that
3377
# still need parents, cleaning them up when those parents are
3379
for (rev_id, record,
3380
digest) in self._knit._read_records_iter(records):
3381
if rev_id in self._annotated_lines:
3383
parent_ids = self._revision_id_graph[rev_id]
3384
parent_ids = [p for p in parent_ids if p not in self._ghosts]
3385
details = self._all_build_details[rev_id]
3386
(index_memo, compression_parent, parents,
3387
record_details) = details
3388
nodes_to_annotate = []
3389
# TODO: Remove the punning between compression parents, and
3390
# parent_ids, we should be able to do this without assuming
3392
if len(parent_ids) == 0:
3393
# There are no parents for this node, so just add it
3394
# TODO: This probably needs to be decoupled
3395
fulltext_content, delta = self._knit._factory.parse_record(
3396
rev_id, record, record_details, None)
3397
fulltext = self._add_fulltext_content(rev_id, fulltext_content)
3398
nodes_to_annotate.extend(self._add_annotation(rev_id, fulltext,
3399
parent_ids, left_matching_blocks=None))
3401
child = (rev_id, parent_ids, record)
3402
# Check if all the parents are present
3403
self._check_parents(child, nodes_to_annotate)
3404
while nodes_to_annotate:
3405
# Should we use a queue here instead of a stack?
3406
(rev_id, parent_ids, record) = nodes_to_annotate.pop()
3407
(index_memo, compression_parent, parents,
3408
record_details) = self._all_build_details[rev_id]
3410
if compression_parent is not None:
3411
comp_children = self._compression_children[compression_parent]
3412
if rev_id not in comp_children:
3413
raise AssertionError("%r not in compression children %r"
3414
% (rev_id, comp_children))
3415
# If there is only 1 child, it is safe to reuse this
3417
reuse_content = (len(comp_children) == 1
3418
and compression_parent not in
3419
self._nodes_to_keep_annotations)
3421
# Remove it from the cache since it will be changing
3422
parent_fulltext_content = self._fulltext_contents.pop(compression_parent)
3423
# Make sure to copy the fulltext since it might be
3425
parent_fulltext = list(parent_fulltext_content.text())
3427
parent_fulltext_content = self._fulltext_contents[compression_parent]
3428
parent_fulltext = parent_fulltext_content.text()
3429
comp_children.remove(rev_id)
3430
fulltext_content, delta = self._knit._factory.parse_record(
3431
rev_id, record, record_details,
3432
parent_fulltext_content,
3433
copy_base_content=(not reuse_content))
3434
fulltext = self._add_fulltext_content(rev_id,
3436
if compression_parent == parent_ids[0]:
3437
# the compression_parent is the left parent, so we can
3439
blocks = KnitContent.get_line_delta_blocks(delta,
3440
parent_fulltext, fulltext)
3442
fulltext_content = self._knit._factory.parse_fulltext(
3444
fulltext = self._add_fulltext_content(rev_id,
3446
nodes_to_annotate.extend(
3447
self._add_annotation(rev_id, fulltext, parent_ids,
3448
left_matching_blocks=blocks))
3450
def _get_heads_provider(self):
3451
"""Create a heads provider for resolving ancestry issues."""
3452
if self._heads_provider is not None:
3453
return self._heads_provider
3454
parent_provider = _mod_graph.DictParentsProvider(
3455
self._revision_id_graph)
3456
graph_obj = _mod_graph.Graph(parent_provider)
3457
head_cache = _mod_graph.FrozenHeadsCache(graph_obj)
3458
self._heads_provider = head_cache
3461
def annotate(self, key):
3462
"""Return the annotated fulltext at the given key.
3464
:param key: The key to annotate.
3466
if len(self._knit._fallback_vfs) > 0:
3467
# stacked knits can't use the fast path at present.
3468
return self._simple_annotate(key)
3471
records = self._get_build_graph(key)
3472
if key in self._ghosts:
3473
raise errors.RevisionNotPresent(key, self._knit)
3474
self._annotate_records(records)
3475
return self._annotated_lines[key]
3476
except errors.RetryWithNewPacks, e:
3477
self._knit._access.reload_or_raise(e)
3478
# The cached build_details are no longer valid
3479
self._all_build_details.clear()
3481
def _simple_annotate(self, key):
3482
"""Return annotated fulltext, rediffing from the full texts.
3484
This is slow but makes no assumptions about the repository
3485
being able to produce line deltas.
3487
# TODO: this code generates a parent maps of present ancestors; it
3488
# could be split out into a separate method, and probably should use
3489
# iter_ancestry instead. -- mbp and robertc 20080704
3490
graph = _mod_graph.Graph(self._knit)
3491
head_cache = _mod_graph.FrozenHeadsCache(graph)
3492
search = graph._make_breadth_first_searcher([key])
3496
present, ghosts = search.next_with_ghosts()
3497
except StopIteration:
3499
keys.update(present)
3500
parent_map = self._knit.get_parent_map(keys)
3502
reannotate = annotate.reannotate
3503
for record in self._knit.get_record_stream(keys, 'topological', True):
3505
fulltext = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
3506
parents = parent_map[key]
3507
if parents is not None:
3508
parent_lines = [parent_cache[parent] for parent in parent_map[key]]
3511
parent_cache[key] = list(
3512
reannotate(parent_lines, fulltext, key, None, head_cache))
3514
return parent_cache[key]
3516
raise errors.RevisionNotPresent(key, self._knit)
3520
from bzrlib._knit_load_data_c import _load_data_c as _load_data
3522
from bzrlib._knit_load_data_py import _load_data_py as _load_data