~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/groupcompress.py

  • Committer: John Arbash Meinel
  • Date: 2009-04-22 17:18:45 UTC
  • mto: This revision was merged to the branch mainline in revision 4301.
  • Revision ID: john@arbash-meinel.com-20090422171845-5dmqokv8ygf3cvs5
Add the ability to convert a gc block into 'human readable' form.

Show diffs side-by-side

added added

removed removed

Lines of Context:
299
299
                 ]
300
300
        return ''.join(chunks)
301
301
 
 
302
    def _dump(self, include_text=False):
 
303
        """Take this block, and spit out a human-readable structure.
 
304
 
 
305
        :param include_text: Inserts also include text bits, chose whether you
 
306
            want this displayed in the dump or not.
 
307
        :return: A dump of the given block. The layout is something like:
 
308
            [('f', length), ('d', delta_length, text_length, [delta_info])]
 
309
            delta_info := [('i', num_bytes, text), ('c', offset, num_bytes),
 
310
            ...]
 
311
        """
 
312
        self._ensure_content()
 
313
        result = []
 
314
        pos = 0
 
315
        while pos < self._content_length:
 
316
            kind = self._content[pos]
 
317
            pos += 1
 
318
            if kind not in ('f', 'd'):
 
319
                raise ValueError('invalid kind character: %r' % (kind,))
 
320
            content_len, len_len = decode_base128_int(
 
321
                                self._content[pos:pos + 5])
 
322
            pos += len_len
 
323
            if content_len + pos > self._content_length:
 
324
                raise ValueError('invalid content_len %d for record @ pos %d'
 
325
                                 % (content_len, pos - len_len - 1))
 
326
            if kind == 'f': # Fulltext
 
327
                result.append(('f', content_len))
 
328
            elif kind == 'd': # Delta
 
329
                delta_content = self._content[pos:pos+content_len]
 
330
                delta_info = []
 
331
                # The first entry in a delta is the decompressed length
 
332
                decomp_len, delta_pos = decode_base128_int(delta_content)
 
333
                result.append(('d', content_len, decomp_len, delta_info))
 
334
                measured_len = 0
 
335
                while delta_pos < content_len:
 
336
                    c = ord(delta_content[delta_pos])
 
337
                    delta_pos += 1
 
338
                    if c & 0x80: # Copy
 
339
                        (offset, length,
 
340
                         delta_pos) = decode_copy_instruction(delta_content, c,
 
341
                                                              delta_pos)
 
342
                        delta_info.append(('c', offset, length))
 
343
                        measured_len += length
 
344
                    else: # Insert
 
345
                        if include_text:
 
346
                            txt = delta_content[delta_pos:delta_pos+c]
 
347
                        else:
 
348
                            txt = ''
 
349
                        delta_info.append(('i', c, txt))
 
350
                        measured_len += c
 
351
                        delta_pos += c
 
352
                if delta_pos != content_len:
 
353
                    raise ValueError('Delta consumed a bad number of bytes:'
 
354
                                     ' %d != %d' % (delta_pos, content_len))
 
355
                if measured_len != decomp_len:
 
356
                    raise ValueError('Delta claimed fulltext was %d bytes, but'
 
357
                                     ' extraction resulted in %d bytes'
 
358
                                     % (decomp_len, measured_len))
 
359
            pos += content_len
 
360
        return result
 
361
 
302
362
 
303
363
class _LazyGroupCompressFactory(object):
304
364
    """Yield content from a GroupCompressBlock on demand."""
1661
1721
    apply_delta_to_source,
1662
1722
    encode_base128_int,
1663
1723
    decode_base128_int,
 
1724
    decode_copy_instruction,
1664
1725
    LinesDeltaIndex,
1665
1726
    )
1666
1727
try: