~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repofmt/pack_repo.py

  • Committer: John Arbash Meinel
  • Date: 2005-09-15 21:35:53 UTC
  • mfrom: (907.1.57)
  • mto: (1393.2.1)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: john@arbash-meinel.com-20050915213552-a6c83a5ef1e20897
(broken) Transport work is merged in. Tests do not pass yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
import re
18
 
import sys
19
 
 
20
 
from bzrlib.lazy_import import lazy_import
21
 
lazy_import(globals(), """
22
 
from itertools import izip
23
 
import time
24
 
 
25
 
from bzrlib import (
26
 
    chk_map,
27
 
    debug,
28
 
    graph,
29
 
    osutils,
30
 
    pack,
31
 
    transactions,
32
 
    ui,
33
 
    xml5,
34
 
    xml6,
35
 
    xml7,
36
 
    )
37
 
from bzrlib.index import (
38
 
    CombinedGraphIndex,
39
 
    GraphIndex,
40
 
    GraphIndexBuilder,
41
 
    GraphIndexPrefixAdapter,
42
 
    InMemoryGraphIndex,
43
 
    )
44
 
from bzrlib.inventory import CHKInventory
45
 
from bzrlib.knit import (
46
 
    KnitPlainFactory,
47
 
    KnitVersionedFiles,
48
 
    _KnitGraphIndex,
49
 
    _DirectPackAccess,
50
 
    )
51
 
from bzrlib import tsort
52
 
""")
53
 
from bzrlib import (
54
 
    bzrdir,
55
 
    chk_serializer,
56
 
    errors,
57
 
    lockable_files,
58
 
    lockdir,
59
 
    revision as _mod_revision,
60
 
    symbol_versioning,
61
 
    )
62
 
 
63
 
from bzrlib.decorators import needs_write_lock
64
 
from bzrlib.btree_index import (
65
 
    BTreeGraphIndex,
66
 
    BTreeBuilder,
67
 
    )
68
 
from bzrlib.index import (
69
 
    GraphIndex,
70
 
    InMemoryGraphIndex,
71
 
    )
72
 
from bzrlib.repofmt.knitrepo import KnitRepository
73
 
from bzrlib.repository import (
74
 
    CommitBuilder,
75
 
    MetaDirRepositoryFormat,
76
 
    RepositoryFormat,
77
 
    RootCommitBuilder,
78
 
    )
79
 
import bzrlib.revision as _mod_revision
80
 
from bzrlib.trace import (
81
 
    mutter,
82
 
    warning,
83
 
    )
84
 
 
85
 
 
86
 
class PackCommitBuilder(CommitBuilder):
87
 
    """A subclass of CommitBuilder to add texts with pack semantics.
88
 
 
89
 
    Specifically this uses one knit object rather than one knit object per
90
 
    added text, reducing memory and object pressure.
91
 
    """
92
 
 
93
 
    def __init__(self, repository, parents, config, timestamp=None,
94
 
                 timezone=None, committer=None, revprops=None,
95
 
                 revision_id=None):
96
 
        CommitBuilder.__init__(self, repository, parents, config,
97
 
            timestamp=timestamp, timezone=timezone, committer=committer,
98
 
            revprops=revprops, revision_id=revision_id)
99
 
        self._file_graph = graph.Graph(
100
 
            repository._pack_collection.text_index.combined_index)
101
 
 
102
 
    def _heads(self, file_id, revision_ids):
103
 
        keys = [(file_id, revision_id) for revision_id in revision_ids]
104
 
        return set([key[1] for key in self._file_graph.heads(keys)])
105
 
 
106
 
 
107
 
class PackRootCommitBuilder(RootCommitBuilder):
108
 
    """A subclass of RootCommitBuilder to add texts with pack semantics.
109
 
 
110
 
    Specifically this uses one knit object rather than one knit object per
111
 
    added text, reducing memory and object pressure.
112
 
    """
113
 
 
114
 
    def __init__(self, repository, parents, config, timestamp=None,
115
 
                 timezone=None, committer=None, revprops=None,
116
 
                 revision_id=None):
117
 
        CommitBuilder.__init__(self, repository, parents, config,
118
 
            timestamp=timestamp, timezone=timezone, committer=committer,
119
 
            revprops=revprops, revision_id=revision_id)
120
 
        self._file_graph = graph.Graph(
121
 
            repository._pack_collection.text_index.combined_index)
122
 
 
123
 
    def _heads(self, file_id, revision_ids):
124
 
        keys = [(file_id, revision_id) for revision_id in revision_ids]
125
 
        return set([key[1] for key in self._file_graph.heads(keys)])
126
 
 
127
 
 
128
 
class Pack(object):
129
 
    """An in memory proxy for a pack and its indices.
130
 
 
131
 
    This is a base class that is not directly used, instead the classes
132
 
    ExistingPack and NewPack are used.
133
 
    """
134
 
 
135
 
    # A map of index 'type' to the file extension and position in the
136
 
    # index_sizes array.
137
 
    index_definitions = {
138
 
        'chk': ('.cix', 4),
139
 
        'revision': ('.rix', 0),
140
 
        'inventory': ('.iix', 1),
141
 
        'text': ('.tix', 2),
142
 
        'signature': ('.six', 3),
143
 
        }
144
 
 
145
 
    def __init__(self, revision_index, inventory_index, text_index,
146
 
        signature_index, chk_index=None):
147
 
        """Create a pack instance.
148
 
 
149
 
        :param revision_index: A GraphIndex for determining what revisions are
150
 
            present in the Pack and accessing the locations of their texts.
151
 
        :param inventory_index: A GraphIndex for determining what inventories are
152
 
            present in the Pack and accessing the locations of their
153
 
            texts/deltas.
154
 
        :param text_index: A GraphIndex for determining what file texts
155
 
            are present in the pack and accessing the locations of their
156
 
            texts/deltas (via (fileid, revisionid) tuples).
157
 
        :param signature_index: A GraphIndex for determining what signatures are
158
 
            present in the Pack and accessing the locations of their texts.
159
 
        :param chk_index: A GraphIndex for accessing content by CHK, if the
160
 
            pack has one.
161
 
        """
162
 
        self.revision_index = revision_index
163
 
        self.inventory_index = inventory_index
164
 
        self.text_index = text_index
165
 
        self.signature_index = signature_index
166
 
        self.chk_index = chk_index
167
 
 
168
 
    def access_tuple(self):
169
 
        """Return a tuple (transport, name) for the pack content."""
170
 
        return self.pack_transport, self.file_name()
171
 
 
172
 
    def _check_references(self):
173
 
        """Make sure our external references are present.
174
 
 
175
 
        Packs are allowed to have deltas whose base is not in the pack, but it
176
 
        must be present somewhere in this collection.  It is not allowed to
177
 
        have deltas based on a fallback repository.
178
 
        (See <https://bugs.launchpad.net/bzr/+bug/288751>)
179
 
        """
180
 
        missing_items = {}
181
 
        for (index_name, external_refs, index) in [
182
 
            ('texts',
183
 
                self._get_external_refs(self.text_index),
184
 
                self._pack_collection.text_index.combined_index),
185
 
            ('inventories',
186
 
                self._get_external_refs(self.inventory_index),
187
 
                self._pack_collection.inventory_index.combined_index),
188
 
            ]:
189
 
            missing = external_refs.difference(
190
 
                k for (idx, k, v, r) in
191
 
                index.iter_entries(external_refs))
192
 
            if missing:
193
 
                missing_items[index_name] = sorted(list(missing))
194
 
        if missing_items:
195
 
            from pprint import pformat
196
 
            raise errors.BzrCheckError(
197
 
                "Newly created pack file %r has delta references to "
198
 
                "items not in its repository:\n%s"
199
 
                % (self, pformat(missing_items)))
200
 
 
201
 
    def file_name(self):
202
 
        """Get the file name for the pack on disk."""
203
 
        return self.name + '.pack'
204
 
 
205
 
    def get_revision_count(self):
206
 
        return self.revision_index.key_count()
207
 
 
208
 
    def index_name(self, index_type, name):
209
 
        """Get the disk name of an index type for pack name 'name'."""
210
 
        return name + Pack.index_definitions[index_type][0]
211
 
 
212
 
    def index_offset(self, index_type):
213
 
        """Get the position in a index_size array for a given index type."""
214
 
        return Pack.index_definitions[index_type][1]
215
 
 
216
 
    def inventory_index_name(self, name):
217
 
        """The inv index is the name + .iix."""
218
 
        return self.index_name('inventory', name)
219
 
 
220
 
    def revision_index_name(self, name):
221
 
        """The revision index is the name + .rix."""
222
 
        return self.index_name('revision', name)
223
 
 
224
 
    def signature_index_name(self, name):
225
 
        """The signature index is the name + .six."""
226
 
        return self.index_name('signature', name)
227
 
 
228
 
    def text_index_name(self, name):
229
 
        """The text index is the name + .tix."""
230
 
        return self.index_name('text', name)
231
 
 
232
 
    def _replace_index_with_readonly(self, index_type):
233
 
        setattr(self, index_type + '_index',
234
 
            self.index_class(self.index_transport,
235
 
                self.index_name(index_type, self.name),
236
 
                self.index_sizes[self.index_offset(index_type)]))
237
 
 
238
 
 
239
 
class ExistingPack(Pack):
240
 
    """An in memory proxy for an existing .pack and its disk indices."""
241
 
 
242
 
    def __init__(self, pack_transport, name, revision_index, inventory_index,
243
 
        text_index, signature_index, chk_index=None):
244
 
        """Create an ExistingPack object.
245
 
 
246
 
        :param pack_transport: The transport where the pack file resides.
247
 
        :param name: The name of the pack on disk in the pack_transport.
248
 
        """
249
 
        Pack.__init__(self, revision_index, inventory_index, text_index,
250
 
            signature_index, chk_index)
251
 
        self.name = name
252
 
        self.pack_transport = pack_transport
253
 
        if None in (revision_index, inventory_index, text_index,
254
 
                signature_index, name, pack_transport):
255
 
            raise AssertionError()
256
 
 
257
 
    def __eq__(self, other):
258
 
        return self.__dict__ == other.__dict__
259
 
 
260
 
    def __ne__(self, other):
261
 
        return not self.__eq__(other)
262
 
 
263
 
    def __repr__(self):
264
 
        return "<%s.%s object at 0x%x, %s, %s" % (
265
 
            self.__class__.__module__, self.__class__.__name__, id(self),
266
 
            self.pack_transport, self.name)
267
 
 
268
 
 
269
 
class ResumedPack(ExistingPack):
270
 
 
271
 
    def __init__(self, name, revision_index, inventory_index, text_index,
272
 
        signature_index, upload_transport, pack_transport, index_transport,
273
 
        pack_collection):
274
 
        """Create a ResumedPack object."""
275
 
        ExistingPack.__init__(self, pack_transport, name, revision_index,
276
 
            inventory_index, text_index, signature_index)
277
 
        self.upload_transport = upload_transport
278
 
        self.index_transport = index_transport
279
 
        self.index_sizes = [None, None, None, None]
280
 
        indices = [
281
 
            ('revision', revision_index),
282
 
            ('inventory', inventory_index),
283
 
            ('text', text_index),
284
 
            ('signature', signature_index),
285
 
            ]
286
 
        for index_type, index in indices:
287
 
            offset = self.index_offset(index_type)
288
 
            self.index_sizes[offset] = index._size
289
 
        self.index_class = pack_collection._index_class
290
 
        self._pack_collection = pack_collection
291
 
        self._state = 'resumed'
292
 
        # XXX: perhaps check that the .pack file exists?
293
 
 
294
 
    def access_tuple(self):
295
 
        if self._state == 'finished':
296
 
            return Pack.access_tuple(self)
297
 
        elif self._state == 'resumed':
298
 
            return self.upload_transport, self.file_name()
299
 
        else:
300
 
            raise AssertionError(self._state)
301
 
 
302
 
    def abort(self):
303
 
        self.upload_transport.delete(self.file_name())
304
 
        indices = [self.revision_index, self.inventory_index, self.text_index,
305
 
            self.signature_index]
306
 
        for index in indices:
307
 
            index._transport.delete(index._name)
308
 
 
309
 
    def finish(self):
310
 
        self._check_references()
311
 
        new_name = '../packs/' + self.file_name()
312
 
        self.upload_transport.rename(self.file_name(), new_name)
313
 
        for index_type in ['revision', 'inventory', 'text', 'signature']:
314
 
            old_name = self.index_name(index_type, self.name)
315
 
            new_name = '../indices/' + old_name
316
 
            self.upload_transport.rename(old_name, new_name)
317
 
            self._replace_index_with_readonly(index_type)
318
 
        self._state = 'finished'
319
 
 
320
 
    def _get_external_refs(self, index):
321
 
        return index.external_references(1)
322
 
 
323
 
 
324
 
class NewPack(Pack):
325
 
    """An in memory proxy for a pack which is being created."""
326
 
 
327
 
    def __init__(self, pack_collection, upload_suffix='', file_mode=None):
328
 
        """Create a NewPack instance.
329
 
 
330
 
        :param pack_collection: A PackCollection into which this is being inserted.
331
 
        :param upload_suffix: An optional suffix to be given to any temporary
332
 
            files created during the pack creation. e.g '.autopack'
333
 
        :param file_mode: Unix permissions for newly created file.
334
 
        """
335
 
        # The relative locations of the packs are constrained, but all are
336
 
        # passed in because the caller has them, so as to avoid object churn.
337
 
        index_builder_class = pack_collection._index_builder_class
338
 
        if pack_collection.chk_index is not None:
339
 
            chk_index = index_builder_class(reference_lists=0)
340
 
        else:
341
 
            chk_index = None
342
 
        Pack.__init__(self,
343
 
            # Revisions: parents list, no text compression.
344
 
            index_builder_class(reference_lists=1),
345
 
            # Inventory: We want to map compression only, but currently the
346
 
            # knit code hasn't been updated enough to understand that, so we
347
 
            # have a regular 2-list index giving parents and compression
348
 
            # source.
349
 
            index_builder_class(reference_lists=2),
350
 
            # Texts: compression and per file graph, for all fileids - so two
351
 
            # reference lists and two elements in the key tuple.
352
 
            index_builder_class(reference_lists=2, key_elements=2),
353
 
            # Signatures: Just blobs to store, no compression, no parents
354
 
            # listing.
355
 
            index_builder_class(reference_lists=0),
356
 
            # CHK based storage - just blobs, no compression or parents.
357
 
            chk_index=chk_index
358
 
            )
359
 
        self._pack_collection = pack_collection
360
 
        # When we make readonly indices, we need this.
361
 
        self.index_class = pack_collection._index_class
362
 
        # where should the new pack be opened
363
 
        self.upload_transport = pack_collection._upload_transport
364
 
        # where are indices written out to
365
 
        self.index_transport = pack_collection._index_transport
366
 
        # where is the pack renamed to when it is finished?
367
 
        self.pack_transport = pack_collection._pack_transport
368
 
        # What file mode to upload the pack and indices with.
369
 
        self._file_mode = file_mode
370
 
        # tracks the content written to the .pack file.
371
 
        self._hash = osutils.md5()
372
 
        # a tuple with the length in bytes of the indices, once the pack
373
 
        # is finalised. (rev, inv, text, sigs, chk_if_in_use)
374
 
        self.index_sizes = None
375
 
        # How much data to cache when writing packs. Note that this is not
376
 
        # synchronised with reads, because it's not in the transport layer, so
377
 
        # is not safe unless the client knows it won't be reading from the pack
378
 
        # under creation.
379
 
        self._cache_limit = 0
380
 
        # the temporary pack file name.
381
 
        self.random_name = osutils.rand_chars(20) + upload_suffix
382
 
        # when was this pack started ?
383
 
        self.start_time = time.time()
384
 
        # open an output stream for the data added to the pack.
385
 
        self.write_stream = self.upload_transport.open_write_stream(
386
 
            self.random_name, mode=self._file_mode)
387
 
        if 'pack' in debug.debug_flags:
388
 
            mutter('%s: create_pack: pack stream open: %s%s t+%6.3fs',
389
 
                time.ctime(), self.upload_transport.base, self.random_name,
390
 
                time.time() - self.start_time)
391
 
        # A list of byte sequences to be written to the new pack, and the
392
 
        # aggregate size of them.  Stored as a list rather than separate
393
 
        # variables so that the _write_data closure below can update them.
394
 
        self._buffer = [[], 0]
395
 
        # create a callable for adding data
396
 
        #
397
 
        # robertc says- this is a closure rather than a method on the object
398
 
        # so that the variables are locals, and faster than accessing object
399
 
        # members.
400
 
        def _write_data(bytes, flush=False, _buffer=self._buffer,
401
 
            _write=self.write_stream.write, _update=self._hash.update):
402
 
            _buffer[0].append(bytes)
403
 
            _buffer[1] += len(bytes)
404
 
            # buffer cap
405
 
            if _buffer[1] > self._cache_limit or flush:
406
 
                bytes = ''.join(_buffer[0])
407
 
                _write(bytes)
408
 
                _update(bytes)
409
 
                _buffer[:] = [[], 0]
410
 
        # expose this on self, for the occasion when clients want to add data.
411
 
        self._write_data = _write_data
412
 
        # a pack writer object to serialise pack records.
413
 
        self._writer = pack.ContainerWriter(self._write_data)
414
 
        self._writer.begin()
415
 
        # what state is the pack in? (open, finished, aborted)
416
 
        self._state = 'open'
417
 
 
418
 
    def abort(self):
419
 
        """Cancel creating this pack."""
420
 
        self._state = 'aborted'
421
 
        self.write_stream.close()
422
 
        # Remove the temporary pack file.
423
 
        self.upload_transport.delete(self.random_name)
424
 
        # The indices have no state on disk.
425
 
 
426
 
    def access_tuple(self):
427
 
        """Return a tuple (transport, name) for the pack content."""
428
 
        if self._state == 'finished':
429
 
            return Pack.access_tuple(self)
430
 
        elif self._state == 'open':
431
 
            return self.upload_transport, self.random_name
432
 
        else:
433
 
            raise AssertionError(self._state)
434
 
 
435
 
    def data_inserted(self):
436
 
        """True if data has been added to this pack."""
437
 
        return bool(self.get_revision_count() or
438
 
            self.inventory_index.key_count() or
439
 
            self.text_index.key_count() or
440
 
            self.signature_index.key_count() or
441
 
            (self.chk_index is not None and self.chk_index.key_count()))
442
 
 
443
 
    def finish(self, suspend=False):
