/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/knit.py

  • Committer: Ian Clatworthy
  • Date: 2009-02-26 11:28:50 UTC
  • mto: (4084.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 4085.
  • Revision ID: ian.clatworthy@canonical.com-20090226112850-e4pdzx2nafu3jso3
added Organizing your workspace to the User Guide

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Knit versionedfile implementation.
 
18
 
 
19
A knit is a versioned file implementation that supports efficient append only
 
20
updates.
 
21
 
 
22
Knit file layout:
 
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"
 
27
 
 
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
 
31
59,59,3
 
32
8
 
33
8         if ie.executable:
 
34
8             e.set('executable', 'yes')
 
35
130,130,2
 
36
8         if elt.get('executable') == 'yes':
 
37
8             ie.executable = True
 
38
end robertc@robertcollins.net-20051003014215-ee2990904cc4c7ad
 
39
 
 
40
 
 
41
whats in an index:
 
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
 
47
 
 
48
 
 
49
so the index sequence is the dictionary compressed sequence number used
 
50
in the deltas to provide line annotation
 
51
 
 
52
"""
 
53
 
 
54
 
 
55
from cStringIO import StringIO
 
56
from itertools import izip, chain
 
57
import operator
 
58
import os
 
59
import sys
 
60
 
 
61
from bzrlib.lazy_import import lazy_import
 
62
lazy_import(globals(), """
 
63
from bzrlib import (
 
64
    annotate,
 
65
    debug,
 
66
    diff,
 
67
    graph as _mod_graph,
 
68
    index as _mod_index,
 
69
    lru_cache,
 
70
    pack,
 
71
    progress,
 
72
    trace,
 
73
    tsort,
 
74
    tuned_gzip,
 
75
    )
 
76
""")
 
77
from bzrlib import (
 
78
    errors,
 
79
    osutils,
 
80
    patiencediff,
 
81
    )
 
82
from bzrlib.errors import (
 
83
    FileExists,
 
84
    NoSuchFile,
 
85
    KnitError,
 
86
    InvalidRevisionId,
 
87
    KnitCorrupt,
 
88
    KnitHeaderError,
 
89
    RevisionNotPresent,
 
90
    RevisionAlreadyPresent,
 
91
    SHA1KnitCorrupt,
 
92
    )
 
93
from bzrlib.osutils import (
 
94
    contains_whitespace,
 
95
    contains_linebreaks,
 
96
    sha_string,
 
97
    sha_strings,
 
98
    split_lines,
 
99
    )
 
100
from bzrlib.versionedfile import (
 
101
    AbsentContentFactory,
 
102
    adapter_registry,
 
103
    ConstantMapper,
 
104
    ContentFactory,
 
105
    ChunkedContentFactory,
 
106
    VersionedFile,
 
107
    VersionedFiles,
 
108
    )
 
109
 
 
110
 
 
111
# TODO: Split out code specific to this format into an associated object.
 
112
 
 
113
# TODO: Can we put in some kind of value to check that the index and data
 
114
# files belong together?
 
115
 
 
116
# TODO: accommodate binaries, perhaps by storing a byte count
 
117
 
 
118
# TODO: function to check whole file
 
119
 
 
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.
 
123
 
 
124
DATA_SUFFIX = '.knit'
 
125
INDEX_SUFFIX = '.kndx'
 
126
_STREAM_MIN_BUFFER_SIZE = 5*1024*1024
 
127
 
 
128
 
 
129
class KnitAdapter(object):
 
130
    """Base class for knit record adaption."""
 
131
 
 
132
    def __init__(self, basis_vf):
 
133
        """Create an adapter which accesses full texts from basis_vf.
 
134
 
 
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.
 
137
        """
 
138
        self._data = KnitVersionedFiles(None, None)
 
139
        self._annotate_factory = KnitAnnotateFactory()
 
140
        self._plain_factory = KnitPlainFactory()
 
141
        self._basis_vf = basis_vf
 
142
 
 
143
 
 
144
class FTAnnotatedToUnannotated(KnitAdapter):
 
145
    """An adapter from FT annotated knits to unannotated ones."""
 
146
 
 
147
    def get_bytes(self, factory):
 
148
        annotated_compressed_bytes = factory._raw_record
 
149
        rec, contents = \
 
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())
 
153
        return bytes
 
154
 
 
155
 
 
156
class DeltaAnnotatedToUnannotated(KnitAdapter):
 
157
    """An adapter for deltas from annotated to unannotated."""
 
158
 
 
159
    def get_bytes(self, factory):
 
160
        annotated_compressed_bytes = factory._raw_record
 
161
        rec, contents = \
 
162
            self._data._parse_record_unchecked(annotated_compressed_bytes)
 
163
        delta = self._annotate_factory.parse_line_delta(contents, rec[1],
 
164
            plain=True)
 
165
        contents = self._plain_factory.lower_line_delta(delta)
 
166
        size, bytes = self._data._record_to_data((rec[1],), rec[3], contents)
 
167
        return bytes
 
168
 
 
169
 
 
170
class FTAnnotatedToFullText(KnitAdapter):
 
171
    """An adapter from FT annotated knits to unannotated ones."""
 
172
 
 
173
    def get_bytes(self, factory):
 
174
        annotated_compressed_bytes = factory._raw_record
 
175
        rec, contents = \
 
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())
 
180
 
 
181
 
 
182
class DeltaAnnotatedToFullText(KnitAdapter):
 
183
    """An adapter for deltas from annotated to unannotated."""
 
184
 
 
185
    def get_bytes(self, factory):
 
186
        annotated_compressed_bytes = factory._raw_record
 
187
        rec, contents = \
 
188
            self._data._parse_record_unchecked(annotated_compressed_bytes)
 
189
        delta = self._annotate_factory.parse_line_delta(contents, rec[1],
 
190
            plain=True)
 
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
 
199
        # one plain.
 
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())
 
204
 
 
205
 
 
206
class FTPlainToFullText(KnitAdapter):
 
207
    """An adapter from FT plain knits to unannotated ones."""
 
208
 
 
209
    def get_bytes(self, factory):
 
210
        compressed_bytes = factory._raw_record
 
211
        rec, contents = \
 
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())
 
216
 
 
217
 
 
218
class DeltaPlainToFullText(KnitAdapter):
 
219
    """An adapter for deltas from annotated to unannotated."""
 
220
 
 
221
    def get_bytes(self, factory):
 
222
        compressed_bytes = factory._raw_record
 
223
        rec, contents = \
 
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
 
236
        # one plain.
 
237
        content, _ = self._plain_factory.parse_record(rec[1], contents,
 
238
            factory._build_details, basis_content)
 
239
        return ''.join(content.text())
 
240
 
 
241
 
 
242
class KnitContentFactory(ContentFactory):
 
243
    """Content factory for streaming from knits.
 
244
 
 
245
    :seealso ContentFactory:
 
246
    """
 
247
 
 
248
    def __init__(self, key, parents, build_details, sha1, raw_record,
 
249
        annotated, knit=None, network_bytes=None):
 
250
        """Create a KnitContentFactory for key.
 
251
 
 
252
        :param key: The key.
 
253
        :param parents: The parents.
 
254
        :param build_details: The build details as returned from
 
255
            get_build_details.
 
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.
 
261
        """
 
262
        ContentFactory.__init__(self)
 
263
        self.sha1 = sha1
 
264
        self.key = key
 
265
        self.parents = parents
 
266
        if build_details[0] == 'line-delta':
 
267
            kind = 'delta'
 
268
        else:
 
269
            kind = 'ft'
 
270
        if annotated:
 
271
            annotated_kind = 'annotated-'
 
272
        else:
 
273
            annotated_kind = ''
 
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
 
278
        self._knit = knit
 
279
 
 
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:'
 
286
        else:
 
287
            parent_bytes = '\t'.join('\x00'.join(key) for key in self.parents)
 
288
        if self._build_details[1]:
 
289
            noeol = 'N'
 
290
        else:
 
291
            noeol = ' '
 
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
 
295
 
 
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,
 
307
            self.storage_kind)
 
308
 
 
309
 
 
310
class LazyKnitContentFactory(ContentFactory):
 
311
    """A ContentFactory which can either generate full text or a wire form.
 
312
 
 
313
    :seealso ContentFactory:
 
314
    """
 
315
 
 
316
    def __init__(self, key, parents, generator, first):
 
317
        """Create a LazyKnitContentFactory.
 
318
 
 
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
 
322
            key.
 
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
 
326
        """
 
327
        self.key = key
 
328
        self.parents = parents
 
329
        self.sha1 = None
 
330
        self._generator = generator
 
331
        self.storage_kind = "knit-delta-closure"
 
332
        if not first:
 
333
            self.storage_kind = self.storage_kind + "-ref"
 
334
        self._first = first
 
335
 
 
336
    def get_bytes_as(self, storage_kind):
 
337
        if storage_kind == self.storage_kind:
 
338
            if self._first:
 
339
                return self._generator._wire_bytes()
 
340
            else:
 
341
                # all the keys etc are contained in the bytes returned in the
 
342
                # first record.
 
343
                return ''
 
344
        if storage_kind in ('chunked', 'fulltext'):
 
345
            chunks = self._generator._get_one_work(self.key).text()
 
346
            if storage_kind == 'chunked':
 
347
                return chunks
 
348
            else:
 
349
                return ''.join(chunks)
 
350
        raise errors.UnavailableRepresentation(self.key, storage_kind,
 
351
            self.storage_kind)
 
352
 
 
353
 
 
354
def knit_delta_closure_to_records(storage_kind, bytes, line_end):
 
355
    """Convert a network record to a iterator over stream records.
 
356
 
 
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.
 
360
    """
 
361
    generator = _NetworkContentMapGenerator(bytes, line_end)
 
362
    return generator.get_record_stream()
 
363
 
 
364
 
 
365
def knit_network_to_record(storage_kind, bytes, line_end):
 
366
    """Convert a network record to a record object.
 
367
 
 
368
    :param storage_kind: The storage kind of the record.
 
369
    :param bytes: The bytes of the record on the network.
 
370
    """
 
371
    start = line_end
 
372
    line_end = bytes.find('\n', start)
 
373
    key = tuple(bytes[start:line_end].split('\x00'))
 
374
    start = line_end + 1
 
375
    line_end = bytes.find('\n', start)
 
376
    parent_line = bytes[start:line_end]
 
377
    if parent_line == 'None:':
 
378
        parents = None
 
379
    else:
 
380
        parents = tuple(
 
381
            [tuple(segment.split('\x00')) for segment in parent_line.split('\t')
 
382
             if segment])
 
383
    start = line_end + 1
 
384
    noeol = bytes[start] == 'N'
 
385
    if 'ft' in storage_kind:
 
386
        method = 'fulltext'
 
387
    else:
 
388
        method = 'line-delta'
 
389
    build_details = (method, noeol)
 
390
    start = start + 1
 
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)]
 
395
 
 
396
 
 
397
class KnitContent(object):
 
398
    """Content of a knit version to which deltas can be applied.
 
399
 
 
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.
 
403
    """
 
404
 
 
405
    def __init__(self):
 
406
        self._should_strip_eol = False
 
407
 
 
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)
 
411
 
 
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():
 
418
            if tag == 'equal':
 
419
                continue
 
420
            # ofrom, oto, length, data
 
421
            yield i1, i2, j2 - j1, new_lines._lines[j1:j2]
 
422
 
 
423
    def line_delta(self, new_lines):
 
424
        return list(self.line_delta_iter(new_lines))
 
425
 
 
426
    @staticmethod
 
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)
 
430
        s_pos = 0
 
431
        t_pos = 0
 
432
        for s_begin, s_end, t_len, new_text in knit_delta:
 
433
            true_n = s_begin - s_pos
 
434
            n = true_n
 
435
            if n > 0:
 
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]:
 
439
                    n-=1
 
440
                if n > 0:
 
441
                    yield s_pos, t_pos, n
 
442
            t_pos += t_len + true_n
 
443
            s_pos = s_end
 
444
        n = target_len - t_pos
 
445
        if n > 0:
 
446
            if source[s_pos + n -1] != target[t_pos + n -1]:
 
447
                n-=1
 
448
            if n > 0:
 
449
                yield s_pos, t_pos, n
 
450
        yield s_pos + (target_len - t_pos), target_len, 0
 
451
 
 
452
 
 
453
class AnnotatedKnitContent(KnitContent):
 
454
    """Annotated content."""
 
455
 
 
456
    def __init__(self, lines):
 
457
        KnitContent.__init__(self)
 
458
        self._lines = lines
 
459
 
 
460
    def annotate(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'))
 
466
        return lines
 
467
 
 
468
    def apply_delta(self, delta, new_version_id):
 
469
        """Apply delta to this object to become new_version_id."""
 
470
        offset = 0
 
471
        lines = self._lines
 
472
        for start, end, count, delta_lines in delta:
 
473
            lines[offset+start:offset+end] = delta_lines
 
474
            offset = offset + (start - end) + count
 
475
 
 
476
    def text(self):
 
477
        try:
 
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
 
482
            # around 20071015
 
483
            raise KnitCorrupt(self,
 
484
                "line in annotated knit missing annotation information: %s"
 
485
                % (e,))
 
486
        if self._should_strip_eol:
 
487
            lines[-1] = lines[-1].rstrip('\n')
 
488
        return lines
 
489
 
 
490
    def copy(self):
 
491
        return AnnotatedKnitContent(self._lines[:])
 
492
 
 
493
 
 
494
class PlainKnitContent(KnitContent):
 
495
    """Unannotated content.
 
496
 
 
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
 
499
    objects.
 
500
    """
 
501
 
 
502
    def __init__(self, lines, version_id):
 
503
        KnitContent.__init__(self)
 
504
        self._lines = lines
 
505
        self._version_id = version_id
 
506
 
 
507
    def annotate(self):
 
508
        """Return a list of (origin, text) for each content line."""
 
509
        return [(self._version_id, line) for line in self._lines]
 
510
 
 
511
    def apply_delta(self, delta, new_version_id):
 
512
        """Apply delta to this object to become new_version_id."""
 
513
        offset = 0
 
514
        lines = self._lines
 
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
 
519
 
 
520
    def copy(self):
 
521
        return PlainKnitContent(self._lines[:], self._version_id)
 
522
 
 
523
    def text(self):
 
524
        lines = self._lines
 
525
        if self._should_strip_eol:
 
526
            lines = lines[:]
 
527
            lines[-1] = lines[-1].rstrip('\n')
 
528
        return lines
 
529
 
 
530
 
 
531
class _KnitFactory(object):
 
532
    """Base class for common Factory functions."""
 
533
 
 
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.
 
537
 
 
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
 
541
            get_build_details
 
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
 
546
            place.
 
547
        :return: (content, delta) A Content object and possibly a line-delta,
 
548
            delta may be None
 
549
        """
 
550
        method, noeol = record_details
 
551
        if method == 'line-delta':
 
552
            if copy_base_content:
 
553
                content = base_content.copy()
 
554
            else:
 
555
                content = base_content
 
556
            delta = self.parse_line_delta(record, version_id)
 
557
            content.apply_delta(delta, version_id)
 
558
        else:
 
559
            content = self.parse_fulltext(record, version_id)
 
560
            delta = None
 
561
        content._should_strip_eol = noeol
 
562
        return (content, delta)
 
563
 
 
564
 
 
565
class KnitAnnotateFactory(_KnitFactory):
 
566
    """Factory for creating annotated Content objects."""
 
567
 
 
568
    annotated = True
 
569
 
 
570
    def make(self, lines, version_id):
 
571
        num_lines = len(lines)
 
572
        return AnnotatedKnitContent(zip([version_id] * num_lines, lines))
 
573
 
 
574
    def parse_fulltext(self, content, version_id):
 
575
        """Convert fulltext to internal representation
 
576
 
 
577
        fulltext content is of the format
 
578
        revid(utf8) plaintext\n
 
579
        internal representation is of the format:
 
580
        (revid, plaintext)
 
581
        """
 
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)
 
588
 
 
589
    def parse_line_delta_iter(self, lines):
 
