~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: 2007-12-10 19:20:41 UTC
  • mto: This revision was merged to the branch mainline in revision 3101.
  • Revision ID: john@arbash-meinel.com-20071210192041-p9ukten4vt7vg8lz
Fix bug #175337, bzr bind shouldn't check the ancestry

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
from bzrlib.lazy_import import lazy_import
 
18
lazy_import(globals(), """
 
19
from itertools import izip
 
20
import math
 
21
import md5
 
22
import time
 
23
 
 
24
from bzrlib import (
 
25
        debug,
 
26
        pack,
 
27
        ui,
 
28
        )
 
29
from bzrlib.graph import Graph
 
30
from bzrlib.index import (
 
31
    GraphIndex,
 
32
    GraphIndexBuilder,
 
33
    InMemoryGraphIndex,
 
34
    CombinedGraphIndex,
 
35
    GraphIndexPrefixAdapter,
 
36
    )
 
37
from bzrlib.knit import KnitGraphIndex, _PackAccess, _KnitData
 
38
from bzrlib.osutils import rand_chars
 
39
from bzrlib.pack import ContainerWriter
 
40
from bzrlib.store import revision
 
41
from bzrlib import tsort
 
42
""")
 
43
from bzrlib import (
 
44
    bzrdir,
 
45
    deprecated_graph,
 
46
    errors,
 
47
    knit,
 
48
    lockable_files,
 
49
    lockdir,
 
50
    osutils,
 
51
    transactions,
 
52
    xml5,
 
53
    xml6,
 
54
    xml7,
 
55
    )
 
56
 
 
57
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
58
from bzrlib.repofmt.knitrepo import KnitRepository
 
59
from bzrlib.repository import (
 
60
    CommitBuilder,
 
61
    MetaDirRepository,
 
62
    MetaDirRepositoryFormat,
 
63
    RootCommitBuilder,
 
64
    )
 
65
import bzrlib.revision as _mod_revision
 
66
from bzrlib.store.revision.knit import KnitRevisionStore
 
67
from bzrlib.store.versioned import VersionedFileStore
 
68
from bzrlib.trace import mutter, note, warning
 
69
 
 
70
 
 
71
class PackCommitBuilder(CommitBuilder):
 
72
    """A subclass of CommitBuilder to add texts with pack semantics.
 
73
    
 
74
    Specifically this uses one knit object rather than one knit object per
 
75
    added text, reducing memory and object pressure.
 
76
    """
 
77
 
 
78
    def __init__(self, repository, parents, config, timestamp=None,
 
79
                 timezone=None, committer=None, revprops=None,
 
80
                 revision_id=None):
 
81
        CommitBuilder.__init__(self, repository, parents, config,
 
82
            timestamp=timestamp, timezone=timezone, committer=committer,
 
83
            revprops=revprops, revision_id=revision_id)
 
84
        self._file_graph = Graph(
 
85
            repository._pack_collection.text_index.combined_index)
 
86
 
 
87
    def _add_text_to_weave(self, file_id, new_lines, parents, nostore_sha):
 
88
        return self.repository._pack_collection._add_text_to_weave(file_id,
 
89
            self._new_revision_id, new_lines, parents, nostore_sha,
 
90
            self.random_revid)
 
91
 
 
92
    def _heads(self, file_id, revision_ids):
 
93
        keys = [(file_id, revision_id) for revision_id in revision_ids]
 
94
        return set([key[1] for key in self._file_graph.heads(keys)])
 
95
 
 
96
 
 
97
class PackRootCommitBuilder(RootCommitBuilder):
 
98
    """A subclass of RootCommitBuilder to add texts with pack semantics.
 
99
    
 
100
    Specifically this uses one knit object rather than one knit object per
 
101
    added text, reducing memory and object pressure.
 
102
    """
 
103
 
 
104
    def __init__(self, repository, parents, config, timestamp=None,
 
105
                 timezone=None, committer=None, revprops=None,
 
106
                 revision_id=None):
 
107
        CommitBuilder.__init__(self, repository, parents, config,
 
108
            timestamp=timestamp, timezone=timezone, committer=committer,
 
109
            revprops=revprops, revision_id=revision_id)
 
110
        self._file_graph = Graph(
 
111
            repository._pack_collection.text_index.combined_index)
 
112
 
 
113
    def _add_text_to_weave(self, file_id, new_lines, parents, nostore_sha):
 
114
        return self.repository._pack_collection._add_text_to_weave(file_id,
 
115
            self._new_revision_id, new_lines, parents, nostore_sha,
 
116
            self.random_revid)
 
117
 
 
118
    def _heads(self, file_id, revision_ids):
 
119
        keys = [(file_id, revision_id) for revision_id in revision_ids]
 
120
        return set([key[1] for key in self._file_graph.heads(keys)])
 
121
 
 
122
 
 
123
class Pack(object):
 
124
    """An in memory proxy for a pack and its indices.
 
125
 
 
126
    This is a base class that is not directly used, instead the classes
 
127
    ExistingPack and NewPack are used.
 
128
    """
 
129
 
 
130
    def __init__(self, revision_index, inventory_index, text_index,
 
131
        signature_index):
 
132
        """Create a pack instance.
 
133
 
 
134
        :param revision_index: A GraphIndex for determining what revisions are
 
135
            present in the Pack and accessing the locations of their texts.
 
136
        :param inventory_index: A GraphIndex for determining what inventories are
 
137
            present in the Pack and accessing the locations of their
 
138
            texts/deltas.
 
139
        :param text_index: A GraphIndex for determining what file texts
 
140
            are present in the pack and accessing the locations of their
 
141
            texts/deltas (via (fileid, revisionid) tuples).
 
142
        :param revision_index: A GraphIndex for determining what signatures are
 
143
            present in the Pack and accessing the locations of their texts.
 
144
        """
 
145
        self.revision_index = revision_index
 
146
        self.inventory_index = inventory_index
 
147
        self.text_index = text_index
 
148
        self.signature_index = signature_index
 
149
 
 
150
    def access_tuple(self):
 
151
        """Return a tuple (transport, name) for the pack content."""
 
152
        return self.pack_transport, self.file_name()
 
153
 
 
154
    def file_name(self):
 
155
        """Get the file name for the pack on disk."""
 
156
        return self.name + '.pack'
 
157
 
 
158
    def get_revision_count(self):
 
159
        return self.revision_index.key_count()
 
160
 
 
161
    def inventory_index_name(self, name):
 
162
        """The inv index is the name + .iix."""
 
163
        return self.index_name('inventory', name)
 
164
 
 
165
    def revision_index_name(self, name):
 
166
        """The revision index is the name + .rix."""
 
167
        return self.index_name('revision', name)
 
168
 
 
169
    def signature_index_name(self, name):
 
170
        """The signature index is the name + .six."""
 
171
        return self.index_name('signature', name)
 
172
 
 
173
    def text_index_name(self, name):
 
174
        """The text index is the name + .tix."""
 
175
        return self.index_name('text', name)
 
176
 
 
177
    def _external_compression_parents_of_texts(self):
 
178
        keys = set()
 
179
        refs = set()
 
180
        for node in self.text_index.iter_all_entries():
 
181
            keys.add(node[1])
 
182
            refs.update(node[3][1])
 
183
        return refs - keys
 
184
 
 
185
 
 
186
class ExistingPack(Pack):
 
187
    """An in memory proxy for an existing .pack and its disk indices."""
 
188
 
 
189
    def __init__(self, pack_transport, name, revision_index, inventory_index,
 
190
        text_index, signature_index):
 
191
        """Create an ExistingPack object.
 
192
 
 
193
        :param pack_transport: The transport where the pack file resides.
 
194
        :param name: The name of the pack on disk in the pack_transport.
 
195
        """
 
196
        Pack.__init__(self, revision_index, inventory_index, text_index,
 
197
            signature_index)
 
198
        self.name = name
 
199
        self.pack_transport = pack_transport
 
200
        assert None not in (revision_index, inventory_index, text_index,
 
201
            signature_index, name, pack_transport)
 
202
 
 
203
    def __eq__(self, other):
 
204
        return self.__dict__ == other.__dict__
 
205
 
 
206
    def __ne__(self, other):
 
207
        return not self.__eq__(other)
 
208
 
 
209
    def __repr__(self):
 
210
        return "<bzrlib.repofmt.pack_repo.Pack object at 0x%x, %s, %s" % (
 
211
            id(self), self.transport, self.name)
 
212
 
 
213
 
 
214
class NewPack(Pack):
 
215
    """An in memory proxy for a pack which is being created."""
 
216
 
 
217
    # A map of index 'type' to the file extension and position in the
 
218
    # index_sizes array.
 
219
    index_definitions = {
 
220
        'revision': ('.rix', 0),
 
221
        'inventory': ('.iix', 1),
 
222
        'text': ('.tix', 2),
 
223
        'signature': ('.six', 3),
 
224
        }
 
225
 
 
226
    def __init__(self, upload_transport, index_transport, pack_transport,
 
227
        upload_suffix='', file_mode=None):
 
228
        """Create a NewPack instance.
 
229
 
 
230
        :param upload_transport: A writable transport for the pack to be
 
231
            incrementally uploaded to.
 
232
        :param index_transport: A writable transport for the pack's indices to
 
233
            be written to when the pack is finished.
 
234
        :param pack_transport: A writable transport for the pack to be renamed
 
235
            to when the upload is complete. This *must* be the same as
 
236
            upload_transport.clone('../packs').
 
237
        :param upload_suffix: An optional suffix to be given to any temporary
 
238
            files created during the pack creation. e.g '.autopack'
 
239
        :param file_mode: An optional file mode to create the new files with.
 
240
        """
 
241
        # The relative locations of the packs are constrained, but all are
 
242
        # passed in because the caller has them, so as to avoid object churn.
 
243
        Pack.__init__(self,
 
244
            # Revisions: parents list, no text compression.
 
245
            InMemoryGraphIndex(reference_lists=1),
 
246
            # Inventory: We want to map compression only, but currently the
 
247
            # knit code hasn't been updated enough to understand that, so we
 
248
            # have a regular 2-list index giving parents and compression
 
249
            # source.
 
250
            InMemoryGraphIndex(reference_lists=2),
 
251
            # Texts: compression and per file graph, for all fileids - so two
 
252
            # reference lists and two elements in the key tuple.
 
253
            InMemoryGraphIndex(reference_lists=2, key_elements=2),
 
254
            # Signatures: Just blobs to store, no compression, no parents
 
255
            # listing.
 
256
            InMemoryGraphIndex(reference_lists=0),
 
257
            )
 
258
        # where should the new pack be opened
 
259
        self.upload_transport = upload_transport
 
260
        # where are indices written out to
 
261
        self.index_transport = index_transport
 
262
        # where is the pack renamed to when it is finished?
 
263
        self.pack_transport = pack_transport
 
264
        # What file mode to upload the pack and indices with.
 
265
        self._file_mode = file_mode
 
266
        # tracks the content written to the .pack file.
 
267
        self._hash = md5.new()
 
268
        # a four-tuple with the length in bytes of the indices, once the pack
 
269
        # is finalised. (rev, inv, text, sigs)
 
270
        self.index_sizes = None
 
271
        # How much data to cache when writing packs. Note that this is not
 
272
        # synchronised with reads, because it's not in the transport layer, so
 
273
        # is not safe unless the client knows it won't be reading from the pack
 
274
        # under creation.
 
275
        self._cache_limit = 0
 
276
        # the temporary pack file name.
 
277
        self.random_name = rand_chars(20) + upload_suffix
 
278
        # when was this pack started ?
 
279
        self.start_time = time.time()
 
280
        # open an output stream for the data added to the pack.
 
281
        self.write_stream = self.upload_transport.open_write_stream(
 
282
            self.random_name, mode=self._file_mode)
 
283
        if 'pack' in debug.debug_flags:
 
284
            mutter('%s: create_pack: pack stream open: %s%s t+%6.3fs',
 
285
                time.ctime(), self.upload_transport.base, self.random_name,
 
286
                time.time() - self.start_time)
 
287
        # A list of byte sequences to be written to the new pack, and the 
 
288
        # aggregate size of them.  Stored as a list rather than separate 
 
289
        # variables so that the _write_data closure below can update them.
 
290
        self._buffer = [[], 0]
 
291
        # create a callable for adding data 
 
292
        #
 
293
        # robertc says- this is a closure rather than a method on the object
 
294
        # so that the variables are locals, and faster than accessing object
 
295
        # members.
 
296
        def _write_data(bytes, flush=False, _buffer=self._buffer,
 
297
            _write=self.write_stream.write, _update=self._hash.update):
 