444
 
        """Finish the new pack.
445
 
 
446
 
        This:
447
 
         - finalises the content
448
 
         - assigns a name (the md5 of the content, currently)
449
 
         - writes out the associated indices
450
 
         - renames the pack into place.
451
 
         - stores the index size tuple for the pack in the index_sizes
452
 
           attribute.
453
 
        """
454
 
        self._writer.end()
455
 
        if self._buffer[1]:
456
 
            self._write_data('', flush=True)
457
 
        self.name = self._hash.hexdigest()
458
 
        if not suspend:
459
 
            self._check_references()
460
 
        # write indices
461
 
        # XXX: It'd be better to write them all to temporary names, then
462
 
        # rename them all into place, so that the window when only some are
463
 
        # visible is smaller.  On the other hand none will be seen until
464
 
        # they're in the names list.
465
 
        self.index_sizes = [None, None, None, None]
466
 
        self._write_index('revision', self.revision_index, 'revision', suspend)
467
 
        self._write_index('inventory', self.inventory_index, 'inventory',
468
 
            suspend)
469
 
        self._write_index('text', self.text_index, 'file texts', suspend)
470
 
        self._write_index('signature', self.signature_index,
471
 
            'revision signatures', suspend)
472
 
        if self.chk_index is not None:
473
 
            self.index_sizes.append(None)
474
 
            self._write_index('chk', self.chk_index,
475
 
                'content hash bytes', suspend)
476
 
        self.write_stream.close()
477
 
        # Note that this will clobber an existing pack with the same name,
478
 
        # without checking for hash collisions. While this is undesirable this
479
 
        # is something that can be rectified in a subsequent release. One way
480
 
        # to rectify it may be to leave the pack at the original name, writing
481
 
        # its pack-names entry as something like 'HASH: index-sizes
482
 
        # temporary-name'. Allocate that and check for collisions, if it is
483
 
        # collision free then rename it into place. If clients know this scheme
484
 
        # they can handle missing-file errors by:
485
 
        #  - try for HASH.pack
486
 
        #  - try for temporary-name
487
 
        #  - refresh the pack-list to see if the pack is now absent
488
 
        new_name = self.name + '.pack'
489
 
        if not suspend:
490
 
            new_name = '../packs/' + new_name
491
 
        self.upload_transport.rename(self.random_name, new_name)
492
 
        self._state = 'finished'
493
 
        if 'pack' in debug.debug_flags:
494
 
            # XXX: size might be interesting?
495
 
            mutter('%s: create_pack: pack finished: %s%s->%s t+%6.3fs',
496
 
                time.ctime(), self.upload_transport.base, self.random_name,
497
 
                new_name, time.time() - self.start_time)
498
 
 
499
 
    def flush(self):
500
 
        """Flush any current data."""
501
 
        if self._buffer[1]:
502
 
            bytes = ''.join(self._buffer[0])
503
 
            self.write_stream.write(bytes)
504
 
            self._hash.update(bytes)
505
 
            self._buffer[:] = [[], 0]
506
 
 
507
 
    def _get_external_refs(self, index):
508
 
        return index._external_references()
509
 
 
510
 
    def set_write_cache_size(self, size):
511
 
        self._cache_limit = size
512
 
 
513
 
    def _write_index(self, index_type, index, label, suspend=False):
514
 
        """Write out an index.
515
 
 
516
 
        :param index_type: The type of index to write - e.g. 'revision'.
517
 
        :param index: The index object to serialise.
518
 
        :param label: What label to give the index e.g. 'revision'.
519
 
        """
520
 
        index_name = self.index_name(index_type, self.name)
521
 
        if suspend:
522
 
            transport = self.upload_transport
523
 
        else:
524
 
            transport = self.index_transport
525
 
        self.index_sizes[self.index_offset(index_type)] = transport.put_file(
526
 
            index_name, index.finish(), mode=self._file_mode)
527
 
        if 'pack' in debug.debug_flags:
528
 
            # XXX: size might be interesting?
529
 
            mutter('%s: create_pack: wrote %s index: %s%s t+%6.3fs',
530
 
                time.ctime(), label, self.upload_transport.base,
531
 
                self.random_name, time.time() - self.start_time)
532
 
        # Replace the writable index on this object with a readonly,
533
 
        # presently unloaded index. We should alter
534
 
        # the index layer to make its finish() error if add_node is
535
 
        # subsequently used. RBC
536
 
        self._replace_index_with_readonly(index_type)
537
 
 
538
 
 
539
 
class AggregateIndex(object):
540
 
    """An aggregated index for the RepositoryPackCollection.
541
 
 
542
 
    AggregateIndex is reponsible for managing the PackAccess object,
543
 
    Index-To-Pack mapping, and all indices list for a specific type of index
544
 
    such as 'revision index'.
545
 
 
546
 
    A CombinedIndex provides an index on a single key space built up
547
 
    from several on-disk indices.  The AggregateIndex builds on this
548
 
    to provide a knit access layer, and allows having up to one writable
549
 
    index within the collection.
550
 
    """
551
 
    # XXX: Probably 'can be written to' could/should be separated from 'acts
552
 
    # like a knit index' -- mbp 20071024
553
 
 
554
 
    def __init__(self, reload_func=None, flush_func=None):
555
 
        """Create an AggregateIndex.
556
 
 
557
 
        :param reload_func: A function to call if we find we are missing an
558
 
            index. Should have the form reload_func() => True if the list of
559
 
            active pack files has changed.
560
 
        """
561
 
        self._reload_func = reload_func
562
 
        self.index_to_pack = {}
563
 
        self.combined_index = CombinedGraphIndex([], reload_func=reload_func)
564
 
        self.data_access = _DirectPackAccess(self.index_to_pack,
565
 
                                             reload_func=reload_func,
566
 
                                             flush_func=flush_func)
567
 
        self.add_callback = None
568
 
 
569
 
    def replace_indices(self, index_to_pack, indices):
570
 
        """Replace the current mappings with fresh ones.
571
 
 
572
 
        This should probably not be used eventually, rather incremental add and
573
 
        removal of indices. It has been added during refactoring of existing
574
 
        code.
575
 
 
576
 
        :param index_to_pack: A mapping from index objects to
577
 
            (transport, name) tuples for the pack file data.
578
 
        :param indices: A list of indices.
579
 
        """
580
 
        # refresh the revision pack map dict without replacing the instance.
581
 
        self.index_to_pack.clear()
582
 
        self.index_to_pack.update(index_to_pack)
583
 
        # XXX: API break - clearly a 'replace' method would be good?
584
 
        self.combined_index._indices[:] = indices
585
 
        # the current add nodes callback for the current writable index if
586
 
        # there is one.
587
 
        self.add_callback = None
588
 
 
589
 
    def add_index(self, index, pack):
590
 
        """Add index to the aggregate, which is an index for Pack pack.
591
 
 
592
 
        Future searches on the aggregate index will seach this new index
593
 
        before all previously inserted indices.
594
 
 
595
 
        :param index: An Index for the pack.
596
 
        :param pack: A Pack instance.
597
 
        """
598
 
        # expose it to the index map
599
 
        self.index_to_pack[index] = pack.access_tuple()
600
 
        # put it at the front of the linear index list
601
 
        self.combined_index.insert_index(0, index)
602
 
 
603
 
    def add_writable_index(self, index, pack):
604
 
        """Add an index which is able to have data added to it.
605
 
 
606
 
        There can be at most one writable index at any time.  Any
607
 
        modifications made to the knit are put into this index.
608
 
 
609
 
        :param index: An index from the pack parameter.
610
 
        :param pack: A Pack instance.
611
 
        """
612
 
        if self.add_callback is not None:
613
 
            raise AssertionError(
614
 
                "%s already has a writable index through %s" % \
615
 
                (self, self.add_callback))
616
 
        # allow writing: queue writes to a new index
617
 
        self.add_index(index, pack)
618
 
        # Updates the index to packs mapping as a side effect,
619
 
        self.data_access.set_writer(pack._writer, index, pack.access_tuple())
620
 
        self.add_callback = index.add_nodes
621
 
 
622
 
    def clear(self):
623
 
        """Reset all the aggregate data to nothing."""
624
 
        self.data_access.set_writer(None, None, (None, None))
625
 
        self.index_to_pack.clear()
626
 
        del self.combined_index._indices[:]
627
 
        self.add_callback = None
628
 
 
629
 
    def remove_index(self, index, pack):
630
 
        """Remove index from the indices used to answer queries.
631
 
 
632
 
        :param index: An index from the pack parameter.
633
 
        :param pack: A Pack instance.
634
 
        """
635
 
        del self.index_to_pack[index]
636
 
        self.combined_index._indices.remove(index)
637
 
        if (self.add_callback is not None and
638
 
            getattr(index, 'add_nodes', None) == self.add_callback):
639
 
            self.add_callback = None
640
 
            self.data_access.set_writer(None, None, (None, None))
641
 
 
642
 
 
643
 
class Packer(object):
644
 
    """Create a pack from packs."""
645
 
 
646
 
    def __init__(self, pack_collection, packs, suffix, revision_ids=None,
647
 
                 reload_func=None):
648
 
        """Create a Packer.
649
 
 
650
 
        :param pack_collection: A RepositoryPackCollection object where the
651
 
            new pack is being written to.
652
 
        :param packs: The packs to combine.
653
 
        :param suffix: The suffix to use on the temporary files for the pack.
654
 
        :param revision_ids: Revision ids to limit the pack to.
655
 
        :param reload_func: A function to call if a pack file/index goes
656
 
            missing. The side effect of calling this function should be to
657
 
            update self.packs. See also AggregateIndex
658
 
        """
659
 
        self.packs = packs
660
 
        self.suffix = suffix
661
 
        self.revision_ids = revision_ids
662
 
        # The pack object we are creating.
663
 
        self.new_pack = None
664
 
        self._pack_collection = pack_collection
665
 
        self._reload_func = reload_func
666
 
        # The index layer keys for the revisions being copied. None for 'all
667
 
        # objects'.
668
 
        self._revision_keys = None
669
 
        # What text keys to copy. None for 'all texts'. This is set by
670
 
        # _copy_inventory_texts
671
 
        self._text_filter = None
672
 
        self._extra_init()
673
 
 
674
 
    def _extra_init(self):
675
 
        """A template hook to allow extending the constructor trivially."""
676
 
 
677
 
    def _pack_map_and_index_list(self, index_attribute):
678
 
        """Convert a list of packs to an index pack map and index list.
679
 
 
680
 
        :param index_attribute: The attribute that the desired index is found
681
 
            on.
682
 
        :return: A tuple (map, list) where map contains the dict from
683
 
            index:pack_tuple, and list contains the indices in the preferred
684
 
            access order.
685
 
        """
686
 
        indices = []
687
 
        pack_map = {}
688
 
        for pack_obj in self.packs:
689
 
            index = getattr(pack_obj, index_attribute)
690
 
            indices.append(index)
691
 
            pack_map[index] = pack_obj
692
 
        return pack_map, indices
693
 
 
694
 
    def _index_contents(self, indices, key_filter=None):
695
 
        """Get an iterable of the index contents from a pack_map.
696
 
 
697
 
        :param indices: The list of indices to query
698
 
        :param key_filter: An optional filter to limit the keys returned.
699
 
        """
700
 
        all_index = CombinedGraphIndex(indices)
701
 
        if key_filter is None:
702
 
            return all_index.iter_all_entries()
703
 
        else:
704
 
            return all_index.iter_entries(key_filter)
705
 
 
706
 
    def pack(self, pb=None):
707
 
        """Create a new pack by reading data from other packs.
708
 
 
709
 
        This does little more than a bulk copy of data. One key difference
710
 
        is that data with the same item key across multiple packs is elided
711
 
        from the output. The new pack is written into the current pack store
712
 
        along with its indices, and the name added to the pack names. The
713
 
        source packs are not altered and are not required to be in the current
714
 
        pack collection.
715
 
 
716
 
        :param pb: An optional progress bar to use. A nested bar is created if
717
 
            this is None.
718
 
        :return: A Pack object, or None if nothing was copied.
719
 
        """
720
 
        # open a pack - using the same name as the last temporary file
721
 
        # - which has already been flushed, so its safe.
722
 
        # XXX: - duplicate code warning with start_write_group; fix before
723
 
        #      considering 'done'.
724
 
        if self._pack_collection._new_pack is not None:
725
 
            raise errors.BzrError('call to %s.pack() while another pack is'
726
 
                                  ' being written.'
727
 
                                  % (self.__class__.__name__,))
728
 
        if self.revision_ids is not None:
729
 
            if len(self.revision_ids) == 0:
730
 
                # silly fetch request.
731
 
                return None
732
 
            else:
733
 
                self.revision_ids = frozenset(self.revision_ids)
734
 
                self.revision_keys = frozenset((revid,) for revid in
735
 
                    self.revision_ids)
736
 
        if pb is None:
737
 
            self.pb = ui.ui_factory.nested_progress_bar()
738
 
        else:
739
 
            self.pb = pb
740
 
        try:
741
 
            return self._create_pack_from_packs()
742
 
        finally:
743
 
            if pb is None:
744
 
                self.pb.finished()
745
 
 
746
 
    def open_pack(self):
747
 
        """Open a pack for the pack we are creating."""
748
 
        new_pack = self._pack_collection.pack_factory(self._pack_collection,
749
 
                upload_suffix=self.suffix,
750
 
                file_mode=self._pack_collection.repo.bzrdir._get_file_mode())
751
 
        # We know that we will process all nodes in order, and don't need to
752
 
        # query, so don't combine any indices spilled to disk until we are done
753
 
        new_pack.revision_index.set_optimize(combine_backing_indices=False)
754
 
        new_pack.inventory_index.set_optimize(combine_backing_indices=False)
755
 
        new_pack.text_index.set_optimize(combine_backing_indices=False)
756
 
        new_pack.signature_index.set_optimize(combine_backing_indices=False)
757
 
        return new_pack
758
 
 
759
 
    def _update_pack_order(self, entries, index_to_pack_map):
760
 
        """Determine how we want our packs to be ordered.
761
 
 
762
 
        This changes the sort order of the self.packs list so that packs unused
763
 
        by 'entries' will be at the end of the list, so that future requests
764
 
        can avoid probing them.  Used packs will be at the front of the
765
 
        self.packs list, in the order of their first use in 'entries'.
766
 
 
767
 
        :param entries: A list of (index, ...) tuples
768
 
        :param index_to_pack_map: A mapping from index objects to pack objects.
769
 
        """
770
 
        packs = []
771
 
        seen_indexes = set()
772
 
        for entry in entries:
773
 
            index = entry[0]
774
 
            if index not in seen_indexes:
775
 
                packs.append(index_to_pack_map[index])
776
 
                seen_indexes.add(index)
777
 
        if len(packs) == len(self.packs):
778
 
            if 'pack' in debug.debug_flags:
779
 
                mutter('Not changing pack list, all packs used.')
780
 
            return
781
 
        seen_packs = set(packs)
782
 
        for pack in self.packs:
783
 
            if pack not in seen_packs:
784
 
                packs.append(pack)
785
 
                seen_packs.add(pack)
786
 
        if 'pack' in debug.debug_flags:
787
 
            old_names = [p.access_tuple()[1] for p in self.packs]
788
 
            new_names = [p.access_tuple()[1] for p in packs]
789
 
            mutter('Reordering packs\nfrom: %s\n  to: %s',
790
 
                   old_names, new_names)
791
 
        self.packs = packs
792
 
 
793
 
    def _copy_revision_texts(self):
794
 
        """Copy revision data to the new pack."""
795
 
        # select revisions
796
 
        if self.revision_ids:
797
 
            revision_keys = [(revision_id,) for revision_id in self.revision_ids]
798
 
        else:
799
 
            revision_keys = None
800
 
        # select revision keys
801
 
        revision_index_map, revision_indices = self._pack_map_and_index_list(
802
 
            'revision_index')
803
 
        revision_nodes = self._index_contents(revision_indices, revision_keys)
804
 
        revision_nodes = list(revision_nodes)
805
 
        self._update_pack_order(revision_nodes, revision_index_map)
806
 
        # copy revision keys and adjust values
807
 
        self.pb.update("Copying revision texts", 1)
808
 
        total_items, readv_group_iter = self._revision_node_readv(revision_nodes)
809
 
        list(self._copy_nodes_graph(revision_index_map, self.new_pack._writer,
810
 
            self.new_pack.revision_index, readv_group_iter, total_items))
811
 
        if 'pack' in debug.debug_flags:
812
 
            mutter('%s: create_pack: revisions copied: %s%s %d items t+%6.3fs',
813
 
                time.ctime(), self._pack_collection._upload_transport.base,
814
 
                self.new_pack.random_name,
815
 
                self.new_pack.revision_index.key_count(),
816
 
                time.time() - self.new_pack.start_time)
817
 
        self._revision_keys = revision_keys
818
 
 
819
 
    def _copy_inventory_texts(self):
820
 
        """Copy the inventory texts to the new pack.
821
 
 
822
 
        self._revision_keys is used to determine what inventories to copy.
823
 
 
824
 
        Sets self._text_filter appropriately.
825
 
        """
826
 
        # select inventory keys
827
 
        inv_keys = self._revision_keys # currently the same keyspace, and note that
828
 
        # querying for keys here could introduce a bug where an inventory item
829
 
        # is missed, so do not change it to query separately without cross
830
 
        # checking like the text key check below.
831
 
        inventory_index_map, inventory_indices = self._pack_map_and_index_list(
832
 
            'inventory_index')
833
 
        inv_nodes = self._index_contents(inventory_indices, inv_keys)
834
 
        # copy inventory keys and adjust values
835
 
        # XXX: Should be a helper function to allow different inv representation
836
 
        # at this point.
837
 
        self.pb.update("Copying inventory texts", 2)
838
 
        total_items, readv_group_iter = self._least_readv_node_readv(inv_nodes)
839
 
        # Only grab the output lines if we will be processing them
840
 
        output_lines = bool(self.revision_ids)
841
 
        inv_lines = self._copy_nodes_graph(inventory_index_map,
842
 
            self.new_pack._writer, self.new_pack.inventory_index,
843
 
            readv_group_iter, total_items, output_lines=output_lines)
844
 
        if self.revision_ids:
845
 
            self._process_inventory_lines(inv_lines)
846
 
        else:
847
 
            # eat the iterator to cause it to execute.
848
 
            list(inv_lines)
849
 
            self._text_filter = None
850
 
        if 'pack' in debug.debug_flags:
851
 
            mutter('%s: create_pack: inventories copied: %s%s %d items t+%6.3fs',
852
 
                time.ctime(), self._pack_collection._upload_transport.base,
853
 
                self.new_pack.random_name,
854
 
                self.new_pack.inventory_index.key_count(),
855
 
                time.time() - self.new_pack.start_time)
856
 
 
857
 
    def _copy_text_texts(self):
