mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-04-27 14:30:19 +00:00
Simplify _WriteBuffer slightly.
This commit is contained in:
parent
d1deda5fdd
commit
fd9e50adf1
@ -114,17 +114,12 @@ class _WriteBuffer:
|
|||||||
_to_write : BytesIO = field(factory=BytesIO)
|
_to_write : BytesIO = field(factory=BytesIO)
|
||||||
_written_bytes : int = field(default=0)
|
_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
|
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()``
|
for now. If the result is some ``True``, it's time to call ``flush()``
|
||||||
and do a real write.
|
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)
|
self._to_write.write(data)
|
||||||
return len(self._to_write.getbuffer()) >= self._batch_size
|
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
|
no holes. As such, the offset is technically unnecessary, but is used
|
||||||
to check the inputs. Possibly we should get rid of it.
|
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()
|
return self._actually_write()
|
||||||
else:
|
else:
|
||||||
return defer.succeed(False)
|
return defer.succeed(False)
|
||||||
|
@ -3757,12 +3757,10 @@ class WriteBufferTests(SyncTestCase):
|
|||||||
``_WriteBuffer`` coalesces small writes into bigger writes based on
|
``_WriteBuffer`` coalesces small writes into bigger writes based on
|
||||||
the batch size.
|
the batch size.
|
||||||
"""
|
"""
|
||||||
offset = 0
|
|
||||||
wb = _WriteBuffer(batch_size)
|
wb = _WriteBuffer(batch_size)
|
||||||
result = b""
|
result = b""
|
||||||
for data in small_writes:
|
for data in small_writes:
|
||||||
should_flush = wb.queue_write(offset, data)
|
should_flush = wb.queue_write(data)
|
||||||
offset += len(data)
|
|
||||||
if should_flush:
|
if should_flush:
|
||||||
flushed_offset, flushed_data = wb.flush()
|
flushed_offset, flushed_data = wb.flush()
|
||||||
self.assertEqual(flushed_offset, len(result))
|
self.assertEqual(flushed_offset, len(result))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user