~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/knit.py

  • Committer: Robert Collins
  • Date: 2007-09-13 03:16:07 UTC
  • mto: (2839.1.1 ianc-integration2)
  • mto: This revision was merged to the branch mainline in revision 2841.
  • Revision ID: robertc@robertcollins.net-20070913031607-fit1cj291o8yu1z2
* New helper ``bzrlib.tuned_gzip.bytes_to_gzip`` which takes a byte string
  and returns a gzipped version of the same. This is used to avoid a bunch
  of api friction during adding of knit hunks. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
100
100
    RevisionNotPresent,
101
101
    RevisionAlreadyPresent,
102
102
    )
103
 
from bzrlib.tuned_gzip import GzipFile
 
103
from bzrlib.tuned_gzip import GzipFile, bytes_to_gzip
104
104
from bzrlib.osutils import (
105
105
    contains_whitespace,
106
106
    contains_linebreaks,
1983
1983
        
1984
1984
        :return: (len, a StringIO instance with the raw data ready to read.)
1985
1985
        """
1986
 
        sio = StringIO()
1987
 
        data_file = GzipFile(None, mode='wb', fileobj=sio,
1988
 
            compresslevel=Z_DEFAULT_COMPRESSION)
1989
 
 
1990
 
        assert isinstance(version_id, str)
1991
 
        data_file.writelines(chain(
 
1986
        bytes = (''.join(chain(
1992
1987
            ["version %s %d %s\n" % (version_id,
1993
1988
                                     len(lines),
1994
1989
                                     digest)],
1995
1990
            lines,
1996
 
            ["end %s\n" % version_id]))
1997
 
        data_file.close()
1998
 
        length= sio.tell()
1999
 
 
2000
 
        sio.seek(0)
2001
 
        return length, sio
 
1991
            ["end %s\n" % version_id])))
 
1992
        assert bytes.__class__ == str
 
1993
        compressed_bytes = bytes_to_gzip(bytes)
 
1994
        return len(compressed_bytes), compressed_bytes
2002
1995
 
2003
1996
    def add_raw_records(self, sizes, raw_data):
2004
1997
        """Append a prepared record to the data file.
2016
2009
        
2017
2010
        Returns index data for retrieving it later, as per add_raw_records.
2018
2011
        """
2019
 
        size, sio = self._record_to_data(version_id, digest, lines)
2020
 
        result = self.add_raw_records([size], sio.getvalue())
 
2012
        size, bytes = self._record_to_data(version_id, digest, lines)
 
2013
        result = self.add_raw_records([size], bytes)
2021
2014
        if self._do_cache:
2022
 
            self._cache[version_id] = sio.getvalue()
 
2015
            self._cache[version_id] = bytes
2023
2016
        return result[0]
2024
2017
 
2025
2018
    def _parse_record_header(self, version_id, raw_data):