858
 
        # select text keys
859
 
        text_index_map, text_nodes = self._get_text_nodes()
860
 
        if self._text_filter is not None:
861
 
            # We could return the keys copied as part of the return value from
862
 
            # _copy_nodes_graph but this doesn't work all that well with the
863
 
            # need to get line output too, so we check separately, and as we're
864
 
            # going to buffer everything anyway, we check beforehand, which
865
 
            # saves reading knit data over the wire when we know there are
866
 
            # mising records.
867
 
            text_nodes = set(text_nodes)
868
 
            present_text_keys = set(_node[1] for _node in text_nodes)
869
 
            missing_text_keys = set(self._text_filter) - present_text_keys
870
 
            if missing_text_keys:
871
 
                # TODO: raise a specific error that can handle many missing
872
 
                # keys.
873
 
                mutter("missing keys during fetch: %r", missing_text_keys)
874
 
                a_missing_key = missing_text_keys.pop()
875
 
                raise errors.RevisionNotPresent(a_missing_key[1],
876
 
                    a_missing_key[0])
877
 
        # copy text keys and adjust values
878
 
        self.pb.update("Copying content texts", 3)
879
 
        total_items, readv_group_iter = self._least_readv_node_readv(text_nodes)
880
 
        list(self._copy_nodes_graph(text_index_map, self.new_pack._writer,
881
 
            self.new_pack.text_index, readv_group_iter, total_items))
882
 
        self._log_copied_texts()
883
 
 
884
 
    def _create_pack_from_packs(self):
885
 
        self.pb.update("Opening pack", 0, 5)
886
 
        self.new_pack = self.open_pack()
887
 
        new_pack = self.new_pack
888
 
        # buffer data - we won't be reading-back during the pack creation and
889
 
        # this makes a significant difference on sftp pushes.
890
 
        new_pack.set_write_cache_size(1024*1024)
891
 
        if 'pack' in debug.debug_flags:
892
 
            plain_pack_list = ['%s%s' % (a_pack.pack_transport.base, a_pack.name)
893
 
                for a_pack in self.packs]
894
 
            if self.revision_ids is not None:
895
 
                rev_count = len(self.revision_ids)
896
 
            else:
897
 
                rev_count = 'all'
898
 
            mutter('%s: create_pack: creating pack from source packs: '
899
 
                '%s%s %s revisions wanted %s t=0',
900
 
                time.ctime(), self._pack_collection._upload_transport.base, new_pack.random_name,
901
 
                plain_pack_list, rev_count)
902
 
        self._copy_revision_texts()
903
 
        self._copy_inventory_texts()
904
 
        self._copy_text_texts()
905
 
        # select signature keys
906
 
        signature_filter = self._revision_keys # same keyspace
907
 
        signature_index_map, signature_indices = self._pack_map_and_index_list(
908
 
            'signature_index')
909
 
        signature_nodes = self._index_contents(signature_indices,
910
 
            signature_filter)
911
 
        # copy signature keys and adjust values
912
 
        self.pb.update("Copying signature texts", 4)
913
 
        self._copy_nodes(signature_nodes, signature_index_map, new_pack._writer,
914
 
            new_pack.signature_index)
915
 
        if 'pack' in debug.debug_flags:
916
 
            mutter('%s: create_pack: revision signatures copied: %s%s %d items t+%6.3fs',
917
 
                time.ctime(), self._pack_collection._upload_transport.base, new_pack.random_name,
918
 
                new_pack.signature_index.key_count(),
919
 
                time.time() - new_pack.start_time)
920
 
        # copy chk contents
921
 
        # NB XXX: how to check CHK references are present? perhaps by yielding
922
 
        # the items? How should that interact with stacked repos?
923
 
        if new_pack.chk_index is not None:
924
 
            self._copy_chks()
925
 
            if 'pack' in debug.debug_flags:
926
 
                mutter('%s: create_pack: chk content copied: %s%s %d items t+%6.3fs',
927
 
                    time.ctime(), self._pack_collection._upload_transport.base,
928
 
                    new_pack.random_name,
929
 
                    new_pack.chk_index.key_count(),
930
 
                    time.time() - new_pack.start_time)
931
 
        new_pack._check_references()
932
 
        if not self._use_pack(new_pack):
933
 
            new_pack.abort()
934
 
            return None
935
 
        self.pb.update("Finishing pack", 5)
936
 
        new_pack.finish()
937
 
        self._pack_collection.allocate(new_pack)
938
 
        return new_pack
939
 
 
940
 
    def _copy_chks(self, refs=None):
941
 
        # XXX: Todo, recursive follow-pointers facility when fetching some
942
 
        # revisions only.
943
 
        chk_index_map, chk_indices = self._pack_map_and_index_list(
944
 
            'chk_index')
945
 
        chk_nodes = self._index_contents(chk_indices, refs)
946
 
        new_refs = set()
947
 
        # TODO: This isn't strictly tasteful as we are accessing some private
948
 
        #       variables (_serializer). Perhaps a better way would be to have
949
 
        #       Repository._deserialise_chk_node()
950
 
        search_key_func = chk_map.search_key_registry.get(
951
 
            self._pack_collection.repo._serializer.search_key_name)
952
 
        def accumlate_refs(lines):
953
 
            # XXX: move to a generic location
954
 
            # Yay mismatch:
955
 
            bytes = ''.join(lines)
956
 
            node = chk_map._deserialise(bytes, ("unknown",), search_key_func)
957
 
            new_refs.update(node.refs())
958
 
        self._copy_nodes(chk_nodes, chk_index_map, self.new_pack._writer,
959
 
            self.new_pack.chk_index, output_lines=accumlate_refs)
960
 
        return new_refs
961
 
 
962
 
    def _copy_nodes(self, nodes, index_map, writer, write_index,
963
 
        output_lines=None):
964
 
        """Copy knit nodes between packs with no graph references.
965
 
 
966
 
        :param output_lines: Output full texts of copied items.
967
 
        """
968
 
        pb = ui.ui_factory.nested_progress_bar()
969
 
        try:
970
 
            return self._do_copy_nodes(nodes, index_map, writer,
971
 
                write_index, pb, output_lines=output_lines)
972
 
        finally:
973
 
            pb.finished()
974
 
 
975
 
    def _do_copy_nodes(self, nodes, index_map, writer, write_index, pb,
976
 
        output_lines=None):
977
 
        # for record verification
978
 
        knit = KnitVersionedFiles(None, None)
979
 
        # plan a readv on each source pack:
980
 
        # group by pack
981
 
        nodes = sorted(nodes)
982
 
        # how to map this into knit.py - or knit.py into this?
983
 
        # we don't want the typical knit logic, we want grouping by pack
984
 
        # at this point - perhaps a helper library for the following code
985
 
        # duplication points?
986
 
        request_groups = {}
987
 
        for index, key, value in nodes:
988
 
            if index not in request_groups:
989
 
                request_groups[index] = []
990
 
            request_groups[index].append((key, value))
991
 
        record_index = 0
992
 
        pb.update("Copied record", record_index, len(nodes))
993
 
        for index, items in request_groups.iteritems():
994
 
            pack_readv_requests = []
995
 
            for key, value in items:
996
 
                # ---- KnitGraphIndex.get_position
997
 
                bits = value[1:].split(' ')
998
 
                offset, length = int(bits[0]), int(bits[1])
999
 
                pack_readv_requests.append((offset, length, (key, value[0])))
1000
 
            # linear scan up the pack
1001
 
            pack_readv_requests.sort()
1002
 
            # copy the data
1003
 
            pack_obj = index_map[index]
1004
 
            transport, path = pack_obj.access_tuple()
1005
 
            try:
1006
 
                reader = pack.make_readv_reader(transport, path,
1007
 
                    [offset[0:2] for offset in pack_readv_requests])
1008
 
            except errors.NoSuchFile:
1009
 
                if self._reload_func is not None:
1010
 
                    self._reload_func()
1011
 
                raise
1012
 
            for (names, read_func), (_1, _2, (key, eol_flag)) in \
1013
 
                izip(reader.iter_records(), pack_readv_requests):
1014
 
                raw_data = read_func(None)
1015
 
                # check the header only
1016
 
                if output_lines is not None:
1017
 
                    output_lines(knit._parse_record(key[-1], raw_data)[0])
1018
 
                else:
1019
 
                    df, _ = knit._parse_record_header(key, raw_data)
1020
 
                    df.close()
1021
 
                pos, size = writer.add_bytes_record(raw_data, names)
1022
 
                write_index.add_node(key, eol_flag + "%d %d" % (pos, size))
1023
 
                pb.update("Copied record", record_index)
1024
 
                record_index += 1
1025
 
 
1026
 
    def _copy_nodes_graph(self, index_map, writer, write_index,
1027
 
        readv_group_iter, total_items, output_lines=False):
1028
 
        """Copy knit nodes between packs.
1029
 
 
1030
 
        :param output_lines: Return lines present in the copied data as
1031
 
            an iterator of line,version_id.
1032
 
        """
1033
 
        pb = ui.ui_factory.nested_progress_bar()
1034
 
        try:
1035
 
            for result in self._do_copy_nodes_graph(index_map, writer,
1036
 
                write_index, output_lines, pb, readv_group_iter, total_items):
1037
 
                yield result
1038
 
        except Exception:
1039
 
            # Python 2.4 does not permit try:finally: in a generator.
1040
 
            pb.finished()
1041
 
            raise
1042
 
        else:
1043
 
            pb.finished()
1044
 
 
1045
 
    def _do_copy_nodes_graph(self, index_map, writer, write_index,
1046
 
        output_lines, pb, readv_group_iter, total_items):
1047
 
        # for record verification
1048
 
        knit = KnitVersionedFiles(None, None)
1049
 
        # for line extraction when requested (inventories only)
1050
 
        if output_lines:
1051
 
            factory = KnitPlainFactory()
1052
 
        record_index = 0
1053
 
        pb.update("Copied record", record_index, total_items)
1054
 
        for index, readv_vector, node_vector in readv_group_iter:
1055
 
            # copy the data
1056
 
            pack_obj = index_map[index]
1057
 
            transport, path = pack_obj.access_tuple()
1058
 
            try:
1059
 
                reader = pack.make_readv_reader(transport, path, readv_vector)
1060
 
            except errors.NoSuchFile:
1061
 
                if self._reload_func is not None:
1062
 
                    self._reload_func()
1063
 
                raise
1064
 
            for (names, read_func), (key, eol_flag, references) in \
1065
 
                izip(reader.iter_records(), node_vector):
1066
 
                raw_data = read_func(None)
1067
 
                if output_lines:
1068
 
                    # read the entire thing
1069
 
                    content, _ = knit._parse_record(key[-1], raw_data)
1070
 
                    if len(references[-1]) == 0:
1071
 
                        line_iterator = factory.get_fulltext_content(content)
1072
 
                    else:
1073
 
                        line_iterator = factory.get_linedelta_content(content)
1074
 
                    for line in line_iterator:
1075
 
                        yield line, key
1076
 
                else:
1077
 
                    # check the header only
1078
 
                    df, _ = knit._parse_record_header(key, raw_data)
1079
 
                    df.close()
1080
 
                pos, size = writer.add_bytes_record(raw_data, names)
1081
 
                write_index.add_node(key, eol_flag + "%d %d" % (pos, size), references)
1082
 
                pb.update("Copied record", record_index)
1083
 
                record_index += 1
1084
 
 
1085
 
    def _get_text_nodes(self):
1086
 
        text_index_map, text_indices = self._pack_map_and_index_list(
1087
 
            'text_index')
1088
 
        return text_index_map, self._index_contents(text_indices,
1089
 
            self._text_filter)
1090
 
 
1091
 
    def _least_readv_node_readv(self, nodes):
1092
 
        """Generate request groups for nodes using the least readv's.
1093
 
 
1094
 
        :param nodes: An iterable of graph index nodes.
1095
 
        :return: Total node count and an iterator of the data needed to perform
1096
 
            readvs to obtain the data for nodes. Each item yielded by the
1097
 
            iterator is a tuple with:
1098
 
            index, readv_vector, node_vector. readv_vector is a list ready to
1099
 
            hand to the transport readv method, and node_vector is a list of
1100
 
            (key, eol_flag, references) for the the node retrieved by the
1101
 
            matching readv_vector.
1102
 
        """
1103
 
        # group by pack so we do one readv per pack
1104
 
        nodes = sorted(nodes)
1105
 
        total = len(nodes)
1106
 
        request_groups = {}
1107
 
        for index, key, value, references in nodes:
1108
 
            if index not in request_groups:
1109
 
                request_groups[index] = []
1110
 
            request_groups[index].append((key, value, references))
1111
 
        result = []
1112
 
        for index, items in request_groups.iteritems():
1113
 
            pack_readv_requests = []
1114
 
            for key, value, references in items:
1115
 
                # ---- KnitGraphIndex.get_position
1116
 
                bits = value[1:].split(' ')
1117
 
                offset, length = int(bits[0]), int(bits[1])
1118
 
                pack_readv_requests.append(
1119
 
                    ((offset, length), (key, value[0], references)))
1120
 
            # linear scan up the pack to maximum range combining.
1121
 
            pack_readv_requests.sort()
1122
 
            # split out the readv and the node data.
1123
 
            pack_readv = [readv for readv, node in pack_readv_requests]
1124
 
            node_vector = [node for readv, node in pack_readv_requests]
1125
 
            result.append((index, pack_readv, node_vector))
1126
 
        return total, result
1127
 
 
1128
 
    def _log_copied_texts(self):
1129
 
        if 'pack' in debug.debug_flags:
1130
 
            mutter('%s: create_pack: file texts copied: %s%s %d items t+%6.3fs',
1131
 
                time.ctime(), self._pack_collection._upload_transport.base,
1132
 
                self.new_pack.random_name,
1133
 
                self.new_pack.text_index.key_count(),
1134
 
                time.time() - self.new_pack.start_time)
1135
 
 
1136
 
    def _process_inventory_lines(self, inv_lines):
1137
 
        """Use up the inv_lines generator and setup a text key filter."""
1138
 
        repo = self._pack_collection.repo
1139
 
        fileid_revisions = repo._find_file_ids_from_xml_inventory_lines(
1140
 
            inv_lines, self.revision_keys)
1141
 
        text_filter = []
1142
 
        for fileid, file_revids in fileid_revisions.iteritems():
1143
 
            text_filter.extend([(fileid, file_revid) for file_revid in file_revids])
1144
 
        self._text_filter = text_filter
1145
 
 
1146
 
    def _revision_node_readv(self, revision_nodes):
1147
 
        """Return the total revisions and the readv's to issue.
1148
 
 
1149
 
        :param revision_nodes: The revision index contents for the packs being
1150
 
            incorporated into the new pack.
1151
 
        :return: As per _least_readv_node_readv.
1152
 
        """
1153
 
        return self._least_readv_node_readv(revision_nodes)
1154
 
 
1155
 
    def _use_pack(self, new_pack):
1156
 
        """Return True if new_pack should be used.
1157
 
 
1158
 
        :param new_pack: The pack that has just been created.
1159
 
        :return: True if the pack should be used.
1160
 
        """
1161
 
        return new_pack.data_inserted()
1162
 
 
1163
 
 
1164
 
class OptimisingPacker(Packer):
1165
 
    """A packer which spends more time to create better disk layouts."""
1166
 
 
1167
 
    def _revision_node_readv(self, revision_nodes):
1168
 
        """Return the total revisions and the readv's to issue.
1169
 
 
1170
 
        This sort places revisions in topological order with the ancestors
1171
 
        after the children.
1172
 
 
1173
 
        :param revision_nodes: The revision index contents for the packs being
1174
 
            incorporated into the new pack.
1175
 
        :return: As per _least_readv_node_readv.
1176
 
        """
1177
 
        # build an ancestors dict
1178
 
        ancestors = {}
1179
 
        by_key = {}
1180
 
        for index, key, value, references in revision_nodes:
1181
 
            ancestors[key] = references[0]
1182
 
            by_key[key] = (index, value, references)
1183
 
        order = tsort.topo_sort(ancestors)
1184
 
        total = len(order)
1185
 
        # Single IO is pathological, but it will work as a starting point.
1186
 
        requests = []
1187
 
        for key in reversed(order):
1188
 
            index, value, references = by_key[key]
1189
 
            # ---- KnitGraphIndex.get_position
1190
 
            bits = value[1:].split(' ')
1191
 
            offset, length = int(bits[0]), int(bits[1])
1192
 
            requests.append(
1193
 
                (index, [(offset, length)], [(key, value[0], references)]))
1194
 
        # TODO: combine requests in the same index that are in ascending order.
1195
 
        return total, requests
1196
 
 
1197
 
    def open_pack(self):
1198
 
        """Open a pack for the pack we are creating."""
1199
 
        new_pack = super(OptimisingPacker, self).open_pack()
1200
 
        # Turn on the optimization flags for all the index builders.
1201
 
        new_pack.revision_index.set_optimize(for_size=True)
1202
 
        new_pack.inventory_index.set_optimize(for_size=True)
1203
 
        new_pack.text_index.set_optimize(for_size=True)
1204
 
        new_pack.signature_index.set_optimize(for_size=True)
1205
 
        return new_pack
1206
 
 
1207
 
 
1208
 
class ReconcilePacker(Packer):
1209
 
    """A packer which regenerates indices etc as it copies.
1210
 
 
1211
 
    This is used by ``bzr reconcile`` to cause parent text pointers to be
1212
 
    regenerated.
1213
 
    """
1214
 
 
1215
 
    def _extra_init(self):
1216
 
        self._data_changed = False
1217
 
 
1218
 
    def _process_inventory_lines(self, inv_lines):
1219
 
        """Generate a text key reference map rather for reconciling with."""
1220
 
        repo = self._pack_collection.repo
1221
 
        refs = repo._find_text_key_references_from_xml_inventory_lines(
1222
 
            inv_lines)
1223
 
        self._text_refs = refs
1224
 
        # during reconcile we:
1225
 
        #  - convert unreferenced texts to full texts
1226
 
        #  - correct texts which reference a text not copied to be full texts
1227
 
        #  - copy all others as-is but with corrected parents.
1228
 
        #  - so at this point we don't know enough to decide what becomes a full
1229
 
        #    text.
1230
 
        self._text_filter = None
1231
 
 
1232
 
    def _copy_text_texts(self):
1233
 
        """generate what texts we should have and then copy."""
1234
 
        self.pb.update("Copying content texts", 3)
1235
 
        # we have three major tasks here:
1236
 
        # 1) generate the ideal index
1237
 
        repo = self._pack_collection.repo
