~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/versionedfile.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-03-16 16:58:03 UTC
  • mfrom: (3224.3.1 news-typo)
  • Revision ID: pqm@pqm.ubuntu.com-20080316165803-tisoc9mpob9z544o
(Matt Nordhoff) Trivial NEWS typo fix

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
#
 
3
# Authors:
 
4
#   Johan Rydberg <jrydberg@gnu.org>
2
5
#
3
6
# This program is free software; you can redistribute it and/or modify
4
7
# it under the terms of the GNU General Public License as published by
12
15
#
13
16
# You should have received a copy of the GNU General Public License
14
17
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
19
 
17
20
"""Versioned text file storage api."""
18
21
 
19
 
from copy import copy
20
 
from cStringIO import StringIO
21
 
import os
22
 
import struct
23
 
from zlib import adler32
24
 
 
25
22
from bzrlib.lazy_import import lazy_import
26
23
lazy_import(globals(), """
27
 
import urllib
28
24
 
29
25
from bzrlib import (
30
 
    annotate,
31
 
    bencode,
32
26
    errors,
33
 
    graph as _mod_graph,
34
 
    groupcompress,
35
 
    index,
36
 
    knit,
37
27
    osutils,
38
28
    multiparent,
39
29
    tsort,
40
30
    revision,
 
31
    ui,
41
32
    )
 
33
from bzrlib.transport.memory import MemoryTransport
42
34
""")
43
 
from bzrlib.registry import Registry
 
35
 
 
36
from cStringIO import StringIO
 
37
 
 
38
from bzrlib.inter import InterObject
44
39
from bzrlib.textmerge import TextMerge
45
40
 
46
41
 
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')
60
 
# adapter_registry.register_lazy(('knit-annotated-ft-gz', 'chunked'),
61
 
#     'bzrlib.knit', 'FTAnnotatedToChunked')
62
 
 
63
 
 
64
 
class ContentFactory(object):
65
 
    """Abstract interface for insertion and retrieval from a VersionedFile.
66
 
 
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).
77
 
    """
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
 
 
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
96
 
        'chunked'
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
107
 
        self.storage_kind = 'chunked'
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
 
 
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').
126
 
 
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
148
 
        elif storage_kind == 'chunked':
149
 
            return [self._text]
150
 
        raise errors.UnavailableRepresentation(self.key, storage_kind,
151
 
            self.storage_kind)
152
 
 
153
 
 
154
 
class AbsentContentFactory(ContentFactory):
155
 
    """A placeholder content factory for unavailable texts.
156
 
 
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
 
 
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
 
 
177
 
 
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
 
 
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
 
 
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
247
 
        just_parents = set()
248
 
        for child_key, parent_keys in parent_map.iteritems():
249
 
            if not parent_keys:
250
 
                # parent_keys may be None if a given VersionedFile claims to
251
 
                # not support graph operations.
252
 
                continue
253
 
            just_parents.update(parent_keys)
254
 
            needed_keys.update(parent_keys)
255
 
            for p in parent_keys:
256
 
                refcounts[p] = setdefault(p, 0) + 1
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)
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
 
 
281
 
    def _process_one_record(self, key, this_chunks):
282
 
        parent_keys = None
283
 
        if key in self.parent_map:
284
 
            # This record should be ready to diff, since we requested
285
 
            # content in 'topological' order
286
 
            parent_keys = self.parent_map.pop(key)
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 = ()
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]
304
 
                p_lines = osutils.chunks_to_lines(parent_chunks)
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...
308
 
                parent_lines.append(p_lines)
309
 
                del p_lines
310
 
            lines = osutils.chunks_to_lines(this_chunks)
311
 
            # Since we needed the lines, we'll go ahead and cache them this way
312
 
            this_chunks = lines
313
 
            self._compute_diff(key, parent_lines, lines)
314
 
            del lines
315
 
        # Is this content required for any more children?
316
 
        if key in self.refcounts:
317
 
            self.chunks[key] = this_chunks
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)
325
 
            self._process_one_record(record.key,
326
 
                                     record.get_bytes_as('chunked'))
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
 
 
334
42
class VersionedFile(object):
335
43
    """Versioned text file storage.
336
 
 
 
44
    
337
45
    A versioned file manages versions of line-based text files,
338
46
    keeping track of the originating version for each line.
339
47
 
345
53
    Texts are identified by a version-id string.
346
54
    """
347
55
 
 
56
    def __init__(self, access_mode):
 
57
        self.finished = False
 
58
        self._access_mode = access_mode
 
59
 
348
60
    @staticmethod
349
61
    def check_not_reserved_id(version_id):
350
62
        revision.check_not_reserved_id(version_id)
353
65
        """Copy this versioned file to name on transport."""
354
66
        raise NotImplementedError(self.copy_to)
355
67
 
356
 
    def get_record_stream(self, versions, ordering, include_delta_closure):
357
 
        """Get a stream of records for versions.
 
68
    def versions(self):
 
69
        """Return a unsorted list of versions."""
 
70
        raise NotImplementedError(self.versions)
358
71
 
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
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.
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)
 
72
    def has_ghost(self, version_id):
 
73
        """Returns whether version is present as a ghost."""
 
74
        raise NotImplementedError(self.has_ghost)
372
75
 
373
76
    def has_version(self, version_id):
374
77
        """Returns whether version is present."""
375
78
        raise NotImplementedError(self.has_version)
376
79
 
377
 
    def insert_record_stream(self, stream):
378
 
        """Insert a record stream into this versioned file.
379
 
 
380
 
        :param stream: A stream of records to insert.
381
 
        :return: None
382
 
        :seealso VersionedFile.get_record_stream:
383
 
        """
384
 
        raise NotImplementedError
385
 
 
386
80
    def add_lines(self, version_id, parents, lines, parent_texts=None,
387
81
        left_matching_blocks=None, nostore_sha=None, random_id=False,
388
82
        check_content=True):
402
96
            the data back accurately. (Checking the lines have been split
403
97
            correctly is expensive and extremely unlikely to catch bugs so it
404
98
            is not done at runtime unless check_content is True.)
405
 
        :param parent_texts: An optional dictionary containing the opaque
 
99
        :param parent_texts: An optional dictionary containing the opaque 
406
100
            representations of some or all of the parents of version_id to
407
101
            allow delta optimisations.  VERY IMPORTANT: the texts must be those
408
102
            returned by add_lines or data corruption can be caused.
434
128
 
435
129
    def add_lines_with_ghosts(self, version_id, parents, lines,
436
130
        parent_texts=None, nostore_sha=None, random_id=False,
437
 
        check_content=True, left_matching_blocks=None):
 
131
        check_content=True):
438
132
        """Add lines to the versioned file, allowing ghosts to be present.
439
 
 
 
133
        
440
134
        This takes the same parameters as add_lines and returns the same.
441
135
        """
442
136
        self._check_write_ok()
443
137
        return self._add_lines_with_ghosts(version_id, parents, lines,
444
 
            parent_texts, nostore_sha, random_id, check_content, left_matching_blocks)
 
138
            parent_texts, nostore_sha, random_id, check_content)