298
            _buffer[0].append(bytes)
 
299
            _buffer[1] += len(bytes)
 
300
            # buffer cap
 
301
            if _buffer[1] > self._cache_limit or flush:
 
302
                bytes = ''.join(_buffer[0])
 
303
                _write(bytes)
 
304
                _update(bytes)
 
305
                _buffer[:] = [[], 0]
 
306
        # expose this on self, for the occasion when clients want to add data.
 
307
        self._write_data = _write_data
 
308
        # a pack writer object to serialise pack records.
 
309
        self._writer = pack.ContainerWriter(self._write_data)
 
310
        self._writer.begin()
 
311
        # what state is the pack in? (open, finished, aborted)
 
312
        self._state = 'open'
 
313
 
 
314
    def abort(self):
 
315
        """Cancel creating this pack."""
 
316
        self._state = 'aborted'
 
317
        self.write_stream.close()
 
318
        # Remove the temporary pack file.
 
319
        self.upload_transport.delete(self.random_name)
 
320
        # The indices have no state on disk.
 
321
 
 
322
    def access_tuple(self):
 
323
        """Return a tuple (transport, name) for the pack content."""
 
324
        assert self._state in ('open', 'finished')
 
325
        if self._state == 'finished':
 
326
            return Pack.access_tuple(self)
 
327
        else:
 
328
            return self.upload_transport, self.random_name
 
329
 
 
330
    def data_inserted(self):
 
331
        """True if data has been added to this pack."""
 
332
        return bool(self.get_revision_count() or
 
333
            self.inventory_index.key_count() or
 
334
            self.text_index.key_count() or
 
335
            self.signature_index.key_count())
 
336
 
 
337
    def finish(self):
 
338
        """Finish the new pack.
 
339
 
 
340
        This:
 
341
         - finalises the content
 
342
         - assigns a name (the md5 of the content, currently)
 
343
         - writes out the associated indices
 
344
         - renames the pack into place.
 
345
         - stores the index size tuple for the pack in the index_sizes
 
346
           attribute.
 
347
        """
 
348
        self._writer.end()
 
349
        if self._buffer[1]:
 
350
            self._write_data('', flush=True)
 
351
        self.name = self._hash.hexdigest()
 
352
        # write indices
 
353
        # XXX: It'd be better to write them all to temporary names, then
 
354
        # rename them all into place, so that the window when only some are
 
355
        # visible is smaller.  On the other hand none will be seen until
 
356
        # they're in the names list.
 
357
        self.index_sizes = [None, None, None, None]
 
358
        self._write_index('revision', self.revision_index, 'revision')
 
359
        self._write_index('inventory', self.inventory_index, 'inventory')
 
360
        self._write_index('text', self.text_index, 'file texts')
 
361
        self._write_index('signature', self.signature_index,
 
362
            'revision signatures')
 
363
        self.write_stream.close()
 
364
        # Note that this will clobber an existing pack with the same name,
 
365
        # without checking for hash collisions. While this is undesirable this
 
366
        # is something that can be rectified in a subsequent release. One way
 
367
        # to rectify it may be to leave the pack at the original name, writing
 
368
        # its pack-names entry as something like 'HASH: index-sizes
 
369
        # temporary-name'. Allocate that and check for collisions, if it is
 
370
        # collision free then rename it into place. If clients know this scheme
 
371
        # they can handle missing-file errors by:
 
372
        #  - try for HASH.pack
 
373
        #  - try for temporary-name
 
374
        #  - refresh the pack-list to see if the pack is now absent
 
375
        self.upload_transport.rename(self.random_name,
 
376
                '../packs/' + self.name + '.pack')
 
377
        self._state = 'finished'
 
378
        if 'pack' in debug.debug_flags:
 
379
            # XXX: size might be interesting?
 
380
            mutter('%s: create_pack: pack renamed into place: %s%s->%s%s t+%6.3fs',
 
381
                time.ctime(), self.upload_transport.base, self.random_name,
 
382
                self.pack_transport, self.name,
 
383
                time.time() - self.start_time)
 
384
 
 
385
    def flush(self):
 
386
        """Flush any current data."""
 
387
        if self._buffer[1]:
 
388
            bytes = ''.join(self._buffer[0])
 
389
            self.write_stream.write(bytes)
 
390
            self._hash.update(bytes)
 
391
            self._buffer[:] = [[], 0]
 
392
 
 
393
    def index_name(self, index_type, name):
 
394
        """Get the disk name of an index type for pack name 'name'."""
 
395
        return name + NewPack.index_definitions[index_type][0]
 
396
 
 
397
    def index_offset(self, index_type):
 
398
        """Get the position in a index_size array for a given index type."""
 
399
        return NewPack.index_definitions[index_type][1]
 
400
 
 
401
    def _replace_index_with_readonly(self, index_type):
 
402
        setattr(self, index_type + '_index',
 
403
            GraphIndex(self.index_transport,
 
404
                self.index_name(index_type, self.name),
 
405
                self.index_sizes[self.index_offset(index_type)]))
 
406
 
 
407
    def set_write_cache_size(self, size):
 
408
        self._cache_limit = size
 
409
 
 
410
    def _write_index(self, index_type, index, label):
 
411
        """Write out an index.
 
412
 
 
413
        :param index_type: The type of index to write - e.g. 'revision'.
 
414
        :param index: The index object to serialise.
 
415
        :param label: What label to give the index e.g. 'revision'.
 
416
        """
 
417
        index_name = self.index_name(index_type, self.name)
 
418
        self.index_sizes[self.index_offset(index_type)] = \
 
419
            self.index_transport.put_file(index_name, index.finish(),
 
420
            mode=self._file_mode)
 
421
        if 'pack' in debug.debug_flags:
 
422
            # XXX: size might be interesting?
 
423
            mutter('%s: create_pack: wrote %s index: %s%s t+%6.3fs',
 
424
                time.ctime(), label, self.upload_transport.base,
 
425
                self.random_name, time.time() - self.start_time)
 
426
        # Replace the writable index on this object with a readonly, 
 
427
        # presently unloaded index. We should alter
 
428
        # the index layer to make its finish() error if add_node is
 
429
        # subsequently used. RBC
 
430
        self._replace_index_with_readonly(index_type)
 
431
 
 
432
 
 
433
class AggregateIndex(object):
 
434
    """An aggregated index for the RepositoryPackCollection.
 
435
 
 
436
    AggregateIndex is reponsible for managing the PackAccess object,
 
437
    Index-To-Pack mapping, and all indices list for a specific type of index
 
438
    such as 'revision index'.
 
439
 
 
440
    A CombinedIndex provides an index on a single key space built up
 
441
    from several on-disk indices.  The AggregateIndex builds on this 
 
442
    to provide a knit access layer, and allows having up to one writable
 
443
    index within the collection.
 
444
    """
 
445
    # XXX: Probably 'can be written to' could/should be separated from 'acts
 
446
    # like a knit index' -- mbp 20071024
 
447
 
 
448
    def __init__(self):
 
449
        """Create an AggregateIndex."""
 
450
        self.index_to_pack = {}
 
451
        self.combined_index = CombinedGraphIndex([])
 
452
        self.knit_access = _PackAccess(self.index_to_pack)
 
453
 
 
454
    def replace_indices(self, index_to_pack, indices):
 
455
        """Replace the current mappings with fresh ones.
 
456
 
 
457
        This should probably not be used eventually, rather incremental add and
 
458
        removal of indices. It has been added during refactoring of existing
 
459
        code.
 
460
 
 
461
        :param index_to_pack: A mapping from index objects to
 
462
            (transport, name) tuples for the pack file data.
 
463
        :param indices: A list of indices.
 
464
        """
 
465
        # refresh the revision pack map dict without replacing the instance.
 
466
        self.index_to_pack.clear()
 
467
        self.index_to_pack.update(index_to_pack)
 
468
        # XXX: API break - clearly a 'replace' method would be good?
 
469
        self.combined_index._indices[:] = indices
 
470
        # the current add nodes callback for the current writable index if
 
471
        # there is one.
 
472
        self.add_callback = None
 
473
 
 
474
    def add_index(self, index, pack):
 
475
        """Add index to the aggregate, which is an index for Pack pack.
 
476
 
 
477
        Future searches on the aggregate index will seach this new index
 
478
        before all previously inserted indices.
 
479
        
 
480
        :param index: An Index for the pack.
 
481
        :param pack: A Pack instance.
 
482
        """
 
483
        # expose it to the index map
 
484
        self.index_to_pack[index] = pack.access_tuple()
 
485
        # put it at the front of the linear index list
 
486
        self.combined_index.insert_index(0, index)
 
487
 
 
488
    def add_writable_index(self, index, pack):
 
489
        """Add an index which is able to have data added to it.
 
490
 
 
491
        There can be at most one writable index at any time.  Any
 
492
        modifications made to the knit are put into this index.
 
493
        
 
494
        :param index: An index from the pack parameter.
 
495
        :param pack: A Pack instance.
 
496
        """
 
497
        assert self.add_callback is None, \
 
498
            "%s already has a writable index through %s" % \
 
499
            (self, self.add_callback)
 
500
        # allow writing: queue writes to a new index
 
501
        self.add_index(index, pack)
 
502
        # Updates the index to packs mapping as a side effect,
 
503
        self.knit_access.set_writer(pack._writer, index, pack.access_tuple())
 
504
        self.add_callback = index.add_nodes
 
505
 
 
506
    def clear(self):
 
507
        """Reset all the aggregate data to nothing."""
 
508
        self.knit_access.set_writer(None, None, (None, None))
 
509
        self.index_to_pack.clear()
 
510
        del self.combined_index._indices[:]
 
511
        self.add_callback = None
 
512
 
 
513
    def remove_index(self, index, pack):
 
514
        """Remove index from the indices used to answer queries.
 
515
        
 
516
        :param index: An index from the pack parameter.
 
517
        :param pack: A Pack instance.
 
518
        """
 
519
        del self.index_to_pack[index]
 
520
        self.combined_index._indices.remove(index)
 
521
        if (self.add_callback is not None and
 
522
            getattr(index, 'add_nodes', None) == self.add_callback):
 
523
            self.add_callback = None
 
524
            self.knit_access.set_writer(None, None, (None, None))
 
525
 
 
526
 
 
527
class Packer(object):
 
528
    """Create a pack from packs."""
 
529
 
 
530
    def __init__(self, pack_collection, packs, suffix, revision_ids=None):
 
531
        """Create a Packer.
 
532
 
 
533
        :param pack_collection: A RepositoryPackCollection object where the
 
534
            new pack is being written to.
 
535
        :param packs: The packs to combine.
 
536
        :param suffix: The suffix to use on the temporary files for the pack.
 
537
        :param revision_ids: Revision ids to limit the pack to.
 
538
        """
 
539
        self.packs = packs
 
540
        self.suffix = suffix
 
541
        self.revision_ids = revision_ids
 
542
        # The pack object we are creating.
 
543
        self.new_pack = None
 
544
        self._pack_collection = pack_collection
 
545
        # The index layer keys for the revisions being copied. None for 'all
 
546
        # objects'.
 
547
        self._revision_keys = None
 
548
        # What text keys to copy. None for 'all texts'. This is set by
 
549
        # _copy_inventory_texts
 
550
        self._text_filter = None
 
551
        self._extra_init()
 
552
 
 
553
    def _extra_init(self):
 
554
        """A template hook to allow extending the constructor trivially."""
 
555
 
 
556
    def pack(self, pb=None):
 
557
        """Create a new pack by reading data from other packs.
 
558
 
 
559
        This does little more than a bulk copy of data. One key difference
 
560
        is that data with the same item key across multiple packs is elided
 
561
        from the output. The new pack is written into the current pack store
 
562
        along with its indices, and the name added to the pack names. The 
 
563
        source packs are not altered and are not required to be in the current
 
564
        pack collection.
 
565
 
 
566
        :param pb: An optional progress bar to use. A nested bar is created if
 
567
            this is None.
 
568
        :return: A Pack object, or None if nothing was copied.
 
569
        """
 
570
        # open a pack - using the same name as the last temporary file
 
571
        # - which has already been flushed, so its safe.
 
572
        # XXX: - duplicate code warning with start_write_group; fix before
 
573
        #      considering 'done'.
 
574
        if self._pack_collection._new_pack is not None:
 
575
            raise errors.BzrError('call to create_pack_from_packs while '
 
576
                'another pack is being written.')
 
577
        if self.revision_ids is not None:
 
578
            if len(self.revision_ids) == 0:
 
579
                # silly fetch request.
 
