~bzr-pqm/bzr/bzr.dev

5652.2.2 by Martin Pool
Rename _fallback_vfs to _immediate_fallbacks
1
# Copyright (C) 2006-2011 Canonical Ltd
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
16
17
"""Versioned text file storage api."""
18
3350.8.2 by Robert Collins
stacked get_parent_map.
19
from copy import copy
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
20
from cStringIO import StringIO
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
21
import os
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
22
import struct
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
23
from zlib import adler32
24
1996.3.7 by John Arbash Meinel
lazy import versionedfile, late-load bzrlib.merge
25
from bzrlib.lazy_import import lazy_import
26
lazy_import(globals(), """
3224.5.20 by Andrew Bennetts
Remove or lazyify a couple more imports.
27
import urllib
1996.3.7 by John Arbash Meinel
lazy import versionedfile, late-load bzrlib.merge
28
29
from bzrlib import (
4454.3.65 by John Arbash Meinel
Tests that VF implementations support .get_annotator()
30
    annotate,
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
31
    bencode,
1996.3.7 by John Arbash Meinel
lazy import versionedfile, late-load bzrlib.merge
32
    errors,
4593.5.36 by John Arbash Meinel
a few more implementations of the interface.
33
    graph as _mod_graph,
3735.32.18 by John Arbash Meinel
We now support generating a network stream.
34
    groupcompress,
3830.3.12 by Martin Pool
Review cleanups: unify has_key impls, add missing_keys(), clean up exception blocks
35
    index,
4005.3.2 by Robert Collins
First passing NetworkRecordStream test - a fulltext from any record type which isn't a chunked or fulltext can be serialised and deserialised successfully.
36
    knit,
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
37
    osutils,
2520.4.3 by Aaron Bentley
Implement plain strategy for extracting and installing multiparent diffs
38
    multiparent,
1996.3.7 by John Arbash Meinel
lazy import versionedfile, late-load bzrlib.merge
39
    tsort,
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
40
    revision,
1996.3.7 by John Arbash Meinel
lazy import versionedfile, late-load bzrlib.merge
41
    )
42
""")
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
43
from bzrlib.registry import Registry
1551.6.7 by Aaron Bentley
Implemented two-way merge, refactored weave merge
44
from bzrlib.textmerge import TextMerge
1563.2.11 by Robert Collins
Consolidate reweave and join as we have no separate usage, make reweave tests apply to all versionedfile implementations and deprecate the old reweave apis.
45
46
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
47
adapter_registry = Registry()
48
adapter_registry.register_lazy(('knit-delta-gz', 'fulltext'), 'bzrlib.knit',
49
    'DeltaPlainToFullText')
50
adapter_registry.register_lazy(('knit-ft-gz', 'fulltext'), 'bzrlib.knit',
51
    'FTPlainToFullText')
52
adapter_registry.register_lazy(('knit-annotated-delta-gz', 'knit-delta-gz'),
53
    'bzrlib.knit', 'DeltaAnnotatedToUnannotated')
54
adapter_registry.register_lazy(('knit-annotated-delta-gz', 'fulltext'),
55
    'bzrlib.knit', 'DeltaAnnotatedToFullText')
56
adapter_registry.register_lazy(('knit-annotated-ft-gz', 'knit-ft-gz'),
57
    'bzrlib.knit', 'FTAnnotatedToUnannotated')
58
adapter_registry.register_lazy(('knit-annotated-ft-gz', 'fulltext'),
59
    'bzrlib.knit', 'FTAnnotatedToFullText')
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
60
# adapter_registry.register_lazy(('knit-annotated-ft-gz', 'chunked'),
61
#     'bzrlib.knit', 'FTAnnotatedToChunked')
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
62
63
3350.3.3 by Robert Collins
Functional get_record_stream interface tests covering full interface.
64
class ContentFactory(object):
65
    """Abstract interface for insertion and retrieval from a VersionedFile.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
66
3350.3.3 by Robert Collins
Functional get_record_stream interface tests covering full interface.
67
    :ivar sha1: None, or the sha1 of the content fulltext.
68
    :ivar storage_kind: The native storage kind of this factory. One of
69
        'mpdiff', 'knit-annotated-ft', 'knit-annotated-delta', 'knit-ft',
70
        'knit-delta', 'fulltext', 'knit-annotated-ft-gz',
71
        'knit-annotated-delta-gz', 'knit-ft-gz', 'knit-delta-gz'.
72
    :ivar key: The key of this content. Each key is a tuple with a single
73
        string in it.
74
    :ivar parents: A tuple of parent keys for self.key. If the object has
75
        no parent information, None (as opposed to () for an empty list of
76
        parents).
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
77
    """
3350.3.3 by Robert Collins
Functional get_record_stream interface tests covering full interface.
78
79
    def __init__(self):
80
        """Create a ContentFactory."""
81
        self.sha1 = None
82
        self.storage_kind = None
83
        self.key = None
84
        self.parents = None
85
86
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
87
class ChunkedContentFactory(ContentFactory):
88
    """Static data content factory.
89
90
    This takes a 'chunked' list of strings. The only requirement on 'chunked' is
91
    that ''.join(lines) becomes a valid fulltext. A tuple of a single string
92
    satisfies this, as does a list of lines.
93
94
    :ivar sha1: None, or the sha1 of the content fulltext.
95
    :ivar storage_kind: The native storage kind of this factory. Always
3890.2.2 by John Arbash Meinel
Change the signature to report the storage kind as 'chunked'
96
        'chunked'
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
97
    :ivar key: The key of this content. Each key is a tuple with a single
98
        string in it.
99
    :ivar parents: A tuple of parent keys for self.key. If the object has
100
        no parent information, None (as opposed to () for an empty list of
101
        parents).
102
     """
103
104
    def __init__(self, key, parents, sha1, chunks):
105
        """Create a ContentFactory."""
106
        self.sha1 = sha1
3890.2.2 by John Arbash Meinel
Change the signature to report the storage kind as 'chunked'
107
        self.storage_kind = 'chunked'
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
108
        self.key = key
109
        self.parents = parents
110
        self._chunks = chunks
111
112
    def get_bytes_as(self, storage_kind):
113
        if storage_kind == 'chunked':
114
            return self._chunks
115
        elif storage_kind == 'fulltext':
116
            return ''.join(self._chunks)
117
        raise errors.UnavailableRepresentation(self.key, storage_kind,
118
            self.storage_kind)
119
120
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
121
class FulltextContentFactory(ContentFactory):
122
    """Static data content factory.
123
124
    This takes a fulltext when created and just returns that during
125
    get_bytes_as('fulltext').
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
126
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
127
    :ivar sha1: None, or the sha1 of the content fulltext.
128
    :ivar storage_kind: The native storage kind of this factory. Always
129
        'fulltext'.
130
    :ivar key: The key of this content. Each key is a tuple with a single
131
        string in it.
132
    :ivar parents: A tuple of parent keys for self.key. If the object has
133
        no parent information, None (as opposed to () for an empty list of
134
        parents).
135
     """
136
137
    def __init__(self, key, parents, sha1, text):
138
        """Create a ContentFactory."""
139
        self.sha1 = sha1
140
        self.storage_kind = 'fulltext'
141
        self.key = key
142
        self.parents = parents
143
        self._text = text
144
145
    def get_bytes_as(self, storage_kind):
146
        if storage_kind == self.storage_kind:
147
            return self._text
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
148
        elif storage_kind == 'chunked':
3976.2.1 by Robert Collins
Use a list not a tuple for chunks returned from FullTextContentFactory objects, because otherwise code tries to assign to tuples.
149
            return [self._text]
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
150
        raise errors.UnavailableRepresentation(self.key, storage_kind,
151
            self.storage_kind)
152
153
154
class AbsentContentFactory(ContentFactory):
3350.3.12 by Robert Collins
Generate streams with absent records.
155
    """A placeholder content factory for unavailable texts.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
156
3350.3.12 by Robert Collins
Generate streams with absent records.
157
    :ivar sha1: None.
158
    :ivar storage_kind: 'absent'.
159
    :ivar key: The key of this content. Each key is a tuple with a single
160
        string in it.
161
    :ivar parents: None.
162
    """
163
164
    def __init__(self, key):
165
        """Create a ContentFactory."""
166
        self.sha1 = None
167
        self.storage_kind = 'absent'
168
        self.key = key
169
        self.parents = None
170
4537.2.1 by John Arbash Meinel
Add AbsentContentFactory.get_bytes_as()
171
    def get_bytes_as(self, storage_kind):
172
        raise ValueError('A request was made for key: %s, but that'
173
                         ' content is not available, and the calling'
174
                         ' code does not handle if it is missing.'
175
                         % (self.key,))
176
3350.3.12 by Robert Collins
Generate streams with absent records.
177
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
178
class AdapterFactory(ContentFactory):
179
    """A content factory to adapt between key prefix's."""
180
181
    def __init__(self, key, parents, adapted):
182
        """Create an adapter factory instance."""
183
        self.key = key
184
        self.parents = parents
185
        self._adapted = adapted
186
187
    def __getattr__(self, attr):
188
        """Return a member from the adapted object."""
189
        if attr in ('key', 'parents'):
190
            return self.__dict__[attr]
191
        else:
192
            return getattr(self._adapted, attr)
193
194
3350.3.14 by Robert Collins
Deprecate VersionedFile.join.
195
def filter_absent(record_stream):
196
    """Adapt a record stream to remove absent records."""
197
    for record in record_stream:
198
        if record.storage_kind != 'absent':
199
            yield record
200
201
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
202
class _MPDiffGenerator(object):
203
    """Pull out the functionality for generating mp_diffs."""
204
205
    def __init__(self, vf, keys):
206
        self.vf = vf
207
        # This is the order the keys were requested in
208
        self.ordered_keys = tuple(keys)
209
        # keys + their parents, what we need to compute the diffs
210
        self.needed_keys = ()
211
        # Map from key: mp_diff
212
        self.diffs = {}
213
        # Map from key: parents_needed (may have ghosts)
214
        self.parent_map = {}
215
        # Parents that aren't present
216
        self.ghost_parents = ()
217
        # Map from parent_key => number of children for this text
218
        self.refcounts = {}
219
        # Content chunks that are cached while we still need them
220
        self.chunks = {}
221
222
    def _find_needed_keys(self):
223
        """Find the set of keys we need to request.
224
225
        This includes all the original keys passed in, and the non-ghost
226
        parents of those keys.
227
228
        :return: (needed_keys, refcounts)
229
            needed_keys is the set of all texts we need to extract
230
            refcounts is a dict of {key: num_children} letting us know when we
231
                no longer need to cache a given parent text
232
        """
233
        # All the keys and their parents
234
        needed_keys = set(self.ordered_keys)
235
        parent_map = self.vf.get_parent_map(needed_keys)
236
        self.parent_map = parent_map
237
        # TODO: Should we be using a different construct here? I think this
238
        #       uses difference_update internally, and we expect the result to
239
        #       be tiny
240
        missing_keys = needed_keys.difference(parent_map)
241
        if missing_keys:
242
            raise errors.RevisionNotPresent(list(missing_keys)[0], self.vf)
243
        # Parents that might be missing. They are allowed to be ghosts, but we
244
        # should check for them
245
        refcounts = {}
246
        setdefault = refcounts.setdefault
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
247
        just_parents = set()
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
248
        for child_key, parent_keys in parent_map.iteritems():
249
            if not parent_keys:
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
250
                # parent_keys may be None if a given VersionedFile claims to
251
                # not support graph operations.
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
252
                continue
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
253
            just_parents.update(parent_keys)
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
254
            needed_keys.update(parent_keys)
255
            for p in parent_keys:
256
                refcounts[p] = setdefault(p, 0) + 1
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
257
        just_parents.difference_update(parent_map)
258
        # Remove any parents that are actually ghosts from the needed set
259
        self.present_parents = set(self.vf.get_parent_map(just_parents))
260
        self.ghost_parents = just_parents.difference(self.present_parents)
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
261
        needed_keys.difference_update(self.ghost_parents)
262
        self.needed_keys = needed_keys
263
        self.refcounts = refcounts
