~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tuned_gzip.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-06-22 17:11:20 UTC
  • mfrom: (4398.8.10 1.16-commit-fulltext)
  • Revision ID: pqm@pqm.ubuntu.com-20090622171120-fuxez9ylfqpxynqn
(jam) Add VF._add_text and reduce memory overhead during commit (see
        bug #109114)

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
    width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
53
53
    crc32=zlib.crc32):
54
54
    """Create a gzip file containing bytes and return its content."""
 
55
    return chunks_to_gzip([bytes])
 
56
 
 
57
 
 
58
def chunks_to_gzip(chunks, factory=zlib.compressobj,
 
59
    level=zlib.Z_DEFAULT_COMPRESSION, method=zlib.DEFLATED,
 
60
    width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
 
61
    crc32=zlib.crc32):
 
62
    """Create a gzip file containing chunks and return its content.
 
63
 
 
64
    :param chunks: An iterable of strings. Each string can have arbitrary
 
65
        layout.
 
66
    """
55
67
    result = [
56
68
        '\037\213'  # self.fileobj.write('\037\213')  # magic header
57
69
        '\010'      # self.fileobj.write('\010')      # compression method
69
81
    # using a compressobj avoids a small header and trailer that the compress()
70
82
    # utility function adds.
71
83
    compress = factory(level, method, width, mem, 0)
72
 
    result.append(compress.compress(bytes))
 
84
    crc = 0
 
85
    total_len = 0
 
86
    for chunk in chunks:
 
87
        crc = crc32(chunk, crc)
 
88
        total_len += len(chunk)
 
89
        zbytes = compress.compress(chunk)
 
90
        if zbytes:
 
91
            result.append(zbytes)
73
92
    result.append(compress.flush())
74
 
    result.append(struct.pack("<L", LOWU32(crc32(bytes))))
75
93
    # size may exceed 2GB, or even 4GB
76
 
    result.append(struct.pack("<L", LOWU32(len(bytes))))
 
94
    result.append(struct.pack("<LL", LOWU32(crc), LOWU32(total_len)))
77
95
    return ''.join(result)
78
96
 
79
97