590
        return iter(self.parse_line_delta(lines))
 
591
 
 
592
    def parse_line_delta(self, lines, version_id, plain=False):
 
593
        """Convert a line based delta into internal representation.
 
594
 
 
595
        line delta is in the form of:
 
596
        intstart intend intcount
 
597
        1..count lines:
 
598
        revid(utf8) newline\n
 
599
        internal representation is
 
600
        (start, end, count, [1..count tuples (revid, newline)])
 
601
 
 
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])
 
605
        """
 
606
        result = []
 
607
        lines = iter(lines)
 
608
        next = lines.next
 
609
 
 
610
        cache = {}
 
611
        def cache_and_return(line):
 
612
            origin, text = line.split(' ', 1)
 
613
            return cache.setdefault(origin, origin), text
 
614
 
 
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
 
618
        if plain:
 
619
            for header in lines:
 
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))
 
623
        else:
 
624
            for header in lines:
 
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))
 
628
        return result
 
629
 
 
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)
 
633
 
 
634
    def get_linedelta_content(self, lines):
 
635
        """Extract just the content from a line delta.
 
636
 
 
637
        This doesn't return all of the extra information stored in a delta.
 
638
        Only the actual content lines.
 
639
        """
 
640
        lines = iter(lines)
 
641
        next = lines.next
 
642
        for header in lines:
 
643
            header = header.split(',')
 
644
            count = int(header[2])
 
645
            for i in xrange(count):
 
646
                origin, text = next().split(' ', 1)
 
647
                yield text
 
648
 
 
649
    def lower_fulltext(self, content):
 
650
        """convert a fulltext content record into a serializable form.
 
651
 
 
652
        see parse_fulltext which this inverts.
 
653
        """
 
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]
 
657
 
 
658
    def lower_line_delta(self, delta):
 
659
        """convert a delta into a serializable form.
 
660
 
 
661
        See parse_line_delta which this inverts.
 
662
        """
 
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
 
665
        out = []
 
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)
 
670
        return out
 
671
 
 
672
    def annotate(self, knit, key):
 
673
        content = knit._get_content(key)
 
674
        # adjust for the fact that serialised annotations are only key suffixes
 
675
        # for this factory.
 
676
        if type(key) == tuple:
 
677
            prefix = key[:-1]
 
678
            origins = content.annotate()
 
679
            result = []
 
680
            for origin, line in origins:
 
681
                result.append((prefix + (origin,), line))
 
682
            return result
 
683
        else:
 
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()
 
687
 
 
688
 
 
689
class KnitPlainFactory(_KnitFactory):
 
690
    """Factory for creating plain Content objects."""
 
691
 
 
692
    annotated = False
 
693
 
 
694
    def make(self, lines, version_id):
 
695
        return PlainKnitContent(lines, version_id)
 
696
 
 
697
    def parse_fulltext(self, content, version_id):
 
698
        """This parses an unannotated fulltext.
 
699
 
 
700
        Note that this is not a noop - the internal representation
 
701
        has (versionid, line) - its just a constant versionid.
 
702
        """
 
703
        return self.make(content, version_id)
 
704
 
 
705
    def parse_line_delta_iter(self, lines, version_id):
 
706
        cur = 0
 
707
        num_lines = len(lines)
 
708
        while cur < num_lines:
 
709
            header = lines[cur]
 
710
            cur += 1
 
711
            start, end, c = [int(n) for n in header.split(',')]
 
712
            yield start, end, c, lines[cur:cur+c]
 
713
            cur += c
 
714
 
 
715
    def parse_line_delta(self, lines, version_id):
 
716
        return list(self.parse_line_delta_iter(lines, version_id))
 
717
 
 
718
    def get_fulltext_content(self, lines):
 
719
        """Extract just the content lines from a fulltext."""
 
720
        return iter(lines)
 
721
 
 
722
    def get_linedelta_content(self, lines):
 
723
        """Extract just the content from a line delta.
 
724
 
 
725
        This doesn't return all of the extra information stored in a delta.
 
726
        Only the actual content lines.
 
727
        """
 
728
        lines = iter(lines)
 
729
        next = lines.next
 
730
        for header in lines:
 
731
            header = header.split(',')
 
732
            count = int(header[2])
 
733
            for i in xrange(count):
 
734
                yield next()
 
735
 
 
736
    def lower_fulltext(self, content):
 
737
        return content.text()
 
738
 
 
739
    def lower_line_delta(self, delta):
 
740
        out = []
 
741
        for start, end, c, lines in delta:
 
742
            out.append('%d,%d,%d\n' % (start, end, c))
 
743
            out.extend(lines)
 
744
        return out
 
745
 
 
746
    def annotate(self, knit, key):
 
747
        annotator = _KnitAnnotator(knit)
 
748
        return annotator.annotate(key)
 
749
 
 
750
 
 
751
 
 
752
def make_file_factory(annotated, mapper):
 
753
    """Create a factory for creating a file based KnitVersionedFiles.
 
754
 
 
755
    This is only functional enough to run interface tests, it doesn't try to
 
756
    provide a full pack environment.
 
757
 
 
758
    :param annotated: knit annotations are wanted.
 
759
    :param mapper: The mapper from keys to paths.
 
760
    """
 
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)
 
765
    return factory
 
766
 
 
767
 
 
768
def make_pack_factory(graph, delta, keylength):
 
769
    """Create a factory for creating a pack based VersionedFiles.
 
770
 
 
771
    This is only functional enough to run interface tests, it doesn't try to
 
772
    provide a full pack environment.
 
773
 
 
774
    :param graph: Store a graph.
 
775
    :param delta: Delta compress contents.
 
776
    :param keylength: How long should keys be.
 
777
    """
 
778
    def factory(transport):
 
779
        parents = graph or delta
 
780
        ref_length = 0
 
781
        if graph:
 
782
            ref_length += 1
 
783
        if delta:
 
784
            ref_length += 1
 
785
            max_delta_chain = 200
 
786
        else:
 
787
            max_delta_chain = 0
 
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)
 
792
        writer.begin()
 
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
 
801
        return result
 
802
    return factory
 
803
 
 
804
 
 
805
def cleanup_pack_knit(versioned_files):
 
806
    versioned_files.stream.close()
 
807
    versioned_files.writer.end()
 
808
 
 
809
 
 
810
def _get_total_build_size(self, keys, positions):
 
811
    """Determine the total bytes to build these keys.
 
812
 
 
813
    (helper function because _KnitGraphIndex and _KndxIndex work the same, but
 
814
    don't inherit from a common base.)
 
815
 
 
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
 
820
    """
 
821
    all_build_index_memos = {}
 
822
    build_keys = keys
 
823
    while build_keys:
 
824
        next_keys = set()
 
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:
 
829
                continue
 
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()])
 
837
 
 
838
 
 
839
class KnitVersionedFiles(VersionedFiles):
 
840
    """Storage for many versioned files using knit compression.
 
841
 
 
842
    Backend storage is managed by indices and data objects.
 
843
 
 
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.
 
848
    """
 
849
 
 
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.
 
853
 
 
854
        :param index: The index for the knit data.
 
855
        :param data_access: The access object to store and retrieve knit
 
856
            records.
 
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.
 
864
        """
 
865
        self._index = index
 
866
        self._access = data_access
 
867
        self._max_delta_chain = max_delta_chain
 
868
        if annotated:
 
869
            self._factory = KnitAnnotateFactory()
 
870
        else:
 
871
            self._factory = KnitPlainFactory()
 
872
        self._fallback_vfs = []
 
873
        self._reload_func = reload_func
 
874
 
 
875
    def __repr__(self):
 
876
        return "%s(%r, %r)" % (
 
877
            self.__class__.__name__,
 
878
            self._index,
 
879
            self._access)
 
880
 
 
881
    def add_fallback_versioned_files(self, a_versioned_files):
 
882
        """Add a source of texts for texts not present in this knit.
 
883
 
 
884
        :param a_versioned_files: A VersionedFiles object.
 
885
        """
 
886
        self._fallback_vfs.append(a_versioned_files)
 
887
 
 
888
    def add_lines(self, key, parents, lines, parent_texts=None,
 
889
        left_matching_blocks=None, nostore_sha=None, random_id=False,
 
890
        check_content=True):
 
891
        """See VersionedFiles.add_lines()."""
 
892
        self._index._check_write_ok()
 
893
        self._check_add(key, lines, random_id, check_content)
 
894
        if parents is None:
 
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.
 
898
            parents = ()
 
899
        return self._add(key, lines, parents,
 
900
            parent_texts, left_matching_blocks, nostore_sha, random_id)
 
901
 
 
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.
 
905
 
 
906
        Any versions not present will be converted into ghosts.
 
907
        """
 
908
        # first thing, if the content is something we don't need to store, find
 
909
        # that out.
 
910
        line_bytes = ''.join(lines)
 
911
        digest = sha_string(line_bytes)
 
912
        if nostore_sha == digest:
 
913
            raise errors.ExistingContent
 
914
 
 
915
        present_parents = []
 
916
        if parent_texts is None:
 
917
            parent_texts = {}
 
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)
 
924
 
 
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]):
 
928
            delta = False
 
929
        else:
 
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])
 
934
 
 
935
        text_length = len(line_bytes)
 
936
        options = []
 
937
        if lines:
 
938
            if lines[-1][-1] != '\n':
 
939
                # copy the contents of lines.
 
940
                lines = lines[:]
 
941
                options.append('no-eol')
 
942
                lines[-1] = lines[-1] + '\n'
 
943
                line_bytes += '\n'
 
944
 
 
945
        for element in key:
 
946
            if type(element) != str:
 
947
                raise TypeError("key contains non-strings: %r" % (key,))
 
948
        # Knit hunks are still last-element only
 
949
        version_id = key[-1]
 
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
 
953
            # EOL.
 
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)
 
960
 
 
961
        if delta:
 
962
            options.append('line-delta')
 
963
            store_lines = self._factory.lower_line_delta(delta_hunks)
 
964
            size, bytes = self._record_to_data(key, digest,
 
965
                store_lines)
 
966
        else:
 
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
 
971
                # _record_to_data.
 
972
                size, bytes = self._record_to_data(key, digest,
 
973
                    lines, [line_bytes])
 
974
            else:
 
975
                # get mixed annotation + content and feed it into the
 
976
                # serialiser.
 
977
                store_lines = self._factory.lower_fulltext(content)
 
978
                size, bytes = self._record_to_data(key, digest,
 
979
                    store_lines)
 
980
 
 
981
        access_memo = self._access.add_raw_records([(key, size)], bytes)[0]
 
982
        self._index.add_records(
 
983
            ((key, options, access_memo, parents),),
 
984
            random_id=random_id)
 
985
        return digest, text_length, content
 
986
 
 
987
    def annotate(self, key):
 
988
        """See VersionedFiles.annotate."""
 
989
        return self._factory.annotate(self, key)
 
990
 
 
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
 
996
        # no basis.
 
997
        keys = self._index.keys()
 
998
        parent_map = self.get_parent_map(keys)
 
999
        for key in 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()
 
1008
 
 
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.
 
1019
        if check_content:
 
1020
            self._check_lines_not_unicode(lines)
 
1021
            self._check_lines_are_lines(lines)
 
1022
 
 
1023
    def _check_header(self, key, line):
 
1024
        rec = self._split_header(line)
 
1025
        self._check_header_version(rec, key[-1])
 
1026
        return rec
 
1027
 
 
1028
    def _check_header_version(self, rec, version_id):
 
1029
        """Checks the header version on original format knit records.
 
1030
 
 
1031
        These have the last component of the key embedded in the record.
 
1032
        """
 
1033
        if rec[1] != version_id:
 
1034
            raise KnitCorrupt(self,
 
1035
                'unexpected version, wanted %r, got %r' % (version_id, rec[1]))
 
1036
 
 
1037
    def _check_should_delta(self, parent):
 
1038
        """Iterate back through the parent listing, looking for a fulltext.
 
1039
 
 
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.
 
1044
 
 
1045
        Return True if we should create a new delta, False if we should use a
 
1046
        full text.
 
1047
        """
 
1048
        delta_size = 0
 
1049
        fulltext_size = None
 
1050
        for count in xrange(self._max_delta_chain):
 
1051
            try:
 
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
 
1055
                # boundaries.
 
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
 
1060
                return False
 
1061
            index_memo, compression_parent, _, _ = parent_details
 
1062
            _, _, size = index_memo
 
1063
            if compression_parent is None:
 
1064
                fulltext_size = size
 
1065
                break
 
1066
            delta_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
 
1070
        else:
 
1071
            # We couldn't find a fulltext, so we must create a new one
 
1072
            return False
 
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
 
1076
 
 
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]
 
1081
 
 
1082
    def _get_components_positions(self, keys, allow_missing=False):
 
1083
        """Produce a map of position data for the components of keys.
 
1084
 
 
1085
        This data is intended to be used for retrieving the knit records.
 
1086
 
 
1087
        A dict of key to (record_details, index_memo, next, parents) is
 
1088
        returned.
 
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
 
1091
            data
 
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
 
1094
 
 
1095
        :param allow_missing: If True do not raise an error on a missing component,
 
1096
            just ignore it.
 
1097
        """
 
1098
        component_data = {}
 
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
 
1115
 
 
1116
    def _get_content(self, key, parent_texts={}):
 
1117
        """Returns a content object that makes up the specified
 
1118
        version."""
 
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)
 
1127
 
 
1128
    def get_parent_map(self, keys):
 
1129
        """Get a map of the graph parents of keys.
 
1130
 
 
1131
        :param keys: The keys to look up parents for.
 
1132
        :return: A mapping from keys to parents. Absent keys are absent from
 
1133
            the mapping.
 
1134
        """
 
1135
        return self._get_parent_map_with_sources(keys)[0]
 
1136
 
 
1137
    def _get_parent_map_with_sources(self, keys):
 
1138
        """Get a map of the parents of keys.
 
1139
 
 
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,
 
1145
            and so on.
 
1146
        """
 
1147
        result = {}
 
1148
        sources = [self._index] + self._fallback_vfs
 
1149
        source_results = []
 
1150
        missing = set(keys)
 
1151
        for source in sources:
 
1152
            if not missing:
 
1153
                break
 
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
 
1159
 
 
1160
    def _get_record_map(self, keys, allow_missing=False):
 
1161
        """Produce a dictionary of knit records.
 
1162
 
 
1163
        :return: {key:(record, record_details, digest, next)}
 
1164
            record
 
1165
                data returned from read_records (a KnitContentobject)
 
1166
            record_details
 
1167
                opaque information to pass to parse_record
 
1168
            digest
 
1169
                SHA1 digest of the full text after all steps are done
 
1170
            next
 
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.
 
1176
        """
 
1177
        raw_map = self._get_record_map_unparsed(keys,
 
1178
            allow_missing=allow_missing)
 
1179
        return self._raw_map_to_record_map(raw_map)
 
1180
 
 
1181
    def _raw_map_to_record_map(self, raw_map):
 
1182
        """Parse the contents of _get_record_map_unparsed.
 
1183
 
 
1184
        :return: see _get_record_map.
 
1185
        """
 
1186
        result = {}
 
1187
        for key in raw_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
 
1191
        return result
 
1192
 
 
1193
    def _get_record_map_unparsed(self, keys, allow_missing=False):
 
1194
        """Get the raw data for reconstructing keys without parsing it.
 
1195
 
 
1196
        :return: A dict suitable for parsing via _raw_map_to_record_map.
 
1197
            key-> raw_bytes, (method, noeol), compression_parent
 
1198
        """
 
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.
 
1206
        while True:
 
1207
            try:
 
1208
                position_map = self._get_components_positions(keys,
 
1209
                    allow_missing=allow_missing)
 
1210
                # key = component_id, r = record_details, i_m = index_memo,
 
1211
                # n = next
 
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))
 
1217
                raw_record_map = {}
 
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)
 
1224
 
 
1225
    @classmethod
 
1226
    def _split_by_prefix(cls, keys):
 
1227
        """For the given keys, split them up based on their prefix.
 
1228
 
 
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
 
1233
        file-id boundaries.
 
1234
 
 
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.
 
1238
 
 
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
 
1243
        """
 
1244
        split_by_prefix = {}
 
1245
        prefix_order = []
 
1246
        for key in keys:
 
1247
            if len(key) == 1:
 
1248
                prefix = ''
 
1249
            else:
 
1250
                prefix = key[0]
 
1251
 
 
1252
            if prefix in split_by_prefix:
 
1253
                split_by_prefix[prefix].append(key)
 
1254
            else:
 
1255
                split_by_prefix[prefix] = [key]
 
1256
                prefix_order.append(prefix)
 
1257
        return split_by_prefix, prefix_order
 
1258
 
 
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.
 
1262
 
 
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
 
1266
        together.
 
1267
 
 
1268
        :return: list of (keys, non_local) tuples that indicate what keys
 
1269
            should be fetched next.
 
1270
        """
 
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)
 
1278
        cur_keys = []
 
1279
        cur_non_local = set()
 
1280
        cur_size = 0
 
1281
        result = []
 
1282
        sizes = []
 
1283
        for prefix in prefix_order:
 
1284
            keys = prefix_split_keys[prefix]
 
1285
            non_local = prefix_split_non_local_keys.get(prefix, [])
 
1286
 
 
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)
 
1294
                cur_keys = []
 
1295
                cur_non_local = set()
 
1296
                cur_size = 0
 
1297
        if cur_keys:
 
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)
 
1303
        return result
 
1304
 
 
1305
    def get_record_stream(self, keys, ordering, include_delta_closure):
 
1306
        """Get a stream of records for keys.
 
1307
 
 
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
 
1311
            children.
 
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.
 
1316
        """
 
1317
        # keys might be a generator
 
1318
        keys = set(keys)
 
1319
        if not keys:
 
1320
            return
 
1321
        if not self._index.has_graph:
 
1322
            # Cannot topological order when no graph has been stored.
 
1323
            ordering = 'unordered'
 
1324
 
 
1325
        remaining_keys = keys
 
1326
        while True:
 
1327
            try:
 
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
 
1333
                return
 
1334
            except errors.RetryWithNewPacks, e:
 
1335
                self._access.reload_or_raise(e)
 
1336
 
 
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)
 
1342
        else:
 
1343
            build_details = self._index.get_build_details(keys)
 
1344
            # map from key to
 
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 = {}
 
1358
            for key in keys:
 
1359
                # the delta chain
 
1360
                try:
 
1361
                    chain = [key, positions[key][2]]
 
1362
                except KeyError:
 
1363
                    needed_from_fallback.add(key)
 
1364
                    continue
 
1365
                result = True
 
1366
                while chain[-1] is not None:
 
1367
                    if chain[-1] in reconstructable_keys:
 
1368
                        result = reconstructable_keys[chain[-1]]
 
1369
                        break
 
1370
                    else:
 
1371
                        try:
 
1372
                            chain.append(positions[chain[-1]][2])
 
1373
                        except KeyError:
 
1374
                            # missing basis component
 
1375
                            needed_from_fallback.add(chain[-1])
 
1376
                            result = True
 
1377
                            break
 
1378
                for chain_key in chain[:-1]:
 
1379
                    reconstructable_keys[chain_key] = result
 
1380
                if not 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:
 
1388
            source_keys = []
 
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
 
1394
                        break
 
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)
 
1399
        else:
 
1400
            if ordering != 'unordered':
 
1401
                raise AssertionError('valid values for ordering are:'
 
1402
                    ' "unordered" or "topological" not: %r'
 
1403
                    % (ordering,))
 
1404
            # Just group by source; remote sources first.
 
1405
            present_keys = []
 
1406
            source_keys = []
 
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
 
1414
            # order.
 
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
 
1428
            # to be passed in.
 
1429
            non_local_keys = needed_from_fallback - absent_keys
 
1430
            for keys, non_local_keys in self._group_keys_for_io(present_keys,
 
1431
                                                                non_local_keys,
 
1432
                                                                positions):
 
1433
                generator = _VFContentMapGenerator(self, keys, non_local_keys,
 
1434
                                                   global_map)
 
1435
                for record in generator.get_record_stream():
 
1436
                    yield record
 
1437
        else:
 
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)
 
1446
                else:
 
1447
                    vf = self._fallback_vfs[parent_maps.index(source) - 1]
 
1448
                    for record in vf.get_record_stream(keys, ordering,
 
1449
                        include_delta_closure):
 
1450
                        yield record
 
1451
 
 
1452
    def get_sha1s(self, keys):
 
1453
        """See VersionedFiles.get_sha1s()."""
 
1454
        missing = set(keys)
 
1455
        record_map = self._get_record_map(missing, allow_missing=True)
 
1456
        result = {}
 
1457
        for key, details in record_map.iteritems():
 
1458
            if key not in missing:
 
1459
                continue
 
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:
 
1464
            if not missing:
 
1465
                break
 
1466
            new_result = source.get_sha1s(missing)
 
1467
            result.update(new_result)
 
1468
            missing.difference_update(set(new_result))
 
1469
        return result
 
1470
 
 
1471
    def insert_record_stream(self, stream):
 
1472
        """Insert a record stream into this container.
 
1473
 
 
1474
        :param stream: A stream of records to insert.
 
1475
        :return: None
 
1476
        :seealso VersionedFiles.get_record_stream:
 
1477
        """
 
1478
        def get_adapter(adapter_key):
 
1479
            try:
 
1480
                return adapters[adapter_key]
 
1481
            except KeyError:
 
1482
                adapter_factory = adapter_registry.get(adapter_key)
 
1483
                adapter = adapter_factory(self)
 
1484
                adapters[adapter_key] = adapter
 
1485
                return adapter
 
1486
        delta_types = set()
 
1487
        if self._factory.annotated:
 
1488
            # self is annotated, we need annotated knits to use directly.
 
1489
            annotated = "annotated-"
 
1490
            convertibles = []
 
1491
        else:
 
1492
            # self is not annotated, but we can strip annotations cheaply.
 
1493
            annotated = ""
 
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)
 
1505
        adapters = {}
 
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.
 
1512
        #
 
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.
 
1516
        #
 
1517
        # key = basis_parent, value = index entry to add
 
1518
        buffered_index_entries = {}
 
1519
        for record in stream:
 
1520
            parents = record.parents
 
1521
            if record.storage_kind in delta_types:
 
1522
                # TODO: eventually the record itself should track
 
1523
                #       compression_parent
 
1524
                compression_parent = parents[0]
 
1525
            else:
 
1526
                compression_parent = None
 
1527
            # Raise an error when a record is missing.
 
1528
            if record.storage_kind == 'absent':
 
1529
                raise RevisionNotPresent([record.key], self)
 
1530
            elif ((record.storage_kind in knit_types)
 
1531
                  and (compression_parent is None
 
1532
                       or not self._fallback_vfs
 
1533
                       or self._index.has_key(compression_parent)
 
1534
                       or not self.has_key(compression_parent))):
 
1535
                # we can insert the knit record literally if either it has no
 
1536
                # compression parent OR we already have its basis in this kvf
 
1537
                # OR the basis is not present even in the fallbacks.  In the
 
1538
                # last case it will either turn up later in the stream and all
 
1539
                # will be well, or it won't turn up at all and we'll raise an
 
1540
                # error at the end.
 
1541
                #
 
1542
                # TODO: self.has_key is somewhat redundant with
 
1543
                # self._index.has_key; we really want something that directly
 
1544
                # asks if it's only present in the fallbacks. -- mbp 20081119
 
1545
                if record.storage_kind not in native_types:
 
1546
                    try:
 
1547
                        adapter_key = (record.storage_kind, "knit-delta-gz")
 
1548
                        adapter = get_adapter(adapter_key)
 
1549
                    except KeyError:
 
1550
                        adapter_key = (record.storage_kind, "knit-ft-gz")
 
1551
                        adapter = get_adapter(adapter_key)
 
1552
                    bytes = adapter.get_bytes(record)
 
1553
                else:
 
1554
                    # It's a knit record, it has a _raw_record field (even if
 
1555
                    # it was reconstituted from a network stream).
 
1556
                    bytes = record._raw_record
 
1557
                options = [record._build_details[0]]
 
1558
                if record._build_details[1]:
 
1559
                    options.append('no-eol')
 
1560
                # Just blat it across.
 
1561
                # Note: This does end up adding data on duplicate keys. As
 
1562
                # modern repositories use atomic insertions this should not
 
1563
                # lead to excessive growth in the event of interrupted fetches.
 
1564
                # 'knit' repositories may suffer excessive growth, but as a
 
1565
                # deprecated format this is tolerable. It can be fixed if
 
1566
                # needed by in the kndx index support raising on a duplicate
 
1567
                # add with identical parents and options.
 
1568
                access_memo = self._access.add_raw_records(
 
1569
                    [(record.key, len(bytes))], bytes)[0]
 
1570
                index_entry = (record.key, options, access_memo, parents)
 
1571
                buffered = False
 
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.
 
1580
                    #
 
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)
 
1587
                        buffered = True
 
1588
                if not buffered:
 
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')))
 
1593
            else:
 
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
 
1598
                # fallback kvfs.
 
1599
                try:
 
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)
 
1607
                try:
 
1608
                    self.add_lines(record.key, parents, lines)
 
1609
                except errors.RevisionAlreadyPresent:
 
1610
                    pass
 
1611
            # Add any records whose basis parent is now available.
 
1612
            added_keys = [record.key]
 
1613
            while added_keys:
 
1614
                key = added_keys.pop(0)
 
1615
                if key in buffered_index_entries:
 
1616
                    index_entries = buffered_index_entries[key]
 
1617
                    self._index.add_records(index_entries)
 
1618
                    added_keys.extend(
 
1619
                        [index_entry[0] for index_entry in index_entries])
 
1620
                    del buffered_index_entries[key]
 
1621
        if buffered_index_entries:
 
1622
            # There were index entries buffered at the end of the stream,
 
1623
            # So these need to be added (if the index supports holding such
 
1624
            # entries for later insertion)
 
1625
            for key in buffered_index_entries:
 
1626
                index_entries = buffered_index_entries[key]
 
1627
                self._index.add_records(index_entries,
 
1628
                    missing_compression_parents=True)
 
1629
 
 
1630
    def get_missing_compression_parent_keys(self):
 
1631
        """Return an iterable of keys of missing compression parents.
 
1632
 
 
1633
        Check this after calling insert_record_stream to find out if there are
 
1634
        any missing compression parents.  If there are, the records that
 
1635
        depend on them are not able to be inserted safely. For atomic
 
1636
        KnitVersionedFiles built on packs, the transaction should be aborted or
 
1637
        suspended - commit will fail at this point. Nonatomic knits will error
 
1638
        earlier because they have no staging area to put pending entries into.
 
1639
        """
 
1640
        return self._index.get_missing_compression_parents()
 
1641
 
 
1642
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
 
1643
        """Iterate over the lines in the versioned files from keys.
 
1644
 
 
1645
        This may return lines from other keys. Each item the returned
 
1646
        iterator yields is a tuple of a line and a text version that that line
 
1647
        is present in (not introduced in).
 
1648
 
 
1649
        Ordering of results is in whatever order is most suitable for the
 
1650
        underlying storage format.
 
1651
 
 
1652
        If a progress bar is supplied, it may be used to indicate progress.
 
1653
        The caller is responsible for cleaning up progress bars (because this
 
1654
        is an iterator).
 
1655
 
 
1656
        NOTES:
 
1657
         * Lines are normalised by the underlying store: they will all have \\n
 
1658
           terminators.
 
1659
         * Lines are returned in arbitrary order.
 
1660
         * If a requested key did not change any lines (or didn't have any
 
1661
           lines), it may not be mentioned at all in the result.
 
1662
 
 
1663
        :return: An iterator over (line, key).
 
1664
        """
 
1665
        if pb is None:
 
1666
            pb = progress.DummyProgress()
 
1667
        keys = set(keys)
 
1668
        total = len(keys)
 
1669
        done = False
 
1670
        while not done:
 
1671
            try:
 
1672
                # we don't care about inclusions, the caller cares.
 
1673
                # but we need to setup a list of records to visit.
 
1674
                # we need key, position, length
 
1675
                key_records = []
 
1676
                build_details = self._index.get_build_details(keys)
 
1677
                for key, details in build_details.iteritems():
 
1678
                    if key in keys:
 
1679
                        key_records.append((key, details[0]))
 
1680
                records_iter = enumerate(self._read_records_iter(key_records))
 
1681
                for (key_idx, (key, data, sha_value)) in records_iter:
 
1682
                    pb.update('Walking content.', key_idx, total)
 
1683
                    compression_parent = build_details[key][1]
 
1684
                    if compression_parent is None:
 
1685
                        # fulltext
 
1686
                        line_iterator = self._factory.get_fulltext_content(data)
 
1687
                    else:
 
1688
                        # Delta
 
1689
                        line_iterator = self._factory.get_linedelta_content(data)
 
1690
                    # Now that we are yielding the data for this key, remove it
 
1691
                    # from the list
 
1692
                    keys.remove(key)
 
1693
                    # XXX: It might be more efficient to yield (key,
 
1694
                    # line_iterator) in the future. However for now, this is a
 
1695
                    # simpler change to integrate into the rest of the
 
1696
                    # codebase. RBC 20071110
 
1697
                    for line in line_iterator:
 
1698
                        yield line, key
 
1699
                done = True
 
1700
            except errors.RetryWithNewPacks, e:
 
1701
                self._access.reload_or_raise(e)
 
1702
        # If there are still keys we've not yet found, we look in the fallback
 
1703
        # vfs, and hope to find them there.  Note that if the keys are found
 
1704
        # but had no changes or no content, the fallback may not return
 
1705
        # anything.
 
1706
        if keys and not self._fallback_vfs:
 
1707
            # XXX: strictly the second parameter is meant to be the file id
 
1708
            # but it's not easily accessible here.
 
1709
            raise RevisionNotPresent(keys, repr(self))
 
1710
        for source in self._fallback_vfs:
 
1711
            if not keys:
 
1712
                break
 
1713
            source_keys = set()
 
1714
            for line, key in source.iter_lines_added_or_present_in_keys(keys):
 
1715
                source_keys.add(key)
 
1716
                yield line, key
 
1717
            keys.difference_update(source_keys)
 
1718
        pb.update('Walking content.', total, total)
 
1719
 
 
1720
    def _make_line_delta(self, delta_seq, new_content):
 
1721
        """Generate a line delta from delta_seq and new_content."""
 
1722
        diff_hunks = []
 
1723
        for op in delta_seq.get_opcodes():
 
1724
            if op[0] == 'equal':
 
1725
                continue
 
1726
            diff_hunks.append((op[1], op[2], op[4]-op[3], new_content._lines[op[3]:op[4]]))
 
1727
        return diff_hunks
 
1728
 
 
1729
    def _merge_annotations(self, content, parents, parent_texts={},
 
1730
                           delta=None, annotated=None,
 
1731
                           left_matching_blocks=None):
 
1732
        """Merge annotations for content and generate deltas.
 
1733
 
 
1734
        This is done by comparing the annotations based on changes to the text
 
1735
        and generating a delta on the resulting full texts. If annotations are
 
1736
        not being created then a simple delta is created.
 
1737
        """
 
1738
        if left_matching_blocks is not None:
 
1739
            delta_seq = diff._PrematchedMatcher(left_matching_blocks)
 
1740
        else:
 
1741
            delta_seq = None
 
1742
        if annotated:
 
1743
            for parent_key in parents:
 
1744
                merge_content = self._get_content(parent_key, parent_texts)
 
1745
                if (parent_key == parents[0] and delta_seq is not None):
 
1746
                    seq = delta_seq
 
1747
                else:
 
1748
                    seq = patiencediff.PatienceSequenceMatcher(
 
1749
                        None, merge_content.text(), content.text())
 
1750
                for i, j, n in seq.get_matching_blocks():
 
1751
                    if n == 0:
 
1752
                        continue
 
1753
                    # this copies (origin, text) pairs across to the new
 
1754
                    # content for any line that matches the last-checked
 
1755
                    # parent.
 
1756
                    content._lines[j:j+n] = merge_content._lines[i:i+n]
 
1757
            # XXX: Robert says the following block is a workaround for a
 
1758
            # now-fixed bug and it can probably be deleted. -- mbp 20080618
 
1759
            if content._lines and content._lines[-1][1][-1] != '\n':
 
1760
                # The copied annotation was from a line without a trailing EOL,
 
1761
                # reinstate one for the content object, to ensure correct
 
1762
                # serialization.
 
1763
                line = content._lines[-1][1] + '\n'
 
1764
                content._lines[-1] = (content._lines[-1][0], line)
 
1765
        if delta:
 
1766
            if delta_seq is None:
 
1767
                reference_content = self._get_content(parents[0], parent_texts)
 
1768
                new_texts = content.text()
 
1769
                old_texts = reference_content.text()
 
1770
                delta_seq = patiencediff.PatienceSequenceMatcher(
 
1771
                                                 None, old_texts, new_texts)
 
1772
            return self._make_line_delta(delta_seq, content)
 
1773
 
 
1774
    def _parse_record(self, version_id, data):
 
1775
        """Parse an original format knit record.
 
1776
 
 
1777
        These have the last element of the key only present in the stored data.
 
1778
        """
 
1779
        rec, record_contents = self._parse_record_unchecked(data)
 
1780
        self._check_header_version(rec, version_id)
 
1781
        return record_contents, rec[3]
 
1782
 
 
1783
    def _parse_record_header(self, key, raw_data):
 
1784
        """Parse a record header for consistency.
 
1785
 
 
1786
        :return: the header and the decompressor stream.
 
1787
                 as (stream, header_record)
 
1788
        """
 
1789
        df = tuned_gzip.GzipFile(mode='rb', fileobj=StringIO(raw_data))
 
1790
        try:
 
1791
            # Current serialise
 
1792
            rec = self._check_header(key, df.readline())
 
1793
        except Exception, e:
 
1794
            raise KnitCorrupt(self,
 
1795
                              "While reading {%s} got %s(%s)"
 
1796
                              % (key, e.__class__.__name__, str(e)))
 
1797
        return df, rec
 
1798
 
 
1799
    def _parse_record_unchecked(self, data):
 
1800
        # profiling notes:
 
1801
        # 4168 calls in 2880 217 internal
 
1802
        # 4168 calls to _parse_record_header in 2121
 
1803
        # 4168 calls to readlines in 330
 
1804
        df = tuned_gzip.GzipFile(mode='rb', fileobj=StringIO(data))
 
1805
        try:
 
1806
            record_contents = df.readlines()
 
1807
        except Exception, e:
 
1808
            raise KnitCorrupt(self, "Corrupt compressed record %r, got %s(%s)" %
 
1809
                (data, e.__class__.__name__, str(e)))
 
1810
        header = record_contents.pop(0)
 
1811
        rec = self._split_header(header)
 
1812
        last_line = record_contents.pop()
 
1813
        if len(record_contents) != int(rec[2]):
 
1814
            raise KnitCorrupt(self,
 
1815
                              'incorrect number of lines %s != %s'
 
1816
                              ' for version {%s} %s'
 
1817
                              % (len(record_contents), int(rec[2]),
 
1818
                                 rec[1], record_contents))
 
1819
        if last_line != 'end %s\n' % rec[1]:
 
1820
            raise KnitCorrupt(self,
 
1821
                              'unexpected version end line %r, wanted %r'
 
1822
                              % (last_line, rec[1]))
 
1823
        df.close()
 
1824
        return rec, record_contents
 
1825
 
 
1826
    def _read_records_iter(self, records):
 
1827
        """Read text records from data file and yield result.
 
1828
 
 
1829
        The result will be returned in whatever is the fastest to read.
 
1830
        Not by the order requested. Also, multiple requests for the same
 
1831
        record will only yield 1 response.
 
1832
        :param records: A list of (key, access_memo) entries
 
1833
        :return: Yields (key, contents, digest) in the order
 
1834
                 read, not the order requested
 
1835
        """
 
1836
        if not records:
 
1837
            return
 
1838
 
 
1839
        # XXX: This smells wrong, IO may not be getting ordered right.
 
1840
        needed_records = sorted(set(records), key=operator.itemgetter(1))
 
1841
        if not needed_records:
 
1842
            return
 
1843
 
 
1844
        # The transport optimizes the fetching as well
 
1845
        # (ie, reads continuous ranges.)
 
1846
        raw_data = self._access.get_raw_records(
 
1847
            [index_memo for key, index_memo in needed_records])
 
1848
 
 
1849
        for (key, index_memo), data in \
 
1850
                izip(iter(needed_records), raw_data):
 
1851
            content, digest = self._parse_record(key[-1], data)
 
1852
            yield key, content, digest
 
1853
 
 
1854
    def _read_records_iter_raw(self, records):
 
1855
        """Read text records from data file and yield raw data.
 
1856
 
 
1857
        This unpacks enough of the text record to validate the id is
 
1858
        as expected but thats all.
 
1859
 
 
1860
        Each item the iterator yields is (key, bytes,
 
1861
            expected_sha1_of_full_text).
 
1862
        """
 
1863
        for key, data in self._read_records_iter_unchecked(records):
 
1864
            # validate the header (note that we can only use the suffix in
 
1865
            # current knit records).
 
1866
            df, rec = self._parse_record_header(key, data)
 
1867
            df.close()
 
1868
            yield key, data, rec[3]
 
1869
 
 
1870
    def _read_records_iter_unchecked(self, records):
 
1871
        """Read text records from data file and yield raw data.
 
1872
 
 
1873
        No validation is done.
 
1874
 
 
1875
        Yields tuples of (key, data).
 
1876
        """
 
1877
        # setup an iterator of the external records:
 
1878
        # uses readv so nice and fast we hope.
 
1879
        if len(records):
 
1880
            # grab the disk data needed.
 
1881
            needed_offsets = [index_memo for key, index_memo
 
1882
                                           in records]
 
1883
            raw_records = self._access.get_raw_records(needed_offsets)
 
1884
 
 
1885
        for key, index_memo in records:
 
1886
            data = raw_records.next()
 
1887
            yield key, data
 
1888
 
 
1889
    def _record_to_data(self, key, digest, lines, dense_lines=None):
 
1890
        """Convert key, digest, lines into a raw data block.
 
1891
 
 
1892
        :param key: The key of the record. Currently keys are always serialised
 
1893
            using just the trailing component.
 
1894
        :param dense_lines: The bytes of lines but in a denser form. For
 
1895
            instance, if lines is a list of 1000 bytestrings each ending in \n,
 
1896
            dense_lines may be a list with one line in it, containing all the
 
1897
            1000's lines and their \n's. Using dense_lines if it is already
 
1898
            known is a win because the string join to create bytes in this
 
1899
            function spends less time resizing the final string.
 
1900
        :return: (len, a StringIO instance with the raw data ready to read.)
 
1901
        """
 
1902
        # Note: using a string copy here increases memory pressure with e.g.
 
1903
        # ISO's, but it is about 3 seconds faster on a 1.2Ghz intel machine
 
1904
        # when doing the initial commit of a mozilla tree. RBC 20070921
 
1905
        bytes = ''.join(chain(
 
1906
            ["version %s %d %s\n" % (key[-1],
 
1907
                                     len(lines),
 
1908
                                     digest)],
 
1909
            dense_lines or lines,
 
1910
            ["end %s\n" % key[-1]]))
 
1911
        if type(bytes) != str:
 
1912
            raise AssertionError(
 
1913
                'data must be plain bytes was %s' % type(bytes))
 
1914
        if lines and lines[-1][-1] != '\n':
 
1915
            raise ValueError('corrupt lines value %r' % lines)
 
1916
        compressed_bytes = tuned_gzip.bytes_to_gzip(bytes)
 
1917
        return len(compressed_bytes), compressed_bytes
 
1918
 
 
1919
    def _split_header(self, line):
 
1920
        rec = line.split()
 
1921
        if len(rec) != 4:
 
1922
            raise KnitCorrupt(self,
 
1923
                              'unexpected number of elements in record header')
 
1924
        return rec
 
1925
 
 
1926
    def keys(self):
 
1927
        """See VersionedFiles.keys."""
 
1928
        if 'evil' in debug.debug_flags:
 
1929
            trace.mutter_callsite(2, "keys scales with size of history")
 
1930
        sources = [self._index] + self._fallback_vfs
 
1931
        result = set()
 
1932
        for source in sources:
 
1933
            result.update(source.keys())
 
1934
        return result
 
1935
 
 
1936
 
 
1937
class _ContentMapGenerator(object):
 
1938
    """Generate texts or expose raw deltas for a set of texts."""
 
1939
 
 
1940
    def _get_content(self, key):
 
1941
        """Get the content object for key."""
 
1942
        # Note that _get_content is only called when the _ContentMapGenerator
 
1943
        # has been constructed with just one key requested for reconstruction.
 
1944
        if key in self.nonlocal_keys:
 
1945
            record = self.get_record_stream().next()
 
1946
            # Create a content object on the fly
 
1947
            lines = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
 
1948
            return PlainKnitContent(lines, record.key)
 
1949
        else:
 
1950
            # local keys we can ask for directly
 
1951
            return self._get_one_work(key)
 
1952
 
 
1953
    def get_record_stream(self):
 
1954
        """Get a record stream for the keys requested during __init__."""
 
1955
        for record in self._work():
 
1956
            yield record
 
1957
 
 
1958
    def _work(self):
 
1959
        """Produce maps of text and KnitContents as dicts.
 
1960
 
 
1961
        :return: (text_map, content_map) where text_map contains the texts for
 
1962
            the requested versions and content_map contains the KnitContents.
 
1963
        """
 
1964
        # NB: By definition we never need to read remote sources unless texts
 
1965
        # are requested from them: we don't delta across stores - and we
 
1966
        # explicitly do not want to to prevent data loss situations.
 
1967
        if self.global_map is None:
 
1968
            self.global_map = self.vf.get_parent_map(self.keys)
 
1969
        nonlocal_keys = self.nonlocal_keys
 
1970
 
 
1971
        missing_keys = set(nonlocal_keys)
 
1972
        # Read from remote versioned file instances and provide to our caller.
 
1973
        for source in self.vf._fallback_vfs:
 
1974
            if not missing_keys:
 
1975
                break
 
1976
            # Loop over fallback repositories asking them for texts - ignore
 
1977
            # any missing from a particular fallback.
 
1978
            for record in source.get_record_stream(missing_keys,
 
1979
                'unordered', True):
 
1980
                if record.storage_kind == 'absent':
 
1981
                    # Not in thie particular stream, may be in one of the
 
1982
                    # other fallback vfs objects.
 
1983
                    continue
 
1984
                missing_keys.remove(record.key)
 
1985
                yield record
 
1986
 
 
1987
        self._raw_record_map = self.vf._get_record_map_unparsed(self.keys,
 
1988
            allow_missing=True)
 
1989
        first = True
 
1990
        for key in self.keys:
 
1991
            if key in self.nonlocal_keys:
 
1992
                continue
 
1993
            yield LazyKnitContentFactory(key, self.global_map[key], self, first)
 
1994
            first = False
 
1995
 
 
1996
    def _get_one_work(self, requested_key):
 
1997
        # Now, if we have calculated everything already, just return the
 
1998
        # desired text.
 
1999
        if requested_key in self._contents_map:
 
2000
            return self._contents_map[requested_key]
 
2001
        # To simplify things, parse everything at once - code that wants one text
 
2002
        # probably wants them all.
 
2003
        # FUTURE: This function could be improved for the 'extract many' case
 
2004
        # by tracking each component and only doing the copy when the number of
 
2005
        # children than need to apply delta's to it is > 1 or it is part of the
 
2006
        # final output.
 
2007
        multiple_versions = len(self.keys) != 1
 
2008
        if self._record_map is None:
 
2009
            self._record_map = self.vf._raw_map_to_record_map(
 
2010
                self._raw_record_map)
 
2011
        record_map = self._record_map
 
2012
        # raw_record_map is key:
 
2013
        # Have read and parsed records at this point.
 
2014
        for key in self.keys:
 
2015
            if key in self.nonlocal_keys:
 
2016
                # already handled
 
2017
                continue
 
2018
            components = []
 
2019
            cursor = key
 
2020
            while cursor is not None:
 
2021
                try:
 
2022
                    record, record_details, digest, next = record_map[cursor]
 
2023
                except KeyError:
 
2024
                    raise RevisionNotPresent(cursor, self)
 
2025
                components.append((cursor, record, record_details, digest))
 
2026
                cursor = next
 
2027
                if cursor in self._contents_map:
 
2028
                    # no need to plan further back
 
2029
                    components.append((cursor, None, None, None))
 
2030
                    break
 
2031
 
 
2032
            content = None
 
2033
            for (component_id, record, record_details,
 
2034
                 digest) in reversed(components):
 
2035
                if component_id in self._contents_map:
 
2036
                    content = self._contents_map[component_id]
 
2037
                else:
 
2038
                    content, delta = self._factory.parse_record(key[-1],
 
2039
                        record, record_details, content,
 
2040
                        copy_base_content=multiple_versions)
 
2041
                    if multiple_versions:
 
2042
                        self._contents_map[component_id] = content
 
2043
 
 
2044
            # digest here is the digest from the last applied component.
 
2045
            text = content.text()
 
2046
            actual_sha = sha_strings(text)
 
2047
            if actual_sha != digest:
 
2048
                raise SHA1KnitCorrupt(self, actual_sha, digest, key, text)
 
2049
        if multiple_versions:
 
2050
            return self._contents_map[requested_key]
 
2051
        else:
 
2052
            return content
 
2053
 
 
2054
    def _wire_bytes(self):
 
2055
        """Get the bytes to put on the wire for 'key'.
 
2056
 
 
2057
        The first collection of bytes asked for returns the serialised
 
2058
        raw_record_map and the additional details (key, parent) for key.
 
2059
        Subsequent calls return just the additional details (key, parent).
 
2060
        The wire storage_kind given for the first key is 'knit-delta-closure',
 
2061
        For subsequent keys it is 'knit-delta-closure-ref'.
 
2062
 
 
2063
        :param key: A key from the content generator.
 
2064
        :return: Bytes to put on the wire.
 
2065
        """
 
2066
        lines = []
 
2067
        # kind marker for dispatch on the far side,
 
2068
        lines.append('knit-delta-closure')
 
2069
        # Annotated or not
 
2070
        if self.vf._factory.annotated:
 
2071
            lines.append('annotated')
 
2072
        else:
 
2073
            lines.append('')
 
2074
        # then the list of keys
 
2075
        lines.append('\t'.join(['\x00'.join(key) for key in self.keys
 
2076
            if key not in self.nonlocal_keys]))
 
2077
        # then the _raw_record_map in serialised form:
 
2078
        map_byte_list = []
 
2079
        # for each item in the map:
 
2080
        # 1 line with key
 
2081
        # 1 line with parents if the key is to be yielded (None: for None, '' for ())
 
2082
        # one line with method
 
2083
        # one line with noeol
 
2084
        # one line with next ('' for None)
 
2085
        # one line with byte count of the record bytes
 
2086
        # the record bytes
 
2087
        for key, (record_bytes, (method, noeol), next) in \
 
2088
            self._raw_record_map.iteritems():
 
2089
            key_bytes = '\x00'.join(key)
 
2090
            parents = self.global_map.get(key, None)
 
2091
            if parents is None:
 
2092
                parent_bytes = 'None:'
 
2093
            else:
 
2094
                parent_bytes = '\t'.join('\x00'.join(key) for key in parents)
 
2095
            method_bytes = method
 
2096
            if noeol:
 
2097
                noeol_bytes = "T"
 
2098
            else:
 
2099
                noeol_bytes = "F"
 
2100
            if next:
 
2101
                next_bytes = '\x00'.join(next)
 
2102
            else:
 
2103
                next_bytes = ''
 
2104
            map_byte_list.append('%s\n%s\n%s\n%s\n%s\n%d\n%s' % (
 
2105
                key_bytes, parent_bytes, method_bytes, noeol_bytes, next_bytes,
 
2106
                len(record_bytes), record_bytes))
 
2107
        map_bytes = ''.join(map_byte_list)
 
2108
        lines.append(map_bytes)
 
2109
        bytes = '\n'.join(lines)
 
2110
        return bytes
 
2111
 
 
2112
 
 
2113
class _VFContentMapGenerator(_ContentMapGenerator):
 
2114
    """Content map generator reading from a VersionedFiles object."""
 
2115
 
 
2116
    def __init__(self, versioned_files, keys, nonlocal_keys=None,
 
2117
        global_map=None, raw_record_map=None):
 
2118
        """Create a _ContentMapGenerator.
 
2119
 
 
2120
        :param versioned_files: The versioned files that the texts are being
 
2121
            extracted from.
 
2122
        :param keys: The keys to produce content maps for.
 
2123
        :param nonlocal_keys: An iterable of keys(possibly intersecting keys)
 
2124
            which are known to not be in this knit, but rather in one of the
 
2125
            fallback knits.
 
2126
        :param global_map: The result of get_parent_map(keys) (or a supermap).
 
2127
            This is required if get_record_stream() is to be used.
 
2128
        :param raw_record_map: A unparsed raw record map to use for answering
 
2129
            contents.
 
2130
        """
 
2131
        # The vf to source data from
 
2132
        self.vf = versioned_files
 
2133
        # The keys desired
 
2134
        self.keys = list(keys)
 
2135
        # Keys known to be in fallback vfs objects
 
2136
        if nonlocal_keys is None:
 
2137
            self.nonlocal_keys = set()
 
2138
        else:
 
2139
            self.nonlocal_keys = frozenset(nonlocal_keys)
 
2140
        # Parents data for keys to be returned in get_record_stream
 
2141
        self.global_map = global_map
 
2142
        # The chunked lists for self.keys in text form
 
2143
        self._text_map = {}
 
2144
        # A cache of KnitContent objects used in extracting texts.
 
2145
        self._contents_map = {}
 
2146
        # All the knit records needed to assemble the requested keys as full
 
2147
        # texts.
 
2148
        self._record_map = None
 
2149
        if raw_record_map is None:
 
2150
            self._raw_record_map = self.vf._get_record_map_unparsed(keys,
 
2151
                allow_missing=True)
 
2152
        else:
 
2153
            self._raw_record_map = raw_record_map
 
2154
        # the factory for parsing records
 
2155
        self._factory = self.vf._factory
 
2156
 
 
2157
 
 
2158
class _NetworkContentMapGenerator(_ContentMapGenerator):
 
2159
    """Content map generator sourced from a network stream."""
 
2160
 
 
2161
    def __init__(self, bytes, line_end):
 
2162
        """Construct a _NetworkContentMapGenerator from a bytes block."""
 
2163
        self._bytes = bytes
 
2164
        self.global_map = {}
 
2165
        self._raw_record_map = {}
 
2166
        self._contents_map = {}
 
2167
        self._record_map = None
 
2168
        self.nonlocal_keys = []
 
2169
        # Get access to record parsing facilities
 
2170
        self.vf = KnitVersionedFiles(None, None)
 
2171
        start = line_end
 
2172
        # Annotated or not
 
2173
        line_end = bytes.find('\n', start)
 
2174
        line = bytes[start:line_end]
 
2175
        start = line_end + 1
 
2176
        if line == 'annotated':
 
2177
            self._factory = KnitAnnotateFactory()
 
2178
        else:
 
2179
            self._factory = KnitPlainFactory()
 
2180
        # list of keys to emit in get_record_stream
 
2181
        line_end = bytes.find('\n', start)
 
2182
        line = bytes[start:line_end]
 
2183
        start = line_end + 1
 
2184
        self.keys = [
 
2185
            tuple(segment.split('\x00')) for segment in line.split('\t')
 
2186
            if segment]
 
2187
        # now a loop until the end. XXX: It would be nice if this was just a
 
2188
        # bunch of the same records as get_record_stream(..., False) gives, but
 
2189
        # there is a decent sized gap stopping that at the moment.
 
2190
        end = len(bytes)
 
2191
        while start < end:
 
2192
            # 1 line with key
 
2193
            line_end = bytes.find('\n', start)
 
2194
            key = tuple(bytes[start:line_end].split('\x00'))
 
2195
            start = line_end + 1
 
2196
            # 1 line with parents (None: for None, '' for ())
 
2197
            line_end = bytes.find('\n', start)
 
2198
            line = bytes[start:line_end]
 
2199
            if line == 'None:':
 
2200
                parents = None
 
2201
            else:
 
2202
                parents = tuple(
 
2203
                    [tuple(segment.split('\x00')) for segment in line.split('\t')
 
2204
                     if segment])
 
2205
            self.global_map[key] = parents
 
2206
            start = line_end + 1
 
2207
            # one line with method
 
2208
            line_end = bytes.find('\n', start)
 
2209
            line = bytes[start:line_end]
 
2210
            method = line
 
2211
            start = line_end + 1
 
2212
            # one line with noeol
 
2213
            line_end = bytes.find('\n', start)
 
2214
            line = bytes[start:line_end]
 
2215
            noeol = line == "T"
 
2216
            start = line_end + 1
 
2217
            # one line with next ('' for None)
 
2218
            line_end = bytes.find('\n', start)
 
2219
            line = bytes[start:line_end]
 
2220
            if not line:
 
2221
                next = None
 
2222
            else:
 
2223
                next = tuple(bytes[start:line_end].split('\x00'))
 
2224
            start = line_end + 1
 
2225
            # one line with byte count of the record bytes
 
2226
            line_end = bytes.find('\n', start)
 
2227
            line = bytes[start:line_end]
 
2228
            count = int(line)
 
2229
            start = line_end + 1
 
2230
            # the record bytes
 
2231
            record_bytes = bytes[start:start+count]
 
2232
            start = start + count
 
2233
            # put it in the map
 
2234
            self._raw_record_map[key] = (record_bytes, (method, noeol), next)
 
2235
 
 
2236
    def get_record_stream(self):
 
2237
        """Get a record stream for for keys requested by the bytestream."""
 
2238
        first = True
 
2239
        for key in self.keys:
 
2240
            yield LazyKnitContentFactory(key, self.global_map[key], self, first)
 
2241
            first = False
 
2242
 
 
2243
    def _wire_bytes(self):
 
2244
        return self._bytes
 
2245
 
 
2246
 
 
2247
class _KndxIndex(object):
 
2248
    """Manages knit index files
 
2249
 
 
2250
    The index is kept in memory and read on startup, to enable
 
2251
    fast lookups of revision information.  The cursor of the index
 
2252
    file is always pointing to the end, making it easy to append
 
2253
    entries.
 
2254
 
 
2255
    _cache is a cache for fast mapping from version id to a Index
 
2256
    object.
 
2257
 
 
2258
    _history is a cache for fast mapping from indexes to version ids.
 
2259
 
 
2260
    The index data format is dictionary compressed when it comes to
 
2261
    parent references; a index entry may only have parents that with a
 
2262
    lover index number.  As a result, the index is topological sorted.
 
2263
 
 
2264
    Duplicate entries may be written to the index for a single version id
 
2265
    if this is done then the latter one completely replaces the former:
 
2266
    this allows updates to correct version and parent information.
 
2267
    Note that the two entries may share the delta, and that successive
 
2268
    annotations and references MUST point to the first entry.
 
2269
 
 
2270
    The index file on disc contains a header, followed by one line per knit
 
2271
    record. The same revision can be present in an index file more than once.
 
2272
    The first occurrence gets assigned a sequence number starting from 0.
 
2273
 
 
2274
    The format of a single line is
 
2275
    REVISION_ID FLAGS BYTE_OFFSET LENGTH( PARENT_ID|PARENT_SEQUENCE_ID)* :\n
 
2276
    REVISION_ID is a utf8-encoded revision id
 
2277
    FLAGS is a comma separated list of flags about the record. Values include
 
2278
        no-eol, line-delta, fulltext.
 
2279
    BYTE_OFFSET is the ascii representation of the byte offset in the data file
 
2280
        that the the compressed data starts at.
 
2281
    LENGTH is the ascii representation of the length of the data file.
 
2282
    PARENT_ID a utf-8 revision id prefixed by a '.' that is a parent of
 
2283
        REVISION_ID.
 
2284
    PARENT_SEQUENCE_ID the ascii representation of the sequence number of a
 
2285
        revision id already in the knit that is a parent of REVISION_ID.
 
2286
    The ' :' marker is the end of record marker.
 
2287
 
 
2288
    partial writes:
 
2289
    when a write is interrupted to the index file, it will result in a line
 
2290
    that does not end in ' :'. If the ' :' is not present at the end of a line,
 
2291
    or at the end of the file, then the record that is missing it will be
 
2292
    ignored by the parser.
 
2293
 
 
2294
    When writing new records to the index file, the data is preceded by '\n'
 
2295
    to ensure that records always start on new lines even if the last write was
 
2296
    interrupted. As a result its normal for the last line in the index to be
 
2297
    missing a trailing newline. One can be added with no harmful effects.
 
2298
 
 
2299
    :ivar _kndx_cache: dict from prefix to the old state of KnitIndex objects,
 
2300
        where prefix is e.g. the (fileid,) for .texts instances or () for
 
2301
        constant-mapped things like .revisions, and the old state is
 
2302
        tuple(history_vector, cache_dict).  This is used to prevent having an
 
2303
        ABI change with the C extension that reads .kndx files.
 
2304
    """
 
2305
 
 
2306
    HEADER = "# bzr knit index 8\n"
 
2307
 
 
2308
    def __init__(self, transport, mapper, get_scope, allow_writes, is_locked):
 
2309
        """Create a _KndxIndex on transport using mapper."""
 
2310
        self._transport = transport
 
2311
        self._mapper = mapper
 
2312
        self._get_scope = get_scope
 
2313
        self._allow_writes = allow_writes
 
2314
        self._is_locked = is_locked
 
2315
        self._reset_cache()
 
2316
        self.has_graph = True
 
2317
 
 
2318
    def add_records(self, records, random_id=False, missing_compression_parents=False):
 
2319
        """Add multiple records to the index.
 
2320
 
 
2321
        :param records: a list of tuples:
 
2322
                         (key, options, access_memo, parents).
 
2323
        :param random_id: If True the ids being added were randomly generated
 
2324
            and no check for existence will be performed.
 
2325
        :param missing_compression_parents: If True the records being added are
 
2326
            only compressed against texts already in the index (or inside
 
2327
            records). If False the records all refer to unavailable texts (or
 
2328
            texts inside records) as compression parents.
 
2329
        """
 
2330
        if missing_compression_parents:
 
2331
            # It might be nice to get the edge of the records. But keys isn't
 
2332
            # _wrong_.
 
2333
            keys = sorted(record[0] for record in records)
 
2334
            raise errors.RevisionNotPresent(keys, self)
 
2335
        paths = {}
 
2336
        for record in records:
 
2337
            key = record[0]
 
2338
            prefix = key[:-1]
 
2339
            path = self._mapper.map(key) + '.kndx'
 
2340
            path_keys = paths.setdefault(path, (prefix, []))
 
2341
            path_keys[1].append(record)
 
2342
        for path in sorted(paths):
 
2343
            prefix, path_keys = paths[path]
 
2344
            self._load_prefixes([prefix])
 
2345
            lines = []
 
2346
            orig_history = self._kndx_cache[prefix][1][:]
 
2347
            orig_cache = self._kndx_cache[prefix][0].copy()
 
2348
 
 
2349
            try:
 
2350
                for key, options, (_, pos, size), parents in path_keys:
 
2351
                    if parents is None:
 
2352
                        # kndx indices cannot be parentless.
 
2353
                        parents = ()
 
2354
                    line = "\n%s %s %s %s %s :" % (
 
2355
                        key[-1], ','.join(options), pos, size,
 
2356
                        self._dictionary_compress(parents))
 
2357
                    if type(line) != str:
 
2358
                        raise AssertionError(
 
2359
                            'data must be utf8 was %s' % type(line))
 
2360
                    lines.append(line)
 
2361
                    self._cache_key(key, options, pos, size, parents)
 
2362
                if len(orig_history):
 
2363
                    self._transport.append_bytes(path, ''.join(lines))
 
2364
                else:
 
2365
                    self._init_index(path, lines)
 
2366
            except:
 
2367
                # If any problems happen, restore the original values and re-raise
 
2368
                self._kndx_cache[prefix] = (orig_cache, orig_history)
 
2369
                raise
 
2370
 
 
2371
    def scan_unvalidated_index(self, graph_index):
 
2372
        """See _KnitGraphIndex.scan_unvalidated_index."""
 
2373
        # Because kndx files do not support atomic insertion via separate index
 
2374
        # files, they do not support this method.
 
2375
        raise NotImplementedError(self.scan_unvalidated_index)
 
2376
 
 
2377
    def get_missing_compression_parents(self):
 
2378
        """See _KnitGraphIndex.get_missing_compression_parents."""
 
2379
        # Because kndx files do not support atomic insertion via separate index
 
2380
        # files, they do not support this method.
 
2381
        raise NotImplementedError(self.get_missing_compression_parents)
 
2382
 
 
2383
    def _cache_key(self, key, options, pos, size, parent_keys):
 
2384
        """Cache a version record in the history array and index cache.
 
2385
 
 
2386
        This is inlined into _load_data for performance. KEEP IN SYNC.
 
2387
        (It saves 60ms, 25% of the __init__ overhead on local 4000 record
 
2388
         indexes).
 
2389
        """
 
2390
        prefix = key[:-1]
 
2391
        version_id = key[-1]
 
2392
        # last-element only for compatibilty with the C load_data.
 
2393
        parents = tuple(parent[-1] for parent in parent_keys)
 
2394
        for parent in parent_keys:
 
2395
            if parent[:-1] != prefix:
 
2396
                raise ValueError("mismatched prefixes for %r, %r" % (
 
2397
                    key, parent_keys))
 
2398
        cache, history = self._kndx_cache[prefix]
 
2399
        # only want the _history index to reference the 1st index entry
 
2400
        # for version_id
 
2401
        if version_id not in cache:
 
2402
            index = len(history)
 
2403
            history.append(version_id)
 
2404
        else:
 
2405
            index = cache[version_id][5]
 
2406
        cache[version_id] = (version_id,
 
2407
                                   options,
 
2408
                                   pos,
 
2409
                                   size,
 
2410
                                   parents,
 
2411
                                   index)
 
2412
 
 
2413
    def check_header(self, fp):
 
2414
        line = fp.readline()
 
2415
        if line == '':
 
2416
            # An empty file can actually be treated as though the file doesn't
 
2417
            # exist yet.
 
2418
            raise errors.NoSuchFile(self)
 
2419
        if line != self.HEADER:
 
2420
            raise KnitHeaderError(badline=line, filename=self)
 
2421
 
 
2422
    def _check_read(self):
 
2423
        if not self._is_locked():
 
2424
            raise errors.ObjectNotLocked(self)
 
2425
        if self._get_scope() != self._scope:
 
2426
            self._reset_cache()
 
2427
 
 
2428
    def _check_write_ok(self):
 
2429
        """Assert if not writes are permitted."""
 
2430
        if not self._is_locked():
 
2431
            raise errors.ObjectNotLocked(self)
 
2432
        if self._get_scope() != self._scope:
 
2433
            self._reset_cache()
 
2434
        if self._mode != 'w':
 
2435
            raise errors.ReadOnlyObjectDirtiedError(self)
 
2436
 
 
2437
    def get_build_details(self, keys):
 
2438
        """Get the method, index_memo and compression parent for keys.
 
2439
 
 
2440
        Ghosts are omitted from the result.
 
2441
 
 
2442
        :param keys: An iterable of keys.
 
