/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/versionedfile.py

  • Committer: Jelmer Vernooij
  • Date: 2008-07-08 15:58:12 UTC
  • mfrom: (3353.1.4 paramiko-1.7.2-compat)
  • mto: This revision was merged to the branch mainline in revision 3531.
  • Revision ID: jelmer@samba.org-20080708155812-gacwrgs7d7n48a0d
Merge bashprompt script that prints the bzr branch nick.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
#
 
3
# Authors:
 
4
#   Johan Rydberg <jrydberg@gnu.org>
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
"""Versioned text file storage api."""
 
21
 
 
22
from copy import copy
 
23
from cStringIO import StringIO
 
24
import os
 
25
import urllib
 
26
from zlib import adler32
 
27
 
 
28
from bzrlib.lazy_import import lazy_import
 
29
lazy_import(globals(), """
 
30
 
 
31
from bzrlib import (
 
32
    errors,
 
33
    osutils,
 
34
    multiparent,
 
35
    tsort,
 
36
    revision,
 
37
    ui,
 
38
    )
 
39
from bzrlib.graph import DictParentsProvider, Graph, _StackedParentsProvider
 
40
from bzrlib.transport.memory import MemoryTransport
 
41
""")
 
42
from bzrlib.inter import InterObject
 
43
from bzrlib.registry import Registry
 
44
from bzrlib.symbol_versioning import *
 
45
from bzrlib.textmerge import TextMerge
 
46
 
 
47
 
 
48
adapter_registry = Registry()
 
49
adapter_registry.register_lazy(('knit-delta-gz', 'fulltext'), 'bzrlib.knit',
 
50
    'DeltaPlainToFullText')
 
51
adapter_registry.register_lazy(('knit-ft-gz', 'fulltext'), 'bzrlib.knit',
 
52
    'FTPlainToFullText')
 
53
adapter_registry.register_lazy(('knit-annotated-delta-gz', 'knit-delta-gz'),
 
54
    'bzrlib.knit', 'DeltaAnnotatedToUnannotated')
 
55
adapter_registry.register_lazy(('knit-annotated-delta-gz', 'fulltext'),
 
56
    'bzrlib.knit', 'DeltaAnnotatedToFullText')
 
57
adapter_registry.register_lazy(('knit-annotated-ft-gz', 'knit-ft-gz'),
 
58
    'bzrlib.knit', 'FTAnnotatedToUnannotated')
 
59
adapter_registry.register_lazy(('knit-annotated-ft-gz', 'fulltext'),
 
60
    'bzrlib.knit', 'FTAnnotatedToFullText')
 
61
 
 
62
 
 
63
class ContentFactory(object):
 
64
    """Abstract interface for insertion and retrieval from a VersionedFile.
 
65
    
 
66
    :ivar sha1: None, or the sha1 of the content fulltext.
 
67
    :ivar storage_kind: The native storage kind of this factory. One of
 
68
        'mpdiff', 'knit-annotated-ft', 'knit-annotated-delta', 'knit-ft',
 
69
        'knit-delta', 'fulltext', 'knit-annotated-ft-gz',
 
70
        'knit-annotated-delta-gz', 'knit-ft-gz', 'knit-delta-gz'.
 
71
    :ivar key: The key of this content. Each key is a tuple with a single
 
72
        string in it.
 
73
    :ivar parents: A tuple of parent keys for self.key. If the object has
 
74
        no parent information, None (as opposed to () for an empty list of
 
75
        parents).
 
76
    """
 
77
 
 
78
    def __init__(self):
 
79
        """Create a ContentFactory."""
 
80
        self.sha1 = None
 
81
        self.storage_kind = None
 
82
        self.key = None
 
83
        self.parents = None
 
84
 
 
85
 
 
86
class FulltextContentFactory(ContentFactory):
 
87
    """Static data content factory.
 
88
 
 
89
    This takes a fulltext when created and just returns that during
 
90
    get_bytes_as('fulltext').
 
91
    
 
92
    :ivar sha1: None, or the sha1 of the content fulltext.
 
93
    :ivar storage_kind: The native storage kind of this factory. Always
 
94
        'fulltext'.
 
95
    :ivar key: The key of this content. Each key is a tuple with a single
 
96
        string in it.
 
97
    :ivar parents: A tuple of parent keys for self.key. If the object has
 
98
        no parent information, None (as opposed to () for an empty list of
 
99
        parents).
 
100
     """
 
101
 
 
102
    def __init__(self, key, parents, sha1, text):
 
103
        """Create a ContentFactory."""
 
104
        self.sha1 = sha1
 
105
        self.storage_kind = 'fulltext'
 
106
        self.key = key
 
107
        self.parents = parents
 
108
        self._text = text
 
109
 
 
110
    def get_bytes_as(self, storage_kind):
 
111
        if storage_kind == self.storage_kind:
 
112
            return self._text
 
113
        raise errors.UnavailableRepresentation(self.key, storage_kind,
 
114
            self.storage_kind)
 
115
 
 
116
 
 
117
class AbsentContentFactory(ContentFactory):
 
118
    """A placeholder content factory for unavailable texts.
 
119
    
 
120
    :ivar sha1: None.
 
121
    :ivar storage_kind: 'absent'.
 
122
    :ivar key: The key of this content. Each key is a tuple with a single
 
123
        string in it.
 
124
    :ivar parents: None.
 
125
    """
 
126
 
 
127
    def __init__(self, key):
 
128
        """Create a ContentFactory."""
 
129
        self.sha1 = None
 
130
        self.storage_kind = 'absent'
 
131
        self.key = key
 
132
        self.parents = None
 
133
 
 
134
 
 
135
class AdapterFactory(ContentFactory):
 
136
    """A content factory to adapt between key prefix's."""
 
137
 
 
138
    def __init__(self, key, parents, adapted):
 
139
        """Create an adapter factory instance."""
 
140
        self.key = key
 
141
        self.parents = parents
 
142
        self._adapted = adapted
 
143
 
 
144
    def __getattr__(self, attr):
 
145
        """Return a member from the adapted object."""
 
146
        if attr in ('key', 'parents'):
 
147
            return self.__dict__[attr]
 
148
        else:
 
149
            return getattr(self._adapted, attr)
 
150
 
 
151
 
 
152
def filter_absent(record_stream):
 
153
    """Adapt a record stream to remove absent records."""
 
154
    for record in record_stream:
 
155
        if record.storage_kind != 'absent':
 
156
            yield record
 
157
 
 
158
 
 
159
class VersionedFile(object):
 
160
    """Versioned text file storage.
 
161
    
 
162
    A versioned file manages versions of line-based text files,
 
163
    keeping track of the originating version for each line.
 
164
 
 
165
    To clients the "lines" of the file are represented as a list of
 
166
    strings. These strings will typically have terminal newline
 
167
    characters, but this is not required.  In particular files commonly
 
168
    do not have a newline at the end of the file.
 
169
 
 
170
    Texts are identified by a version-id string.
 
171
    """
 
172
 
 
173
    @staticmethod
 
174
    def check_not_reserved_id(version_id):
 
175
        revision.check_not_reserved_id(version_id)
 
176
 
 
177
    def copy_to(self, name, transport):
 
178
        """Copy this versioned file to name on transport."""
 
179
        raise NotImplementedError(self.copy_to)
 
180
 
 
181
    def get_record_stream(self, versions, ordering, include_delta_closure):
 