264
        return needed_keys, refcounts
265
266
    def _compute_diff(self, key, parent_lines, lines):
267
        """Compute a single mp_diff, and store it in self._diffs"""
268
        if len(parent_lines) > 0:
269
            # XXX: _extract_blocks is not usefully defined anywhere...
270
            #      It was meant to extract the left-parent diff without
271
            #      having to recompute it for Knit content (pack-0.92,
272
            #      etc). That seems to have regressed somewhere
273
            left_parent_blocks = self.vf._extract_blocks(key,
274
                parent_lines[0], lines)
275
        else:
276
            left_parent_blocks = None
277
        diff = multiparent.MultiParent.from_lines(lines,
278
                    parent_lines, left_parent_blocks)
279
        self.diffs[key] = diff
280
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
281
    def _process_one_record(self, key, this_chunks):
282
        parent_keys = None
283
        if key in self.parent_map:
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
284
            # This record should be ready to diff, since we requested
285
            # content in 'topological' order
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
286
            parent_keys = self.parent_map.pop(key)
5374.2.3 by John Arbash Meinel
Replace the generic VersionedFiles.make_mpdiffs interface.
287
            # If a VersionedFile claims 'no-graph' support, then it may return
288
            # None for any parent request, so we replace it with an empty tuple
289
            if parent_keys is None:
290
                parent_keys = ()
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
291
            parent_lines = []
292
            for p in parent_keys:
293
                # Alternatively we could check p not in self.needed_keys, but
294
                # ghost_parents should be tiny versus huge
295
                if p in self.ghost_parents:
296
                    continue
297
                refcount = self.refcounts[p]
298
                if refcount == 1: # Last child reference
299
                    self.refcounts.pop(p)
300
                    parent_chunks = self.chunks.pop(p)
301
                else:
302
                    self.refcounts[p] = refcount - 1
303
                    parent_chunks = self.chunks[p]
5374.2.4 by John Arbash Meinel
Cache the lines as extracted if they are used right now.
304
                p_lines = osutils.chunks_to_lines(parent_chunks)
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
305
                # TODO: Should we cache the line form? We did the
306
                #       computation to get it, but storing it this way will
307
                #       be less memory efficient...
5374.2.4 by John Arbash Meinel
Cache the lines as extracted if they are used right now.
308
                parent_lines.append(p_lines)
309
                del p_lines
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
310
            lines = osutils.chunks_to_lines(this_chunks)
5374.2.4 by John Arbash Meinel
Cache the lines as extracted if they are used right now.
311
            # Since we needed the lines, we'll go ahead and cache them this way
312
            this_chunks = lines
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
313
            self._compute_diff(key, parent_lines, lines)
5374.2.4 by John Arbash Meinel
Cache the lines as extracted if they are used right now.
314
            del lines
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
315
        # Is this content required for any more children?
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
316
        if key in self.refcounts:
317
            self.chunks[key] = this_chunks
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
318
319
    def _extract_diffs(self):
320
        needed_keys, refcounts = self._find_needed_keys()
321
        for record in self.vf.get_record_stream(needed_keys,
322
                                                'topological', True):
323
            if record.storage_kind == 'absent':
324
                raise errors.RevisionNotPresent(record.key, self.vf)
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
325
            self._process_one_record(record.key,
326
                                     record.get_bytes_as('chunked'))
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
327
        
328
    def compute_diffs(self):
329
        self._extract_diffs()
330
        dpop = self.diffs.pop
331
        return [dpop(k) for k in self.ordered_keys]
332
333
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
334
class VersionedFile(object):
335
    """Versioned text file storage.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
336
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
337
    A versioned file manages versions of line-based text files,
338
    keeping track of the originating version for each line.
339
340
    To clients the "lines" of the file are represented as a list of
341
    strings. These strings will typically have terminal newline
342
    characters, but this is not required.  In particular files commonly
343
    do not have a newline at the end of the file.
344
345
    Texts are identified by a version-id string.
346
    """
347
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
348
    @staticmethod
2229.2.3 by Aaron Bentley
change reserved_id to is_reserved_id, add check_not_reserved for DRY
349
    def check_not_reserved_id(version_id):
350
        revision.check_not_reserved_id(version_id)
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
351
1563.2.15 by Robert Collins
remove the weavestore assumptions about the number and nature of files it manages.
352
    def copy_to(self, name, transport):
353
        """Copy this versioned file to name on transport."""
354
        raise NotImplementedError(self.copy_to)
1863.1.1 by John Arbash Meinel
Allow Versioned files to do caching if explicitly asked, and implement for Knit
355
3350.3.3 by Robert Collins
Functional get_record_stream interface tests covering full interface.
356
    def get_record_stream(self, versions, ordering, include_delta_closure):
357
        """Get a stream of records for versions.
358
359
        :param versions: The versions to include. Each version is a tuple
360
            (version,).
361
        :param ordering: Either 'unordered' or 'topological'. A topologically
362
            sorted stream has compression parents strictly before their
363
            children.
364
        :param include_delta_closure: If True then the closure across any
3350.3.22 by Robert Collins
Review feedback.
365
            compression parents will be included (in the data content of the
366
            stream, not in the emitted records). This guarantees that
367
            'fulltext' can be used successfully on every record.
3350.3.3 by Robert Collins
Functional get_record_stream interface tests covering full interface.
368
        :return: An iterator of ContentFactory objects, each of which is only
369
            valid until the iterator is advanced.
370
        """
371
        raise NotImplementedError(self.get_record_stream)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
372
373
    def has_version(self, version_id):
374
        """Returns whether version is present."""
375
        raise NotImplementedError(self.has_version)
376
5195.3.26 by Parth Malwankar
reverted changes done to insert_record_stream API
377
    def insert_record_stream(self, stream):
3350.3.8 by Robert Collins
Basic stream insertion, no fast path yet for knit to knit.
378
        """Insert a record stream into this versioned file.
379
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
380
        :param stream: A stream of records to insert.
3350.3.8 by Robert Collins
Basic stream insertion, no fast path yet for knit to knit.
381
        :return: None
382
        :seealso VersionedFile.get_record_stream:
383
        """
384
        raise NotImplementedError
385
2520.4.140 by Aaron Bentley
Use matching blocks from mpdiff for knit delta creation
386
    def add_lines(self, version_id, parents, lines, parent_texts=None,
2805.6.7 by Robert Collins
Review feedback.
387
        left_matching_blocks=None, nostore_sha=None, random_id=False,
388
        check_content=True):
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
389
        """Add a single text on top of the versioned file.
390
391
        Must raise RevisionAlreadyPresent if the new version is
392
        already present in file history.
393
394
        Must raise RevisionNotPresent if any of the given parents are
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
395
        not present in file history.
2805.6.3 by Robert Collins
* The ``VersionedFile`` interface no longer protects against misuse when
396
397
        :param lines: A list of lines. Each line must be a bytestring. And all
398
            of them except the last must be terminated with \n and contain no
399
            other \n's. The last line may either contain no \n's or a single
400
            terminated \n. If the lines list does meet this constraint the add
401
            routine may error or may succeed - but you will be unable to read
402
            the data back accurately. (Checking the lines have been split
2805.6.7 by Robert Collins
Review feedback.
403
            correctly is expensive and extremely unlikely to catch bugs so it
404
            is not done at runtime unless check_content is True.)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
405
        :param parent_texts: An optional dictionary containing the opaque
2805.6.3 by Robert Collins
* The ``VersionedFile`` interface no longer protects against misuse when
406
            representations of some or all of the parents of version_id to
407
            allow delta optimisations.  VERY IMPORTANT: the texts must be those
408
            returned by add_lines or data corruption can be caused.
2520.4.148 by Aaron Bentley
Updates from review
409
        :param left_matching_blocks: a hint about which areas are common
410
            between the text and its left-hand-parent.  The format is
411
            the SequenceMatcher.get_matching_blocks format.
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
412
        :param nostore_sha: Raise ExistingContent and do not add the lines to
413
            the versioned file if the digest of the lines matches this.
2805.6.4 by Robert Collins
Don't check for existing versions when adding texts with random revision ids.
414
        :param random_id: If True a random id has been selected rather than
415
            an id determined by some deterministic process such as a converter
416
            from a foreign VCS. When True the backend may choose not to check
417
            for uniqueness of the resulting key within the versioned file, so
418
            this should only be done when the result is expected to be unique
419
            anyway.
2805.6.7 by Robert Collins
Review feedback.
420
        :param check_content: If True, the lines supplied are verified to be
421
            bytestrings that are correctly formed lines.
2776.1.1 by Robert Collins
* The ``add_lines`` methods on ``VersionedFile`` implementations has changed
422
        :return: The text sha1, the number of bytes in the text, and an opaque
423
                 representation of the inserted version which can be provided
424
                 back to future add_lines calls in the parent_texts dictionary.
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
425
        """
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
426
        self._check_write_ok()
2520.4.140 by Aaron Bentley
Use matching blocks from mpdiff for knit delta creation
427
        return self._add_lines(version_id, parents, lines, parent_texts,
2805.6.7 by Robert Collins
Review feedback.
428
            left_matching_blocks, nostore_sha, random_id, check_content)
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
429
2520.4.140 by Aaron Bentley
Use matching blocks from mpdiff for knit delta creation
430
    def _add_lines(self, version_id, parents, lines, parent_texts,
2805.6.7 by Robert Collins
Review feedback.
431
        left_matching_blocks, nostore_sha, random_id, check_content):
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
432
        """Helper to do the class specific add_lines."""
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
433
        raise NotImplementedError(self.add_lines)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
434
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
435
    def add_lines_with_ghosts(self, version_id, parents, lines,
2805.6.7 by Robert Collins
Review feedback.
436
        parent_texts=None, nostore_sha=None, random_id=False,
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
437
        check_content=True, left_matching_blocks=None):
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
438
        """Add lines to the versioned file, allowing ghosts to be present.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
439
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
440
        This takes the same parameters as add_lines and returns the same.
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
441
        """
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
442
        self._check_write_ok()
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
443
        return self._add_lines_with_ghosts(version_id, parents, lines,
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
444
            parent_texts, nostore_sha, random_id, check_content, left_matching_blocks)
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
445
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
446
    def _add_lines_with_ghosts(self, version_id, parents, lines, parent_texts,
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
447
        nostore_sha, random_id, check_content, left_matching_blocks):
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
448
        """Helper to do class specific add_lines_with_ghosts."""
1594.2.8 by Robert Collins
add ghost aware apis to knits.
449
        raise NotImplementedError(self.add_lines_with_ghosts)
450
1563.2.19 by Robert Collins
stub out a check for knits.
451
    def check(self, progress_bar=None):
452
        """Check the versioned file for integrity."""
453
        raise NotImplementedError(self.check)
454
1666.1.6 by Robert Collins
Make knit the default format.
455
    def _check_lines_not_unicode(self, lines):
456
        """Check that lines being added to a versioned file are not unicode."""
457
        for line in lines:
458
            if line.__class__ is not str:
459
                raise errors.BzrBadParameterUnicode("lines")
460
461
    def _check_lines_are_lines(self, lines):
462
        """Check that the lines really are full lines without inline EOL."""
463
        for line in lines:
464
            if '\n' in line[:-1]:
465
                raise errors.BzrBadParameterContainsNewline("lines")
466
2535.3.1 by Andrew Bennetts
Add get_format_signature to VersionedFile
467
    def get_format_signature(self):
468
        """Get a text description of the data encoding in this file.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
469
2831.7.1 by Ian Clatworthy
versionedfile.py code cleanups
470
        :since: 0.90
2535.3.1 by Andrew Bennetts
Add get_format_signature to VersionedFile
471
        """
472
        raise NotImplementedError(self.get_format_signature)
473
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
474
    def make_mpdiffs(self, version_ids):
2831.7.1 by Ian Clatworthy
versionedfile.py code cleanups
475
        """Create multiparent diffs for specified versions."""
5374.2.3 by John Arbash Meinel
Replace the generic VersionedFiles.make_mpdiffs interface.
476
        # XXX: Can't use _MPDiffGenerator just yet. This is because version_ids
