Perform vacuum if migration is performed

This commit is contained in:
emeric
2024-04-19 12:01:23 +02:00
parent 4432def11f
commit d6b86a9360
5 changed files with 33 additions and 17 deletions
+5 -1
View File
@@ -460,7 +460,7 @@ SELECT
session.getDboSession()->execute("DROP INDEX " + indexName); session.getDboSession()->execute("DROP INDEX " + indexName);
} }
void doDbMigration(Session& session) bool doDbMigration(Session& session)
{ {
static const std::string outdatedMsg{ "Outdated database, please rebuild it (delete the .db file and restart)" }; static const std::string outdatedMsg{ "Outdated database, please rebuild it (delete the .db file and restart)" };
@@ -496,6 +496,7 @@ SELECT
{56, migrateFromV56}, {56, migrateFromV56},
}; };
bool migrationPerformed{};
{ {
LMS_SCOPED_TRACE_OVERVIEW("Database", "Migration"); LMS_SCOPED_TRACE_OVERVIEW("Database", "Migration");
auto transaction{ session.createWriteTransaction() }; auto transaction{ session.createWriteTransaction() };
@@ -530,7 +531,10 @@ SELECT
VersionInfo::get(session).modify()->setVersion(++version); VersionInfo::get(session).modify()->setVersion(++version);
LMS_LOG(DB, INFO, "Migration complete to version " << version); LMS_LOG(DB, INFO, "Migration complete to version " << version);
migrationPerformed = true;
} }
} }
return migrationPerformed;
} }
} }
+1 -1
View File
@@ -52,6 +52,6 @@ namespace lms::db
namespace Migration namespace Migration
{ {
void doDbMigration(Session& session); bool doDbMigration(Session& session); // return true if migration was performed
} }
} }
+11 -3
View File
@@ -143,20 +143,23 @@ namespace lms::db
} }
} }
void Session::migrateIfNeeded() bool Session::migrateSchemaIfNeeded()
{ {
Migration::doDbMigration(*this); const bool migrationPerformed{ Migration::doDbMigration(*this) };
// TODO: move this elsewhere // TODO: move this elsewhere
{ {
auto uniqueTransaction{ createWriteTransaction() }; auto uniqueTransaction{ createWriteTransaction() };
ScanSettings::init(*this); ScanSettings::init(*this);
} }
return migrationPerformed;
} }
void Session::createIndexesIfNeeded() void Session::createIndexesIfNeeded()
{ {
LMS_SCOPED_TRACE_OVERVIEW("Database", "IndexCreation"); LMS_SCOPED_TRACE_OVERVIEW("Database", "IndexCreation");
LMS_LOG(DB, INFO, "Creating indexes... This may take a while...");
auto transaction{ createWriteTransaction() }; auto transaction{ createWriteTransaction() };
_session.execute("CREATE INDEX IF NOT EXISTS artist_id_idx ON artist(id)"); _session.execute("CREATE INDEX IF NOT EXISTS artist_id_idx ON artist(id)");
@@ -227,6 +230,8 @@ namespace lms::db
_session.execute("CREATE INDEX IF NOT EXISTS starred_track_user_backend_idx ON starred_track(user_id,backend)"); _session.execute("CREATE INDEX IF NOT EXISTS starred_track_user_backend_idx ON starred_track(user_id,backend)");
_session.execute("CREATE INDEX IF NOT EXISTS starred_track_track_user_backend_idx ON starred_track(track_id,user_id,backend)"); _session.execute("CREATE INDEX IF NOT EXISTS starred_track_track_user_backend_idx ON starred_track(track_id,user_id,backend)");
LMS_LOG(DB, INFO, "Indexes created!");
} }
void Session::vacuumIfNeeded() void Session::vacuumIfNeeded()
@@ -242,6 +247,10 @@ namespace lms::db
LMS_LOG(DB, INFO, "page stats: page_count = " << pageCount << ", freelist_count = " << freeListCount); LMS_LOG(DB, INFO, "page stats: page_count = " << pageCount << ", freelist_count = " << freeListCount);
if (freeListCount >= (pageCount / 10)) if (freeListCount >= (pageCount / 10))
vacuum();
}
void Session::vacuum()
{ {
LMS_SCOPED_TRACE_OVERVIEW("Database", "Vacuum"); LMS_SCOPED_TRACE_OVERVIEW("Database", "Vacuum");
LMS_LOG(DB, INFO, "Performing vacuum... This may take a while..."); LMS_LOG(DB, INFO, "Performing vacuum... This may take a while...");
@@ -254,7 +263,6 @@ namespace lms::db
LMS_LOG(DB, INFO, "Vacuum complete!"); LMS_LOG(DB, INFO, "Vacuum complete!");
} }
}
void Session::refreshTracingLoggerStats() void Session::refreshTracingLoggerStats()
{ {
@@ -92,9 +92,10 @@ namespace lms::db
void analyzeEntry(const std::string& entry); void analyzeEntry(const std::string& entry);
void prepareTablesIfNeeded(); // need to run only once at startup void prepareTablesIfNeeded(); // need to run only once at startup
void migrateIfNeeded(); bool migrateSchemaIfNeeded(); // returns true if migration was performed
void createIndexesIfNeeded(); void createIndexesIfNeeded();
void vacuumIfNeeded(); void vacuumIfNeeded();
void vacuum();
void refreshTracingLoggerStats(); void refreshTracingLoggerStats();
// returning a ptr here to ease further wrapping using operator-> // returning a ptr here to ease further wrapping using operator->
+4 -1
View File
@@ -293,10 +293,13 @@ namespace lms
{ {
db::Session session{ database }; db::Session session{ database };
session.prepareTablesIfNeeded(); session.prepareTablesIfNeeded();
session.migrateIfNeeded(); bool migrationPerformed{ session.migrateSchemaIfNeeded() };
session.createIndexesIfNeeded(); session.createIndexesIfNeeded();
// As this may be quite long, we only do it during startup // As this may be quite long, we only do it during startup
if (migrationPerformed)
session.vacuum();
else
session.vacuumIfNeeded(); session.vacuumIfNeeded();
// force optimize in case scanner aborted during a large import: // force optimize in case scanner aborted during a large import: