~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/pack.py

  • Committer: Martin Pool
  • Date: 2009-07-17 10:38:41 UTC
  • mfrom: (4536 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4558.
  • Revision ID: mbp@sourcefrog.net-20090717103841-z35onk04bkiw7zb6
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
159
159
 
160
160
 
161
161
class ReadVFile(object):
162
 
    """Adapt a readv result iterator to a file like protocol."""
 
162
    """Adapt a readv result iterator to a file like protocol.
 
163
    
 
164
    The readv result must support the iterator protocol returning (offset,
 
165
    data_bytes) pairs.
 
166
    """
 
167
 
 
168
    # XXX: This could be a generic transport class, as other code may want to
 
169
    # gradually consume the readv result.
163
170
 
164
171
    def __init__(self, readv_result):
 
172
        """Construct a new ReadVFile wrapper.
 
173
 
 
174
        :seealso: make_readv_reader
 
175
 
 
176
        :param readv_result: the most recent readv result - list or generator
 
177
        """
 
178
        # readv can return a sequence or an iterator, but we require an
 
179
        # iterator to know how much has been consumed.
 
180
        readv_result = iter(readv_result)
165
181
        self.readv_result = readv_result
166
 
        # the most recent readv result block
167
182
        self._string = None
168
183
 
169
184
    def _next(self):
170
185
        if (self._string is None or
171
186
            self._string.tell() == self._string_length):
172
 
            length, data = self.readv_result.next()
 
187
            offset, data = self.readv_result.next()
173
188
            self._string_length = len(data)
174
189
            self._string = StringIO(data)
175
190
 
177
192
        self._next()
178
193
        result = self._string.read(length)
179
194
        if len(result) < length:
180
 
            raise errors.BzrError('request for too much data from a readv hunk.')
 
195
            raise errors.BzrError('wanted %d bytes but next '
 
196
                'hunk only contains %d: %r...' %
 
197
                (length, len(result), result[:20]))
181
198
        return result
182
199
 
183
200
    def readline(self):
185
202
        self._next()
186
203
        result = self._string.readline()
187
204
        if self._string.tell() == self._string_length and result[-1] != '\n':
188
 
            raise errors.BzrError('short readline in the readvfile hunk.')
 
205
            raise errors.BzrError('short readline in the readvfile hunk: %r'
 
206
                % (readline, ))
189
207
        return result
190
208
 
191
209
 
406
424
        # the buffer.
407
425
        last_buffer_length = None
408
426
        cur_buffer_length = len(self._buffer)
409
 
        while cur_buffer_length != last_buffer_length:
 
427
        last_state_handler = None
 
428
        while (cur_buffer_length != last_buffer_length
 
429
               or last_state_handler != self._state_handler):
410
430
            last_buffer_length = cur_buffer_length
 
431
            last_state_handler = self._state_handler
411
432
            self._state_handler()
412
433
            cur_buffer_length = len(self._buffer)
413
434