2443
        :return: A dict of key:(index_memo, compression_parent, parents,
 
2444
            record_details).
 
2445
            index_memo
 
2446
                opaque structure to pass to read_records to extract the raw
 
2447
                data
 
2448
            compression_parent
 
2449
                Content that this record is built upon, may be None
 
2450
            parents
 
2451
                Logical parents of this node
 
2452
            record_details
 
2453
                extra information about the content which needs to be passed to
 
2454
                Factory.parse_record
 
2455
        """
 
2456
        parent_map = self.get_parent_map(keys)
 
2457
        result = {}
 
2458
        for key in keys:
 
2459
            if key not in parent_map:
 
2460
                continue # Ghost
 
2461
            method = self.get_method(key)
 
2462
            parents = parent_map[key]
 
2463
            if method == 'fulltext':
 
2464
                compression_parent = None
 
2465
            else:
 
2466
                compression_parent = parents[0]
 
2467
            noeol = 'no-eol' in self.get_options(key)
 
2468
            index_memo = self.get_position(key)
 
2469
            result[key] = (index_memo, compression_parent,
 
2470
                                  parents, (method, noeol))
 
2471
        return result
 
2472
 
 
2473
    def get_method(self, key):
 
2474
        """Return compression method of specified key."""
 
2475
        options = self.get_options(key)
 
2476
        if 'fulltext' in options:
 
2477
            return 'fulltext'
 
2478
        elif 'line-delta' in options:
 
2479
            return 'line-delta'
 
2480
        else:
 
2481
            raise errors.KnitIndexUnknownMethod(self, options)
 
2482
 
 
2483
    def get_options(self, key):
 
2484
        """Return a list representing options.
 
2485
 
 
2486
        e.g. ['foo', 'bar']
 
2487
        """
 
2488
        prefix, suffix = self._split_key(key)
 
2489
        self._load_prefixes([prefix])
 
2490
        try:
 
2491
            return self._kndx_cache[prefix][0][suffix][1]
 
2492
        except KeyError:
 
2493
            raise RevisionNotPresent(key, self)
 
2494
 
 
2495
    def get_parent_map(self, keys):
 
2496
        """Get a map of the parents of keys.
 
2497
 
 
2498
        :param keys: The keys to look up parents for.
 
2499
        :return: A mapping from keys to parents. Absent keys are absent from
 
2500
            the mapping.
 
2501
        """
 
2502
        # Parse what we need to up front, this potentially trades off I/O
 
2503
        # locality (.kndx and .knit in the same block group for the same file
 
2504
        # id) for less checking in inner loops.
 
2505
        prefixes = set(key[:-1] for key in keys)
 
2506
        self._load_prefixes(prefixes)
 
2507
        result = {}
 
2508
        for key in keys:
 
2509
            prefix = key[:-1]
 
2510
            try:
 
2511
                suffix_parents = self._kndx_cache[prefix][0][key[-1]][4]
 
2512
            except KeyError:
 
2513
                pass
 
2514
            else:
 
2515
                result[key] = tuple(prefix + (suffix,) for
 
2516
                    suffix in suffix_parents)
 
2517
        return result
 
2518
 
 
2519
    def get_position(self, key):
 
2520
        """Return details needed to access the version.
 
2521
 
 
2522
        :return: a tuple (key, data position, size) to hand to the access
 
2523
            logic to get the record.
 
2524
        """
 
2525
        prefix, suffix = self._split_key(key)
 
2526
        self._load_prefixes([prefix])
 
2527
        entry = self._kndx_cache[prefix][0][suffix]
 
2528
        return key, entry[2], entry[3]
 
2529
 
 
2530
    has_key = _mod_index._has_key_from_parent_map
 
2531
 
 
2532
    def _init_index(self, path, extra_lines=[]):
 
2533
        """Initialize an index."""
 
2534
        sio = StringIO()
 
2535
        sio.write(self.HEADER)
 
2536
        sio.writelines(extra_lines)
 
2537
        sio.seek(0)
 
2538
        self._transport.put_file_non_atomic(path, sio,
 
2539
                            create_parent_dir=True)
 
2540
                           # self._create_parent_dir)
 
2541
                           # mode=self._file_mode,
 
2542
                           # dir_mode=self._dir_mode)
 
2543
 
 
2544
    def keys(self):
 
2545
        """Get all the keys in the collection.
 
2546
 
 
2547
        The keys are not ordered.
 
2548
        """
 
2549
        result = set()
 
2550
        # Identify all key prefixes.
 
2551
        # XXX: A bit hacky, needs polish.
 
2552
        if type(self._mapper) == ConstantMapper:
 
2553
            prefixes = [()]
 
2554
        else:
 
2555
            relpaths = set()
 
2556
            for quoted_relpath in self._transport.iter_files_recursive():
 
2557
                path, ext = os.path.splitext(quoted_relpath)
 
2558
                relpaths.add(path)
 
2559
            prefixes = [self._mapper.unmap(path) for path in relpaths]
 
2560
        self._load_prefixes(prefixes)
 
2561
        for prefix in prefixes:
 
2562
            for suffix in self._kndx_cache[prefix][1]:
 
2563
                result.add(prefix + (suffix,))
 
2564
        return result
 
2565
 
 
2566
    def _load_prefixes(self, prefixes):
 
2567
        """Load the indices for prefixes."""
 
2568
        self._check_read()
 
2569
        for prefix in prefixes:
 
2570
            if prefix not in self._kndx_cache:
 
2571
                # the load_data interface writes to these variables.
 
2572
                self._cache = {}
 
2573
                self._history = []
 
2574
                self._filename = prefix
 
2575
                try:
 
2576
                    path = self._mapper.map(prefix) + '.kndx'
 
2577
                    fp = self._transport.get(path)
 
2578
                    try:
 
2579
                        # _load_data may raise NoSuchFile if the target knit is
 
2580
                        # completely empty.
 
2581
                        _load_data(self, fp)
 
2582
                    finally:
 
2583
                        fp.close()
 
2584
                    self._kndx_cache[prefix] = (self._cache, self._history)
 
2585
                    del self._cache
 
2586
                    del self._filename
 
2587
                    del self._history
 
2588
                except NoSuchFile:
 
2589
                    self._kndx_cache[prefix] = ({}, [])
 
2590
                    if type(self._mapper) == ConstantMapper:
 
2591
                        # preserve behaviour for revisions.kndx etc.
 
2592
                        self._init_index(path)
 
2593
                    del self._cache
 
2594
                    del self._filename
 
2595
                    del self._history
 
2596
 
 
2597
    missing_keys = _mod_index._missing_keys_from_parent_map
 
2598
 
 
2599
    def _partition_keys(self, keys):
 
2600
        """Turn keys into a dict of prefix:suffix_list."""
 
2601
        result = {}
 
2602
        for key in keys:
 
2603
            prefix_keys = result.setdefault(key[:-1], [])
 
2604
            prefix_keys.append(key[-1])
 
2605
        return result
 
2606
 
 
2607
    def _dictionary_compress(self, keys):
 
2608
        """Dictionary compress keys.
 
2609
 
 
2610
        :param keys: The keys to generate references to.
 
2611
        :return: A string representation of keys. keys which are present are
 
2612
            dictionary compressed, and others are emitted as fulltext with a
 
2613
            '.' prefix.
 
2614
        """
 
2615
        if not keys:
 
2616
            return ''
 
2617
        result_list = []
 
2618
        prefix = keys[0][:-1]
 
2619
        cache = self._kndx_cache[prefix][0]
 
2620
        for key in keys:
 
2621
            if key[:-1] != prefix:
 
2622
                # kndx indices cannot refer across partitioned storage.
 
2623
                raise ValueError("mismatched prefixes for %r" % keys)
 
2624
            if key[-1] in cache:
 
2625
                # -- inlined lookup() --
 
2626
                result_list.append(str(cache[key[-1]][5]))
 
2627
                # -- end lookup () --
 
2628
            else:
 
2629
                result_list.append('.' + key[-1])
 
2630
        return ' '.join(result_list)
 
2631
 
 
2632
    def _reset_cache(self):
 
2633
        # Possibly this should be a LRU cache. A dictionary from key_prefix to
 
2634
        # (cache_dict, history_vector) for parsed kndx files.
 
2635
        self._kndx_cache = {}
 
2636
        self._scope = self._get_scope()
 
2637
        allow_writes = self._allow_writes()
 
2638
        if allow_writes:
 
2639
            self._mode = 'w'
 
2640
        else:
 
2641
            self._mode = 'r'
 
2642
 
 
2643
    def _sort_keys_by_io(self, keys, positions):
 
2644
        """Figure out an optimal order to read the records for the given keys.
 
2645
 
 
2646
        Sort keys, grouped by index and sorted by position.
 
2647
 
 
2648
        :param keys: A list of keys whose records we want to read. This will be
 
2649
            sorted 'in-place'.
 
2650
        :param positions: A dict, such as the one returned by
 
2651
            _get_components_positions()
 
2652
        :return: None
 
2653
        """
 
2654
        def get_sort_key(key):
 
2655
            index_memo = positions[key][1]
 
2656
            # Group by prefix and position. index_memo[0] is the key, so it is
 
2657
            # (file_id, revision_id) and we don't want to sort on revision_id,
 
2658
            # index_memo[1] is the position, and index_memo[2] is the size,
 
2659
            # which doesn't matter for the sort
 
2660
            return index_memo[0][:-1], index_memo[1]
 
2661
        return keys.sort(key=get_sort_key)
 
2662
 
 
2663
    _get_total_build_size = _get_total_build_size
 
2664
 
 
2665
    def _split_key(self, key):
 
2666
        """Split key into a prefix and suffix."""
 
2667
        return key[:-1], key[-1]
 
2668
 
 
2669
 
 
2670
class _KnitGraphIndex(object):
 
2671
    """A KnitVersionedFiles index layered on GraphIndex."""
 
2672
 
 
2673
    def __init__(self, graph_index, is_locked, deltas=False, parents=True,
 
2674
        add_callback=None):
 
2675
        """Construct a KnitGraphIndex on a graph_index.
 
2676
 
 
2677
        :param graph_index: An implementation of bzrlib.index.GraphIndex.
 
2678
        :param is_locked: A callback to check whether the object should answer
 
2679
            queries.
 
2680
        :param deltas: Allow delta-compressed records.
 
2681
        :param parents: If True, record knits parents, if not do not record
 
2682
            parents.
 
2683
        :param add_callback: If not None, allow additions to the index and call
 
2684
            this callback with a list of added GraphIndex nodes:
 
2685
            [(node, value, node_refs), ...]
 
2686
        :param is_locked: A callback, returns True if the index is locked and
 
2687
            thus usable.
 
2688
        """
 
2689
        self._add_callback = add_callback
 
2690
        self._graph_index = graph_index
 
2691
        self._deltas = deltas
 
2692
        self._parents = parents
 
2693
        if deltas and not parents:
 
2694
            # XXX: TODO: Delta tree and parent graph should be conceptually
 
2695
            # separate.
 
2696
            raise KnitCorrupt(self, "Cannot do delta compression without "
 
2697
                "parent tracking.")
 
2698
        self.has_graph = parents
 
2699
        self._is_locked = is_locked
 
2700
        self._missing_compression_parents = set()
 
2701
 
 
2702
    def __repr__(self):
 
2703
        return "%s(%r)" % (self.__class__.__name__, self._graph_index)
 
2704
 
 
2705
    def add_records(self, records, random_id=False,
 
2706
        missing_compression_parents=False):
 
2707
        """Add multiple records to the index.
 
2708
 
 
2709
        This function does not insert data into the Immutable GraphIndex
 
2710
        backing the KnitGraphIndex, instead it prepares data for insertion by
 
2711
        the caller and checks that it is safe to insert then calls
 
2712
        self._add_callback with the prepared GraphIndex nodes.
 
2713
 
 
2714
        :param records: a list of tuples:
 
2715
                         (key, options, access_memo, parents).
 
2716
        :param random_id: If True the ids being added were randomly generated
 
2717
            and no check for existence will be performed.
 
2718
        :param missing_compression_parents: If True the records being added are
 
2719
            only compressed against texts already in the index (or inside
 
2720
            records). If False the records all refer to unavailable texts (or
 
2721
            texts inside records) as compression parents.
 
2722
        """
 
2723
        if not self._add_callback:
 
2724
            raise errors.ReadOnlyError(self)
 
2725
        # we hope there are no repositories with inconsistent parentage
 
2726
        # anymore.
 
2727
 
 
2728
        keys = {}
 
2729
        compression_parents = set()
 
2730
        for (key, options, access_memo, parents) in records:
 
2731
            if self._parents:
 
2732
                parents = tuple(parents)
 
2733
            index, pos, size = access_memo
 
2734
            if 'no-eol' in options:
 
2735
                value = 'N'
 
2736
            else:
 
2737
                value = ' '
 
2738
            value += "%d %d" % (pos, size)
 
2739
            if not self._deltas:
 
2740
                if 'line-delta' in options:
 
2741
                    raise KnitCorrupt(self, "attempt to add line-delta in non-delta knit")
 
2742
            if self._parents:
 
2743
                if self._deltas:
 
2744
                    if 'line-delta' in options:
 
2745
                        node_refs = (parents, (parents[0],))
 
2746
                        if missing_compression_parents:
 
2747
                            compression_parents.add(parents[0])
 
2748
                    else:
 
2749
                        node_refs = (parents, ())
 
2750
                else:
 
2751
                    node_refs = (parents, )
 
2752
            else:
 
2753
                if parents:
 
2754
                    raise KnitCorrupt(self, "attempt to add node with parents "
 
2755
                        "in parentless index.")
 
2756
                node_refs = ()
 
2757
            keys[key] = (value, node_refs)
 
2758
        # check for dups
 
2759
        if not random_id:
 
2760
            present_nodes = self._get_entries(keys)
 
2761
            for (index, key, value, node_refs) in present_nodes:
 
2762
                if (value[0] != keys[key][0][0] or
 
2763
                    node_refs[:1] != keys[key][1][:1]):
 
2764
                    raise KnitCorrupt(self, "inconsistent details in add_records"
 
2765
                        ": %s %s" % ((value, node_refs), keys[key]))
 
2766
                del keys[key]
 
2767
        result = []
 
2768
        if self._parents:
 
2769
            for key, (value, node_refs) in keys.iteritems():
 
2770
                result.append((key, value, node_refs))
 
2771
        else:
 
2772
            for key, (value, node_refs) in keys.iteritems():
 
2773
                result.append((key, value))
 
2774
        self._add_callback(result)
 
2775
        if missing_compression_parents:
 
2776
            # This may appear to be incorrect (it does not check for
 
2777
            # compression parents that are in the existing graph index),
 
2778
            # but such records won't have been buffered, so this is
 
2779
            # actually correct: every entry when
 
2780
            # missing_compression_parents==True either has a missing parent, or
 
2781
            # a parent that is one of the keys in records.
 
2782
            compression_parents.difference_update(keys)
 
2783
            self._missing_compression_parents.update(compression_parents)
 
2784
        # Adding records may have satisfied missing compression parents.
 
2785
        self._missing_compression_parents.difference_update(keys)
 
2786
 
 
2787
    def scan_unvalidated_index(self, graph_index):
 
2788
        """Inform this _KnitGraphIndex that there is an unvalidated index.
 
2789
 
 
2790
        This allows this _KnitGraphIndex to keep track of any missing
 
2791
        compression parents we may want to have filled in to make those
 
2792
        indices valid.
 
2793
 
 
2794
        :param graph_index: A GraphIndex
 
2795
        """
 
2796
        if self._deltas:
 
2797
            new_missing = graph_index.external_references(ref_list_num=1)
 
2798
            new_missing.difference_update(self.get_parent_map(new_missing))
 
2799
            self._missing_compression_parents.update(new_missing)
 
2800
 
 
2801
    def get_missing_compression_parents(self):
 
2802
        """Return the keys of missing compression parents.
 
2803
 
 
2804
        Missing compression parents occur when a record stream was missing
 
