1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-11 16:44:05 +02:00

* Only open database once. Might speed up loading a bit.

This commit is contained in:
Christian Muehlhaeuser
2011-11-23 11:37:36 +01:00
parent 86860b6da3
commit 8cff1f7b76
3 changed files with 55 additions and 56 deletions

View File

@@ -49,53 +49,15 @@ DatabaseImpl::DatabaseImpl( const QString& dbname, Database* parent )
, m_lastalbid( 0 ) , m_lastalbid( 0 )
, m_lasttrkid( 0 ) , m_lasttrkid( 0 )
{ {
bool schemaUpdated = false; QTime t;
int version = getDatabaseVersion( dbname ); t.start();
if ( version > 0 && version != CURRENT_SCHEMA_VERSION ) bool schemaUpdated = openDatabase( dbname );
{ tDebug( LOGVERBOSE ) << "Opened database:" << t.elapsed();
QString newname = QString( "%1.v%2" ).arg( dbname ).arg( version );
tLog() << endl << "****************************" << endl;
tLog() << "Schema version too old: " << version << ". Current version is:" << CURRENT_SCHEMA_VERSION;
tLog() << "Moving" << dbname << newname;
tLog() << "If the migration fails, you can recover your DB by copying" << newname << "back to" << dbname;
tLog() << endl << "****************************" << endl;
QFile::copy( dbname, newname );
{
db = QSqlDatabase::addDatabase( "QSQLITE", "tomahawk" );
db.setDatabaseName( dbname );
if( !db.open() )
throw "db moving failed";
TomahawkSqlQuery query = newquery();
query.exec( "PRAGMA auto_vacuum = FULL" );
schemaUpdated = updateSchema( version );
if ( !schemaUpdated )
{
Q_ASSERT( false );
QTimer::singleShot( 0, qApp, SLOT( quit() ) );
}
}
}
else
{
db = QSqlDatabase::addDatabase( "QSQLITE", "tomahawk" );
db.setDatabaseName( dbname );
if ( !db.open() )
{
tLog() << "Failed to open database" << dbname;
throw "failed to open db"; // TODO
}
if ( version < 0 )
schemaUpdated = updateSchema( 0 );
}
TomahawkSqlQuery query = newquery(); TomahawkSqlQuery query = newquery();
query.exec( "SELECT v FROM settings WHERE k='dbid'" ); query.exec( "SELECT v FROM settings WHERE k='dbid'" );
if( query.next() ) if ( query.next() )
{ {
m_dbid = query.value( 0 ).toString(); m_dbid = query.value( 0 ).toString();
} }
@@ -107,14 +69,17 @@ DatabaseImpl::DatabaseImpl( const QString& dbname, Database* parent )
tLog() << "Database ID:" << m_dbid; tLog() << "Database ID:" << m_dbid;
// make sqlite behave how we want: // make sqlite behave how we want:
query.exec( "PRAGMA auto_vacuum = FULL" );
query.exec( "PRAGMA synchronous = ON" ); query.exec( "PRAGMA synchronous = ON" );
query.exec( "PRAGMA foreign_keys = ON" ); query.exec( "PRAGMA foreign_keys = ON" );
//query.exec( "PRAGMA temp_store = MEMORY" ); //query.exec( "PRAGMA temp_store = MEMORY" );
tDebug( LOGVERBOSE ) << "Tweaked db pragmas:" << t.elapsed();
// in case of unclean shutdown last time: // in case of unclean shutdown last time:
query.exec( "UPDATE source SET isonline = 'false'" ); query.exec( "UPDATE source SET isonline = 'false'" );
m_fuzzyIndex = new FuzzyIndex( *this, schemaUpdated ); m_fuzzyIndex = new FuzzyIndex( *this, schemaUpdated );
tDebug( LOGVERBOSE ) << "Loaded index:" << t.elapsed();
} }
@@ -141,7 +106,7 @@ DatabaseImpl::updateSchema( int oldVersion )
tLog() << "Create tables... old version is" << oldVersion; tLog() << "Create tables... old version is" << oldVersion;
QString sql( get_tomahawk_sql() ); QString sql( get_tomahawk_sql() );
QStringList statements = sql.split( ";", QString::SkipEmptyParts ); QStringList statements = sql.split( ";", QString::SkipEmptyParts );
db.transaction(); m_db.transaction();
foreach ( const QString& sl, statements ) foreach ( const QString& sl, statements )
{ {
@@ -154,13 +119,13 @@ DatabaseImpl::updateSchema( int oldVersion )
query.exec( s ); query.exec( s );
} }
db.commit(); m_db.commit();
return true; return true;
} }
else // update in place! run the proper upgrade script else // update in place! run the proper upgrade script
{ {
int cur = oldVersion; int cur = oldVersion;
db.transaction(); m_db.transaction();
while ( cur < CURRENT_SCHEMA_VERSION ) while ( cur < CURRENT_SCHEMA_VERSION )
{ {
cur++; cur++;
@@ -186,7 +151,7 @@ DatabaseImpl::updateSchema( int oldVersion )
q.exec( clean ); q.exec( clean );
} }
} }
db.commit(); m_db.commit();
tLog() << "DB Upgrade successful!"; tLog() << "DB Upgrade successful!";
return true; return true;
} }
@@ -619,9 +584,10 @@ DatabaseImpl::resultFromHint( const Tomahawk::query_ptr& origquery )
} }
int bool
DatabaseImpl::getDatabaseVersion( const QString& dbname ) DatabaseImpl::openDatabase( const QString& dbname )
{ {
bool schemaUpdated = false;
int version = -1; int version = -1;
{ {
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE", "tomahawk" ); QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE", "tomahawk" );
@@ -639,9 +605,41 @@ DatabaseImpl::getDatabaseVersion( const QString& dbname )
version = qry.value( 0 ).toInt(); version = qry.value( 0 ).toInt();
tLog() << "Database schema of" << dbname << "is" << version; tLog() << "Database schema of" << dbname << "is" << version;
} }
if ( version < 0 || version == CURRENT_SCHEMA_VERSION )
m_db = db;
} }
if ( version > 0 && version != CURRENT_SCHEMA_VERSION )
{
QSqlDatabase::removeDatabase( "tomahawk" ); QSqlDatabase::removeDatabase( "tomahawk" );
return version; QString newname = QString( "%1.v%2" ).arg( dbname ).arg( version );
tLog() << endl << "****************************" << endl;
tLog() << "Schema version too old: " << version << ". Current version is:" << CURRENT_SCHEMA_VERSION;
tLog() << "Moving" << dbname << newname;
tLog() << "If the migration fails, you can recover your DB by copying" << newname << "back to" << dbname;
tLog() << endl << "****************************" << endl;
QFile::copy( dbname, newname );
{
m_db = QSqlDatabase::addDatabase( "QSQLITE", "tomahawk" );
m_db.setDatabaseName( dbname );
if ( !m_db.open() )
throw "db moving failed";
schemaUpdated = updateSchema( version );
if ( !schemaUpdated )
{
Q_ASSERT( false );
QTimer::singleShot( 0, qApp, SLOT( quit() ) );
}
}
}
else if ( version < 0 )
{
schemaUpdated = updateSchema( 0 );
}
return schemaUpdated;
} }