182
        """Get a stream of records for versions.
 
183
 
 
184
        :param versions: The versions to include. Each version is a tuple
 
185
            (version,).
 
186
        :param ordering: Either 'unordered' or 'topological'. A topologically
 
187
            sorted stream has compression parents strictly before their
 
188
            children.
 
189
        :param include_delta_closure: If True then the closure across any
 
190
            compression parents will be included (in the data content of the
 
191
            stream, not in the emitted records). This guarantees that
 
192
            'fulltext' can be used successfully on every record.
 
193
        :return: An iterator of ContentFactory objects, each of which is only
 
194
            valid until the iterator is advanced.
 
195
        """
 
196
        raise NotImplementedError(self.get_record_stream)
 
197
 
 
198
    def has_version(self, version_id):
 
199
        """Returns whether version is present."""
 
200
        raise NotImplementedError(self.has_version)
 
201
 
 
202
    def insert_record_stream(self, stream):
 
203
        """Insert a record stream into this versioned file.
 
204
 
 
205
        :param stream: A stream of records to insert. 
 
206
        :return: None
 
207
        :seealso VersionedFile.get_record_stream:
 
208
        """
 
209
        raise NotImplementedError
 
210
 
 
211
    def add_lines(self, version_id, parents, lines, parent_texts=None,
 
212
        left_matching_blocks=None, nostore_sha=None, random_id=False,
 
213
        check_content=True):
 
214
        """Add a single text on top of the versioned file.
 
215
 
 
216
        Must raise RevisionAlreadyPresent if the new version is
 
217
        already present in file history.
 
218
 
 
219
        Must raise RevisionNotPresent if any of the given parents are
 
220
        not present in file history.
 
221
 
 
222
        :param lines: A list of lines. Each line must be a bytestring. And all
 
223
            of them except the last must be terminated with \n and contain no
 
224
            other \n's. The last line may either contain no \n's or a single
 
225
            terminated \n. If the lines list does meet this constraint the add
 
226
            routine may error or may succeed - but you will be unable to read
 
227
            the data back accurately. (Checking the lines have been split
 
228
            correctly is expensive and extremely unlikely to catch bugs so it
 
229
            is not done at runtime unless check_content is True.)
 
230
        :param parent_texts: An optional dictionary containing the opaque 
 
231
            representations of some or all of the parents of version_id to
 
232
            allow delta optimisations.  VERY IMPORTANT: the texts must be those
 
233
            returned by add_lines or data corruption can be caused.
 
234
        :param left_matching_blocks: a hint about which areas are common
 
235
            between the text and its left-hand-parent.  The format is
 
236
            the SequenceMatcher.get_matching_blocks format.
 
237
        :param nostore_sha: Raise ExistingContent and do not add the lines to
 
238
            the versioned file if the digest of the lines matches this.
 
239
        :param random_id: If True a random id has been selected rather than
 
240
            an id determined by some deterministic process such as a converter
 
241
            from a foreign VCS. When True the backend may choose not to check
 
242
            for uniqueness of the resulting key within the versioned file, so
 
243
            this should only be done when the result is expected to be unique
 
244
            anyway.
 
245
        :param check_content: If True, the lines supplied are verified to be
 
246
            bytestrings that are correctly formed lines.
 
247
        :return: The text sha1, the number of bytes in the text, and an opaque
 
248
                 representation of the inserted version which can be provided
 
249
                 back to future add_lines calls in the parent_texts dictionary.
 
250
        """
 
251
        self._check_write_ok()
 
252
        return self._add_lines(version_id, parents, lines, parent_texts,
 
253
            left_matching_blocks, nostore_sha, random_id, check_content)
 
254
 
 
255
    def _add_lines(self, version_id, parents, lines, parent_texts,
 
256
        left_matching_blocks, nostore_sha, random_id, check_content):
 
257
        """Helper to do the class specific add_lines."""
 
258
        raise NotImplementedError(self.add_lines)
 
259
 
 
260
    def add_lines_with_ghosts(self, version_id, parents, lines,
 
261
        parent_texts=None, nostore_sha=None, random_id=False,
 
262
        check_content=True, left_matching_blocks=None):
 
263
        """Add lines to the versioned file, allowing ghosts to be present.
 
264
        
 
265
        This takes the same parameters as add_lines and returns the same.
 
266
        """
 
267
        self._check_write_ok()
 
268
        return self._add_lines_with_ghosts(version_id, parents, lines,
 
269
            parent_texts, nostore_sha, random_id, check_content, left_matching_blocks)
 
270
 
 
271
    def _add_lines_with_ghosts(self, version_id, parents, lines, parent_texts,
 
272
        nostore_sha, random_id, check_content, left_matching_blocks):
 
273
        """Helper to do class specific add_lines_with_ghosts."""
 
274
        raise NotImplementedError(self.add_lines_with_ghosts)
 
275
 
 
276
    def check(self, progress_bar=None):
 
277
        """Check the versioned file for integrity."""
 
278
        raise NotImplementedError(self.check)
 
279
 
 
280
    def _check_lines_not_unicode(self, lines):
 
281
        """Check that lines being added to a versioned file are not unicode."""
 
282
        for line in lines:
 
283
            if line.__class__ is not str:
 
284
                raise errors.BzrBadParameterUnicode("lines")
 
285
 
 
286
    def _check_lines_are_lines(self, lines):
 
287
        """Check that the lines really are full lines without inline EOL."""
 
288
        for line in lines:
 
289
            if '\n' in line[:-1]:
 
290
                raise errors.BzrBadParameterContainsNewline("lines")
 
291
 
 
292
    def get_format_signature(self):
 
293
        """Get a text description of the data encoding in this file.
 
294
        
 
295
        :since: 0.90
 
296
        """
 
297
        raise NotImplementedError(self.get_format_signature)
 
298
 
 
299
    def make_mpdiffs(self, version_ids):
 
300
        """Create multiparent diffs for specified versions."""
 
301
        knit_versions = set()
 
302
        knit_versions.update(version_ids)
 
303
        parent_map = self.get_parent_map(version_ids)
 
304
        for version_id in version_ids:
 
305
            try:
 
306
                knit_versions.update(parent_map[version_id])
 
307
            except KeyError:
 
308
                raise errors.RevisionNotPresent(version_id, self)
 
309
        # We need to filter out ghosts, because we can't diff against them.
 
310
        knit_versions = set(self.get_parent_map(knit_versions).keys())
 
311
        lines = dict(zip(knit_versions,
 
312
            self._get_lf_split_line_list(knit_versions)))
 
313
        diffs = []
 
314
        for version_id in version_ids:
 
315
            target = lines[version_id]
 
316
            try:
 
317
                parents = [lines[p] for p in parent_map[version_id] if p in
 
318
                    knit_versions]
 
319
            except KeyError:
 
320
                # I don't know how this could ever trigger.
 
321
                # parent_map[version_id] was already triggered in the previous
 
322
                # for loop, and lines[p] has the 'if p in knit_versions' check,
 
323
                # so we again won't have a KeyError.
 
324
                raise errors.RevisionNotPresent(version_id, self)
 
325
            if len(parents) > 0:
 
326
                left_parent_blocks = self._extract_blocks(version_id,
 
327
                                                          parents[0], target)
 
328
            else:
 
329
                left_parent_blocks = None
 
330
            diffs.append(multiparent.MultiParent.from_lines(target, parents,
 
331
                         left_parent_blocks))
 
332
        return diffs
 
333
 
 
334
    def _extract_blocks(self, version_id, source, target):
 
335
        return None
 
