~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repofmt/pack_repo.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-08-17 07:52:09 UTC
  • mfrom: (1910.3.4 trivial)
  • Revision ID: pqm@pqm.ubuntu.com-20060817075209-e85a1f9e05ff8b87
(andrew) Trivial fixes to NotImplemented errors.

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