445
139
 
446
140
    def _add_lines_with_ghosts(self, version_id, parents, lines, parent_texts,
447
 
        nostore_sha, random_id, check_content, left_matching_blocks):
 
141
        nostore_sha, random_id, check_content):
448
142
        """Helper to do class specific add_lines_with_ghosts."""
449
143
        raise NotImplementedError(self.add_lines_with_ghosts)
450
144
 
464
158
            if '\n' in line[:-1]:
465
159
                raise errors.BzrBadParameterContainsNewline("lines")
466
160
 
 
161
    def _check_write_ok(self):
 
162
        """Is the versioned file marked as 'finished' ? Raise if it is."""
 
163
        if self.finished:
 
164
            raise errors.OutSideTransaction()
 
165
        if self._access_mode != 'w':
 
166
            raise errors.ReadOnlyObjectDirtiedError(self)
 
167
 
 
168
    def enable_cache(self):
 
169
        """Tell this versioned file that it should cache any data it reads.
 
170
        
 
171
        This is advisory, implementations do not have to support caching.
 
172
        """
 
173
        pass
 
174
    
 
175
    def clear_cache(self):
 
176
        """Remove any data cached in the versioned file object.
 
177
 
 
178
        This only needs to be supported if caches are supported
 
179
        """
 
180
        pass
 
181
 
 
182
    def clone_text(self, new_version_id, old_version_id, parents):
 
183
        """Add an identical text to old_version_id as new_version_id.
 
184
 
 
185
        Must raise RevisionNotPresent if the old version or any of the
 
186
        parents are not present in file history.
 
187
 
 
188
        Must raise RevisionAlreadyPresent if the new version is
 
189
        already present in file history."""
 
190
        self._check_write_ok()
 
191
        return self._clone_text(new_version_id, old_version_id, parents)
 
192
 
 
193
    def _clone_text(self, new_version_id, old_version_id, parents):
 
194
        """Helper function to do the _clone_text work."""
 
195
        raise NotImplementedError(self.clone_text)
 
196
 
 
197
    def create_empty(self, name, transport, mode=None):
 
198
        """Create a new versioned file of this exact type.
 
199
 
 
200
        :param name: the file name
 
201
        :param transport: the transport
 
202
        :param mode: optional file mode.
 
203
        """
 
204
        raise NotImplementedError(self.create_empty)
 
205
 
467
206
    def get_format_signature(self):
468
207
        """Get a text description of the data encoding in this file.
469
 
 
 
208
        
470
209
        :since: 0.90
471
210
        """
472
211
        raise NotImplementedError(self.get_format_signature)
473
212
 
474
213
    def make_mpdiffs(self, version_ids):
475
214
        """Create multiparent diffs for specified versions."""
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*
480
215
        knit_versions = set()
481
 
        knit_versions.update(version_ids)
482
 
        parent_map = self.get_parent_map(version_ids)
483
216
        for version_id in version_ids:
484
 
            try:
485
 
                knit_versions.update(parent_map[version_id])
486
 
            except KeyError:
487
 
                raise errors.RevisionNotPresent(version_id, self)
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())
 
217
            knit_versions.add(version_id)
 
218
            knit_versions.update(self.get_parents(version_id))
490
219
        lines = dict(zip(knit_versions,
491
220
            self._get_lf_split_line_list(knit_versions)))
492
221
        diffs = []
493
222
        for version_id in version_ids:
494
223
            target = lines[version_id]
495
 
            try:
496
 
                parents = [lines[p] for p in parent_map[version_id] if p in
497
 
                    knit_versions]
498
 
            except KeyError:
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.
503
 
                raise errors.RevisionNotPresent(version_id, self)
 
224
            parents = [lines[p] for p in self.get_parents(version_id)]
504
225
            if len(parents) > 0:
505
226
                left_parent_blocks = self._extract_blocks(version_id,
506
227
                                                          parents[0], target)
530
251
        for version, parent_ids, expected_sha1, mpdiff in records:
531
252
            needed_parents.update(p for p in parent_ids
532
253
                                  if not mpvf.has_version(p))
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)):
 
254
        for parent_id, lines in zip(needed_parents,
 
255
                                 self._get_lf_split_line_list(needed_parents)):
536
256
            mpvf.add_version(lines, parent_id, [])
537
257
        for (version, parent_ids, expected_sha1, mpdiff), lines in\
538
258
            zip(records, mpvf.get_line_list(versions)):
541
261
                    mpvf.get_diff(parent_ids[0]).num_lines()))
542
262
            else:
543
263
                left_matching_blocks = None
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)
 
264
            _, _, version_text = self.add_lines(version, parent_ids, lines,
 
265
                vf_parents, left_matching_blocks=left_matching_blocks)
554
266
            vf_parents[version] = version_text
555
 
        sha1s = self.get_sha1s(versions)
556
 
        for version, parent_ids, expected_sha1, mpdiff in records:
557
 
            if expected_sha1 != sha1s[version]:
 
267
        for (version, parent_ids, expected_sha1, mpdiff), sha1 in\
 
268
             zip(records, self.get_sha1s(versions)):
 
269
            if expected_sha1 != sha1:
558
270
                raise errors.VersionedFileInvalidChecksum(version)
559
271
 
 
272
    def get_sha1(self, version_id):
 
273
        """Get the stored sha1 sum for the given revision.
 
274
        
 
275
        :param version_id: The name of the version to lookup
 
276
        """
 
277
        raise NotImplementedError(self.get_sha1)
 
278
 
 
279
    def get_sha1s(self, version_ids):
 
280
        """Get the stored sha1 sums for the given revisions.
 
281
 
 
282
        :param version_ids: The names of the versions to lookup
 
283
        :return: a list of sha1s in order according to the version_ids
 
284
        """
 
285
        raise NotImplementedError(self.get_sha1s)
 
286
 
 
287
    def get_suffixes(self):
 
288
        """Return the file suffixes associated with this versioned file."""
 
289
        raise NotImplementedError(self.get_suffixes)
 
290
    
560
291
    def get_text(self, version_id):
561
292
        """Return version contents as a text string.
562
293
 
597
328
        if isinstance(version_ids, basestring):
598
329
            version_ids = [version_ids]
599
330
        raise NotImplementedError(self.get_ancestry)
600
 
 
 
331
        
601
332
    def get_ancestry_with_ghosts(self, version_ids):
602
333
        """Return a list of all ancestors of given version(s). This
603
334
        will not include the null revision.
604
335
 
605
336
        Must raise RevisionNotPresent if any of the given versions are
606
337
        not present in file history.
607
 
 
 
338
        
608
339
        Ghosts that are known about will be included in ancestry list,
609
340
        but are not explicitly marked.
610
341
        """
611
342
        raise NotImplementedError(self.get_ancestry_with_ghosts)
612
 
 
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)
 
343
        
 
344
    def get_graph(self, version_ids=None):
 
345
        """Return a graph from the versioned file. 
 
346
        
 
347
        Ghosts are not listed or referenced in the graph.
 
348
        :param version_ids: Versions to select.
 
349
                            None means retrieve all versions.
 