336
 
 
337
    def add_mpdiffs(self, records):
 
338
        """Add mpdiffs to this VersionedFile.
 
339
 
 
340
        Records should be iterables of version, parents, expected_sha1,
 
341
        mpdiff. mpdiff should be a MultiParent instance.
 
342
        """
 
343
        # Does this need to call self._check_write_ok()? (IanC 20070919)
 
344
        vf_parents = {}
 
345
        mpvf = multiparent.MultiMemoryVersionedFile()
 
346
        versions = []
 
347
        for version, parent_ids, expected_sha1, mpdiff in records:
 
348
            versions.append(version)
 
349
            mpvf.add_diff(mpdiff, version, parent_ids)
 
350
        needed_parents = set()
 
351
        for version, parent_ids, expected_sha1, mpdiff in records:
 
352
            needed_parents.update(p for p in parent_ids
 
353
                                  if not mpvf.has_version(p))
 
354
        present_parents = set(self.get_parent_map(needed_parents).keys())
 
355
        for parent_id, lines in zip(present_parents,
 
356
                                 self._get_lf_split_line_list(present_parents)):
 
357
            mpvf.add_version(lines, parent_id, [])
 
358
        for (version, parent_ids, expected_sha1, mpdiff), lines in\
 
359
            zip(records, mpvf.get_line_list(versions)):
 
360
            if len(parent_ids) == 1:
 
361
                left_matching_blocks = list(mpdiff.get_matching_blocks(0,
 
362
                    mpvf.get_diff(parent_ids[0]).num_lines()))
 
363
            else:
 
364
                left_matching_blocks = None
 
365
            try:
 
366
                _, _, version_text = self.add_lines_with_ghosts(version,
 
367
                    parent_ids, lines, vf_parents,
 
368
                    left_matching_blocks=left_matching_blocks)
 
369
            except NotImplementedError:
 
370
                # The vf can't handle ghosts, so add lines normally, which will
 
371
                # (reasonably) fail if there are ghosts in the data.
 
372
                _, _, version_text = self.add_lines(version,
 
373
                    parent_ids, lines, vf_parents,
 
374
                    left_matching_blocks=left_matching_blocks)
 
375
            vf_parents[version] = version_text
 
376
        sha1s = self.get_sha1s(versions)
 
377
        for version, parent_ids, expected_sha1, mpdiff in records:
 
378
            if expected_sha1 != sha1s[version]:
 
379
                raise errors.VersionedFileInvalidChecksum(version)
 
380
 
 
381
    def get_text(self, version_id):
 
382
        """Return version contents as a text string.
 
383
 
 
384
        Raises RevisionNotPresent if version is not present in
 
385
        file history.
 
386
        """
 
387
        return ''.join(self.get_lines(version_id))
 
388
    get_string = get_text
 
389
 
 
390
    def get_texts(self, version_ids):
 
391
        """Return the texts of listed versions as a list of strings.
 
392
 
 
393
        Raises RevisionNotPresent if version is not present in
 
394
        file history.
 
395
        """
 
396
        return [''.join(self.get_lines(v)) for v in version_ids]
 
397
 
 
398
    def get_lines(self, version_id):
 
399
        """Return version contents as a sequence of lines.
 
400
 
 
401
        Raises RevisionNotPresent if version is not present in
 
402
        file history.
 
403
        """
 
404
        raise NotImplementedError(self.get_lines)
 
405
 
 
406
    def _get_lf_split_line_list(self, version_ids):
 
407
        return [StringIO(t).readlines() for t in self.get_texts(version_ids)]
 
408
 
 
409
    def get_ancestry(self, version_ids, topo_sorted=True):
 
410
        """Return a list of all ancestors of given version(s). This
 
411
        will not include the null revision.
 
412
 
 
413
        This list will not be topologically sorted if topo_sorted=False is
 
414
        passed.
 
415
 
 
416
        Must raise RevisionNotPresent if any of the given versions are
 
417
        not present in file history."""
 
418
        if isinstance(version_ids, basestring):
 
419
            version_ids = [version_ids]
 
420
        raise NotImplementedError(self.get_ancestry)
 
421
        
 
422
    def get_ancestry_with_ghosts(self, version_ids):
 
423
        """Return a list of all ancestors of given version(s). This
 
424
        will not include the null revision.
 
425
 
 
426
        Must raise RevisionNotPresent if any of the given versions are
 
427
        not present in file history.
 
428
        
 
429
        Ghosts that are known about will be included in ancestry list,
 
430
        but are not explicitly marked.
 
431
        """
 
432
        raise NotImplementedError(self.get_ancestry_with_ghosts)
 
433
    
 
434
    def get_parent_map(self, version_ids):
 
435
        """Get a map of the parents of version_ids.
 
436
 
 
437
        :param version_ids: The version ids to look up parents for.
 
438
        :return: A mapping from version id to parents.
 
439
        """
 
440
        raise NotImplementedError(self.get_parent_map)
 
441
 
 
442
    def get_parents_with_ghosts(self, version_id):
 
443
        """Return version names for parents of version_id.
 
444
 
 
445
        Will raise RevisionNotPresent if version_id is not present
 
446
        in the history.
 
447
 
 
448
        Ghosts that are known about will be included in the parent list,
 
449
        but are not explicitly marked.
 
450
        """
 
451
        try:
 
452
            return list(self.get_parent_map([version_id])[version_id])
 
453
        except KeyError:
 
454
            raise errors.RevisionNotPresent(version_id, self)
 
455
 
 
456
    def annotate(self, version_id):
 
457
        """Return a list of (version-id, line) tuples for version_id.
 
458
 
 
459
        :raise RevisionNotPresent: If the given version is
 
460
        not present in file history.
 
461
        """
 
462
        raise NotImplementedError(self.annotate)
 
463
 
 
464
    def iter_lines_added_or_present_in_versions(self, version_ids=None,
 
465
                                                pb=None):
 
466
        """Iterate over the lines in the versioned file from version_ids.
 
467
 
 
468
        This may return lines from other versions. Each item the returned
 
469
        iterator yields is a tuple of a line and a text version that that line
 
470
        is present in (not introduced in).
 
471
 
 
472
        Ordering of results is in whatever order is most suitable for the
 
473
        underlying storage format.
 
474
 
 
475
        If a progress bar is supplied, it may be used to indicate progress.
 
476
        The caller is responsible for cleaning up progress bars (because this
 
477
        is an iterator).
 
478
 
 
479
        NOTES: Lines are normalised: they will all have \n terminators.
 
480
               Lines are returned in arbitrary order.
 
481
 
 
482
        :return: An iterator over (line, version_id).
 
483
        """
 
484
        raise NotImplementedError(self.iter_lines_added_or_present_in_versions)
 
485
 
 
486
    def plan_merge(self, ver_a, ver_b):
 
487
        """Return pseudo-annotation indicating how the two versions merge.
 
488
 
 
489
        This is computed between versions a and b and their common
 
490
        base.
 
491
 
 
492
        Weave lines present in none of them are skipped entirely.
 
493
 
 
494
        Legend:
 
495
        killed-base Dead in base revision
 
496
        killed-both Killed in each revision
 
497
        killed-a    Killed in a
 
498
        killed-b    Killed in b
 
499
        unchanged   Alive in both a and b (possibly created in both)
 
500
        new-a       Created in a
 
501
        new-b       Created in b
 
502
        ghost-a     Killed in a, unborn in b    
 
503
        ghost-b     Killed in b, unborn in a
 
504
        irrelevant  Not in either revision
 
505
        """
 
