/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: James Westby
  • Date: 2009-03-12 06:04:54 UTC
  • mto: This revision was merged to the branch mainline in revision 4139.
  • Revision ID: james.westby@canonical.com-20090312060454-f04pmpufns32zyk6
Move the shelve docs from bzrtools to the main help.

Also change them to document the new "shelve" rather than the old one.

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