From dd73eb6273fbdb78c0347c9e5f47b16c20b2c678 Mon Sep 17 00:00:00 2001 From: "guido.dassori@gmail.com" Date: Sat, 12 Jun 2021 10:24:00 +0200 Subject: [PATCH] explicit flush --- aiodiskdb/aiodiskdb.py | 6 ++++++ test/test_read_write.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/aiodiskdb/aiodiskdb.py b/aiodiskdb/aiodiskdb.py index 3fff3c1..6a4735b 100644 --- a/aiodiskdb/aiodiskdb.py +++ b/aiodiskdb/aiodiskdb.py @@ -321,6 +321,12 @@ def _is_file_header(self, data: bytes) -> bool: return False return True + @ensure_async_lock(LockType.TRANSACTION) + async def flush(self): + if not self._buffers[-1].data: + return + return await self._flush_buffer_no_transaction_lock() + @ensure_async_lock(LockType.TRANSACTION) async def _flush_buffer(self): """ diff --git a/test/test_read_write.py b/test/test_read_write.py index 37c333b..b643874 100644 --- a/test/test_read_write.py +++ b/test/test_read_write.py @@ -3,6 +3,20 @@ from test import AioDiskDBTestCase, run_test_db +class TestFlush(AioDiskDBTestCase): + @run_test_db + async def test(self): + item_location = await self.sut.add(b'test_1') + self.assertEqual( + ItemLocation(0, 0, 6), + item_location + ) + await self.sut.flush() + self.assertEqual(self.sut._buffers[-1].data, b'') + read1 = await self.sut.read(item_location) + self.assertEqual(b'test_1', read1) + + class TestReadWriteCached(AioDiskDBTestCase): @run_test_db async def test(self):