350
        """
 
351
        if version_ids is None:
 
352
            return dict(self.iter_parents(self.versions()))
 
353
        result = {}
 
354
        pending = set(version_ids)
 
355
        while pending:
 
356
            this_iteration = pending
 
357
            pending = set()
 
358
            for version, parents in self.iter_parents(this_iteration):
 
359
                result[version] = parents
 
360
                for parent in parents:
 
361
                    if parent in result:
 
362
                        continue
 
363
                    pending.add(parent)
 
364
        return result
 
365
 
 
366
    def get_graph_with_ghosts(self):
 
367
        """Return a graph for the entire versioned file.
 
368
        
 
369
        Ghosts are referenced in parents list but are not
 
370
        explicitly listed.
 
371
        """
 
372
        raise NotImplementedError(self.get_graph_with_ghosts)
 
373
 
 
374
    def get_parents(self, version_id):
 
375
        """Return version names for parents of a version.
 
376
 
 
377
        Must raise RevisionNotPresent if version is not present in
 
378
        file history.
 
379
        """
 
380
        raise NotImplementedError(self.get_parents)
620
381
 
621
382
    def get_parents_with_ghosts(self, version_id):
622
383
        """Return version names for parents of version_id.
627
388
        Ghosts that are known about will be included in the parent list,
628
389
        but are not explicitly marked.
629
390
        """
630
 
        try:
631
 
            return list(self.get_parent_map([version_id])[version_id])
632
 
        except KeyError:
633
 
            raise errors.RevisionNotPresent(version_id, self)
 
391
        raise NotImplementedError(self.get_parents_with_ghosts)
 
392
 
 
393
    def annotate_iter(self, version_id):
 
394
        """Yield list of (version-id, line) pairs for the specified
 
395
        version.
 
396
 
 
397
        Must raise RevisionNotPresent if the given version is
 
398
        not present in file history.
 
399
        """
 
400
        raise NotImplementedError(self.annotate_iter)
634
401
 
635
402
    def annotate(self, version_id):
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.
 
403
        return list(self.annotate_iter(version_id))
 
404
 
 
405
    def join(self, other, pb=None, msg=None, version_ids=None,
 
406
             ignore_missing=False):
 
407
        """Integrate versions from other into this versioned file.
 
408
 
 
409
        If version_ids is None all versions from other should be
 
410
        incorporated into this versioned file.
 
411
 
 
412
        Must raise RevisionNotPresent if any of the specified versions
 
413
        are not present in the other file's history unless ignore_missing
 
414
        is supplied in which case they are silently skipped.
640
415
        """
641
 
        raise NotImplementedError(self.annotate)
 
416
        self._check_write_ok()
 
417
        return InterVersionedFile.get(other, self).join(
 
418
            pb,
 
419
            msg,
 
420
            version_ids,
 
421
            ignore_missing)
642
422
 
643
423
    def iter_lines_added_or_present_in_versions(self, version_ids=None,
644
424
                                                pb=None):
662
442
        """
663
443
        raise NotImplementedError(self.iter_lines_added_or_present_in_versions)
664
444
 
 
445
    def iter_parents(self, version_ids):
 
446
        """Iterate through the parents for many version ids.
 
447
 
 
448
        :param version_ids: An iterable yielding version_ids.
 
449
        :return: An iterator that yields (version_id, parents). Requested 
 
450
            version_ids not present in the versioned file are simply skipped.
 
451
            The order is undefined, allowing for different optimisations in
 
452
            the underlying implementation.
 
453
        """
 
454
        for version_id in version_ids:
 
455
            try:
 
456
                yield version_id, tuple(self.get_parents(version_id))
 
457
            except errors.RevisionNotPresent:
 
458
                pass
 
459
 
 
460
    def transaction_finished(self):
 
461
        """The transaction that this file was opened in has finished.
 
462
 
 
463
        This records self.finished = True and should cause all mutating
 
464
        operations to error.
 
465
        """
 
466
        self.finished = True
 
467
 
665
468
    def plan_merge(self, ver_a, ver_b):
666
469
        """Return pseudo-annotation indicating how the two versions merge.
667
470
 
678
481
        unchanged   Alive in both a and b (possibly created in both)
679
482
        new-a       Created in a
680
483
        new-b       Created in b
681
 
        ghost-a     Killed in a, unborn in b
 
484
        ghost-a     Killed in a, unborn in b    
682
485
        ghost-b     Killed in b, unborn in a
683
486
        irrelevant  Not in either revision
684
487
        """
685
488
        raise NotImplementedError(VersionedFile.plan_merge)
686
 
 
 
489
        
687
490
    def weave_merge(self, plan, a_marker=TextMerge.A_MARKER,
688
491
                    b_marker=TextMerge.B_MARKER):
689
492
        return PlanWeaveMerge(plan, a_marker, b_marker).merge_lines()[0]
690
493
 
691
494
 
692
 
class RecordingVersionedFilesDecorator(object):
693
 
    """A minimal versioned files that records calls made on it.
694
 
 
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):
702
 
        """Create a RecordingVersionedFilesDecorator decorating backing_vf.
703
 
 
704
 
        :param backing_vf: The versioned file to answer all methods.
705
 
        """
706
 
        self._backing_vf = backing_vf
707
 
        self.calls = []
708
 
 
709
 
    def add_lines(self, key, parents, lines, parent_texts=None,
710
 
        left_matching_blocks=None, nostore_sha=None, random_id=False,
711
 
        check_content=True):
712
 
        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
 
 
717
 
    def check(self):
718
 
        self._backing_vf.check()
719
 
 
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
 
 
724
 
    def get_record_stream(self, keys, sort_order, include_delta_closure):
725
 
        self.calls.append(("get_record_stream", list(keys), sort_order,
726
 
            include_delta_closure))
727
 
        return self._backing_vf.get_record_stream(keys, sort_order,
728
 
            include_delta_closure)
729
 
 
730
 
    def get_sha1s(self, keys):
731
 
        self.calls.append(("get_sha1s", copy(keys)))
732
 
        return self._backing_vf.get_sha1s(keys)
733
 
 
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)))
736
 
        return self._backing_vf.iter_lines_added_or_present_in_keys(keys, pb=pb)
737
 
 
738
 
    def keys(self):
739
 
        self.calls.append(("keys",))
740
 
        return self._backing_vf.keys()
741
 
 
742
 
 
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
 
 
781
 
class KeyMapper(object):
782
 
    """KeyMappers map between keys and underlying partitioned storage."""
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.
795
 
 
796
 
        :param partition_id: The underlying partition id.
797
 
        :return: As much of a key (or prefix) as is derivable from the partition
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.
833
 
 
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.
872
 
 
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.
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.
927
 
 
928
 
    :ivar _immediate_fallback_vfs: For subclasses that support stacking,
929
 
        this is a list of other VersionedFiles immediately underneath this
930
 
        one.  They may in turn each have further fallbacks.
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
 
 
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.
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.)
949
 
        :param parent_texts: An optional dictionary containing the opaque
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
 
 
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 VersionedFileCommitBuilder.
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.
997
 
        return self.add_lines(key, parents, osutils.split_lines(text),
998
 
                              nostore_sha=nostore_sha,
999
 
                              random_id=random_id,
1000
 
                              check_content=True)
1001
 
 
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.
1020
 
        chunks_to_lines = osutils.chunks_to_lines
1021
 
        for record in self.get_record_stream(needed_parents, 'unordered',
1022
 
            True):
1023
 
            if record.storage_kind == 'absent':
1024
 
                continue
1025
 
            mpvf.add_version(chunks_to_lines(record.get_bytes_as('chunked')),
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):
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
 
        """
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
 
 
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
 
 
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
 
 
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
 
 
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
1125
 
        :return: a dict from key to sha1 digest. Keys of texts which are not
