From 765c999dfb2a423b6713dfe9edf637269213dd10 Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Wed, 11 Sep 2024 02:09:23 +0300 Subject: [PATCH] Fix the status table, vacuum on init every 24 hours --- minode/sql.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/minode/sql.py b/minode/sql.py index eaefc36..092881e 100644 --- a/minode/sql.py +++ b/minode/sql.py @@ -18,14 +18,27 @@ class Inventory(): ) self._db.executescript(""" BEGIN; - CREATE TABLE IF NOT EXISTS status (vacuumtime integer); + CREATE TABLE IF NOT EXISTS status + (key text, value integer, UNIQUE(key) ON CONFLICT REPLACE); + INSERT INTO status VALUES ('version', 1); CREATE TABLE IF NOT EXISTS objects (vector unique, expires integer, type integer, version integer, stream integer, tag, data, offset integer); COMMIT; """) self.rowid = len(self) or None - # self._cur.execute('INSERT INTO status VALUES (?)', int(time.time())) + cur = self._db.cursor() + cur.execute("SELECT value FROM status WHERE key='lastvacuumtime'") + now = int(time.time()) + try: + vacuumed = cur.fetchone()[0] + except TypeError: + pass + else: + if vacuumed < now - 86400: # 24 hours + cur.execute('VACUUM') + cur.execute("INSERT INTO status VALUES ('lastvacuumtime', ?)", (now,)) + self._db.commit() def __objects(self, cur): return (