580
                return None
 
581
            else:
 
582
                self.revision_ids = frozenset(self.revision_ids)
 
583
        if pb is None:
 
584
            self.pb = ui.ui_factory.nested_progress_bar()
 
585
        else:
 
586
            self.pb = pb
 
587
        try:
 
588
            return self._create_pack_from_packs()
 
589
        finally:
 
590
            if pb is None:
 
591
                self.pb.finished()
 
592
 
 
593
    def open_pack(self):
 
594
        """Open a pack for the pack we are creating."""
 
595
        return NewPack(self._pack_collection._upload_transport,
 
596
            self._pack_collection._index_transport,
 
597
            self._pack_collection._pack_transport, upload_suffix=self.suffix,
 
598
            file_mode=self._pack_collection.repo.control_files._file_mode)
 
599
 
 
600
    def _copy_revision_texts(self):
 
601
        """Copy revision data to the new pack."""
 
602
        # select revisions
 
603
        if self.revision_ids:
 
604
            revision_keys = [(revision_id,) for revision_id in self.revision_ids]
 
605
        else:
 
606
            revision_keys = None
 
607
        # select revision keys
 
608
        revision_index_map = self._pack_collection._packs_list_to_pack_map_and_index_list(
 
609
            self.packs, 'revision_index')[0]
 
610
        revision_nodes = self._pack_collection._index_contents(revision_index_map, revision_keys)
 
611
        # copy revision keys and adjust values
 
612
        self.pb.update("Copying revision texts", 1)
 
613
        list(self._copy_nodes_graph(revision_nodes, revision_index_map,
 
614
            self.new_pack._writer, self.new_pack.revision_index))
 
615
        if 'pack' in debug.debug_flags:
 
616
            mutter('%s: create_pack: revisions copied: %s%s %d items t+%6.3fs',
 
617
                time.ctime(), self._pack_collection._upload_transport.base,
 
618
                self.new_pack.random_name,
 
619
                self.new_pack.revision_index.key_count(),
 
620
                time.time() - self.new_pack.start_time)
 
621
        self._revision_keys = revision_keys
 
622
 
 
623
    def _copy_inventory_texts(self):
 
624
        """Copy the inventory texts to the new pack.
 
625
 
 
626
        self._revision_keys is used to determine what inventories to copy.
 
627
 
 
628
        Sets self._text_filter appropriately.
 
629
        """
 
630
        # select inventory keys
 
631
        inv_keys = self._revision_keys # currently the same keyspace, and note that
 
632
        # querying for keys here could introduce a bug where an inventory item
 
633
        # is missed, so do not change it to query separately without cross
 
634
        # checking like the text key check below.
 
635
        inventory_index_map = self._pack_collection._packs_list_to_pack_map_and_index_list(
 
636
            self.packs, 'inventory_index')[0]
 
637
        inv_nodes = self._pack_collection._index_contents(inventory_index_map, inv_keys)
 
638
        # copy inventory keys and adjust values
 
639
        # XXX: Should be a helper function to allow different inv representation
 
640
        # at this point.
 
641
        self.pb.update("Copying inventory texts", 2)
 
642
        inv_lines = self._copy_nodes_graph(inv_nodes, inventory_index_map,
 
643
            self.new_pack._writer, self.new_pack.inventory_index, output_lines=True)
 
644
        if self.revision_ids:
 
645
            self._process_inventory_lines(inv_lines)
 
646
        else:
 
647
            # eat the iterator to cause it to execute.
 
648
            list(inv_lines)
 
649
            self._text_filter = None
 
650
        if 'pack' in debug.debug_flags:
 
651
            mutter('%s: create_pack: inventories copied: %s%s %d items t+%6.3fs',
 
652
                time.ctime(), self._pack_collection._upload_transport.base,
 
653
                self.new_pack.random_name,
 
654
                self.new_pack.inventory_index.key_count(),
 
655
                time.time() - new_pack.start_time)
 
656
 
 
657
    def _copy_text_texts(self):
 
658
        # select text keys
 
659
        text_index_map, text_nodes = self._get_text_nodes()
 
660
        if self._text_filter is not None:
 
661
            # We could return the keys copied as part of the return value from
 
662
            # _copy_nodes_graph but this doesn't work all that well with the
 
663
            # need to get line output too, so we check separately, and as we're
 
664
            # going to buffer everything anyway, we check beforehand, which
 
665
            # saves reading knit data over the wire when we know there are
 
666
            # mising records.
 
667
            text_nodes = set(text_nodes)
 
668
            present_text_keys = set(_node[1] for _node in text_nodes)
 
669
            missing_text_keys = set(self._text_filter) - present_text_keys
 
670
            if missing_text_keys:
 
671
                # TODO: raise a specific error that can handle many missing
 
672
                # keys.
 
673
                a_missing_key = missing_text_keys.pop()
 
674
                raise errors.RevisionNotPresent(a_missing_key[1],
 
675
                    a_missing_key[0])
 
676
        # copy text keys and adjust values
 
677
        self.pb.update("Copying content texts", 3)
 
678
        list(self._copy_nodes_graph(text_nodes, text_index_map,
 
679
            self.new_pack._writer, self.new_pack.text_index))
 
680
        self._log_copied_texts()
 
681
 
 
682
    def _check_references(self):
 
683
        """Make sure our external refereneces are present."""
 
684
        external_refs = self.new_pack._external_compression_parents_of_texts()
 
685
        if external_refs:
 
686
            index = self._pack_collection.text_index.combined_index
 
687
            found_items = list(index.iter_entries(external_refs))
 
688
            if len(found_items) != len(external_refs):
 
689
                found_keys = set(k for idx, k, refs, value in found_items)
 
690
                missing_items = external_refs - found_keys
 
691
                missing_file_id, missing_revision_id = missing_items.pop()
 
692
                raise errors.RevisionNotPresent(missing_revision_id,
 
693
                                                missing_file_id)
 
694
 
 
695
    def _create_pack_from_packs(self):
 
696
        self.pb.update("Opening pack", 0, 5)
 
697
        self.new_pack = self.open_pack()
 
698
        new_pack = self.new_pack
 
699
        # buffer data - we won't be reading-back during the pack creation and
 
700
        # this makes a significant difference on sftp pushes.
 
701
        new_pack.set_write_cache_size(1024*1024)
 
702
        if 'pack' in debug.debug_flags:
 
703
            plain_pack_list = ['%s%s' % (a_pack.pack_transport.base, a_pack.name)
 
704
                for a_pack in self.packs]
 
705
            if self.revision_ids is not None:
 
706
                rev_count = len(self.revision_ids)
 
707
            else:
 
708
                rev_count = 'all'
 
709
            mutter('%s: create_pack: creating pack from source packs: '
 
710
                '%s%s %s revisions wanted %s t=0',
 
711
                time.ctime(), self._pack_collection._upload_transport.base, new_pack.random_name,
 
712
                plain_pack_list, rev_count)
 
713
        self._copy_revision_texts()
 
714
        self._copy_inventory_texts()
 
715
        self._copy_text_texts()
 
716
        # select signature keys
 
717
        signature_filter = self._revision_keys # same keyspace
 
718
        signature_index_map = self._pack_collection._packs_list_to_pack_map_and_index_list(
 
719
            self.packs, 'signature_index')[0]
 
720
        signature_nodes = self._pack_collection._index_contents(signature_index_map,
 
721
            signature_filter)
 
722
        # copy signature keys and adjust values
 
723
        self.pb.update("Copying signature texts", 4)
 
724
        self._copy_nodes(signature_nodes, signature_index_map, new_pack._writer,
 
725
            new_pack.signature_index)
 
726
        if 'pack' in debug.debug_flags:
 
727
            mutter('%s: create_pack: revision signatures copied: %s%s %d items t+%6.3fs',
 
728
                time.ctime(), self._pack_collection._upload_transport.base, new_pack.random_name,
 
729
                new_pack.signature_index.key_count(),
 
730
                time.time() - new_pack.start_time)
 
731
        self._check_references()
 
732
        if not self._use_pack(new_pack):
 
733
            new_pack.abort()
 
734
            return None
 
735
        self.pb.update("Finishing pack", 5)
 
736
        new_pack.finish()
 
737
        self._pack_collection.allocate(new_pack)
 
738
        return new_pack
 
739
 
 
740
    def _copy_nodes(self, nodes, index_map, writer, write_index):
 
741
        """Copy knit nodes between packs with no graph references."""
 
742
        pb = ui.ui_factory.nested_progress_bar()
 
743
        try:
 
744
            return self._do_copy_nodes(nodes, index_map, writer,
 
745
                write_index, pb)
 
746
        finally:
 
747
            pb.finished()
 
748
 
 
749
    def _do_copy_nodes(self, nodes, index_map, writer, write_index, pb):
 
750
        # for record verification
 
751
        knit_data = _KnitData(None)
 
752
        # plan a readv on each source pack:
 
753
        # group by pack
 
754
        nodes = sorted(nodes)
 
755
        # how to map this into knit.py - or knit.py into this?
 
756
        # we don't want the typical knit logic, we want grouping by pack
 
757
        # at this point - perhaps a helper library for the following code 
 
758
        # duplication points?
 
759
        request_groups = {}
 
760
        for index, key, value in nodes:
 
761
            if index not in request_groups:
 
762
                request_groups[index] = []
 
763
            request_groups[index].append((key, value))
 
764
        record_index = 0
 
765
        pb.update("Copied record", record_index, len(nodes))
 
766
        for index, items in request_groups.iteritems():
 
767
            pack_readv_requests = []
 
768
            for key, value in items:
 
769
                # ---- KnitGraphIndex.get_position
 
770
                bits = value[1:].split(' ')
 
771
                offset, length = int(bits[0]), int(bits[1])
 
772
                pack_readv_requests.append((offset, length, (key, value[0])))
 
773
            # linear scan up the pack
 
774
            pack_readv_requests.sort()
 
775
            # copy the data
 
776
            transport, path = index_map[index]
 
777
            reader = pack.make_readv_reader(transport, path,
 
778
                [offset[0:2] for offset in pack_readv_requests])
 
779
            for (names, read_func), (_1, _2, (key, eol_flag)) in \
 
780
                izip(reader.iter_records(), pack_readv_requests):
 
781
                raw_data = read_func(None)
 
782
                # check the header only
 
783
                df, _ = knit_data._parse_record_header(key[-1], raw_data)
 
784
                df.close()
 
785
                pos, size = writer.add_bytes_record(raw_data, names)
 
786
                write_index.add_node(key, eol_flag + "%d %d" % (pos, size))
 
787
                pb.update("Copied record", record_index)
 
788
                record_index += 1
 
789
 
 
790
    def _copy_nodes_graph(self, nodes, index_map, writer, write_index,
 
791
        output_lines=False):
 
792
        """Copy knit nodes between packs.
 
793
 
 
794
        :param output_lines: Return lines present in the copied data as
 
795
            an iterator of line,version_id.
 
796
        """
 
797
        pb = ui.ui_factory.nested_progress_bar()
 
798
        try:
 
799
            for result in self._do_copy_nodes_graph(nodes, index_map, writer,
 
800
                write_index, output_lines, pb):
 
801
                yield result
 
802
        except Exception:
 
803
            # Python 2.4 does not permit try:finally: in a generator.
 
804
            pb.finished()
 
805
            raise
 
806
        else:
 
807
            pb.finished()
 
808
 
 
809
    def _do_copy_nodes_graph(self, nodes, index_map, writer, write_index,
 
810
        output_lines, pb):
 
811
        # for record verification
 
812
        knit_data = _KnitData(None)
 
813
        # for line extraction when requested (inventories only)
 
814
        if output_lines:
 
815
            factory = knit.KnitPlainFactory()
 
816
        # plan a readv on each source pack:
 
817
        # group by pack
 
818
        nodes = sorted(nodes)
 
819
        # how to map this into knit.py - or knit.py into this?
 
820
        # we don't want the typical knit logic, we want grouping by pack
 
821
        # at this point - perhaps a helper library for the following code 
 
822
        # duplication points?
 
823
        request_groups = {}
 
824
        record_index = 0
 
825
        pb.update("Copied record", record_index, len(nodes))
 
826
        for index, key, value, references in nodes:
 
827
            if index not in request_groups:
 
828
                request_groups[index] = []
 
829
            request_groups[index].append((key, value, references))
 
830
        for index, items in request_groups.iteritems():
 
831
            pack_readv_requests = []
 
832
            for key, value, references in items:
 
833
                # ---- KnitGraphIndex.get_position
 
834
                bits = value[1:].split(' ')
 
