Move VACUUM into cleanup()

This commit is contained in:
Lee Miller 2024-10-13 02:16:38 +03:00
parent 1059a853ad
commit f191322429
Signed by: lee.miller
GPG Key ID: 4F97A5EA88F4AB63

View File

@ -32,17 +32,17 @@ 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 +61,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
# conditional vacuum and validity check
if self._deleted > 10000:
logging.info('Doing VACUUM for objects')
cur.execute('VACUUM')
cur.execute(
"INSERT INTO status VALUES ('lastvacuumtime', ?)",
(int(time.time()),))
self._db.commit()
self._deleted = 0
logging.info( logging.info(
'Deleted %s expired objects, %s pending', 'Deleted %s expired objects, %s pending',
cur.rowcount, len(self._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', ?)", (now,))
self._db.commit()
self._deleted = 0
self.lastvacuumtime = now
def filter(self, stream=None, object_type=None, tag=None): def filter(self, stream=None, object_type=None, tag=None):
clauses = [] clauses = []