Simplify _WriteBuffer slightly.

This commit is contained in:
Itamar Turner-Trauring 2022-11-23 10:13:18 -05:00
parent d1deda5fdd
commit fd9e50adf1
2 changed files with 4 additions and 10 deletions

View File

@ -114,17 +114,12 @@ class _WriteBuffer:
_to_write : BytesIO = field(factory=BytesIO)
_written_bytes : int = field(default=0)
def queue_write(self, offset: int, data: bytes) -> bool:
def queue_write(self, data: bytes) -> bool:
"""
Queue a write. If the result is ``False``, no further action is needed
for now. If the result is some ``True``, it's time to call ``flush()``
and do a real write.
Callers of this function are expected to queue the data in order, with
no holes. As such, the offset is technically unnecessary, but is used
to check the inputs. Possibly we should get rid of it.
"""
assert offset == self.get_total_bytes_queued()
self._to_write.write(data)
return len(self._to_write.getbuffer()) >= self._batch_size
@ -290,7 +285,8 @@ class WriteBucketProxy(object):
no holes. As such, the offset is technically unnecessary, but is used
to check the inputs. Possibly we should get rid of it.
"""
if self._write_buffer.queue_write(offset, data):
assert offset == self._write_buffer.get_total_bytes_queued()
if self._write_buffer.queue_write(data):
return self._actually_write()
else:
return defer.succeed(False)

View File

@ -3757,12 +3757,10 @@ class WriteBufferTests(SyncTestCase):
``_WriteBuffer`` coalesces small writes into bigger writes based on
the batch size.
"""
offset = 0
wb = _WriteBuffer(batch_size)
result = b""
for data in small_writes:
should_flush = wb.queue_write(offset, data)
offset += len(data)
should_flush = wb.queue_write(data)
if should_flush:
flushed_offset, flushed_data = wb.flush()
self.assertEqual(flushed_offset, len(result))