2805
        basis texts, or a index was scanned that had missing basis texts.
 
2806
        """
 
2807
        return frozenset(self._missing_compression_parents)
 
2808
 
 
2809
    def _check_read(self):
 
2810
        """raise if reads are not permitted."""
 
2811
        if not self._is_locked():
 
2812
            raise errors.ObjectNotLocked(self)
 
2813
 
 
2814
    def _check_write_ok(self):
 
2815
        """Assert if writes are not permitted."""
 
2816
        if not self._is_locked():
 
2817
            raise errors.ObjectNotLocked(self)
 
2818
 
 
2819
    def _compression_parent(self, an_entry):
 
2820
        # return the key that an_entry is compressed against, or None
 
2821
        # Grab the second parent list (as deltas implies parents currently)
 
2822
        compression_parents = an_entry[3][1]
 
2823
        if not compression_parents:
 
2824
            return None
 
2825
        if len(compression_parents) != 1:
 
2826
            raise AssertionError(
 
2827
                "Too many compression parents: %r" % compression_parents)
 
2828
        return compression_parents[0]
 
2829
 
 
2830
    def get_build_details(self, keys):
 
2831
        """Get the method, index_memo and compression parent for version_ids.
 
2832
 
 
2833
        Ghosts are omitted from the result.
 
2834
 
 
2835
        :param keys: An iterable of keys.
 
2836
        :return: A dict of key:
 
2837
            (index_memo, compression_parent, parents, record_details).
 
2838
            index_memo
 
2839
                opaque structure to pass to read_records to extract the raw
 
2840
                data
 
2841
            compression_parent
 
2842
                Content that this record is built upon, may be None
 
2843
            parents
 
2844
                Logical parents of this node
 
2845
            record_details
 
2846
                extra information about the content which needs to be passed to
 
2847
                Factory.parse_record
 
2848
        """
 
2849
        self._check_read()
 
2850
        result = {}
 
2851
        entries = self._get_entries(keys, False)
 
2852
        for entry in entries:
 
2853
            key = entry[1]
 
2854
            if not self._parents:
 
2855
                parents = ()
 
2856
            else:
 
2857
                parents = entry[3][0]
 
2858
            if not self._deltas:
 
2859
                compression_parent_key = None
 
2860
            else:
 
2861
                compression_parent_key = self._compression_parent(entry)
 
2862
            noeol = (entry[2][0] == 'N')
 
2863
            if compression_parent_key:
 
2864
                method = 'line-delta'
 
2865
            else:
 
2866
                method = 'fulltext'
 
2867
            result[key] = (self._node_to_position(entry),
 
2868
                                  compression_parent_key, parents,
 
2869
                                  (method, noeol))
 
2870
        return result
 
2871
 
 
2872
    def _get_entries(self, keys, check_present=False):
 
2873
        """Get the entries for keys.
 
2874
 
 
2875
        :param keys: An iterable of index key tuples.
 
2876
        """
 
2877
        keys = set(keys)
 
2878
        found_keys = set()
 
2879
        if self._parents:
 
2880
            for node in self._graph_index.iter_entries(keys):
 
2881
                yield node
 
2882
                found_keys.add(node[1])
 
2883
        else:
 
2884
            # adapt parentless index to the rest of the code.
 
2885
            for node in self._graph_index.iter_entries(keys):
 
2886
                yield node[0], node[1], node[2], ()
 
2887
                found_keys.add(node[1])
 
2888
        if check_present:
 
2889
            missing_keys = keys.difference(found_keys)
 
2890
            if missing_keys:
 
2891
                raise RevisionNotPresent(missing_keys.pop(), self)
 
2892
 
 
2893
    def get_method(self, key):
 
2894
        """Return compression method of specified key."""
 
2895
        return self._get_method(self._get_node(key))
 
2896
 
 
2897
    def _get_method(self, node):
 
2898
        if not self._deltas:
 
2899
            return 'fulltext'
 
2900
        if self._compression_parent(node):
 
2901
            return 'line-delta'
 
2902
        else:
 
2903
            return 'fulltext'
 
2904
 
 
2905
    def _get_node(self, key):
 
2906
        try:
 
2907
            return list(self._get_entries([key]))[0]
 
2908
        except IndexError:
 
2909
            raise RevisionNotPresent(key, self)
 
2910
 
 
2911
    def get_options(self, key):
 
2912
        """Return a list representing options.
 
2913
 
 
2914
        e.g. ['foo', 'bar']
 
2915
        """
 
2916
        node = self._get_node(key)
 
2917
        options = [self._get_method(node)]
 
2918
        if node[2][0] == 'N':
 
2919
            options.append('no-eol')
 
2920
        return options
 
2921
 
 
2922
    def get_parent_map(self, keys):
 
2923
        """Get a map of the parents of keys.
 
2924
 
 
2925
        :param keys: The keys to look up parents for.
 
2926
        :return: A mapping from keys to parents. Absent keys are absent from
 
2927
            the mapping.
 
2928
        """
 
2929
        self._check_read()
 
2930
        nodes = self._get_entries(keys)
 
2931
        result = {}
 
2932
        if self._parents:
 
2933
            for node in nodes:
 
2934
                result[node[1]] = node[3][0]
 
2935
        else:
 
2936
            for node in nodes:
 
2937
                result[node[1]] = None
 
2938
        return result
 
2939
 
 
2940
    def get_position(self, key):
 
2941
        """Return details needed to access the version.
 
2942
 
 
2943
        :return: a tuple (index, data position, size) to hand to the access
 
2944
            logic to get the record.
 
2945
        """
 
2946
        node = self._get_node(key)
 
2947
        return self._node_to_position(node)
 
2948
 
 
2949
    has_key = _mod_index._has_key_from_parent_map
 
2950
 
 
2951
    def keys(self):
 
2952
        """Get all the keys in the collection.
 
2953
 
 
2954
        The keys are not ordered.
 
2955
        """
 
2956
        self._check_read()
 
2957
        return [node[1] for node in self._graph_index.iter_all_entries()]
 
2958
 
 
2959
    missing_keys = _mod_index._missing_keys_from_parent_map
 
2960
 
 
2961
    def _node_to_position(self, node):
 
2962
        """Convert an index value to position details."""
 
2963
        bits = node[2][1:].split(' ')
 
2964
        return node[0], int(bits[0]), int(bits[1])
 
2965
 
 
2966
    def _sort_keys_by_io(self, keys, positions):
 
2967
        """Figure out an optimal order to read the records for the given keys.
 
2968
 
 
2969
        Sort keys, grouped by index and sorted by position.
 
2970
 
 
2971
        :param keys: A list of keys whose records we want to read. This will be
 
2972
            sorted 'in-place'.
 
2973
        :param positions: A dict, such as the one returned by
 
2974
            _get_components_positions()
 
2975
        :return: None
 
2976
        """
 
2977
        def get_index_memo(key):
 
2978
            # index_memo is at offset [1]. It is made up of (GraphIndex,
 
2979
            # position, size). GI is an object, which will be unique for each
 
2980
            # pack file. This causes us to group by pack file, then sort by
 
2981
            # position. Size doesn't matter, but it isn't worth breaking up the
 
2982
            # tuple.
 
2983
            return positions[key][1]
 
2984
        return keys.sort(key=get_index_memo)
 
2985
 
 
2986
    _get_total_build_size = _get_total_build_size
 
2987
 
 
2988
 
 
2989
class _KnitKeyAccess(object):
 
2990
    """Access to records in .knit files."""
 
2991
 
 
2992
    def __init__(self, transport, mapper):
 
2993
        """Create a _KnitKeyAccess with transport and mapper.
 
2994
 
 
2995
        :param transport: The transport the access object is rooted at.
 
2996
        :param mapper: The mapper used to map keys to .knit files.
 
2997
        """
 
2998
        self._transport = transport
 
2999
        self._mapper = mapper
 
3000
 
 
3001
    def add_raw_records(self, key_sizes, raw_data):
 
3002
        """Add raw knit bytes to a storage area.
 
3003
 
 
3004
        The data is spooled to the container writer in one bytes-record per
 
3005
        raw data item.
 
3006
 
 
3007
        :param sizes: An iterable of tuples containing the key and size of each
 
3008
            raw data segment.
 
3009
        :param raw_data: A bytestring containing the data.
 
3010
        :return: A list of memos to retrieve the record later. Each memo is an
 
3011
            opaque index memo. For _KnitKeyAccess the memo is (key, pos,
 
3012
            length), where the key is the record key.
 
3013
        """
 
3014
        if type(raw_data) != str:
 
3015
            raise AssertionError(
 
3016
                'data must be plain bytes was %s' % type(raw_data))
 
3017
        result = []
 
3018
        offset = 0
 
3019
        # TODO: This can be tuned for writing to sftp and other servers where
 
3020
        # append() is relatively expensive by grouping the writes to each key
 
3021
        # prefix.
 
3022
        for key, size in key_sizes:
 
3023
            path = self._mapper.map(key)
 
3024
            try:
 
3025
                base = self._transport.append_bytes(path + '.knit',
 
3026
                    raw_data[offset:offset+size])
 
3027
            except errors.NoSuchFile:
 
3028
                self._transport.mkdir(osutils.dirname(path))
 
3029
                base = self._transport.append_bytes(path + '.knit',
 
3030
                    raw_data[offset:offset+size])
 
3031
            # if base == 0:
 
3032
            # chmod.
 
3033
            offset += size
 
3034
            result.append((key, base, size))
 
3035
        return result
 
3036
 
 
3037
    def get_raw_records(self, memos_for_retrieval):
 
3038
        """Get the raw bytes for a records.
 
3039
 
 
3040
        :param memos_for_retrieval: An iterable containing the access memo for
 
3041
            retrieving the bytes.
 
3042
        :return: An iterator over the bytes of the records.
 
3043
        """
 
3044
        # first pass, group into same-index request to minimise readv's issued.
 
3045
        request_lists = []
 
3046
        current_prefix = None
 
3047
        for (key, offset, length) in memos_for_retrieval:
 
3048
            if current_prefix == key[:-1]:
 
3049
                current_list.append((offset, length))
 
3050
            else:
 
3051
                if current_prefix is not None:
 
3052
                    request_lists.append((current_prefix, current_list))
 
3053
                current_prefix = key[:-1]
 
3054
                current_list = [(offset, length)]
 
3055
        # handle the last entry
 
3056
        if current_prefix is not None:
 
3057
            request_lists.append((current_prefix, current_list))
 
3058
        for prefix, read_vector in request_lists:
 
3059
            path = self._mapper.map(prefix) + '.knit'
 
3060
            for pos, data in self._transport.readv(path, read_vector):
 
3061
                yield data
 
3062
 
 
3063
 
 
3064
class _DirectPackAccess(object):
 
3065
    """Access to data in one or more packs with less translation."""
 
3066
 
 
3067
    def __init__(self, index_to_packs, reload_func=None):
 
3068
        """Create a _DirectPackAccess object.
 
3069
 
 
3070
        :param index_to_packs: A dict mapping index objects to the transport
 
3071
            and file names for obtaining data.
 
3072
        :param reload_func: A function to call if we determine that the pack
 
3073
            files have moved and we need to reload our caches. See
 
3074
            bzrlib.repo_fmt.pack_repo.AggregateIndex for more details.
 
3075
        """
 
3076
        self._container_writer = None
 
3077
        self._write_index = None
 
3078
        self._indices = index_to_packs
 
3079
        self._reload_func = reload_func
 
3080
 
 
3081
    def add_raw_records(self, key_sizes, raw_data):
 
3082
        """Add raw knit bytes to a storage area.
 
3083
 
 
3084
        The data is spooled to the container writer in one bytes-record per
 
3085
        raw data item.
 
3086
 
 
3087
        :param sizes: An iterable of tuples containing the key and size of each
 
3088
            raw data segment.
 
3089
        :param raw_data: A bytestring containing the data.
 
3090
        :return: A list of memos to retrieve the record later. Each memo is an
 
3091
            opaque index memo. For _DirectPackAccess the memo is (index, pos,
 
3092
            length), where the index field is the write_index object supplied
 
3093
            to the PackAccess object.
 
3094
        """
 
3095
        if type(raw_data) != str:
 
3096
            raise AssertionError(
 
3097
                'data must be plain bytes was %s' % type(raw_data))
 
3098
        result = []
 
3099
        offset = 0
 
3100
        for key, size in key_sizes:
 
3101
            p_offset, p_length = self._container_writer.add_bytes_record(
 
3102
                raw_data[offset:offset+size], [])
 
3103
            offset += size
 
3104
            result.append((self._write_index, p_offset, p_length))
 
3105
        return result
 
3106
 
 
3107
    def get_raw_records(self, memos_for_retrieval):
 
3108
        """Get the raw bytes for a records.
 
3109
 
 
3110
        :param memos_for_retrieval: An iterable containing the (index, pos,
 
3111
            length) memo for retrieving the bytes. The Pack access method
 
3112
            looks up the pack to use for a given record in its index_to_pack
 
3113
            map.
 
3114
        :return: An iterator over the bytes of the records.
 
3115
        """
 
3116
        # first pass, group into same-index requests
 
3117
        request_lists = []
 
3118
        current_index = None
 
3119
        for (index, offset, length) in memos_for_retrieval:
 
3120
            if current_index == index:
 
3121
                current_list.append((offset, length))
 
3122
            else:
 
3123
                if current_index is not None:
 
3124
                    request_lists.append((current_index, current_list))
 
3125
                current_index = index
 
3126
                current_list = [(offset, length)]
 
3127
        # handle the last entry
 
3128
        if current_index is not None:
 
3129
            request_lists.append((current_index, current_list))
 
3130
        for index, offsets in request_lists:
 
3131
            try:
 
3132
                transport, path = self._indices[index]
 
3133
            except KeyError:
 
3134
                # A KeyError here indicates that someone has triggered an index
 
3135
                # reload, and this index has gone missing, we need to start
 
3136
                # over.
 
3137
                if self._reload_func is None:
 
3138
                    # If we don't have a _reload_func there is nothing that can
 
3139
                    # be done
 
3140
                    raise
 
3141
                raise errors.RetryWithNewPacks(index,
 
3142
                                               reload_occurred=True,
 
3143
                                               exc_info=sys.exc_info())
 
3144
            try:
 
3145
                reader = pack.make_readv_reader(transport, path, offsets)
 
3146
                for names, read_func in reader.iter_records():
 
3147
                    yield read_func(None)
 
3148
            except errors.NoSuchFile:
 
3149
                # A NoSuchFile error indicates that a pack file has gone
 
3150
                # missing on disk, we need to trigger a reload, and start over.
 
3151
                if self._reload_func is None:
 
3152
                    raise
 
3153
                raise errors.RetryWithNewPacks(transport.abspath(path),
 
3154
                                               reload_occurred=False,
 
3155
                                               exc_info=sys.exc_info())
 
3156
 
 
3157
    def set_writer(self, writer, index, transport_packname):
 
3158
        """Set a writer to use for adding data."""
 
3159
        if index is not None:
 
3160
            self._indices[index] = transport_packname
 
3161
        self._container_writer = writer
 
3162
        self._write_index = index
 
3163
 
 
3164
    def reload_or_raise(self, retry_exc):
 
3165
        """Try calling the reload function, or re-raise the original exception.
 
3166
 
 
3167
        This should be called after _DirectPackAccess raises a
 
3168
        RetryWithNewPacks exception. This function will handle the common logic
 
3169
        of determining when the error is fatal versus being temporary.
 
3170
        It will also make sure that the original exception is raised, rather
 
3171
        than the RetryWithNewPacks exception.
 
3172
 
 
3173
        If this function returns, then the calling function should retry
 
3174
        whatever operation was being performed. Otherwise an exception will
 
3175
        be raised.
 
3176
 
 
3177
        :param retry_exc: A RetryWithNewPacks exception.
 
