1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-04-05 08:32:42 +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_lasttrkid( 0 )
{
bool schemaUpdated = false;
int version = getDatabaseVersion( dbname );
QTime t;
t.start();
if ( version > 0 && version != CURRENT_SCHEMA_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 );
{
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 );
}
bool schemaUpdated = openDatabase( dbname );
tDebug( LOGVERBOSE ) << "Opened database:" << t.elapsed();
TomahawkSqlQuery query = newquery();
query.exec( "SELECT v FROM settings WHERE k='dbid'" );
if( query.next() )
if ( query.next() )
{
m_dbid = query.value( 0 ).toString();
}
@ -107,14 +69,17 @@ DatabaseImpl::DatabaseImpl( const QString& dbname, Database* parent )
tLog() << "Database ID:" << m_dbid;
// make sqlite behave how we want:
query.exec( "PRAGMA auto_vacuum = FULL" );
query.exec( "PRAGMA synchronous = ON" );
query.exec( "PRAGMA foreign_keys = ON" );
//query.exec( "PRAGMA temp_store = MEMORY" );
tDebug( LOGVERBOSE ) << "Tweaked db pragmas:" << t.elapsed();
// in case of unclean shutdown last time:
query.exec( "UPDATE source SET isonline = 'false'" );
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;
QString sql( get_tomahawk_sql() );
QStringList statements = sql.split( ";", QString::SkipEmptyParts );
db.transaction();
m_db.transaction();
foreach ( const QString& sl, statements )
{
@ -154,13 +119,13 @@ DatabaseImpl::updateSchema( int oldVersion )
query.exec( s );
}
db.commit();
m_db.commit();
return true;
}
else // update in place! run the proper upgrade script
{
int cur = oldVersion;
db.transaction();
m_db.transaction();
while ( cur < CURRENT_SCHEMA_VERSION )
{
cur++;
@ -186,7 +151,7 @@ DatabaseImpl::updateSchema( int oldVersion )
q.exec( clean );
}
}
db.commit();
m_db.commit();
tLog() << "DB Upgrade successful!";
return true;
}
@ -619,9 +584,10 @@ DatabaseImpl::resultFromHint( const Tomahawk::query_ptr& origquery )
}
int
DatabaseImpl::getDatabaseVersion( const QString& dbname )
bool
DatabaseImpl::openDatabase( const QString& dbname )
{
bool schemaUpdated = false;
int version = -1;
{
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE", "tomahawk" );
@ -639,9 +605,41 @@ DatabaseImpl::getDatabaseVersion( const QString& dbname )
version = qry.value( 0 ).toInt();
tLog() << "Database schema of" << dbname << "is" << version;
}
if ( version < 0 || version == CURRENT_SCHEMA_VERSION )
m_db = db;
}
QSqlDatabase::removeDatabase( "tomahawk" );
if ( version > 0 && version != CURRENT_SCHEMA_VERSION )
{
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;
public:
static int getDatabaseVersion( const QString& dbname );
DatabaseImpl( const QString& dbname, Database* parent = 0 );
~DatabaseImpl();
TomahawkSqlQuery newquery() { return TomahawkSqlQuery( db ); }
QSqlDatabase& database() { return db; }
bool openDatabase( const QString& dbname );
TomahawkSqlQuery newquery() { return TomahawkSqlQuery( m_db ); }
QSqlDatabase& database() { return m_db; }
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 m_ready;
QSqlDatabase db;
QSqlDatabase m_db;
QString m_lastart, m_lastalb, m_lasttrk;
int m_lastartid, m_lastalbid, m_lasttrkid;

View File

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