1126
 
            present in the store are not present in the returned
1127
 
            dictionary.
1128
 
        """
1129
 
        raise NotImplementedError(self.get_sha1s)
1130
 
 
1131
 
    has_key = index._has_key_from_parent_map
1132
 
 
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
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.
1142
 
        """
1143
 
        raise NotImplementedError(self.get_missing_compression_parent_keys)
1144
 
 
1145
 
    def insert_record_stream(self, stream):
1146
 
        """Insert a record stream into this container.
1147
 
 
1148
 
        :param stream: A stream of records to insert.
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."""
1183
 
        generator = _MPDiffGenerator(self, keys)
1184
 
        return generator.compute_diffs()
1185
 
 
1186
 
    def get_annotator(self):
1187
 
        return annotate.Annotator(self)
1188
 
 
1189
 
    missing_keys = index._missing_keys_from_parent_map
1190
 
 
1191
 
    def _extract_blocks(self, version_id, source, target):
1192
 
        return None
1193
 
 
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 = []
1202
 
        for a_vfs in self._immediate_fallback_vfs:
1203
 
            all_fallbacks.append(a_vfs)
1204
 
            all_fallbacks.extend(a_vfs._transitive_fallbacks())
1205
 
        return all_fallbacks
1206
 
 
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
 
 
1276
 
    def check(self, progress_bar=None, keys=None):
1277
 
        """See VersionedFiles.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.
1281
 
        for prefix, vf in self._iter_all_components():
1282
 
            vf.check()
1283
 
        if keys is not None:
1284
 
            return self.get_record_stream(keys, 'unordered', True)
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)
1361
 
            for suffix, sha1 in vf_sha1s.iteritems():
1362
 
                sha1s[prefix + (suffix,)] = sha1
1363
 
        return sha1s
1364
 
 
1365
 
    def insert_record_stream(self, stream):
1366
 
        """Insert a record stream into this container.
1367
 
 
1368
 
        :param stream: A stream of records to insert.
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 VersionedFilesWithFallbacks(VersionedFiles):
1426
 
 
1427
 
    def without_fallbacks(self):
1428
 
        """Return a clone of this object without any fallbacks configured."""
1429
 
        raise NotImplementedError(self.without_fallbacks)
1430
 
 
1431
 
    def add_fallback_versioned_files(self, a_versioned_files):
1432
 
        """Add a source of texts for texts not present in this knit.
1433
 
 
1434
 
        :param a_versioned_files: A VersionedFiles object.
1435
 
        """
1436
 
        raise NotImplementedError(self.add_fallback_versioned_files)
1437
 
 
1438
 
    def get_known_graph_ancestry(self, keys):
1439
 
        """Get a KnownGraph instance with the ancestry of keys."""
1440
 
        parent_map, missing_keys = self._index.find_ancestry(keys)
1441
 
        for fallback in self._transitive_fallbacks():
1442
 
            if not missing_keys:
1443
 
                break
1444
 
            (f_parent_map, f_missing_keys) = fallback._index.find_ancestry(
1445
 
                                                missing_keys)
1446
 
            parent_map.update(f_parent_map)
1447
 
            missing_keys = f_missing_keys
1448
 
        kg = _mod_graph.KnownGraph(parent_map)
1449
 
        return kg
1450
 
 
1451
 
 
1452
 
class _PlanMergeVersionedFile(VersionedFiles):
 
495
class _PlanMergeVersionedFile(object):
1453
496
    """A VersionedFile for uncommitted and committed texts.
1454
497
 
1455
498
    It is intended to allow merges to be planned with working tree texts.
1456
 
    It implements only the small part of the VersionedFiles interface used by
 
499
    It implements only the small part of the VersionedFile interface used by
1457
500
    PlanMerge.  It falls back to multiple versionedfiles for data not stored in
1458
501
    _PlanMergeVersionedFile itself.
1459
 
 
1460
 
    :ivar: fallback_versionedfiles a list of VersionedFiles objects that can be
1461
 
        queried for missing texts.
1462
502
    """
1463
503
 
1464
 
    def __init__(self, file_id):
1465
 
        """Create a _PlanMergeVersionedFile.
 
504
    def __init__(self, file_id, fallback_versionedfiles=None):
 
505
        """Constuctor
1466
506
 
1467
 
        :param file_id: Used with _PlanMerge code which is not yet fully
1468
 
            tuple-keyspace aware.
 
507
        :param file_id: Used when raising exceptions.
 
508
        :param fallback_versionedfiles: If supplied, the set of fallbacks to
 
509
            use.  Otherwise, _PlanMergeVersionedFile.fallback_versionedfiles
 
510
            can be appended to later.
1469
511
        """
1470
512
        self._file_id = file_id
1471
 
        # fallback locations
1472
 
        self.fallback_versionedfiles = []
1473
 
        # Parents for locally held keys.
 
513
        if fallback_versionedfiles is None:
 
514
            self.fallback_versionedfiles = []
 
515
        else:
 
516
            self.fallback_versionedfiles = fallback_versionedfiles
1474
517
        self._parents = {}
1475
 
        # line data for locally held keys.
1476
518
        self._lines = {}
1477
 
        # key lookup providers
1478
 
        self._providers = [_mod_graph.DictParentsProvider(self._parents)]
1479
519
 
1480
520
    def plan_merge(self, ver_a, ver_b, base=None):
1481
521
        """See VersionedFile.plan_merge"""
1482
522
        from bzrlib.merge import _PlanMerge
1483
523
        if base is None:
1484
 
            return _PlanMerge(ver_a, ver_b, self, (self._file_id,)).plan_merge()
1485
 
        old_plan = list(_PlanMerge(ver_a, base, self, (self._file_id,)).plan_merge())
1486
 
        new_plan = list(_PlanMerge(ver_a, ver_b, self, (self._file_id,)).plan_merge())
 
524
            return _PlanMerge(ver_a, ver_b, self).plan_merge()
 
525
        old_plan = list(_PlanMerge(ver_a, base, self).plan_merge())
 
526
        new_plan = list(_PlanMerge(ver_a, ver_b, self).plan_merge())
1487
527
        return _PlanMerge._subtract_plans(old_plan, new_plan)
1488
528
 
1489
529
    def plan_lca_merge(self, ver_a, ver_b, base=None):
1490
530
        from bzrlib.merge import _PlanLCAMerge
1491
 
        graph = _mod_graph.Graph(self)
1492
 
        new_plan = _PlanLCAMerge(ver_a, ver_b, self, (self._file_id,), graph).plan_merge()
 
531
        graph = self._get_graph()
 
532
        new_plan = _PlanLCAMerge(ver_a, ver_b, self, graph).plan_merge()
1493
533
        if base is None:
1494
534
            return new_plan
1495
 
        old_plan = _PlanLCAMerge(ver_a, base, self, (self._file_id,), graph).plan_merge()
 
535
        old_plan = _PlanLCAMerge(ver_a, base, self, graph).plan_merge()
1496
536
        return _PlanLCAMerge._subtract_plans(list(old_plan), list(new_plan))
1497
537
 
1498
 
    def add_lines(self, key, parents, lines):
1499
 
        """See VersionedFiles.add_lines
 