1238
 
        ancestors = dict([(key[0], tuple(ref[0] for ref in refs[0])) for
1239
 
            _1, key, _2, refs in
1240
 
            self.new_pack.revision_index.iter_all_entries()])
1241
 
        ideal_index = repo._generate_text_key_index(self._text_refs, ancestors)
1242
 
        # 2) generate a text_nodes list that contains all the deltas that can
1243
 
        #    be used as-is, with corrected parents.
1244
 
        ok_nodes = []
1245
 
        bad_texts = []
1246
 
        discarded_nodes = []
1247
 
        NULL_REVISION = _mod_revision.NULL_REVISION
1248
 
        text_index_map, text_nodes = self._get_text_nodes()
1249
 
        for node in text_nodes:
1250
 
            # 0 - index
1251
 
            # 1 - key
1252
 
            # 2 - value
1253
 
            # 3 - refs
1254
 
            try:
1255
 
                ideal_parents = tuple(ideal_index[node[1]])
1256
 
            except KeyError:
1257
 
                discarded_nodes.append(node)
1258
 
                self._data_changed = True
1259
 
            else:
1260
 
                if ideal_parents == (NULL_REVISION,):
1261
 
                    ideal_parents = ()
1262
 
                if ideal_parents == node[3][0]:
1263
 
                    # no change needed.
1264
 
                    ok_nodes.append(node)
1265
 
                elif ideal_parents[0:1] == node[3][0][0:1]:
1266
 
                    # the left most parent is the same, or there are no parents
1267
 
                    # today. Either way, we can preserve the representation as
1268
 
                    # long as we change the refs to be inserted.
1269
 
                    self._data_changed = True
1270
 
                    ok_nodes.append((node[0], node[1], node[2],
1271
 
                        (ideal_parents, node[3][1])))
1272
 
                    self._data_changed = True
1273
 
                else:
1274
 
                    # Reinsert this text completely
1275
 
                    bad_texts.append((node[1], ideal_parents))
1276
 
                    self._data_changed = True
1277
 
        # we're finished with some data.
1278
 
        del ideal_index
1279
 
        del text_nodes
1280
 
        # 3) bulk copy the ok data
1281
 
        total_items, readv_group_iter = self._least_readv_node_readv(ok_nodes)
1282
 
        list(self._copy_nodes_graph(text_index_map, self.new_pack._writer,
1283
 
            self.new_pack.text_index, readv_group_iter, total_items))
1284
 
        # 4) adhoc copy all the other texts.
1285
 
        # We have to topologically insert all texts otherwise we can fail to
1286
 
        # reconcile when parts of a single delta chain are preserved intact,
1287
 
        # and other parts are not. E.g. Discarded->d1->d2->d3. d1 will be
1288
 
        # reinserted, and if d3 has incorrect parents it will also be
1289
 
        # reinserted. If we insert d3 first, d2 is present (as it was bulk
1290
 
        # copied), so we will try to delta, but d2 is not currently able to be
1291
 
        # extracted because it's basis d1 is not present. Topologically sorting
1292
 
        # addresses this. The following generates a sort for all the texts that
1293
 
        # are being inserted without having to reference the entire text key
1294
 
        # space (we only topo sort the revisions, which is smaller).
1295
 
        topo_order = tsort.topo_sort(ancestors)
1296
 
        rev_order = dict(zip(topo_order, range(len(topo_order))))
1297
 
        bad_texts.sort(key=lambda key:rev_order[key[0][1]])
1298
 
        transaction = repo.get_transaction()
1299
 
        file_id_index = GraphIndexPrefixAdapter(
1300
 
            self.new_pack.text_index,
1301
 
            ('blank', ), 1,
1302
 
            add_nodes_callback=self.new_pack.text_index.add_nodes)
1303
 
        data_access = _DirectPackAccess(
1304
 
                {self.new_pack.text_index:self.new_pack.access_tuple()})
1305
 
        data_access.set_writer(self.new_pack._writer, self.new_pack.text_index,
1306
 
            self.new_pack.access_tuple())
1307
 
        output_texts = KnitVersionedFiles(
1308
 
            _KnitGraphIndex(self.new_pack.text_index,
1309
 
                add_callback=self.new_pack.text_index.add_nodes,
1310
 
                deltas=True, parents=True, is_locked=repo.is_locked),
1311
 
            data_access=data_access, max_delta_chain=200)
1312
 
        for key, parent_keys in bad_texts:
1313
 
            # We refer to the new pack to delta data being output.
1314
 
            # A possible improvement would be to catch errors on short reads
1315
 
            # and only flush then.
1316
 
            self.new_pack.flush()
1317
 
            parents = []
1318
 
            for parent_key in parent_keys:
1319
 
                if parent_key[0] != key[0]:
1320
 
                    # Graph parents must match the fileid
1321
 
                    raise errors.BzrError('Mismatched key parent %r:%r' %
1322
 
                        (key, parent_keys))
1323
 
                parents.append(parent_key[1])
1324
 
            text_lines = osutils.split_lines(repo.texts.get_record_stream(
1325
 
                [key], 'unordered', True).next().get_bytes_as('fulltext'))
1326
 
            output_texts.add_lines(key, parent_keys, text_lines,
1327
 
                random_id=True, check_content=False)
1328
 
        # 5) check that nothing inserted has a reference outside the keyspace.
1329
 
        missing_text_keys = self.new_pack.text_index._external_references()
1330
 
        if missing_text_keys:
1331
 
            raise errors.BzrCheckError('Reference to missing compression parents %r'
1332
 
                % (missing_text_keys,))
1333
 
        self._log_copied_texts()
1334
 
 
1335
 
    def _use_pack(self, new_pack):
1336
 
        """Override _use_pack to check for reconcile having changed content."""
1337
 
        # XXX: we might be better checking this at the copy time.
1338
 
        original_inventory_keys = set()
1339
 
        inv_index = self._pack_collection.inventory_index.combined_index
1340
 
        for entry in inv_index.iter_all_entries():
1341
 
            original_inventory_keys.add(entry[1])
1342
 
        new_inventory_keys = set()
1343
 
        for entry in new_pack.inventory_index.iter_all_entries():
1344
 
            new_inventory_keys.add(entry[1])
1345
 
        if new_inventory_keys != original_inventory_keys:
1346
 
            self._data_changed = True
1347
 
        return new_pack.data_inserted() and self._data_changed
1348
 
 
1349
 
 
1350
 
class RepositoryPackCollection(object):
1351
 
    """Management of packs within a repository.
1352
 
 
1353
 
    :ivar _names: map of {pack_name: (index_size,)}
1354
 
    """
1355
 
 
1356
 
    pack_factory = NewPack
1357
 
 
1358
 
    def __init__(self, repo, transport, index_transport, upload_transport,
1359
 
                 pack_transport, index_builder_class, index_class,
1360
 
                 use_chk_index):
1361
 
        """Create a new RepositoryPackCollection.
1362
 
 
1363
 
        :param transport: Addresses the repository base directory
1364
 
            (typically .bzr/repository/).
1365
 
        :param index_transport: Addresses the directory containing indices.
1366
 
        :param upload_transport: Addresses the directory into which packs are written
1367
 
            while they're being created.
1368
 
        :param pack_transport: Addresses the directory of existing complete packs.
1369
 
        :param index_builder_class: The index builder class to use.
1370
 
        :param index_class: The index class to use.
1371
 
        :param use_chk_index: Whether to setup and manage a CHK index.
1372
 
        """
1373
 
        # XXX: This should call self.reset()
1374
 
        self.repo = repo
1375
 
        self.transport = transport
1376
 
        self._index_transport = index_transport
1377
 
        self._upload_transport = upload_transport
1378
 
        self._pack_transport = pack_transport
1379
 
        self._index_builder_class = index_builder_class
1380
 
        self._index_class = index_class
1381
 
        self._suffix_offsets = {'.rix': 0, '.iix': 1, '.tix': 2, '.six': 3,
1382
 
            '.cix': 4}
1383
 
        self.packs = []
1384
 
        # name:Pack mapping
1385
 
        self._names = None
1386
 
        self._packs_by_name = {}
1387
 
        # the previous pack-names content
1388
 
        self._packs_at_load = None
1389
 
        # when a pack is being created by this object, the state of that pack.
1390
 
        self._new_pack = None
1391
 
        # aggregated revision index data
1392
 
        flush = self._flush_new_pack
1393
 
        self.revision_index = AggregateIndex(self.reload_pack_names, flush)
1394
 
        self.inventory_index = AggregateIndex(self.reload_pack_names, flush)
1395
 
        self.text_index = AggregateIndex(self.reload_pack_names, flush)
1396
 
        self.signature_index = AggregateIndex(self.reload_pack_names, flush)
1397
 
        if use_chk_index:
1398
 
            self.chk_index = AggregateIndex(self.reload_pack_names, flush)
1399
 
        else:
1400
 
            # used to determine if we're using a chk_index elsewhere.
1401
 
            self.chk_index = None
1402
 
        # resumed packs
1403
 
        self._resumed_packs = []
1404
 
 
1405
 
    def add_pack_to_memory(self, pack):
1406
 
        """Make a Pack object available to the repository to satisfy queries.
1407
 
 
1408
 
        :param pack: A Pack object.
1409
 
        """
1410
 
        if pack.name in self._packs_by_name:
1411
 
            raise AssertionError(
1412
 
                'pack %s already in _packs_by_name' % (pack.name,))
1413
 
        self.packs.append(pack)
1414
 
        self._packs_by_name[pack.name] = pack
1415
 
        self.revision_index.add_index(pack.revision_index, pack)
1416
 
        self.inventory_index.add_index(pack.inventory_index, pack)
1417
 
        self.text_index.add_index(pack.text_index, pack)
1418
 
        self.signature_index.add_index(pack.signature_index, pack)
1419
 
        if self.chk_index is not None:
1420
 
            self.chk_index.add_index(pack.chk_index, pack)
1421
 
 
1422
 
    def all_packs(self):
1423
 
        """Return a list of all the Pack objects this repository has.
1424
 
 
1425
 
        Note that an in-progress pack being created is not returned.
1426
 
 
1427
 
        :return: A list of Pack objects for all the packs in the repository.
1428
 
        """
1429
 
        result = []
1430
 
        for name in self.names():
1431
 
            result.append(self.get_pack_by_name(name))
1432
 
        return result
1433
 
 
1434
 
    def autopack(self):
1435
 
        """Pack the pack collection incrementally.
1436
 
 
1437
 
        This will not attempt global reorganisation or recompression,
1438
 
        rather it will just ensure that the total number of packs does
1439
 
        not grow without bound. It uses the _max_pack_count method to
1440
 
        determine if autopacking is needed, and the pack_distribution
1441
 
        method to determine the number of revisions in each pack.
1442
 
 
1443
 
        If autopacking takes place then the packs name collection will have
1444
 
        been flushed to disk - packing requires updating the name collection
1445
 
        in synchronisation with certain steps. Otherwise the names collection
1446
 
        is not flushed.
1447
 
 
1448
 
        :return: True if packing took place.
1449
 
        """
1450
 
        while True:
1451
 
            try:
1452
 
                return self._do_autopack()
1453
 
            except errors.RetryAutopack, e:
1454
 
                # If we get a RetryAutopack exception, we should abort the
1455
 
                # current action, and retry.
1456
 
                pass
1457
 
 
1458
 
    def _do_autopack(self):
1459
 
        # XXX: Should not be needed when the management of indices is sane.
1460
 
        total_revisions = self.revision_index.combined_index.key_count()
1461
 
        total_packs = len(self._names)
1462
 
        if self._max_pack_count(total_revisions) >= total_packs:
1463
 
            return False
1464
 
        # determine which packs need changing
1465
 
        pack_distribution = self.pack_distribution(total_revisions)
1466
 
        existing_packs = []
1467
 
        for pack in self.all_packs():
1468
 
            revision_count = pack.get_revision_count()
1469
 
            if revision_count == 0:
1470
 
                # revision less packs are not generated by normal operation,
1471
 
                # only by operations like sign-my-commits, and thus will not
1472
 
                # tend to grow rapdily or without bound like commit containing
1473
 
                # packs do - leave them alone as packing them really should
1474
 
                # group their data with the relevant commit, and that may
1475
 
                # involve rewriting ancient history - which autopack tries to
1476
 
                # avoid. Alternatively we could not group the data but treat
1477
 
                # each of these as having a single revision, and thus add
1478
 
                # one revision for each to the total revision count, to get
1479
 
                # a matching distribution.
1480
 
                continue
1481
 
            existing_packs.append((revision_count, pack))
1482
 
        pack_operations = self.plan_autopack_combinations(
1483
 
            existing_packs, pack_distribution)
1484
 
        num_new_packs = len(pack_operations)
1485
 
        num_old_packs = sum([len(po[1]) for po in pack_operations])
1486
 
        num_revs_affected = sum([po[0] for po in pack_operations])
1487
 
        mutter('Auto-packing repository %s, which has %d pack files, '
1488
 
            'containing %d revisions. Packing %d files into %d affecting %d'
1489
 
            ' revisions', self, total_packs, total_revisions, num_old_packs,
1490
 
            num_new_packs, num_revs_affected)
1491
 
        self._execute_pack_operations(pack_operations,
1492
 
                                      reload_func=self._restart_autopack)
1493
 
        mutter('Auto-packing repository %s completed', self)
1494
 
        return True
1495
 
 
1496
 
    def _execute_pack_operations(self, pack_operations, _packer_class=Packer,
1497
 
                                 reload_func=None):
1498
 
        """Execute a series of pack operations.
1499
 
 
1500
 
        :param pack_operations: A list of [revision_count, packs_to_combine].
1501
 
        :param _packer_class: The class of packer to use (default: Packer).
1502
 
        :return: None.
1503
 
        """
1504
 
        for revision_count, packs in pack_operations:
1505
 
            # we may have no-ops from the setup logic
1506
 
            if len(packs) == 0:
1507
 
                continue
1508
 
            packer = _packer_class(self, packs, '.autopack',
1509
 
                                   reload_func=reload_func)
1510
 
            try:
1511
 
                packer.pack()
1512
 
            except errors.RetryWithNewPacks:
1513
 
                # An exception is propagating out of this context, make sure
1514
 
                # this packer has cleaned up. Packer() doesn't set its new_pack
1515
 
                # state into the RepositoryPackCollection object, so we only
1516
 
                # have access to it directly here.
1517
 
                if packer.new_pack is not None:
1518
 
                    packer.new_pack.abort()
1519
 
                raise
1520
 
            for pack in packs:
1521
 
                self._remove_pack_from_memory(pack)
1522
 
        # record the newly available packs and stop advertising the old
1523
 
        # packs
1524
 
        self._save_pack_names(clear_obsolete_packs=True)
1525
 
        # Move the old packs out of the way now they are no longer referenced.
1526
 
        for revision_count, packs in pack_operations:
1527
 
            self._obsolete_packs(packs)
1528
 
 
1529
 
    def _flush_new_pack(self):
1530
 
        if self._new_pack is not None:
1531
 
            self._new_pack.flush()
1532
 
 
1533
 
    def lock_names(self):
1534
 
        """Acquire the mutex around the pack-names index.
1535
 
 
1536
 
        This cannot be used in the middle of a read-only transaction on the
1537
 
        repository.
1538
 
        """
1539
 
        self.repo.control_files.lock_write()
1540
 
 
1541
 
    def _already_packed(self):
1542
 
        """Is the collection already packed?"""
1543
 
        return len(self._names) < 2
1544
 
 
1545
 
    def pack(self):
1546
 
        """Pack the pack collection totally."""
1547
 
        self.ensure_loaded()
1548
 
        total_packs = len(self._names)
1549
 
        if self._already_packed():
1550
 
            # This is arguably wrong because we might not be optimal, but for
1551
 
            # now lets leave it in. (e.g. reconcile -> one pack. But not
1552
 
            # optimal.
1553
 
            return
1554
 
        total_revisions = self.revision_index.combined_index.key_count()
1555
 
        # XXX: the following may want to be a class, to pack with a given
1556
 
        # policy.
1557
 
        mutter('Packing repository %s, which has %d pack files, '
1558
 
            'containing %d revisions into 1 packs.', self, total_packs,
1559
 
            total_revisions)
1560
 
        # determine which packs need changing
1561
 
        pack_distribution = [1]
1562
 
        pack_operations = [[0, []]]
1563
 
        for pack in self.all_packs():
1564
 
            pack_operations[-1][0] += pack.get_revision_count()
1565
 
            pack_operations[-1][1].append(pack)
1566
 
        self._execute_pack_operations(pack_operations, OptimisingPacker)
1567
 
 
1568
 
    def plan_autopack_combinations(self, existing_packs, pack_distribution):
1569
 
        """Plan a pack operation.
1570
 
 
1571
 
        :param existing_packs: The packs to pack. (A list of (revcount, Pack)
1572
 
            tuples).
1573
 
        :param pack_distribution: A list with the number of revisions desired
1574
 
            in each pack.
1575
 
        """
1576
 
        if len(existing_packs) <= len(pack_distribution):
1577
 
            return []
1578
 
        existing_packs.sort(reverse=True)
1579
 
        pack_operations = [[0, []]]
1580
 
        # plan out what packs to keep, and what to reorganise
1581
 
        while len(existing_packs):
1582
 
            # take the largest pack, and if its less than the head of the
1583
 
            # distribution chart we will include its contents in the new pack
1584
 
            # for that position. If its larger, we remove its size from the
1585
 
            # distribution chart
1586
 
            next_pack_rev_count, next_pack = existing_packs.pop(0)
1587
 
            if next_pack_rev_count >= pack_distribution[0]:
1588
 
                # this is already packed 'better' than this, so we can
1589
 
                # not waste time packing it.
1590
 
                while next_pack_rev_count > 0:
1591
 
                    next_pack_rev_count -= pack_distribution[0]
1592
 
                    if next_pack_rev_count >= 0:
1593
 
                        # more to go
1594
 
                        del pack_distribution[0]
1595
 
                    else:
1596
 
                        # didn't use that entire bucket up
1597
 
                        pack_distribution[0] = -next_pack_rev_count
1598
 
            else:
1599
 
                # add the revisions we're going to add to the next output pack
1600
 
                pack_operations[-1][0] += next_pack_rev_count
1601
 
                # allocate this pack to the next pack sub operation
1602
 
                pack_operations[-1][1].append(next_pack)
1603
 
                if pack_operations[-1][0] >= pack_distribution[0]:
1604
 
                    # this pack is used up, shift left.
1605
 
                    del pack_distribution[0]
1606
 
                    pack_operations.append([0, []])
1607
 
        # Now that we know which pack files we want to move, shove them all
1608
 
        # into a single pack file.
1609
 
        final_rev_count = 0
1610
 
        final_pack_list = []