835
                offset, length = int(bits[0]), int(bits[1])
 
836
                pack_readv_requests.append((offset, length, (key, value[0], references)))
 
837
            # linear scan up the pack
 
838
            pack_readv_requests.sort()
 
839
            # copy the data
 
840
            transport, path = index_map[index]
 
841
            reader = pack.make_readv_reader(transport, path,
 
842
                [offset[0:2] for offset in pack_readv_requests])
 
843
            for (names, read_func), (_1, _2, (key, eol_flag, references)) in \
 
844
                izip(reader.iter_records(), pack_readv_requests):
 
845
                raw_data = read_func(None)
 
846
                version_id = key[-1]
 
847
                if output_lines:
 
848
                    # read the entire thing
 
849
                    content, _ = knit_data._parse_record(version_id, raw_data)
 
850
                    if len(references[-1]) == 0:
 
851
                        line_iterator = factory.get_fulltext_content(content)
 
852
                    else:
 
853
                        line_iterator = factory.get_linedelta_content(content)
 
854
                    for line in line_iterator:
 
855
                        yield line, version_id
 
856
                else:
 
857
                    # check the header only
 
858
                    df, _ = knit_data._parse_record_header(version_id, raw_data)
 
859
                    df.close()
 
860
                pos, size = writer.add_bytes_record(raw_data, names)
 
861
                write_index.add_node(key, eol_flag + "%d %d" % (pos, size), references)
 
862
                pb.update("Copied record", record_index)
 
863
                record_index += 1
 
864
 
 
865
    def _get_text_nodes(self):
 
866
        text_index_map = self._pack_collection._packs_list_to_pack_map_and_index_list(
 
867
            self.packs, 'text_index')[0]
 
868
        return text_index_map, self._pack_collection._index_contents(text_index_map,
 
869
            self._text_filter)
 
870
 
 
871
    def _log_copied_texts(self):
 
872
        if 'pack' in debug.debug_flags:
 
873
            mutter('%s: create_pack: file texts copied: %s%s %d items t+%6.3fs',
 
874
                time.ctime(), self._pack_collection._upload_transport.base,
 
875
                self.new_pack.random_name,
 
876
                self.new_pack.text_index.key_count(),
 
877
                time.time() - self.new_pack.start_time)
 
878
 
 
879
    def _process_inventory_lines(self, inv_lines):
 
880
        """Use up the inv_lines generator and setup a text key filter."""
 
881
        repo = self._pack_collection.repo
 
882
        fileid_revisions = repo._find_file_ids_from_xml_inventory_lines(
 
883
            inv_lines, self.revision_ids)
 
884
        text_filter = []
 
885
        for fileid, file_revids in fileid_revisions.iteritems():
 
886
            text_filter.extend([(fileid, file_revid) for file_revid in file_revids])
 
887
        self._text_filter = text_filter
 
888
 
 
889
    def _use_pack(self, new_pack):
 
890
        """Return True if new_pack should be used.
 
891
 
 
892
        :param new_pack: The pack that has just been created.
 
893
        :return: True if the pack should be used.
 
894
        """
 
895
        return new_pack.data_inserted()
 
896
 
 
897
 
 
898
class ReconcilePacker(Packer):
 
899
    """A packer which regenerates indices etc as it copies.
 
900
    
 
901
    This is used by ``bzr reconcile`` to cause parent text pointers to be
 
902
    regenerated.
 
903
    """
 
904
 
 
905
    def _extra_init(self):
 
906
        self._data_changed = False
 
907
 
 
908
    def _process_inventory_lines(self, inv_lines):
 
909
        """Generate a text key reference map rather for reconciling with."""
 
910
        repo = self._pack_collection.repo
 
911
        refs = repo._find_text_key_references_from_xml_inventory_lines(
 
912
            inv_lines)
 
913
        self._text_refs = refs
 
914
        # during reconcile we:
 
915
        #  - convert unreferenced texts to full texts
 
916
        #  - correct texts which reference a text not copied to be full texts
 
917
        #  - copy all others as-is but with corrected parents.
 
918
        #  - so at this point we don't know enough to decide what becomes a full
 
919
        #    text.
 
920
        self._text_filter = None
 
921
 
 
922
    def _copy_text_texts(self):
 
923
        """generate what texts we should have and then copy."""
 
924
        self.pb.update("Copying content texts", 3)
 
925
        # we have three major tasks here:
 
926
        # 1) generate the ideal index
 
927
        repo = self._pack_collection.repo
 
928
        ancestors = dict([(key[0], tuple(ref[0] for ref in refs[0])) for
 
929
            _1, key, _2, refs in 
 
930
            self.new_pack.revision_index.iter_all_entries()])
 
931
        ideal_index = repo._generate_text_key_index(self._text_refs, ancestors)
 
932
        # 2) generate a text_nodes list that contains all the deltas that can
 
933
        #    be used as-is, with corrected parents.
 
934
        ok_nodes = []
 
935
        bad_texts = []
 
936
        discarded_nodes = []
 
937
        NULL_REVISION = _mod_revision.NULL_REVISION
 
938
        text_index_map, text_nodes = self._get_text_nodes()
 
939
        for node in text_nodes:
 
940
            # 0 - index
 
941
            # 1 - key 
 
942
            # 2 - value
 
943
            # 3 - refs
 
944
            try:
 
945
                ideal_parents = tuple(ideal_index[node[1]])
 
946
            except KeyError:
 
947
                discarded_nodes.append(node)
 
948
                self._data_changed = True
 
949
            else:
 
950
                if ideal_parents == (NULL_REVISION,):
 
951
                    ideal_parents = ()
 
952
                if ideal_parents == node[3][0]:
 
953
                    # no change needed.
 
954
                    ok_nodes.append(node)
 
955
                elif ideal_parents[0:1] == node[3][0][0:1]:
 
956
                    # the left most parent is the same, or there are no parents
 
957
                    # today. Either way, we can preserve the representation as
 
958
                    # long as we change the refs to be inserted.
 
959
                    self._data_changed = True
 
960
                    ok_nodes.append((node[0], node[1], node[2],
 
961
                        (ideal_parents, node[3][1])))
 
962
                    self._data_changed = True
 
963
                else:
 
964
                    # Reinsert this text completely
 
965
                    bad_texts.append((node[1], ideal_parents))
 
966
                    self._data_changed = True
 
967
        # we're finished with some data.
 
968
        del ideal_index
 
969
        del text_nodes
 
970
        # 3) bulk copy the ok data
 
971
        list(self._copy_nodes_graph(ok_nodes, text_index_map,
 
972
            self.new_pack._writer, self.new_pack.text_index))
 
973
        # 4) adhoc copy all the other texts.
 
974
        # We have to topologically insert all texts otherwise we can fail to
 
975
        # reconcile when parts of a single delta chain are preserved intact,
 
976
        # and other parts are not. E.g. Discarded->d1->d2->d3. d1 will be
 
977
        # reinserted, and if d3 has incorrect parents it will also be
 
978
        # reinserted. If we insert d3 first, d2 is present (as it was bulk
 
979
        # copied), so we will try to delta, but d2 is not currently able to be
 
980
        # extracted because it's basis d1 is not present. Topologically sorting
 
981
        # addresses this. The following generates a sort for all the texts that
 
982
        # are being inserted without having to reference the entire text key
 
983
        # space (we only topo sort the revisions, which is smaller).
 
984
        topo_order = tsort.topo_sort(ancestors)
 
985
        rev_order = dict(zip(topo_order, range(len(topo_order))))
 
986
        bad_texts.sort(key=lambda key:rev_order[key[0][1]])
 
987
        transaction = repo.get_transaction()
 
988
        file_id_index = GraphIndexPrefixAdapter(
 
989
            self.new_pack.text_index,
 
990
            ('blank', ), 1,
 
991
            add_nodes_callback=self.new_pack.text_index.add_nodes)
 
992
        knit_index = KnitGraphIndex(file_id_index,
 
993
            add_callback=file_id_index.add_nodes,
 
994
            deltas=True, parents=True)
 
995
        output_knit = knit.KnitVersionedFile('reconcile-texts',
 
996
            self._pack_collection.transport,
 
997
            None,
 
998
            index=knit_index,
 
999
            access_method=_PackAccess(
 
1000
                {self.new_pack.text_index:self.new_pack.access_tuple()},
 
1001
                (self.new_pack._writer, self.new_pack.text_index)),
 
1002
            factory=knit.KnitPlainFactory())
 
1003
        for key, parent_keys in bad_texts:
 
1004
            # We refer to the new pack to delta data being output.
 
1005
            # A possible improvement would be to catch errors on short reads
 
1006
            # and only flush then.
 
1007
            self.new_pack.flush()
 
1008
            parents = []
 
1009
            for parent_key in parent_keys:
 
1010
                if parent_key[0] != key[0]:
 
1011
                    # Graph parents must match the fileid
 
1012
                    raise errors.BzrError('Mismatched key parent %r:%r' %
 
1013
                        (key, parent_keys))
 
1014
                parents.append(parent_key[1])
 
1015
            source_weave = repo.weave_store.get_weave(key[0], transaction)
 
1016
            text_lines = source_weave.get_lines(key[1])
 
1017
            # adapt the 'knit' to the current file_id.
 
1018
            file_id_index = GraphIndexPrefixAdapter(
 
1019
                self.new_pack.text_index,
 
1020
                (key[0], ), 1,
 
1021
                add_nodes_callback=self.new_pack.text_index.add_nodes)
 
1022
            knit_index._graph_index = file_id_index
 
1023
            knit_index._add_callback = file_id_index.add_nodes
 
1024
            output_knit.add_lines_with_ghosts(
 
1025
                key[1], parents, text_lines, random_id=True, check_content=False)
 
1026
        # 5) check that nothing inserted has a reference outside the keyspace.
 
1027
        missing_text_keys = self.new_pack._external_compression_parents_of_texts()
 
1028
        if missing_text_keys:
 
1029
            raise errors.BzrError('Reference to missing compression parents %r'
 
1030
                % (refs - keys,))
 
1031
        self._log_copied_texts()
 
1032
 
 
1033
    def _use_pack(self, new_pack):
 
1034
        """Override _use_pack to check for reconcile having changed content."""
 
1035
        # XXX: we might be better checking this at the copy time.
 
1036
        original_inventory_keys = set()
 
1037
        inv_index = self._pack_collection.inventory_index.combined_index
 
1038
        for entry in inv_index.iter_all_entries():
 
1039
            original_inventory_keys.add(entry[1])
 
1040
        new_inventory_keys = set()
 
1041
        for entry in new_pack.inventory_index.iter_all_entries():
 
1042
            new_inventory_keys.add(entry[1])
 
1043
        if new_inventory_keys != original_inventory_keys:
 
1044
            self._data_changed = True
 
1045
        return new_pack.data_inserted() and self._data_changed
 
1046
 
 
1047
 
 
1048
class RepositoryPackCollection(object):
 
1049
    """Management of packs within a repository."""
 
1050
 
 
1051
    def __init__(self, repo, transport, index_transport, upload_transport,
 
1052
                 pack_transport):
 
1053
        """Create a new RepositoryPackCollection.
 
1054
 
 
1055
        :param transport: Addresses the repository base directory 
 
1056
            (typically .bzr/repository/).
 
1057
        :param index_transport: Addresses the directory containing indices.
 
1058
        :param upload_transport: Addresses the directory into which packs are written
 
1059
            while they're being created.
 
1060
        :param pack_transport: Addresses the directory of existing complete packs.
 
1061
        """
 
1062
        self.repo = repo
 
1063
        self.transport = transport
 
1064
        self._index_transport = index_transport
 
1065
        self._upload_transport = upload_transport
 
1066
        self._pack_transport = pack_transport
 
1067
        self._suffix_offsets = {'.rix': 0, '.iix': 1, '.tix': 2, '.six': 3}
 
1068
        self.packs = []
 
1069
        # name:Pack mapping
 
1070
        self._packs_by_name = {}
 
1071
        # the previous pack-names content
 
1072
        self._packs_at_load = None
 
1073
        # when a pack is being created by this object, the state of that pack.
 
1074
        self._new_pack = None
 
1075
        # aggregated revision index data
 
1076
        self.revision_index = AggregateIndex()
 
1077
        self.inventory_index = AggregateIndex()
 
1078
        self.text_index = AggregateIndex()
 
1079
        self.signature_index = AggregateIndex()
 
1080
 
 
1081
    def add_pack_to_memory(self, pack):
 
1082
        """Make a Pack object available to the repository to satisfy queries.
 
1083
        
 
1084
        :param pack: A Pack object.
 
1085
        """
 
