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 (