3178
        """
 
3179
        is_error = False
 
3180
        if self._reload_func is None:
 
3181
            is_error = True
 
3182
        elif not self._reload_func():
 
3183
            # The reload claimed that nothing changed
 
3184
            if not retry_exc.reload_occurred:
 
3185
                # If there wasn't an earlier reload, then we really were
 
3186
                # expecting to find changes. We didn't find them, so this is a
 
3187
                # hard error
 
3188
                is_error = True
 
3189
        if is_error:
 
3190
            exc_class, exc_value, exc_traceback = retry_exc.exc_info
 
3191
            raise exc_class, exc_value, exc_traceback
 
3192
 
 
3193
 
 
3194
# Deprecated, use PatienceSequenceMatcher instead
 
3195
KnitSequenceMatcher = patiencediff.PatienceSequenceMatcher
 
3196
 
 
3197
 
 
3198
def annotate_knit(knit, revision_id):
 
3199
    """Annotate a knit with no cached annotations.
 
3200
 
 
3201
    This implementation is for knits with no cached annotations.
 
3202
    It will work for knits with cached annotations, but this is not
 
3203
    recommended.
 
3204
    """
 
3205
    annotator = _KnitAnnotator(knit)
 
3206
    return iter(annotator.annotate(revision_id))
 
3207
 
 
3208
 
 
3209
class _KnitAnnotator(object):
 
3210
    """Build up the annotations for a text."""
 
3211
 
 
3212
    def __init__(self, knit):
 
3213
        self._knit = knit
 
3214
 
 
3215
        # Content objects, differs from fulltexts because of how final newlines
 
3216
        # are treated by knits. the content objects here will always have a
 
3217
        # final newline
 
3218
        self._fulltext_contents = {}
 
3219
 
 
3220
        # Annotated lines of specific revisions
 
3221
        self._annotated_lines = {}
 
3222
 
 
3223
        # Track the raw data for nodes that we could not process yet.
 
3224
        # This maps the revision_id of the base to a list of children that will
 
3225
        # annotated from it.
 
3226
        self._pending_children = {}
 
3227
 
 
3228
        # Nodes which cannot be extracted
 
3229
        self._ghosts = set()
 
3230
 
 
3231
        # Track how many children this node has, so we know if we need to keep
 
3232
        # it
 
3233
        self._annotate_children = {}
 
3234
        self._compression_children = {}
 
3235
 
 
3236
        self._all_build_details = {}
 
3237
        # The children => parent revision_id graph
 
3238
        self._revision_id_graph = {}
 
3239
 
 
3240
        self._heads_provider = None
 
3241
 
 
3242
        self._nodes_to_keep_annotations = set()
 
3243
        self._generations_until_keep = 100
 
3244
 
 
3245
    def set_generations_until_keep(self, value):
 
3246
        """Set the number of generations before caching a node.
 
3247
 
 
3248
        Setting this to -1 will cache every merge node, setting this higher
 
3249
        will cache fewer nodes.
 
3250
        """
 
3251
        self._generations_until_keep = value
 
3252
 
 
3253
    def _add_fulltext_content(self, revision_id, content_obj):
 
3254
        self._fulltext_contents[revision_id] = content_obj
 
3255
        # TODO: jam 20080305 It might be good to check the sha1digest here
 
3256
        return content_obj.text()
 
3257
 
 
3258
    def _check_parents(self, child, nodes_to_annotate):
 
3259
        """Check if all parents have been processed.
 
3260
 
 
3261
        :param child: A tuple of (rev_id, parents, raw_content)
 
3262
        :param nodes_to_annotate: If child is ready, add it to
 
3263
            nodes_to_annotate, otherwise put it back in self._pending_children
 
3264
        """
 
3265
        for parent_id in child[1]:
 
3266
            if (parent_id not in self._annotated_lines):
 
3267
                # This parent is present, but another parent is missing
 
3268
                self._pending_children.setdefault(parent_id,
 
3269
                                                  []).append(child)
 
3270
                break
 
3271
        else:
 
3272
            # This one is ready to be processed
 
3273
            nodes_to_annotate.append(child)
 
3274
 
 
3275
    def _add_annotation(self, revision_id, fulltext, parent_ids,
 
3276
                        left_matching_blocks=None):
 
3277
        """Add an annotation entry.
 
3278
 
 
3279
        All parents should already have been annotated.
 
3280
        :return: A list of children that now have their parents satisfied.
 
3281
        """
 
3282
        a = self._annotated_lines
 
3283
        annotated_parent_lines = [a[p] for p in parent_ids]
 
3284
        annotated_lines = list(annotate.reannotate(annotated_parent_lines,
 
3285
            fulltext, revision_id, left_matching_blocks,
 
3286
            heads_provider=self._get_heads_provider()))
 
3287
        self._annotated_lines[revision_id] = annotated_lines
 
3288
        for p in parent_ids:
 
3289
            ann_children = self._annotate_children[p]
 
3290
            ann_children.remove(revision_id)
 
3291
            if (not ann_children
 
3292
                and p not in self._nodes_to_keep_annotations):
 
3293
                del self._annotated_lines[p]
 
3294
                del self._all_build_details[p]
 
3295
                if p in self._fulltext_contents:
 
3296
                    del self._fulltext_contents[p]
 
3297
        # Now that we've added this one, see if there are any pending
 
3298
        # deltas to be done, certainly this parent is finished
 
3299
        nodes_to_annotate = []
 
3300
        for child in self._pending_children.pop(revision_id, []):
 
3301
            self._check_parents(child, nodes_to_annotate)
 
3302
        return nodes_to_annotate
 
3303
 
 
3304
    def _get_build_graph(self, key):
 
3305
        """Get the graphs for building texts and annotations.
 
3306
 
 
3307
        The data you need for creating a full text may be different than the
 
3308
        data you need to annotate that text. (At a minimum, you need both
 
3309
        parents to create an annotation, but only need 1 parent to generate the
 
3310
        fulltext.)
 
3311
 
 
3312
        :return: A list of (key, index_memo) records, suitable for
 
3313
            passing to read_records_iter to start reading in the raw data fro/
 
3314
            the pack file.
 
3315
        """
 
3316
        if key in self._annotated_lines:
 
3317
            # Nothing to do
 
3318
            return []
 
3319
        pending = set([key])
 
3320
        records = []
 
3321
        generation = 0
 
3322
        kept_generation = 0
 
3323
        while pending:
 
3324
            # get all pending nodes
 
3325
            generation += 1
 
3326
            this_iteration = pending
 
3327
            build_details = self._knit._index.get_build_details(this_iteration)
 
3328
            self._all_build_details.update(build_details)
 
3329
            # new_nodes = self._knit._index._get_entries(this_iteration)
 
3330
            pending = set()
 
3331
            for key, details in build_details.iteritems():
 
3332
                (index_memo, compression_parent, parents,
 
3333
                 record_details) = details
 
3334
                self._revision_id_graph[key] = parents
 
3335
                records.append((key, index_memo))
 
3336
                # Do we actually need to check _annotated_lines?
 
3337
                pending.update(p for p in parents
 
3338
                                 if p not in self._all_build_details)
 
3339
                if compression_parent:
 
3340
                    self._compression_children.setdefault(compression_parent,
 
3341
                        []).append(key)
 
3342
                if parents:
 
3343
                    for parent in parents:
 
3344
                        self._annotate_children.setdefault(parent,
 
3345
                            []).append(key)
 
3346
                    num_gens = generation - kept_generation
 
3347
                    if ((num_gens >= self._generations_until_keep)
 
3348
                        and len(parents) > 1):
 
3349
                        kept_generation = generation
 
3350
                        self._nodes_to_keep_annotations.add(key)
 
3351
 
 
3352
            missing_versions = this_iteration.difference(build_details.keys())
 
3353
            self._ghosts.update(missing_versions)
 
3354
            for missing_version in missing_versions:
 
3355
                # add a key, no parents
 
3356
                self._revision_id_graph[missing_version] = ()
 
3357
                pending.discard(missing_version) # don't look for it
 
3358
        if self._ghosts.intersection(self._compression_children):
 
3359
            raise KnitCorrupt(
 
3360
                "We cannot have nodes which have a ghost compression parent:\n"
 
3361
                "ghosts: %r\n"
 
3362
                "compression children: %r"
 
3363
                % (self._ghosts, self._compression_children))
 
3364
        # Cleanout anything that depends on a ghost so that we don't wait for
 
3365
        # the ghost to show up
 
3366
        for node in self._ghosts:
 
3367
            if node in self._annotate_children:
 
3368
                # We won't be building this node
 
3369
                del self._annotate_children[node]
 
3370
        # Generally we will want to read the records in reverse order, because
 
3371
        # we find the parent nodes after the children
 
3372
        records.reverse()
 
3373
        return records
 
3374
 
 
3375
    def _annotate_records(self, records):
 
3376
        """Build the annotations for the listed records."""
 
3377
        # We iterate in the order read, rather than a strict order requested
 
3378
        # However, process what we can, and put off to the side things that
 
3379
        # still need parents, cleaning them up when those parents are
 
3380
        # processed.
 
3381
        for (rev_id, record,
 
3382
             digest) in self._knit._read_records_iter(records):
 
3383
            if rev_id in self._annotated_lines:
 
3384
                continue
 
3385
            parent_ids = self._revision_id_graph[rev_id]
 
3386
            parent_ids = [p for p in parent_ids if p not in self._ghosts]
 
3387
            details = self._all_build_details[rev_id]
 
3388
            (index_memo, compression_parent, parents,
 
3389
             record_details) = details
 
3390
            nodes_to_annotate = []
 
3391
            # TODO: Remove the punning between compression parents, and
 
3392
            #       parent_ids, we should be able to do this without assuming
 
3393
            #       the build order
 
3394
            if len(parent_ids) == 0:
 
3395
                # There are no parents for this node, so just add it
 
3396
                # TODO: This probably needs to be decoupled
 
3397
                fulltext_content, delta = self._knit._factory.parse_record(
 
3398
                    rev_id, record, record_details, None)
 
3399
                fulltext = self._add_fulltext_content(rev_id, fulltext_content)
 
3400
                nodes_to_annotate.extend(self._add_annotation(rev_id, fulltext,
 
3401
                    parent_ids, left_matching_blocks=None))
 
3402
            else:
 
3403
                child = (rev_id, parent_ids, record)
 
3404
                # Check if all the parents are present
 
3405
                self._check_parents(child, nodes_to_annotate)
 
3406
            while nodes_to_annotate:
 
3407
                # Should we use a queue here instead of a stack?
 
3408
                (rev_id, parent_ids, record) = nodes_to_annotate.pop()
 
3409
                (index_memo, compression_parent, parents,
 
3410
                 record_details) = self._all_build_details[rev_id]
 
3411
                blocks = None
 
3412
                if compression_parent is not None:
 
3413
                    comp_children = self._compression_children[compression_parent]
 
3414
                    if rev_id not in comp_children:
 
3415
                        raise AssertionError("%r not in compression children %r"
 
3416
                            % (rev_id, comp_children))
 
3417
                    # If there is only 1 child, it is safe to reuse this
 
3418
                    # content
 
3419
                    reuse_content = (len(comp_children) == 1
 
3420
                        and compression_parent not in
 
3421
                            self._nodes_to_keep_annotations)
 
3422
                    if reuse_content:
 
3423
                        # Remove it from the cache since it will be changing
 
3424
                        parent_fulltext_content = self._fulltext_contents.pop(compression_parent)
 
3425
                        # Make sure to copy the fulltext since it might be
 
3426
                        # modified
 
3427
                        parent_fulltext = list(parent_fulltext_content.text())
 
3428
                    else:
 
3429
                        parent_fulltext_content = self._fulltext_contents[compression_parent]
 
3430
                        parent_fulltext = parent_fulltext_content.text()
 
3431
                    comp_children.remove(rev_id)
 
3432
                    fulltext_content, delta = self._knit._factory.parse_record(
 
3433
                        rev_id, record, record_details,
 
3434
                        parent_fulltext_content,
 
3435
                        copy_base_content=(not reuse_content))
 
3436
                    fulltext = self._add_fulltext_content(rev_id,
 
3437
                                                          fulltext_content)
 
3438
                    if compression_parent == parent_ids[0]:
 
3439
                        # the compression_parent is the left parent, so we can
 
3440
                        # re-use the delta
 
3441
                        blocks = KnitContent.get_line_delta_blocks(delta,
 
3442
                                parent_fulltext, fulltext)
 
3443
                else:
 
3444
                    fulltext_content = self._knit._factory.parse_fulltext(
 
3445
                        record, rev_id)
 
3446
                    fulltext = self._add_fulltext_content(rev_id,
 
3447
                        fulltext_content)
 
3448
                nodes_to_annotate.extend(
 
3449
                    self._add_annotation(rev_id, fulltext, parent_ids,
 
3450
                                     left_matching_blocks=blocks))
 
3451
 
 
3452
    def _get_heads_provider(self):
 
3453
        """Create a heads provider for resolving ancestry issues."""
 
3454
        if self._heads_provider is not None:
 
3455
            return self._heads_provider
 
3456
        parent_provider = _mod_graph.DictParentsProvider(
 
3457
            self._revision_id_graph)
 
3458
        graph_obj = _mod_graph.Graph(parent_provider)
 
3459
        head_cache = _mod_graph.FrozenHeadsCache(graph_obj)
 
3460
        self._heads_provider = head_cache
 
3461
        return head_cache
 
3462
 
 
3463
    def annotate(self, key):
 
3464
        """Return the annotated fulltext at the given key.
 
3465
 
 
3466
        :param key: The key to annotate.
 
3467
        """
 
3468
        if len(self._knit._fallback_vfs) > 0:
 
3469
            # stacked knits can't use the fast path at present.
 
3470
            return self._simple_annotate(key)
 
3471
        while True:
 
3472
            try:
 
3473
                records = self._get_build_graph(key)
 
3474
                if key in self._ghosts:
 
3475
                    raise errors.RevisionNotPresent(key, self._knit)
 
3476
                self._annotate_records(records)
 
3477
                return self._annotated_lines[key]
 
3478
            except errors.RetryWithNewPacks, e:
 
3479
                self._knit._access.reload_or_raise(e)
 
3480
                # The cached build_details are no longer valid
 
3481
                self._all_build_details.clear()
 
3482
 
 
3483
    def _simple_annotate(self, key):
 
3484
        """Return annotated fulltext, rediffing from the full texts.
 
3485
 
 
3486
        This is slow but makes no assumptions about the repository
 
3487
        being able to produce line deltas.
 
3488
        """
 
3489
        # TODO: this code generates a parent maps of present ancestors; it
 
3490
        # could be split out into a separate method, and probably should use
 
3491
        # iter_ancestry instead. -- mbp and robertc 20080704
 
3492
        graph = _mod_graph.Graph(self._knit)
 
3493
        head_cache = _mod_graph.FrozenHeadsCache(graph)
 
3494
        search = graph._make_breadth_first_searcher([key])
 
3495
        keys = set()
 
3496
        while True:
 
3497
            try:
 
3498
                present, ghosts = search.next_with_ghosts()
 
3499
            except StopIteration:
 
3500
                break
 
3501
            keys.update(present)
 
3502
        parent_map = self._knit.get_parent_map(keys)
 
3503
        parent_cache = {}
 
3504
        reannotate = annotate.reannotate
 
3505
        for record in self._knit.get_record_stream(keys, 'topological', True):
 
3506
            key = record.key
 
3507
            fulltext = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
 
3508
            parents = parent_map[key]
 
3509
            if parents is not None:
 
3510
                parent_lines = [parent_cache[parent] for parent in parent_map[key]]
 
3511
            else:
 
3512
                parent_lines = []
 
3513
            parent_cache[key] = list(
 
3514
                reannotate(parent_lines, fulltext, key, None, head_cache))
 
3515
        try:
 
3516
            return parent_cache[key]
 
3517
        except KeyError, e:
 
3518
            raise errors.RevisionNotPresent(key, self._knit)
 
3519
 
 
3520
 
 
3521
try:
 
3522
    from bzrlib._knit_load_data_c import _load_data_c as _load_data
 
3523
except ImportError:
 
3524
    from bzrlib._knit_load_data_py import _load_data_py as _load_data