Implement vacuuming for notes db

This commit is contained in:
Kovid Goyal 2023-08-23 10:08:38 +05:30
parent bdcd3b6203
commit 713f1af61e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 21 additions and 6 deletions

View File

@ -1384,10 +1384,12 @@ class DB:
finally:
self.reopen()
def vacuum(self, include_fts_db):
def vacuum(self, include_fts_db, include_notes_db):
self.execute('VACUUM')
if self.fts_enabled and include_fts_db:
self.fts.vacuum()
if include_notes_db:
self.notes.vacuum()
@property
def user_version(self):

View File

@ -2690,10 +2690,10 @@ class Cache:
return self.backend.dump_and_restore(callback=callback, sql=sql)
@write_api
def vacuum(self, include_fts_db=False):
def vacuum(self, include_fts_db=False, include_notes_db=True):
self.is_doing_rebuild_or_vacuum = True
try:
self.backend.vacuum(include_fts_db)
self.backend.vacuum(include_fts_db, include_notes_db)
finally:
self.is_doing_rebuild_or_vacuum = False

View File

@ -369,3 +369,6 @@ class Notes:
shutil.copyfileobj(src, dest)
add_dir(self.backup_dir)
add_dir(self.resources_dir)
def vacuum(self, conn):
conn.execute('VACUUM notes_db')

View File

@ -52,6 +52,15 @@ class DBCheck(QDialog): # {{{
' depending on the size of the Full text database.'))
la.setWordWrap(True)
l.addWidget(la)
self.notes = n = QCheckBox(_('Also compact the notes database'))
l.addWidget(n)
la = QLabel('<p style="margin-left: 20px; font-style: italic">' + _(
'This can be a very slow and memory intensive operation,'
' depending on the size of the notes database.'))
la.setWordWrap(True)
l.addWidget(la)
l.addStretch(10)
self.bb1 = bb = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel, self)
l.addWidget(bb)
@ -70,6 +79,7 @@ class DBCheck(QDialog): # {{{
l.addStretch(10)
self.resize(self.sizeHint())
self.db = weakref.ref(db.new_api)
self.setMinimumWidth(450)
def start(self):
self.setWindowTitle(_('Vacuuming...'))
@ -77,12 +87,12 @@ class DBCheck(QDialog): # {{{
QApplication.setOverrideCursor(QCursor(Qt.CursorShape.WaitCursor))
self.vacuum_started = True
db = self.db()
t = self.thread = Thread(target=self.vacuum, args=(db, self.fts.isChecked()), daemon=True, name='VacuumDB')
t = self.thread = Thread(target=self.vacuum, args=(db, self.fts.isChecked(), self.notes.isChecked()), daemon=True, name='VacuumDB')
t.start()
def vacuum(self, db, include_fts_db):
def vacuum(self, db, include_fts_db, include_notes_db):
try:
db.vacuum(include_fts_db)
db.vacuum(include_fts_db, include_notes_db)
except Exception as e:
import traceback
self.error = (as_unicode(e), traceback.format_exc())