Move VACUUM into cleanup()

This commit is contained in:
Lee Miller 2024-10-10 21:04:15 +03:00
parent bc5576039c
commit af7b0ff778
Signed by: lee.miller
GPG Key ID: 4F97A5EA88F4AB63

View File

@ -32,17 +32,16 @@ class Inventory():
COMMIT; COMMIT;
""") """)
self.rowid = len(self) or None self.rowid = len(self) or None
cur = self._db.cursor()
cur.execute("SELECT value FROM status WHERE key='lastvacuumtime'")
now = int(time.time())
try: try:
vacuumed = cur.fetchone()[0] self.lastvacuumtime = self._db.execute(
"SELECT value FROM status WHERE key='lastvacuumtime'"
).fetchone()[0]
except TypeError: except TypeError:
pass self.lastvacuumtime = int(time.time())
else: self._db.execute(
if vacuumed < now - 86400: # 24 hours "INSERT INTO status VALUES ('lastvacuumtime', ?)",
cur.execute('VACUUM') (self.lastvacuumtime,)
cur.execute("INSERT INTO status VALUES ('lastvacuumtime', ?)", (now,)) )
self._db.commit() self._db.commit()
self._db.row_factory = self.__object self._db.row_factory = self.__object
@ -61,24 +60,24 @@ class Inventory():
'Not cleaning up, %s objects pending', len(self._pending)) 'Not cleaning up, %s objects pending', len(self._pending))
return return
with self._lock: with self._lock:
now = int(time.time())
cur = self._db.execute( cur = self._db.execute(
'DELETE FROM objects WHERE expires < ?', 'DELETE FROM objects WHERE expires < ?', (now - 3 * 3600,))
(int(time.time()) - 3 * 3600,)
)
self._db.commit() self._db.commit()
self._deleted += cur.rowcount self._deleted += cur.rowcount
logging.info(
'Deleted %s expired objects, %s pending',
cur.rowcount, len(self._pending))
# conditional vacuum and validity check # conditional vacuum and validity check
if self._deleted > 10000: # 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') logging.info('Doing VACUUM for objects')
cur.execute('VACUUM') cur.execute('VACUUM')
cur.execute( cur.execute(
"INSERT INTO status VALUES ('lastvacuumtime', ?)", "INSERT INTO status VALUES ('lastvacuumtime', ?)", (now,))
(int(time.time()),))
self._db.commit() self._db.commit()
self.lastvacuumtime = now
self._deleted = 0 self._deleted = 0
logging.info(
'Deleted %s expired objects, %s pending',
cur.rowcount, len(self._pending))
def filter(self, stream=None, object_type=None, tag=None): def filter(self, stream=None, object_type=None, tag=None):
clauses = [] clauses = []