538
    def add_lines(self, version_id, parents, lines):
 
539
        """See VersionedFile.add_lines
1500
540
 
1501
 
        Lines are added locally, not to fallback versionedfiles.  Also, ghosts
1502
 
        are permitted.  Only reserved ids are permitted.
 
541
        Lines are added locally, not fallback versionedfiles.  Also, ghosts are
 
542
        permitted.  Only reserved ids are permitted.
1503
543
        """
1504
 
        if type(key) is not tuple:
1505
 
            raise TypeError(key)
1506
 
        if not revision.is_reserved_id(key[-1]):
 
544
        if not revision.is_reserved_id(version_id):
1507
545
            raise ValueError('Only reserved ids may be used')
1508
546
        if parents is None:
1509
547
            raise ValueError('Parents may not be None')
1510
548
        if lines is None:
1511
549
            raise ValueError('Lines may not be None')
1512
 
        self._parents[key] = tuple(parents)
1513
 
        self._lines[key] = lines
 
550
        self._parents[version_id] = parents
 
551
        self._lines[version_id] = lines
1514
552
 
1515
 
    def get_record_stream(self, keys, ordering, include_delta_closure):
1516
 
        pending = set(keys)
1517
 
        for key in keys:
1518
 
            if key in self._lines:
1519
 
                lines = self._lines[key]
1520
 
                parents = self._parents[key]
1521
 
                pending.remove(key)
1522
 
                yield ChunkedContentFactory(key, parents, None, lines)
 
553
    def get_lines(self, version_id):
 
554
        """See VersionedFile.get_ancestry"""
 
555
        lines = self._lines.get(version_id)
 
556
        if lines is not None:
 
557
            return lines
1523
558
        for versionedfile in self.fallback_versionedfiles:
1524
 
            for record in versionedfile.get_record_stream(
1525
 
                pending, 'unordered', True):
1526
 
                if record.storage_kind == 'absent':
 
559
            try:
 
560
                return versionedfile.get_lines(version_id)
 
561
            except errors.RevisionNotPresent:
 
562
                continue
 
563
        else:
 
564
            raise errors.RevisionNotPresent(version_id, self._file_id)
 
565
 
 
566
    def get_ancestry(self, version_id, topo_sorted=False):
 
567
        """See VersionedFile.get_ancestry.
 
568
 
 
569
        Note that this implementation assumes that if a VersionedFile can
 
570
        answer get_ancestry at all, it can give an authoritative answer.  In
 
571
        fact, ghosts can invalidate this assumption.  But it's good enough
 
572
        99% of the time, and far cheaper/simpler.
 
573
 
 
574
        Also note that the results of this version are never topologically
 
575
        sorted, and are a set.
 
576
        """
 
577
        if topo_sorted:
 
578
            raise ValueError('This implementation does not provide sorting')
 
579
        parents = self._parents.get(version_id)
 
580
        if parents is None:
 
581
            for vf in self.fallback_versionedfiles:
 
582
                try:
 
583
                    return vf.get_ancestry(version_id, topo_sorted=False)
 
584
                except errors.RevisionNotPresent:
1527
585
                    continue
1528
 
                else:
1529
 
                    pending.remove(record.key)
1530
 
                    yield record
1531
 
            if not pending:
1532
 
                return
1533
 
        # report absent entries
1534
 
        for key in pending:
1535
 
            yield AbsentContentFactory(key)
1536
 
 
1537
 
    def get_parent_map(self, keys):
1538
 
        """See VersionedFiles.get_parent_map"""
1539
 
        # We create a new provider because a fallback may have been added.
1540
 
        # If we make fallbacks private we can update a stack list and avoid
1541
 
        # object creation thrashing.
1542
 
        keys = set(keys)
1543
 
        result = {}
1544
 
        if revision.NULL_REVISION in keys:
1545
 
            keys.remove(revision.NULL_REVISION)
1546
 
            result[revision.NULL_REVISION] = ()
1547
 
        self._providers = self._providers[:1] + self.fallback_versionedfiles
1548
 
        result.update(
1549
 
            _mod_graph.StackedParentsProvider(
1550
 
                self._providers).get_parent_map(keys))
1551
 
        for key, parents in result.iteritems():
1552
 
            if parents == ():
1553
 
                result[key] = (revision.NULL_REVISION,)
1554
 
        return result
 
586
            else:
 
587
                raise errors.RevisionNotPresent(version_id, self._file_id)
 
588
        ancestry = set([version_id])
 
589
        for parent in parents:
 
590
            ancestry.update(self.get_ancestry(parent, topo_sorted=False))
 
591
        return ancestry
 
592
 
 
593
    def get_parents(self, version_id):
 
594
        """See VersionedFile.get_parents"""
 
595
        parents = self._parents.get(version_id)
 
596
        if parents is not None:
 
597
            return parents
 
598
        for versionedfile in self.fallback_versionedfiles:
 
599
            try:
 
600
                return versionedfile.get_parents(version_id)
 
601
            except errors.RevisionNotPresent:
 
602
                continue
 
603
        else:
 
604
            raise errors.RevisionNotPresent(version_id, self._file_id)
 
605
 
 
606
    def _get_graph(self):
 
607
        from bzrlib.graph import (
 
608
            DictParentsProvider,
 
609
            Graph,
 
610
            _StackedParentsProvider,
 
611
            )
 
612
        from bzrlib.repofmt.knitrepo import _KnitParentsProvider
 
613
        parent_providers = [DictParentsProvider(self._parents)]
 
614
        for vf in self.fallback_versionedfiles:
 
615
            parent_providers.append(_KnitParentsProvider(vf))
 
616
        return Graph(_StackedParentsProvider(parent_providers))
1555
617
 
1556
618
 
1557
619
class PlanWeaveMerge(TextMerge):
1558
620
    """Weave merge that takes a plan as its input.
1559
 
 
 
621
    
1560
622
    This exists so that VersionedFile.plan_merge is implementable.
1561
623
    Most callers will want to use WeaveMerge instead.
1562
624
    """
1564
626
    def __init__(self, plan, a_marker=TextMerge.A_MARKER,
1565
627
                 b_marker=TextMerge.B_MARKER):
1566
628
        TextMerge.__init__(self, a_marker, b_marker)
1567
 
        self.plan = list(plan)
 
629
        self.plan = plan
