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__ == 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)
1300
trace.mutter('Collapsed %d keys into %d requests w/ %d file_ids'
1301
' w/ sizes: %s', total_keys, len(result),
1302
len(prefix_split_keys), sizes)
1305
def get_record_stream(self, keys, ordering, include_delta_closure):
1306
"""Get a stream of records for keys.
1308
:param keys: The keys to include.
1309
:param ordering: Either 'unordered' or 'topological'. A topologically
1310
sorted stream has compression parents strictly before their
1312
:param include_delta_closure: If True then the closure across any
1313
compression parents will be included (in the opaque data).
1314
:return: An iterator of ContentFactory objects, each of which is only
1315
valid until the iterator is advanced.
1317
# keys might be a generator
1321
if not self._index.has_graph:
1322
# Cannot topological order when no graph has been stored.
1323
ordering = 'unordered'
1325
remaining_keys = keys
1328
keys = set(remaining_keys)
1329
for content_factory in self._get_remaining_record_stream(keys,
1330
ordering, include_delta_closure):
1331
remaining_keys.discard(content_factory.key)
1332
yield content_factory
1334
except errors.RetryWithNewPacks, e:
1335
self._access.reload_or_raise(e)
1337
def _get_remaining_record_stream(self, keys, ordering,
1338
include_delta_closure):
1339
"""This function is the 'retry' portion for get_record_stream."""
1340
if include_delta_closure:
1341
positions = self._get_components_positions(keys, allow_missing=True)
1343
build_details = self._index.get_build_details(keys)
1345
# (record_details, access_memo, compression_parent_key)
1346
positions = dict((key, self._build_details_to_components(details))
1347
for key, details in build_details.iteritems())
1348
absent_keys = keys.difference(set(positions))
1349
# There may be more absent keys : if we're missing the basis component
1350
# and are trying to include the delta closure.
1351
# XXX: We should not ever need to examine remote sources because we do
1352
# not permit deltas across versioned files boundaries.
1353
if include_delta_closure:
1354
needed_from_fallback = set()
1355
# Build up reconstructable_keys dict. key:True in this dict means
1356
# the key can be reconstructed.
1357
reconstructable_keys = {}
1361
chain = [key, positions[key][2]]
1363
needed_from_fallback.add(key)
1366
while chain[-1] is not None:
1367
if chain[-1] in reconstructable_keys:
1368
result = reconstructable_keys[chain[-1]]
1372
chain.append(positions[chain[-1]][2])
1374
# missing basis component
1375
needed_from_fallback.add(chain[-1])
1378
for chain_key in chain[:-1]:
1379
reconstructable_keys[chain_key] = result
1381
needed_from_fallback.add(key)
1382
# Double index lookups here : need a unified api ?
1383
global_map, parent_maps = self._get_parent_map_with_sources(keys)
1384
if ordering == 'topological':
1385
# Global topological sort
1386
present_keys = tsort.topo_sort(global_map)
1387
# Now group by source:
1389
current_source = None
1390
for key in present_keys:
1391
for parent_map in parent_maps:
1392
if key in parent_map:
1393
key_source = parent_map
1395
if current_source is not key_source:
1396
source_keys.append((key_source, []))
1397
current_source = key_source
1398
source_keys[-1][1].append(key)
1400
if ordering != 'unordered':
1401
raise AssertionError('valid values for ordering are:'
1402
' "unordered" or "topological" not: %r'
1404
# Just group by source; remote sources first.
1407
for parent_map in reversed(parent_maps):
1408
source_keys.append((parent_map, []))
1409
for key in parent_map:
1410
present_keys.append(key)
1411
source_keys[-1][1].append(key)
1412
# We have been requested to return these records in an order that
1413
# suits us. So we ask the index to give us an optimally sorted
1415
for source, sub_keys in source_keys:
1416
if source is parent_maps[0]:
1417
# Only sort the keys for this VF
1418
self._index._sort_keys_by_io(sub_keys, positions)
1419
absent_keys = keys - set(global_map)
1420
for key in absent_keys:
1421
yield AbsentContentFactory(key)
1422
# restrict our view to the keys we can answer.
1423
# XXX: Memory: TODO: batch data here to cap buffered data at (say) 1MB.
1424
# XXX: At that point we need to consider the impact of double reads by
1425
# utilising components multiple times.
1426
if include_delta_closure:
1427
# XXX: get_content_maps performs its own index queries; allow state
1429
non_local_keys = needed_from_fallback - absent_keys
1430
for keys, non_local_keys in self._group_keys_for_io(present_keys,
1433
generator = _VFContentMapGenerator(self, keys, non_local_keys,
1435
for record in generator.get_record_stream():
1438
for source, keys in source_keys:
1439
if source is parent_maps[0]:
1440
# this KnitVersionedFiles
1441
records = [(key, positions[key][1]) for key in keys]
1442
for key, raw_data, sha1 in self._read_records_iter_raw(records):
1443
(record_details, index_memo, _) = positions[key]
1444
yield KnitContentFactory(key, global_map[key],
1445
record_details, sha1, raw_data, self._factory.annotated, None)
1447
vf = self._fallback_vfs[parent_maps.index(source) - 1]
1448
for record in vf.get_record_stream(keys, ordering,
1449
include_delta_closure):
1452
def get_sha1s(self, keys):
1453
"""See VersionedFiles.get_sha1s()."""
1455
record_map = self._get_record_map(missing, allow_missing=True)
1457
for key, details in record_map.iteritems():
1458
if key not in missing:
1460
# record entry 2 is the 'digest'.
1461
result[key] = details[2]
1462
missing.difference_update(set(result))
1463
for source in self._fallback_vfs:
1466
new_result = source.get_sha1s(missing)
1467
result.update(new_result)
1468
missing.difference_update(set(new_result))
1471
def insert_record_stream(self, stream):
1472
"""Insert a record stream into this container.
1474
:param stream: A stream of records to insert.
1476
:seealso VersionedFiles.get_record_stream:
1478
def get_adapter(adapter_key):
1480
return adapters[adapter_key]
1482
adapter_factory = adapter_registry.get(adapter_key)
1483
adapter = adapter_factory(self)
1484
adapters[adapter_key] = adapter
1487
if self._factory.annotated:
1488
# self is annotated, we need annotated knits to use directly.
1489
annotated = "annotated-"
1492
# self is not annotated, but we can strip annotations cheaply.
1494
convertibles = set(["knit-annotated-ft-gz"])
1495
if self._max_delta_chain:
1496
delta_types.add("knit-annotated-delta-gz")
1497
convertibles.add("knit-annotated-delta-gz")
1498
# The set of types we can cheaply adapt without needing basis texts.
1499
native_types = set()
1500
if self._max_delta_chain:
1501
native_types.add("knit-%sdelta-gz" % annotated)
1502
delta_types.add("knit-%sdelta-gz" % annotated)
1503
native_types.add("knit-%sft-gz" % annotated)
1504
knit_types = native_types.union(convertibles)
1506
# Buffer all index entries that we can't add immediately because their
1507
# basis parent is missing. We don't buffer all because generating
1508
# annotations may require access to some of the new records. However we
1509
# can't generate annotations from new deltas until their basis parent
1510
# is present anyway, so we get away with not needing an index that
1511
# includes the new keys.
1513
# See <http://launchpad.net/bugs/300177> about ordering of compression
1514
# parents in the records - to be conservative, we insist that all
1515
# parents must be present to avoid expanding to a fulltext.
1517
# key = basis_parent, value = index entry to add
1518
buffered_index_entries = {}
1519
for record in stream:
1521
parents = record.parents
1522
if record.storage_kind in delta_types:
1523
# TODO: eventually the record itself should track
1524
# compression_parent
1525
compression_parent = parents[0]
1527
compression_parent = None
1528
# Raise an error when a record is missing.
1529
if record.storage_kind == 'absent':
1530
raise RevisionNotPresent([record.key], self)
1531
elif ((record.storage_kind in knit_types)
1532
and (compression_parent is None
1533
or not self._fallback_vfs
1534
or self._index.has_key(compression_parent)
1535
or not self.has_key(compression_parent))):
1536
# we can insert the knit record literally if either it has no
1537
# compression parent OR we already have its basis in this kvf
1538
# OR the basis is not present even in the fallbacks. In the
1539
# last case it will either turn up later in the stream and all
1540
# will be well, or it won't turn up at all and we'll raise an
1543
# TODO: self.has_key is somewhat redundant with
1544
# self._index.has_key; we really want something that directly
1545
# asks if it's only present in the fallbacks. -- mbp 20081119
1546
if record.storage_kind not in native_types:
1548
adapter_key = (record.storage_kind, "knit-delta-gz")
1549
adapter = get_adapter(adapter_key)
1551
adapter_key = (record.storage_kind, "knit-ft-gz")
1552
adapter = get_adapter(adapter_key)
1553
bytes = adapter.get_bytes(record)
1555
# It's a knit record, it has a _raw_record field (even if
1556
# it was reconstituted from a network stream).
1557
bytes = record._raw_record
1558
options = [record._build_details[0]]
1559
if record._build_details[1]:
1560
options.append('no-eol')
1561
# Just blat it across.
1562
# Note: This does end up adding data on duplicate keys. As
1563
# modern repositories use atomic insertions this should not
1564
# lead to excessive growth in the event of interrupted fetches.
1565
# 'knit' repositories may suffer excessive growth, but as a
1566
# deprecated format this is tolerable. It can be fixed if
1567
# needed by in the kndx index support raising on a duplicate
1568
# add with identical parents and options.
1569
access_memo = self._access.add_raw_records(
1570
[(record.key, len(bytes))], bytes)[0]
1571
index_entry = (record.key, options, access_memo, parents)
1572
if 'fulltext' not in options:
1573
# Not a fulltext, so we need to make sure the compression
1574
# parent will also be present.
1575
# Note that pack backed knits don't need to buffer here
1576
# because they buffer all writes to the transaction level,
1577
# but we don't expose that difference at the index level. If
1578
# the query here has sufficient cost to show up in
1579
# profiling we should do that.
1581
# They're required to be physically in this
1582
# KnitVersionedFiles, not in a fallback.
1583
if not self._index.has_key(compression_parent):
1584
pending = buffered_index_entries.setdefault(
1585
compression_parent, [])
1586
pending.append(index_entry)
1589
self._index.add_records([index_entry])
1590
elif record.storage_kind == 'chunked':
1591
self.add_lines(record.key, parents,
1592
osutils.chunks_to_lines(record.get_bytes_as('chunked')))
1594
# Not suitable for direct insertion as a
1595
# delta, either because it's not the right format, or this
1596
# KnitVersionedFiles doesn't permit deltas (_max_delta_chain ==
1597
# 0) or because it depends on a base only present in the
1600
# Try getting a fulltext directly from the record.
1601
bytes = record.get_bytes_as('fulltext')
1602
except errors.UnavailableRepresentation:
1603
adapter_key = record.storage_kind, 'fulltext'
1604
adapter = get_adapter(adapter_key)
1605
bytes = adapter.get_bytes(record)
1606
lines = split_lines(bytes)
1608
self.add_lines(record.key, parents, lines)
1609
except errors.RevisionAlreadyPresent:
1611
# Add any records whose basis parent is now available.
1613
added_keys = [record.key]
1615
key = added_keys.pop(0)
1616
if key in buffered_index_entries:
1617
index_entries = buffered_index_entries[key]
1618
self._index.add_records(index_entries)
1620
[index_entry[0] for index_entry in index_entries])
1621
del buffered_index_entries[key]
1622
if buffered_index_entries:
1623
# There were index entries buffered at the end of the stream,
1624
# So these need to be added (if the index supports holding such
1625
# entries for later insertion)
1626
for key in buffered_index_entries:
1627
index_entries = buffered_index_entries[key]
1628
self._index.add_records(index_entries,
1629
missing_compression_parents=True)
1631
def get_missing_compression_parent_keys(self):
1632
"""Return an iterable of keys of missing compression parents.
1634
Check this after calling insert_record_stream to find out if there are
1635
any missing compression parents. If there are, the records that
1636
depend on them are not able to be inserted safely. For atomic
1637
KnitVersionedFiles built on packs, the transaction should be aborted or
1638
suspended - commit will fail at this point. Nonatomic knits will error
1639
earlier because they have no staging area to put pending entries into.
1641
return self._index.get_missing_compression_parents()
1643
def iter_lines_added_or_present_in_keys(self, keys, pb=None):
1644
"""Iterate over the lines in the versioned files from keys.
1646
This may return lines from other keys. Each item the returned
1647
iterator yields is a tuple of a line and a text version that that line
1648
is present in (not introduced in).
1650
Ordering of results is in whatever order is most suitable for the
1651
underlying storage format.
1653
If a progress bar is supplied, it may be used to indicate progress.
1654
The caller is responsible for cleaning up progress bars (because this
1658
* Lines are normalised by the underlying store: they will all have \\n
1660
* Lines are returned in arbitrary order.
1661
* If a requested key did not change any lines (or didn't have any
1662
lines), it may not be mentioned at all in the result.
1664
:return: An iterator over (line, key).
1667
pb = progress.DummyProgress()
1673
# we don't care about inclusions, the caller cares.
1674
# but we need to setup a list of records to visit.
1675
# we need key, position, length
1677
build_details = self._index.get_build_details(keys)
1678
for key, details in build_details.iteritems():
1680
key_records.append((key, details[0]))
1681
records_iter = enumerate(self._read_records_iter(key_records))
1682
for (key_idx, (key, data, sha_value)) in records_iter:
1683
pb.update('Walking content.', key_idx, total)
1684
compression_parent = build_details[key][1]
1685
if compression_parent is None:
1687
line_iterator = self._factory.get_fulltext_content(data)
1690
line_iterator = self._factory.get_linedelta_content(data)
1691
# Now that we are yielding the data for this key, remove it
1694
# XXX: It might be more efficient to yield (key,
1695
# line_iterator) in the future. However for now, this is a
1696
# simpler change to integrate into the rest of the
1697
# codebase. RBC 20071110
1698
for line in line_iterator:
1701
except errors.RetryWithNewPacks, e:
1702
self._access.reload_or_raise(e)
1703
# If there are still keys we've not yet found, we look in the fallback
1704
# vfs, and hope to find them there. Note that if the keys are found
1705
# but had no changes or no content, the fallback may not return
1707
if keys and not self._fallback_vfs:
1708
# XXX: strictly the second parameter is meant to be the file id
1709
# but it's not easily accessible here.
1710
raise RevisionNotPresent(keys, repr(self))
1711
for source in self._fallback_vfs:
1715
for line, key in source.iter_lines_added_or_present_in_keys(keys):
1716
source_keys.add(key)
1718
keys.difference_update(source_keys)
1719
pb.update('Walking content.', total, total)
1721
def _make_line_delta(self, delta_seq, new_content):
1722
"""Generate a line delta from delta_seq and new_content."""
1724
for op in delta_seq.get_opcodes():
1725
if op[0] == 'equal':
1727
diff_hunks.append((op[1], op[2], op[4]-op[3], new_content._lines[op[3]:op[4]]))
1730
def _merge_annotations(self, content, parents, parent_texts={},
1731
delta=None, annotated=None,
1732
left_matching_blocks=None):
1733
"""Merge annotations for content and generate deltas.
1735
This is done by comparing the annotations based on changes to the text
1736
and generating a delta on the resulting full texts. If annotations are
1737
not being created then a simple delta is created.
1739
if left_matching_blocks is not None:
1740
delta_seq = diff._PrematchedMatcher(left_matching_blocks)
1744
for parent_key in parents:
1745
merge_content = self._get_content(parent_key, parent_texts)
1746
if (parent_key == parents[0] and delta_seq is not None):
1749
seq = patiencediff.PatienceSequenceMatcher(
1750
None, merge_content.text(), content.text())
1751
for i, j, n in seq.get_matching_blocks():
1754
# this copies (origin, text) pairs across to the new
1755
# content for any line that matches the last-checked
1757
content._lines[j:j+n] = merge_content._lines[i:i+n]
1758
# XXX: Robert says the following block is a workaround for a
1759
# now-fixed bug and it can probably be deleted. -- mbp 20080618
1760
if content._lines and content._lines[-1][1][-1] != '\n':
1761
# The copied annotation was from a line without a trailing EOL,
1762
# reinstate one for the content object, to ensure correct
1764
line = content._lines[-1][1] + '\n'
1765
content._lines[-1] = (content._lines[-1][0], line)
1767
if delta_seq is None:
1768
reference_content = self._get_content(parents[0], parent_texts)
1769
new_texts = content.text()
1770
old_texts = reference_content.text()
1771
delta_seq = patiencediff.PatienceSequenceMatcher(
1772
None, old_texts, new_texts)
1773
return self._make_line_delta(delta_seq, content)
1775
def _parse_record(self, version_id, data):
1776
"""Parse an original format knit record.
1778
These have the last element of the key only present in the stored data.
1780
rec, record_contents = self._parse_record_unchecked(data)
1781
self._check_header_version(rec, version_id)
1782
return record_contents, rec[3]
1784
def _parse_record_header(self, key, raw_data):
1785
"""Parse a record header for consistency.
1787
:return: the header and the decompressor stream.
1788
as (stream, header_record)
1790
df = tuned_gzip.GzipFile(mode='rb', fileobj=StringIO(raw_data))
1793
rec = self._check_header(key, df.readline())
1794
except Exception, e:
1795
raise KnitCorrupt(self,
1796
"While reading {%s} got %s(%s)"
1797
% (key, e.__class__.__name__, str(e)))
1800
def _parse_record_unchecked(self, data):
1802
# 4168 calls in 2880 217 internal
1803
# 4168 calls to _parse_record_header in 2121
1804
# 4168 calls to readlines in 330
1805
df = tuned_gzip.GzipFile(mode='rb', fileobj=StringIO(data))
1807
record_contents = df.readlines()
1808
except Exception, e:
1809
raise KnitCorrupt(self, "Corrupt compressed record %r, got %s(%s)" %
1810
(data, e.__class__.__name__, str(e)))
1811
header = record_contents.pop(0)
1812
rec = self._split_header(header)
1813
last_line = record_contents.pop()
1814
if len(record_contents) != int(rec[2]):
1815
raise KnitCorrupt(self,
1816
'incorrect number of lines %s != %s'
1817
' for version {%s} %s'
1818
% (len(record_contents), int(rec[2]),
1819
rec[1], record_contents))
1820
if last_line != 'end %s\n' % rec[1]:
1821
raise KnitCorrupt(self,
1822
'unexpected version end line %r, wanted %r'
1823
% (last_line, rec[1]))
1825
return rec, record_contents
1827
def _read_records_iter(self, records):
1828
"""Read text records from data file and yield result.
1830
The result will be returned in whatever is the fastest to read.
1831
Not by the order requested. Also, multiple requests for the same
1832
record will only yield 1 response.
1833
:param records: A list of (key, access_memo) entries
1834
:return: Yields (key, contents, digest) in the order
1835
read, not the order requested
1840
# XXX: This smells wrong, IO may not be getting ordered right.
1841
needed_records = sorted(set(records), key=operator.itemgetter(1))
1842
if not needed_records:
1845
# The transport optimizes the fetching as well
1846
# (ie, reads continuous ranges.)
1847
raw_data = self._access.get_raw_records(
1848
[index_memo for key, index_memo in needed_records])
1850
for (key, index_memo), data in \
1851
izip(iter(needed_records), raw_data):
1852
content, digest = self._parse_record(key[-1], data)
1853
yield key, content, digest
1855
def _read_records_iter_raw(self, records):
1856
"""Read text records from data file and yield raw data.
1858
This unpacks enough of the text record to validate the id is
1859
as expected but thats all.
1861
Each item the iterator yields is (key, bytes,
1862
expected_sha1_of_full_text).
1864
for key, data in self._read_records_iter_unchecked(records):
1865
# validate the header (note that we can only use the suffix in
1866
# current knit records).
1867
df, rec = self._parse_record_header(key, data)
1869
yield key, data, rec[3]
1871
def _read_records_iter_unchecked(self, records):
1872
"""Read text records from data file and yield raw data.
1874
No validation is done.
1876
Yields tuples of (key, data).
1878
# setup an iterator of the external records:
1879
# uses readv so nice and fast we hope.
1881
# grab the disk data needed.
1882
needed_offsets = [index_memo for key, index_memo
1884
raw_records = self._access.get_raw_records(needed_offsets)
1886
for key, index_memo in records:
1887
data = raw_records.next()
1890
def _record_to_data(self, key, digest, lines, dense_lines=None):
1891
"""Convert key, digest, lines into a raw data block.
1893
:param key: The key of the record. Currently keys are always serialised
1894
using just the trailing component.
1895
:param dense_lines: The bytes of lines but in a denser form. For
1896
instance, if lines is a list of 1000 bytestrings each ending in \n,
1897
dense_lines may be a list with one line in it, containing all the
1898
1000's lines and their \n's. Using dense_lines if it is already
1899
known is a win because the string join to create bytes in this
1900
function spends less time resizing the final string.
1901
:return: (len, a StringIO instance with the raw data ready to read.)
1903
# Note: using a string copy here increases memory pressure with e.g.
1904
# ISO's, but it is about 3 seconds faster on a 1.2Ghz intel machine
1905
# when doing the initial commit of a mozilla tree. RBC 20070921
1906
bytes = ''.join(chain(
1907
["version %s %d %s\n" % (key[-1],
1910
dense_lines or lines,
1911
["end %s\n" % key[-1]]))
1912
if type(bytes) != str:
1913
raise AssertionError(
1914
'data must be plain bytes was %s' % type(bytes))
1915
if lines and lines[-1][-1] != '\n':
1916
raise ValueError('corrupt lines value %r' % lines)
1917
compressed_bytes = tuned_gzip.bytes_to_gzip(bytes)
1918
return len(compressed_bytes), compressed_bytes
1920
def _split_header(self, line):
1923
raise KnitCorrupt(self,
1924
'unexpected number of elements in record header')
1928
"""See VersionedFiles.keys."""
1929
if 'evil' in debug.debug_flags:
1930
trace.mutter_callsite(2, "keys scales with size of history")
1931
sources = [self._index] + self._fallback_vfs
1933
for source in sources:
1934
result.update(source.keys())
1938
class _ContentMapGenerator(object):
1939
"""Generate texts or expose raw deltas for a set of texts."""
1941
def _get_content(self, key):
1942
"""Get the content object for key."""
1943
# Note that _get_content is only called when the _ContentMapGenerator
1944
# has been constructed with just one key requested for reconstruction.
1945
if key in self.nonlocal_keys:
1946
record = self.get_record_stream().next()
1947
# Create a content object on the fly
1948
lines = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
1949
return PlainKnitContent(lines, record.key)
1951
# local keys we can ask for directly
1952
return self._get_one_work(key)
1954
def get_record_stream(self):
1955
"""Get a record stream for the keys requested during __init__."""
1956
for record in self._work():
1960
"""Produce maps of text and KnitContents as dicts.
1962
:return: (text_map, content_map) where text_map contains the texts for
1963
the requested versions and content_map contains the KnitContents.
1965
# NB: By definition we never need to read remote sources unless texts
1966
# are requested from them: we don't delta across stores - and we
1967
# explicitly do not want to to prevent data loss situations.
1968
if self.global_map is None:
1969
self.global_map = self.vf.get_parent_map(self.keys)
1970
nonlocal_keys = self.nonlocal_keys
1972
missing_keys = set(nonlocal_keys)
1973
# Read from remote versioned file instances and provide to our caller.
1974
for source in self.vf._fallback_vfs:
1975
if not missing_keys:
1977
# Loop over fallback repositories asking them for texts - ignore
1978
# any missing from a particular fallback.
1979
for record in source.get_record_stream(missing_keys,
1981
if record.storage_kind == 'absent':
1982
# Not in thie particular stream, may be in one of the
1983
# other fallback vfs objects.
1985
missing_keys.remove(record.key)
1988
self._raw_record_map = self.vf._get_record_map_unparsed(self.keys,
1991
for key in self.keys:
1992
if key in self.nonlocal_keys:
1994
yield LazyKnitContentFactory(key, self.global_map[key], self, first)
1997
def _get_one_work(self, requested_key):
1998
# Now, if we have calculated everything already, just return the
2000
if requested_key in self._contents_map:
2001
return self._contents_map[requested_key]
2002
# To simplify things, parse everything at once - code that wants one text
2003
# probably wants them all.
2004
# FUTURE: This function could be improved for the 'extract many' case
2005
# by tracking each component and only doing the copy when the number of
2006
# children than need to apply delta's to it is > 1 or it is part of the
2008
multiple_versions = len(self.keys) != 1
2009
if self._record_map is None:
2010
self._record_map = self.vf._raw_map_to_record_map(
2011
self._raw_record_map)
2012
record_map = self._record_map
2013
# raw_record_map is key:
2014
# Have read and parsed records at this point.
2015
for key in self.keys:
2016
if key in self.nonlocal_keys:
2021
while cursor is not None:
2023
record, record_details, digest, next = record_map[cursor]
2025
raise RevisionNotPresent(cursor, self)
2026
components.append((cursor, record, record_details, digest))
2028
if cursor in self._contents_map:
2029
# no need to plan further back
2030
components.append((cursor, None, None, None))
2034
for (component_id, record, record_details,
2035
digest) in reversed(components):
2036
if component_id in self._contents_map:
2037
content = self._contents_map[component_id]
2039
content, delta = self._factory.parse_record(key[-1],
2040
record, record_details, content,
2041
copy_base_content=multiple_versions)
2042
if multiple_versions:
2043
self._contents_map[component_id] = content
2045
# digest here is the digest from the last applied component.
2046
text = content.text()
2047
actual_sha = sha_strings(text)
2048
if actual_sha != digest:
2049
raise SHA1KnitCorrupt(self, actual_sha, digest, key, text)
2050
if multiple_versions:
2051
return self._contents_map[requested_key]
2055
def _wire_bytes(self):
2056
"""Get the bytes to put on the wire for 'key'.
2058
The first collection of bytes asked for returns the serialised
2059
raw_record_map and the additional details (key, parent) for key.
2060
Subsequent calls return just the additional details (key, parent).
2061
The wire storage_kind given for the first key is 'knit-delta-closure',
2062
For subsequent keys it is 'knit-delta-closure-ref'.
2064
:param key: A key from the content generator.
2065
:return: Bytes to put on the wire.
2068
# kind marker for dispatch on the far side,
2069
lines.append('knit-delta-closure')
2071
if self.vf._factory.annotated:
2072
lines.append('annotated')
2075
# then the list of keys
2076
lines.append('\t'.join(['\x00'.join(key) for key in self.keys
2077
if key not in self.nonlocal_keys]))
2078
# then the _raw_record_map in serialised form:
2080
# for each item in the map:
2082
# 1 line with parents if the key is to be yielded (None: for None, '' for ())
2083
# one line with method
2084
# one line with noeol
2085
# one line with next ('' for None)
2086
# one line with byte count of the record bytes
2088
for key, (record_bytes, (method, noeol), next) in \
2089
self._raw_record_map.iteritems():
2090
key_bytes = '\x00'.join(key)
2091
parents = self.global_map.get(key, None)
2093
parent_bytes = 'None:'
2095
parent_bytes = '\t'.join('\x00'.join(key) for key in parents)
2096
method_bytes = method
2102
next_bytes = '\x00'.join(next)
2105
map_byte_list.append('%s\n%s\n%s\n%s\n%s\n%d\n%s' % (
2106
key_bytes, parent_bytes, method_bytes, noeol_bytes, next_bytes,
2107
len(record_bytes), record_bytes))
2108
map_bytes = ''.join(map_byte_list)
2109
lines.append(map_bytes)
2110
bytes = '\n'.join(lines)
2114
class _VFContentMapGenerator(_ContentMapGenerator):
2115
"""Content map generator reading from a VersionedFiles object."""
2117
def __init__(self, versioned_files, keys, nonlocal_keys=None,
2118
global_map=None, raw_record_map=None):
2119
"""Create a _ContentMapGenerator.
2121
:param versioned_files: The versioned files that the texts are being
2123
:param keys: The keys to produce content maps for.
2124
:param nonlocal_keys: An iterable of keys(possibly intersecting keys)
2125
which are known to not be in this knit, but rather in one of the
2127
:param global_map: The result of get_parent_map(keys) (or a supermap).
2128
This is required if get_record_stream() is to be used.
2129
:param raw_record_map: A unparsed raw record map to use for answering
2132
# The vf to source data from
2133
self.vf = versioned_files
2135
self.keys = list(keys)
2136
# Keys known to be in fallback vfs objects
2137
if nonlocal_keys is None:
2138
self.nonlocal_keys = set()
2140
self.nonlocal_keys = frozenset(nonlocal_keys)
2141
# Parents data for keys to be returned in get_record_stream
2142
self.global_map = global_map
2143
# The chunked lists for self.keys in text form
2145
# A cache of KnitContent objects used in extracting texts.
2146
self._contents_map = {}
2147
# All the knit records needed to assemble the requested keys as full
2149
self._record_map = None
2150
if raw_record_map is None:
2151
self._raw_record_map = self.vf._get_record_map_unparsed(keys,
2154
self._raw_record_map = raw_record_map
2155
# the factory for parsing records
2156
self._factory = self.vf._factory
2159
class _NetworkContentMapGenerator(_ContentMapGenerator):
2160
"""Content map generator sourced from a network stream."""
2162
def __init__(self, bytes, line_end):
2163
"""Construct a _NetworkContentMapGenerator from a bytes block."""
2165
self.global_map = {}
2166
self._raw_record_map = {}
2167
self._contents_map = {}
2168
self._record_map = None
2169
self.nonlocal_keys = []
2170
# Get access to record parsing facilities
2171
self.vf = KnitVersionedFiles(None, None)
2174
line_end = bytes.find('\n', start)
2175
line = bytes[start:line_end]
2176
start = line_end + 1
2177
if line == 'annotated':
2178
self._factory = KnitAnnotateFactory()
2180
self._factory = KnitPlainFactory()
2181
# list of keys to emit in get_record_stream
2182
line_end = bytes.find('\n', start)
2183
line = bytes[start:line_end]
2184
start = line_end + 1
2186
tuple(segment.split('\x00')) for segment in line.split('\t')
2188
# now a loop until the end. XXX: It would be nice if this was just a
2189
# bunch of the same records as get_record_stream(..., False) gives, but
2190
# there is a decent sized gap stopping that at the moment.
2194
line_end = bytes.find('\n', start)
2195
key = tuple(bytes[start:line_end].split('\x00'))
2196
start = line_end + 1
2197
# 1 line with parents (None: for None, '' for ())
2198
line_end = bytes.find('\n', start)
2199
line = bytes[start:line_end]
2204
[tuple(segment.split('\x00')) for segment in line.split('\t')
2206
self.global_map[key] = parents
2207
start = line_end + 1
2208
# one line with method
2209
line_end = bytes.find('\n', start)
2210
line = bytes[start:line_end]
2212
start = line_end + 1
2213
# one line with noeol
2214
line_end = bytes.find('\n', start)
2215
line = bytes[start:line_end]
2217
start = line_end + 1
2218
# one line with next ('' for None)
2219
line_end = bytes.find('\n', start)
2220
line = bytes[start:line_end]
2224
next = tuple(bytes[start:line_end].split('\x00'))
2225
start = line_end + 1
2226
# one line with byte count of the record bytes
2227
line_end = bytes.find('\n', start)
2228
line = bytes[start:line_end]
2230
start = line_end + 1
2232
record_bytes = bytes[start:start+count]
2233
start = start + count
2235
self._raw_record_map[key] = (record_bytes, (method, noeol), next)
2237
def get_record_stream(self):
2238
"""Get a record stream for for keys requested by the bytestream."""
2240
for key in self.keys:
2241
yield LazyKnitContentFactory(key, self.global_map[key], self, first)
2244
def _wire_bytes(self):
2248
class _KndxIndex(object):
2249
"""Manages knit index files
2251
The index is kept in memory and read on startup, to enable
2252
fast lookups of revision information. The cursor of the index
2253
file is always pointing to the end, making it easy to append
2256
_cache is a cache for fast mapping from version id to a Index
2259
_history is a cache for fast mapping from indexes to version ids.
2261
The index data format is dictionary compressed when it comes to
2262
parent references; a index entry may only have parents that with a
2263
lover index number. As a result, the index is topological sorted.
2265
Duplicate entries may be written to the index for a single version id
2266
if this is done then the latter one completely replaces the former:
2267
this allows updates to correct version and parent information.
2268
Note that the two entries may share the delta, and that successive
2269
annotations and references MUST point to the first entry.
2271
The index file on disc contains a header, followed by one line per knit
2272
record. The same revision can be present in an index file more than once.
2273
The first occurrence gets assigned a sequence number starting from 0.
2275
The format of a single line is
2276
REVISION_ID FLAGS BYTE_OFFSET LENGTH( PARENT_ID|PARENT_SEQUENCE_ID)* :\n
2277
REVISION_ID is a utf8-encoded revision id
2278
FLAGS is a comma separated list of flags about the record. Values include
2279
no-eol, line-delta, fulltext.
2280
BYTE_OFFSET is the ascii representation of the byte offset in the data file
2281
that the the compressed data starts at.
2282
LENGTH is the ascii representation of the length of the data file.
2283
PARENT_ID a utf-8 revision id prefixed by a '.' that is a parent of
2285
PARENT_SEQUENCE_ID the ascii representation of the sequence number of a
2286
revision id already in the knit that is a parent of REVISION_ID.
2287
The ' :' marker is the end of record marker.
2290
when a write is interrupted to the index file, it will result in a line
2291
that does not end in ' :'. If the ' :' is not present at the end of a line,
2292
or at the end of the file, then the record that is missing it will be
2293
ignored by the parser.
2295
When writing new records to the index file, the data is preceded by '\n'
2296
to ensure that records always start on new lines even if the last write was
2297
interrupted. As a result its normal for the last line in the index to be
2298
missing a trailing newline. One can be added with no harmful effects.
2300
:ivar _kndx_cache: dict from prefix to the old state of KnitIndex objects,
2301
where prefix is e.g. the (fileid,) for .texts instances or () for
2302
constant-mapped things like .revisions, and the old state is
2303
tuple(history_vector, cache_dict). This is used to prevent having an
2304
ABI change with the C extension that reads .kndx files.
2307
HEADER = "# bzr knit index 8\n"
2309
def __init__(self, transport, mapper, get_scope, allow_writes, is_locked):
2310
"""Create a _KndxIndex on transport using mapper."""
2311
self._transport = transport
2312
self._mapper = mapper
2313
self._get_scope = get_scope
2314
self._allow_writes = allow_writes
2315
self._is_locked = is_locked
2317
self.has_graph = True
2319
def add_records(self, records, random_id=False, missing_compression_parents=False):
2320
"""Add multiple records to the index.
2322
:param records: a list of tuples:
2323
(key, options, access_memo, parents).
2324
:param random_id: If True the ids being added were randomly generated
2325
and no check for existence will be performed.
2326
:param missing_compression_parents: If True the records being added are
2327
only compressed against texts already in the index (or inside
2328
records). If False the records all refer to unavailable texts (or
2329
texts inside records) as compression parents.
2331
if missing_compression_parents:
2332
# It might be nice to get the edge of the records. But keys isn't
2334
keys = sorted(record[0] for record in records)
2335
raise errors.RevisionNotPresent(keys, self)
2337
for record in records:
2340
path = self._mapper.map(key) + '.kndx'
2341
path_keys = paths.setdefault(path, (prefix, []))
2342
path_keys[1].append(record)
2343
for path in sorted(paths):
2344
prefix, path_keys = paths[path]
2345
self._load_prefixes([prefix])
2347
orig_history = self._kndx_cache[prefix][1][:]
2348
orig_cache = self._kndx_cache[prefix][0].copy()
2351
for key, options, (_, pos, size), parents in path_keys:
2353
# kndx indices cannot be parentless.
2355
line = "\n%s %s %s %s %s :" % (
2356
key[-1], ','.join(options), pos, size,
2357
self._dictionary_compress(parents))
2358
if type(line) != str:
2359
raise AssertionError(
2360
'data must be utf8 was %s' % type(line))
2362
self._cache_key(key, options, pos, size, parents)
2363
if len(orig_history):
2364
self._transport.append_bytes(path, ''.join(lines))
2366
self._init_index(path, lines)
2368
# If any problems happen, restore the original values and re-raise
2369
self._kndx_cache[prefix] = (orig_cache, orig_history)
2372
def scan_unvalidated_index(self, graph_index):
2373
"""See _KnitGraphIndex.scan_unvalidated_index."""
2374
# Because kndx files do not support atomic insertion via separate index
2375
# files, they do not support this method.
2376
raise NotImplementedError(self.scan_unvalidated_index)
2378
def get_missing_compression_parents(self):
2379
"""See _KnitGraphIndex.get_missing_compression_parents."""
2380
# Because kndx files do not support atomic insertion via separate index
2381
# files, they do not support this method.
2382
raise NotImplementedError(self.get_missing_compression_parents)
2384
def _cache_key(self, key, options, pos, size, parent_keys):
2385
"""Cache a version record in the history array and index cache.
2387
This is inlined into _load_data for performance. KEEP IN SYNC.
2388
(It saves 60ms, 25% of the __init__ overhead on local 4000 record
2392
version_id = key[-1]
2393
# last-element only for compatibilty with the C load_data.
2394
parents = tuple(parent[-1] for parent in parent_keys)
2395
for parent in parent_keys:
2396
if parent[:-1] != prefix:
2397
raise ValueError("mismatched prefixes for %r, %r" % (
2399
cache, history = self._kndx_cache[prefix]
2400
# only want the _history index to reference the 1st index entry
2402
if version_id not in cache:
2403
index = len(history)
2404
history.append(version_id)
2406
index = cache[version_id][5]
2407
cache[version_id] = (version_id,
2414
def check_header(self, fp):
2415
line = fp.readline()
2417
# An empty file can actually be treated as though the file doesn't
2419
raise errors.NoSuchFile(self)
2420
if line != self.HEADER:
2421
raise KnitHeaderError(badline=line, filename=self)
2423
def _check_read(self):
2424
if not self._is_locked():
2425
raise errors.ObjectNotLocked(self)
2426
if self._get_scope() != self._scope:
2429
def _check_write_ok(self):
2430
"""Assert if not writes are permitted."""
2431
if not self._is_locked():
2432
raise errors.ObjectNotLocked(self)
2433
if self._get_scope() != self._scope:
2435
if self._mode != 'w':
2436
raise errors.ReadOnlyObjectDirtiedError(self)
2438
def get_build_details(self, keys):
2439
"""Get the method, index_memo and compression parent for keys.
2441
Ghosts are omitted from the result.
2443
:param keys: An iterable of keys.
2444
:return: A dict of key:(index_memo, compression_parent, parents,
2447
opaque structure to pass to read_records to extract the raw
2450
Content that this record is built upon, may be None
2452
Logical parents of this node
2454
extra information about the content which needs to be passed to
2455
Factory.parse_record
2457
parent_map = self.get_parent_map(keys)
2460
if key not in parent_map:
2462
method = self.get_method(key)
2463
parents = parent_map[key]
2464
if method == 'fulltext':
2465
compression_parent = None
2467
compression_parent = parents[0]
2468
noeol = 'no-eol' in self.get_options(key)
2469
index_memo = self.get_position(key)
2470
result[key] = (index_memo, compression_parent,
2471
parents, (method, noeol))
2474
def get_method(self, key):
2475
"""Return compression method of specified key."""
2476
options = self.get_options(key)
2477
if 'fulltext' in options:
2479
elif 'line-delta' in options:
2482
raise errors.KnitIndexUnknownMethod(self, options)
2484
def get_options(self, key):
2485
"""Return a list representing options.
2489
prefix, suffix = self._split_key(key)
2490
self._load_prefixes([prefix])
2492
return self._kndx_cache[prefix][0][suffix][1]
2494
raise RevisionNotPresent(key, self)
2496
def get_parent_map(self, keys):
2497
"""Get a map of the parents of keys.
2499
:param keys: The keys to look up parents for.
2500
:return: A mapping from keys to parents. Absent keys are absent from
2503
# Parse what we need to up front, this potentially trades off I/O
2504
# locality (.kndx and .knit in the same block group for the same file
2505
# id) for less checking in inner loops.
2506
prefixes = set(key[:-1] for key in keys)
2507
self._load_prefixes(prefixes)
2512
suffix_parents = self._kndx_cache[prefix][0][key[-1]][4]
2516
result[key] = tuple(prefix + (suffix,) for
2517
suffix in suffix_parents)
2520
def get_position(self, key):
2521
"""Return details needed to access the version.
2523
:return: a tuple (key, data position, size) to hand to the access
2524
logic to get the record.
2526
prefix, suffix = self._split_key(key)
2527
self._load_prefixes([prefix])
2528
entry = self._kndx_cache[prefix][0][suffix]
2529
return key, entry[2], entry[3]
2531
has_key = _mod_index._has_key_from_parent_map
2533
def _init_index(self, path, extra_lines=[]):
2534
"""Initialize an index."""
2536
sio.write(self.HEADER)
2537
sio.writelines(extra_lines)
2539
self._transport.put_file_non_atomic(path, sio,
2540
create_parent_dir=True)
2541
# self._create_parent_dir)
2542
# mode=self._file_mode,
2543
# dir_mode=self._dir_mode)
2546
"""Get all the keys in the collection.
2548
The keys are not ordered.
2551
# Identify all key prefixes.
2552
# XXX: A bit hacky, needs polish.
2553
if type(self._mapper) == ConstantMapper:
2557
for quoted_relpath in self._transport.iter_files_recursive():
2558
path, ext = os.path.splitext(quoted_relpath)
2560
prefixes = [self._mapper.unmap(path) for path in relpaths]
2561
self._load_prefixes(prefixes)
2562
for prefix in prefixes:
2563
for suffix in self._kndx_cache[prefix][1]:
2564
result.add(prefix + (suffix,))
2567
def _load_prefixes(self, prefixes):
2568
"""Load the indices for prefixes."""
2570
for prefix in prefixes:
2571
if prefix not in self._kndx_cache:
2572
# the load_data interface writes to these variables.
2575
self._filename = prefix
2577
path = self._mapper.map(prefix) + '.kndx'
2578
fp = self._transport.get(path)
2580
# _load_data may raise NoSuchFile if the target knit is
2582
_load_data(self, fp)
2585
self._kndx_cache[prefix] = (self._cache, self._history)
2590
self._kndx_cache[prefix] = ({}, [])
2591
if type(self._mapper) == ConstantMapper:
2592
# preserve behaviour for revisions.kndx etc.
2593
self._init_index(path)
2598
missing_keys = _mod_index._missing_keys_from_parent_map
2600
def _partition_keys(self, keys):
2601
"""Turn keys into a dict of prefix:suffix_list."""
2604
prefix_keys = result.setdefault(key[:-1], [])
2605
prefix_keys.append(key[-1])
2608
def _dictionary_compress(self, keys):
2609
"""Dictionary compress keys.
2611
:param keys: The keys to generate references to.
2612
:return: A string representation of keys. keys which are present are
2613
dictionary compressed, and others are emitted as fulltext with a
2619
prefix = keys[0][:-1]
2620
cache = self._kndx_cache[prefix][0]
2622
if key[:-1] != prefix:
2623
# kndx indices cannot refer across partitioned storage.
2624
raise ValueError("mismatched prefixes for %r" % keys)
2625
if key[-1] in cache:
2626
# -- inlined lookup() --
2627
result_list.append(str(cache[key[-1]][5]))
2628
# -- end lookup () --
2630
result_list.append('.' + key[-1])
2631
return ' '.join(result_list)
2633
def _reset_cache(self):
2634
# Possibly this should be a LRU cache. A dictionary from key_prefix to
2635
# (cache_dict, history_vector) for parsed kndx files.
2636
self._kndx_cache = {}
2637
self._scope = self._get_scope()
2638
allow_writes = self._allow_writes()
2644
def _sort_keys_by_io(self, keys, positions):
2645
"""Figure out an optimal order to read the records for the given keys.
2647
Sort keys, grouped by index and sorted by position.
2649
:param keys: A list of keys whose records we want to read. This will be
2651
:param positions: A dict, such as the one returned by
2652
_get_components_positions()
2655
def get_sort_key(key):
2656
index_memo = positions[key][1]
2657
# Group by prefix and position. index_memo[0] is the key, so it is
2658
# (file_id, revision_id) and we don't want to sort on revision_id,
2659
# index_memo[1] is the position, and index_memo[2] is the size,
2660
# which doesn't matter for the sort
2661
return index_memo[0][:-1], index_memo[1]
2662
return keys.sort(key=get_sort_key)
2664
_get_total_build_size = _get_total_build_size
2666
def _split_key(self, key):
2667
"""Split key into a prefix and suffix."""
2668
return key[:-1], key[-1]
2671
class _KnitGraphIndex(object):
2672
"""A KnitVersionedFiles index layered on GraphIndex."""
2674
def __init__(self, graph_index, is_locked, deltas=False, parents=True,
2676
"""Construct a KnitGraphIndex on a graph_index.
2678
:param graph_index: An implementation of bzrlib.index.GraphIndex.
2679
:param is_locked: A callback to check whether the object should answer
2681
:param deltas: Allow delta-compressed records.
2682
:param parents: If True, record knits parents, if not do not record
2684
:param add_callback: If not None, allow additions to the index and call
2685
this callback with a list of added GraphIndex nodes:
2686
[(node, value, node_refs), ...]
2687
:param is_locked: A callback, returns True if the index is locked and
2690
self._add_callback = add_callback
2691
self._graph_index = graph_index
2692
self._deltas = deltas
2693
self._parents = parents
2694
if deltas and not parents:
2695
# XXX: TODO: Delta tree and parent graph should be conceptually
2697
raise KnitCorrupt(self, "Cannot do delta compression without "
2699
self.has_graph = parents
2700
self._is_locked = is_locked
2701
self._missing_compression_parents = set()
2704
return "%s(%r)" % (self.__class__.__name__, self._graph_index)
2706
def add_records(self, records, random_id=False,
2707
missing_compression_parents=False):
2708
"""Add multiple records to the index.
2710
This function does not insert data into the Immutable GraphIndex
2711
backing the KnitGraphIndex, instead it prepares data for insertion by
2712
the caller and checks that it is safe to insert then calls
2713
self._add_callback with the prepared GraphIndex nodes.
2715
:param records: a list of tuples:
2716
(key, options, access_memo, parents).
2717
:param random_id: If True the ids being added were randomly generated
2718
and no check for existence will be performed.
2719
:param missing_compression_parents: If True the records being added are
2720
only compressed against texts already in the index (or inside
2721
records). If False the records all refer to unavailable texts (or
2722
texts inside records) as compression parents.
2724
if not self._add_callback:
2725
raise errors.ReadOnlyError(self)
2726
# we hope there are no repositories with inconsistent parentage
2730
compression_parents = set()
2731
for (key, options, access_memo, parents) in records:
2733
parents = tuple(parents)
2734
index, pos, size = access_memo
2735
if 'no-eol' in options:
2739
value += "%d %d" % (pos, size)
2740
if not self._deltas:
2741
if 'line-delta' in options:
2742
raise KnitCorrupt(self, "attempt to add line-delta in non-delta knit")
2745
if 'line-delta' in options:
2746
node_refs = (parents, (parents[0],))
2747
if missing_compression_parents:
2748
compression_parents.add(parents[0])
2750
node_refs = (parents, ())
2752
node_refs = (parents, )
2755
raise KnitCorrupt(self, "attempt to add node with parents "
2756
"in parentless index.")
2758
keys[key] = (value, node_refs)
2761
present_nodes = self._get_entries(keys)
2762
for (index, key, value, node_refs) in present_nodes:
2763
if (value[0] != keys[key][0][0] or
2764
node_refs[:1] != keys[key][1][:1]):
2765
raise KnitCorrupt(self, "inconsistent details in add_records"
2766
": %s %s" % ((value, node_refs), keys[key]))
2770
for key, (value, node_refs) in keys.iteritems():
2771
result.append((key, value, node_refs))
2773
for key, (value, node_refs) in keys.iteritems():
2774
result.append((key, value))
2775
self._add_callback(result)
2776
if missing_compression_parents:
2777
# This may appear to be incorrect (it does not check for
2778
# compression parents that are in the existing graph index),
2779
# but such records won't have been buffered, so this is
2780
# actually correct: every entry when
2781
# missing_compression_parents==True either has a missing parent, or
2782
# a parent that is one of the keys in records.
2783
compression_parents.difference_update(keys)
2784
self._missing_compression_parents.update(compression_parents)
2785
# Adding records may have satisfied missing compression parents.
2786
self._missing_compression_parents.difference_update(keys)
2788
def scan_unvalidated_index(self, graph_index):
2789
"""Inform this _KnitGraphIndex that there is an unvalidated index.
2791
This allows this _KnitGraphIndex to keep track of any missing
2792
compression parents we may want to have filled in to make those
2795
:param graph_index: A GraphIndex
2798
new_missing = graph_index.external_references(ref_list_num=1)
2799
new_missing.difference_update(self.get_parent_map(new_missing))
2800
self._missing_compression_parents.update(new_missing)
2802
def get_missing_compression_parents(self):
2803
"""Return the keys of missing compression parents.
2805
Missing compression parents occur when a record stream was missing
2806
basis texts, or a index was scanned that had missing basis texts.
2808
return frozenset(self._missing_compression_parents)
2810
def _check_read(self):
2811
"""raise if reads are not permitted."""
2812
if not self._is_locked():
2813
raise errors.ObjectNotLocked(self)
2815
def _check_write_ok(self):
2816
"""Assert if writes are not permitted."""
2817
if not self._is_locked():
2818
raise errors.ObjectNotLocked(self)
2820
def _compression_parent(self, an_entry):
2821
# return the key that an_entry is compressed against, or None
2822
# Grab the second parent list (as deltas implies parents currently)
2823
compression_parents = an_entry[3][1]
2824
if not compression_parents:
2826
if len(compression_parents) != 1:
2827
raise AssertionError(
2828
"Too many compression parents: %r" % compression_parents)
2829
return compression_parents[0]
2831
def get_build_details(self, keys):
2832
"""Get the method, index_memo and compression parent for version_ids.
2834
Ghosts are omitted from the result.
2836
:param keys: An iterable of keys.
2837
:return: A dict of key:
2838
(index_memo, compression_parent, parents, record_details).
2840
opaque structure to pass to read_records to extract the raw
2843
Content that this record is built upon, may be None
2845
Logical parents of this node
2847
extra information about the content which needs to be passed to
2848
Factory.parse_record
2852
entries = self._get_entries(keys, False)
2853
for entry in entries:
2855
if not self._parents:
2858
parents = entry[3][0]
2859
if not self._deltas:
2860
compression_parent_key = None
2862
compression_parent_key = self._compression_parent(entry)
2863
noeol = (entry[2][0] == 'N')
2864
if compression_parent_key:
2865
method = 'line-delta'
2868
result[key] = (self._node_to_position(entry),
2869
compression_parent_key, parents,
2873
def _get_entries(self, keys, check_present=False):
2874
"""Get the entries for keys.
2876
:param keys: An iterable of index key tuples.
2881
for node in self._graph_index.iter_entries(keys):
2883
found_keys.add(node[1])
2885
# adapt parentless index to the rest of the code.
2886
for node in self._graph_index.iter_entries(keys):
2887
yield node[0], node[1], node[2], ()
2888
found_keys.add(node[1])
2890
missing_keys = keys.difference(found_keys)
2892
raise RevisionNotPresent(missing_keys.pop(), self)
2894
def get_method(self, key):
2895
"""Return compression method of specified key."""
2896
return self._get_method(self._get_node(key))
2898
def _get_method(self, node):
2899
if not self._deltas:
2901
if self._compression_parent(node):
2906
def _get_node(self, key):
2908
return list(self._get_entries([key]))[0]
2910
raise RevisionNotPresent(key, self)
2912
def get_options(self, key):
2913
"""Return a list representing options.
2917
node = self._get_node(key)
2918
options = [self._get_method(node)]
2919
if node[2][0] == 'N':
2920
options.append('no-eol')
2923
def get_parent_map(self, keys):
2924
"""Get a map of the parents of keys.
2926
:param keys: The keys to look up parents for.
2927
:return: A mapping from keys to parents. Absent keys are absent from
2931
nodes = self._get_entries(keys)
2935
result[node[1]] = node[3][0]
2938
result[node[1]] = None
2941
def get_position(self, key):
2942
"""Return details needed to access the version.
2944
:return: a tuple (index, data position, size) to hand to the access
2945
logic to get the record.
2947
node = self._get_node(key)
2948
return self._node_to_position(node)
2950
has_key = _mod_index._has_key_from_parent_map
2953
"""Get all the keys in the collection.
2955
The keys are not ordered.
2958
return [node[1] for node in self._graph_index.iter_all_entries()]
2960
missing_keys = _mod_index._missing_keys_from_parent_map
2962
def _node_to_position(self, node):
2963
"""Convert an index value to position details."""
2964
bits = node[2][1:].split(' ')
2965
return node[0], int(bits[0]), int(bits[1])
2967
def _sort_keys_by_io(self, keys, positions):
2968
"""Figure out an optimal order to read the records for the given keys.
2970
Sort keys, grouped by index and sorted by position.
2972
:param keys: A list of keys whose records we want to read. This will be
2974
:param positions: A dict, such as the one returned by
2975
_get_components_positions()
2978
def get_index_memo(key):
2979
# index_memo is at offset [1]. It is made up of (GraphIndex,
2980
# position, size). GI is an object, which will be unique for each
2981
# pack file. This causes us to group by pack file, then sort by
2982
# position. Size doesn't matter, but it isn't worth breaking up the
2984
return positions[key][1]
2985
return keys.sort(key=get_index_memo)
2987
_get_total_build_size = _get_total_build_size
2990
class _KnitKeyAccess(object):
2991
"""Access to records in .knit files."""
2993
def __init__(self, transport, mapper):
2994
"""Create a _KnitKeyAccess with transport and mapper.
2996
:param transport: The transport the access object is rooted at.
2997
:param mapper: The mapper used to map keys to .knit files.
2999
self._transport = transport
3000
self._mapper = mapper
3002
def add_raw_records(self, key_sizes, raw_data):
3003
"""Add raw knit bytes to a storage area.
3005
The data is spooled to the container writer in one bytes-record per
3008
:param sizes: An iterable of tuples containing the key and size of each
3010
:param raw_data: A bytestring containing the data.
3011
:return: A list of memos to retrieve the record later. Each memo is an
3012
opaque index memo. For _KnitKeyAccess the memo is (key, pos,
3013
length), where the key is the record key.
3015
if type(raw_data) != str:
3016
raise AssertionError(
3017
'data must be plain bytes was %s' % type(raw_data))
3020
# TODO: This can be tuned for writing to sftp and other servers where
3021
# append() is relatively expensive by grouping the writes to each key
3023
for key, size in key_sizes:
3024
path = self._mapper.map(key)
3026
base = self._transport.append_bytes(path + '.knit',
3027
raw_data[offset:offset+size])
3028
except errors.NoSuchFile:
3029
self._transport.mkdir(osutils.dirname(path))
3030
base = self._transport.append_bytes(path + '.knit',
3031
raw_data[offset:offset+size])
3035
result.append((key, base, size))
3038
def get_raw_records(self, memos_for_retrieval):
3039
"""Get the raw bytes for a records.
3041
:param memos_for_retrieval: An iterable containing the access memo for
3042
retrieving the bytes.
3043
:return: An iterator over the bytes of the records.
3045
# first pass, group into same-index request to minimise readv's issued.
3047
current_prefix = None
3048
for (key, offset, length) in memos_for_retrieval:
3049
if current_prefix == key[:-1]:
3050
current_list.append((offset, length))
3052
if current_prefix is not None:
3053
request_lists.append((current_prefix, current_list))
3054
current_prefix = key[:-1]
3055
current_list = [(offset, length)]
3056
# handle the last entry
3057
if current_prefix is not None:
3058
request_lists.append((current_prefix, current_list))
3059
for prefix, read_vector in request_lists:
3060
path = self._mapper.map(prefix) + '.knit'
3061
for pos, data in self._transport.readv(path, read_vector):
3065
class _DirectPackAccess(object):
3066
"""Access to data in one or more packs with less translation."""
3068
def __init__(self, index_to_packs, reload_func=None):
3069
"""Create a _DirectPackAccess object.
3071
:param index_to_packs: A dict mapping index objects to the transport
3072
and file names for obtaining data.
3073
:param reload_func: A function to call if we determine that the pack
3074
files have moved and we need to reload our caches. See
3075
bzrlib.repo_fmt.pack_repo.AggregateIndex for more details.
3077
self._container_writer = None
3078
self._write_index = None
3079
self._indices = index_to_packs
3080
self._reload_func = reload_func
3082
def add_raw_records(self, key_sizes, raw_data):
3083
"""Add raw knit bytes to a storage area.
3085
The data is spooled to the container writer in one bytes-record per
3088
:param sizes: An iterable of tuples containing the key and size of each
3090
:param raw_data: A bytestring containing the data.
3091
:return: A list of memos to retrieve the record later. Each memo is an
3092
opaque index memo. For _DirectPackAccess the memo is (index, pos,
3093
length), where the index field is the write_index object supplied
3094
to the PackAccess object.
3096
if type(raw_data) != str:
3097
raise AssertionError(
3098
'data must be plain bytes was %s' % type(raw_data))
3101
for key, size in key_sizes:
3102
p_offset, p_length = self._container_writer.add_bytes_record(
3103
raw_data[offset:offset+size], [])
3105
result.append((self._write_index, p_offset, p_length))
3108
def get_raw_records(self, memos_for_retrieval):
3109
"""Get the raw bytes for a records.
3111
:param memos_for_retrieval: An iterable containing the (index, pos,
3112
length) memo for retrieving the bytes. The Pack access method
3113
looks up the pack to use for a given record in its index_to_pack
3115
:return: An iterator over the bytes of the records.
3117
# first pass, group into same-index requests
3119
current_index = None
3120
for (index, offset, length) in memos_for_retrieval:
3121
if current_index == index:
3122
current_list.append((offset, length))
3124
if current_index is not None:
3125
request_lists.append((current_index, current_list))
3126
current_index = index
3127
current_list = [(offset, length)]
3128
# handle the last entry
3129
if current_index is not None:
3130
request_lists.append((current_index, current_list))
3131
for index, offsets in request_lists:
3133
transport, path = self._indices[index]
3135
# A KeyError here indicates that someone has triggered an index
3136
# reload, and this index has gone missing, we need to start
3138
if self._reload_func is None:
3139
# If we don't have a _reload_func there is nothing that can
3142
raise errors.RetryWithNewPacks(index,
3143
reload_occurred=True,
3144
exc_info=sys.exc_info())
3146
reader = pack.make_readv_reader(transport, path, offsets)
3147
for names, read_func in reader.iter_records():
3148
yield read_func(None)
3149
except errors.NoSuchFile:
3150
# A NoSuchFile error indicates that a pack file has gone
3151
# missing on disk, we need to trigger a reload, and start over.
3152
if self._reload_func is None:
3154
raise errors.RetryWithNewPacks(transport.abspath(path),
3155
reload_occurred=False,
3156
exc_info=sys.exc_info())
3158
def set_writer(self, writer, index, transport_packname):
3159
"""Set a writer to use for adding data."""
3160
if index is not None:
3161
self._indices[index] = transport_packname
3162
self._container_writer = writer
3163
self._write_index = index
3165
def reload_or_raise(self, retry_exc):
3166
"""Try calling the reload function, or re-raise the original exception.
3168
This should be called after _DirectPackAccess raises a
3169
RetryWithNewPacks exception. This function will handle the common logic
3170
of determining when the error is fatal versus being temporary.
3171
It will also make sure that the original exception is raised, rather
3172
than the RetryWithNewPacks exception.
3174
If this function returns, then the calling function should retry
3175
whatever operation was being performed. Otherwise an exception will
3178
:param retry_exc: A RetryWithNewPacks exception.
3181
if self._reload_func is None:
3183
elif not self._reload_func():
3184
# The reload claimed that nothing changed
3185
if not retry_exc.reload_occurred:
3186
# If there wasn't an earlier reload, then we really were
3187
# expecting to find changes. We didn't find them, so this is a
3191
exc_class, exc_value, exc_traceback = retry_exc.exc_info
3192
raise exc_class, exc_value, exc_traceback
3195
# Deprecated, use PatienceSequenceMatcher instead
3196
KnitSequenceMatcher = patiencediff.PatienceSequenceMatcher
3199
def annotate_knit(knit, revision_id):
3200
"""Annotate a knit with no cached annotations.
3202
This implementation is for knits with no cached annotations.
3203
It will work for knits with cached annotations, but this is not
3206
annotator = _KnitAnnotator(knit)
3207
return iter(annotator.annotate(revision_id))
3210
class _KnitAnnotator(object):
3211
"""Build up the annotations for a text."""
3213
def __init__(self, knit):
3216
# Content objects, differs from fulltexts because of how final newlines
3217
# are treated by knits. the content objects here will always have a
3219
self._fulltext_contents = {}
3221
# Annotated lines of specific revisions
3222
self._annotated_lines = {}
3224
# Track the raw data for nodes that we could not process yet.
3225
# This maps the revision_id of the base to a list of children that will
3226
# annotated from it.
3227
self._pending_children = {}
3229
# Nodes which cannot be extracted
3230
self._ghosts = set()
3232
# Track how many children this node has, so we know if we need to keep
3234
self._annotate_children = {}
3235
self._compression_children = {}
3237
self._all_build_details = {}
3238
# The children => parent revision_id graph
3239
self._revision_id_graph = {}
3241
self._heads_provider = None
3243
self._nodes_to_keep_annotations = set()
3244
self._generations_until_keep = 100
3246
def set_generations_until_keep(self, value):
3247
"""Set the number of generations before caching a node.
3249
Setting this to -1 will cache every merge node, setting this higher
3250
will cache fewer nodes.
3252
self._generations_until_keep = value
3254
def _add_fulltext_content(self, revision_id, content_obj):
3255
self._fulltext_contents[revision_id] = content_obj
3256
# TODO: jam 20080305 It might be good to check the sha1digest here
3257
return content_obj.text()
3259
def _check_parents(self, child, nodes_to_annotate):
3260
"""Check if all parents have been processed.
3262
:param child: A tuple of (rev_id, parents, raw_content)
3263
:param nodes_to_annotate: If child is ready, add it to
3264
nodes_to_annotate, otherwise put it back in self._pending_children
3266
for parent_id in child[1]:
3267
if (parent_id not in self._annotated_lines):
3268
# This parent is present, but another parent is missing
3269
self._pending_children.setdefault(parent_id,
3273
# This one is ready to be processed
3274
nodes_to_annotate.append(child)
3276
def _add_annotation(self, revision_id, fulltext, parent_ids,
3277
left_matching_blocks=None):
3278
"""Add an annotation entry.
3280
All parents should already have been annotated.
3281
:return: A list of children that now have their parents satisfied.
3283
a = self._annotated_lines
3284
annotated_parent_lines = [a[p] for p in parent_ids]
3285
annotated_lines = list(annotate.reannotate(annotated_parent_lines,
3286
fulltext, revision_id, left_matching_blocks,
3287
heads_provider=self._get_heads_provider()))
3288
self._annotated_lines[revision_id] = annotated_lines
3289
for p in parent_ids:
3290
ann_children = self._annotate_children[p]
3291
ann_children.remove(revision_id)
3292
if (not ann_children
3293
and p not in self._nodes_to_keep_annotations):
3294
del self._annotated_lines[p]
3295
del self._all_build_details[p]
3296
if p in self._fulltext_contents:
3297
del self._fulltext_contents[p]
3298
# Now that we've added this one, see if there are any pending
3299
# deltas to be done, certainly this parent is finished
3300
nodes_to_annotate = []
3301
for child in self._pending_children.pop(revision_id, []):
3302
self._check_parents(child, nodes_to_annotate)
3303
return nodes_to_annotate
3305
def _get_build_graph(self, key):
3306
"""Get the graphs for building texts and annotations.
3308
The data you need for creating a full text may be different than the
3309
data you need to annotate that text. (At a minimum, you need both
3310
parents to create an annotation, but only need 1 parent to generate the
3313
:return: A list of (key, index_memo) records, suitable for
3314
passing to read_records_iter to start reading in the raw data fro/
3317
if key in self._annotated_lines:
3320
pending = set([key])
3325
# get all pending nodes
3327
this_iteration = pending
3328
build_details = self._knit._index.get_build_details(this_iteration)
3329
self._all_build_details.update(build_details)
3330
# new_nodes = self._knit._index._get_entries(this_iteration)
3332
for key, details in build_details.iteritems():
3333
(index_memo, compression_parent, parents,
3334
record_details) = details
3335
self._revision_id_graph[key] = parents
3336
records.append((key, index_memo))
3337
# Do we actually need to check _annotated_lines?
3338
pending.update(p for p in parents
3339
if p not in self._all_build_details)
3340
if compression_parent:
3341
self._compression_children.setdefault(compression_parent,
3344
for parent in parents:
3345
self._annotate_children.setdefault(parent,
3347
num_gens = generation - kept_generation
3348
if ((num_gens >= self._generations_until_keep)
3349
and len(parents) > 1):
3350
kept_generation = generation
3351
self._nodes_to_keep_annotations.add(key)
3353
missing_versions = this_iteration.difference(build_details.keys())
3354
self._ghosts.update(missing_versions)
3355
for missing_version in missing_versions:
3356
# add a key, no parents
3357
self._revision_id_graph[missing_version] = ()
3358
pending.discard(missing_version) # don't look for it
3359
if self._ghosts.intersection(self._compression_children):
3361
"We cannot have nodes which have a ghost compression parent:\n"
3363
"compression children: %r"
3364
% (self._ghosts, self._compression_children))
3365
# Cleanout anything that depends on a ghost so that we don't wait for
3366
# the ghost to show up
3367
for node in self._ghosts:
3368
if node in self._annotate_children:
3369
# We won't be building this node
3370
del self._annotate_children[node]
3371
# Generally we will want to read the records in reverse order, because
3372
# we find the parent nodes after the children
3376
def _annotate_records(self, records):
3377
"""Build the annotations for the listed records."""
3378
# We iterate in the order read, rather than a strict order requested
3379
# However, process what we can, and put off to the side things that
3380
# still need parents, cleaning them up when those parents are
3382
for (rev_id, record,
3383
digest) in self._knit._read_records_iter(records):
3384
if rev_id in self._annotated_lines:
3386
parent_ids = self._revision_id_graph[rev_id]
3387
parent_ids = [p for p in parent_ids if p not in self._ghosts]
3388
details = self._all_build_details[rev_id]
3389
(index_memo, compression_parent, parents,
3390
record_details) = details
3391
nodes_to_annotate = []
3392
# TODO: Remove the punning between compression parents, and
3393
# parent_ids, we should be able to do this without assuming
3395
if len(parent_ids) == 0:
3396
# There are no parents for this node, so just add it
3397
# TODO: This probably needs to be decoupled
3398
fulltext_content, delta = self._knit._factory.parse_record(
3399
rev_id, record, record_details, None)
3400
fulltext = self._add_fulltext_content(rev_id, fulltext_content)
3401
nodes_to_annotate.extend(self._add_annotation(rev_id, fulltext,
3402
parent_ids, left_matching_blocks=None))
3404
child = (rev_id, parent_ids, record)
3405
# Check if all the parents are present
3406
self._check_parents(child, nodes_to_annotate)
3407
while nodes_to_annotate:
3408
# Should we use a queue here instead of a stack?
3409
(rev_id, parent_ids, record) = nodes_to_annotate.pop()
3410
(index_memo, compression_parent, parents,
3411
record_details) = self._all_build_details[rev_id]
3413
if compression_parent is not None:
3414
comp_children = self._compression_children[compression_parent]
3415
if rev_id not in comp_children:
3416
raise AssertionError("%r not in compression children %r"
3417
% (rev_id, comp_children))
3418
# If there is only 1 child, it is safe to reuse this
3420
reuse_content = (len(comp_children) == 1
3421
and compression_parent not in
3422
self._nodes_to_keep_annotations)
3424
# Remove it from the cache since it will be changing
3425
parent_fulltext_content = self._fulltext_contents.pop(compression_parent)
3426
# Make sure to copy the fulltext since it might be
3428
parent_fulltext = list(parent_fulltext_content.text())
3430
parent_fulltext_content = self._fulltext_contents[compression_parent]
3431
parent_fulltext = parent_fulltext_content.text()
3432
comp_children.remove(rev_id)
3433
fulltext_content, delta = self._knit._factory.parse_record(
3434
rev_id, record, record_details,
3435
parent_fulltext_content,
3436
copy_base_content=(not reuse_content))
3437
fulltext = self._add_fulltext_content(rev_id,
3439
if compression_parent == parent_ids[0]:
3440
# the compression_parent is the left parent, so we can
3442
blocks = KnitContent.get_line_delta_blocks(delta,
3443
parent_fulltext, fulltext)
3445
fulltext_content = self._knit._factory.parse_fulltext(
3447
fulltext = self._add_fulltext_content(rev_id,
3449
nodes_to_annotate.extend(
3450
self._add_annotation(rev_id, fulltext, parent_ids,
3451
left_matching_blocks=blocks))
3453
def _get_heads_provider(self):
3454
"""Create a heads provider for resolving ancestry issues."""
3455
if self._heads_provider is not None:
3456
return self._heads_provider
3457
parent_provider = _mod_graph.DictParentsProvider(
3458
self._revision_id_graph)
3459
graph_obj = _mod_graph.Graph(parent_provider)
3460
head_cache = _mod_graph.FrozenHeadsCache(graph_obj)
3461
self._heads_provider = head_cache
3464
def annotate(self, key):
3465
"""Return the annotated fulltext at the given key.
3467
:param key: The key to annotate.
3469
if len(self._knit._fallback_vfs) > 0:
3470
# stacked knits can't use the fast path at present.
3471
return self._simple_annotate(key)
3474
records = self._get_build_graph(key)
3475
if key in self._ghosts:
3476
raise errors.RevisionNotPresent(key, self._knit)
3477
self._annotate_records(records)
3478
return self._annotated_lines[key]
3479
except errors.RetryWithNewPacks, e:
3480
self._knit._access.reload_or_raise(e)
3481
# The cached build_details are no longer valid
3482
self._all_build_details.clear()
3484
def _simple_annotate(self, key):
3485
"""Return annotated fulltext, rediffing from the full texts.
3487
This is slow but makes no assumptions about the repository
3488
being able to produce line deltas.
3490
# TODO: this code generates a parent maps of present ancestors; it
3491
# could be split out into a separate method, and probably should use
3492
# iter_ancestry instead. -- mbp and robertc 20080704
3493
graph = _mod_graph.Graph(self._knit)
3494
head_cache = _mod_graph.FrozenHeadsCache(graph)
3495
search = graph._make_breadth_first_searcher([key])
3499
present, ghosts = search.next_with_ghosts()
3500
except StopIteration:
3502
keys.update(present)
3503
parent_map = self._knit.get_parent_map(keys)
3505
reannotate = annotate.reannotate
3506
for record in self._knit.get_record_stream(keys, 'topological', True):
3508
fulltext = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
3509
parents = parent_map[key]
3510
if parents is not None:
3511
parent_lines = [parent_cache[parent] for parent in parent_map[key]]
3514
parent_cache[key] = list(
3515
reannotate(parent_lines, fulltext, key, None, head_cache))
3517
return parent_cache[key]
3519
raise errors.RevisionNotPresent(key, self._knit)
3523
from bzrlib._knit_load_data_c import _load_data_c as _load_data
3525
from bzrlib._knit_load_data_py import _load_data_py as _load_data