1611
 
        for num_revs, pack_files in pack_operations:
1612
 
            final_rev_count += num_revs
1613
 
            final_pack_list.extend(pack_files)
1614
 
        if len(final_pack_list) == 1:
1615
 
            raise AssertionError('We somehow generated an autopack with a'
1616
 
                ' single pack file being moved.')
1617
 
            return []
1618
 
        return [[final_rev_count, final_pack_list]]
1619
 
 
1620
 
    def ensure_loaded(self):
1621
 
        """Ensure we have read names from disk.
1622
 
 
1623
 
        :return: True if the disk names had not been previously read.
1624
 
        """
1625
 
        # NB: if you see an assertion error here, its probably access against
1626
 
        # an unlocked repo. Naughty.
1627
 
        if not self.repo.is_locked():
1628
 
            raise errors.ObjectNotLocked(self.repo)
1629
 
        if self._names is None:
1630
 
            self._names = {}
1631
 
            self._packs_at_load = set()
1632
 
            for index, key, value in self._iter_disk_pack_index():
1633
 
                name = key[0]
1634
 
                self._names[name] = self._parse_index_sizes(value)
1635
 
                self._packs_at_load.add((key, value))
1636
 
            result = True
1637
 
        else:
1638
 
            result = False
1639
 
        # populate all the metadata.
1640
 
        self.all_packs()
1641
 
        return result
1642
 
 
1643
 
    def _parse_index_sizes(self, value):
1644
 
        """Parse a string of index sizes."""
1645
 
        return tuple([int(digits) for digits in value.split(' ')])
1646
 
 
1647
 
    def get_pack_by_name(self, name):
1648
 
        """Get a Pack object by name.
1649
 
 
1650
 
        :param name: The name of the pack - e.g. '123456'
1651
 
        :return: A Pack object.
1652
 
        """
1653
 
        try:
1654
 
            return self._packs_by_name[name]
1655
 
        except KeyError:
1656
 
            rev_index = self._make_index(name, '.rix')
1657
 
            inv_index = self._make_index(name, '.iix')
1658
 
            txt_index = self._make_index(name, '.tix')
1659
 
            sig_index = self._make_index(name, '.six')
1660
 
            if self.chk_index is not None:
1661
 
                chk_index = self._make_index(name, '.cix')
1662
 
            else:
1663
 
                chk_index = None
1664
 
            result = ExistingPack(self._pack_transport, name, rev_index,
1665
 
                inv_index, txt_index, sig_index, chk_index)
1666
 
            self.add_pack_to_memory(result)
1667
 
            return result
1668
 
 
1669
 
    def _resume_pack(self, name):
1670
 
        """Get a suspended Pack object by name.
1671
 
 
1672
 
        :param name: The name of the pack - e.g. '123456'
1673
 
        :return: A Pack object.
1674
 
        """
1675
 
        if not re.match('[a-f0-9]{32}', name):
1676
 
            # Tokens should be md5sums of the suspended pack file, i.e. 32 hex
1677
 
            # digits.
1678
 
            raise errors.UnresumableWriteGroup(
1679
 
                self.repo, [name], 'Malformed write group token')
1680
 
        try:
1681
 
            rev_index = self._make_index(name, '.rix', resume=True)
1682
 
            inv_index = self._make_index(name, '.iix', resume=True)
1683
 
            txt_index = self._make_index(name, '.tix', resume=True)
1684
 
            sig_index = self._make_index(name, '.six', resume=True)
1685
 
            result = ResumedPack(name, rev_index, inv_index, txt_index,
1686
 
                sig_index, self._upload_transport, self._pack_transport,
1687
 
                self._index_transport, self)
1688
 
        except errors.NoSuchFile, e:
1689
 
            raise errors.UnresumableWriteGroup(self.repo, [name], str(e))
1690
 
        self.add_pack_to_memory(result)
1691
 
        self._resumed_packs.append(result)
1692
 
        return result
1693
 
 
1694
 
    def allocate(self, a_new_pack):
1695
 
        """Allocate name in the list of packs.
1696
 
 
1697
 
        :param a_new_pack: A NewPack instance to be added to the collection of
1698
 
            packs for this repository.
1699
 
        """
1700
 
        self.ensure_loaded()
1701
 
        if a_new_pack.name in self._names:
1702
 
            raise errors.BzrError(
1703
 
                'Pack %r already exists in %s' % (a_new_pack.name, self))
1704
 
        self._names[a_new_pack.name] = tuple(a_new_pack.index_sizes)
1705
 
        self.add_pack_to_memory(a_new_pack)
1706
 
 
1707
 
    def _iter_disk_pack_index(self):
1708
 
        """Iterate over the contents of the pack-names index.
1709
 
 
1710
 
        This is used when loading the list from disk, and before writing to
1711
 
        detect updates from others during our write operation.
1712
 
        :return: An iterator of the index contents.
1713
 
        """
1714
 
        return self._index_class(self.transport, 'pack-names', None
1715
 
                ).iter_all_entries()
1716
 
 
1717
 
    def _make_index(self, name, suffix, resume=False):
1718
 
        size_offset = self._suffix_offsets[suffix]
1719
 
        index_name = name + suffix
1720
 
        if resume:
1721
 
            transport = self._upload_transport
1722
 
            index_size = transport.stat(index_name).st_size
1723
 
        else:
1724
 
            transport = self._index_transport
1725
 
            index_size = self._names[name][size_offset]
1726
 
        return self._index_class(transport, index_name, index_size)
1727
 
 
1728
 
    def _max_pack_count(self, total_revisions):
1729
 
        """Return the maximum number of packs to use for total revisions.
1730
 
 
1731
 
        :param total_revisions: The total number of revisions in the
1732
 
            repository.
1733
 
        """
1734
 
        if not total_revisions:
1735
 
            return 1
1736
 
        digits = str(total_revisions)
1737
 
        result = 0
1738
 
        for digit in digits:
1739
 
            result += int(digit)
1740
 
        return result
1741
 
 
1742
 
    def names(self):
1743
 
        """Provide an order to the underlying names."""
1744
 
        return sorted(self._names.keys())
1745
 
 
1746
 
    def _obsolete_packs(self, packs):
1747
 
        """Move a number of packs which have been obsoleted out of the way.
1748
 
 
1749
 
        Each pack and its associated indices are moved out of the way.
1750
 
 
1751
 
        Note: for correctness this function should only be called after a new
1752
 
        pack names index has been written without these pack names, and with
1753
 
        the names of packs that contain the data previously available via these
1754
 
        packs.
1755
 
 
1756
 
        :param packs: The packs to obsolete.
1757
 
        :param return: None.
1758
 
        """
1759
 
        for pack in packs:
1760
 
            pack.pack_transport.rename(pack.file_name(),
1761
 
                '../obsolete_packs/' + pack.file_name())
1762
 
            # TODO: Probably needs to know all possible indices for this pack
1763
 
            # - or maybe list the directory and move all indices matching this
1764
 
            # name whether we recognize it or not?
1765
 
            suffixes = ['.iix', '.six', '.tix', '.rix']
1766
 
            if self.chk_index is not None:
1767
 
                suffixes.append('.cix')
1768
 
            for suffix in suffixes:
1769
 
                self._index_transport.rename(pack.name + suffix,
1770
 
                    '../obsolete_packs/' + pack.name + suffix)
1771
 
 
1772
 
    def pack_distribution(self, total_revisions):
1773
 
        """Generate a list of the number of revisions to put in each pack.
1774
 
 
1775
 
        :param total_revisions: The total number of revisions in the
1776
 
            repository.
1777
 
        """
1778
 
        if total_revisions == 0:
1779
 
            return [0]
1780
 
        digits = reversed(str(total_revisions))
1781
 
        result = []
1782
 
        for exponent, count in enumerate(digits):
1783
 
            size = 10 ** exponent
1784
 
            for pos in range(int(count)):
1785
 
                result.append(size)
1786
 
        return list(reversed(result))
1787
 
 
1788
 
    def _pack_tuple(self, name):
1789
 
        """Return a tuple with the transport and file name for a pack name."""
1790
 
        return self._pack_transport, name + '.pack'
1791
 
 
1792
 
    def _remove_pack_from_memory(self, pack):
1793
 
        """Remove pack from the packs accessed by this repository.
1794
 
 
1795
 
        Only affects memory state, until self._save_pack_names() is invoked.
1796
 
        """
1797
 
        self._names.pop(pack.name)
1798
 
        self._packs_by_name.pop(pack.name)
1799
 
        self._remove_pack_indices(pack)
1800
 
        self.packs.remove(pack)
1801
 
 
1802
 
    def _remove_pack_indices(self, pack):
1803
 
        """Remove the indices for pack from the aggregated indices."""
1804
 
        self.revision_index.remove_index(pack.revision_index, pack)
1805
 
        self.inventory_index.remove_index(pack.inventory_index, pack)
1806
 
        self.text_index.remove_index(pack.text_index, pack)
1807
 
        self.signature_index.remove_index(pack.signature_index, pack)
1808
 
        if self.chk_index is not None:
1809
 
            self.chk_index.remove_index(pack.chk_index, pack)
1810
 
 
1811
 
    def reset(self):
1812
 
        """Clear all cached data."""
1813
 
        # cached revision data
1814
 
        self.repo._revision_knit = None
1815
 
        self.revision_index.clear()
1816
 
        # cached signature data
1817
 
        self.repo._signature_knit = None
1818
 
        self.signature_index.clear()
1819
 
        # cached file text data
1820
 
        self.text_index.clear()
1821
 
        self.repo._text_knit = None
1822
 
        # cached inventory data
1823
 
        self.inventory_index.clear()
1824
 
        # cached chk data
1825
 
        if self.chk_index is not None:
1826
 
            self.chk_index.clear()
1827
 
        # remove the open pack
1828
 
        self._new_pack = None
1829
 
        # information about packs.
1830
 
        self._names = None
1831
 
        self.packs = []
1832
 
        self._packs_by_name = {}
1833
 
        self._packs_at_load = None
1834
 
 
1835
 
    def _unlock_names(self):
1836
 
        """Release the mutex around the pack-names index."""
1837
 
        self.repo.control_files.unlock()
1838
 
 
1839
 
    def _diff_pack_names(self):
1840
 
        """Read the pack names from disk, and compare it to the one in memory.
1841
 
 
1842
 
        :return: (disk_nodes, deleted_nodes, new_nodes)
1843
 
            disk_nodes    The final set of nodes that should be referenced
1844
 
            deleted_nodes Nodes which have been removed from when we started
1845
 
            new_nodes     Nodes that are newly introduced
1846
 
        """
1847
 
        # load the disk nodes across
1848
 
        disk_nodes = set()
1849
 
        for index, key, value in self._iter_disk_pack_index():
1850
 
            disk_nodes.add((key, value))
1851
 
 
1852
 
        # do a two-way diff against our original content
1853
 
        current_nodes = set()
1854
 
        for name, sizes in self._names.iteritems():
1855
 
            current_nodes.add(
1856
 
                ((name, ), ' '.join(str(size) for size in sizes)))
1857
 
 
1858
 
        # Packs no longer present in the repository, which were present when we
1859
 
        # locked the repository
1860
 
        deleted_nodes = self._packs_at_load - current_nodes
1861
 
        # Packs which this process is adding
1862
 
        new_nodes = current_nodes - self._packs_at_load
1863
 
 
1864
 
        # Update the disk_nodes set to include the ones we are adding, and
1865
 
        # remove the ones which were removed by someone else
1866
 
        disk_nodes.difference_update(deleted_nodes)
1867
 
        disk_nodes.update(new_nodes)
1868
 
 
1869
 
        return disk_nodes, deleted_nodes, new_nodes
1870
 
 
1871
 
    def _syncronize_pack_names_from_disk_nodes(self, disk_nodes):
1872
 
        """Given the correct set of pack files, update our saved info.
1873
 
 
1874
 
        :return: (removed, added, modified)
1875
 
            removed     pack names removed from self._names
1876
 
            added       pack names added to self._names
1877
 
            modified    pack names that had changed value
1878
 
        """
1879
 
        removed = []
1880
 
        added = []
1881
 
        modified = []
1882
 
        ## self._packs_at_load = disk_nodes
1883
 
        new_names = dict(disk_nodes)
1884
 
        # drop no longer present nodes
1885
 
        for pack in self.all_packs():
1886
 
            if (pack.name,) not in new_names:
1887
 
                removed.append(pack.name)
1888
 
                self._remove_pack_from_memory(pack)
1889
 
        # add new nodes/refresh existing ones
1890
 
        for key, value in disk_nodes:
1891
 
            name = key[0]
1892
 
            sizes = self._parse_index_sizes(value)
1893
 
            if name in self._names:
1894
 
                # existing
1895
 
                if sizes != self._names[name]:
1896
 
                    # the pack for name has had its indices replaced - rare but
1897
 
                    # important to handle. XXX: probably can never happen today
1898
 
                    # because the three-way merge code above does not handle it
1899
 
                    # - you may end up adding the same key twice to the new
1900
 
                    # disk index because the set values are the same, unless
1901
 
                    # the only index shows up as deleted by the set difference
1902
 
                    # - which it may. Until there is a specific test for this,
1903
 
                    # assume its broken. RBC 20071017.
1904
 
                    self._remove_pack_from_memory(self.get_pack_by_name(name))
1905
 
                    self._names[name] = sizes
1906
 
                    self.get_pack_by_name(name)
1907
 
                    modified.append(name)
1908
 
            else:
1909
 
                # new
1910
 
                self._names[name] = sizes
1911
 
                self.get_pack_by_name(name)
1912
 
                added.append(name)
1913
 
        return removed, added, modified
1914
 
 
1915
 
    def _save_pack_names(self, clear_obsolete_packs=False):
1916
 
        """Save the list of packs.
1917
 
 
1918
 
        This will take out the mutex around the pack names list for the
1919
 
        duration of the method call. If concurrent updates have been made, a
1920
 
        three-way merge between the current list and the current in memory list
1921
 
        is performed.
1922
 
 
1923
 
        :param clear_obsolete_packs: If True, clear out the contents of the
1924
 
            obsolete_packs directory.
1925
 
        """
1926
 
        self.lock_names()
1927
 
        try:
1928
 
            builder = self._index_builder_class()
1929
 
            disk_nodes, deleted_nodes, new_nodes = self._diff_pack_names()
1930
 
            # TODO: handle same-name, index-size-changes here -
1931
 
            # e.g. use the value from disk, not ours, *unless* we're the one
1932
 
            # changing it.
1933
 
            for key, value in disk_nodes:
1934
 
                builder.add_node(key, value)
1935
 
            self.transport.put_file('pack-names', builder.finish(),
1936
 
                mode=self.repo.bzrdir._get_file_mode())
1937
 
            # move the baseline forward
1938
 
            self._packs_at_load = disk_nodes
1939
 
            if clear_obsolete_packs:
1940
 
                self._clear_obsolete_packs()
1941
 
        finally:
1942
 
            self._unlock_names()
1943
 
        # synchronise the memory packs list with what we just wrote:
1944
 
        self._syncronize_pack_names_from_disk_nodes(disk_nodes)
1945
 
 
1946
 
    def reload_pack_names(self):
1947
 
        """Sync our pack listing with what is present in the repository.
1948
 
 
1949
 
        This should be called when we find out that something we thought was
1950
 
        present is now missing. This happens when another process re-packs the
1951
 
        repository, etc.
1952
 
 
1953
 
        :return: True if the in-memory list of packs has been altered at all.
1954
 
        """
1955
 
        # The ensure_loaded call is to handle the case where the first call
1956
 
        # made involving the collection was to reload_pack_names, where we 
1957
 
        # don't have a view of disk contents. Its a bit of a bandaid, and
1958
 
        # causes two reads of pack-names, but its a rare corner case not struck
1959
 
        # with regular push/pull etc.
1960
 
        first_read = self.ensure_loaded()
1961
 
        if first_read:
1962
 
            return True
1963
 
        # out the new value.
1964
 
        disk_nodes, _, _ = self._diff_pack_names()
1965
 
        self._packs_at_load = disk_nodes
1966
 
        (removed, added,
1967
 
         modified) = self._syncronize_pack_names_from_disk_nodes(disk_nodes)
1968
 
        if removed or added or modified:
1969
 
            return True
1970
 
        return False
1971
 
 
1972
 
    def _restart_autopack(self):
1973
 
        """Reload the pack names list, and restart the autopack code."""
1974
 
        if not self.reload_pack_names():
1975
 
            # Re-raise the original exception, because something went missing
1976
 
            # and a restart didn't find it
1977
 
            raise
1978
 
        raise errors.RetryAutopack(self.repo, False, sys.exc_info())
1979
 
 
1980
 
    def _clear_obsolete_packs(self):
1981
 
        """Delete everything from the obsolete-packs directory.
1982
 
        """
1983
 
        obsolete_pack_transport = self.transport.clone('obsolete_packs')
1984
 
        for filename in obsolete_pack_transport.list_dir('.'):
1985
 
            try:
1986
 
                obsolete_pack_transport.delete(filename)
1987
 
            except (errors.PathError, errors.TransportError), e:
1988
 
                warning("couldn't delete obsolete pack, skipping it:\n%s" % (e,))
1989
 
 
1990
 
    def _start_write_group(self):
1991
 
        # Do not permit preparation for writing if we're not in a 'write lock'.
1992
 
        if not self.repo.is_write_locked():
1993
 
            raise errors.NotWriteLocked(self)
1994
 
        self._new_pack = self.pack_factory(self, upload_suffix='.pack',
1995
 
            file_mode=self.repo.bzrdir._get_file_mode())
1996
 
        # allow writing: queue writes to a new index
1997
 
        self.revision_index.add_writable_index(self._new_pack.revision_index,
1998
 
            self._new_pack)
1999
 
        self.inventory_index.add_writable_index(self._new_pack.inventory_index,
2000
 
            self._new_pack)
2001
 
        self.text_index.add_writable_index(self._new_pack.text_index,
2002
 
            self._new_pack)
2003
 
        self.signature_index.add_writable_index(self._new_pack.signature_index,
2004
 
            self._new_pack)
2005
 
        if self.chk_index is not None:
2006
 
            self.chk_index.add_writable_index(self._new_pack.chk_index,
2007
 
                self._new_pack)
2008
 
            self.repo.chk_bytes._index._add_callback = self.chk_index.add_callback
2009
 
 
2010
 
        self.repo.inventories._index._add_callback = self.inventory_index.add_callback
2011
 
        self.repo.revisions._index._add_callback = self.revision_index.add_callback
2012
 
        self.repo.signatures._index._add_callback = self.signature_index.add_callback
2013
 
        self.repo.texts._index._add_callback = self.text_index.add_callback
