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