1568
630
 
1569
631
    def _merge_struct(self):
1570
632
        lines_a = []
1583
645
                yield(lines_a,)
1584
646
            else:
1585
647
                yield (lines_a, lines_b)
1586
 
 
 
648
       
1587
649
        # We previously considered either 'unchanged' or 'killed-both' lines
1588
650
        # to be possible places to resynchronize.  However, assuming agreement
1589
651
        # on killed-both lines may be too aggressive. -- mbp 20060324
1595
657
                lines_a = []
1596
658
                lines_b = []
1597
659
                ch_a = ch_b = False
1598
 
 
 
660
                
1599
661
            if state == 'unchanged':
1600
662
                if line:
1601
663
                    yield ([line],)
1617
679
            elif state == 'conflicted-b':
1618
680
                ch_b = ch_a = True
1619
681
                lines_b.append(line)
1620
 
            elif state == 'killed-both':
1621
 
                # This counts as a change, even though there is no associated
1622
 
                # line
1623
 
                ch_b = ch_a = True
1624
682
            else:
1625
 
                if state not in ('irrelevant', 'ghost-a', 'ghost-b',
1626
 
                        'killed-base'):
1627
 
                    raise AssertionError(state)
 
683
                assert state in ('irrelevant', 'ghost-a', 'ghost-b', 
 
684
                                 'killed-base', 'killed-both'), state
1628
685
        for struct in outstanding_struct():
1629
686
            yield struct
1630
687
 
1631
 
    def base_from_plan(self):
1632
 
        """Construct a BASE file from the plan text."""
1633
 
        base_lines = []
1634
 
        for state, line in self.plan:
1635
 
            if state in ('killed-a', 'killed-b', 'killed-both', 'unchanged'):
1636
 
                # If unchanged, then this line is straight from base. If a or b
1637
 
                # or both killed the line, then it *used* to be in base.
1638
 
                base_lines.append(line)
1639
 
            else:
1640
 
                if state not in ('killed-base', 'irrelevant',
1641
 
                                 'ghost-a', 'ghost-b',
1642
 
                                 'new-a', 'new-b',
1643
 
                                 'conflicted-a', 'conflicted-b'):
1644
 
                    # killed-base, irrelevant means it doesn't apply
1645
 
                    # ghost-a/ghost-b are harder to say for sure, but they
1646
 
                    # aren't in the 'inc_c' which means they aren't in the
1647
 
                    # shared base of a & b. So we don't include them.  And
1648
 
                    # obviously if the line is newly inserted, it isn't in base
1649
 
 
1650
 
                    # If 'conflicted-a' or b, then it is new vs one base, but
1651
 
                    # old versus another base. However, if we make it present
1652
 
                    # in the base, it will be deleted from the target, and it
1653
 
                    # seems better to get a line doubled in the merge result,
1654
 
                    # rather than have it deleted entirely.
1655
 
                    # Example, each node is the 'text' at that point:
1656
 
                    #           MN
1657
 
                    #          /   \
1658
 
                    #        MaN   MbN
1659
 
                    #         |  X  |
1660
 
                    #        MabN MbaN
1661
 
                    #          \   /
1662
 
                    #           ???
1663
 
                    # There was a criss-cross conflict merge. Both sides
1664
 
                    # include the other, but put themselves first.
1665
 
                    # Weave marks this as a 'clean' merge, picking OTHER over
1666
 
                    # THIS. (Though the details depend on order inserted into
1667
 
                    # weave, etc.)
1668
 
                    # LCA generates a plan:
1669
 
                    # [('unchanged', M),
1670
 
                    #  ('conflicted-b', b),
1671
 
                    #  ('unchanged', a),
1672
 
                    #  ('conflicted-a', b),
1673
 
                    #  ('unchanged', N)]
1674
 
                    # If you mark 'conflicted-*' as part of BASE, then a 3-way
1675
 
                    # merge tool will cleanly generate "MaN" (as BASE vs THIS
1676
 
                    # removes one 'b', and BASE vs OTHER removes the other)
1677
 
                    # If you include neither, 3-way creates a clean "MbabN" as
1678
 
                    # THIS adds one 'b', and OTHER does too.
1679
 
                    # It seems that having the line 2 times is better than
1680
 
                    # having it omitted. (Easier to manually delete than notice
1681
 
                    # it needs to be added.)
1682
 
                    raise AssertionError('Unknown state: %s' % (state,))
1683
 
        return base_lines
1684
 
 
1685
688
 
1686
689
class WeaveMerge(PlanWeaveMerge):
1687
690
    """Weave merge that takes a VersionedFile and two versions as its input."""
1688
691
 
