mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-06 14:16:32 +02:00
Experimental: Re-implement asynchronous artist/album ID.
No more boost::thread usage, using QFutureInterface instead. Now we keep the same model as before but use the undocumented API of QFutureInterface
This commit is contained in:
@@ -719,6 +719,27 @@ TomahawkApp::loadUrl( const QString& url )
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
TomahawkApp::notify( QObject *receiver, QEvent *e )
|
||||
{
|
||||
try
|
||||
{
|
||||
return TOMAHAWK_APPLICATION::notify( receiver, e );
|
||||
}
|
||||
catch ( const std::exception& e )
|
||||
{
|
||||
qWarning( "TomahawkApp::notify caught a std exception in a Qt event handler: " );
|
||||
qFatal( e.what() );
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
qFatal( "TomahawkApp::notify caught a non-std-exception from a Qt event handler. Aborting." );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
TomahawkApp::instanceStarted( KDSingleApplicationGuard::Instance instance )
|
||||
{
|
||||
|
@@ -99,6 +99,9 @@ public:
|
||||
|
||||
bool isTomahawkLoaded() const { return m_loaded; }
|
||||
|
||||
// reimplemented from QApplication/QCoreApplication
|
||||
virtual bool notify( QObject* receiver, QEvent* e );
|
||||
|
||||
signals:
|
||||
void tomahawkLoaded();
|
||||
|
||||
|
@@ -23,13 +23,22 @@
|
||||
#include "AlbumPlaylistInterface.h"
|
||||
#include "database/Database.h"
|
||||
#include "database/DatabaseImpl.h"
|
||||
#include "database/IdThreadWorker.h"
|
||||
#include "Query.h"
|
||||
#include "Source.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <QReadWriteLock>
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
QHash< QString, album_ptr > Album::s_albumsByName = QHash< QString, album_ptr >();
|
||||
QHash< unsigned int, album_ptr > Album::s_albumsById = QHash< unsigned int, album_ptr >();
|
||||
|
||||
static QMutex s_nameCacheMutex;
|
||||
static QMutex s_idCacheMutex;
|
||||
static QReadWriteLock s_idMutex;
|
||||
|
||||
Album::~Album()
|
||||
{
|
||||
@@ -40,6 +49,12 @@ Album::~Album()
|
||||
#endif
|
||||
}
|
||||
|
||||
inline QString
|
||||
albumCacheKey( const Tomahawk::artist_ptr& artist, const QString& albumName )
|
||||
{
|
||||
return QString( "%1\t\t%2" ).arg( artist->name() ).arg( albumName );
|
||||
}
|
||||
|
||||
|
||||
album_ptr
|
||||
Album::get( const Tomahawk::artist_ptr& artist, const QString& name, bool autoCreate )
|
||||
@@ -47,11 +62,22 @@ Album::get( const Tomahawk::artist_ptr& artist, const QString& name, bool autoCr
|
||||
if ( !Database::instance() || !Database::instance()->impl() )
|
||||
return album_ptr();
|
||||
|
||||
int albid = Database::instance()->impl()->albumId( artist->id(), name, autoCreate );
|
||||
if ( albid < 1 && autoCreate )
|
||||
return album_ptr();
|
||||
QMutexLocker l( &s_nameCacheMutex );
|
||||
|
||||
return Album::get( albid, name, artist );
|
||||
const QString key = albumCacheKey( artist, name );
|
||||
if ( s_albumsByName.contains( key ) )
|
||||
{
|
||||
return s_albumsByName[ key ];
|
||||
}
|
||||
|
||||
qDebug() << "LOOKING UP ALBUM:" << artist->name() << name;
|
||||
album_ptr album = album_ptr( new Album( name, artist ) );
|
||||
album->setWeakRef( album.toWeakRef() );
|
||||
album->loadId( autoCreate );
|
||||
|
||||
s_albumsByName[ key ] = album;
|
||||
|
||||
return album;
|
||||
}
|
||||
|
||||
|
||||
@@ -61,17 +87,17 @@ Album::get( unsigned int id, const QString& name, const Tomahawk::artist_ptr& ar
|
||||
static QHash< unsigned int, album_ptr > s_albums;
|
||||
static QMutex s_mutex;
|
||||
|
||||
QMutexLocker lock( &s_mutex );
|
||||
if ( s_albums.contains( id ) )
|
||||
QMutexLocker lock( &s_idCacheMutex );
|
||||
if ( s_albumsById.contains( id ) )
|
||||
{
|
||||
return s_albums.value( id );
|
||||
return s_albumsById.value( id );
|
||||
}
|
||||
|
||||
album_ptr a = album_ptr( new Album( id, name, artist ), &QObject::deleteLater );
|
||||
a->setWeakRef( a.toWeakRef() );
|
||||
|
||||
if ( id > 0 )
|
||||
s_albums.insert( id, a );
|
||||
s_albumsById.insert( id, a );
|
||||
|
||||
return a;
|
||||
}
|
||||
@@ -79,6 +105,7 @@ Album::get( unsigned int id, const QString& name, const Tomahawk::artist_ptr& ar
|
||||
|
||||
Album::Album( unsigned int id, const QString& name, const Tomahawk::artist_ptr& artist )
|
||||
: QObject()
|
||||
, m_waitingForId( false )
|
||||
, m_id( id )
|
||||
, m_name( name )
|
||||
, m_artist( artist )
|
||||
@@ -92,6 +119,20 @@ Album::Album( unsigned int id, const QString& name, const Tomahawk::artist_ptr&
|
||||
}
|
||||
|
||||
|
||||
Album::Album( const QString& name, const Tomahawk::artist_ptr& artist )
|
||||
: QObject()
|
||||
, m_waitingForId( true )
|
||||
, m_name( name )
|
||||
, m_artist( artist )
|
||||
, m_coverLoaded( false )
|
||||
, m_coverLoading( false )
|
||||
#ifndef ENABLE_HEADLESS
|
||||
, m_cover( 0 )
|
||||
#endif
|
||||
{
|
||||
m_sortname = DatabaseImpl::sortname( name );
|
||||
}
|
||||
|
||||
void
|
||||
Album::onTracksLoaded( Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection )
|
||||
{
|
||||
@@ -106,6 +147,49 @@ Album::artist() const
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Album::loadId( bool autoCreate )
|
||||
{
|
||||
Q_ASSERT( m_waitingForId );
|
||||
IdThreadWorker::getAlbumId( m_ownRef.toStrongRef(), autoCreate );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Album::setIdFuture( QFuture<unsigned int> future )
|
||||
{
|
||||
m_idFuture = future;
|
||||
}
|
||||
|
||||
|
||||
unsigned int
|
||||
Album::id() const
|
||||
{
|
||||
s_idMutex.lockForRead();
|
||||
const bool waiting = m_waitingForId;
|
||||
unsigned int finalId = m_id;
|
||||
s_idMutex.unlock();
|
||||
|
||||
if ( waiting )
|
||||
{
|
||||
qDebug() << Q_FUNC_INFO << "ALBUM WAITING FOR MUTEX:" << m_name;
|
||||
finalId = m_idFuture.result();
|
||||
|
||||
qDebug() << Q_FUNC_INFO << "ALBUM GOT ID::" << m_name << finalId;
|
||||
s_idMutex.lockForWrite();
|
||||
m_id = finalId;
|
||||
m_waitingForId = false;
|
||||
|
||||
if ( m_id > 0 )
|
||||
s_albumsById[ m_id ] = m_ownRef.toStrongRef();
|
||||
s_idMutex.unlock();
|
||||
|
||||
}
|
||||
|
||||
return finalId;
|
||||
}
|
||||
|
||||
|
||||
#ifndef ENABLE_HEADLESS
|
||||
QPixmap
|
||||
Album::cover( const QSize& size, bool forceLoad ) const
|
||||
|
@@ -27,6 +27,7 @@
|
||||
#ifndef ENABLE_HEADLESS
|
||||
#include <QtGui/QPixmap>
|
||||
#endif
|
||||
#include <QFuture>
|
||||
|
||||
#include "Typedefs.h"
|
||||
#include "PlaylistInterface.h"
|
||||
@@ -34,6 +35,8 @@
|
||||
#include "Collection.h"
|
||||
#include "infosystem/InfoSystem.h"
|
||||
|
||||
class IdThreadWorker;
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
@@ -45,10 +48,11 @@ public:
|
||||
static album_ptr get( const Tomahawk::artist_ptr& artist, const QString& name, bool autoCreate = false );
|
||||
static album_ptr get( unsigned int id, const QString& name, const Tomahawk::artist_ptr& artist );
|
||||
|
||||
explicit Album( unsigned int id, const QString& name, const Tomahawk::artist_ptr& artist );
|
||||
Album( unsigned int id, const QString& name, const Tomahawk::artist_ptr& artist );
|
||||
Album( const QString& name, const Tomahawk::artist_ptr& artist );
|
||||
virtual ~Album();
|
||||
|
||||
unsigned int id() const { return m_id; }
|
||||
unsigned int id() const;
|
||||
QString name() const { return m_name; }
|
||||
QString sortname() const { return m_sortname; }
|
||||
|
||||
@@ -64,6 +68,7 @@ public:
|
||||
QWeakPointer< Tomahawk::Album > weakRef() { return m_ownRef; }
|
||||
void setWeakRef( QWeakPointer< Tomahawk::Album > weakRef ) { m_ownRef = weakRef; }
|
||||
|
||||
void loadId( bool autoCreate );
|
||||
signals:
|
||||
void tracksAdded( const QList<Tomahawk::query_ptr>& tracks, Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection );
|
||||
void updated();
|
||||
@@ -78,8 +83,11 @@ private slots:
|
||||
private:
|
||||
Q_DISABLE_COPY( Album )
|
||||
QString infoid() const;
|
||||
void setIdFuture( QFuture<unsigned int> future );
|
||||
|
||||
unsigned int m_id;
|
||||
mutable bool m_waitingForId;
|
||||
mutable QFuture<unsigned int> m_idFuture;
|
||||
mutable unsigned int m_id;
|
||||
QString m_name;
|
||||
QString m_sortname;
|
||||
|
||||
@@ -98,6 +106,11 @@ private:
|
||||
QHash< Tomahawk::ModelMode, QHash< Tomahawk::collection_ptr, Tomahawk::playlistinterface_ptr > > m_playlistInterface;
|
||||
|
||||
QWeakPointer< Tomahawk::Album > m_ownRef;
|
||||
|
||||
static QHash< QString, album_ptr > s_albumsByName;
|
||||
static QHash< unsigned int, album_ptr > s_albumsById;
|
||||
|
||||
friend class ::IdThreadWorker;
|
||||
};
|
||||
|
||||
} // ns
|
||||
|
@@ -25,12 +25,21 @@
|
||||
#include "database/DatabaseImpl.h"
|
||||
#include "database/DatabaseCommand_AllAlbums.h"
|
||||
#include "database/DatabaseCommand_TrackStats.h"
|
||||
#include "database/IdThreadWorker.h"
|
||||
#include "Source.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <QReadWriteLock>
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
QHash< QString, artist_ptr > Artist::s_artistsByName = QHash< QString, artist_ptr >();
|
||||
QHash< unsigned int, artist_ptr > Artist::s_artistsById = QHash< unsigned int, artist_ptr >();
|
||||
|
||||
static QMutex s_nameCacheMutex;
|
||||
static QMutex s_idCacheMutex;
|
||||
static QReadWriteLock s_idMutex;
|
||||
|
||||
Artist::~Artist()
|
||||
{
|
||||
@@ -45,34 +54,40 @@ Artist::~Artist()
|
||||
artist_ptr
|
||||
Artist::get( const QString& name, bool autoCreate )
|
||||
{
|
||||
if ( name.isEmpty() )
|
||||
return artist_ptr();
|
||||
|
||||
QMutexLocker lock( &s_nameCacheMutex );
|
||||
if ( s_artistsByName.contains( name ) )
|
||||
return s_artistsByName.value( name );
|
||||
|
||||
if ( !Database::instance() || !Database::instance()->impl() )
|
||||
return artist_ptr();
|
||||
|
||||
int artid = Database::instance()->impl()->artistId( name, autoCreate );
|
||||
if ( artid < 1 && autoCreate )
|
||||
return artist_ptr();
|
||||
artist_ptr artist = artist_ptr( new Artist( name ), &QObject::deleteLater );
|
||||
artist->setWeakRef( artist.toWeakRef() );
|
||||
artist->loadId( autoCreate );
|
||||
|
||||
return Artist::get( artid, name );
|
||||
s_artistsByName[ name ] = artist;
|
||||
|
||||
return artist;
|
||||
}
|
||||
|
||||
|
||||
artist_ptr
|
||||
Artist::get( unsigned int id, const QString& name )
|
||||
{
|
||||
static QHash< unsigned int, artist_ptr > s_artists;
|
||||
static QMutex s_mutex;
|
||||
|
||||
QMutexLocker lock( &s_mutex );
|
||||
if ( s_artists.contains( id ) )
|
||||
QMutexLocker lock( &s_idCacheMutex );
|
||||
if ( s_artistsById.contains( id ) )
|
||||
{
|
||||
return s_artists.value( id );
|
||||
return s_artistsById.value( id );
|
||||
}
|
||||
|
||||
artist_ptr a = artist_ptr( new Artist( id, name ), &QObject::deleteLater );
|
||||
a->setWeakRef( a.toWeakRef() );
|
||||
|
||||
if ( id > 0 )
|
||||
s_artists.insert( id, a );
|
||||
s_artistsById.insert( id, a );
|
||||
|
||||
return a;
|
||||
}
|
||||
@@ -80,6 +95,7 @@ Artist::get( unsigned int id, const QString& name )
|
||||
|
||||
Artist::Artist( unsigned int id, const QString& name )
|
||||
: QObject()
|
||||
, m_waitingForFuture( false )
|
||||
, m_id( id )
|
||||
, m_name( name )
|
||||
, m_coverLoaded( false )
|
||||
@@ -95,6 +111,24 @@ Artist::Artist( unsigned int id, const QString& name )
|
||||
}
|
||||
|
||||
|
||||
Artist::Artist( const QString& name )
|
||||
: QObject()
|
||||
, m_waitingForFuture( true )
|
||||
, m_id( 0 )
|
||||
, m_name( name )
|
||||
, m_coverLoaded( false )
|
||||
, m_coverLoading( false )
|
||||
, m_simArtistsLoaded( false )
|
||||
, m_biographyLoaded( false )
|
||||
, m_infoJobs( 0 )
|
||||
#ifndef ENABLE_HEADLESS
|
||||
, m_cover( 0 )
|
||||
#endif
|
||||
{
|
||||
m_sortname = DatabaseImpl::sortname( name, true );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Artist::onTracksLoaded( Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection )
|
||||
{
|
||||
@@ -192,6 +226,47 @@ Artist::similarArtists() const
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Artist::loadId( bool autoCreate )
|
||||
{
|
||||
Q_ASSERT( m_waitingForFuture );
|
||||
|
||||
IdThreadWorker::getArtistId( m_ownRef.toStrongRef(), autoCreate );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Artist::setIdFuture( QFuture<unsigned int> future )
|
||||
{
|
||||
m_idFuture = future;
|
||||
}
|
||||
|
||||
|
||||
unsigned int
|
||||
Artist::id() const
|
||||
{
|
||||
s_idMutex.lockForRead();
|
||||
const bool waiting = m_waitingForFuture;
|
||||
unsigned int finalid = m_id;
|
||||
s_idMutex.unlock();
|
||||
|
||||
if ( waiting )
|
||||
{
|
||||
finalid = m_idFuture.result();
|
||||
|
||||
s_idMutex.lockForWrite();
|
||||
m_id = finalid;
|
||||
m_waitingForFuture = false;
|
||||
|
||||
if ( m_id > 0 )
|
||||
s_artistsById[ m_id ] = m_ownRef.toStrongRef();
|
||||
s_idMutex.unlock();
|
||||
}
|
||||
|
||||
return m_id;
|
||||
}
|
||||
|
||||
|
||||
QString
|
||||
Artist::biography() const
|
||||
{
|
||||
|
@@ -27,10 +27,16 @@
|
||||
#include <QtGui/QPixmap>
|
||||
#endif
|
||||
|
||||
#include <Qt/qfutureinterface.h>
|
||||
#include <QFuture>
|
||||
|
||||
#include "Typedefs.h"
|
||||
#include "DllMacro.h"
|
||||
#include "Query.h"
|
||||
|
||||
|
||||
class IdThreadWorker;
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
@@ -42,10 +48,11 @@ public:
|
||||
static artist_ptr get( const QString& name, bool autoCreate = false );
|
||||
static artist_ptr get( unsigned int id, const QString& name );
|
||||
|
||||
explicit Artist( unsigned int id, const QString& name );
|
||||
Artist( unsigned int id, const QString& name );
|
||||
explicit Artist( const QString& name );
|
||||
virtual ~Artist();
|
||||
|
||||
unsigned int id() const { return m_id; }
|
||||
unsigned int id() const;
|
||||
QString name() const { return m_name; }
|
||||
QString sortname() const { return m_sortname; }
|
||||
|
||||
@@ -72,6 +79,7 @@ public:
|
||||
QWeakPointer< Tomahawk::Artist > weakRef() { return m_ownRef; }
|
||||
void setWeakRef( QWeakPointer< Tomahawk::Artist > weakRef ) { m_ownRef = weakRef; }
|
||||
|
||||
void loadId( bool autoCreate );
|
||||
signals:
|
||||
void tracksAdded( const QList<Tomahawk::query_ptr>& tracks, Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection );
|
||||
void albumsAdded( const QList<Tomahawk::album_ptr>& albums, Tomahawk::ModelMode mode );
|
||||
@@ -93,7 +101,12 @@ private:
|
||||
Artist();
|
||||
QString infoid() const;
|
||||
|
||||
unsigned int m_id;
|
||||
void setIdFuture( QFuture<unsigned int> idFuture );
|
||||
|
||||
mutable bool m_waitingForFuture;
|
||||
mutable QFuture<unsigned int> m_idFuture;
|
||||
mutable unsigned int m_id;
|
||||
|
||||
QString m_name;
|
||||
QString m_sortname;
|
||||
|
||||
@@ -123,6 +136,11 @@ private:
|
||||
QHash< Tomahawk::ModelMode, QHash< Tomahawk::collection_ptr, Tomahawk::playlistinterface_ptr > > m_playlistInterface;
|
||||
|
||||
QWeakPointer< Tomahawk::Artist > m_ownRef;
|
||||
|
||||
static QHash< QString, artist_ptr > s_artistsByName;
|
||||
static QHash< unsigned int, artist_ptr > s_artistsById;
|
||||
|
||||
friend class ::IdThreadWorker;
|
||||
};
|
||||
|
||||
} // ns
|
||||
|
@@ -257,6 +257,7 @@ set( libSources
|
||||
database/DatabaseCommand_SetTrackAttributes.cpp
|
||||
database/Database.cpp
|
||||
database/TomahawkSqlQuery.cpp
|
||||
database/IdThreadWorker.cpp
|
||||
|
||||
infosystem/InfoSystem.cpp
|
||||
infosystem/InfoSystemCache.cpp
|
||||
|
@@ -60,7 +60,10 @@ public slots:
|
||||
/// Takes a spotify link and performs the default open action on it
|
||||
bool openRdioLink( const QString& link );
|
||||
|
||||
/// Creates a link from the requested data and copies it to the clipboard
|
||||
void copyToClipboard( const Tomahawk::query_ptr& query );
|
||||
|
||||
|
||||
QString copyPlaylistToClipboard( const Tomahawk::dynplaylist_ptr& playlist );
|
||||
void savePlaylistToFile( const Tomahawk::playlist_ptr& playlist, const QString& filename );
|
||||
|
||||
|
@@ -22,6 +22,7 @@
|
||||
#include "DatabaseCommand.h"
|
||||
#include "DatabaseImpl.h"
|
||||
#include "DatabaseWorker.h"
|
||||
#include "IdThreadWorker.h"
|
||||
#include "utils/Logger.h"
|
||||
#include "Source.h"
|
||||
|
||||
@@ -43,6 +44,7 @@ Database::Database( const QString& dbname, QObject* parent )
|
||||
, m_ready( false )
|
||||
, m_impl( new DatabaseImpl( dbname ) )
|
||||
, m_workerRW( new DatabaseWorkerThread( this, true ) )
|
||||
, m_idWorker( new IdThreadWorker( this ) )
|
||||
{
|
||||
s_instance = this;
|
||||
|
||||
@@ -67,6 +69,7 @@ Database::Database( const QString& dbname, QObject* parent )
|
||||
workerThread.data()->start();
|
||||
m_workerThreads << workerThread;
|
||||
}
|
||||
m_idWorker->start();
|
||||
}
|
||||
|
||||
|
||||
@@ -74,6 +77,9 @@ Database::~Database()
|
||||
{
|
||||
qDebug() << Q_FUNC_INFO;
|
||||
|
||||
m_idWorker->stop();
|
||||
delete m_idWorker;
|
||||
|
||||
if ( m_workerRW )
|
||||
m_workerRW.data()->quit();
|
||||
foreach ( QWeakPointer< DatabaseWorkerThread > workerThread, m_workerThreads )
|
||||
@@ -99,6 +105,7 @@ Database::~Database()
|
||||
|
||||
qDeleteAll( m_implHash.values() );
|
||||
delete m_impl;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@@ -32,6 +32,7 @@
|
||||
class DatabaseImpl;
|
||||
class DatabaseWorkerThread;
|
||||
class DatabaseWorker;
|
||||
class IdThreadWorker;
|
||||
|
||||
/*
|
||||
This class is really a firewall/pimpl - the public functions of LibraryImpl
|
||||
@@ -78,6 +79,7 @@ private:
|
||||
DatabaseImpl* m_impl;
|
||||
QWeakPointer< DatabaseWorkerThread > m_workerRW;
|
||||
QList< QWeakPointer< DatabaseWorkerThread > > m_workerThreads;
|
||||
IdThreadWorker* m_idWorker;
|
||||
int m_maxConcurrentThreads;
|
||||
|
||||
QHash< QThread*, DatabaseImpl* > m_implHash;
|
||||
|
@@ -42,7 +42,6 @@
|
||||
|
||||
#define CURRENT_SCHEMA_VERSION 28
|
||||
|
||||
|
||||
DatabaseImpl::DatabaseImpl( const QString& dbname )
|
||||
{
|
||||
QTime t;
|
||||
|
181
src/libtomahawk/database/IdThreadWorker.cpp
Normal file
181
src/libtomahawk/database/IdThreadWorker.cpp
Normal file
@@ -0,0 +1,181 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2012 Leo Franchi <lfranchi@kde.org>
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "IdThreadWorker.h"
|
||||
|
||||
#include "Artist.h"
|
||||
#include "Album.h"
|
||||
#include "Database.h"
|
||||
#include "DatabaseImpl.h"
|
||||
#include "Source.h"
|
||||
|
||||
#define ID_THREAD_DEBUG 1
|
||||
|
||||
#include <Qt/qfutureinterface.h>
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
namespace {
|
||||
enum QueryType {
|
||||
ArtistType,
|
||||
AlbumType
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
static QWaitCondition s_waitCond;
|
||||
static QMutex s_mutex;
|
||||
|
||||
struct QueueItem
|
||||
{
|
||||
QFutureInterface<unsigned int> promise;
|
||||
artist_ptr artist;
|
||||
album_ptr album;
|
||||
QueryType type;
|
||||
bool create;
|
||||
};
|
||||
|
||||
// TODO Q_GLOBAL_STATIC
|
||||
QQueue< QueueItem* > IdThreadWorker::s_workQueue = QQueue< QueueItem* >();
|
||||
|
||||
IdThreadWorker::IdThreadWorker( Database* db )
|
||||
: QThread()
|
||||
, m_db( db )
|
||||
, m_stop( false )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
IdThreadWorker::~IdThreadWorker()
|
||||
{
|
||||
wait();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
IdThreadWorker::stop()
|
||||
{
|
||||
{
|
||||
QMutexLocker l( &s_mutex );
|
||||
m_stop = true;
|
||||
}
|
||||
|
||||
s_waitCond.wakeOne();
|
||||
}
|
||||
|
||||
|
||||
QueueItem*
|
||||
internalGet( const artist_ptr& artist, const album_ptr& album, bool autoCreate, QueryType type )
|
||||
{
|
||||
QueueItem* item = new QueueItem;
|
||||
item->artist = artist;
|
||||
item->album = album;
|
||||
item->type = type;
|
||||
item->create = autoCreate;
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
IdThreadWorker::getArtistId( const artist_ptr& artist, bool autoCreate )
|
||||
{
|
||||
QueueItem* item = internalGet( artist, album_ptr(), autoCreate, ArtistType );
|
||||
artist->setIdFuture( item->promise.future() );
|
||||
|
||||
#if ID_THREAD_DEBUG
|
||||
qDebug() << "QUEUEING ARTIST:" << artist->name();
|
||||
#endif
|
||||
|
||||
s_mutex.lock();
|
||||
s_workQueue.enqueue( item );
|
||||
s_mutex.unlock();
|
||||
s_waitCond.wakeOne();
|
||||
#if ID_THREAD_DEBUG
|
||||
qDebug() << "DONE WOKE UP THREAD:" << artist->name();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
IdThreadWorker::getAlbumId( const album_ptr& album, bool autoCreate )
|
||||
{
|
||||
QueueItem* item = internalGet( artist_ptr(), album, autoCreate, AlbumType );
|
||||
album->setIdFuture( item->promise.future() );
|
||||
|
||||
#if ID_THREAD_DEBUG
|
||||
qDebug() << "QUEUEING ALUBM:" << album->artist()->name() << album->name();
|
||||
#endif
|
||||
s_mutex.lock();
|
||||
s_workQueue.enqueue( item );
|
||||
s_mutex.unlock();
|
||||
s_waitCond.wakeOne();
|
||||
#if ID_THREAD_DEBUG
|
||||
qDebug() << "DONE WOKE UP THREAD:" << album->artist()->name() << album->name();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
IdThreadWorker::run()
|
||||
{
|
||||
m_impl = Database::instance()->impl();
|
||||
|
||||
while ( !m_stop )
|
||||
{
|
||||
s_mutex.lock();
|
||||
#if ID_THREAD_DEBUG
|
||||
qDebug() << "IdWorkerThread waiting on condition...";
|
||||
#endif
|
||||
s_waitCond.wait( &s_mutex );
|
||||
#if ID_THREAD_DEBUG
|
||||
qDebug() << "IdWorkerThread WOKEN UP";
|
||||
#endif
|
||||
|
||||
while ( !s_workQueue.isEmpty() )
|
||||
{
|
||||
QueueItem* item = s_workQueue.dequeue();
|
||||
s_mutex.unlock();
|
||||
|
||||
#if ID_THREAD_DEBUG
|
||||
qDebug() << "WITH CONTENTS:" << (item->type == ArtistType ? item->artist->name() : item->album->artist()->name() + " _ " + item->album->name());
|
||||
#endif
|
||||
if ( item->type == ArtistType )
|
||||
{
|
||||
unsigned int id = m_impl->artistId( item->artist->name(), item->create );
|
||||
item->promise.reportFinished( &id );
|
||||
|
||||
item->artist->id();
|
||||
delete item;
|
||||
}
|
||||
else if ( item->type == AlbumType )
|
||||
{
|
||||
unsigned int artistId = m_impl->artistId( item->album->artist()->name(), item->create );
|
||||
unsigned int albumId = m_impl->albumId( artistId, item->album->name(), item->create );
|
||||
item->promise.reportFinished( &albumId );
|
||||
|
||||
item->album->id();
|
||||
delete item;
|
||||
}
|
||||
|
||||
s_mutex.lock();
|
||||
}
|
||||
|
||||
s_mutex.unlock();
|
||||
}
|
||||
}
|
54
src/libtomahawk/database/IdThreadWorker.h
Normal file
54
src/libtomahawk/database/IdThreadWorker.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2012 Leo Franchi <lfranchi@kde.org>
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef IDTHREADWORKER_H
|
||||
#define IDTHREADWORKER_H
|
||||
|
||||
#include "DllMacro.h"
|
||||
#include "Typedefs.h"
|
||||
|
||||
#include <QThread>
|
||||
#include <QQueue>
|
||||
#include <QWaitCondition>
|
||||
#include <QMutex>
|
||||
|
||||
class QueueItem;
|
||||
class Database;
|
||||
class DatabaseImpl;
|
||||
|
||||
class DLLEXPORT IdThreadWorker : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit IdThreadWorker( Database* db );
|
||||
virtual ~IdThreadWorker();
|
||||
|
||||
void run();
|
||||
void stop();
|
||||
|
||||
static void getArtistId( const Tomahawk::artist_ptr& artist, bool autoCreate = false );
|
||||
static void getAlbumId( const Tomahawk::album_ptr& album, bool autoCreate = false );
|
||||
|
||||
private:
|
||||
Database* m_db;
|
||||
DatabaseImpl* m_impl;
|
||||
bool m_stop;
|
||||
|
||||
static QQueue< QueueItem* > s_workQueue;
|
||||
};
|
||||
|
||||
#endif // IDTHREADWORKER_H
|
Reference in New Issue
Block a user