2014
 
 
2015
 
    def _abort_write_group(self):
2016
 
        # FIXME: just drop the transient index.
2017
 
        # forget what names there are
2018
 
        if self._new_pack is not None:
2019
 
            try:
2020
 
                self._new_pack.abort()
2021
 
            finally:
2022
 
                # XXX: If we aborted while in the middle of finishing the write
2023
 
                # group, _remove_pack_indices can fail because the indexes are
2024
 
                # already gone.  If they're not there we shouldn't fail in this
2025
 
                # case.  -- mbp 20081113
2026
 
                self._remove_pack_indices(self._new_pack)
2027
 
                self._new_pack = None
2028
 
        for resumed_pack in self._resumed_packs:
2029
 
            try:
2030
 
                resumed_pack.abort()
2031
 
            finally:
2032
 
                # See comment in previous finally block.
2033
 
                try:
2034
 
                    self._remove_pack_indices(resumed_pack)
2035
 
                except KeyError:
2036
 
                    pass
2037
 
        del self._resumed_packs[:]
2038
 
        self.repo._text_knit = None
2039
 
 
2040
 
    def _remove_resumed_pack_indices(self):
2041
 
        for resumed_pack in self._resumed_packs:
2042
 
            self._remove_pack_indices(resumed_pack)
2043
 
        del self._resumed_packs[:]
2044
 
 
2045
 
    def _commit_write_group(self):
2046
 
        all_missing = set()
2047
 
        for prefix, versioned_file in (
2048
 
                ('revisions', self.repo.revisions),
2049
 
                ('inventories', self.repo.inventories),
2050
 
                ('texts', self.repo.texts),
2051
 
                ('signatures', self.repo.signatures),
2052
 
                ):
2053
 
            missing = versioned_file.get_missing_compression_parent_keys()
2054
 
            all_missing.update([(prefix,) + key for key in missing])
2055
 
        if all_missing:
2056
 
            raise errors.BzrCheckError(
2057
 
                "Repository %s has missing compression parent(s) %r "
2058
 
                 % (self.repo, sorted(all_missing)))
2059
 
        self._remove_pack_indices(self._new_pack)
2060
 
        should_autopack = False
2061
 
        if self._new_pack.data_inserted():
2062
 
            # get all the data to disk and read to use
2063
 
            self._new_pack.finish()
2064
 
            self.allocate(self._new_pack)
2065
 
            self._new_pack = None
2066
 
            should_autopack = True
2067
 
        else:
2068
 
            self._new_pack.abort()
2069
 
            self._new_pack = None
2070
 
        for resumed_pack in self._resumed_packs:
2071
 
            # XXX: this is a pretty ugly way to turn the resumed pack into a
2072
 
            # properly committed pack.
2073
 
            self._names[resumed_pack.name] = None
2074
 
            self._remove_pack_from_memory(resumed_pack)
2075
 
            resumed_pack.finish()
2076
 
            self.allocate(resumed_pack)
2077
 
            should_autopack = True
2078
 
        del self._resumed_packs[:]
2079
 
        if should_autopack:
2080
 
            if not self.autopack():
2081
 
                # when autopack takes no steps, the names list is still
2082
 
                # unsaved.
2083
 
                self._save_pack_names()
2084
 
        self.repo._text_knit = None
2085
 
 
2086
 
    def _suspend_write_group(self):
2087
 
        tokens = [pack.name for pack in self._resumed_packs]
2088
 
        self._remove_pack_indices(self._new_pack)
2089
 
        if self._new_pack.data_inserted():
2090
 
            # get all the data to disk and read to use
2091
 
            self._new_pack.finish(suspend=True)
2092
 
            tokens.append(self._new_pack.name)
2093
 
            self._new_pack = None
2094
 
        else:
2095
 
            self._new_pack.abort()
2096
 
            self._new_pack = None
2097
 
        self._remove_resumed_pack_indices()
2098
 
        self.repo._text_knit = None
2099
 
        return tokens
2100
 
 
2101
 
    def _resume_write_group(self, tokens):
2102
 
        for token in tokens:
2103
 
            self._resume_pack(token)
2104
 
 
2105
 
 
2106
 
class KnitPackRepository(KnitRepository):
2107
 
    """Repository with knit objects stored inside pack containers.
2108
 
 
2109
 
    The layering for a KnitPackRepository is:
2110
 
 
2111
 
    Graph        |  HPSS    | Repository public layer |
2112
 
    ===================================================
2113
 
    Tuple based apis below, string based, and key based apis above
2114
 
    ---------------------------------------------------
2115
 
    KnitVersionedFiles
2116
 
      Provides .texts, .revisions etc
2117
 
      This adapts the N-tuple keys to physical knit records which only have a
2118
 
      single string identifier (for historical reasons), which in older formats
2119
 
      was always the revision_id, and in the mapped code for packs is always
2120
 
      the last element of key tuples.
2121
 
    ---------------------------------------------------
2122
 
    GraphIndex
2123
 
      A separate GraphIndex is used for each of the
2124
 
      texts/inventories/revisions/signatures contained within each individual
2125
 
      pack file. The GraphIndex layer works in N-tuples and is unaware of any
2126
 
      semantic value.
2127
 
    ===================================================
2128
 
 
2129
 
    """
2130
 
 
2131
 
    def __init__(self, _format, a_bzrdir, control_files, _commit_builder_class,
2132
 
        _serializer):
2133
 
        KnitRepository.__init__(self, _format, a_bzrdir, control_files,
2134
 
            _commit_builder_class, _serializer)
2135
 
        index_transport = self._transport.clone('indices')
2136
 
        self._pack_collection = RepositoryPackCollection(self, self._transport,
2137
 
            index_transport,
2138
 
            self._transport.clone('upload'),
2139
 
            self._transport.clone('packs'),
2140
 
            _format.index_builder_class,
2141
 
            _format.index_class,
2142
 
            use_chk_index=self._format.supports_chks,
2143
 
            )
2144
 
        self.inventories = KnitVersionedFiles(
2145
 
            _KnitGraphIndex(self._pack_collection.inventory_index.combined_index,
2146
 
                add_callback=self._pack_collection.inventory_index.add_callback,
2147
 
                deltas=True, parents=True, is_locked=self.is_locked),
2148
 
            data_access=self._pack_collection.inventory_index.data_access,
2149
 
            max_delta_chain=200)
2150
 
        self.revisions = KnitVersionedFiles(
2151
 
            _KnitGraphIndex(self._pack_collection.revision_index.combined_index,
2152
 
                add_callback=self._pack_collection.revision_index.add_callback,
2153
 
                deltas=False, parents=True, is_locked=self.is_locked),
2154
 
            data_access=self._pack_collection.revision_index.data_access,
2155
 
            max_delta_chain=0)
2156
 
        self.signatures = KnitVersionedFiles(
2157
 
            _KnitGraphIndex(self._pack_collection.signature_index.combined_index,
2158
 
                add_callback=self._pack_collection.signature_index.add_callback,
2159
 
                deltas=False, parents=False, is_locked=self.is_locked),
2160
 
            data_access=self._pack_collection.signature_index.data_access,
2161
 
            max_delta_chain=0)
2162
 
        self.texts = KnitVersionedFiles(
2163
 
            _KnitGraphIndex(self._pack_collection.text_index.combined_index,
2164
 
                add_callback=self._pack_collection.text_index.add_callback,
2165
 
                deltas=True, parents=True, is_locked=self.is_locked),
2166
 
            data_access=self._pack_collection.text_index.data_access,
2167
 
            max_delta_chain=200)
2168
 
        if _format.supports_chks:
2169
 
            # No graph, no compression:- references from chks are between
2170
 
            # different objects not temporal versions of the same; and without
2171
 
            # some sort of temporal structure knit compression will just fail.
2172
 
            self.chk_bytes = KnitVersionedFiles(
2173
 
                _KnitGraphIndex(self._pack_collection.chk_index.combined_index,
2174
 
                    add_callback=self._pack_collection.chk_index.add_callback,
2175
 
                    deltas=False, parents=False, is_locked=self.is_locked),
2176
 
                data_access=self._pack_collection.chk_index.data_access,
2177
 
                max_delta_chain=0)
2178
 
        else:
2179
 
            self.chk_bytes = None
2180
 
        # True when the repository object is 'write locked' (as opposed to the
2181
 
        # physical lock only taken out around changes to the pack-names list.)
2182
 
        # Another way to represent this would be a decorator around the control
2183
 
        # files object that presents logical locks as physical ones - if this
2184
 
        # gets ugly consider that alternative design. RBC 20071011
2185
 
        self._write_lock_count = 0
2186
 
        self._transaction = None
2187
 
        # for tests
2188
 
        self._reconcile_does_inventory_gc = True
2189
 
        self._reconcile_fixes_text_parents = True
2190
 
        self._reconcile_backsup_inventory = False
2191
 
 
2192
 
    def _warn_if_deprecated(self):
2193
 
        # This class isn't deprecated, but one sub-format is
2194
 
        if isinstance(self._format, RepositoryFormatKnitPack5RichRootBroken):
2195
 
            from bzrlib import repository
2196
 
            if repository._deprecation_warning_done:
2197
 
                return
2198
 
            repository._deprecation_warning_done = True
2199
 
            warning("Format %s for %s is deprecated - please use"
2200
 
                    " 'bzr upgrade --1.6.1-rich-root'"
2201
 
                    % (self._format, self.bzrdir.transport.base))
2202
 
 
2203
 
    def _abort_write_group(self):
2204
 
        self._pack_collection._abort_write_group()
2205
 
 
2206
 
    def _find_inconsistent_revision_parents(self):
2207
 
        """Find revisions with incorrectly cached parents.
2208
 
 
2209
 
        :returns: an iterator yielding tuples of (revison-id, parents-in-index,
2210
 
            parents-in-revision).
2211
 
        """
2212
 
        if not self.is_locked():
2213
 
            raise errors.ObjectNotLocked(self)
2214
 
        pb = ui.ui_factory.nested_progress_bar()
2215
 
        result = []
2216
 
        try:
2217
 
            revision_nodes = self._pack_collection.revision_index \
2218
 
                .combined_index.iter_all_entries()
2219
 
            index_positions = []
2220
 
            # Get the cached index values for all revisions, and also the
2221
 
            # location in each index of the revision text so we can perform
2222
 
            # linear IO.
2223
 
            for index, key, value, refs in revision_nodes:
2224
 
                node = (index, key, value, refs)
2225
 
                index_memo = self.revisions._index._node_to_position(node)
2226
 
                assert index_memo[0] == index
2227
 
                index_positions.append((index_memo, key[0],
2228
 
                                       tuple(parent[0] for parent in refs[0])))
2229
 
                pb.update("Reading revision index", 0, 0)
2230
 
            index_positions.sort()
2231
 
            batch_size = 1000
2232
 
            pb.update("Checking cached revision graph", 0,
2233
 
                      len(index_positions))
2234
 
            for offset in xrange(0, len(index_positions), 1000):
2235
 
                pb.update("Checking cached revision graph", offset)
2236
 
                to_query = index_positions[offset:offset + batch_size]
2237
 
                if not to_query:
2238
 
                    break
2239
 
                rev_ids = [item[1] for item in to_query]
2240
 
                revs = self.get_revisions(rev_ids)
2241
 
                for revision, item in zip(revs, to_query):
2242
 
                    index_parents = item[2]
2243
 
                    rev_parents = tuple(revision.parent_ids)
2244
 
                    if index_parents != rev_parents:
2245
 
                        result.append((revision.revision_id, index_parents,
2246
 
                                       rev_parents))
2247
 
        finally:
2248
 
            pb.finished()
2249
 
        return result
2250
 
 
2251
 
    def _make_parents_provider(self):
2252
 
        return graph.CachingParentsProvider(self)
2253
 
 
2254
 
    def _refresh_data(self):
2255
 
        if not self.is_locked():
2256
 
            return
2257
 
        self._pack_collection.reload_pack_names()
2258
 
 
2259
 
    def _start_write_group(self):
2260
 
        self._pack_collection._start_write_group()
2261
 
 
2262
 
    def _commit_write_group(self):
2263
 
        return self._pack_collection._commit_write_group()
2264
 
 
2265
 
    def suspend_write_group(self):
2266
 
        # XXX check self._write_group is self.get_transaction()?
2267
 
        tokens = self._pack_collection._suspend_write_group()
2268
 
        self._write_group = None
2269
 
        return tokens
2270
 
 
2271
 
    def _resume_write_group(self, tokens):
2272
 
        self._start_write_group()
2273
 
        self._pack_collection._resume_write_group(tokens)
2274
 
 
2275
 
    def get_transaction(self):
2276
 
        if self._write_lock_count:
2277
 
            return self._transaction
2278
 
        else:
2279
 
            return self.control_files.get_transaction()
2280
 
 
2281
 
    def is_locked(self):
2282
 
        return self._write_lock_count or self.control_files.is_locked()
2283
 
 
2284
 
    def is_write_locked(self):
2285
 
        return self._write_lock_count
2286
 
 
2287
 
    def lock_write(self, token=None):
2288
 
        locked = self.is_locked()
2289
 
        if not self._write_lock_count and locked:
2290
 
            raise errors.ReadOnlyError(self)
2291
 
        self._write_lock_count += 1
2292
 
        if self._write_lock_count == 1:
2293
 
            self._transaction = transactions.WriteTransaction()
2294
 
            for repo in self._fallback_repositories:
2295
 
                # Writes don't affect fallback repos
2296
 
                repo.lock_read()
2297
 
        if not locked:
2298
 
            self._refresh_data()
2299
 
 
2300
 
    def lock_read(self):
2301
 
        locked = self.is_locked()
2302
 
        if self._write_lock_count:
2303
 
            self._write_lock_count += 1
2304
 
        else:
2305
 
            self.control_files.lock_read()
2306
 
            for repo in self._fallback_repositories:
2307
 
                # Writes don't affect fallback repos
2308
 
                repo.lock_read()
2309
 
        if not locked:
2310
 
            self._refresh_data()
2311
 
 
2312
 
    def leave_lock_in_place(self):
2313
 
        # not supported - raise an error
2314
 
        raise NotImplementedError(self.leave_lock_in_place)
2315
 
 
2316
 
    def dont_leave_lock_in_place(self):
2317
 
        # not supported - raise an error
2318
 
        raise NotImplementedError(self.dont_leave_lock_in_place)
2319
 
 
2320
 
    @needs_write_lock
2321
 
    def pack(self):
2322
 
        """Compress the data within the repository.
2323
 
 
2324
 
        This will pack all the data to a single pack. In future it may
2325
 
        recompress deltas or do other such expensive operations.
2326
 
        """
2327
 
        self._pack_collection.pack()
2328
 
 
2329
 
    @needs_write_lock
2330
 
    def reconcile(self, other=None, thorough=False):
2331
 
        """Reconcile this repository."""
2332
 
        from bzrlib.reconcile import PackReconciler
2333
 
        reconciler = PackReconciler(self, thorough=thorough)
2334
 
        reconciler.reconcile()
2335
 
        return reconciler
2336
 
 
2337
 
    def _reconcile_pack(self, collection, packs, extension, revs, pb):
2338
 
        packer = ReconcilePacker(collection, packs, extension, revs)
2339
 
        return packer.pack(pb)
2340
 
 
2341
 
    def unlock(self):
2342
 
        if self._write_lock_count == 1 and self._write_group is not None:
2343
 
            self.abort_write_group()
2344
 
            self._transaction = None
2345
 
            self._write_lock_count = 0
2346
 
            raise errors.BzrError(
2347
 
                'Must end write group before releasing write lock on %s'
2348
 
                % self)
2349
 
        if self._write_lock_count:
2350
 
            self._write_lock_count -= 1
2351
 
            if not self._write_lock_count:
2352
 
                transaction = self._transaction
2353
 
                self._transaction = None
2354
 
                transaction.finish()
2355
 
                for repo in self._fallback_repositories:
2356
 
                    repo.unlock()
2357
 
        else:
2358
 
            self.control_files.unlock()
2359
 
            for repo in self._fallback_repositories:
2360
 
                repo.unlock()
2361
 
 
2362
 
 
2363
 
class CHKInventoryRepository(KnitPackRepository):
2364
 
    """subclass of KnitPackRepository that uses CHK based inventories."""
2365
 
 
2366
 
    def _add_inventory_checked(self, revision_id, inv, parents):
2367
 
        """Add inv to the repository after checking the inputs.
2368
 
 
2369
 
        This function can be overridden to allow different inventory styles.
2370
 
 
2371
 
        :seealso: add_inventory, for the contract.
2372
 
        """
2373
 
        # make inventory
2374
 
        serializer = self._format._serializer
2375
 
        result = CHKInventory.from_inventory(self.chk_bytes, inv,
2376
 
            maximum_size=serializer.maximum_size,
2377
 
            search_key_name=serializer.search_key_name)
2378
 
        inv_lines = result.to_lines()
2379
 
        return self._inventory_add_lines(revision_id, parents,
2380
 
            inv_lines, check_content=False)
2381
 
 
2382
 
    def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
2383
 
                               parents, basis_inv=None, propagate_caches=False):
2384
 
        """Add a new inventory expressed as a delta against another revision.
2385
 
 
2386
 
        :param basis_revision_id: The inventory id the delta was created
2387
 
            against.
2388
 
        :param delta: The inventory delta (see Inventory.apply_delta for
2389
 
            details).
2390
 
        :param new_revision_id: The revision id that the inventory is being
2391
 
            added for.
2392
 
        :param parents: The revision ids of the parents that revision_id is
2393
 
            known to have and are in the repository already. These are supplied
2394
 
            for repositories that depend on the inventory graph for revision
2395
 
            graph access, as well as for those that pun ancestry with delta
2396
 
            compression.
2397
 
        :param basis_inv: The basis inventory if it is already known,
2398
 
            otherwise None.
2399
 
        :param propagate_caches: If True, the caches for this inventory are
2400
 
          copied to and updated for the result if possible.
2401
 
 
2402
 
        :returns: (validator, new_inv)
2403
 
            The validator(which is a sha1 digest, though what is sha'd is
2404
 
            repository format specific) of the serialized inventory, and the
2405
 
            resulting inventory.
2406
 
        """
2407
 
        if basis_revision_id == _mod_revision.NULL_REVISION:
2408
 
            return KnitPackRepository.add_inventory_by_delta(self,
2409
 
                basis_revision_id, delta, new_revision_id, parents)
2410
 
        if not self.is_in_write_group():
2411
 
            raise AssertionError("%r not in write group" % (self,))
2412
 
        _mod_revision.check_not_reserved_id(new_revision_id)
2413
 
        basis_tree = self.revision_tree(basis_revision_id)
