diff --git a/minode/sql.py b/minode/sql.py index 3557844..cf69faf 100644 --- a/minode/sql.py +++ b/minode/sql.py @@ -32,17 +32,17 @@ class Inventory(): COMMIT; """) self.rowid = len(self) or None - cur = self._db.cursor() - cur.execute("SELECT value FROM status WHERE key='lastvacuumtime'") - now = int(time.time()) try: - vacuumed = cur.fetchone()[0] + self.lastvacuumtime = self._db.execute( + "SELECT value FROM status WHERE key='lastvacuumtime'" + ).fetchone()[0] except TypeError: - pass - else: - if vacuumed < now - 86400: # 24 hours - cur.execute('VACUUM') - cur.execute("INSERT INTO status VALUES ('lastvacuumtime', ?)", (now,)) + self.lastvacuumtime = int(time.time()) + self._db.execute( + "INSERT INTO status VALUES ('lastvacuumtime', ?)", + (self.lastvacuumtime,) + ) + self._db.commit() self._db.row_factory = self.__object @@ -61,24 +61,24 @@ class Inventory(): 'Not cleaning up, %s objects pending', len(self._pending)) return with self._lock: + now = int(time.time()) cur = self._db.execute( - 'DELETE FROM objects WHERE expires < ?', - (int(time.time()) - 3 * 3600,) - ) + 'DELETE FROM objects WHERE expires < ?', (now - 3 * 3600,)) self._db.commit() self._deleted += cur.rowcount - # conditional vacuum and validity check - if self._deleted > 10000: + logging.info( + 'Deleted %s expired objects, %s pending', + cur.rowcount, len(self._pending)) + # conditional vacuum and validity check (TODO) + # every 24 hours or after deleting a lot of items + if self._deleted > 10000 or self.lastvacuumtime < now - 86400: logging.info('Doing VACUUM for objects') cur.execute('VACUUM') cur.execute( - "INSERT INTO status VALUES ('lastvacuumtime', ?)", - (int(time.time()),)) + "INSERT INTO status VALUES ('lastvacuumtime', ?)", (now,)) self._db.commit() self._deleted = 0 - logging.info( - 'Deleted %s expired objects, %s pending', - cur.rowcount, len(self._pending)) + self.lastvacuumtime = now def filter(self, stream=None, object_type=None, tag=None): clauses = []