506
        raise NotImplementedError(VersionedFile.plan_merge)
 
507
        
 
508
    def weave_merge(self, plan, a_marker=TextMerge.A_MARKER,
 
509
                    b_marker=TextMerge.B_MARKER):
 
510
        return PlanWeaveMerge(plan, a_marker, b_marker).merge_lines()[0]
 
511
 
 
512
 
 
513
class RecordingVersionedFilesDecorator(object):
 
514
    """A minimal versioned files that records calls made on it.
 
515
    
 
516
    Only enough methods have been added to support tests using it to date.
 
517
 
 
518
    :ivar calls: A list of the calls made; can be reset at any time by
 
519
        assigning [] to it.
 
520
    """
 
521
 
 
522
    def __init__(self, backing_vf):
 
523
        """Create a RecordingVersionedFileDsecorator decorating backing_vf.
 
524
        
 
525
        :param backing_vf: The versioned file to answer all methods.
 
526
        """
 
527
        self._backing_vf = backing_vf
 
528
        self.calls = []
 
529
 
 
530
    def add_lines(self, key, parents, lines, parent_texts=None,
 
531
        left_matching_blocks=None, nostore_sha=None, random_id=False,
 
532
        check_content=True):
 
533
        self.calls.append(("add_lines", key, parents, lines, parent_texts,
 
534
            left_matching_blocks, nostore_sha, random_id, check_content))
 
535
        return self._backing_vf.add_lines(key, parents, lines, parent_texts,
 
536
            left_matching_blocks, nostore_sha, random_id, check_content)
 
537
 
 
538
    def get_parent_map(self, keys):
 
539
        self.calls.append(("get_parent_map", copy(keys)))
 
540
        return self._backing_vf.get_parent_map(keys)
 
541
 
 
542
    def get_record_stream(self, keys, sort_order, include_delta_closure):
 
543
        self.calls.append(("get_record_stream", list(keys), sort_order,
 
544
            include_delta_closure))
 
545
        return self._backing_vf.get_record_stream(keys, sort_order,
 
546
            include_delta_closure)
 
547
 
 
548
    def get_sha1s(self, keys):
 
549
        self.calls.append(("get_sha1s", copy(keys)))
 
550
        return self._backing_vf.get_sha1s(keys)
 
551
 
 
552
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
 
553
        self.calls.append(("iter_lines_added_or_present_in_keys", copy(keys)))
 
554
        return self._backing_vf.iter_lines_added_or_present_in_keys(keys, pb=pb)
 
555
 
 
556
    def keys(self):
 
557
        self.calls.append(("keys",))
 
558
        return self._backing_vf.keys()
 
559
 
 
560
 
 
561
class KeyMapper(object):
 
562
    """KeyMappers map between keys and underlying partitioned storage."""
 
563
 
 
564
    def map(self, key):
 
565
        """Map key to an underlying storage identifier.
 
566
 
 
567
        :param key: A key tuple e.g. ('file-id', 'revision-id').
 
568
        :return: An underlying storage identifier, specific to the partitioning
 
569
            mechanism.
 
570
        """
 
571
        raise NotImplementedError(self.map)
 
572
 
 
573
    def unmap(self, partition_id):
 
574
        """Map a partitioned storage id back to a key prefix.
 
575
        
 
576
        :param partition_id: The underlying partition id.
 
577
        :return: As much of a key (or prefix) as is derivable from the partition
 
578
            id.
 
579
        """
 
580
        raise NotImplementedError(self.unmap)
 
581
 
 
582
 
 
583
class ConstantMapper(KeyMapper):
 
584
    """A key mapper that maps to a constant result."""
 
585
 
 
586
    def __init__(self, result):
 
587
        """Create a ConstantMapper which will return result for all maps."""
 
588
        self._result = result
 
589
 
 
590
    def map(self, key):
 
591
        """See KeyMapper.map()."""
 
592
        return self._result
 
593
 
 
594
 
 
595
class URLEscapeMapper(KeyMapper):
 
596
    """Base class for use with transport backed storage.
 
597
 
 
598
    This provides a map and unmap wrapper that respectively url escape and
 
599
    unescape their outputs and inputs.
 
600
    """
 
601
 
 
602
    def map(self, key):
 
603
        """See KeyMapper.map()."""
 
604
        return urllib.quote(self._map(key))
 
605
 
 
606
    def unmap(self, partition_id):
 
607
        """See KeyMapper.unmap()."""
 
608
        return self._unmap(urllib.unquote(partition_id))
 
609
 
 
610
 
 
611
class PrefixMapper(URLEscapeMapper):
 
612
    """A key mapper that extracts the first component of a key.
 
613
    
 
614
    This mapper is for use with a transport based backend.
 
615
    """
 
616
 
 
617
    def _map(self, key):
 
618
        """See KeyMapper.map()."""
 
619
        return key[0]
 
620
 
 
621
    def _unmap(self, partition_id):
 
622
        """See KeyMapper.unmap()."""
 
623
        return (partition_id,)
 
624
 
 
625
 
 
626
class HashPrefixMapper(URLEscapeMapper):
 
627
    """A key mapper that combines the first component of a key with a hash.
 
628
 
 
629
    This mapper is for use with a transport based backend.
 
630
    """
 
631
 
 
632
    def _map(self, key):
 
633
        """See KeyMapper.map()."""
 
634
        prefix = self._escape(key[0])
 
635
        return "%02x/%s" % (adler32(prefix) & 0xff, prefix)
 
636
 
 
637
    def _escape(self, prefix):
 
638
        """No escaping needed here."""
 
639
        return prefix
 
640
 
 
641
    def _unmap(self, partition_id):
 
642
        """See KeyMapper.unmap()."""
 
643
        return (self._unescape(osutils.basename(partition_id)),)
 
644
 
 
645
    def _unescape(self, basename):
 
646
        """No unescaping needed for HashPrefixMapper."""
 
647
        return basename
 
648
 
 
649
 
 
650
class HashEscapedPrefixMapper(HashPrefixMapper):
 
651
    """Combines the escaped first component of a key with a hash.
 
652
    
 
653
    This mapper is for use with a transport based backend.
 
654
    """
 
655
 
 
656
    _safe = "abcdefghijklmnopqrstuvwxyz0123456789-_@,."
 
657
 
 
658
    def _escape(self, prefix):
 
659
        """Turn a key element into a filesystem safe string.
 
660
 
 
661
        This is similar to a plain urllib.quote, except
 
662
        it uses specific safe characters, so that it doesn't
 
663
        have to translate a lot of valid file ids.
 
664
        """
 
665
        # @ does not get escaped. This is because it is a valid
 
666
        # filesystem character we use all the time, and it looks
 
667
        # a lot better than seeing %40 all the time.
 
668
        r = [((c in self._safe) and c or ('%%%02x' % ord(c)))
 
669
             for c in prefix]
 
670
        return ''.join(r)
 
671
 
 
672
    def _unescape(self, basename):
 
673
        """Escaped names are easily unescaped by urlutils."""
 
674
        return urllib.unquote(basename)
 
675
 
 
676
 
 
677
def make_versioned_files_factory(versioned_file_factory, mapper):
 
678
    """Create a ThunkedVersionedFiles factory.
 
679
 
 
680
    This will create a callable which when called creates a
 
681
    ThunkedVersionedFiles on a transport, using mapper to access individual
 
682
    versioned files, and versioned_file_factory to create each individual file.
 
683
    """
 
