~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bundle/serializer/v10.py

  • Committer: Aaron Bentley
  • Date: 2007-06-14 20:14:17 UTC
  • mto: (2520.5.2 bzr.mpbundle)
  • mto: This revision was merged to the branch mainline in revision 2631.
  • Revision ID: abentley@panoramicfeedback.com-20070614201417-kqyis466gje6y242
Use less memory when writing bzip-encoded files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from cStringIO import StringIO
2
2
import bz2
3
3
 
 
4
# The number of bytes per base64-encoded line.  We could use less, but it would
 
5
# be ugly
 
6
BASE64_LINE_BYTES = 57
 
7
 
4
8
from bzrlib import (
5
9
    iterablefile,
6
10
    multiparent,
13
17
class BundleWriter(object):
14
18
 
15
19
    def __init__(self, fileobj):
16
 
        self._stringio = StringIO()
17
 
        self._container = pack.ContainerWriter(self._stringio.write)
 
20
        self._container = pack.ContainerWriter(self._write_encoded)
18
21
        self._fileobj = fileobj
 
22
        self._compressor = bz2.BZ2Compressor()
 
23
        self._base64_buffer = ''
19
24
 
20
25
    def begin(self):
21
26
        self._fileobj.write(serializer._get_bundle_header('1.0alpha'))
22
27
        self._fileobj.write('#\n')
23
28
        self._container.begin()
24
29
 
 
30
    def _write_encoded(self, bytes):
 
31
        self._base64_buffer += self._compressor.compress(bytes)
 
32
        if len(self._base64_buffer) >=  BASE64_LINE_BYTES:
 
33
            to_leave = len(self._base64_buffer) % BASE64_LINE_BYTES
 
34
            self._fileobj.write(self._base64_buffer[:-to_leave].encode(
 
35
                'base-64'))
 
36
            self._base64_buffer = self._base64_buffer[-to_leave:]
 
37
 
25
38
    def end(self):
26
39
        self._container.end()
27
 
        encoded = self._stringio.getvalue().encode('bz2').encode('base-64')
28
 
        self._fileobj.write(encoded)
 
40
        tail = self._base64_buffer+self._compressor.flush()
 
41
        self._fileobj.write(tail.encode('base-64'))
29
42
 
30
43
    def add_multiparent_record(self, mp_bytes, parents, repo_kind,
31
44
                               revision_id, file_id):