/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: Robert Collins
  • Date: 2009-02-15 21:24:20 UTC
  • mto: (4022.1.4 fetch.RemoteSink)
  • mto: This revision was merged to the branch mainline in revision 4026.
  • Revision ID: robertc@robertcollins.net-20090215212420-2h3c8fdf0w2h6e0v
Change the signature on VersionedFiles adapters to allow less typing and more flexability inside adapters.

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