1086
        assert pack.name not in self._packs_by_name
 
1087
        self.packs.append(pack)
 
1088
        self._packs_by_name[pack.name] = pack
 
1089
        self.revision_index.add_index(pack.revision_index, pack)
 
1090
        self.inventory_index.add_index(pack.inventory_index, pack)
 
1091
        self.text_index.add_index(pack.text_index, pack)
 
1092
        self.signature_index.add_index(pack.signature_index, pack)
 
1093
        
 
1094
    def _add_text_to_weave(self, file_id, revision_id, new_lines, parents,
 
1095
        nostore_sha, random_revid):
 
1096
        file_id_index = GraphIndexPrefixAdapter(
 
1097
            self.text_index.combined_index,
 
1098
            (file_id, ), 1,
 
1099
            add_nodes_callback=self.text_index.add_callback)
 
1100
        self.repo._text_knit._index._graph_index = file_id_index
 
1101
        self.repo._text_knit._index._add_callback = file_id_index.add_nodes
 
1102
        return self.repo._text_knit.add_lines_with_ghosts(
 
1103
            revision_id, parents, new_lines, nostore_sha=nostore_sha,
 
1104
            random_id=random_revid, check_content=False)[0:2]
 
1105
 
 
1106
    def all_packs(self):
 
1107
        """Return a list of all the Pack objects this repository has.
 
1108
 
 
1109
        Note that an in-progress pack being created is not returned.
 
1110
 
 
1111
        :return: A list of Pack objects for all the packs in the repository.
 
1112
        """
 
1113
        result = []
 
1114
        for name in self.names():
 
1115
            result.append(self.get_pack_by_name(name))
 
1116
        return result
 
1117
 
 
1118
    def autopack(self):
 
1119
        """Pack the pack collection incrementally.
 
1120
        
 
1121
        This will not attempt global reorganisation or recompression,
 
1122
        rather it will just ensure that the total number of packs does
 
1123
        not grow without bound. It uses the _max_pack_count method to
 
1124
        determine if autopacking is needed, and the pack_distribution
 
1125
        method to determine the number of revisions in each pack.
 
1126
 
 
1127
        If autopacking takes place then the packs name collection will have
 
1128
        been flushed to disk - packing requires updating the name collection
 
1129
        in synchronisation with certain steps. Otherwise the names collection
 
1130
        is not flushed.
 
1131
 
 
1132
        :return: True if packing took place.
 
1133
        """
 
1134
        # XXX: Should not be needed when the management of indices is sane.
 
1135
        total_revisions = self.revision_index.combined_index.key_count()
 
1136
        total_packs = len(self._names)
 
1137
        if self._max_pack_count(total_revisions) >= total_packs:
 
1138
            return False
 
1139
        # XXX: the following may want to be a class, to pack with a given
 
1140
        # policy.
 
1141
        mutter('Auto-packing repository %s, which has %d pack files, '
 
1142
            'containing %d revisions into %d packs.', self, total_packs,
 
1143
            total_revisions, self._max_pack_count(total_revisions))
 
1144
        # determine which packs need changing
 
1145
        pack_distribution = self.pack_distribution(total_revisions)
 
1146
        existing_packs = []
 
1147
        for pack in self.all_packs():
 
1148
            revision_count = pack.get_revision_count()
 
1149
            if revision_count == 0:
 
1150
                # revision less packs are not generated by normal operation,
 
1151
                # only by operations like sign-my-commits, and thus will not
 
1152
                # tend to grow rapdily or without bound like commit containing
 
1153
                # packs do - leave them alone as packing them really should
 
1154
                # group their data with the relevant commit, and that may
 
1155
                # involve rewriting ancient history - which autopack tries to
 
1156
                # avoid. Alternatively we could not group the data but treat
 
1157
                # each of these as having a single revision, and thus add 
 
1158
                # one revision for each to the total revision count, to get
 
1159
                # a matching distribution.
 
1160
                continue
 
1161
            existing_packs.append((revision_count, pack))
 
1162
        pack_operations = self.plan_autopack_combinations(
 
1163
            existing_packs, pack_distribution)
 
1164
        self._execute_pack_operations(pack_operations)
 
1165
        return True
 
1166
 
 
1167
    def _execute_pack_operations(self, pack_operations):
 
1168
        """Execute a series of pack operations.
 
1169
 
 
1170
        :param pack_operations: A list of [revision_count, packs_to_combine].
 
1171
        :return: None.
 
1172
        """
 
1173
        for revision_count, packs in pack_operations:
 
1174
            # we may have no-ops from the setup logic
 
1175
            if len(packs) == 0:
 
1176
                continue
 
1177
            Packer(self, packs, '.autopack').pack()
 
1178
            for pack in packs:
 
1179
                self._remove_pack_from_memory(pack)
 
1180
        # record the newly available packs and stop advertising the old
 
1181
        # packs
 
1182
        self._save_pack_names(clear_obsolete_packs=True)
 
1183
        # Move the old packs out of the way now they are no longer referenced.
 
1184
        for revision_count, packs in pack_operations:
 
1185
            self._obsolete_packs(packs)
 
1186
 
 
1187
    def lock_names(self):
 
1188
        """Acquire the mutex around the pack-names index.
 
1189
        
 
1190
        This cannot be used in the middle of a read-only transaction on the
 
1191
        repository.
 
1192
        """
 
1193
        self.repo.control_files.lock_write()
 
1194
 
 
1195
    def pack(self):
 
1196
        """Pack the pack collection totally."""
 
1197
        self.ensure_loaded()
 
1198
        total_packs = len(self._names)
 
1199
        if total_packs < 2:
 
1200
            return
 
1201
        total_revisions = self.revision_index.combined_index.key_count()
 
1202
        # XXX: the following may want to be a class, to pack with a given
 
1203
        # policy.
 
1204
        mutter('Packing repository %s, which has %d pack files, '
 
1205
            'containing %d revisions into 1 packs.', self, total_packs,
 
1206
            total_revisions)
 
1207
        # determine which packs need changing
 
1208
        pack_distribution = [1]
 
1209
        pack_operations = [[0, []]]
 
1210
        for pack in self.all_packs():
 
1211
            revision_count = pack.get_revision_count()
 
1212
            pack_operations[-1][0] += revision_count
 
1213
            pack_operations[-1][1].append(pack)
 
1214
        self._execute_pack_operations(pack_operations)
 
1215
 
 
1216
    def plan_autopack_combinations(self, existing_packs, pack_distribution):
 
1217
        """Plan a pack operation.
 
1218
 
 
1219
        :param existing_packs: The packs to pack. (A list of (revcount, Pack)
 
1220
            tuples).
 
1221
        :param pack_distribution: A list with the number of revisions desired
 
1222
            in each pack.
 
1223
        """
 
1224
        if len(existing_packs) <= len(pack_distribution):
 
1225
            return []
 
1226
        existing_packs.sort(reverse=True)
 
1227
        pack_operations = [[0, []]]
 
1228
        # plan out what packs to keep, and what to reorganise
 
1229
        while len(existing_packs):
 
1230
            # take the largest pack, and if its less than the head of the
 
1231
            # distribution chart we will include its contents in the new pack for
 
1232
            # that position. If its larger, we remove its size from the
 
1233
            # distribution chart
 
1234
            next_pack_rev_count, next_pack = existing_packs.pop(0)
 
1235
            if next_pack_rev_count >= pack_distribution[0]:
 
1236
                # this is already packed 'better' than this, so we can
 
1237
                # not waste time packing it.
 
1238
                while next_pack_rev_count > 0:
 
1239
                    next_pack_rev_count -= pack_distribution[0]
 
1240
                    if next_pack_rev_count >= 0:
 
1241
                        # more to go
 
1242
                        del pack_distribution[0]
 
1243
                    else:
 
1244
                        # didn't use that entire bucket up
 
1245
                        pack_distribution[0] = -next_pack_rev_count
 
1246
            else:
 
1247
                # add the revisions we're going to add to the next output pack
 
1248
                pack_operations[-1][0] += next_pack_rev_count
 
1249
                # allocate this pack to the next pack sub operation
 
1250
                pack_operations[-1][1].append(next_pack)
 
1251
                if pack_operations[-1][0] >= pack_distribution[0]:
 
1252
                    # this pack is used up, shift left.
 
1253
                    del pack_distribution[0]
 
1254
                    pack_operations.append([0, []])
 
1255
        
 
1256
        return pack_operations
 
1257
 
 
1258
    def ensure_loaded(self):
 
1259
        # NB: if you see an assertion error here, its probably access against
 
1260
        # an unlocked repo. Naughty.
 
1261
        if not self.repo.is_locked():
 
1262
            raise errors.ObjectNotLocked(self.repo)
 
1263
        if self._names is None:
 
1264
            self._names = {}
 
1265
            self._packs_at_load = set()
 
1266
            for index, key, value in self._iter_disk_pack_index():
 
1267
                name = key[0]
 
1268
                self._names[name] = self._parse_index_sizes(value)
 
1269
                self._packs_at_load.add((key, value))
 
1270
        # populate all the metadata.
 
1271
        self.all_packs()
 
1272
 
 
1273
    def _parse_index_sizes(self, value):
 
1274
        """Parse a string of index sizes."""
 
1275
        return tuple([int(digits) for digits in value.split(' ')])
 
1276
 
 
1277
    def get_pack_by_name(self, name):
 
1278
        """Get a Pack object by name.
 
1279
 
 
1280
        :param name: The name of the pack - e.g. '123456'
 
1281
        :return: A Pack object.
 
1282
        """
 
1283
        try:
 
1284
            return self._packs_by_name[name]
 
1285
        except KeyError:
 
1286
            rev_index = self._make_index(name, '.rix')
 
1287
            inv_index = self._make_index(name, '.iix')
 
1288
            txt_index = self._make_index(name, '.tix')
 
1289
            sig_index = self._make_index(name, '.six')
 
1290
            result = ExistingPack(self._pack_transport, name, rev_index,
 
1291
                inv_index, txt_index, sig_index)
 
1292
            self.add_pack_to_memory(result)
 
1293
            return result
 
1294
 
 
1295
    def allocate(self, a_new_pack):
 
1296
        """Allocate name in the list of packs.
 
1297
 
 
1298
        :param a_new_pack: A NewPack instance to be added to the collection of
 
1299
            packs for this repository.
 
1300
        """
 
1301
        self.ensure_loaded()
 
1302
        if a_new_pack.name in self._names:
 
1303
            raise errors.BzrError(
 
1304
                'Pack %r already exists in %s' % (a_new_pack.name, self))
 
1305
        self._names[a_new_pack.name] = tuple(a_new_pack.index_sizes)
 
1306
        self.add_pack_to_memory(a_new_pack)
 
1307
 
 
1308
    def _iter_disk_pack_index(self):
 
1309
        """Iterate over the contents of the pack-names index.
 
1310
        
 
1311
        This is used when loading the list from disk, and before writing to
 
1312
        detect updates from others during our write operation.
 
1313
        :return: An iterator of the index contents.
 
1314
        """
 
1315
        return GraphIndex(self.transport, 'pack-names', None
 
1316
                ).iter_all_entries()
 
1317
 
 
1318
    def _make_index(self, name, suffix):
 
1319
        size_offset = self._suffix_offsets[suffix]
 
1320
        index_name = name + suffix
 
1321
        index_size = self._names[name][size_offset]
 
1322
        return GraphIndex(
 
1323
            self._index_transport, index_name, index_size)
 
1324
 
 
1325
    def _max_pack_count(self, total_revisions):
 
1326
        """Return the maximum number of packs to use for total revisions.
 
1327
        
 
1328
        :param total_revisions: The total number of revisions in the
 
1329
            repository.
 
1330
        """
 
1331
        if not total_revisions:
 
1332
            return 1
 
1333
        digits = str(total_revisions)
 
1334
        result = 0
 
1335
        for digit in digits:
 
1336
            result += int(digit)
 
1337
        return result
 
1338
 
 
1339
    def names(self):
 
1340
        """Provide an order to the underlying names."""
 
1341
        return sorted(self._names.keys())
 
1342
 
 
1343
    def _obsolete_packs(self, packs):
 
1344
        """Move a number of packs which have been obsoleted out of the way.
 
1345
 
 
1346
        Each pack and its associated indices are moved out of the way.
 
1347
 
 
1348
        Note: for correctness this function should only be called after a new
 
1349
        pack names index has been written without these pack names, and with
 
1350
        the names of packs that contain the data previously available via these
 
1351
        packs.
 
1352
 
 
1353
        :param packs: The packs to obsolete.
 
1354
        :param return: None.
 
1355
        """
 
