Fix the status table, vacuum on init every 24 hours

This commit is contained in:
Lee Miller 2024-09-11 02:09:23 +03:00
parent 7261103c87
commit 765c999dfb
Signed by: lee.miller
GPG Key ID: 4F97A5EA88F4AB63

View File

@ -18,14 +18,27 @@ class Inventory():
) )
self._db.executescript(""" self._db.executescript("""
BEGIN; 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 CREATE TABLE IF NOT EXISTS objects
(vector unique, expires integer, type integer, version integer, (vector unique, expires integer, type integer, version integer,
stream integer, tag, data, offset integer); stream integer, tag, data, offset integer);
COMMIT; COMMIT;
""") """)
self.rowid = len(self) or None 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): def __objects(self, cur):
return ( return (