1689
 
    def __init__(self, versionedfile, ver_a, ver_b,
 
692
    def __init__(self, versionedfile, ver_a, ver_b, 
1690
693
        a_marker=PlanWeaveMerge.A_MARKER, b_marker=PlanWeaveMerge.B_MARKER):
1691
694
        plan = versionedfile.plan_merge(ver_a, ver_b)
1692
695
        PlanWeaveMerge.__init__(self, plan, a_marker, b_marker)
1693
696
 
1694
697
 
1695
 
class VirtualVersionedFiles(VersionedFiles):
1696
 
    """Dummy implementation for VersionedFiles that uses other functions for
1697
 
    obtaining fulltexts and parent maps.
1698
 
 
1699
 
    This is always on the bottom of the stack and uses string keys
1700
 
    (rather than tuples) internally.
1701
 
    """
1702
 
 
1703
 
    def __init__(self, get_parent_map, get_lines):
1704
 
        """Create a VirtualVersionedFiles.
1705
 
 
1706
 
        :param get_parent_map: Same signature as Repository.get_parent_map.
1707
 
        :param get_lines: Should return lines for specified key or None if
1708
 
                          not available.
1709
 
        """
1710
 
        super(VirtualVersionedFiles, self).__init__()
1711
 
        self._get_parent_map = get_parent_map
1712
 
        self._get_lines = get_lines
1713
 
 
1714
 
    def check(self, progressbar=None):
1715
 
        """See VersionedFiles.check.
1716
 
 
1717
 
        :note: Always returns True for VirtualVersionedFiles.
1718
 
        """
1719
 
        return True
1720
 
 
1721
 
    def add_mpdiffs(self, records):
1722
 
        """See VersionedFiles.mpdiffs.
1723
 
 
1724
 
        :note: Not implemented for VirtualVersionedFiles.
1725
 
        """
1726
 
        raise NotImplementedError(self.add_mpdiffs)
1727
 
 
1728
 
    def get_parent_map(self, keys):
1729
 
        """See VersionedFiles.get_parent_map."""
1730
 
        return dict([((k,), tuple([(p,) for p in v]))
1731
 
            for k,v in self._get_parent_map([k for (k,) in keys]).iteritems()])
1732
 
 
1733
 
    def get_sha1s(self, keys):
1734
 
        """See VersionedFiles.get_sha1s."""
1735
 
        ret = {}
1736
 
        for (k,) in keys:
1737
 
            lines = self._get_lines(k)
1738
 
            if lines is not None:
1739
 
                if not isinstance(lines, list):
1740
 
                    raise AssertionError
1741
 
                ret[(k,)] = osutils.sha_strings(lines)
1742
 
        return ret
1743
 
 
1744
 
    def get_record_stream(self, keys, ordering, include_delta_closure):
1745
 
        """See VersionedFiles.get_record_stream."""
1746
 
        for (k,) in list(keys):
1747
 
            lines = self._get_lines(k)
1748
 
            if lines is not None:
1749
 
                if not isinstance(lines, list):
1750
 
                    raise AssertionError
1751
 
                yield ChunkedContentFactory((k,), None,
1752
 
                        sha1=osutils.sha_strings(lines),
1753
 
                        chunks=lines)
1754
 
            else:
1755
 
                yield AbsentContentFactory((k,))
1756
 
 
1757
 
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
1758
 
        """See VersionedFile.iter_lines_added_or_present_in_versions()."""
1759
 
        for i, (key,) in enumerate(keys):
1760
 
            if pb is not None:
1761
 
                pb.update("Finding changed lines", i, len(keys))
1762
 
            for l in self._get_lines(key):
1763
 
                yield (l, key)
1764
 
 
1765
 
 
1766
 
class NoDupeAddLinesDecorator(object):
1767
 
    """Decorator for a VersionedFiles that skips doing an add_lines if the key
1768
 
    is already present.
1769
 
    """
1770
 
 
1771
 
    def __init__(self, store):
1772
 
        self._store = store
1773
 
 
1774
 
    def add_lines(self, key, parents, lines, parent_texts=None,
1775
 
            left_matching_blocks=None, nostore_sha=None, random_id=False,
1776
 
            check_content=True):
1777
 
        """See VersionedFiles.add_lines.
1778
 
        
1779
 
        This implementation may return None as the third element of the return
1780
 
        value when the original store wouldn't.
1781
 
        """
1782
 
        if nostore_sha:
1783
 
            raise NotImplementedError(
1784
 
                "NoDupeAddLinesDecorator.add_lines does not implement the "
1785
 
                "nostore_sha behaviour.")
1786
 
        if key[-1] is None:
1787
 
            sha1 = osutils.sha_strings(lines)
1788
 
            key = ("sha1:" + sha1,)
1789
 
        else:
1790
 
            sha1 = None
1791
 
        if key in self._store.get_parent_map([key]):
1792
 
            # This key has already been inserted, so don't do it again.
1793
 
            if sha1 is None:
1794
 
                sha1 = osutils.sha_strings(lines)
1795
 
            return sha1, sum(map(len, lines)), None
1796
 
        return self._store.add_lines(key, parents, lines,
1797
 
                parent_texts=parent_texts,
1798
 
                left_matching_blocks=left_matching_blocks,
1799
 
                nostore_sha=nostore_sha, random_id=random_id,
1800
 
                check_content=check_content)
1801
 
 
1802
 
    def __getattr__(self, name):
1803
 
        return getattr(self._store, name)
1804
 
 
1805
 
 
1806
 
def network_bytes_to_kind_and_offset(network_bytes):
1807
 
    """Strip of a record kind from the front of network_bytes.
1808
 
 
1809
 
    :param network_bytes: The bytes of a record.
1810
 
    :return: A tuple (storage_kind, offset_of_remaining_bytes)
1811
 
    """
1812
 
    line_end = network_bytes.find('\n')
1813
 
    storage_kind = network_bytes[:line_end]
1814
 
    return storage_kind, line_end + 1
1815
 
 
1816
 
 
1817
 
class NetworkRecordStream(object):
1818
 
    """A record_stream which reconstitures a serialised stream."""
1819
 
 
1820
 
    def __init__(self, bytes_iterator):
1821
 
        """Create a NetworkRecordStream.
1822
 
 
1823
 
        :param bytes_iterator: An iterator of bytes. Each item in this
1824
 
            iterator should have been obtained from a record_streams'
1825
 
            record.get_bytes_as(record.storage_kind) call.
1826
 
        """
1827
 
        self._bytes_iterator = bytes_iterator
1828
 
        self._kind_factory = {
1829
 
            'fulltext': fulltext_network_to_record,
1830
 
            'groupcompress-block': groupcompress.network_block_to_records,
1831
 
            'knit-ft-gz': knit.knit_network_to_record,
1832
 
            'knit-delta-gz': knit.knit_network_to_record,
1833
 
            'knit-annotated-ft-gz': knit.knit_network_to_record,
1834
 
            'knit-annotated-delta-gz': knit.knit_network_to_record,
1835
 
            'knit-delta-closure': knit.knit_delta_closure_to_records,
1836
 
            }
1837
 
 
1838
 
    def read(self):
1839
 
        """Read the stream.
1840
 
 
1841
 
        :return: An iterator as per VersionedFiles.get_record_stream().
1842
 
        """
1843
 
        for bytes in self._bytes_iterator:
1844
 
            storage_kind, line_end = network_bytes_to_kind_and_offset(bytes)
1845
 
            for record in self._kind_factory[storage_kind](
1846
 
                storage_kind, bytes, line_end):
1847
 
                yield record
1848
 
 
1849
 
 
1850
 
def fulltext_network_to_record(kind, bytes, line_end):
1851
 
    """Convert a network fulltext record to record."""
1852
 
    meta_len, = struct.unpack('!L', bytes[line_end:line_end+4])
1853
 
    record_meta = bytes[line_end+4:line_end+4+meta_len]
1854
 
    key, parents = bencode.bdecode_as_tuple(record_meta)
1855
 
    if parents == 'nil':
1856
 
        parents = None
1857
 
    fulltext = bytes[line_end+4+meta_len:]
1858
 
    return [FulltextContentFactory(key, parents, None, fulltext)]
1859
 
 
1860
 
 
1861
 
def _length_prefix(bytes):
1862
 
    return struct.pack('!L', len(bytes))
1863
 
 
1864
 
 
1865
 
def record_to_fulltext_bytes(record):
1866
 
    if record.parents is None:
1867
 
        parents = 'nil'
1868
 
    else:
1869
 
        parents = record.parents
1870
 
    record_meta = bencode.bencode((record.key, parents))
1871
 
    record_content = record.get_bytes_as('fulltext')
1872
 
    return "fulltext\n%s%s%s" % (
1873
 
        _length_prefix(record_meta), record_meta, record_content)
1874
 
 
1875
 
 
1876
 
def sort_groupcompress(parent_map):
1877
 
    """Sort and group the keys in parent_map into groupcompress order.
1878
 
 
1879
 
    groupcompress is defined (currently) as reverse-topological order, grouped
1880
 
    by the key prefix.
1881
 
 
1882
 
    :return: A sorted-list of keys
1883
 
    """
1884
 
    # gc-optimal ordering is approximately reverse topological,
1885
 
    # properly grouped by file-id.
1886
 
    per_prefix_map = {}
1887
 
    for item in parent_map.iteritems():
1888
 
        key = item[0]
1889
 
        if isinstance(key, str) or len(key) == 1:
1890
 
            prefix = ''
1891
 
        else:
1892
 
            prefix = key[0]
1893
 
        try:
1894
 
            per_prefix_map[prefix].append(item)
1895
 
        except KeyError:
1896
 
            per_prefix_map[prefix] = [item]
1897
 
 
1898
 
    present_keys = []
1899
 
    for prefix in sorted(per_prefix_map):
1900
 
        present_keys.extend(reversed(tsort.topo_sort(per_prefix_map[prefix])))
1901
 
    return present_keys
1902
 
 
1903
 
 
1904
 
class _KeyRefs(object):
1905
 
 
1906
 
    def __init__(self, track_new_keys=False):
1907
 
        # dict mapping 'key' to 'set of keys referring to that key'
1908
 
        self.refs = {}
1909
 
        if track_new_keys:
1910
 
            # set remembering all new keys
1911
 
            self.new_keys = set()
1912
 
        else:
1913
 
            self.new_keys = None
1914
 
 
1915
 
    def clear(self):
1916
 
        if self.refs:
1917
 
            self.refs.clear()
1918
 
        if self.new_keys:
1919
 
            self.new_keys.clear()
1920
 
 
1921
 
    def add_references(self, key, refs):
1922
 
        # Record the new references
1923
 
        for referenced in refs:
1924
 
            try:
1925
 
                needed_by = self.refs[referenced]
1926
 
            except KeyError:
1927
 
                needed_by = self.refs[referenced] = set()
1928
 
            needed_by.add(key)
1929
 
        # Discard references satisfied by the new key
1930
 
        self.add_key(key)
1931
 
 
1932
 
    def get_new_keys(self):
1933
 
        return self.new_keys
1934
 
    
1935
 
    def get_unsatisfied_refs(self):
1936
 
        return self.refs.iterkeys()
1937
 
 
1938
 
    def _satisfy_refs_for_key(self, key):
1939
 
        try:
1940
 
            del self.refs[key]
1941
 
        except KeyError:
1942
 
            # No keys depended on this key.  That's ok.
1943
 
            pass
1944
 
 
1945
 
    def add_key(self, key):
1946
 
        # satisfy refs for key, and remember that we've seen this key.
1947
 
        self._satisfy_refs_for_key(key)
1948
 
        if self.new_keys is not None:
1949
 
            self.new_keys.add(key)
1950
 
 
1951
 
    def satisfy_refs_for_keys(self, keys):
1952
 
        for key in keys:
1953
 
            self._satisfy_refs_for_key(key)
1954
 
 
1955
 
    def get_referrers(self):
1956
 
        result = set()
1957
 
        for referrers in self.refs.itervalues():
1958
 
            result.update(referrers)
1959
 
        return result
1960
 
 
1961
 
 
1962
 
 
 
698
class InterVersionedFile(InterObject):
 
699
    """This class represents operations taking place between two VersionedFiles.
 
700
 
 
701
    Its instances have methods like join, and contain
 
702
    references to the source and target versionedfiles these operations can be 
 
703
    carried out on.
 
704
 
 
705
    Often we will provide convenience methods on 'versionedfile' which carry out
 
706
    operations with another versionedfile - they will always forward to
 
707
    InterVersionedFile.get(other).method_name(parameters).
 
708
    """
 
709
 
 
710
    _optimisers = []
 
711
    """The available optimised InterVersionedFile types."""
 
712
 
 
713
    def join(self, pb=None, msg=None, version_ids=None, ignore_missing=False):
 
714
        """Integrate versions from self.source into self.target.
 
715
 
 
716
        If version_ids is None all versions from source should be
 
717
        incorporated into this versioned file.
 
718
 
 
719
        Must raise RevisionNotPresent if any of the specified versions
 
720
        are not present in the other file's history unless ignore_missing is 
 
721
        supplied in which case they are silently skipped.
 
722
        """
 
723
        # the default join: 
 
724
        # - if the target is empty, just add all the versions from 
 
725
        #   source to target, otherwise:
 
726
        # - make a temporary versioned file of type target
 
727
        # - insert the source content into it one at a time
 
728
        # - join them
 
729
        if not self.target.versions():
 
730
            target = self.target
 
731
        else:
 
732
            # Make a new target-format versioned file. 
 
733
            temp_source = self.target.create_empty("temp", MemoryTransport())
 
734
            target = temp_source
 
735
        version_ids = self._get_source_version_ids(version_ids, ignore_missing)
 
736
        graph = self.source.get_graph(version_ids)
 
737
        order = tsort.topo_sort(graph.items())
 
738
        pb = ui.ui_factory.nested_progress_bar()
 
739
        parent_texts = {}
 
740
        try:
 
741
            # TODO for incremental cross-format work:
 
742
            # make a versioned file with the following content:
 
743
            # all revisions we have been asked to join
 
744
            # all their ancestors that are *not* in target already.
 
745
            # the immediate parents of the above two sets, with 
 
746
            # empty parent lists - these versions are in target already
 
747
            # and the incorrect version data will be ignored.
 
748
            # TODO: for all ancestors that are present in target already,
 
749
            # check them for consistent data, this requires moving sha1 from
 
750
            # 
 
751
            # TODO: remove parent texts when they are not relevant any more for 
 
752
            # memory pressure reduction. RBC 20060313
 
753
            # pb.update('Converting versioned data', 0, len(order))
 
754
            total = len(order)
 
755
            for index, version in enumerate(order):
 
756
                pb.update('Converting versioned data', index, total)
 
757
                _, _, parent_text = target.add_lines(version,
 
758
                                               self.source.get_parents(version),
 
759
                                               self.source.get_lines(version),
 
760
                                               parent_texts=parent_texts)
 
761
                parent_texts[version] = parent_text
 
762
            
 
763
            # this should hit the native code path for target
 
764
            if target is not self.target:
 
765
                return self.target.join(temp_source,
 
766
                                        pb,
 
767
                                        msg,
 
768
                                        version_ids,
 
769
                                        ignore_missing)
 
770
            else:
 
771
                return total
 
772
        finally:
 
773
            pb.finished()
 
774
 
 
775
    def _get_source_version_ids(self, version_ids, ignore_missing):
 
776
        """Determine the version ids to be used from self.source.
 
777
 
 
778
        :param version_ids: The caller-supplied version ids to check. (None 
 
779
                            for all). If None is in version_ids, it is stripped.
 
780
        :param ignore_missing: if True, remove missing ids from the version 
 
781
                               list. If False, raise RevisionNotPresent on
 
782
                               a missing version id.
 
783
        :return: A set of version ids.
 
784
        """
 
785
        if version_ids is None:
 
786
            # None cannot be in source.versions
 
787
            return set(self.source.versions())
 
788
        else:
 
789
            if ignore_missing:
 
790
                return set(self.source.versions()).intersection(set(version_ids))
 
791
            else:
 
792
                new_version_ids = set()
 
793
                for version in version_ids:
 
794
                    if version is None:
 
795
                        continue
 
796
                    if not self.source.has_version(version):
 
797
                        raise errors.RevisionNotPresent(version, str(self.source))
 
798
                    else:
 
799
                        new_version_ids.add(version)
 
800
                return new_version_ids