From 228fd90817a5c5f3a9dc36fee8b0fd08af41a334 Mon Sep 17 00:00:00 2001 From: Peter Wang Date: Fri, 22 Apr 2022 20:21:28 +0800 Subject: [PATCH] Add `shrink_disk_usage` for sqlite3 base queues Performs a ``VACUUM`` against the sqlite, and rebuild the database file, this usually takes long time and frees a lot of disk space after `get()` fixes #176 --- persistqueue/sqlackqueue.py | 5 ----- persistqueue/sqlbase.py | 5 +++++ persistqueue/tests/test_sqlackqueue.py | 1 + persistqueue/tests/test_sqlqueue.py | 1 + 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/persistqueue/sqlackqueue.py b/persistqueue/sqlackqueue.py index 80b0197..877ebad 100644 --- a/persistqueue/sqlackqueue.py +++ b/persistqueue/sqlackqueue.py @@ -167,11 +167,6 @@ def clear_acked_data( ) return sql, AckStatus.acked - @sqlbase.with_conditional_transaction - def shrink_disk_usage(self): - sql = """VACUUM""" - return sql, () - @property def _sql_mark_ack_status(self): return self._SQL_MARK_ACK_UPDATE.format( diff --git a/persistqueue/sqlbase.py b/persistqueue/sqlbase.py index 607b3cb..84dba02 100644 --- a/persistqueue/sqlbase.py +++ b/persistqueue/sqlbase.py @@ -212,6 +212,11 @@ def queue(self): datarows.append(item) return datarows + @with_conditional_transaction + def shrink_disk_usage(self): + sql = """VACUUM""" + return sql, () + @property def size(self): return self.total diff --git a/persistqueue/tests/test_sqlackqueue.py b/persistqueue/tests/test_sqlackqueue.py index e455da6..ee81f30 100644 --- a/persistqueue/tests/test_sqlackqueue.py +++ b/persistqueue/tests/test_sqlackqueue.py @@ -75,6 +75,7 @@ def test_open_close_1000(self): # assert adding another one still works q.put('foobar') data = q.get() + q.shrink_disk_usage() self.assertEqual('foobar', data) def test_random_read_write(self): diff --git a/persistqueue/tests/test_sqlqueue.py b/persistqueue/tests/test_sqlqueue.py index 87cfb90..4f5452a 100644 --- a/persistqueue/tests/test_sqlqueue.py +++ b/persistqueue/tests/test_sqlqueue.py @@ -75,6 +75,7 @@ def test_open_close_1000(self): # assert adding another one still works q.put('foobar') data = q.get() + q.shrink_disk_usage() self.assertEqual('foobar', data) def test_random_read_write(self):