1356
        for pack in packs:
 
1357
            pack.pack_transport.rename(pack.file_name(),
 
1358
                '../obsolete_packs/' + pack.file_name())
 
1359
            # TODO: Probably needs to know all possible indices for this pack
 
1360
            # - or maybe list the directory and move all indices matching this
 
1361
            # name whether we recognize it or not?
 
1362
            for suffix in ('.iix', '.six', '.tix', '.rix'):
 
1363
                self._index_transport.rename(pack.name + suffix,
 
1364
                    '../obsolete_packs/' + pack.name + suffix)
 
1365
 
 
1366
    def pack_distribution(self, total_revisions):
 
1367
        """Generate a list of the number of revisions to put in each pack.
 
1368
 
 
1369
        :param total_revisions: The total number of revisions in the
 
1370
            repository.
 
1371
        """
 
1372
        if total_revisions == 0:
 
1373
            return [0]
 
1374
        digits = reversed(str(total_revisions))
 
1375
        result = []
 
1376
        for exponent, count in enumerate(digits):
 
1377
            size = 10 ** exponent
 
1378
            for pos in range(int(count)):
 
1379
                result.append(size)
 
1380
        return list(reversed(result))
 
1381
 
 
1382
    def _pack_tuple(self, name):
 
1383
        """Return a tuple with the transport and file name for a pack name."""
 
1384
        return self._pack_transport, name + '.pack'
 
1385
 
 
1386
    def _remove_pack_from_memory(self, pack):
 
1387
        """Remove pack from the packs accessed by this repository.
 
1388
        
 
1389
        Only affects memory state, until self._save_pack_names() is invoked.
 
1390
        """
 
1391
        self._names.pop(pack.name)
 
1392
        self._packs_by_name.pop(pack.name)
 
1393
        self._remove_pack_indices(pack)
 
1394
 
 
1395
    def _remove_pack_indices(self, pack):
 
1396
        """Remove the indices for pack from the aggregated indices."""
 
1397
        self.revision_index.remove_index(pack.revision_index, pack)
 
1398
        self.inventory_index.remove_index(pack.inventory_index, pack)
 
1399
        self.text_index.remove_index(pack.text_index, pack)
 
1400
        self.signature_index.remove_index(pack.signature_index, pack)
 
1401
 
 
1402
    def reset(self):
 
1403
        """Clear all cached data."""
 
1404
        # cached revision data
 
1405
        self.repo._revision_knit = None
 
1406
        self.revision_index.clear()
 
1407
        # cached signature data
 
1408
        self.repo._signature_knit = None
 
1409
        self.signature_index.clear()
 
1410
        # cached file text data
 
1411
        self.text_index.clear()
 
1412
        self.repo._text_knit = None
 
1413
        # cached inventory data
 
1414
        self.inventory_index.clear()
 
1415
        # remove the open pack
 
1416
        self._new_pack = None
 
1417
        # information about packs.
 
1418
        self._names = None
 
1419
        self.packs = []
 
1420
        self._packs_by_name = {}
 
1421
        self._packs_at_load = None
 
1422
 
 
1423
    def _make_index_map(self, index_suffix):
 
1424
        """Return information on existing indices.
 
1425
 
 
1426
        :param suffix: Index suffix added to pack name.
 
1427
 
 
1428
        :returns: (pack_map, indices) where indices is a list of GraphIndex 
 
1429
        objects, and pack_map is a mapping from those objects to the 
 
1430
        pack tuple they describe.
 
1431
        """
 
1432
        # TODO: stop using this; it creates new indices unnecessarily.
 
1433
        self.ensure_loaded()
 
1434
        suffix_map = {'.rix': 'revision_index',
 
1435
            '.six': 'signature_index',
 
1436
            '.iix': 'inventory_index',
 
1437
            '.tix': 'text_index',
 
1438
        }
 
1439
        return self._packs_list_to_pack_map_and_index_list(self.all_packs(),
 
1440
            suffix_map[index_suffix])
 
1441
 
 
1442
    def _packs_list_to_pack_map_and_index_list(self, packs, index_attribute):
 
1443
        """Convert a list of packs to an index pack map and index list.
 
1444
 
 
1445
        :param packs: The packs list to process.
 
1446
        :param index_attribute: The attribute that the desired index is found
 
1447
            on.
 
1448
        :return: A tuple (map, list) where map contains the dict from
 
1449
            index:pack_tuple, and lsit contains the indices in the same order
 
1450
            as the packs list.
 
1451
        """
 
1452
        indices = []
 
1453
        pack_map = {}
 
1454
        for pack in packs:
 
1455
            index = getattr(pack, index_attribute)
 
1456
            indices.append(index)
 
1457
            pack_map[index] = (pack.pack_transport, pack.file_name())
 
1458
        return pack_map, indices
 
1459
 
 
1460
    def _index_contents(self, pack_map, key_filter=None):
 
1461
        """Get an iterable of the index contents from a pack_map.
 
1462
 
 
1463
        :param pack_map: A map from indices to pack details.
 
1464
        :param key_filter: An optional filter to limit the
 
1465
            keys returned.
 
1466
        """
 
1467
        indices = [index for index in pack_map.iterkeys()]
 
1468
        all_index = CombinedGraphIndex(indices)
 
1469
        if key_filter is None:
 
1470
            return all_index.iter_all_entries()
 
1471
        else:
 
1472
            return all_index.iter_entries(key_filter)
 
1473
 
 
1474
    def _unlock_names(self):
 
1475
        """Release the mutex around the pack-names index."""
 
1476
        self.repo.control_files.unlock()
 
1477
 
 
1478
    def _save_pack_names(self, clear_obsolete_packs=False):
 
1479
        """Save the list of packs.
 
1480
 
 
1481
        This will take out the mutex around the pack names list for the
 
1482
        duration of the method call. If concurrent updates have been made, a
 
1483
        three-way merge between the current list and the current in memory list
 
1484
        is performed.
 
1485
 
 
1486
        :param clear_obsolete_packs: If True, clear out the contents of the
 
1487
            obsolete_packs directory.
 
1488
        """
 
1489
        self.lock_names()
 
1490
        try:
 
1491
            builder = GraphIndexBuilder()
 
1492
            # load the disk nodes across
 
1493
            disk_nodes = set()
 
1494
            for index, key, value in self._iter_disk_pack_index():
 
1495
                disk_nodes.add((key, value))
 
1496
            # do a two-way diff against our original content
 
1497
            current_nodes = set()
 
1498
            for name, sizes in self._names.iteritems():
 
1499
                current_nodes.add(
 
1500
                    ((name, ), ' '.join(str(size) for size in sizes)))
 
1501
            deleted_nodes = self._packs_at_load - current_nodes
 
1502
            new_nodes = current_nodes - self._packs_at_load
 
1503
            disk_nodes.difference_update(deleted_nodes)
 
1504
            disk_nodes.update(new_nodes)
 
1505
            # TODO: handle same-name, index-size-changes here - 
 
1506
            # e.g. use the value from disk, not ours, *unless* we're the one
 
1507
            # changing it.
 
1508
            for key, value in disk_nodes:
 
1509
                builder.add_node(key, value)
 
1510
            self.transport.put_file('pack-names', builder.finish(),
 
1511
                mode=self.repo.control_files._file_mode)
 
1512
            # move the baseline forward
 
1513
            self._packs_at_load = disk_nodes
 
1514
            # now clear out the obsolete packs directory
 
1515
            if clear_obsolete_packs:
 
1516
                self.transport.clone('obsolete_packs').delete_multi(
 
1517
                    self.transport.list_dir('obsolete_packs'))
 
1518
        finally:
 
1519
            self._unlock_names()
 
1520
        # synchronise the memory packs list with what we just wrote:
 
1521
        new_names = dict(disk_nodes)
 
1522
        # drop no longer present nodes
 
1523
        for pack in self.all_packs():
 
1524
            if (pack.name,) not in new_names:
 
1525
                self._remove_pack_from_memory(pack)
 
1526
        # add new nodes/refresh existing ones
 
1527
        for key, value in disk_nodes:
 
1528
            name = key[0]
 
1529
            sizes = self._parse_index_sizes(value)
 
1530
            if name in self._names:
 
1531
                # existing
 
1532
                if sizes != self._names[name]:
 
1533
                    # the pack for name has had its indices replaced - rare but
 
1534
                    # important to handle. XXX: probably can never happen today
 
1535
                    # because the three-way merge code above does not handle it
 
1536
                    # - you may end up adding the same key twice to the new
 
1537
                    # disk index because the set values are the same, unless
 
1538
                    # the only index shows up as deleted by the set difference
 
1539
                    # - which it may. Until there is a specific test for this,
 
1540
                    # assume its broken. RBC 20071017.
 
1541
                    self._remove_pack_from_memory(self.get_pack_by_name(name))
 
1542
                    self._names[name] = sizes
 
1543
                    self.get_pack_by_name(name)
 
1544
            else:
 
1545
                # new
 
1546
                self._names[name] = sizes
 
1547
                self.get_pack_by_name(name)
 
1548
 
 
1549
    def _start_write_group(self):
 
1550
        # Do not permit preparation for writing if we're not in a 'write lock'.
 
1551
        if not self.repo.is_write_locked():
 
1552
            raise errors.NotWriteLocked(self)
 
1553
        self._new_pack = NewPack(self._upload_transport, self._index_transport,
 
1554
            self._pack_transport, upload_suffix='.pack',
 
1555
            file_mode=self.repo.control_files._file_mode)
 
1556
        # allow writing: queue writes to a new index
 
1557
        self.revision_index.add_writable_index(self._new_pack.revision_index,
 
1558
            self._new_pack)
 
1559
        self.inventory_index.add_writable_index(self._new_pack.inventory_index,
 
1560
            self._new_pack)
 
1561
        self.text_index.add_writable_index(self._new_pack.text_index,
 
1562
            self._new_pack)
 
1563
        self.signature_index.add_writable_index(self._new_pack.signature_index,
 
1564
            self._new_pack)
 
1565
 
 
1566
        # reused revision and signature knits may need updating
 
1567
        #
 
1568
        # "Hysterical raisins. client code in bzrlib grabs those knits outside
 
1569
        # of write groups and then mutates it inside the write group."
 
1570
        if self.repo._revision_knit is not None:
 
1571
            self.repo._revision_knit._index._add_callback = \
 
1572
                self.revision_index.add_callback
 
1573
        if self.repo._signature_knit is not None:
 
1574
            self.repo._signature_knit._index._add_callback = \
 
1575
                self.signature_index.add_callback
 
1576
        # create a reused knit object for text addition in commit.
 
1577
        self.repo._text_knit = self.repo.weave_store.get_weave_or_empty(
 
1578
            'all-texts', None)
 
1579
 
 
1580
    def _abort_write_group(self):
 
1581
        # FIXME: just drop the transient index.
 
1582
        # forget what names there are
 
1583
        self._new_pack.abort()
 
1584
        self._remove_pack_indices(self._new_pack)
 
1585
        self._new_pack = None
 
1586
        self.repo._text_knit = None
 
1587
 
 
1588
    def _commit_write_group(self):
 
1589
        self._remove_pack_indices(self._new_pack)
 
1590
        if self._new_pack.data_inserted():
 
1591
            # get all the data to disk and read to use
 
1592
            self._new_pack.finish()
 
1593
            self.allocate(self._new_pack)
 
1594
            self._new_pack = None
 
1595
            if not self.autopack():
 
1596
                # when autopack takes no steps, the names list is still
 
1597
                # unsaved.
 
1598
                self._save_pack_names()
 
1599
        else:
 
1600
            self._new_pack.abort()
 
1601
            self._new_pack = None
 
1602
        self.repo._text_knit = None
 
1603
 
 
1604
 
 
1605
class KnitPackRevisionStore(KnitRevisionStore):
 
1606
    """An object to adapt access from RevisionStore's to use KnitPacks.
 
1607
 
 
1608
    This class works by replacing the original RevisionStore.
 
1609
    We need to do this because the KnitPackRevisionStore is less
 
1610
    isolated in its layering - it uses services from the repo.
 
1611
    """
 
1612
 
 
1613
    def __init__(self, repo, transport, revisionstore):
 
1614
        """Create a KnitPackRevisionStore on repo with revisionstore.
 
1615
 
 
1616
        This will store its state in the Repository, use the
 
1617
        indices to provide a KnitGraphIndex,
 
1618
        and at the end of transactions write new indices.
 
1619
        """
 
1620
        KnitRevisionStore.__init__(self, revisionstore.versioned_file_store)
 