View File

@@ -44,13 +44,13 @@ friend class FuzzyIndex;
friend class DatabaseCommand_UpdateSearchIndex; friend class DatabaseCommand_UpdateSearchIndex;
public: public:
static int getDatabaseVersion( const QString& dbname );
DatabaseImpl( const QString& dbname, Database* parent = 0 ); DatabaseImpl( const QString& dbname, Database* parent = 0 );
~DatabaseImpl(); ~DatabaseImpl();
TomahawkSqlQuery newquery() { return TomahawkSqlQuery( db ); } bool openDatabase( const QString& dbname );
QSqlDatabase& database() { return db; }
TomahawkSqlQuery newquery() { return TomahawkSqlQuery( m_db ); }
QSqlDatabase& database() { return m_db; }
int artistId( const QString& name_orig, bool autoCreate ); int artistId( const QString& name_orig, bool autoCreate );
int trackId( int artistid, const QString& name_orig, bool autoCreate ); int trackId( int artistid, const QString& name_orig, bool autoCreate );
@@ -86,7 +86,7 @@ private:
bool updateSchema( int oldVersion ); bool updateSchema( int oldVersion );
bool m_ready; bool m_ready;
QSqlDatabase db; QSqlDatabase m_db;
QString m_lastart, m_lastalb, m_lasttrk; QString m_lastart, m_lastalb, m_lasttrk;
int m_lastartid, m_lastalbid, m_lasttrkid; int m_lastartid, m_lastalbid, m_lasttrkid;

View File

@@ -47,6 +47,7 @@ FuzzyIndex::FuzzyIndex( DatabaseImpl& db, bool wipeIndex )
if ( wipeIndex ) if ( wipeIndex )
{ {
tLog( LOGVERBOSE ) << "Wiping fuzzy index...";
beginIndexing(); beginIndexing();
endIndexing(); endIndexing();
} }