684
    def factory(transport):
 
685
        return ThunkedVersionedFiles(transport, versioned_file_factory, mapper,
 
686
            lambda:True)
 
687
    return factory
 
688
 
 
689
 
 
690
class VersionedFiles(object):
 
691
    """Storage for many versioned files.
 
692
 
 
693
    This object allows a single keyspace for accessing the history graph and
 
694
    contents of named bytestrings.
 
695
 
 
696
    Currently no implementation allows the graph of different key prefixes to
 
697
    intersect, but the API does allow such implementations in the future.
 
698
 
 
699
    The keyspace is expressed via simple tuples. Any instance of VersionedFiles
 
700
    may have a different length key-size, but that size will be constant for
 
701
    all texts added to or retrieved from it. For instance, bzrlib uses
 
702
    instances with a key-size of 2 for storing user files in a repository, with
 
703
    the first element the fileid, and the second the version of that file.
 
704
 
 
705
    The use of tuples allows a single code base to support several different
 
706
    uses with only the mapping logic changing from instance to instance.
 
707
    """
 
708
 
 
709
    def add_lines(self, key, parents, lines, parent_texts=None,
 
710
        left_matching_blocks=None, nostore_sha=None, random_id=False,
 
711
        check_content=True):
 
712
        """Add a text to the store.
 
713
 
 
714
        :param key: The key tuple of the text to add.
 
715
        :param parents: The parents key tuples of the text to add.
 
716
        :param lines: A list of lines. Each line must be a bytestring. And all
 
717
            of them except the last must be terminated with \n and contain no
 
718
            other \n's. The last line may either contain no \n's or a single
 
719
            terminating \n. If the lines list does meet this constraint the add
 
720
            routine may error or may succeed - but you will be unable to read
 
721
            the data back accurately. (Checking the lines have been split
 
722
            correctly is expensive and extremely unlikely to catch bugs so it
 
723
            is not done at runtime unless check_content is True.)
 
724
        :param parent_texts: An optional dictionary containing the opaque 
 
725
            representations of some or all of the parents of version_id to
 
726
            allow delta optimisations.  VERY IMPORTANT: the texts must be those
 
727
            returned by add_lines or data corruption can be caused.
 
728
        :param left_matching_blocks: a hint about which areas are common
 
729
            between the text and its left-hand-parent.  The format is
 
730
            the SequenceMatcher.get_matching_blocks format.
 
731
        :param nostore_sha: Raise ExistingContent and do not add the lines to
 
732
            the versioned file if the digest of the lines matches this.
 
733
        :param random_id: If True a random id has been selected rather than
 
734
            an id determined by some deterministic process such as a converter
 
735
            from a foreign VCS. When True the backend may choose not to check
 
736
            for uniqueness of the resulting key within the versioned file, so
 
737
            this should only be done when the result is expected to be unique
 
738
            anyway.
 
739
        :param check_content: If True, the lines supplied are verified to be
 
740
            bytestrings that are correctly formed lines.
 
741
        :return: The text sha1, the number of bytes in the text, and an opaque
 
742
                 representation of the inserted version which can be provided
 
743
                 back to future add_lines calls in the parent_texts dictionary.
 
744
        """
 
745
        raise NotImplementedError(self.add_lines)
 
746
 
 
747
    def add_mpdiffs(self, records):
 
748
        """Add mpdiffs to this VersionedFile.
 
749
 
 
750
        Records should be iterables of version, parents, expected_sha1,
 
751
        mpdiff. mpdiff should be a MultiParent instance.
 
752
        """
 
753
        vf_parents = {}
 
754
        mpvf = multiparent.MultiMemoryVersionedFile()
 
755
        versions = []
 
756
        for version, parent_ids, expected_sha1, mpdiff in records:
 
757
            versions.append(version)
 
758
            mpvf.add_diff(mpdiff, version, parent_ids)
 
759
        needed_parents = set()
 
760
        for version, parent_ids, expected_sha1, mpdiff in records:
 
761
            needed_parents.update(p for p in parent_ids
 
762
                                  if not mpvf.has_version(p))
 
763
        # It seems likely that adding all the present parents as fulltexts can
 
764
        # easily exhaust memory.
 
765
        split_lines = osutils.split_lines
 
766
        for record in self.get_record_stream(needed_parents, 'unordered',
 
767
            True):
 
768
            if record.storage_kind == 'absent':
 
769
                continue
 
770
            mpvf.add_version(split_lines(record.get_bytes_as('fulltext')),
 
771
                record.key, [])
 
772
        for (key, parent_keys, expected_sha1, mpdiff), lines in\
 
773
            zip(records, mpvf.get_line_list(versions)):
 
774
            if len(parent_keys) == 1:
 
775
                left_matching_blocks = list(mpdiff.get_matching_blocks(0,
 
776
                    mpvf.get_diff(parent_keys[0]).num_lines()))
 
777
            else:
 
778
                left_matching_blocks = None
 
779
            version_sha1, _, version_text = self.add_lines(key,
 
780
                parent_keys, lines, vf_parents,
 
781
                left_matching_blocks=left_matching_blocks)
 
782
            if version_sha1 != expected_sha1:
 
783
                raise errors.VersionedFileInvalidChecksum(version)
 
784
            vf_parents[key] = version_text
 
785
 
 
786
    def annotate(self, key):
 
787
        """Return a list of (version-key, line) tuples for the text of key.
 
788
 
 
789
        :raise RevisionNotPresent: If the key is not present.
 
790
        """
 
791
        raise NotImplementedError(self.annotate)
 
792
 
 
793
    def check(self, progress_bar=None):
 
794
        """Check this object for integrity."""
 
795
        raise NotImplementedError(self.check)
 
796
 
 
797
    @staticmethod
 
798
    def check_not_reserved_id(version_id):
 
799
        revision.check_not_reserved_id(version_id)
 
800
 
 
801
    def _check_lines_not_unicode(self, lines):
 
802
        """Check that lines being added to a versioned file are not unicode."""
 
803
        for line in lines:
 
804
            if line.__class__ is not str:
 
805
                raise errors.BzrBadParameterUnicode("lines")
 
806
 
 
807
    def _check_lines_are_lines(self, lines):
 
808
        """Check that the lines really are full lines without inline EOL."""
 
809
        for line in lines:
 
810
            if '\n' in line[:-1]:
 
811
                raise errors.BzrBadParameterContainsNewline("lines")
 
812
 
 
813
    def get_parent_map(self, keys):
 
814
        """Get a map of the parents of keys.
 
815
 
 
816
        :param keys: The keys to look up parents for.
 
817
        :return: A mapping from keys to parents. Absent keys are absent from
 
818
            the mapping.
 
819
        """
 
820
        raise NotImplementedError(self.get_parent_map)
 
821
 
 
822
    def get_record_stream(self, keys, ordering, include_delta_closure):
 
823
        """Get a stream of records for keys.
 
824
 
 
825
        :param keys: The keys to include.
 
826
        :param ordering: Either 'unordered' or 'topological'. A topologically
 
827
            sorted stream has compression parents strictly before their
 
828
            children.
 
829
        :param include_delta_closure: If True then the closure across any
 
830
            compression parents will be included (in the opaque data).
 
831
        :return: An iterator of ContentFactory objects, each of which is only
 
832
            valid until the iterator is advanced.
 
833
        """
 
834
        raise NotImplementedError(self.get_record_stream)
 
835
 
 
836
    def get_sha1s(self, keys):
 
