Perform vacuum if migration is performed
This commit is contained in:
@@ -460,7 +460,7 @@ SELECT
|
||||
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)" };
|
||||
|
||||
@@ -496,6 +496,7 @@ SELECT
|
||||
{56, migrateFromV56},
|
||||
};
|
||||
|
||||
bool migrationPerformed{};
|
||||
{
|
||||
LMS_SCOPED_TRACE_OVERVIEW("Database", "Migration");
|
||||
auto transaction{ session.createWriteTransaction() };
|
||||
@@ -530,7 +531,10 @@ SELECT
|
||||
VersionInfo::get(session).modify()->setVersion(++version);
|
||||
|
||||
LMS_LOG(DB, INFO, "Migration complete to version " << version);
|
||||
migrationPerformed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return migrationPerformed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,6 @@ namespace lms::db
|
||||
|
||||
namespace Migration
|
||||
{
|
||||
void doDbMigration(Session& session);
|
||||
bool doDbMigration(Session& session); // return true if migration was performed
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
auto uniqueTransaction{ createWriteTransaction() };
|
||||
ScanSettings::init(*this);
|
||||
}
|
||||
|
||||
return migrationPerformed;
|
||||
}
|
||||
|
||||
void Session::createIndexesIfNeeded()
|
||||
{
|
||||
LMS_SCOPED_TRACE_OVERVIEW("Database", "IndexCreation");
|
||||
LMS_LOG(DB, INFO, "Creating indexes... This may take a while...");
|
||||
|
||||
auto transaction{ createWriteTransaction() };
|
||||
_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_track_user_backend_idx ON starred_track(track_id,user_id,backend)");
|
||||
|
||||
LMS_LOG(DB, INFO, "Indexes created!");
|
||||
}
|
||||
|
||||
void Session::vacuumIfNeeded()
|
||||
@@ -242,18 +247,21 @@ namespace lms::db
|
||||
|
||||
LMS_LOG(DB, INFO, "page stats: page_count = " << pageCount << ", freelist_count = " << freeListCount);
|
||||
if (freeListCount >= (pageCount / 10))
|
||||
vacuum();
|
||||
}
|
||||
|
||||
void Session::vacuum()
|
||||
{
|
||||
LMS_SCOPED_TRACE_OVERVIEW("Database", "Vacuum");
|
||||
LMS_LOG(DB, INFO, "Performing vacuum... This may take a while...");
|
||||
|
||||
// We manually take a lock here since vacuum cannot be inside a transaction
|
||||
{
|
||||
LMS_SCOPED_TRACE_OVERVIEW("Database", "Vacuum");
|
||||
LMS_LOG(DB, INFO, "Performing vacuum... This may take a while...");
|
||||
|
||||
// We manually take a lock here since vacuum cannot be inside a transaction
|
||||
{
|
||||
std::unique_lock lock{ _db.getMutex() };
|
||||
_db.executeSql("VACUUM");
|
||||
}
|
||||
|
||||
LMS_LOG(DB, INFO, "Vacuum complete!");
|
||||
std::unique_lock lock{ _db.getMutex() };
|
||||
_db.executeSql("VACUUM");
|
||||
}
|
||||
|
||||
LMS_LOG(DB, INFO, "Vacuum complete!");
|
||||
}
|
||||
|
||||
void Session::refreshTracingLoggerStats()
|
||||
|
||||
@@ -92,9 +92,10 @@ namespace lms::db
|
||||
void analyzeEntry(const std::string& entry);
|
||||
|
||||
void prepareTablesIfNeeded(); // need to run only once at startup
|
||||
void migrateIfNeeded();
|
||||
bool migrateSchemaIfNeeded(); // returns true if migration was performed
|
||||
void createIndexesIfNeeded();
|
||||
void vacuumIfNeeded();
|
||||
void vacuum();
|
||||
void refreshTracingLoggerStats();
|
||||
|
||||
// returning a ptr here to ease further wrapping using operator->
|
||||
|
||||
+5
-2
@@ -293,11 +293,14 @@ namespace lms
|
||||
{
|
||||
db::Session session{ database };
|
||||
session.prepareTablesIfNeeded();
|
||||
session.migrateIfNeeded();
|
||||
bool migrationPerformed{ session.migrateSchemaIfNeeded() };
|
||||
session.createIndexesIfNeeded();
|
||||
|
||||
// As this may be quite long, we only do it during startup
|
||||
session.vacuumIfNeeded();
|
||||
if (migrationPerformed)
|
||||
session.vacuum();
|
||||
else
|
||||
session.vacuumIfNeeded();
|
||||
|
||||
// force optimize in case scanner aborted during a large import:
|
||||
// queries may be too slow to even be able to relaunch a scan using the web interface
|
||||
|
||||
Reference in New Issue
Block a user