~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tuned_gzip.py

Merge doc updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
# Written by Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
17
17
 
18
18
"""Bzrlib specific gzip tunings. We plan to feed these to the upstream gzip."""
19
19
 
 
20
from cStringIO import StringIO
 
21
 
20
22
# make GzipFile faster:
21
23
import gzip
22
24
from gzip import U32, LOWU32, FEXTRA, FCOMMENT, FNAME, FHCRC
63
65
        self.extrasize += len_data
64
66
        self.size += len_data
65
67
 
 
68
    def _write_gzip_header(self):
 
69
        """A tuned version of gzip._write_gzip_header
 
70
 
 
71
        We have some extra constrains that plain Gzip does not.
 
72
        1) We want to write the whole blob at once. rather than multiple 
 
73
           calls to fileobj.write().
 
74
        2) We never have a filename
 
75
        3) We don't care about the time
 
76
        """
 
77
        self.fileobj.write(
 
78
           '\037\213'   # self.fileobj.write('\037\213')  # magic header
 
79
            '\010'      # self.fileobj.write('\010')      # compression method
 
80
                        # fname = self.filename[:-3]
 
81
                        # flags = 0
 
82
                        # if fname:
 
83
                        #     flags = FNAME
 
84
            '\x00'      # self.fileobj.write(chr(flags))
 
85
            '\0\0\0\0'  # write32u(self.fileobj, long(time.time()))
 
86
            '\002'      # self.fileobj.write('\002')
 
87
            '\377'      # self.fileobj.write('\377')
 
88
                        # if fname:
 
89
            ''          #     self.fileobj.write(fname + '\000')
 
90
            )
 
91
 
66
92
    def _read(self, size=1024):
67
93
        # various optimisations:
68
94
        # reduces lsprof count from 2500 to 
139
165
        4168 in 200
140
166
        """
141
167
        # We've read to the end of the file, so we should have 8 bytes of 
142
 
        # unused data in the decompressor. If we dont, there is a corrupt file.
 
168
        # unused data in the decompressor. If we don't, there is a corrupt file.
143
169
        # We use these 8 bytes to calculate the CRC and the recorded file size.
144
170
        # We then check the that the computed CRC and size of the
145
171
        # uncompressed data matches the stored values.  Note that the size
255
281
        # to :
256
282
        # 4168 calls in 417.
257
283
        # Negative numbers result in reading all the lines
258
 
        if sizehint <= 0:
259
 
            sizehint = -1
260
 
        content = self.read(sizehint)
261
 
        return bzrlib.osutils.split_lines(content)
 
284
        
 
285
        # python's gzip routine uses sizehint. This is a more efficient way
 
286
        # than python uses to honor it. But it is even more efficient to
 
287
        # just read the entire thing and use cStringIO to split into lines.
 
288
        # if sizehint <= 0:
 
289
        #     sizehint = -1
 
290
        # content = self.read(sizehint)
 
291
        # return bzrlib.osutils.split_lines(content)
 
292
        content = StringIO(self.read(-1))
 
293
        return content.readlines()
262
294
 
263
295
    def _unread(self, buf, len_buf=None):
264
296
        """tuned to remove unneeded len calls.