837
        """Get the sha1's of the texts for the given keys.
 
838
 
 
839
        :param keys: The names of the keys to lookup
 
840
        :return: a dict from key to sha1 digest. Keys of texts which are not
 
841
            present in the store are not present in the returned
 
842
            dictionary.
 
843
        """
 
844
        raise NotImplementedError(self.get_sha1s)
 
845
 
 
846
    def insert_record_stream(self, stream):
 
847
        """Insert a record stream into this container.
 
848
 
 
849
        :param stream: A stream of records to insert. 
 
850
        :return: None
 
851
        :seealso VersionedFile.get_record_stream:
 
852
        """
 
853
        raise NotImplementedError
 
854
 
 
855
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
 
856
        """Iterate over the lines in the versioned files from keys.
 
857
 
 
858
        This may return lines from other keys. Each item the returned
 
859
        iterator yields is a tuple of a line and a text version that that line
 
860
        is present in (not introduced in).
 
861
 
 
862
        Ordering of results is in whatever order is most suitable for the
 
863
        underlying storage format.
 
864
 
 
865
        If a progress bar is supplied, it may be used to indicate progress.
 
866
        The caller is responsible for cleaning up progress bars (because this
 
867
        is an iterator).
 
868
 
 
869
        NOTES:
 
870
         * Lines are normalised by the underlying store: they will all have \n
 
871
           terminators.
 
872
         * Lines are returned in arbitrary order.
 
873
 
 
874
        :return: An iterator over (line, key).
 
875
        """
 
876
        raise NotImplementedError(self.iter_lines_added_or_present_in_keys)
 
877
 
 
878
    def keys(self):
 
879
        """Return a iterable of the keys for all the contained texts."""
 
880
        raise NotImplementedError(self.keys)
 
881
 
 
882
    def make_mpdiffs(self, keys):
 
883
        """Create multiparent diffs for specified keys."""
 
884
        keys_order = tuple(keys)
 
885
        keys = frozenset(keys)
 
886
        knit_keys = set(keys)
 
887
        parent_map = self.get_parent_map(keys)
 
888
        for parent_keys in parent_map.itervalues():
 
889
            if parent_keys:
 
890
                knit_keys.update(parent_keys)
 
891
        missing_keys = keys - set(parent_map)
 
892
        if missing_keys:
 
893
            raise errors.RevisionNotPresent(missing_keys.pop(), self)
 
894
        # We need to filter out ghosts, because we can't diff against them.
 
895
        maybe_ghosts = knit_keys - keys
 
896
        ghosts = maybe_ghosts - set(self.get_parent_map(maybe_ghosts))
 
897
        knit_keys.difference_update(ghosts)
 
898
        lines = {}
 
899
        split_lines = osutils.split_lines
 
900
        for record in self.get_record_stream(knit_keys, 'topological', True):
 
901
            lines[record.key] = split_lines(record.get_bytes_as('fulltext'))
 
902
            # line_block_dict = {}
 
903
            # for parent, blocks in record.extract_line_blocks():
 
904
            #   line_blocks[parent] = blocks
 
905
            # line_blocks[record.key] = line_block_dict
 
906
        diffs = []
 
907
        for key in keys_order:
 
908
            target = lines[key]
 
909
            parents = parent_map[key] or []
 
910
            # Note that filtering knit_keys can lead to a parent difference
 
911
            # between the creation and the application of the mpdiff.
 
912
            parent_lines = [lines[p] for p in parents if p in knit_keys]
 
913
            if len(parent_lines) > 0:
 
914
                left_parent_blocks = self._extract_blocks(key, parent_lines[0],
 
915
                    target)
 
916
            else:
 
917
                left_parent_blocks = None
 
918
            diffs.append(multiparent.MultiParent.from_lines(target,
 
919
                parent_lines, left_parent_blocks))
 
920
        return diffs
 
921
 
 
922
    def _extract_blocks(self, version_id, source, target):
 
923
        return None
 
924
 
 
925
 
 
926
class ThunkedVersionedFiles(VersionedFiles):
 
927
    """Storage for many versioned files thunked onto a 'VersionedFile' class.
 
928
 
 
929
    This object allows a single keyspace for accessing the history graph and
 
930
    contents of named bytestrings.
 
931
 
 
932
    Currently no implementation allows the graph of different key prefixes to
 
933
    intersect, but the API does allow such implementations in the future.
 
934
    """
 
935
 
 
936
    def __init__(self, transport, file_factory, mapper, is_locked):
 
937
        """Create a ThunkedVersionedFiles."""
 
938
        self._transport = transport
 
939
        self._file_factory = file_factory
 
940
        self._mapper = mapper
 
941
        self._is_locked = is_locked
 
942
 
 
943
    def add_lines(self, key, parents, lines, parent_texts=None,
 
944
        left_matching_blocks=None, nostore_sha=None, random_id=False,
 
945
        check_content=True):
 
946
        """See VersionedFiles.add_lines()."""
 
947
        path = self._mapper.map(key)
 
948
        version_id = key[-1]
 
949
        parents = [parent[-1] for parent in parents]
 
950
        vf = self._get_vf(path)
 
951
        try:
 
952
            try:
 
953
                return vf.add_lines_with_ghosts(version_id, parents, lines,
 
954
                    parent_texts=parent_texts,
 
955
                    left_matching_blocks=left_matching_blocks,
 
956
                    nostore_sha=nostore_sha, random_id=random_id,
 
957
                    check_content=check_content)
 
958
            except NotImplementedError:
 
959
                return vf.add_lines(version_id, parents, lines,
 
960
                    parent_texts=parent_texts,
 
961
                    left_matching_blocks=left_matching_blocks,
 
962
                    nostore_sha=nostore_sha, random_id=random_id,
 
963
                    check_content=check_content)
 
964
        except errors.NoSuchFile:
 
965
            # parent directory may be missing, try again.
 
966
            self._transport.mkdir(osutils.dirname(path))
 
967
            try:
 
968
                return vf.add_lines_with_ghosts(version_id, parents, lines,
 
969
                    parent_texts=parent_texts,
 
970
                    left_matching_blocks=left_matching_blocks,
 
971
                    nostore_sha=nostore_sha, random_id=random_id,
 
972
                    check_content=check_content)
 
973
            except NotImplementedError:
 
974
                return vf.add_lines(version_id, parents, lines,
 
975
                    parent_texts=parent_texts,
 
976
                    left_matching_blocks=left_matching_blocks,
 
977
                    nostore_sha=nostore_sha, random_id=random_id,
 
978
                    check_content=check_content)
 
979
 
 
980
    def annotate(self, key):
 
981
        """Return a list of (version-key, line) tuples for the text of key.
 
982
 
 
983
        :raise RevisionNotPresent: If the key is not present.
 
984
        """
 
985
        prefix = key[:-1]
 
986
        path = self._mapper.map(prefix)
 
987
        vf = self._get_vf(path)
 
988
        origins = vf.annotate(key[-1])
 
989
        result = []
 
990
        for origin, line in origins:
 
991
            result.append((prefix + (origin,), line))
 
992
        return result
 
993
 
 
994
    def check(self, progress_bar=None):
 
995
        """See VersionedFiles.check()."""
 
996
        for prefix, vf in self._iter_all_components():
 
997
            vf.check()
 
998
 
 
999
    def get_parent_map(self, keys):
 
1000
        """Get a map of the parents of keys.
 
1001
 
 
1002
        :param keys: The keys to look up parents for.
 
1003
        :return: A mapping from keys to parents. Absent keys are absent from
 
1004
            the mapping.
 
1005
        """
 
