1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
# groupcompress, a bzr plugin providing improved disk utilisation
# Copyright (C) 2008 Canonical Limited.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
"""Repostory formats using B+Tree indices and groupcompress compression."""
import md5
import time
from bzrlib import debug, errors, pack, repository
from bzrlib.btree_index import (
BTreeBuilder,
BTreeGraphIndex,
)
from bzrlib.index import GraphIndex, GraphIndexBuilder
from bzrlib.repository import InterPackRepo
from bzrlib.plugins.groupcompress.groupcompress import (
_GCGraphIndex,
GroupCompressVersionedFiles,
)
from bzrlib.osutils import rand_chars
from bzrlib.repofmt.pack_repo import (
Pack,
NewPack,
KnitPackRepository,
RepositoryPackCollection,
RepositoryFormatPackDevelopment2,
RepositoryFormatPackDevelopment2Subtree,
RepositoryFormatKnitPack1,
RepositoryFormatKnitPack3,
RepositoryFormatKnitPack4,
Packer,
ReconcilePacker,
OptimisingPacker,
)
try:
from bzrlib.repofmt.pack_repo import (
CHKInventoryRepository,
RepositoryFormatPackDevelopment4,
RepositoryFormatPackDevelopment4Subtree,
)
chk_support = True
except ImportError:
chk_support = False
from bzrlib import ui
def open_pack(self):
return self._pack_collection.pack_factory(self._pack_collection,
upload_suffix=self.suffix,
file_mode=self._pack_collection.repo.bzrdir._get_file_mode())
Packer.open_pack = open_pack
class GCPack(NewPack):
def __init__(self, pack_collection, upload_suffix='', file_mode=None):
"""Create a NewPack instance.
:param upload_transport: A writable transport for the pack to be
incrementally uploaded to.
:param index_transport: A writable transport for the pack's indices to
be written to when the pack is finished.
:param pack_transport: A writable transport for the pack to be renamed
to when the upload is complete. This *must* be the same as
upload_transport.clone('../packs').
:param upload_suffix: An optional suffix to be given to any temporary
files created during the pack creation. e.g '.autopack'
:param file_mode: An optional file mode to create the new files with.
"""
# replaced from bzr.dev to:
# - change inventory reference list length to 1
# - change texts reference lists to 1
# TODO: patch this to be parameterised upstream
# The relative locations of the packs are constrained, but all are
# passed in because the caller has them, so as to avoid object churn.
index_builder_class = pack_collection._index_builder_class
if chk_support:
# from brisbane-core
if pack_collection.chk_index is not None:
chk_index = index_builder_class(reference_lists=0)
else:
chk_index = None
Pack.__init__(self,
# Revisions: parents list, no text compression.
index_builder_class(reference_lists=1),
# Inventory: We want to map compression only, but currently the
# knit code hasn't been updated enough to understand that, so we
# have a regular 2-list index giving parents and compression
# source.
index_builder_class(reference_lists=1),
# Texts: compression and per file graph, for all fileids - so two
# reference lists and two elements in the key tuple.
index_builder_class(reference_lists=1, key_elements=2),
# Signatures: Just blobs to store, no compression, no parents
# listing.
index_builder_class(reference_lists=0),
# CHK based storage - just blobs, no compression or parents.
chk_index=chk_index
)
else:
# from bzr.dev
Pack.__init__(self,
# Revisions: parents list, no text compression.
index_builder_class(reference_lists=1),
# Inventory: compressed, with graph for compatibility with other
# existing bzrlib code.
index_builder_class(reference_lists=1),
# Texts: per file graph:
index_builder_class(reference_lists=1, key_elements=2),
# Signatures: Just blobs to store, no compression, no parents
# listing.
index_builder_class(reference_lists=0),
)
self._pack_collection = pack_collection
# When we make readonly indices, we need this.
self.index_class = pack_collection._index_class
# where should the new pack be opened
self.upload_transport = pack_collection._upload_transport
# where are indices written out to
self.index_transport = pack_collection._index_transport
# where is the pack renamed to when it is finished?
self.pack_transport = pack_collection._pack_transport
# What file mode to upload the pack and indices with.
self._file_mode = file_mode
# tracks the content written to the .pack file.
self._hash = md5.new()
# a four-tuple with the length in bytes of the indices, once the pack
# is finalised. (rev, inv, text, sigs)
self.index_sizes = None
# How much data to cache when writing packs. Note that this is not
# synchronised with reads, because it's not in the transport layer, so
# is not safe unless the client knows it won't be reading from the pack
# under creation.
self._cache_limit = 0
# the temporary pack file name.
self.random_name = rand_chars(20) + upload_suffix
# when was this pack started ?
self.start_time = time.time()
# open an output stream for the data added to the pack.
self.write_stream = self.upload_transport.open_write_stream(
self.random_name, mode=self._file_mode)
if 'pack' in debug.debug_flags:
mutter('%s: create_pack: pack stream open: %s%s t+%6.3fs',
time.ctime(), self.upload_transport.base, self.random_name,
time.time() - self.start_time)
# A list of byte sequences to be written to the new pack, and the
# aggregate size of them. Stored as a list rather than separate
# variables so that the _write_data closure below can update them.
self._buffer = [[], 0]
# create a callable for adding data
#
# robertc says- this is a closure rather than a method on the object
# so that the variables are locals, and faster than accessing object
# members.
def _write_data(bytes, flush=False, _buffer=self._buffer,
_write=self.write_stream.write, _update=self._hash.update):
_buffer[0].append(bytes)
_buffer[1] += len(bytes)
# buffer cap
if _buffer[1] > self._cache_limit or flush:
bytes = ''.join(_buffer[0])
_write(bytes)
_update(bytes)
_buffer[:] = [[], 0]
# expose this on self, for the occasion when clients want to add data.
self._write_data = _write_data
# a pack writer object to serialise pack records.
self._writer = pack.ContainerWriter(self._write_data)
self._writer.begin()
# what state is the pack in? (open, finished, aborted)
self._state = 'open'
RepositoryPackCollection.pack_factory = NewPack
class GCRepositoryPackCollection(RepositoryPackCollection):
pack_factory = GCPack
def _make_index(self, name, suffix):
"""Overridden to use BTreeGraphIndex objects."""
size_offset = self._suffix_offsets[suffix]
index_name = name + suffix
index_size = self._names[name][size_offset]
return BTreeGraphIndex(
self._index_transport, index_name, index_size)
def _start_write_group(self):
# Overridden to add 'self.pack_factory()'
# Do not permit preparation for writing if we're not in a 'write lock'.
if not self.repo.is_write_locked():
raise errors.NotWriteLocked(self)
self._new_pack = self.pack_factory(self, upload_suffix='.pack',
file_mode=self.repo.bzrdir._get_file_mode())
# allow writing: queue writes to a new index
self.revision_index.add_writable_index(self._new_pack.revision_index,
self._new_pack)
self.inventory_index.add_writable_index(self._new_pack.inventory_index,
self._new_pack)
self.text_index.add_writable_index(self._new_pack.text_index,
self._new_pack)
self.signature_index.add_writable_index(self._new_pack.signature_index,
self._new_pack)
if chk_support and self.chk_index is not None:
self.chk_index.add_writable_index(self._new_pack.chk_index,
self._new_pack)
self.repo.chk_bytes._index._add_callback = self.chk_index.add_callback
self.repo.inventories._index._add_callback = self.inventory_index.add_callback
self.repo.revisions._index._add_callback = self.revision_index.add_callback
self.repo.signatures._index._add_callback = self.signature_index.add_callback
self.repo.texts._index._add_callback = self.text_index.add_callback
class GCPackRepository(KnitPackRepository):
"""GC customisation of KnitPackRepository."""
def __init__(self, _format, a_bzrdir, control_files, _commit_builder_class,
_serializer):
"""Overridden to change pack collection class."""
KnitPackRepository.__init__(self, _format, a_bzrdir, control_files,
_commit_builder_class, _serializer)
# and now replace everything it did :)
index_transport = self._transport.clone('indices')
if chk_support:
self._pack_collection = GCRepositoryPackCollection(self,
self._transport, index_transport,
self._transport.clone('upload'),
self._transport.clone('packs'),
_format.index_builder_class,
_format.index_class,
use_chk_index=self._format.supports_chks,
)
else:
self._pack_collection = GCRepositoryPackCollection(self,
self._transport, index_transport,
self._transport.clone('upload'),
self._transport.clone('packs'),
_format.index_builder_class,
_format.index_class)
self.inventories = GroupCompressVersionedFiles(
_GCGraphIndex(self._pack_collection.inventory_index.combined_index,
add_callback=self._pack_collection.inventory_index.add_callback,
parents=True, is_locked=self.is_locked),
access=self._pack_collection.inventory_index.data_access)
self.revisions = GroupCompressVersionedFiles(
_GCGraphIndex(self._pack_collection.revision_index.combined_index,
add_callback=self._pack_collection.revision_index.add_callback,
parents=True, is_locked=self.is_locked),
access=self._pack_collection.revision_index.data_access,
delta=False)
self.signatures = GroupCompressVersionedFiles(
_GCGraphIndex(self._pack_collection.signature_index.combined_index,
add_callback=self._pack_collection.signature_index.add_callback,
parents=False, is_locked=self.is_locked),
access=self._pack_collection.signature_index.data_access,
delta=False)
self.texts = GroupCompressVersionedFiles(
_GCGraphIndex(self._pack_collection.text_index.combined_index,
add_callback=self._pack_collection.text_index.add_callback,
parents=True, is_locked=self.is_locked),
access=self._pack_collection.text_index.data_access)
if chk_support and _format.supports_chks:
# No graph, no compression:- references from chks are between
# different objects not temporal versions of the same; and without
# some sort of temporal structure knit compression will just fail.
self.chk_bytes = GroupCompressVersionedFiles(
_GCGraphIndex(self._pack_collection.chk_index.combined_index,
add_callback=self._pack_collection.chk_index.add_callback,
parents=False, is_locked=self.is_locked),
access=self._pack_collection.chk_index.data_access)
else:
self.chk_bytes = None
# True when the repository object is 'write locked' (as opposed to the
# physical lock only taken out around changes to the pack-names list.)
# Another way to represent this would be a decorator around the control
# files object that presents logical locks as physical ones - if this
# gets ugly consider that alternative design. RBC 20071011
self._write_lock_count = 0
self._transaction = None
# for tests
self._reconcile_does_inventory_gc = True
self._reconcile_fixes_text_parents = True
self._reconcile_backsup_inventory = False
if chk_support:
class GCCHKPackRepository(CHKInventoryRepository):
"""GC customisation of CHKInventoryRepository."""
def __init__(self, _format, a_bzrdir, control_files, _commit_builder_class,
_serializer):
"""Overridden to change pack collection class."""
KnitPackRepository.__init__(self, _format, a_bzrdir, control_files,
_commit_builder_class, _serializer)
# and now replace everything it did :)
index_transport = self._transport.clone('indices')
if chk_support:
self._pack_collection = GCRepositoryPackCollection(self,
self._transport, index_transport,
self._transport.clone('upload'),
self._transport.clone('packs'),
_format.index_builder_class,
_format.index_class,
use_chk_index=self._format.supports_chks,
)
else:
self._pack_collection = GCRepositoryPackCollection(self,
self._transport, index_transport,
self._transport.clone('upload'),
self._transport.clone('packs'),
_format.index_builder_class,
_format.index_class)
self.inventories = GroupCompressVersionedFiles(
_GCGraphIndex(self._pack_collection.inventory_index.combined_index,
add_callback=self._pack_collection.inventory_index.add_callback,
parents=True, is_locked=self.is_locked),
access=self._pack_collection.inventory_index.data_access)
self.revisions = GroupCompressVersionedFiles(
_GCGraphIndex(self._pack_collection.revision_index.combined_index,
add_callback=self._pack_collection.revision_index.add_callback,
parents=True, is_locked=self.is_locked),
access=self._pack_collection.revision_index.data_access,
delta=False)
self.signatures = GroupCompressVersionedFiles(
_GCGraphIndex(self._pack_collection.signature_index.combined_index,
add_callback=self._pack_collection.signature_index.add_callback,
parents=False, is_locked=self.is_locked),
access=self._pack_collection.signature_index.data_access,
delta=False)
self.texts = GroupCompressVersionedFiles(
_GCGraphIndex(self._pack_collection.text_index.combined_index,
add_callback=self._pack_collection.text_index.add_callback,
parents=True, is_locked=self.is_locked),
access=self._pack_collection.text_index.data_access)
if chk_support and _format.supports_chks:
# No graph, no compression:- references from chks are between
# different objects not temporal versions of the same; and without
# some sort of temporal structure knit compression will just fail.
self.chk_bytes = GroupCompressVersionedFiles(
_GCGraphIndex(self._pack_collection.chk_index.combined_index,
add_callback=self._pack_collection.chk_index.add_callback,
parents=False, is_locked=self.is_locked),
access=self._pack_collection.chk_index.data_access)
else:
self.chk_bytes = None
# True when the repository object is 'write locked' (as opposed to the
# physical lock only taken out around changes to the pack-names list.)
# Another way to represent this would be a decorator around the control
# files object that presents logical locks as physical ones - if this
# gets ugly consider that alternative design. RBC 20071011
self._write_lock_count = 0
self._transaction = None
# for tests
self._reconcile_does_inventory_gc = True
self._reconcile_fixes_text_parents = True
self._reconcile_backsup_inventory = False
class RepositoryFormatPackGCPlain(RepositoryFormatPackDevelopment2):
"""A B+Tree index using pack repository."""
repository_class = GCPackRepository
def get_format_string(self):
"""See RepositoryFormat.get_format_string()."""
return ("Bazaar development format - btree+gc "
"(needs bzr.dev from 1.6)\n")
def get_format_description(self):
"""See RepositoryFormat.get_format_description()."""
return ("Development repository format - btree+groupcompress "
", interoperates with pack-0.92\n")
class RepositoryFormatPackGCRichRoot(RepositoryFormatKnitPack4):
"""A B+Tree index using pack repository."""
repository_class = GCPackRepository
def get_format_string(self):
"""See RepositoryFormat.get_format_string()."""
return ("Bazaar development format - btree+gc-rich-root "
"(needs bzr.dev from 1.6)\n")
def get_format_description(self):
"""See RepositoryFormat.get_format_description()."""
return ("Development repository format - btree+groupcompress "
", interoperates with rich-root-pack\n")
class RepositoryFormatPackGCSubtrees(RepositoryFormatPackDevelopment2Subtree):
"""A B+Tree index using pack repository."""
repository_class = GCPackRepository
def get_format_string(self):
"""See RepositoryFormat.get_format_string()."""
return ("Bazaar development format - btree+gc-subtrees "
"(needs bzr.dev from 1.6)\n")
def get_format_description(self):
"""See RepositoryFormat.get_format_description()."""
return ("Development repository format - btree+groupcompress "
", interoperates with pack-0.92-subtrees\n")
if chk_support:
'Bazaar development format - 1.9+gc (needs bzr.dev from 1.9)\n',
class RepositoryFormatPackGCPlainCHK(RepositoryFormatPackDevelopment4):
"""A CHK+group compress pack repository."""
repository_class = GCCHKPackRepository
def get_format_string(self):
"""See RepositoryFormat.get_format_string()."""
return ('Bazaar development format - chk+gc '
'(needs bzr.dev from 1.12)\n')
def get_format_description(self):
"""See RepositoryFormat.get_format_description()."""
return ("Development repository format - chk+groupcompress "
", interoperates with pack-0.92\n")
def pack_incompatible(source, target, orig_method=InterPackRepo.is_compatible):
"""Be incompatible with the regular fetch code."""
formats = (RepositoryFormatPackGCPlain, RepositoryFormatPackGCRichRoot,
RepositoryFormatPackGCSubtrees)
if chk_support:
formats = formats + (RepositoryFormatPackGCPlain,)
if isinstance(source._format, formats) or isinstance(target._format, formats):
return False
else:
return orig_method(source, target)
InterPackRepo.is_compatible = staticmethod(pack_incompatible)
|