2414
 
        basis_tree.lock_read()
2415
 
        try:
2416
 
            if basis_inv is None:
2417
 
                basis_inv = basis_tree.inventory
2418
 
            result = basis_inv.create_by_apply_delta(delta, new_revision_id,
2419
 
                propagate_caches=propagate_caches)
2420
 
            inv_lines = result.to_lines()
2421
 
            return self._inventory_add_lines(new_revision_id, parents,
2422
 
                inv_lines, check_content=False), result
2423
 
        finally:
2424
 
            basis_tree.unlock()
2425
 
 
2426
 
    def _iter_inventories(self, revision_ids):
2427
 
        """Iterate over many inventory objects."""
2428
 
        keys = [(revision_id,) for revision_id in revision_ids]
2429
 
        stream = self.inventories.get_record_stream(keys, 'unordered', True)
2430
 
        texts = {}
2431
 
        for record in stream:
2432
 
            if record.storage_kind != 'absent':
2433
 
                texts[record.key] = record.get_bytes_as('fulltext')
2434
 
            else:
2435
 
                raise errors.NoSuchRevision(self, record.key)
2436
 
        for key in keys:
2437
 
            yield CHKInventory.deserialise(self.chk_bytes, texts[key], key)
2438
 
 
2439
 
    def _iter_inventory_xmls(self, revision_ids):
2440
 
        # Without a native 'xml' inventory, this method doesn't make sense, so
2441
 
        # make it raise to trap naughty direct users.
2442
 
        raise NotImplementedError(self._iter_inventory_xmls)
2443
 
 
2444
 
    def _find_revision_outside_set(self, revision_ids):
2445
 
        revision_set = frozenset(revision_ids)
2446
 
        for revid in revision_ids:
2447
 
            parent_ids = self.get_parent_map([revid]).get(revid, ())
2448
 
            for parent in parent_ids:
2449
 
                if parent in revision_set:
2450
 
                    # Parent is not outside the set
2451
 
                    continue
2452
 
                if parent not in self.get_parent_map([parent]):
2453
 
                    # Parent is a ghost
2454
 
                    continue
2455
 
                return parent
2456
 
        return _mod_revision.NULL_REVISION
2457
 
 
2458
 
    def _find_file_keys_to_fetch(self, revision_ids, pb):
2459
 
        rich_root = self.supports_rich_root()
2460
 
        revision_outside_set = self._find_revision_outside_set(revision_ids)
2461
 
        if revision_outside_set == _mod_revision.NULL_REVISION:
2462
 
            uninteresting_root_keys = set()
2463
 
        else:
2464
 
            uninteresting_inv = self.get_inventory(revision_outside_set)
2465
 
            uninteresting_root_keys = set([uninteresting_inv.id_to_entry.key()])
2466
 
        interesting_root_keys = set()
2467
 
        for idx, inv in enumerate(self.iter_inventories(revision_ids)):
2468
 
            interesting_root_keys.add(inv.id_to_entry.key())
2469
 
        revision_ids = frozenset(revision_ids)
2470
 
        file_id_revisions = {}
2471
 
        bytes_to_info = CHKInventory._bytes_to_utf8name_key
2472
 
        for records, items in chk_map.iter_interesting_nodes(self.chk_bytes,
2473
 
                    interesting_root_keys, uninteresting_root_keys,
2474
 
                    pb=pb):
2475
 
            # This is cheating a bit to use the last grabbed 'inv', but it
2476
 
            # works
2477
 
            for name, bytes in items:
2478
 
                (name_utf8, file_id, revision_id) = bytes_to_info(bytes)
2479
 
                if not rich_root and name_utf8 == '':
2480
 
                    continue
2481
 
                if revision_id in revision_ids:
2482
 
                    # Would we rather build this up into file_id => revision
2483
 
                    # maps?
2484
 
                    try:
2485
 
                        file_id_revisions[file_id].add(revision_id)
2486
 
                    except KeyError:
2487
 
                        file_id_revisions[file_id] = set([revision_id])
2488
 
        for file_id, revisions in file_id_revisions.iteritems():
2489
 
            yield ('file', file_id, revisions)
2490
 
 
2491
 
    def fileids_altered_by_revision_ids(self, revision_ids, _inv_weave=None):
2492
 
        """Find the file ids and versions affected by revisions.
2493
 
 
2494
 
        :param revisions: an iterable containing revision ids.
2495
 
        :param _inv_weave: The inventory weave from this repository or None.
2496
 
            If None, the inventory weave will be opened automatically.
2497
 
        :return: a dictionary mapping altered file-ids to an iterable of
2498
 
            revision_ids. Each altered file-ids has the exact revision_ids that
2499
 
            altered it listed explicitly.
2500
 
        """
2501
 
        rich_roots = self.supports_rich_root()
2502
 
        result = {}
2503
 
        pb = ui.ui_factory.nested_progress_bar()
2504
 
        try:
2505
 
            total = len(revision_ids)
2506
 
            for pos, inv in enumerate(self.iter_inventories(revision_ids)):
2507
 
                pb.update("Finding text references", pos, total)
2508
 
                for entry in inv.iter_just_entries():
2509
 
                    if entry.revision != inv.revision_id:
2510
 
                        continue
2511
 
                    if not rich_roots and entry.file_id == inv.root_id:
2512
 
                        continue
2513
 
                    alterations = result.setdefault(entry.file_id, set([]))
2514
 
                    alterations.add(entry.revision)
2515
 
            return result
2516
 
        finally:
2517
 
            pb.finished()
2518
 
 
2519
 
    def find_text_key_references(self):
2520
 
        """Find the text key references within the repository.
2521
 
 
2522
 
        :return: A dictionary mapping text keys ((fileid, revision_id) tuples)
2523
 
            to whether they were referred to by the inventory of the
2524
 
            revision_id that they contain. The inventory texts from all present
2525
 
            revision ids are assessed to generate this report.
2526
 
        """
2527
 
        # XXX: Slow version but correct: rewrite as a series of delta
2528
 
        # examinations/direct tree traversal. Note that that will require care
2529
 
        # as a common node is reachable both from the inventory that added it,
2530
 
        # and others afterwards.
2531
 
        revision_keys = self.revisions.keys()
2532
 
        result = {}
2533
 
        rich_roots = self.supports_rich_root()
2534
 
        pb = ui.ui_factory.nested_progress_bar()
2535
 
        try:
2536
 
            all_revs = self.all_revision_ids()
2537
 
            total = len(all_revs)
2538
 
            for pos, inv in enumerate(self.iter_inventories(all_revs)):
2539
 
                pb.update("Finding text references", pos, total)
2540
 
                for _, entry in inv.iter_entries():
2541
 
                    if not rich_roots and entry.file_id == inv.root_id:
2542
 
                        continue
2543
 
                    key = (entry.file_id, entry.revision)
2544
 
                    result.setdefault(key, False)
2545
 
                    if entry.revision == inv.revision_id:
2546
 
                        result[key] = True
2547
 
            return result
2548
 
        finally:
2549
 
            pb.finished()
2550
 
 
2551
 
    def _reconcile_pack(self, collection, packs, extension, revs, pb):
2552
 
        packer = CHKReconcilePacker(collection, packs, extension, revs)
2553
 
        return packer.pack(pb)
2554
 
 
2555
 
 
2556
 
class CHKReconcilePacker(ReconcilePacker):
2557
 
    """Subclass of ReconcilePacker for handling chk inventories."""
2558
 
 
2559
 
    def _process_inventory_lines(self, inv_lines):
2560
 
        """Generate a text key reference map rather for reconciling with."""
2561
 
        repo = self._pack_collection.repo
2562
 
        # XXX: This double-reads the inventories; but it works.
2563
 
        refs = repo.find_text_key_references()
2564
 
        self._text_refs = refs
2565
 
        # during reconcile we:
2566
 
        #  - convert unreferenced texts to full texts
2567
 
        #  - correct texts which reference a text not copied to be full texts
2568
 
        #  - copy all others as-is but with corrected parents.
2569
 
        #  - so at this point we don't know enough to decide what becomes a full
2570
 
        #    text.
2571
 
        self._text_filter = None
2572
 
        # Copy the selected inventory roots, extracting the CHK references
2573
 
        # needed.
2574
 
        pending_refs = set()
2575
 
        for line, revid in inv_lines:
2576
 
            if line.startswith('id_to_entry: '):
2577
 
                pending_refs.add((line[13:],))
2578
 
        while pending_refs:
2579
 
            pending_refs = self._copy_chks(pending_refs)
2580
 
 
2581
 
 
2582
 
class RepositoryFormatPack(MetaDirRepositoryFormat):
2583
 
    """Format logic for pack structured repositories.
2584
 
 
2585
 
    This repository format has:
2586
 
     - a list of packs in pack-names
2587
 
     - packs in packs/NAME.pack
2588
 
     - indices in indices/NAME.{iix,six,tix,rix}
2589
 
     - knit deltas in the packs, knit indices mapped to the indices.
2590
 
     - thunk objects to support the knits programming API.
2591
 
     - a format marker of its own
2592
 
     - an optional 'shared-storage' flag
2593
 
     - an optional 'no-working-trees' flag
2594
 
     - a LockDir lock
2595
 
    """
2596
 
 
2597
 
    # Set this attribute in derived classes to control the repository class
2598
 
    # created by open and initialize.
2599
 
    repository_class = None
2600
 
    # Set this attribute in derived classes to control the
2601
 
    # _commit_builder_class that the repository objects will have passed to
2602
 
    # their constructor.
2603
 
    _commit_builder_class = None
2604
 
    # Set this attribute in derived clases to control the _serializer that the
2605
 
    # repository objects will have passed to their constructor.
2606
 
    _serializer = None
2607
 
    # Packs are not confused by ghosts.
2608
 
    supports_ghosts = True
2609
 
    # External references are not supported in pack repositories yet.
2610
 
    supports_external_lookups = False
2611
 
    # Most pack formats do not use chk lookups.
2612
 
    supports_chks = False
2613
 
    # What index classes to use
2614
 
    index_builder_class = None
2615
 
    index_class = None
2616
 
    _fetch_uses_deltas = True
2617
 
    fast_deltas = False
2618
 
 
2619
 
    def initialize(self, a_bzrdir, shared=False):
2620
 
        """Create a pack based repository.
2621
 
 
2622
 
        :param a_bzrdir: bzrdir to contain the new repository; must already
2623
 
            be initialized.
2624
 
        :param shared: If true the repository will be initialized as a shared
2625
 
                       repository.
2626
 
        """
2627
 
        mutter('creating repository in %s.', a_bzrdir.transport.base)
2628
 
        dirs = ['indices', 'obsolete_packs', 'packs', 'upload']
2629
 
        builder = self.index_builder_class()
2630
 
        files = [('pack-names', builder.finish())]
2631
 
        utf8_files = [('format', self.get_format_string())]
2632
 
 
2633
 
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
2634
 
        return self.open(a_bzrdir=a_bzrdir, _found=True)
2635
 
 
2636
 
    def open(self, a_bzrdir, _found=False, _override_transport=None):
2637
 
        """See RepositoryFormat.open().
2638
 
 
2639
 
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
2640
 
                                    repository at a slightly different url
2641
 
                                    than normal. I.e. during 'upgrade'.
2642
 
        """
2643
 
        if not _found:
2644
 
            format = RepositoryFormat.find_format(a_bzrdir)
2645
 
        if _override_transport is not None:
2646
 
            repo_transport = _override_transport
2647
 
        else:
2648
 
            repo_transport = a_bzrdir.get_repository_transport(None)
2649
 
        control_files = lockable_files.LockableFiles(repo_transport,
2650
 
                                'lock', lockdir.LockDir)
2651
 
        return self.repository_class(_format=self,
2652
 
                              a_bzrdir=a_bzrdir,
2653
 
                              control_files=control_files,
2654
 
                              _commit_builder_class=self._commit_builder_class,
2655
 
                              _serializer=self._serializer)
2656
 
 
2657
 
 
2658
 
class RepositoryFormatKnitPack1(RepositoryFormatPack):
2659
 
    """A no-subtrees parameterized Pack repository.
2660
 
 
2661
 
    This format was introduced in 0.92.
2662
 
    """
2663
 
 
2664
 
    repository_class = KnitPackRepository
2665
 
    _commit_builder_class = PackCommitBuilder
2666
 
    @property
2667
 
    def _serializer(self):
2668
 
        return xml5.serializer_v5
2669
 
    # What index classes to use
2670
 
    index_builder_class = InMemoryGraphIndex
2671
 
    index_class = GraphIndex
2672
 
 
2673
 
    def _get_matching_bzrdir(self):
2674
 
        return bzrdir.format_registry.make_bzrdir('pack-0.92')
2675
 
 
2676
 
    def _ignore_setting_bzrdir(self, format):
2677
 
        pass
2678
 
 
2679
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
2680
 
 
2681
 
    def get_format_string(self):
2682
 
        """See RepositoryFormat.get_format_string()."""
2683
 
        return "Bazaar pack repository format 1 (needs bzr 0.92)\n"
2684
 
 
2685
 
    def get_format_description(self):
2686
 
        """See RepositoryFormat.get_format_description()."""
2687
 
        return "Packs containing knits without subtree support"
2688
 
 
2689
 
    def check_conversion_target(self, target_format):
2690
 
        pass
2691
 
 
2692
 
 
2693
 
class RepositoryFormatKnitPack3(RepositoryFormatPack):
2694
 
    """A subtrees parameterized Pack repository.
2695
 
 
2696
 
    This repository format uses the xml7 serializer to get:
2697
 
     - support for recording full info about the tree root
2698
 
     - support for recording tree-references
2699
 
 
2700
 
    This format was introduced in 0.92.
2701
 
    """
2702
 
 
2703
 
    repository_class = KnitPackRepository
2704
 
    _commit_builder_class = PackRootCommitBuilder
2705
 
    rich_root_data = True
2706
 
    supports_tree_reference = True
2707
 
    @property
2708
 
    def _serializer(self):
2709
 
        return xml7.serializer_v7
2710
 
    # What index classes to use
2711
 
    index_builder_class = InMemoryGraphIndex
2712
 
    index_class = GraphIndex
2713
 
 
2714
 
    def _get_matching_bzrdir(self):
2715
 
        return bzrdir.format_registry.make_bzrdir(
2716
 
            'pack-0.92-subtree')
2717
 
 
2718
 
    def _ignore_setting_bzrdir(self, format):
2719
 
        pass
2720
 
 
2721
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
2722
 
 
2723
 
    def check_conversion_target(self, target_format):
2724
 
        if not target_format.rich_root_data:
2725
 
            raise errors.BadConversionTarget(
2726
 
                'Does not support rich root data.', target_format)
2727
 
        if not getattr(target_format, 'supports_tree_reference', False):
2728
 
            raise errors.BadConversionTarget(
2729
 
                'Does not support nested trees', target_format)
2730
 
 
2731
 
    def get_format_string(self):
2732
 
        """See RepositoryFormat.get_format_string()."""
2733
 
        return "Bazaar pack repository format 1 with subtree support (needs bzr 0.92)\n"
2734
 
 
2735
 
    def get_format_description(self):
2736
 
        """See RepositoryFormat.get_format_description()."""
2737
 
        return "Packs containing knits with subtree support\n"
2738
 
 
2739
 
 
2740
 
class RepositoryFormatKnitPack4(RepositoryFormatPack):
2741
 
    """A rich-root, no subtrees parameterized Pack repository.
2742
 
 
2743
 
    This repository format uses the xml6 serializer to get:
2744
 
     - support for recording full info about the tree root
2745
 
 
2746
 
    This format was introduced in 1.0.
2747
 
    """
2748
 
 
2749
 
    repository_class = KnitPackRepository
2750
 
    _commit_builder_class = PackRootCommitBuilder
2751
 
    rich_root_data = True
2752
 
    supports_tree_reference = False
2753
 
    @property
2754
 
    def _serializer(self):
2755
 
        return xml6.serializer_v6
2756
 
    # What index classes to use
2757
 
    index_builder_class = InMemoryGraphIndex
2758
 
    index_class = GraphIndex
2759
 
 
2760
 
    def _get_matching_bzrdir(self):
2761
 
        return bzrdir.format_registry.make_bzrdir(
2762
 
            'rich-root-pack')
2763
 
 
2764
 
    def _ignore_setting_bzrdir(self, format):
2765
 
        pass
2766
 
 
2767
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
2768
 
 
2769
 
    def check_conversion_target(self, target_format):
2770
 
        if not target_format.rich_root_data:
2771
 
            raise errors.BadConversionTarget(
2772
 
                'Does not support rich root data.', target_format)
2773
 
 
2774
 
    def get_format_string(self):
2775
 
        """See RepositoryFormat.get_format_string()."""
2776
 
        return ("Bazaar pack repository format 1 with rich root"
2777
 
                " (needs bzr 1.0)\n")
2778
 
 
2779
 
    def get_format_description(self):
2780
 
        """See RepositoryFormat.get_format_description()."""
2781
 
        return "Packs containing knits with rich root support\n"
2782
 
 
2783
 
 
2784
 
class RepositoryFormatKnitPack5(RepositoryFormatPack):
2785
 
    """Repository that supports external references to allow stacking.
2786
 
 
2787
 
    New in release 1.6.
2788
 
 
2789
 
    Supports external lookups, which results in non-truncated ghosts after
2790
 
    reconcile compared to pack-0.92 formats.
2791
 
    """
2792
 
 
2793
 
    repository_class = KnitPackRepository
2794
 
    _commit_builder_class = PackCommitBuilder
2795
 
    supports_external_lookups = True
2796
 
    # What index classes to use
2797
 
    index_builder_class = InMemoryGraphIndex
2798
 
    index_class = GraphIndex
2799
 
 
2800
 
    @property
2801
 
    def _serializer(self):
2802
 
        return xml5.serializer_v5
2803
 
 
2804
 
    def _get_matching_bzrdir(self):
2805
 
        return bzrdir.format_registry.make_bzrdir('1.6')
2806
 
 
2807
 
    def _ignore_setting_bzrdir(self, format):
2808
 
        pass
2809
 
 
2810
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
2811
 
 
2812
 
    def get_format_string(self):
2813
 
        """See RepositoryFormat.get_format_string()."""
2814
 
        return "Bazaar RepositoryFormatKnitPack5 (bzr 1.6)\n"
2815
 
 
2816
 
    def get_format_description(self):
2817
 
        """See RepositoryFormat.get_format_description()."""
2818
 
        return "Packs 5 (adds stacking support, requires bzr 1.6)"
2819
 
 
2820
 
    def check_conversion_target(self, target_format):
2821
 
        pass
2822
 
 
2823
 
 
2824
 
class RepositoryFormatKnitPack5RichRoot(RepositoryFormatPack):
2825
 
    """A repository with rich roots and stacking.
2826
 
 
2827
 
    New in release 1.6.1.
2828
 
 
2829
 
    Supports stacking on other repositories, allowing data to be accessed
2830
 
    without being stored locally.
2831
 
    """
2832
 
 
2833
 
    repository_class = KnitPackRepository
2834
 
    _commit_builder_class = PackRootCommitBuilder
2835
 
    rich_root_data = True
2836
 
    supports_tree_reference = False # no subtrees
2837
 
    supports_external_lookups = True
2838
 
    # What index classes to use
2839
 
    index_builder_class = InMemoryGraphIndex
2840
 
    index_class = GraphIndex
2841
 
 
2842
 
    @property
2843
 
    def _serializer(self):
2844
 
        return xml6.serializer_v6
2845
 
 
2846
 
    def _get_matching_bzrdir(self):
2847
 
        return bzrdir.format_registry.make_bzrdir(
2848
 
            '1.6.1-rich-root')
2849
 
 
2850
 
    def _ignore_setting_bzrdir(self, format):
2851
 
        pass
2852
 
 
2853
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
2854
 
 
2855
 
    def check_conversion_target(self, target_format):
2856
 
        if not target_format.rich_root_data:
2857
 
            raise errors.BadConversionTarget(
2858
 
                'Does not support rich root data.', target_format)
2859
 
 
2860
 
    def get_format_string(self):
2861
 
        """See RepositoryFormat.get_format_string()."""
2862
 
        return "Bazaar RepositoryFormatKnitPack5RichRoot (bzr 1.6.1)\n"
2863
 
 
2864
 
    def get_format_description(self):
2865
 
        return "Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)"
2866
 
 
2867
 
 
2868
 
class RepositoryFormatKnitPack5RichRootBroken(RepositoryFormatPack):
2869
 
    """A repository with rich roots and external references.
2870
 
 
2871
 
    New in release 1.6.
2872
 
 
2873
 
    Supports external lookups, which results in non-truncated ghosts after
2874
 
    reconcile compared to pack-0.92 formats.
2875
 
 
2876
 
    This format was deprecated because the serializer it uses accidentally
2877
 
    supported subtrees, when the format was not intended to. This meant that
2878
 
    someone could accidentally fetch from an incorrect repository.
2879
 
    """
2880
 
 
2881
 
    repository_class = KnitPackRepository
2882
 
    _commit_builder_class = PackRootCommitBuilder
2883
 
    rich_root_data = True
2884
 
    supports_tree_reference = False # no subtrees
2885
 
 
2886
 
    supports_external_lookups = True
2887
 
    # What index classes to use
2888
 
    index_builder_class = InMemoryGraphIndex
2889
 
    index_class = GraphIndex
2890
 
 
2891
 
    @property
2892
 
    def _serializer(self):
2893
 
        return xml7.serializer_v7
2894
 
 
2895
 
    def _get_matching_bzrdir(self):
2896
 
        matching = bzrdir.format_registry.make_bzrdir(
2897
 
            '1.6.1-rich-root')
2898
 
        matching.repository_format = self
2899
 
        return matching
2900
 
 
2901
 
    def _ignore_setting_bzrdir(self, format):
2902
 
        pass
2903
 
 
2904
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
2905
 
 
2906
 
    def check_conversion_target(self, target_format):
2907
 
        if not target_format.rich_root_data:
2908
 
            raise errors.BadConversionTarget(
2909
 
                'Does not support rich root data.', target_format)
2910
 
 
2911
 
    def get_format_string(self):
2912
 
        """See RepositoryFormat.get_format_string()."""
2913
 
        return "Bazaar RepositoryFormatKnitPack5RichRoot (bzr 1.6)\n"
2914
 
 
2915
 
    def get_format_description(self):
2916
 
        return ("Packs 5 rich-root (adds stacking support, requires bzr 1.6)"
2917
 
                " (deprecated)")
2918
 
 
2919
 
 
2920
 
class RepositoryFormatKnitPack6(RepositoryFormatPack):
2921
 
    """A repository with stacking and btree indexes,
2922
 
    without rich roots or subtrees.
2923
 
 
2924
 
    This is equivalent to pack-1.6 with B+Tree indices.
2925
 
    """
2926
 
 
2927
 
    repository_class = KnitPackRepository
2928
 
    _commit_builder_class = PackCommitBuilder
2929
 
    supports_external_lookups = True
2930
 
    # What index classes to use
2931
 
    index_builder_class = BTreeBuilder
2932
 
    index_class = BTreeGraphIndex
2933
 
 
2934
 
    @property
2935
 
    def _serializer(self):
2936
 
        return xml5.serializer_v5
2937
 
 
2938
 
    def _get_matching_bzrdir(self):
2939
 
        return bzrdir.format_registry.make_bzrdir('1.9')
2940
 
 
2941
 
    def _ignore_setting_bzrdir(self, format):
2942
 
        pass
2943
 
 
2944
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
2945
 
 
2946
 
    def get_format_string(self):
2947
 
        """See RepositoryFormat.get_format_string()."""
2948
 
        return "Bazaar RepositoryFormatKnitPack6 (bzr 1.9)\n"
2949
 
 
2950
 
    def get_format_description(self):
2951
 
        """See RepositoryFormat.get_format_description()."""
2952
 
        return "Packs 6 (uses btree indexes, requires bzr 1.9)"
2953
 
 
2954
 
    def check_conversion_target(self, target_format):
2955
 
        pass
2956
 
 
2957
 
 
2958
 
class RepositoryFormatKnitPack6RichRoot(RepositoryFormatPack):
2959
 
    """A repository with rich roots, no subtrees, stacking and btree indexes.
2960
 
 
2961
 
    1.6-rich-root with B+Tree indices.
2962
 
    """
2963
 
 
2964
 
    repository_class = KnitPackRepository
2965
 
    _commit_builder_class = PackRootCommitBuilder
2966
 
    rich_root_data = True
2967
 
    supports_tree_reference = False # no subtrees
2968
 
    supports_external_lookups = True
2969
 
    # What index classes to use
2970
 
    index_builder_class = BTreeBuilder
2971
 
    index_class = BTreeGraphIndex
2972
 
 
2973
 
    @property
2974
 
    def _serializer(self):
2975
 
        return xml6.serializer_v6
2976
 
 
2977
 
    def _get_matching_bzrdir(self):
2978
 
        return bzrdir.format_registry.make_bzrdir(
2979
 
            '1.9-rich-root')
2980
 
 
2981
 
    def _ignore_setting_bzrdir(self, format):
2982
 
        pass
2983
 
 
2984
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
2985
 
 
2986
 
    def check_conversion_target(self, target_format):
2987
 
        if not target_format.rich_root_data:
2988
 
            raise errors.BadConversionTarget(
2989
 
                'Does not support rich root data.', target_format)
2990
 
 
2991
 
    def get_format_string(self):
2992
 
        """See RepositoryFormat.get_format_string()."""
2993
 
        return "Bazaar RepositoryFormatKnitPack6RichRoot (bzr 1.9)\n"
2994
 
 
2995
 
    def get_format_description(self):
2996
 
        return "Packs 6 rich-root (uses btree indexes, requires bzr 1.9)"
2997
 
 
2998
 
 
2999
 
class RepositoryFormatPackDevelopment2(RepositoryFormatPack):
3000
 
    """A no-subtrees development repository.
3001
 
 
3002
 
    This format should be retained until the second release after bzr 1.7.
3003
 
 
3004
 
    This is pack-1.6.1 with B+Tree indices.
3005
 
    """
3006
 
 
3007
 
    repository_class = KnitPackRepository
3008
 
    _commit_builder_class = PackCommitBuilder
3009
 
    supports_external_lookups = True
3010
 
    # What index classes to use
3011
 
    index_builder_class = BTreeBuilder
3012
 
    index_class = BTreeGraphIndex
3013
 
    # Set to true to get the fast-commit code path tested until a really fast
3014
 
    # format lands in trunk. Not actually fast in this format.
3015
 
    fast_deltas = True
3016
 
 
3017
 
    @property
3018
 
    def _serializer(self):
3019
 
        return xml5.serializer_v5
3020
 
 
3021
 
    def _get_matching_bzrdir(self):
3022
 
        return bzrdir.format_registry.make_bzrdir('development2')
3023
 
 
3024
 
    def _ignore_setting_bzrdir(self, format):
3025
 
        pass
3026
 
 
3027
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
3028
 
 
3029
 
    def get_format_string(self):
3030
 
        """See RepositoryFormat.get_format_string()."""
3031
 
        return "Bazaar development format 2 (needs bzr.dev from before 1.8)\n"
3032
 
 
3033
 
    def get_format_description(self):
3034
 
        """See RepositoryFormat.get_format_description()."""
3035
 
        return ("Development repository format, currently the same as "
3036
 
            "1.6.1 with B+Trees.\n")
3037
 
 
3038
 
    def check_conversion_target(self, target_format):
3039
 
        pass
3040
 
 
3041
 
 
3042
 
class RepositoryFormatPackDevelopment2Subtree(RepositoryFormatPack):
3043
 
    """A subtrees development repository.
3044
 
 
3045
 
    This format should be retained until the second release after bzr 1.7.
3046
 
 
3047
 
    1.6.1-subtree[as it might have been] with B+Tree indices.
3048
 
    """
3049
 
 
3050
 
    repository_class = KnitPackRepository
3051
 
    _commit_builder_class = PackRootCommitBuilder
3052
 
    rich_root_data = True
3053
 
    supports_tree_reference = True
3054
 
    supports_external_lookups = True
3055
 
    # What index classes to use
3056
 
    index_builder_class = BTreeBuilder
3057
 
    index_class = BTreeGraphIndex
3058
 
 
3059
 
    @property
3060
 
    def _serializer(self):
3061
 
        return xml7.serializer_v7
3062
 
 
3063
 
    def _get_matching_bzrdir(self):
3064
 
        return bzrdir.format_registry.make_bzrdir(
3065
 
            'development2-subtree')
3066
 
 
3067
 
    def _ignore_setting_bzrdir(self, format):
3068
 
        pass
3069
 
 
3070
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
3071
 
 
3072
 
    def check_conversion_target(self, target_format):
3073
 
        if not target_format.rich_root_data:
3074
 
            raise errors.BadConversionTarget(
3075
 
                'Does not support rich root data.', target_format)
3076
 
        if not getattr(target_format, 'supports_tree_reference', False):
3077
 
            raise errors.BadConversionTarget(
3078
 
                'Does not support nested trees', target_format)
3079
 
 
3080
 
    def get_format_string(self):
3081
 
        """See RepositoryFormat.get_format_string()."""
3082
 
        return ("Bazaar development format 2 with subtree support "
3083
 
            "(needs bzr.dev from before 1.8)\n")
3084
 
 
3085
 
    def get_format_description(self):
3086
 
        """See RepositoryFormat.get_format_description()."""
3087
 
        return ("Development repository format, currently the same as "
3088
 
            "1.6.1-subtree with B+Tree indices.\n")
3089
 
 
3090
 
 
3091
 
class RepositoryFormatPackDevelopment5(RepositoryFormatPack):
3092
 
    """A no-subtrees development repository.
3093
 
 
3094
 
    This format should be retained until the second release after bzr 1.13.
3095
 
 
3096
 
    This is pack-1.9 with CHKMap based inventories.
3097
 
    """
3098
 
 
3099
 
    repository_class = CHKInventoryRepository
3100
 
    _commit_builder_class = PackCommitBuilder
3101
 
    _serializer = chk_serializer.chk_serializer_parent_id
3102
 
    supports_external_lookups = True
3103
 
    # What index classes to use
3104
 
    index_builder_class = BTreeBuilder
3105
 
    index_class = BTreeGraphIndex
3106
 
    supports_chks = True
3107
 
    _commit_inv_deltas = True
3108
 
 
3109
 
    def _get_matching_bzrdir(self):
3110
 
        return bzrdir.format_registry.make_bzrdir('development5')
3111
 
 
3112
 
    def _ignore_setting_bzrdir(self, format):
3113
 
        pass
3114
 
 
3115
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
3116
 
 
3117
 
    def get_format_string(self):
3118
 
        """See RepositoryFormat.get_format_string()."""
3119
 
        # This will need to be updated (at least replacing 1.13 with the target
3120
 
        # bzr release) once we merge brisbane-core into bzr.dev, I've used
3121
 
        # 'merge-bbc-dev4-to-bzr.dev' into comments at relevant places to make
3122
 
        # them easily greppable.  -- vila 2009016
3123
 
        return "Bazaar development format 5 (needs bzr.dev from before 1.13)\n"
3124
 
 
3125
 
    def get_format_description(self):
3126
 
        """See RepositoryFormat.get_format_description()."""
3127
 
        return ("Development repository format, currently the same as"
3128
 
                " 1.9 with B+Trees and chk support.\n")
3129
 
 
3130
 
    def check_conversion_target(self, target_format):
3131
 
        pass
3132
 
 
3133
 
 
3134
 
class RepositoryFormatPackDevelopment5Subtree(RepositoryFormatPack):
3135
 
    # merge-bbc-dev4-to-bzr.dev
3136
 
    """A subtrees development repository.
3137
 
 
3138
 
    This format should be retained until the second release after bzr 1.13.
3139
 
 
3140
 
    1.9-subtree[as it might have been] with CHKMap based inventories.
3141
 
    """
3142
 
 
3143
 
    repository_class = CHKInventoryRepository
3144
 
    _commit_builder_class = PackRootCommitBuilder
3145
 
    rich_root_data = True
3146
 
    supports_tree_reference = True
3147
 
    _serializer = chk_serializer.chk_serializer_subtree_parent_id
3148
 
    supports_external_lookups = True
3149
 
    # What index classes to use
3150
 
    index_builder_class = BTreeBuilder
3151
 
    index_class = BTreeGraphIndex
3152
 
    supports_chks = True
3153
 
    _commit_inv_deltas = True
3154
 
 
3155
 
    def _get_matching_bzrdir(self):
3156
 
        return bzrdir.format_registry.make_bzrdir(
3157
 
            'development5-subtree')
3158
 
 
3159
 
    def _ignore_setting_bzrdir(self, format):
3160
 
        pass
3161
 
 
3162
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
3163
 
 
3164
 
    def check_conversion_target(self, target_format):
3165
 
        if not target_format.rich_root_data:
3166
 
            raise errors.BadConversionTarget(
3167
 
                'Does not support rich root data.', target_format)
3168
 
        if not getattr(target_format, 'supports_tree_reference', False):
3169
 
            raise errors.BadConversionTarget(
3170
 
                'Does not support nested trees', target_format)
3171
 
 
3172
 
    def get_format_string(self):
3173
 
        """See RepositoryFormat.get_format_string()."""
3174
 
        # merge-bbc-dev4-to-bzr.dev
3175
 
        return ("Bazaar development format 5 with subtree support"
3176
 
                " (needs bzr.dev from before 1.13)\n")
3177
 
 
3178
 
    def get_format_description(self):
3179
 
        """See RepositoryFormat.get_format_description()."""
3180
 
        return ("Development repository format, currently the same as"
3181
 
                " 1.9-subtree with B+Tree and chk support.\n")
3182
 
 
3183
 
 
3184
 
class RepositoryFormatPackDevelopment5Hash16(RepositoryFormatPack):
3185
 
    """A no-subtrees development repository.
3186
 
 
3187
 
    This format should be retained until the second release after bzr 1.13.
3188
 
 
3189
 
    This is pack-1.9 with CHKMap based inventories with 16-way hash tries.
3190
 
    """
3191
 
 
3192
 
    repository_class = CHKInventoryRepository
3193
 
    _commit_builder_class = PackCommitBuilder
3194
 
    _serializer = chk_serializer.chk_serializer_16_parent_id
3195
 
    supports_external_lookups = True
3196
 
    # What index classes to use
3197
 
    index_builder_class = BTreeBuilder
3198
 
    index_class = BTreeGraphIndex
3199
 
    supports_chks = True
3200
 
    _commit_inv_deltas = True
3201
 
 
3202
 
    def _get_matching_bzrdir(self):
3203
 
        return bzrdir.format_registry.make_bzrdir('development5-hash16')
3204
 
 
3205
 
    def _ignore_setting_bzrdir(self, format):
3206
 
        pass
3207
 
 
3208
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
3209
 
 
3210
 
    def get_format_string(self):
3211
 
        """See RepositoryFormat.get_format_string()."""
3212
 
        return ("Bazaar development format 5 hash 16"
3213
 
                " (needs bzr.dev from before 1.13)\n")
3214
 
 
3215
 
    def get_format_description(self):
3216
 
        """See RepositoryFormat.get_format_description()."""
3217
 
        return ("Development repository format, currently the same as"
3218
 
                " 1.9 with B+Trees and chk support and 16-way hash tries\n")
3219
 
 
3220
 
    def check_conversion_target(self, target_format):
3221
 
        pass
3222
 
 
3223
 
 
3224
 
class RepositoryFormatPackDevelopment5Hash255(RepositoryFormatPack):
3225
 
    """A no-subtrees development repository.
3226
 
 
3227
 
    This format should be retained until the second release after bzr 1.13.
3228
 
 
3229
 
    This is pack-1.9 with CHKMap based inventories with 255-way hash tries.
3230
 
    """
3231
 
 
3232
 
    repository_class = CHKInventoryRepository
3233
 
    _commit_builder_class = PackCommitBuilder
3234
 
    _serializer = chk_serializer.chk_serializer_255_parent_id
3235
 
    supports_external_lookups = True
3236
 
    # What index classes to use
3237
 
    index_builder_class = BTreeBuilder
3238
 
    index_class = BTreeGraphIndex
3239
 
    supports_chks = True
3240
 
    _commit_inv_deltas = True
3241
 
 
3242
 
    def _get_matching_bzrdir(self):
3243
 
        return bzrdir.format_registry.make_bzrdir('development5-hash255')
3244
 
 
3245
 
    def _ignore_setting_bzrdir(self, format):
3246
 
        pass
3247
 
 
3248
 
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
3249
 
 
3250
 
    def get_format_string(self):
3251
 
        """See RepositoryFormat.get_format_string()."""
3252
 
        return ("Bazaar development format 5 hash 255"
3253
 
                " (needs bzr.dev from before 1.13)\n")
3254
 
 
3255
 
    def get_format_description(self):
3256
 
        """See RepositoryFormat.get_format_description()."""
3257
 
        return ("Development repository format, currently the same as"
3258
 
                " 1.9 with B+Trees and chk support and 255-way hash tries\n")
3259
 
 
3260
 
    def check_conversion_target(self, target_format):
3261
 
        pass