1006
        prefixes = self._partition_keys(keys)
 
1007
        result = {}
 
1008
        for prefix, suffixes in prefixes.items():
 
1009
            path = self._mapper.map(prefix)
 
1010
            vf = self._get_vf(path)
 
1011
            parent_map = vf.get_parent_map(suffixes)
 
1012
            for key, parents in parent_map.items():
 
1013
                result[prefix + (key,)] = tuple(
 
1014
                    prefix + (parent,) for parent in parents)
 
1015
        return result
 
1016
 
 
1017
    def _get_vf(self, path):
 
1018
        if not self._is_locked():
 
1019
            raise errors.ObjectNotLocked(self)
 
1020
        return self._file_factory(path, self._transport, create=True,
 
1021
            get_scope=lambda:None)
 
1022
 
 
1023
    def _partition_keys(self, keys):
 
1024
        """Turn keys into a dict of prefix:suffix_list."""
 
1025
        result = {}
 
1026
        for key in keys:
 
1027
            prefix_keys = result.setdefault(key[:-1], [])
 
1028
            prefix_keys.append(key[-1])
 
1029
        return result
 
1030
 
 
1031
    def _get_all_prefixes(self):
 
1032
        # Identify all key prefixes.
 
1033
        # XXX: A bit hacky, needs polish.
 
1034
        if type(self._mapper) == ConstantMapper:
 
1035
            paths = [self._mapper.map(())]
 
1036
            prefixes = [()]
 
1037
        else:
 
1038
            relpaths = set()
 
1039
            for quoted_relpath in self._transport.iter_files_recursive():
 
1040
                path, ext = os.path.splitext(quoted_relpath)
 
1041
                relpaths.add(path)
 
1042
            paths = list(relpaths)
 
1043
            prefixes = [self._mapper.unmap(path) for path in paths]
 
1044
        return zip(paths, prefixes)
 
1045
 
 
1046
    def get_record_stream(self, keys, ordering, include_delta_closure):
 
1047
        """See VersionedFiles.get_record_stream()."""
 
1048
        # Ordering will be taken care of by each partitioned store; group keys
 
1049
        # by partition.
 
1050
        keys = sorted(keys)
 
1051
        for prefix, suffixes, vf in self._iter_keys_vf(keys):
 
1052
            suffixes = [(suffix,) for suffix in suffixes]
 
1053
            for record in vf.get_record_stream(suffixes, ordering,
 
1054
                include_delta_closure):
 
1055
                if record.parents is not None:
 
1056
                    record.parents = tuple(
 
1057
                        prefix + parent for parent in record.parents)
 
1058
                record.key = prefix + record.key
 
1059
                yield record
 
1060
 
 
1061
    def _iter_keys_vf(self, keys):
 
1062
        prefixes = self._partition_keys(keys)
 
1063
        sha1s = {}
 
1064
        for prefix, suffixes in prefixes.items():
 
1065
            path = self._mapper.map(prefix)
 
1066
            vf = self._get_vf(path)
 
1067
            yield prefix, suffixes, vf
 
1068
 
 
1069
    def get_sha1s(self, keys):
 
1070
        """See VersionedFiles.get_sha1s()."""
 
1071
        sha1s = {}
 
1072
        for prefix,suffixes, vf in self._iter_keys_vf(keys):
 
1073
            vf_sha1s = vf.get_sha1s(suffixes)
 
1074
            for suffix, sha1 in vf_sha1s.iteritems():
 
1075
                sha1s[prefix + (suffix,)] = sha1
 
1076
        return sha1s
 
1077
 
 
1078
    def insert_record_stream(self, stream):
 
1079
        """Insert a record stream into this container.
 
1080
 
 
1081
        :param stream: A stream of records to insert. 
 
1082
        :return: None
 
1083
        :seealso VersionedFile.get_record_stream:
 
1084
        """
 
1085
        for record in stream:
 
1086
            prefix = record.key[:-1]
 
1087
            key = record.key[-1:]
 
1088
            if record.parents is not None:
 
1089
                parents = [parent[-1:] for parent in record.parents]
 
1090
            else:
 
1091
                parents = None
 
1092
            thunk_record = AdapterFactory(key, parents, record)
 
1093
            path = self._mapper.map(prefix)
 
1094
            # Note that this parses the file many times; we can do better but
 
1095
            # as this only impacts weaves in terms of performance, it is
 
1096
            # tolerable.
 
1097
            vf = self._get_vf(path)
 
1098
            vf.insert_record_stream([thunk_record])
 
1099
 
 
1100
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
 
1101
        """Iterate over the lines in the versioned files from keys.
 
1102
 
 
1103
        This may return lines from other keys. Each item the returned
 
1104
        iterator yields is a tuple of a line and a text version that that line
 
1105
        is present in (not introduced in).
 
1106
 
 
1107
        Ordering of results is in whatever order is most suitable for the
 
1108
        underlying storage format.
 
1109
 
 
1110
        If a progress bar is supplied, it may be used to indicate progress.
 
1111
        The caller is responsible for cleaning up progress bars (because this
 
1112
        is an iterator).
 
1113
 
 
1114
        NOTES:
 
1115
         * Lines are normalised by the underlying store: they will all have \n
 
1116
           terminators.
 
1117
         * Lines are returned in arbitrary order.
 
1118
 
 
1119
        :return: An iterator over (line, key).
 
1120
        """
 
1121
        for prefix, suffixes, vf in self._iter_keys_vf(keys):
 
1122
            for line, version in vf.iter_lines_added_or_present_in_versions(suffixes):
 
1123
                yield line, prefix + (version,)
 
1124
 
 
1125
    def _iter_all_components(self):
 
1126
        for path, prefix in self._get_all_prefixes():
 
1127
            yield prefix, self._get_vf(path)
 
1128
 
 
1129
    def keys(self):
 
1130
        """See VersionedFiles.keys()."""
 
1131
        result = set()
 
1132
        for prefix, vf in self._iter_all_components():
 
1133
            for suffix in vf.versions():
 
1134
                result.add(prefix + (suffix,))
 
1135
        return result
 
1136
 
 
1137
 
 
1138
class _PlanMergeVersionedFile(VersionedFiles):
 
1139
    """A VersionedFile for uncommitted and committed texts.
 
1140
 
 
1141
    It is intended to allow merges to be planned with working tree texts.
 
1142
    It implements only the small part of the VersionedFiles interface used by
 
1143
    PlanMerge.  It falls back to multiple versionedfiles for data not stored in
 
1144
    _PlanMergeVersionedFile itself.
 
1145
 
 
1146
    :ivar: fallback_versionedfiles a list of VersionedFiles objects that can be
 
1147
        queried for missing texts.
 
1148
    """
 
1149
 
 
1150
    def __init__(self, file_id):
 
1151
        """Create a _PlanMergeVersionedFile.
 
1152
 
 
1153
        :param file_id: Used with _PlanMerge code which is not yet fully
 
1154
            tuple-keyspace aware.
 
1155
        """
 
1156
        self._file_id = file_id
 
1157
        # fallback locations
 
1158
        self.fallback_versionedfiles = []
 
1159
        # Parents for locally held keys.
 
1160
        self._parents = {}
 
1161
        # line data for locally held keys.
 
1162
        self._lines = {}
 
1163
        # key lookup providers
 
1164
        self._providers = [DictParentsProvider(self._parents)]
 