1621
        self.repo = repo
 
1622
        self._serializer = revisionstore._serializer
 
1623
        self.transport = transport
 
1624
 
 
1625
    def get_revision_file(self, transaction):
 
1626
        """Get the revision versioned file object."""
 
1627
        if getattr(self.repo, '_revision_knit', None) is not None:
 
1628
            return self.repo._revision_knit
 
1629
        self.repo._pack_collection.ensure_loaded()
 
1630
        add_callback = self.repo._pack_collection.revision_index.add_callback
 
1631
        # setup knit specific objects
 
1632
        knit_index = KnitGraphIndex(
 
1633
            self.repo._pack_collection.revision_index.combined_index,
 
1634
            add_callback=add_callback)
 
1635
        self.repo._revision_knit = knit.KnitVersionedFile(
 
1636
            'revisions', self.transport.clone('..'),
 
1637
            self.repo.control_files._file_mode,
 
1638
            create=False, access_mode=self.repo._access_mode(),
 
1639
            index=knit_index, delta=False, factory=knit.KnitPlainFactory(),
 
1640
            access_method=self.repo._pack_collection.revision_index.knit_access)
 
1641
        return self.repo._revision_knit
 
1642
 
 
1643
    def get_signature_file(self, transaction):
 
1644
        """Get the signature versioned file object."""
 
1645
        if getattr(self.repo, '_signature_knit', None) is not None:
 
1646
            return self.repo._signature_knit
 
1647
        self.repo._pack_collection.ensure_loaded()
 
1648
        add_callback = self.repo._pack_collection.signature_index.add_callback
 
1649
        # setup knit specific objects
 
1650
        knit_index = KnitGraphIndex(
 
1651
            self.repo._pack_collection.signature_index.combined_index,
 
1652
            add_callback=add_callback, parents=False)
 
1653
        self.repo._signature_knit = knit.KnitVersionedFile(
 
1654
            'signatures', self.transport.clone('..'),
 
1655
            self.repo.control_files._file_mode,
 
1656
            create=False, access_mode=self.repo._access_mode(),
 
1657
            index=knit_index, delta=False, factory=knit.KnitPlainFactory(),
 
1658
            access_method=self.repo._pack_collection.signature_index.knit_access)
 
1659
        return self.repo._signature_knit
 
1660
 
 
1661
 
 
1662
class KnitPackTextStore(VersionedFileStore):
 
1663
    """Presents a TextStore abstraction on top of packs.
 
1664
 
 
1665
    This class works by replacing the original VersionedFileStore.
 
1666
    We need to do this because the KnitPackRevisionStore is less
 
1667
    isolated in its layering - it uses services from the repo and shares them
 
1668
    with all the data written in a single write group.
 
1669
    """
 
1670
 
 
1671
    def __init__(self, repo, transport, weavestore):
 
1672
        """Create a KnitPackTextStore on repo with weavestore.
 
1673
 
 
1674
        This will store its state in the Repository, use the
 
1675
        indices FileNames to provide a KnitGraphIndex,
 
1676
        and at the end of transactions write new indices.
 
1677
        """
 
1678
        # don't call base class constructor - it's not suitable.
 
1679
        # no transient data stored in the transaction
 
1680
        # cache.
 
1681
        self._precious = False
 
1682
        self.repo = repo
 
1683
        self.transport = transport
 
1684
        self.weavestore = weavestore
 
1685
        # XXX for check() which isn't updated yet
 
1686
        self._transport = weavestore._transport
 
1687
 
 
1688
    def get_weave_or_empty(self, file_id, transaction):
 
1689
        """Get a 'Knit' backed by the .tix indices.
 
1690
 
 
1691
        The transaction parameter is ignored.
 
1692
        """
 
1693
        self.repo._pack_collection.ensure_loaded()
 
1694
        add_callback = self.repo._pack_collection.text_index.add_callback
 
1695
        # setup knit specific objects
 
1696
        file_id_index = GraphIndexPrefixAdapter(
 
1697
            self.repo._pack_collection.text_index.combined_index,
 
1698
            (file_id, ), 1, add_nodes_callback=add_callback)
 
1699
        knit_index = KnitGraphIndex(file_id_index,
 
1700
            add_callback=file_id_index.add_nodes,
 
1701
            deltas=True, parents=True)
 
1702
        return knit.KnitVersionedFile('text:' + file_id,
 
1703
            self.transport.clone('..'),
 
1704
            None,
 
1705
            index=knit_index,
 
1706
            access_method=self.repo._pack_collection.text_index.knit_access,
 
1707
            factory=knit.KnitPlainFactory())
 
1708
 
 
1709
    get_weave = get_weave_or_empty
 
1710
 
 
1711
    def __iter__(self):
 
1712
        """Generate a list of the fileids inserted, for use by check."""
 
1713
        self.repo._pack_collection.ensure_loaded()
 
1714
        ids = set()
 
1715
        for index, key, value, refs in \
 
1716
            self.repo._pack_collection.text_index.combined_index.iter_all_entries():
 
1717
            ids.add(key[0])
 
1718
        return iter(ids)
 
1719
 
 
1720
 
 
1721
class InventoryKnitThunk(object):
 
1722
    """An object to manage thunking get_inventory_weave to pack based knits."""
 
1723
 
 
1724
    def __init__(self, repo, transport):
 
1725
        """Create an InventoryKnitThunk for repo at transport.
 
1726
 
 
1727
        This will store its state in the Repository, use the
 
1728
        indices FileNames to provide a KnitGraphIndex,
 
1729
        and at the end of transactions write a new index..
 
1730
        """
 
1731
        self.repo = repo
 
1732
        self.transport = transport
 
1733
 
 
1734
    def get_weave(self):
 
1735
        """Get a 'Knit' that contains inventory data."""
 
1736
        self.repo._pack_collection.ensure_loaded()
 
1737
        add_callback = self.repo._pack_collection.inventory_index.add_callback
 
1738
        # setup knit specific objects
 
1739
        knit_index = KnitGraphIndex(
 
1740
            self.repo._pack_collection.inventory_index.combined_index,
 
1741
            add_callback=add_callback, deltas=True, parents=True)
 
1742
        return knit.KnitVersionedFile(
 
1743
            'inventory', self.transport.clone('..'),
 
1744
            self.repo.control_files._file_mode,
 
1745
            create=False, access_mode=self.repo._access_mode(),
 
1746
            index=knit_index, delta=True, factory=knit.KnitPlainFactory(),
 
1747
            access_method=self.repo._pack_collection.inventory_index.knit_access)
 
1748
 
 
1749
 
 
1750
class KnitPackRepository(KnitRepository):
 
1751
    """Experimental graph-knit using repository."""
 
1752
 
 
1753
    def __init__(self, _format, a_bzrdir, control_files, _revision_store,
 
1754
        control_store, text_store, _commit_builder_class, _serializer):
 
1755
        KnitRepository.__init__(self, _format, a_bzrdir, control_files,
 
1756
            _revision_store, control_store, text_store, _commit_builder_class,
 
1757
            _serializer)
 
1758
        index_transport = control_files._transport.clone('indices')
 
1759
        self._pack_collection = RepositoryPackCollection(self, control_files._transport,
 
1760
            index_transport,
 
1761
            control_files._transport.clone('upload'),
 
1762
            control_files._transport.clone('packs'))
 
1763
        self._revision_store = KnitPackRevisionStore(self, index_transport, self._revision_store)
 
1764
        self.weave_store = KnitPackTextStore(self, index_transport, self.weave_store)
 
1765
        self._inv_thunk = InventoryKnitThunk(self, index_transport)
 
1766
        # True when the repository object is 'write locked' (as opposed to the
 
1767
        # physical lock only taken out around changes to the pack-names list.) 
 
1768
        # Another way to represent this would be a decorator around the control
 
1769
        # files object that presents logical locks as physical ones - if this
 
1770
        # gets ugly consider that alternative design. RBC 20071011
 
1771
        self._write_lock_count = 0
 
1772
        self._transaction = None
 
1773
        # for tests
 
1774
        self._reconcile_does_inventory_gc = True
 
1775
        self._reconcile_fixes_text_parents = True
 
1776
        self._reconcile_backsup_inventory = False
 
1777
 
 
1778
    def _abort_write_group(self):
 
1779
        self._pack_collection._abort_write_group()
 
1780
 
 
1781
    def _access_mode(self):
 
1782
        """Return 'w' or 'r' for depending on whether a write lock is active.
 
1783
        
 
1784
        This method is a helper for the Knit-thunking support objects.
 
1785
        """
 
1786
        if self.is_write_locked():
 
1787
            return 'w'
 
1788
        return 'r'
 
1789
 
 
1790
    def _find_inconsistent_revision_parents(self):
 
1791
        """Find revisions with incorrectly cached parents.
 
1792
 
 
1793
        :returns: an iterator yielding tuples of (revison-id, parents-in-index,
 
1794
            parents-in-revision).
 
1795
        """
 
1796
        if not self.is_locked():
 
1797
            raise errors.ObjectNotLocked(self)
 
1798
        pb = ui.ui_factory.nested_progress_bar()
 
1799
        result = []
 
1800
        try:
 
1801
            revision_nodes = self._pack_collection.revision_index \
 
1802
                .combined_index.iter_all_entries()
 
1803
            index_positions = []
 
1804
            # Get the cached index values for all revisions, and also the location
 
1805
            # in each index of the revision text so we can perform linear IO.
 
1806
            for index, key, value, refs in revision_nodes:
 
1807
                pos, length = value[1:].split(' ')
 
1808
                index_positions.append((index, int(pos), key[0],
 
1809
                    tuple(parent[0] for parent in refs[0])))
 
1810
                pb.update("Reading revision index.", 0, 0)
 
1811
            index_positions.sort()
 
1812
            batch_count = len(index_positions) / 1000 + 1
 
1813
            pb.update("Checking cached revision graph.", 0, batch_count)
 
1814
            for offset in xrange(batch_count):
 
1815
                pb.update("Checking cached revision graph.", offset)
 
1816
                to_query = index_positions[offset * 1000:(offset + 1) * 1000]
 
1817
                if not to_query:
 
1818
                    break
 
1819
                rev_ids = [item[2] for item in to_query]
 
1820
                revs = self.get_revisions(rev_ids)
 
1821
                for revision, item in zip(revs, to_query):
 
1822
                    index_parents = item[3]
 
1823
                    rev_parents = tuple(revision.parent_ids)
 
1824
                    if index_parents != rev_parents:
 
1825
                        result.append((revision.revision_id, index_parents, rev_parents))
 
1826
        finally:
 
1827
            pb.finished()
 
1828
        return result
 
1829
 
 
1830
    def get_parents(self, revision_ids):
 
1831
        """See StackedParentsProvider.get_parents.
 
1832
        
 
1833
        This implementation accesses the combined revision index to provide
 
1834
        answers.
 
1835
        """
 
1836
        self._pack_collection.ensure_loaded()
 
1837
        index = self._pack_collection.revision_index.combined_index
 
1838
        search_keys = set()
 
1839
        for revision_id in revision_ids:
 
1840
            if revision_id != _mod_revision.NULL_REVISION:
 
1841
                search_keys.add((revision_id,))
 
1842
        found_parents = {_mod_revision.NULL_REVISION:[]}
 
1843
        for index, key, value, refs in index.iter_entries(search_keys):
 
1844
            parents = refs[0]
 
1845
            if not parents:
 
1846
                parents = (_mod_revision.NULL_REVISION,)
 
1847
            else:
 
1848
                parents = tuple(parent[0] for parent in parents)
 
1849
            found_parents[key[0]] = parents
 
1850
        result = []
 
1851
        for revision_id in revision_ids:
 
1852
            try:
 
1853
                result.append(found_parents[revision_id])
 
1854
            except KeyError:
 
1855
                result.append(None)
 
1856
        return result
 
1857
 
 
1858
    def _make_parents_provider(self):
 
1859
        return self
 
1860
 
 
1861
    def _refresh_data(self):
 
1862
        if self._write_lock_count == 1 or (
 
1863
            self.control_files._lock_count == 1 and
 
1864
            self.control_files._lock_mode == 'r'):
 
1865
            # forget what names there are
 
1866
            self._pack_collection.reset()
 
1867
            # XXX: Better to do an in-memory merge when acquiring a new lock -
 
1868
            # factor out code from _save_pack_names.
 
1869
            self._pack_collection.ensure_loaded()
 
1870
 
 
1871
    def _start_write_group(self):
 
1872
        self._pack_collection._start_write_group()
 
1873
 
 
1874
    def _commit_write_group(self):
 
1875
        return self._pack_collection._commit_write_group()
 
1876
 
 
1877
    def get_inventory_weave(self):
 
1878
        return self._inv_thunk.get_weave()
 
1879
 
 
1880
    def get_transaction(self):
 
1881
        if self._write_lock_count:
 
1882
            return self._transaction
 
1883
        else:
 
1884
            return self.control_files.get_transaction()
 
1885
 
 
1886
    def is_locked(self):
 
1887
        return self._write_lock_count or self.control_files.is_locked()
 
1888
 
 
1889
    def is_write_locked(self):
 
1890
        return self._write_lock_count
 
1891
 
 
1892
    def lock_write(self, token=None):
 
1893
        if not self._write_lock_count and self.is_locked():
 
1894
            raise errors.ReadOnlyError(self)
 
1895
        self._write_lock_count += 1
 
1896
        if self._write_lock_count == 1:
 
1897
            from bzrlib import transactions
 
1898
            self._transaction = transactions.WriteTransaction()
 
1899
        self._refresh_data()
 
1900
 
 
1901
    def lock_read(self):
 
1902
        if self._write_lock_count:
 
1903
            self._write_lock_count += 1
 
1904
        else:
 
1905
            self.control_files.lock_read()
 
1906
        self._refresh_data()
 
1907
 
 
1908
    def leave_lock_in_place(self):
 
1909
        # not supported - raise an error
 
1910
        raise NotImplementedError(self.leave_lock_in_place)
 
1911
 
 
1912
    def dont_leave_lock_in_place(self):
 
1913
        # not supported - raise an error
 
1914
        raise NotImplementedError(self.dont_leave_lock_in_place)
 
1915
 
 
1916
    @needs_write_lock
 
1917
    def pack(self):
 
1918
        """Compress the data within the repository.
 
1919
 
 
1920
        This will pack all the data to a single pack. In future it may
 
1921
        recompress deltas or do other such expensive operations.
 
1922
        """
 
1923
        self._pack_collection.pack()
 
1924
 
 
1925
    @needs_write_lock
 
1926
    def reconcile(self, other=None, thorough=False):
 
1927
        """Reconcile this repository."""
 
1928
        from bzrlib.reconcile import PackReconciler
 
1929
        reconciler = PackReconciler(self, thorough=thorough)
 
1930
        reconciler.reconcile()
 
1931
        return reconciler
 
1932
 
 
1933
    def unlock(self):
 
1934
        if self._write_lock_count == 1 and self._write_group is not None:
 
1935
            self.abort_write_group()
 
1936
            self._transaction = None
 
1937
            self._write_lock_count = 0
 
1938
            raise errors.BzrError(
 
1939
                'Must end write group before releasing write lock on %s'
 
1940
                % self)
 
1941
        if self._write_lock_count:
 
1942
            self._write_lock_count -= 1
 
1943
            if not self._write_lock_count:
 
1944
                transaction = self._transaction
 
1945
                self._transaction = None
 
1946
                transaction.finish()
 
1947
        else:
 
1948
            self.control_files.unlock()
 
1949
 
 
1950
 
 
1951
class RepositoryFormatPack(MetaDirRepositoryFormat):
 
1952
    """Format logic for pack structured repositories.
 
1953
 
 
1954
    This repository format has:
 
1955
     - a list of packs in pack-names
 
1956
     - packs in packs/NAME.pack
 
1957
     - indices in indices/NAME.{iix,six,tix,rix}
 
1958
     - knit deltas in the packs, knit indices mapped to the indices.
 
1959
     - thunk objects to support the knits programming API.
 
1960
     - a format marker of its own
 
1961
     - an optional 'shared-storage' flag
 
1962
     - an optional 'no-working-trees' flag
 
1963
     - a LockDir lock
 
1964
    """
 
1965
 
 
1966
    # Set this attribute in derived classes to control the repository class
 
1967
    # created by open and initialize.
 
1968
    repository_class = None
 
1969
    # Set this attribute in derived classes to control the
 
1970
    # _commit_builder_class that the repository objects will have passed to
 
1971
    # their constructor.
 
1972
    _commit_builder_class = None
 
1973
    # Set this attribute in derived clases to control the _serializer that the
 
1974
    # repository objects will have passed to their constructor.
 
1975
    _serializer = None
 
1976
 
 
1977
    def _get_control_store(self, repo_transport, control_files):
 
1978
        """Return the control store for this repository."""
 
1979
        return VersionedFileStore(
 
1980
            repo_transport,
 
1981
            prefixed=False,
 
1982
            file_mode=control_files._file_mode,
 
1983
            versionedfile_class=knit.KnitVersionedFile,
 
1984
            versionedfile_kwargs={'factory': knit.KnitPlainFactory()},
 
1985
            )
 
1986
 
 
1987
    def _get_revision_store(self, repo_transport, control_files):
 
1988
        """See RepositoryFormat._get_revision_store()."""
 
1989
        versioned_file_store = VersionedFileStore(
 
1990
            repo_transport,
 
1991
            file_mode=control_files._file_mode,
 
1992
            prefixed=False,
 
1993
            precious=True,
 
1994
            versionedfile_class=knit.KnitVersionedFile,
 
1995
            versionedfile_kwargs={'delta': False,
 
1996
                                  'factory': knit.KnitPlainFactory(),
 
1997
                                 },
 
1998
            escaped=True,
 
1999
            )
 
2000
        return KnitRevisionStore(versioned_file_store)
 
2001
 
 
2002
    def _get_text_store(self, transport, control_files):
 
2003
        """See RepositoryFormat._get_text_store()."""
 
2004
        return self._get_versioned_file_store('knits',
 
2005
                                  transport,
 
2006
                                  control_files,
 
2007
                                  versionedfile_class=knit.KnitVersionedFile,
 
2008
                                  versionedfile_kwargs={
 
2009
                                      'create_parent_dir': True,
 
2010
                                      'delay_create': True,
 
2011
                                      'dir_mode': control_files._dir_mode,
 
2012
                                  },
 
2013
                                  escaped=True)
 
2014
 
 
2015
    def initialize(self, a_bzrdir, shared=False):
 
2016
        """Create a pack based repository.
 
2017
 
 
2018
        :param a_bzrdir: bzrdir to contain the new repository; must already
 
2019
            be initialized.
 
2020
        :param shared: If true the repository will be initialized as a shared
 
2021
                       repository.
 
2022
        """
 
2023
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
2024
        dirs = ['indices', 'obsolete_packs', 'packs', 'upload']
 
2025
        builder = GraphIndexBuilder()
 
2026
        files = [('pack-names', builder.finish())]
 
2027
        utf8_files = [('format', self.get_format_string())]
 
2028
        
 
2029
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
2030
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
2031
 
 
2032
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
2033
        """See RepositoryFormat.open().
 
2034
        
 
2035
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
2036
                                    repository at a slightly different url
 
2037
                                    than normal. I.e. during 'upgrade'.
 
2038
        """
 
2039
        if not _found:
 
2040
            format = RepositoryFormat.find_format(a_bzrdir)
 
2041
            assert format.__class__ ==  self.__class__
 
2042
        if _override_transport is not None:
 
2043
            repo_transport = _override_transport
 
2044
        else:
 
2045
            repo_transport = a_bzrdir.get_repository_transport(None)
 
2046
        control_files = lockable_files.LockableFiles(repo_transport,
 
2047
                                'lock', lockdir.LockDir)
 
2048
        text_store = self._get_text_store(repo_transport, control_files)
 
2049
        control_store = self._get_control_store(repo_transport, control_files)
 
2050
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
2051
        return self.repository_class(_format=self,
 
2052
                              a_bzrdir=a_bzrdir,
 
2053
                              control_files=control_files,
 
2054
                              _revision_store=_revision_store,
 
2055
                              control_store=control_store,
 
2056
                              text_store=text_store,
 
2057
                              _commit_builder_class=self._commit_builder_class,
 
2058
                              _serializer=self._serializer)
 
2059
 
 
2060
 
 
2061
class RepositoryFormatKnitPack1(RepositoryFormatPack):
 
2062
    """A no-subtrees parameterised Pack repository.
 
2063
 
 
2064
    This format was introduced in 0.92.
 
2065
    """
 
2066
 
 
2067
    repository_class = KnitPackRepository
 
2068
    _commit_builder_class = PackCommitBuilder
 
2069
    _serializer = xml5.serializer_v5
 
2070
 
 
2071
    def _get_matching_bzrdir(self):
 
2072
        return bzrdir.format_registry.make_bzrdir('pack-0.92')
 
2073
 
 
2074
    def _ignore_setting_bzrdir(self, format):
 
2075
        pass
 
2076
 
 
2077
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
 
2078
 
 
2079
    def get_format_string(self):
 
2080
        """See RepositoryFormat.get_format_string()."""
 
2081
        return "Bazaar pack repository format 1 (needs bzr 0.92)\n"
 
2082
 
 
2083
    def get_format_description(self):
 
2084
        """See RepositoryFormat.get_format_description()."""
 
2085
        return "Packs containing knits without subtree support"
 
2086
 
 
2087
    def check_conversion_target(self, target_format):
 
2088
        pass
 
2089
 
 
2090
 
 
2091
class RepositoryFormatKnitPack3(RepositoryFormatPack):
 
2092
    """A subtrees parameterised Pack repository.
 
2093
 
 
2094
    This repository format uses the xml7 serializer to get:
 
2095
     - support for recording full info about the tree root
 
2096
     - support for recording tree-references
 
2097
 
 
2098
    This format was introduced in 0.92.
 
2099
    """
 
2100
 
 
2101
    repository_class = KnitPackRepository
 
2102
    _commit_builder_class = PackRootCommitBuilder
 
2103
    rich_root_data = True
 
2104
    supports_tree_reference = True
 
2105
    _serializer = xml7.serializer_v7
 
2106
 
 
2107
    def _get_matching_bzrdir(self):
 
2108
        return bzrdir.format_registry.make_bzrdir(
 
2109
            'pack-0.92-subtree')
 
2110
 
 
2111
    def _ignore_setting_bzrdir(self, format):
 
2112
        pass
 
2113
 
 
2114
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
 
2115
 
 
2116
    def check_conversion_target(self, target_format):
 
2117
        if not target_format.rich_root_data:
 
2118
            raise errors.BadConversionTarget(
 
2119
                'Does not support rich root data.', target_format)
 
2120
        if not getattr(target_format, 'supports_tree_reference', False):
 
2121
            raise errors.BadConversionTarget(
 
2122
                'Does not support nested trees', target_format)
 
2123
            
 
2124
    def get_format_string(self):
 
2125
        """See RepositoryFormat.get_format_string()."""
 
2126
        return "Bazaar pack repository format 1 with subtree support (needs bzr 0.92)\n"
 
2127
 
 
2128
    def get_format_description(self):
 
2129
        """See RepositoryFormat.get_format_description()."""
 
2130
        return "Packs containing knits with subtree support\n"
 
2131
 
 
2132
 
 
2133
class RepositoryFormatKnitPack4(RepositoryFormatPack):
 
2134
    """A rich-root, no subtrees parameterised Pack repository.
 
2135
 
 
2136
    This repository format uses the xml6 serializer to get:
 
2137
     - support for recording full info about the tree root
 
2138
 
 
2139
    This format was introduced in 1.0.
 
2140
    """
 
2141
 
 
2142
    repository_class = KnitPackRepository
 
2143
    _commit_builder_class = PackRootCommitBuilder
 
2144
    rich_root_data = True
 
2145
    supports_tree_reference = False
 
2146
    _serializer = xml6.serializer_v6
 
2147
 
 
2148
    def _get_matching_bzrdir(self):
 
2149
        return bzrdir.format_registry.make_bzrdir(
 
2150
            'rich-root-pack')
 
2151
 
 
2152
    def _ignore_setting_bzrdir(self, format):
 
2153
        pass
 
2154
 
 
2155
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
 
2156
 
 
2157
    def check_conversion_target(self, target_format):
 
2158
        if not target_format.rich_root_data:
 
2159
            raise errors.BadConversionTarget(
 
2160
                'Does not support rich root data.', target_format)
 
2161
 
 
2162
    def get_format_string(self):
 
2163
        """See RepositoryFormat.get_format_string()."""
 
2164
        return ("Bazaar pack repository format 1 with rich root"
 
2165
                " (needs bzr 1.0)\n")
 
2166
 
 
2167
    def get_format_description(self):
 
2168
        """See RepositoryFormat.get_format_description()."""
 
2169
        return "Packs containing knits with rich root support\n"