477
        #      is a list of strings, not keys. And while self.get_record_stream
478
        #      is supported, it takes *keys*, while self.get_parent_map() takes
479
        #      strings... *sigh*
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
480
        knit_versions = set()
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
481
        knit_versions.update(version_ids)
482
        parent_map = self.get_parent_map(version_ids)
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
483
        for version_id in version_ids:
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
484
            try:
485
                knit_versions.update(parent_map[version_id])
486
            except KeyError:
3453.3.1 by Daniel Fischer
Raise the right exception in make_mpdiffs (bug #235687)
487
                raise errors.RevisionNotPresent(version_id, self)
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
488
        # We need to filter out ghosts, because we can't diff against them.
489
        knit_versions = set(self.get_parent_map(knit_versions).keys())
2520.4.90 by Aaron Bentley
Handle \r terminated lines in Weaves properly
490
        lines = dict(zip(knit_versions,
491
            self._get_lf_split_line_list(knit_versions)))
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
492
        diffs = []
493
        for version_id in version_ids:
494
            target = lines[version_id]
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
495
            try:
496
                parents = [lines[p] for p in parent_map[version_id] if p in
497
                    knit_versions]
498
            except KeyError:
3453.3.2 by John Arbash Meinel
Add a test case for the first loop, unable to find a way to trigger the second loop
499
                # I don't know how this could ever trigger.
500
                # parent_map[version_id] was already triggered in the previous
501
                # for loop, and lines[p] has the 'if p in knit_versions' check,
502
                # so we again won't have a KeyError.
3453.3.1 by Daniel Fischer
Raise the right exception in make_mpdiffs (bug #235687)
503
                raise errors.RevisionNotPresent(version_id, self)
2520.4.48 by Aaron Bentley
Support getting blocks from knit deltas with no final EOL
504
            if len(parents) > 0:
505
                left_parent_blocks = self._extract_blocks(version_id,
506
                                                          parents[0], target)
507
            else:
508
                left_parent_blocks = None
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
509
            diffs.append(multiparent.MultiParent.from_lines(target, parents,
510
                         left_parent_blocks))
511
        return diffs
512
2520.4.48 by Aaron Bentley
Support getting blocks from knit deltas with no final EOL
513
    def _extract_blocks(self, version_id, source, target):
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
514
        return None
2520.4.3 by Aaron Bentley
Implement plain strategy for extracting and installing multiparent diffs
515
2520.4.61 by Aaron Bentley
Do bulk insertion of records
516
    def add_mpdiffs(self, records):
2831.7.1 by Ian Clatworthy
versionedfile.py code cleanups
517
        """Add mpdiffs to this VersionedFile.
2520.4.126 by Aaron Bentley
Add more docs
518
519
        Records should be iterables of version, parents, expected_sha1,
2831.7.1 by Ian Clatworthy
versionedfile.py code cleanups
520
        mpdiff. mpdiff should be a MultiParent instance.
2520.4.126 by Aaron Bentley
Add more docs
521
        """
2831.7.1 by Ian Clatworthy
versionedfile.py code cleanups
522
        # Does this need to call self._check_write_ok()? (IanC 20070919)
2520.4.61 by Aaron Bentley
Do bulk insertion of records
523
        vf_parents = {}
2520.4.141 by Aaron Bentley
More batch operations adding mpdiffs
524
        mpvf = multiparent.MultiMemoryVersionedFile()
525
        versions = []
526
        for version, parent_ids, expected_sha1, mpdiff in records:
527
            versions.append(version)
528
            mpvf.add_diff(mpdiff, version, parent_ids)
529
        needed_parents = set()
2520.4.142 by Aaron Bentley
Clean up installation of inventory records
530
        for version, parent_ids, expected_sha1, mpdiff in records:
2520.4.141 by Aaron Bentley
More batch operations adding mpdiffs
531
            needed_parents.update(p for p in parent_ids
532
                                  if not mpvf.has_version(p))
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
533
        present_parents = set(self.get_parent_map(needed_parents).keys())
534
        for parent_id, lines in zip(present_parents,
535
                                 self._get_lf_split_line_list(present_parents)):
2520.4.141 by Aaron Bentley
More batch operations adding mpdiffs
536
            mpvf.add_version(lines, parent_id, [])
537
        for (version, parent_ids, expected_sha1, mpdiff), lines in\
538
            zip(records, mpvf.get_line_list(versions)):
539
            if len(parent_ids) == 1:
2520.4.140 by Aaron Bentley
Use matching blocks from mpdiff for knit delta creation
540
                left_matching_blocks = list(mpdiff.get_matching_blocks(0,
2520.4.141 by Aaron Bentley
More batch operations adding mpdiffs
541
                    mpvf.get_diff(parent_ids[0]).num_lines()))
2520.4.140 by Aaron Bentley
Use matching blocks from mpdiff for knit delta creation
542
            else:
543
                left_matching_blocks = None
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
544
            try:
545
                _, _, version_text = self.add_lines_with_ghosts(version,
546
                    parent_ids, lines, vf_parents,
547
                    left_matching_blocks=left_matching_blocks)
548
            except NotImplementedError:
549
                # The vf can't handle ghosts, so add lines normally, which will
550
                # (reasonably) fail if there are ghosts in the data.
551
                _, _, version_text = self.add_lines(version,
552
                    parent_ids, lines, vf_parents,
553
                    left_matching_blocks=left_matching_blocks)
2520.4.61 by Aaron Bentley
Do bulk insertion of records
554
            vf_parents[version] = version_text
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
555
        sha1s = self.get_sha1s(versions)
556
        for version, parent_ids, expected_sha1, mpdiff in records:
557
            if expected_sha1 != sha1s[version]:
2520.4.71 by Aaron Bentley
Update test to accept VersionedFileInvalidChecksum instead of TestamentMismatch
558
                raise errors.VersionedFileInvalidChecksum(version)
2520.4.3 by Aaron Bentley
Implement plain strategy for extracting and installing multiparent diffs
559
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
560
    def get_text(self, version_id):
561
        """Return version contents as a text string.
562
563
        Raises RevisionNotPresent if version is not present in
564
        file history.
565
        """
566
        return ''.join(self.get_lines(version_id))
567
    get_string = get_text
568
1756.2.1 by Aaron Bentley
Implement get_texts
569
    def get_texts(self, version_ids):
570
        """Return the texts of listed versions as a list of strings.
571
572
        Raises RevisionNotPresent if version is not present in
573
        file history.
574
        """
575
        return [''.join(self.get_lines(v)) for v in version_ids]
576
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
577
    def get_lines(self, version_id):
578
        """Return version contents as a sequence of lines.
579
580
        Raises RevisionNotPresent if version is not present in
581
        file history.
582
        """
583
        raise NotImplementedError(self.get_lines)
584
2520.4.90 by Aaron Bentley
Handle \r terminated lines in Weaves properly
585
    def _get_lf_split_line_list(self, version_ids):
586
        return [StringIO(t).readlines() for t in self.get_texts(version_ids)]
2520.4.3 by Aaron Bentley
Implement plain strategy for extracting and installing multiparent diffs
587
2530.1.1 by Aaron Bentley
Make topological sorting optional for get_ancestry
588
    def get_ancestry(self, version_ids, topo_sorted=True):
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
589
        """Return a list of all ancestors of given version(s). This
590
        will not include the null revision.
591
2490.2.32 by Aaron Bentley
Merge of not-sorting-ancestry branch
592
        This list will not be topologically sorted if topo_sorted=False is
593
        passed.
2530.1.1 by Aaron Bentley
Make topological sorting optional for get_ancestry
594
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
595
        Must raise RevisionNotPresent if any of the given versions are
596
        not present in file history."""
597
        if isinstance(version_ids, basestring):
598
            version_ids = [version_ids]
599
        raise NotImplementedError(self.get_ancestry)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
600
1594.2.8 by Robert Collins
add ghost aware apis to knits.
601
    def get_ancestry_with_ghosts(self, version_ids):
602
        """Return a list of all ancestors of given version(s). This
603
        will not include the null revision.
604
605
        Must raise RevisionNotPresent if any of the given versions are
606
        not present in file history.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
607
1594.2.8 by Robert Collins
add ghost aware apis to knits.
608
        Ghosts that are known about will be included in ancestry list,
609
        but are not explicitly marked.
610
        """
611
        raise NotImplementedError(self.get_ancestry_with_ghosts)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
612
3287.5.1 by Robert Collins
Add VersionedFile.get_parent_map.
613
    def get_parent_map(self, version_ids):
614
        """Get a map of the parents of version_ids.
615
616
        :param version_ids: The version ids to look up parents for.
617
        :return: A mapping from version id to parents.
618
        """
619
        raise NotImplementedError(self.get_parent_map)
620
1594.2.8 by Robert Collins
add ghost aware apis to knits.
621
    def get_parents_with_ghosts(self, version_id):
622
        """Return version names for parents of version_id.
623
624
        Will raise RevisionNotPresent if version_id is not present
625
        in the history.
626
627
        Ghosts that are known about will be included in the parent list,
628
        but are not explicitly marked.
629
        """
3287.5.1 by Robert Collins
Add VersionedFile.get_parent_map.
630
        try:
631
            return list(self.get_parent_map([version_id])[version_id])
632
        except KeyError:
633
            raise errors.RevisionNotPresent(version_id, self)
1594.2.8 by Robert Collins
add ghost aware apis to knits.
634
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
635
    def annotate(self, version_id):
3316.2.13 by Robert Collins
* ``VersionedFile.annotate_iter`` is deprecated. While in principal this
636
        """Return a list of (version-id, line) tuples for version_id.
637
638
        :raise RevisionNotPresent: If the given version is
639
        not present in file history.
640
        """
641
        raise NotImplementedError(self.annotate)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
642
2975.3.2 by Robert Collins
Review feedback - document the API change and improve readability in pack's _do_copy_nodes.
643
    def iter_lines_added_or_present_in_versions(self, version_ids=None,
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
644
                                                pb=None):
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
645
        """Iterate over the lines in the versioned file from version_ids.
646
2975.3.2 by Robert Collins
Review feedback - document the API change and improve readability in pack's _do_copy_nodes.
647
        This may return lines from other versions. Each item the returned
648
        iterator yields is a tuple of a line and a text version that that line
649
        is present in (not introduced in).
650
651
        Ordering of results is in whatever order is most suitable for the
652
        underlying storage format.
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
653
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
654
        If a progress bar is supplied, it may be used to indicate progress.
655
        The caller is responsible for cleaning up progress bars (because this
656
        is an iterator).
657
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
658
        NOTES: Lines are normalised: they will all have \n terminators.
659
               Lines are returned in arbitrary order.
2975.3.2 by Robert Collins
Review feedback - document the API change and improve readability in pack's _do_copy_nodes.
660
661
        :return: An iterator over (line, version_id).
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
662
        """
663
        raise NotImplementedError(self.iter_lines_added_or_present_in_versions)
664
1551.6.15 by Aaron Bentley
Moved plan_merge into Weave
665
    def plan_merge(self, ver_a, ver_b):
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
666
        """Return pseudo-annotation indicating how the two versions merge.
667
668
        This is computed between versions a and b and their common
669
        base.
670
671
        Weave lines present in none of them are skipped entirely.
1664.2.2 by Aaron Bentley
Added legend for plan-merge output
672
673
        Legend:
674
        killed-base Dead in base revision
675
        killed-both Killed in each revision
676
        killed-a    Killed in a
677
        killed-b    Killed in b
678
        unchanged   Alive in both a and b (possibly created in both)
679
        new-a       Created in a
680
        new-b       Created in b
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
681
        ghost-a     Killed in a, unborn in b
1664.2.5 by Aaron Bentley
Update plan-merge legend
682
        ghost-b     Killed in b, unborn in a
1664.2.2 by Aaron Bentley
Added legend for plan-merge output
683
        irrelevant  Not in either revision
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
684
        """
1551.6.15 by Aaron Bentley
Moved plan_merge into Weave
685
        raise NotImplementedError(VersionedFile.plan_merge)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
686
1996.3.7 by John Arbash Meinel
lazy import versionedfile, late-load bzrlib.merge
687
    def weave_merge(self, plan, a_marker=TextMerge.A_MARKER,
1551.6.14 by Aaron Bentley
Tweaks from merge review
688
                    b_marker=TextMerge.B_MARKER):
1551.6.12 by Aaron Bentley
Indicate conflicts from merge_lines, insead of guessing
689
        return PlanWeaveMerge(plan, a_marker, b_marker).merge_lines()[0]
1551.6.10 by Aaron Bentley
Renamed WeaveMerge to PlanMerge, added plan method, created planless WeaveMerge
690
1664.2.7 by Aaron Bentley
Merge bzr.dev
691
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
692
class RecordingVersionedFilesDecorator(object):
693
    """A minimal versioned files that records calls made on it.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
694
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
695
    Only enough methods have been added to support tests using it to date.
696
697
    :ivar calls: A list of the calls made; can be reset at any time by
698
        assigning [] to it.
699
    """
700
701
    def __init__(self, backing_vf):
3871.4.1 by John Arbash Meinel
Add a VFDecorator that can yield records in a specified order
702
        """Create a RecordingVersionedFilesDecorator decorating backing_vf.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
703
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
704
        :param backing_vf: The versioned file to answer all methods.
705
        """
706
        self._backing_vf = backing_vf
707
        self.calls = []
708
3350.8.2 by Robert Collins
stacked get_parent_map.
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
        self.calls.append(("add_lines", key, parents, lines, parent_texts,
713
            left_matching_blocks, nostore_sha, random_id, check_content))
714
        return self._backing_vf.add_lines(key, parents, lines, parent_texts,
715
            left_matching_blocks, nostore_sha, random_id, check_content)
716
3517.4.19 by Martin Pool
Update test for knit.check() to expect it to recurse into fallback vfs
717
    def check(self):
718
        self._backing_vf.check()
719
3350.8.2 by Robert Collins
stacked get_parent_map.
720
    def get_parent_map(self, keys):
721
        self.calls.append(("get_parent_map", copy(keys)))
722
        return self._backing_vf.get_parent_map(keys)
723
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
724
    def get_record_stream(self, keys, sort_order, include_delta_closure):
3350.8.7 by Robert Collins
get_record_stream for fulltexts working (but note extreme memory use!).
725
        self.calls.append(("get_record_stream", list(keys), sort_order,
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
726
            include_delta_closure))
727
        return self._backing_vf.get_record_stream(keys, sort_order,
728
            include_delta_closure)
729
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
730
    def get_sha1s(self, keys):
731
        self.calls.append(("get_sha1s", copy(keys)))
732
        return self._backing_vf.get_sha1s(keys)
733
3350.8.5 by Robert Collins
Iter_lines_added_or_present_in_keys stacks.
734
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
735
        self.calls.append(("iter_lines_added_or_present_in_keys", copy(keys)))
3350.8.14 by Robert Collins
Review feedback.
736
        return self._backing_vf.iter_lines_added_or_present_in_keys(keys, pb=pb)
3350.8.5 by Robert Collins
Iter_lines_added_or_present_in_keys stacks.
737
3350.8.4 by Robert Collins
Vf.keys() stacking support.
738
    def keys(self):
739
        self.calls.append(("keys",))
740
        return self._backing_vf.keys()
741
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
742
3871.4.1 by John Arbash Meinel
Add a VFDecorator that can yield records in a specified order
743
class OrderingVersionedFilesDecorator(RecordingVersionedFilesDecorator):
744
    """A VF that records calls, and returns keys in specific order.
745
746
    :ivar calls: A list of the calls made; can be reset at any time by
747
        assigning [] to it.
748
    """
749
750
    def __init__(self, backing_vf, key_priority):
751
        """Create a RecordingVersionedFilesDecorator decorating backing_vf.
752
753
        :param backing_vf: The versioned file to answer all methods.
754
        :param key_priority: A dictionary defining what order keys should be
755
            returned from an 'unordered' get_record_stream request.
756
            Keys with lower priority are returned first, keys not present in
757
            the map get an implicit priority of 0, and are returned in
758
            lexicographical order.
759
        """
760
        RecordingVersionedFilesDecorator.__init__(self, backing_vf)
761
        self._key_priority = key_priority
762
763
    def get_record_stream(self, keys, sort_order, include_delta_closure):
764
        self.calls.append(("get_record_stream", list(keys), sort_order,
765
            include_delta_closure))
766
        if sort_order == 'unordered':
767
            def sort_key(key):
768
                return (self._key_priority.get(key, 0), key)
769
            # Use a defined order by asking for the keys one-by-one from the
770
            # backing_vf
771
            for key in sorted(keys, key=sort_key):
772
                for record in self._backing_vf.get_record_stream([key],
773
                                'unordered', include_delta_closure):
774
                    yield record
775
        else:
776
            for record in self._backing_vf.get_record_stream(keys, sort_order,
777
                            include_delta_closure):
778
                yield record
779
780
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
781
class KeyMapper(object):
3350.6.10 by Martin Pool
VersionedFiles review cleanups
782
    """KeyMappers map between keys and underlying partitioned storage."""
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
783
784
    def map(self, key):
785
        """Map key to an underlying storage identifier.
786
787
        :param key: A key tuple e.g. ('file-id', 'revision-id').
788
        :return: An underlying storage identifier, specific to the partitioning
789
            mechanism.
790
        """
791
        raise NotImplementedError(self.map)
792
793
    def unmap(self, partition_id):
794
        """Map a partitioned storage id back to a key prefix.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
795
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
796
        :param partition_id: The underlying partition id.
3350.6.10 by Martin Pool
VersionedFiles review cleanups
797
        :return: As much of a key (or prefix) as is derivable from the partition
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
798
            id.
799
        """
800
        raise NotImplementedError(self.unmap)
801
802
803
class ConstantMapper(KeyMapper):
804
    """A key mapper that maps to a constant result."""
805
806
    def __init__(self, result):
807
        """Create a ConstantMapper which will return result for all maps."""
808
        self._result = result
809
810
    def map(self, key):
811
        """See KeyMapper.map()."""
812
        return self._result
813
814
815
class URLEscapeMapper(KeyMapper):
816
    """Base class for use with transport backed storage.
817
818
    This provides a map and unmap wrapper that respectively url escape and
819
    unescape their outputs and inputs.
820
    """
821
822
    def map(self, key):
823
        """See KeyMapper.map()."""
824
        return urllib.quote(self._map(key))
825
826
    def unmap(self, partition_id):
827
        """See KeyMapper.unmap()."""
828
        return self._unmap(urllib.unquote(partition_id))
829
830
831
class PrefixMapper(URLEscapeMapper):
832
    """A key mapper that extracts the first component of a key.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
833
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
834
    This mapper is for use with a transport based backend.
835
    """
836
837
    def _map(self, key):
838
        """See KeyMapper.map()."""
839
        return key[0]
840
841
    def _unmap(self, partition_id):
842
        """See KeyMapper.unmap()."""
843
        return (partition_id,)
844
845
846
class HashPrefixMapper(URLEscapeMapper):
847
    """A key mapper that combines the first component of a key with a hash.
848
849
    This mapper is for use with a transport based backend.
850
    """
851
852
    def _map(self, key):
853
        """See KeyMapper.map()."""
854
        prefix = self._escape(key[0])
855
        return "%02x/%s" % (adler32(prefix) & 0xff, prefix)
856
857
    def _escape(self, prefix):
858
        """No escaping needed here."""
859
        return prefix
860
861
    def _unmap(self, partition_id):
862
        """See KeyMapper.unmap()."""
863
        return (self._unescape(osutils.basename(partition_id)),)
864
865
    def _unescape(self, basename):
866
        """No unescaping needed for HashPrefixMapper."""
867
        return basename
868
869
870
class HashEscapedPrefixMapper(HashPrefixMapper):
871
    """Combines the escaped first component of a key with a hash.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
872
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
873
    This mapper is for use with a transport based backend.
874
    """
875
876
    _safe = "abcdefghijklmnopqrstuvwxyz0123456789-_@,."
877
878
    def _escape(self, prefix):
879
        """Turn a key element into a filesystem safe string.
880
881
        This is similar to a plain urllib.quote, except
882
        it uses specific safe characters, so that it doesn't
883
        have to translate a lot of valid file ids.
884
        """
885
        # @ does not get escaped. This is because it is a valid
886
        # filesystem character we use all the time, and it looks
887
        # a lot better than seeing %40 all the time.
888
        r = [((c in self._safe) and c or ('%%%02x' % ord(c)))
889
             for c in prefix]
890
        return ''.join(r)
891
892
    def _unescape(self, basename):
893
        """Escaped names are easily unescaped by urlutils."""
894
        return urllib.unquote(basename)
895
896
897
def make_versioned_files_factory(versioned_file_factory, mapper):
898
    """Create a ThunkedVersionedFiles factory.
899
900
    This will create a callable which when called creates a
901
    ThunkedVersionedFiles on a transport, using mapper to access individual
902
    versioned files, and versioned_file_factory to create each individual file.
903
    """
904
    def factory(transport):
905
        return ThunkedVersionedFiles(transport, versioned_file_factory, mapper,
906
            lambda:True)
907
    return factory
908
909
910
class VersionedFiles(object):
911
    """Storage for many versioned files.
912
913
    This object allows a single keyspace for accessing the history graph and
914
    contents of named bytestrings.
915
916
    Currently no implementation allows the graph of different key prefixes to
917
    intersect, but the API does allow such implementations in the future.
3350.6.7 by Robert Collins
Review feedback, making things more clear, adding documentation on what is used where.
918
919
    The keyspace is expressed via simple tuples. Any instance of VersionedFiles
920
    may have a different length key-size, but that size will be constant for
921
    all texts added to or retrieved from it. For instance, bzrlib uses
922
    instances with a key-size of 2 for storing user files in a repository, with
923
    the first element the fileid, and the second the version of that file.
924
925
    The use of tuples allows a single code base to support several different
926
    uses with only the mapping logic changing from instance to instance.
5652.2.2 by Martin Pool
Rename _fallback_vfs to _immediate_fallbacks
927
5652.2.4 by Martin Pool
Rename to _immediate_fallback_vfs
928
    :ivar _immediate_fallback_vfs: For subclasses that support stacking,
5652.2.2 by Martin Pool
Rename _fallback_vfs to _immediate_fallbacks
929
        this is a list of other VersionedFiles immediately underneath this
930
        one.  They may in turn each have further fallbacks.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
931
    """
932
933
    def add_lines(self, key, parents, lines, parent_texts=None,
934
        left_matching_blocks=None, nostore_sha=None, random_id=False,
935
        check_content=True):
936
        """Add a text to the store.
937
4241.4.1 by Ian Clatworthy
add sha generation support to versionedfiles
938
        :param key: The key tuple of the text to add. If the last element is
939
            None, a CHK string will be generated during the addition.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
940
        :param parents: The parents key tuples of the text to add.
941
        :param lines: A list of lines. Each line must be a bytestring. And all
942
            of them except the last must be terminated with \n and contain no
943
            other \n's. The last line may either contain no \n's or a single
944
            terminating \n. If the lines list does meet this constraint the add
945
            routine may error or may succeed - but you will be unable to read
946
            the data back accurately. (Checking the lines have been split
947
            correctly is expensive and extremely unlikely to catch bugs so it
948
            is not done at runtime unless check_content is True.)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
949
        :param parent_texts: An optional dictionary containing the opaque
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
950
            representations of some or all of the parents of version_id to
951
            allow delta optimisations.  VERY IMPORTANT: the texts must be those
952
            returned by add_lines or data corruption can be caused.
953
        :param left_matching_blocks: a hint about which areas are common
954
            between the text and its left-hand-parent.  The format is
955
            the SequenceMatcher.get_matching_blocks format.
956
        :param nostore_sha: Raise ExistingContent and do not add the lines to
957
            the versioned file if the digest of the lines matches this.
958
        :param random_id: If True a random id has been selected rather than
959
            an id determined by some deterministic process such as a converter
960
            from a foreign VCS. When True the backend may choose not to check
961
            for uniqueness of the resulting key within the versioned file, so
962
            this should only be done when the result is expected to be unique
963
            anyway.
964
        :param check_content: If True, the lines supplied are verified to be
965
            bytestrings that are correctly formed lines.
966
        :return: The text sha1, the number of bytes in the text, and an opaque
967
                 representation of the inserted version which can be provided
968
                 back to future add_lines calls in the parent_texts dictionary.
969
        """
970
        raise NotImplementedError(self.add_lines)
971
4398.8.6 by John Arbash Meinel
Switch the api from VF.add_text to VF._add_text and trim some extra 'features'.
972
    def _add_text(self, key, parents, text, nostore_sha=None, random_id=False):
973
        """Add a text to the store.
974
975
        This is a private function for use by CommitBuilder.
976
977
        :param key: The key tuple of the text to add. If the last element is
978
            None, a CHK string will be generated during the addition.
979
        :param parents: The parents key tuples of the text to add.
980
        :param text: A string containing the text to be committed.
981
        :param nostore_sha: Raise ExistingContent and do not add the lines to
982
            the versioned file if the digest of the lines matches this.
983
        :param random_id: If True a random id has been selected rather than
984
            an id determined by some deterministic process such as a converter
985
            from a foreign VCS. When True the backend may choose not to check
986
            for uniqueness of the resulting key within the versioned file, so
987
            this should only be done when the result is expected to be unique
988
            anyway.
989
        :param check_content: If True, the lines supplied are verified to be
990
            bytestrings that are correctly formed lines.
991
        :return: The text sha1, the number of bytes in the text, and an opaque
992
                 representation of the inserted version which can be provided
993
                 back to future _add_text calls in the parent_texts dictionary.
994
        """
995
        # The default implementation just thunks over to .add_lines(),
996
        # inefficient, but it works.
4398.8.1 by John Arbash Meinel
Add a VersionedFile.add_text() api.
997
        return self.add_lines(key, parents, osutils.split_lines(text),
998
                              nostore_sha=nostore_sha,
999
                              random_id=random_id,
4398.8.6 by John Arbash Meinel
Switch the api from VF.add_text to VF._add_text and trim some extra 'features'.
1000
                              check_content=True)
4398.8.1 by John Arbash Meinel
Add a VersionedFile.add_text() api.
1001
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1002
    def add_mpdiffs(self, records):
1003
        """Add mpdiffs to this VersionedFile.
1004
1005
        Records should be iterables of version, parents, expected_sha1,
1006
        mpdiff. mpdiff should be a MultiParent instance.
1007
        """
1008
        vf_parents = {}
1009
        mpvf = multiparent.MultiMemoryVersionedFile()
1010
        versions = []
1011
        for version, parent_ids, expected_sha1, mpdiff in records:
1012
            versions.append(version)
1013
            mpvf.add_diff(mpdiff, version, parent_ids)
1014
        needed_parents = set()
1015
        for version, parent_ids, expected_sha1, mpdiff in records:
1016
            needed_parents.update(p for p in parent_ids
1017
                                  if not mpvf.has_version(p))
1018
        # It seems likely that adding all the present parents as fulltexts can
1019
        # easily exhaust memory.
3890.2.9 by John Arbash Meinel
Start using osutils.chunks_as_lines rather than osutils.split_lines.
1020
        chunks_to_lines = osutils.chunks_to_lines
3350.8.11 by Robert Collins
Stacked add_mpdiffs.
1021
        for record in self.get_record_stream(needed_parents, 'unordered',
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1022
            True):
3350.8.11 by Robert Collins
Stacked add_mpdiffs.
1023
            if record.storage_kind == 'absent':
1024
                continue
3890.2.9 by John Arbash Meinel
Start using osutils.chunks_as_lines rather than osutils.split_lines.
1025
            mpvf.add_version(chunks_to_lines(record.get_bytes_as('chunked')),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1026
                record.key, [])
1027
        for (key, parent_keys, expected_sha1, mpdiff), lines in\
1028
            zip(records, mpvf.get_line_list(versions)):
1029
            if len(parent_keys) == 1:
1030
                left_matching_blocks = list(mpdiff.get_matching_blocks(0,
1031
                    mpvf.get_diff(parent_keys[0]).num_lines()))
1032
            else:
1033
                left_matching_blocks = None
1034
            version_sha1, _, version_text = self.add_lines(key,
1035
                parent_keys, lines, vf_parents,
1036
                left_matching_blocks=left_matching_blocks)
1037
            if version_sha1 != expected_sha1:
1038
                raise errors.VersionedFileInvalidChecksum(version)
1039
            vf_parents[key] = version_text
1040
1041
    def annotate(self, key):
1042
        """Return a list of (version-key, line) tuples for the text of key.
1043
1044
        :raise RevisionNotPresent: If the key is not present.
1045
        """
1046
        raise NotImplementedError(self.annotate)
1047
1048
    def check(self, progress_bar=None):
4332.3.26 by Robert Collins
Allow passing keys to check to VersionedFile.check().
1049
        """Check this object for integrity.
1050
        
1051
        :param progress_bar: A progress bar to output as the check progresses.
1052
        :param keys: Specific keys within the VersionedFiles to check. When
1053
            this parameter is not None, check() becomes a generator as per
1054
            get_record_stream. The difference to get_record_stream is that
1055
            more or deeper checks will be performed.
1056
        :return: None, or if keys was supplied a generator as per
1057
            get_record_stream.
1058
        """
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1059
        raise NotImplementedError(self.check)
1060
1061
    @staticmethod
1062
    def check_not_reserved_id(version_id):
1063
        revision.check_not_reserved_id(version_id)
1064
4744.2.5 by John Arbash Meinel
Change to a generic 'VersionedFiles.clear_cache()' api.
1065
    def clear_cache(self):
1066
        """Clear whatever caches this VersionedFile holds.
1067
1068
        This is generally called after an operation has been performed, when we
1069
        don't expect to be using this versioned file again soon.
1070
        """
1071
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1072
    def _check_lines_not_unicode(self, lines):
1073
        """Check that lines being added to a versioned file are not unicode."""
1074
        for line in lines:
1075
            if line.__class__ is not str:
1076
                raise errors.BzrBadParameterUnicode("lines")
1077
1078
    def _check_lines_are_lines(self, lines):
1079
        """Check that the lines really are full lines without inline EOL."""
1080
        for line in lines:
1081
            if '\n' in line[:-1]:
1082
                raise errors.BzrBadParameterContainsNewline("lines")
1083
4593.5.36 by John Arbash Meinel
a few more implementations of the interface.
1084
    def get_known_graph_ancestry(self, keys):
1085
        """Get a KnownGraph instance with the ancestry of keys."""
1086
        # most basic implementation is a loop around get_parent_map
1087
        pending = set(keys)
1088
        parent_map = {}
1089
        while pending:
1090
            this_parent_map = self.get_parent_map(pending)
1091
            parent_map.update(this_parent_map)
1092
            pending = set()
1093
            map(pending.update, this_parent_map.itervalues())
1094
            pending = pending.difference(parent_map)
1095
        kg = _mod_graph.KnownGraph(parent_map)
1096
        return kg
1097
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1098
    def get_parent_map(self, keys):
1099
        """Get a map of the parents of keys.
1100
1101
        :param keys: The keys to look up parents for.
1102
        :return: A mapping from keys to parents. Absent keys are absent from
1103
            the mapping.
1104
        """
1105
        raise NotImplementedError(self.get_parent_map)
1106
1107
    def get_record_stream(self, keys, ordering, include_delta_closure):
1108
        """Get a stream of records for keys.
1109
1110
        :param keys: The keys to include.
1111
        :param ordering: Either 'unordered' or 'topological'. A topologically
1112
            sorted stream has compression parents strictly before their
1113
            children.
1114
        :param include_delta_closure: If True then the closure across any
1115
            compression parents will be included (in the opaque data).
1116
        :return: An iterator of ContentFactory objects, each of which is only
1117
            valid until the iterator is advanced.
1118
        """
1119
        raise NotImplementedError(self.get_record_stream)
1120
1121
    def get_sha1s(self, keys):
1122
        """Get the sha1's of the texts for the given keys.
1123
1124
        :param keys: The names of the keys to lookup
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
1125
        :return: a dict from key to sha1 digest. Keys of texts which are not
3350.8.14 by Robert Collins
Review feedback.
1126
            present in the store are not present in the returned
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
1127
            dictionary.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1128
        """
1129
        raise NotImplementedError(self.get_sha1s)
1130
3830.3.12 by Martin Pool
Review cleanups: unify has_key impls, add missing_keys(), clean up exception blocks
1131
    has_key = index._has_key_from_parent_map
1132
4009.3.3 by Andrew Bennetts
Add docstrings.
1133
    def get_missing_compression_parent_keys(self):
1134
        """Return an iterable of keys of missing compression parents.
1135
1136
        Check this after calling insert_record_stream to find out if there are
1137
        any missing compression parents.  If there are, the records that
4009.3.12 by Robert Collins
Polish on inserting record streams with missing compression parents.
1138
        depend on them are not able to be inserted safely. The precise
1139
        behaviour depends on the concrete VersionedFiles class in use.
1140
1141
        Classes that do not support this will raise NotImplementedError.
4009.3.3 by Andrew Bennetts
Add docstrings.
1142
        """
1143
        raise NotImplementedError(self.get_missing_compression_parent_keys)
1144
5195.3.26 by Parth Malwankar
reverted changes done to insert_record_stream API
1145
    def insert_record_stream(self, stream):
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1146
        """Insert a record stream into this container.
1147
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1148
        :param stream: A stream of records to insert.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1149
        :return: None
1150
        :seealso VersionedFile.get_record_stream:
1151
        """
1152
        raise NotImplementedError
1153
1154
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
1155
        """Iterate over the lines in the versioned files from keys.
1156
1157
        This may return lines from other keys. Each item the returned
1158
        iterator yields is a tuple of a line and a text version that that line
1159
        is present in (not introduced in).
1160
1161
        Ordering of results is in whatever order is most suitable for the
1162
        underlying storage format.
1163
1164
        If a progress bar is supplied, it may be used to indicate progress.
1165
        The caller is responsible for cleaning up progress bars (because this
1166
        is an iterator).
1167
1168
        NOTES:
1169
         * Lines are normalised by the underlying store: they will all have \n
1170
           terminators.
1171
         * Lines are returned in arbitrary order.
1172
1173
        :return: An iterator over (line, key).
1174
        """
1175
        raise NotImplementedError(self.iter_lines_added_or_present_in_keys)
1176
1177
    def keys(self):
1178
        """Return a iterable of the keys for all the contained texts."""
1179
        raise NotImplementedError(self.keys)
1180
1181
    def make_mpdiffs(self, keys):
1182
        """Create multiparent diffs for specified keys."""
5374.2.3 by John Arbash Meinel
Replace the generic VersionedFiles.make_mpdiffs interface.
1183
        generator = _MPDiffGenerator(self, keys)
1184
        return generator.compute_diffs()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1185
5363.1.1 by Jelmer Vernooij
Provide VersionedFiles.get_annotator.
1186
    def get_annotator(self):
1187
        return annotate.Annotator(self)
1188
3830.3.12 by Martin Pool
Review cleanups: unify has_key impls, add missing_keys(), clean up exception blocks
1189
    missing_keys = index._missing_keys_from_parent_map
1190
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1191
    def _extract_blocks(self, version_id, source, target):
1192
        return None
1193
5050.70.2 by Martin Pool
Search all fallbacks for get_known_graph_ancestry
1194
    def _transitive_fallbacks(self):
1195
        """Return the whole stack of fallback versionedfiles.
1196
1197
        This VersionedFiles may have a list of fallbacks, but it doesn't
1198
        necessarily know about the whole stack going down, and it can't know
1199
        at open time because they may change after the objects are opened.
1200
        """
1201
        all_fallbacks = []
5652.2.4 by Martin Pool
Rename to _immediate_fallback_vfs
1202
        for a_vfs in self._immediate_fallback_vfs:
5050.70.2 by Martin Pool
Search all fallbacks for get_known_graph_ancestry
1203
            all_fallbacks.append(a_vfs)
1204
            all_fallbacks.extend(a_vfs._transitive_fallbacks())
1205
        return all_fallbacks
1206
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1207
1208
class ThunkedVersionedFiles(VersionedFiles):
1209
    """Storage for many versioned files thunked onto a 'VersionedFile' class.
1210
1211
    This object allows a single keyspace for accessing the history graph and
1212
    contents of named bytestrings.
1213
1214
    Currently no implementation allows the graph of different key prefixes to
1215
    intersect, but the API does allow such implementations in the future.
1216
    """
1217
1218
    def __init__(self, transport, file_factory, mapper, is_locked):
1219
        """Create a ThunkedVersionedFiles."""
1220
        self._transport = transport
1221
        self._file_factory = file_factory
1222
        self._mapper = mapper
1223
        self._is_locked = is_locked
1224
1225
    def add_lines(self, key, parents, lines, parent_texts=None,
1226
        left_matching_blocks=None, nostore_sha=None, random_id=False,
1227
        check_content=True):
1228
        """See VersionedFiles.add_lines()."""
1229
        path = self._mapper.map(key)
1230
        version_id = key[-1]
1231
        parents = [parent[-1] for parent in parents]
1232
        vf = self._get_vf(path)
1233
        try:
1234
            try:
1235
                return vf.add_lines_with_ghosts(version_id, parents, lines,
1236
                    parent_texts=parent_texts,
1237
                    left_matching_blocks=left_matching_blocks,
1238
                    nostore_sha=nostore_sha, random_id=random_id,
1239
                    check_content=check_content)
1240
            except NotImplementedError:
1241
                return vf.add_lines(version_id, parents, lines,
1242
                    parent_texts=parent_texts,
1243
                    left_matching_blocks=left_matching_blocks,
1244
                    nostore_sha=nostore_sha, random_id=random_id,
1245
                    check_content=check_content)
1246
        except errors.NoSuchFile:
1247
            # parent directory may be missing, try again.
1248
            self._transport.mkdir(osutils.dirname(path))
1249
            try:
1250
                return vf.add_lines_with_ghosts(version_id, parents, lines,
1251
                    parent_texts=parent_texts,
1252
                    left_matching_blocks=left_matching_blocks,
1253
                    nostore_sha=nostore_sha, random_id=random_id,
1254
                    check_content=check_content)
1255
            except NotImplementedError:
1256
                return vf.add_lines(version_id, parents, lines,
1257
                    parent_texts=parent_texts,
1258
                    left_matching_blocks=left_matching_blocks,
1259
                    nostore_sha=nostore_sha, random_id=random_id,
1260
                    check_content=check_content)
1261
1262
    def annotate(self, key):
1263
        """Return a list of (version-key, line) tuples for the text of key.
1264
1265
        :raise RevisionNotPresent: If the key is not present.
1266
        """
1267
        prefix = key[:-1]
1268
        path = self._mapper.map(prefix)
1269
        vf = self._get_vf(path)
1270
        origins = vf.annotate(key[-1])
1271
        result = []
1272
        for origin, line in origins:
1273
            result.append((prefix + (origin,), line))
1274
        return result
1275
4332.3.26 by Robert Collins
Allow passing keys to check to VersionedFile.check().
1276
    def check(self, progress_bar=None, keys=None):
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1277
        """See VersionedFiles.check()."""
4332.3.26 by Robert Collins
Allow passing keys to check to VersionedFile.check().
1278
        # XXX: This is over-enthusiastic but as we only thunk for Weaves today
1279
        # this is tolerable. Ideally we'd pass keys down to check() and 
1280
        # have the older VersiondFile interface updated too.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1281
        for prefix, vf in self._iter_all_components():
1282
            vf.check()
4332.3.26 by Robert Collins
Allow passing keys to check to VersionedFile.check().
1283
        if keys is not None:
1284
            return self.get_record_stream(keys, 'unordered', True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1285
1286
    def get_parent_map(self, keys):
1287
        """Get a map of the parents of keys.
1288
1289
        :param keys: The keys to look up parents for.
1290
        :return: A mapping from keys to parents. Absent keys are absent from
1291
            the mapping.
1292
        """
1293
        prefixes = self._partition_keys(keys)
1294
        result = {}
1295
        for prefix, suffixes in prefixes.items():
1296
            path = self._mapper.map(prefix)
1297
            vf = self._get_vf(path)
1298
            parent_map = vf.get_parent_map(suffixes)
1299
            for key, parents in parent_map.items():
1300
                result[prefix + (key,)] = tuple(
1301
                    prefix + (parent,) for parent in parents)
1302
        return result
1303
1304
    def _get_vf(self, path):
1305
        if not self._is_locked():
1306
            raise errors.ObjectNotLocked(self)
1307
        return self._file_factory(path, self._transport, create=True,
1308
            get_scope=lambda:None)
1309
1310
    def _partition_keys(self, keys):
1311
        """Turn keys into a dict of prefix:suffix_list."""
1312
        result = {}
1313
        for key in keys:
1314
            prefix_keys = result.setdefault(key[:-1], [])
1315
            prefix_keys.append(key[-1])
1316
        return result
1317
1318
    def _get_all_prefixes(self):
1319
        # Identify all key prefixes.
1320
        # XXX: A bit hacky, needs polish.
1321
        if type(self._mapper) == ConstantMapper:
1322
            paths = [self._mapper.map(())]
1323
            prefixes = [()]
1324
        else:
1325
            relpaths = set()
1326
            for quoted_relpath in self._transport.iter_files_recursive():
1327
                path, ext = os.path.splitext(quoted_relpath)
1328
                relpaths.add(path)
1329
            paths = list(relpaths)
1330
            prefixes = [self._mapper.unmap(path) for path in paths]
1331
        return zip(paths, prefixes)
1332
1333
    def get_record_stream(self, keys, ordering, include_delta_closure):
1334
        """See VersionedFiles.get_record_stream()."""
1335
        # Ordering will be taken care of by each partitioned store; group keys
1336
        # by partition.
1337
        keys = sorted(keys)
1338
        for prefix, suffixes, vf in self._iter_keys_vf(keys):
1339
            suffixes = [(suffix,) for suffix in suffixes]
1340
            for record in vf.get_record_stream(suffixes, ordering,
1341
                include_delta_closure):
1342
                if record.parents is not None:
1343
                    record.parents = tuple(
1344
                        prefix + parent for parent in record.parents)
1345
                record.key = prefix + record.key
1346
                yield record
1347
1348
    def _iter_keys_vf(self, keys):
1349
        prefixes = self._partition_keys(keys)
1350
        sha1s = {}
1351
        for prefix, suffixes in prefixes.items():
1352
            path = self._mapper.map(prefix)
1353
            vf = self._get_vf(path)
1354
            yield prefix, suffixes, vf
1355
1356
    def get_sha1s(self, keys):
1357
        """See VersionedFiles.get_sha1s()."""
1358
        sha1s = {}
1359
        for prefix,suffixes, vf in self._iter_keys_vf(keys):
1360
            vf_sha1s = vf.get_sha1s(suffixes)
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
1361
            for suffix, sha1 in vf_sha1s.iteritems():
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1362
                sha1s[prefix + (suffix,)] = sha1
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
1363
        return sha1s
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1364
5195.3.26 by Parth Malwankar
reverted changes done to insert_record_stream API
1365
    def insert_record_stream(self, stream):
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1366
        """Insert a record stream into this container.
1367
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1368
        :param stream: A stream of records to insert.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1369
        :return: None
1370
        :seealso VersionedFile.get_record_stream:
1371
        """
1372
        for record in stream:
1373
            prefix = record.key[:-1]
1374
            key = record.key[-1:]
1375
            if record.parents is not None:
1376
                parents = [parent[-1:] for parent in record.parents]
1377
            else:
1378
                parents = None
1379
            thunk_record = AdapterFactory(key, parents, record)
1380
            path = self._mapper.map(prefix)
1381
            # Note that this parses the file many times; we can do better but
1382
            # as this only impacts weaves in terms of performance, it is
1383
            # tolerable.
1384
            vf = self._get_vf(path)
1385
            vf.insert_record_stream([thunk_record])
1386
1387
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
1388
        """Iterate over the lines in the versioned files from keys.
1389
1390
        This may return lines from other keys. Each item the returned
1391
        iterator yields is a tuple of a line and a text version that that line
1392
        is present in (not introduced in).
1393
1394
        Ordering of results is in whatever order is most suitable for the
1395
        underlying storage format.
1396
1397
        If a progress bar is supplied, it may be used to indicate progress.
1398
        The caller is responsible for cleaning up progress bars (because this
1399
        is an iterator).
1400
1401
        NOTES:
1402
         * Lines are normalised by the underlying store: they will all have \n
1403
           terminators.
1404
         * Lines are returned in arbitrary order.
1405
1406
        :return: An iterator over (line, key).
1407
        """
1408
        for prefix, suffixes, vf in self._iter_keys_vf(keys):
1409
            for line, version in vf.iter_lines_added_or_present_in_versions(suffixes):
1410
                yield line, prefix + (version,)
1411
1412
    def _iter_all_components(self):
1413
        for path, prefix in self._get_all_prefixes():
1414
            yield prefix, self._get_vf(path)
1415
1416
    def keys(self):
1417
        """See VersionedFiles.keys()."""
1418
        result = set()
1419
        for prefix, vf in self._iter_all_components():
1420
            for suffix in vf.versions():
1421
                result.add(prefix + (suffix,))
1422
        return result
1423
1424
1425
class _PlanMergeVersionedFile(VersionedFiles):
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1426
    """A VersionedFile for uncommitted and committed texts.
1427
1428
    It is intended to allow merges to be planned with working tree texts.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1429
    It implements only the small part of the VersionedFiles interface used by
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1430
    PlanMerge.  It falls back to multiple versionedfiles for data not stored in
1431
    _PlanMergeVersionedFile itself.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1432
1433
    :ivar: fallback_versionedfiles a list of VersionedFiles objects that can be
1434
        queried for missing texts.
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1435
    """
1436
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1437
    def __init__(self, file_id):
1438
        """Create a _PlanMergeVersionedFile.
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1439
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1440
        :param file_id: Used with _PlanMerge code which is not yet fully
1441
            tuple-keyspace aware.
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1442
        """
1443
        self._file_id = file_id
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1444
        # fallback locations
1445
        self.fallback_versionedfiles = []
1446
        # Parents for locally held keys.
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1447
        self._parents = {}
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1448
        # line data for locally held keys.
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1449
        self._lines = {}
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1450
        # key lookup providers
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
1451
        self._providers = [_mod_graph.DictParentsProvider(self._parents)]
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1452
3062.2.3 by Aaron Bentley
Sync up with bzr.dev API changes
1453
    def plan_merge(self, ver_a, ver_b, base=None):
3062.1.13 by Aaron Bentley
Make _PlanMerge an implementation detail of _PlanMergeVersionedFile
1454
        """See VersionedFile.plan_merge"""
3144.3.7 by Aaron Bentley
Update from review
1455
        from bzrlib.merge import _PlanMerge
3062.2.3 by Aaron Bentley
Sync up with bzr.dev API changes
1456
        if base is None:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1457
            return _PlanMerge(ver_a, ver_b, self, (self._file_id,)).plan_merge()
1458
        old_plan = list(_PlanMerge(ver_a, base, self, (self._file_id,)).plan_merge())
1459
        new_plan = list(_PlanMerge(ver_a, ver_b, self, (self._file_id,)).plan_merge())
3062.2.3 by Aaron Bentley
Sync up with bzr.dev API changes
1460
        return _PlanMerge._subtract_plans(old_plan, new_plan)
1461
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
1462
    def plan_lca_merge(self, ver_a, ver_b, base=None):
3144.3.7 by Aaron Bentley
Update from review
1463
        from bzrlib.merge import _PlanLCAMerge
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
1464
        graph = _mod_graph.Graph(self)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1465
        new_plan = _PlanLCAMerge(ver_a, ver_b, self, (self._file_id,), graph).plan_merge()
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
1466
        if base is None:
1467
            return new_plan
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1468
        old_plan = _PlanLCAMerge(ver_a, base, self, (self._file_id,), graph).plan_merge()
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
1469
        return _PlanLCAMerge._subtract_plans(list(old_plan), list(new_plan))
3062.1.13 by Aaron Bentley
Make _PlanMerge an implementation detail of _PlanMergeVersionedFile
1470
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1471
    def add_lines(self, key, parents, lines):
1472
        """See VersionedFiles.add_lines
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1473
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1474
        Lines are added locally, not to fallback versionedfiles.  Also, ghosts
1475
        are permitted.  Only reserved ids are permitted.
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1476
        """
3350.6.8 by Martin Pool
Change stray pdb calls to exceptions
1477
        if type(key) is not tuple:
1478
            raise TypeError(key)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1479
        if not revision.is_reserved_id(key[-1]):
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1480
            raise ValueError('Only reserved ids may be used')
1481
        if parents is None:
1482
            raise ValueError('Parents may not be None')
1483
        if lines is None:
1484
            raise ValueError('Lines may not be None')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1485
        self._parents[key] = tuple(parents)
1486
        self._lines[key] = lines
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1487
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1488
    def get_record_stream(self, keys, ordering, include_delta_closure):
1489
        pending = set(keys)
1490
        for key in keys:
1491
            if key in self._lines:
1492
                lines = self._lines[key]
1493
                parents = self._parents[key]
1494
                pending.remove(key)
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
1495
                yield ChunkedContentFactory(key, parents, None, lines)
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1496
        for versionedfile in self.fallback_versionedfiles:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1497
            for record in versionedfile.get_record_stream(
1498
                pending, 'unordered', True):
1499
                if record.storage_kind == 'absent':
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1500
                    continue
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1501
                else:
1502
                    pending.remove(record.key)
1503
                    yield record
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
1504
            if not pending:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1505
                return
1506
        # report absent entries
1507
        for key in pending:
1508
            yield AbsentContentFactory(key)
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1509
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1510
    def get_parent_map(self, keys):
1511
        """See VersionedFiles.get_parent_map"""
1512
        # We create a new provider because a fallback may have been added.
1513
        # If we make fallbacks private we can update a stack list and avoid
1514
        # object creation thrashing.
3350.6.6 by Robert Collins
Fix test_plan_file_merge
1515
        keys = set(keys)
1516
        result = {}
1517
        if revision.NULL_REVISION in keys:
1518
            keys.remove(revision.NULL_REVISION)
1519
            result[revision.NULL_REVISION] = ()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1520
        self._providers = self._providers[:1] + self.fallback_versionedfiles
3350.6.6 by Robert Collins
Fix test_plan_file_merge
1521
        result.update(
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
1522
            _mod_graph.StackedParentsProvider(
1523
                self._providers).get_parent_map(keys))
3350.6.5 by Robert Collins
Update to bzr.dev.
1524
        for key, parents in result.iteritems():
1525
            if parents == ():
1526
                result[key] = (revision.NULL_REVISION,)
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
1527
        return result
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
1528
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1529
1551.6.10 by Aaron Bentley
Renamed WeaveMerge to PlanMerge, added plan method, created planless WeaveMerge
1530
class PlanWeaveMerge(TextMerge):
1551.6.13 by Aaron Bentley
Cleanup
1531
    """Weave merge that takes a plan as its input.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1532
1551.6.14 by Aaron Bentley
Tweaks from merge review
1533
    This exists so that VersionedFile.plan_merge is implementable.
1534
    Most callers will want to use WeaveMerge instead.
1551.6.13 by Aaron Bentley
Cleanup
1535
    """
1536
1551.6.14 by Aaron Bentley
Tweaks from merge review
1537
    def __init__(self, plan, a_marker=TextMerge.A_MARKER,
1538
                 b_marker=TextMerge.B_MARKER):
1551.6.10 by Aaron Bentley
Renamed WeaveMerge to PlanMerge, added plan method, created planless WeaveMerge
1539
        TextMerge.__init__(self, a_marker, b_marker)
4634.101.7 by John Arbash Meinel
Start working on the ability to drop a .BASE file for --weave and --lca merge.
1540
        self.plan = list(plan)
1551.6.10 by Aaron Bentley
Renamed WeaveMerge to PlanMerge, added plan method, created planless WeaveMerge
1541
1551.6.7 by Aaron Bentley
Implemented two-way merge, refactored weave merge
1542
    def _merge_struct(self):
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
1543
        lines_a = []
1544
        lines_b = []
1545
        ch_a = ch_b = False
1664.2.8 by Aaron Bentley
Fix WeaveMerge when plan doesn't end with unchanged lines
1546
1547
        def outstanding_struct():
1548
            if not lines_a and not lines_b:
1549
                return
1550
            elif ch_a and not ch_b:
1551
                # one-sided change:
1552
                yield(lines_a,)
1553
            elif ch_b and not ch_a:
1554
                yield (lines_b,)
1555
            elif lines_a == lines_b:
1556
                yield(lines_a,)
1557
            else:
1558
                yield (lines_a, lines_b)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1559
1616.1.18 by Martin Pool
(weave-merge) don't treat killed-both lines as points of agreement;
1560
        # We previously considered either 'unchanged' or 'killed-both' lines
1561
        # to be possible places to resynchronize.  However, assuming agreement
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1562
        # on killed-both lines may be too aggressive. -- mbp 20060324
1551.6.7 by Aaron Bentley
Implemented two-way merge, refactored weave merge
1563
        for state, line in self.plan:
1616.1.18 by Martin Pool
(weave-merge) don't treat killed-both lines as points of agreement;
1564
            if state == 'unchanged':
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
1565
                # resync and flush queued conflicts changes if any
1664.2.8 by Aaron Bentley
Fix WeaveMerge when plan doesn't end with unchanged lines
1566
                for struct in outstanding_struct():
1567
                    yield struct
1551.6.11 by Aaron Bentley
Switched TextMerge_lines to work on a list
1568
                lines_a = []
1569
                lines_b = []
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
1570
                ch_a = ch_b = False
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1571
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
1572
            if state == 'unchanged':
1573
                if line:
1551.6.5 by Aaron Bentley
Got weave merge producing structural output
1574
                    yield ([line],)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
1575
            elif state == 'killed-a':
1576
                ch_a = True
1577
                lines_b.append(line)
1578
            elif state == 'killed-b':
1579
                ch_b = True
1580
                lines_a.append(line)
1581
            elif state == 'new-a':
1582
                ch_a = True
1583
                lines_a.append(line)
1584
            elif state == 'new-b':
1585
                ch_b = True
1586
                lines_b.append(line)
3144.3.2 by Aaron Bentley
Get conflict handling working
1587
            elif state == 'conflicted-a':
1588
                ch_b = ch_a = True
1589
                lines_a.append(line)
1590
            elif state == 'conflicted-b':
1591
                ch_b = ch_a = True
1592
                lines_b.append(line)
4312.1.1 by John Arbash Meinel
Add a per-implementation test that deleting lines conflicts with modifying lines.
1593
            elif state == 'killed-both':
1594
                # This counts as a change, even though there is no associated
1595
                # line
1596
                ch_b = ch_a = True
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
1597
            else:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
1598
                if state not in ('irrelevant', 'ghost-a', 'ghost-b',
4312.1.1 by John Arbash Meinel
Add a per-implementation test that deleting lines conflicts with modifying lines.
1599
                        'killed-base'):
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
1600
                    raise AssertionError(state)
1664.2.8 by Aaron Bentley
Fix WeaveMerge when plan doesn't end with unchanged lines
1601
        for struct in outstanding_struct():
1602
            yield struct
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
1603
4634.101.7 by John Arbash Meinel
Start working on the ability to drop a .BASE file for --weave and --lca merge.
1604
    def base_from_plan(self):
1605
        """Construct a BASE file from the plan text."""
1606
        base_lines = []
1607
        for state, line in self.plan:
4634.101.9 by John Arbash Meinel
Reverse the .BASE values for --lca and conflicted lines.
1608
            if state in ('killed-a', 'killed-b', 'killed-both', 'unchanged'):
4634.101.7 by John Arbash Meinel
Start working on the ability to drop a .BASE file for --weave and --lca merge.
1609
                # If unchanged, then this line is straight from base. If a or b
1610
                # or both killed the line, then it *used* to be in base.
1611
                base_lines.append(line)
1612
            else:
1613
                if state not in ('killed-base', 'irrelevant',
1614
                                 'ghost-a', 'ghost-b',
4634.101.9 by John Arbash Meinel
Reverse the .BASE values for --lca and conflicted lines.
1615
                                 'new-a', 'new-b',
1616
                                 'conflicted-a', 'conflicted-b'):
4634.101.7 by John Arbash Meinel
Start working on the ability to drop a .BASE file for --weave and --lca merge.
1617
                    # killed-base, irrelevant means it doesn't apply
1618
                    # ghost-a/ghost-b are harder to say for sure, but they
1619
                    # aren't in the 'inc_c' which means they aren't in the
4634.101.13 by John Arbash Meinel
Clean up a code comment.
1620
                    # shared base of a & b. So we don't include them.  And
1621
                    # obviously if the line is newly inserted, it isn't in base
1622
1623
                    # If 'conflicted-a' or b, then it is new vs one base, but
1624
                    # old versus another base. However, if we make it present
1625
                    # in the base, it will be deleted from the target, and it
4634.101.9 by John Arbash Meinel
Reverse the .BASE values for --lca and conflicted lines.
1626
                    # seems better to get a line doubled in the merge result,
1627
                    # rather than have it deleted entirely.
4634.101.13 by John Arbash Meinel
Clean up a code comment.
1628
                    # Example, each node is the 'text' at that point:
1629
                    #           MN
1630
                    #          /   \
1631
                    #        MaN   MbN
1632
                    #         |  X  |
1633
                    #        MabN MbaN
1634
                    #          \   /
1635
                    #           ???
1636
                    # There was a criss-cross conflict merge. Both sides
1637
                    # include the other, but put themselves first.
1638
                    # Weave marks this as a 'clean' merge, picking OTHER over
1639
                    # THIS. (Though the details depend on order inserted into
1640
                    # weave, etc.)
1641
                    # LCA generates a plan:
1642
                    # [('unchanged', M),
1643
                    #  ('conflicted-b', b),
1644
                    #  ('unchanged', a),
1645
                    #  ('conflicted-a', b),
1646
                    #  ('unchanged', N)]
1647
                    # If you mark 'conflicted-*' as part of BASE, then a 3-way
1648
                    # merge tool will cleanly generate "MaN" (as BASE vs THIS
1649
                    # removes one 'b', and BASE vs OTHER removes the other)
1650
                    # If you include neither, 3-way creates a clean "MbabN" as
1651
                    # THIS adds one 'b', and OTHER does too.
1652
                    # It seems that having the line 2 times is better than
1653
                    # having it omitted. (Easier to manually delete than notice
1654
                    # it needs to be added.)
4634.101.7 by John Arbash Meinel
Start working on the ability to drop a .BASE file for --weave and --lca merge.
1655
                    raise AssertionError('Unknown state: %s' % (state,))
1656
        return base_lines
1657
1664.2.14 by Aaron Bentley
spacing fix
1658
1551.6.10 by Aaron Bentley
Renamed WeaveMerge to PlanMerge, added plan method, created planless WeaveMerge
1659
class WeaveMerge(PlanWeaveMerge):
2831.7.1 by Ian Clatworthy
versionedfile.py code cleanups
1660
    """Weave merge that takes a VersionedFile and two versions as its input."""
1551.6.13 by Aaron Bentley
Cleanup
1661
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1662
    def __init__(self, versionedfile, ver_a, ver_b,
1551.6.14 by Aaron Bentley
Tweaks from merge review
1663
        a_marker=PlanWeaveMerge.A_MARKER, b_marker=PlanWeaveMerge.B_MARKER):
1551.6.15 by Aaron Bentley
Moved plan_merge into Weave
1664
        plan = versionedfile.plan_merge(ver_a, ver_b)
1551.6.10 by Aaron Bentley
Renamed WeaveMerge to PlanMerge, added plan method, created planless WeaveMerge
1665
        PlanWeaveMerge.__init__(self, plan, a_marker, b_marker)
1666
1667
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1668
class VirtualVersionedFiles(VersionedFiles):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1669
    """Dummy implementation for VersionedFiles that uses other functions for
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1670
    obtaining fulltexts and parent maps.
1671
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1672
    This is always on the bottom of the stack and uses string keys
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1673
    (rather than tuples) internally.
1674
    """
1675
1676
    def __init__(self, get_parent_map, get_lines):
1677
        """Create a VirtualVersionedFiles.
1678
1679
        :param get_parent_map: Same signature as Repository.get_parent_map.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1680
        :param get_lines: Should return lines for specified key or None if
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1681
                          not available.
1682
        """
1683
        super(VirtualVersionedFiles, self).__init__()
1684
        self._get_parent_map = get_parent_map
1685
        self._get_lines = get_lines
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1686
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1687
    def check(self, progressbar=None):
1688
        """See VersionedFiles.check.
1689
1690
        :note: Always returns True for VirtualVersionedFiles.
1691
        """
1692
        return True
1693
1694
    def add_mpdiffs(self, records):
1695
        """See VersionedFiles.mpdiffs.
1696
1697
        :note: Not implemented for VirtualVersionedFiles.
1698
        """
1699
        raise NotImplementedError(self.add_mpdiffs)
1700
1701
    def get_parent_map(self, keys):
1702
        """See VersionedFiles.get_parent_map."""
3518.1.2 by Jelmer Vernooij
Fix some stylistic issues pointed out by Ian.
1703
        return dict([((k,), tuple([(p,) for p in v]))
1704
            for k,v in self._get_parent_map([k for (k,) in keys]).iteritems()])
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1705
1706
    def get_sha1s(self, keys):
1707
        """See VersionedFiles.get_sha1s."""
1708
        ret = {}
1709
        for (k,) in keys:
1710
            lines = self._get_lines(k)
1711
            if lines is not None:
3518.1.2 by Jelmer Vernooij
Fix some stylistic issues pointed out by Ian.
1712
                if not isinstance(lines, list):
1713
                    raise AssertionError
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1714
                ret[(k,)] = osutils.sha_strings(lines)
1715
        return ret
1716
1717
    def get_record_stream(self, keys, ordering, include_delta_closure):
1718
        """See VersionedFiles.get_record_stream."""
1719
        for (k,) in list(keys):
1720
            lines = self._get_lines(k)
1721
            if lines is not None:
3518.1.2 by Jelmer Vernooij
Fix some stylistic issues pointed out by Ian.
1722
                if not isinstance(lines, list):
1723
                    raise AssertionError
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
1724
                yield ChunkedContentFactory((k,), None,
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1725
                        sha1=osutils.sha_strings(lines),
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
1726
                        chunks=lines)
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1727
            else:
1728
                yield AbsentContentFactory((k,))
1729
3949.4.1 by Jelmer Vernooij
Implement VirtualVersionedFiles.iter_lines_added_or_present_in_keys.
1730
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
1731
        """See VersionedFile.iter_lines_added_or_present_in_versions()."""
1732
        for i, (key,) in enumerate(keys):
1733
            if pb is not None:
4110.2.10 by Martin Pool
Tweak iter_lines progress messages
1734
                pb.update("Finding changed lines", i, len(keys))
3949.4.1 by Jelmer Vernooij
Implement VirtualVersionedFiles.iter_lines_added_or_present_in_keys.
1735
            for l in self._get_lines(key):
1736
                yield (l, key)
4005.3.2 by Robert Collins
First passing NetworkRecordStream test - a fulltext from any record type which isn't a chunked or fulltext can be serialised and deserialised successfully.
1737
1738
5375.1.4 by Andrew Bennetts
Put NoDupeAddLinesDecorator in versionedfiles.py, and make it slightly more reusable.
1739
class NoDupeAddLinesDecorator(object):
1740
    """Decorator for a VersionedFiles that skips doing an add_lines if the key
1741
    is already present.
1742
    """
1743
1744
    def __init__(self, store):
1745
        self._store = store
1746
1747
    def add_lines(self, key, parents, lines, parent_texts=None,
1748
            left_matching_blocks=None, nostore_sha=None, random_id=False,
1749
            check_content=True):
1750
        """See VersionedFiles.add_lines.
1751
        
1752
        This implementation may return None as the third element of the return
1753
        value when the original store wouldn't.
1754
        """
1755
        if nostore_sha:
1756
            raise NotImplementedError(
1757
                "NoDupeAddLinesDecorator.add_lines does not implement the "
1758
                "nostore_sha behaviour.")
1759
        if key[-1] is None:
1760
            sha1 = osutils.sha_strings(lines)
1761
            key = ("sha1:" + sha1,)
1762
        else:
1763
            sha1 = None
1764
        if key in self._store.get_parent_map([key]):
1765
            # This key has already been inserted, so don't do it again.
1766
            if sha1 is None:
1767
                sha1 = osutils.sha_strings(lines)
1768
            return sha1, sum(map(len, lines)), None
1769
        return self._store.add_lines(key, parents, lines,
1770
                parent_texts=parent_texts,
1771
                left_matching_blocks=left_matching_blocks,
1772
                nostore_sha=nostore_sha, random_id=random_id,
1773
                check_content=check_content)
1774
1775
    def __getattr__(self, name):
1776
        return getattr(self._store, name)
1777
1778
4005.3.2 by Robert Collins
First passing NetworkRecordStream test - a fulltext from any record type which isn't a chunked or fulltext can be serialised and deserialised successfully.
1779
def network_bytes_to_kind_and_offset(network_bytes):
1780
    """Strip of a record kind from the front of network_bytes.
1781
1782
    :param network_bytes: The bytes of a record.
1783
    :return: A tuple (storage_kind, offset_of_remaining_bytes)
1784
    """
1785
    line_end = network_bytes.find('\n')
1786
    storage_kind = network_bytes[:line_end]
1787
    return storage_kind, line_end + 1
1788
1789
1790
class NetworkRecordStream(object):
1791
    """A record_stream which reconstitures a serialised stream."""
1792
1793
    def __init__(self, bytes_iterator):
1794
        """Create a NetworkRecordStream.
1795
1796
        :param bytes_iterator: An iterator of bytes. Each item in this
1797
            iterator should have been obtained from a record_streams'
1798
            record.get_bytes_as(record.storage_kind) call.
1799
        """
1800
        self._bytes_iterator = bytes_iterator
4476.3.4 by Andrew Bennetts
Network serialisation, and most tests passing with InterDifferingSerializer commented out.
1801
        self._kind_factory = {
1802
            'fulltext': fulltext_network_to_record,
1803
            'groupcompress-block': groupcompress.network_block_to_records,
1804
            'knit-ft-gz': knit.knit_network_to_record,
1805
            'knit-delta-gz': knit.knit_network_to_record,
1806
            'knit-annotated-ft-gz': knit.knit_network_to_record,
1807
            'knit-annotated-delta-gz': knit.knit_network_to_record,
1808
            'knit-delta-closure': knit.knit_delta_closure_to_records,
4005.3.2 by Robert Collins
First passing NetworkRecordStream test - a fulltext from any record type which isn't a chunked or fulltext can be serialised and deserialised successfully.
1809
            }
1810
1811
    def read(self):
1812
        """Read the stream.
1813
1814
        :return: An iterator as per VersionedFiles.get_record_stream().
1815
        """
1816
        for bytes in self._bytes_iterator:
1817
            storage_kind, line_end = network_bytes_to_kind_and_offset(bytes)
4005.3.6 by Robert Collins
Support delta_closure=True with NetworkRecordStream to transmit deltas over the wire when full text extraction is required on the far end.
1818
            for record in self._kind_factory[storage_kind](
1819
                storage_kind, bytes, line_end):
1820
                yield record
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
1821
1822
1823
def fulltext_network_to_record(kind, bytes, line_end):
1824
    """Convert a network fulltext record to record."""
1825
    meta_len, = struct.unpack('!L', bytes[line_end:line_end+4])
4060.1.4 by Robert Collins
Streaming fetch from remote servers.
1826
    record_meta = bytes[line_end+4:line_end+4+meta_len]
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
1827
    key, parents = bencode.bdecode_as_tuple(record_meta)
1828
    if parents == 'nil':
1829
        parents = None
4060.1.4 by Robert Collins
Streaming fetch from remote servers.
1830
    fulltext = bytes[line_end+4+meta_len:]
1831
    return [FulltextContentFactory(key, parents, None, fulltext)]
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
1832
1833
1834
def _length_prefix(bytes):
1835
    return struct.pack('!L', len(bytes))
1836
1837
4060.1.4 by Robert Collins
Streaming fetch from remote servers.
1838
def record_to_fulltext_bytes(record):
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
1839
    if record.parents is None:
1840
        parents = 'nil'
1841
    else:
1842
        parents = record.parents
1843
    record_meta = bencode.bencode((record.key, parents))
1844
    record_content = record.get_bytes_as('fulltext')
1845
    return "fulltext\n%s%s%s" % (
1846
        _length_prefix(record_meta), record_meta, record_content)
4111.1.1 by Robert Collins
Add a groupcompress sort order.
1847
1848
1849
def sort_groupcompress(parent_map):
1850
    """Sort and group the keys in parent_map into groupcompress order.
1851
1852
    groupcompress is defined (currently) as reverse-topological order, grouped
1853
    by the key prefix.
1854
1855
    :return: A sorted-list of keys
1856
    """
1857
    # gc-optimal ordering is approximately reverse topological,
1858
    # properly grouped by file-id.
1859
    per_prefix_map = {}
1860
    for item in parent_map.iteritems():
1861
        key = item[0]
1862
        if isinstance(key, str) or len(key) == 1:
1863
            prefix = ''
1864
        else:
1865
            prefix = key[0]
1866
        try:
1867
            per_prefix_map[prefix].append(item)
1868
        except KeyError:
1869
            per_prefix_map[prefix] = [item]
1870
1871
    present_keys = []
1872
    for prefix in sorted(per_prefix_map):
1873
        present_keys.extend(reversed(tsort.topo_sort(per_prefix_map[prefix])))
1874
    return present_keys
5757.8.1 by Jelmer Vernooij
Avoid bzrlib.knit imports when using groupcompress repositories.
1875
1876
1877
class _KeyRefs(object):
1878
1879
    def __init__(self, track_new_keys=False):
1880
        # dict mapping 'key' to 'set of keys referring to that key'
1881
        self.refs = {}
1882
        if track_new_keys:
1883
            # set remembering all new keys
1884
            self.new_keys = set()
1885
        else:
1886
            self.new_keys = None
1887
1888
    def clear(self):
1889
        if self.refs:
1890
            self.refs.clear()
1891
        if self.new_keys:
1892
            self.new_keys.clear()
1893
1894
    def add_references(self, key, refs):
1895
        # Record the new references
1896
        for referenced in refs:
1897
            try:
1898
                needed_by = self.refs[referenced]
1899
            except KeyError:
1900
                needed_by = self.refs[referenced] = set()
1901
            needed_by.add(key)
1902
        # Discard references satisfied by the new key
1903
        self.add_key(key)
1904
1905
    def get_new_keys(self):
1906
        return self.new_keys
1907
    
1908
    def get_unsatisfied_refs(self):
1909
        return self.refs.iterkeys()
1910
1911
    def _satisfy_refs_for_key(self, key):
1912
        try:
1913
            del self.refs[key]
1914
        except KeyError:
1915
            # No keys depended on this key.  That's ok.
1916
            pass
1917
1918
    def add_key(self, key):
1919
        # satisfy refs for key, and remember that we've seen this key.
1920
        self._satisfy_refs_for_key(key)
1921
        if self.new_keys is not None:
1922
            self.new_keys.add(key)
1923
1924
    def satisfy_refs_for_keys(self, keys):
1925
        for key in keys:
1926
            self._satisfy_refs_for_key(key)
1927
1928
    def get_referrers(self):
1929
        result = set()
1930
        for referrers in self.refs.itervalues():
1931
            result.update(referrers)
1932
        return result
1933
1934
1935