1165
 
 
1166
    def plan_merge(self, ver_a, ver_b, base=None):
 
1167
        """See VersionedFile.plan_merge"""
 
1168
        from bzrlib.merge import _PlanMerge
 
1169
        if base is None:
 
1170
            return _PlanMerge(ver_a, ver_b, self, (self._file_id,)).plan_merge()
 
1171
        old_plan = list(_PlanMerge(ver_a, base, self, (self._file_id,)).plan_merge())
 
1172
        new_plan = list(_PlanMerge(ver_a, ver_b, self, (self._file_id,)).plan_merge())
 
1173
        return _PlanMerge._subtract_plans(old_plan, new_plan)
 
1174
 
 
1175
    def plan_lca_merge(self, ver_a, ver_b, base=None):
 
1176
        from bzrlib.merge import _PlanLCAMerge
 
1177
        graph = Graph(self)
 
1178
        new_plan = _PlanLCAMerge(ver_a, ver_b, self, (self._file_id,), graph).plan_merge()
 
1179
        if base is None:
 
1180
            return new_plan
 
1181
        old_plan = _PlanLCAMerge(ver_a, base, self, (self._file_id,), graph).plan_merge()
 
1182
        return _PlanLCAMerge._subtract_plans(list(old_plan), list(new_plan))
 
1183
 
 
1184
    def add_lines(self, key, parents, lines):
 
1185
        """See VersionedFiles.add_lines
 
1186
 
 
1187
        Lines are added locally, not to fallback versionedfiles.  Also, ghosts
 
1188
        are permitted.  Only reserved ids are permitted.
 
1189
        """
 
1190
        if type(key) is not tuple:
 
1191
            raise TypeError(key)
 
1192
        if not revision.is_reserved_id(key[-1]):
 
1193
            raise ValueError('Only reserved ids may be used')
 
1194
        if parents is None:
 
1195
            raise ValueError('Parents may not be None')
 
1196
        if lines is None:
 
1197
            raise ValueError('Lines may not be None')
 
1198
        self._parents[key] = tuple(parents)
 
1199
        self._lines[key] = lines
 
1200
 
 
1201
    def get_record_stream(self, keys, ordering, include_delta_closure):
 
1202
        pending = set(keys)
 
1203
        for key in keys:
 
1204
            if key in self._lines:
 
1205
                lines = self._lines[key]
 
1206
                parents = self._parents[key]
 
1207
                pending.remove(key)
 
1208
                yield FulltextContentFactory(key, parents, None,
 
1209
                    ''.join(lines))
 
1210
        for versionedfile in self.fallback_versionedfiles:
 
1211
            for record in versionedfile.get_record_stream(
 
1212
                pending, 'unordered', True):
 
1213
                if record.storage_kind == 'absent':
 
1214
                    continue
 
1215
                else:
 
1216
                    pending.remove(record.key)
 
1217
                    yield record
 
1218
            if not pending:
 
1219
                return
 
1220
        # report absent entries
 
1221
        for key in pending:
 
1222
            yield AbsentContentFactory(key)
 
1223
 
 
1224
    def get_parent_map(self, keys):
 
1225
        """See VersionedFiles.get_parent_map"""
 
1226
        # We create a new provider because a fallback may have been added.
 
1227
        # If we make fallbacks private we can update a stack list and avoid
 
1228
        # object creation thrashing.
 
1229
        keys = set(keys)
 
1230
        result = {}
 
1231
        if revision.NULL_REVISION in keys:
 
1232
            keys.remove(revision.NULL_REVISION)
 
1233
            result[revision.NULL_REVISION] = ()
 
1234
        self._providers = self._providers[:1] + self.fallback_versionedfiles
 
1235
        result.update(
 
1236
            _StackedParentsProvider(self._providers).get_parent_map(keys))
 
1237
        for key, parents in result.iteritems():
 
1238
            if parents == ():
 
1239
                result[key] = (revision.NULL_REVISION,)
 
1240
        return result
 
1241
 
 
1242
 
 
1243
class PlanWeaveMerge(TextMerge):
 
1244
    """Weave merge that takes a plan as its input.
 
1245
    
 
1246
    This exists so that VersionedFile.plan_merge is implementable.
 
1247
    Most callers will want to use WeaveMerge instead.
 
1248
    """
 
1249
 
 
1250
    def __init__(self, plan, a_marker=TextMerge.A_MARKER,
 
1251
                 b_marker=TextMerge.B_MARKER):
 
1252
        TextMerge.__init__(self, a_marker, b_marker)
 
1253
        self.plan = plan
 
1254
 
 
1255
    def _merge_struct(self):
 
1256
        lines_a = []
 
1257
        lines_b = []
 
1258
        ch_a = ch_b = False
 
1259
 
 
1260
        def outstanding_struct():
 
1261
            if not lines_a and not lines_b:
 
1262
                return
 
1263
            elif ch_a and not ch_b:
 
1264
                # one-sided change:
 
1265
                yield(lines_a,)
 
1266
            elif ch_b and not ch_a:
 
1267
                yield (lines_b,)
 
1268
            elif lines_a == lines_b:
 
1269
                yield(lines_a,)
 
1270
            else:
 
1271
                yield (lines_a, lines_b)
 
1272
       
 
1273
        # We previously considered either 'unchanged' or 'killed-both' lines
 
1274
        # to be possible places to resynchronize.  However, assuming agreement
 
1275
        # on killed-both lines may be too aggressive. -- mbp 20060324
 
1276
        for state, line in self.plan:
 
1277
            if state == 'unchanged':
 
1278
                # resync and flush queued conflicts changes if any
 
1279
                for struct in outstanding_struct():
 
1280
                    yield struct
 
1281
                lines_a = []
 
1282
                lines_b = []
 
1283
                ch_a = ch_b = False
 
1284
                
 
1285
            if state == 'unchanged':
 
1286
                if line:
 
1287
                    yield ([line],)
 
1288
            elif state == 'killed-a':
 
1289
                ch_a = True
 
1290
                lines_b.append(line)
 
1291
            elif state == 'killed-b':
 
1292
                ch_b = True
 
1293
                lines_a.append(line)
 
1294
            elif state == 'new-a':
 
1295
                ch_a = True
 
1296
                lines_a.append(line)
 
1297
            elif state == 'new-b':
 
1298
                ch_b = True
 
1299
                lines_b.append(line)
 
1300
            elif state == 'conflicted-a':
 
1301
                ch_b = ch_a = True
 
1302
                lines_a.append(line)
 
1303
            elif state == 'conflicted-b':
 
1304
                ch_b = ch_a = True
 
1305
                lines_b.append(line)
 
1306
            else:
 
1307
                if state not in ('irrelevant', 'ghost-a', 'ghost-b',
 
1308
                        'killed-base', 'killed-both'):
 
1309
                    raise AssertionError(state)
 
1310
        for struct in outstanding_struct():
 
1311
            yield struct
 
1312
 
 
1313
 
 
1314
class WeaveMerge(PlanWeaveMerge):
 
1315
    """Weave merge that takes a VersionedFile and two versions as its input."""
 
1316
 
 
1317
    def __init__(self, versionedfile, ver_a, ver_b, 
 
1318
        a_marker=PlanWeaveMerge.A_MARKER, b_marker=PlanWeaveMerge.B_MARKER):
 
1319
        plan = versionedfile.plan_merge(ver_a, ver_b)
 
1320
        PlanWeaveMerge.__init__(self, plan, a_marker, b_marker)
 
1321
 
 
1322