1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-26 07:14:47 +02:00

* IdThreadWorker supports loading track IDs.

This commit is contained in:
Christian Muehlhaeuser
2013-05-16 11:41:32 +02:00
parent fcedb57178
commit 8e76ffcc5d
2 changed files with 64 additions and 14 deletions

View File

@@ -23,6 +23,7 @@
#include "Database.h" #include "Database.h"
#include "DatabaseImpl.h" #include "DatabaseImpl.h"
#include "Source.h" #include "Source.h"
#include "utils/Logger.h"
#define ID_THREAD_DEBUG 0 #define ID_THREAD_DEBUG 0
@@ -33,7 +34,8 @@ using namespace Tomahawk;
namespace { namespace {
enum QueryType { enum QueryType {
ArtistType, ArtistType,
AlbumType AlbumType,
TrackType
}; };
} }
@@ -46,6 +48,7 @@ struct QueueItem
QFutureInterface<unsigned int> promise; QFutureInterface<unsigned int> promise;
artist_ptr artist; artist_ptr artist;
album_ptr album; album_ptr album;
trackdata_ptr track;
QueryType type; QueryType type;
bool create; bool create;
}; };
@@ -80,11 +83,12 @@ IdThreadWorker::stop()
QueueItem* QueueItem*
internalGet( const artist_ptr& artist, const album_ptr& album, bool autoCreate, QueryType type ) internalGet( const artist_ptr& artist, const album_ptr& album, const trackdata_ptr& track, bool autoCreate, QueryType type )
{ {
QueueItem* item = new QueueItem; QueueItem* item = new QueueItem;
item->artist = artist; item->artist = artist;
item->album = album; item->album = album;
item->track = track;
item->type = type; item->type = type;
item->create = autoCreate; item->create = autoCreate;
item->promise.reportStarted(); item->promise.reportStarted();
@@ -96,11 +100,11 @@ internalGet( const artist_ptr& artist, const album_ptr& album, bool autoCreate,
void void
IdThreadWorker::getArtistId( const artist_ptr& artist, bool autoCreate ) IdThreadWorker::getArtistId( const artist_ptr& artist, bool autoCreate )
{ {
QueueItem* item = internalGet( artist, album_ptr(), autoCreate, ArtistType ); QueueItem* item = internalGet( artist, album_ptr(), trackdata_ptr(), autoCreate, ArtistType );
artist->setIdFuture( item->promise.future() ); artist->setIdFuture( item->promise.future() );
#if ID_THREAD_DEBUG #if ID_THREAD_DEBUG
qDebug() << "QUEUEING ARTIST:" << artist->name(); tDebug() << "QUEUEING ARTIST:" << artist->name();
#endif #endif
s_mutex.lock(); s_mutex.lock();
@@ -108,7 +112,7 @@ IdThreadWorker::getArtistId( const artist_ptr& artist, bool autoCreate )
s_mutex.unlock(); s_mutex.unlock();
s_waitCond.wakeOne(); s_waitCond.wakeOne();
#if ID_THREAD_DEBUG #if ID_THREAD_DEBUG
qDebug() << "DONE WOKE UP THREAD:" << artist->name(); tDebug() << "DONE WOKE UP THREAD:" << artist->name();
#endif #endif
} }
@@ -116,22 +120,41 @@ IdThreadWorker::getArtistId( const artist_ptr& artist, bool autoCreate )
void void
IdThreadWorker::getAlbumId( const album_ptr& album, bool autoCreate ) IdThreadWorker::getAlbumId( const album_ptr& album, bool autoCreate )
{ {
QueueItem* item = internalGet( artist_ptr(), album, autoCreate, AlbumType ); QueueItem* item = internalGet( artist_ptr(), album, trackdata_ptr(), autoCreate, AlbumType );
album->setIdFuture( item->promise.future() ); album->setIdFuture( item->promise.future() );
#if ID_THREAD_DEBUG #if ID_THREAD_DEBUG
qDebug() << "QUEUEING ALUBM:" << album->artist()->name() << album->name(); tDebug() << "QUEUEING ALUBM:" << album->artist()->name() << album->name();
#endif #endif
s_mutex.lock(); s_mutex.lock();
s_workQueue.enqueue( item ); s_workQueue.enqueue( item );
s_mutex.unlock(); s_mutex.unlock();
s_waitCond.wakeOne(); s_waitCond.wakeOne();
#if ID_THREAD_DEBUG #if ID_THREAD_DEBUG
qDebug() << "DONE WOKE UP THREAD:" << album->artist()->name() << album->name(); tDebug() << "DONE WOKE UP THREAD:" << album->artist()->name() << album->name();
#endif #endif
} }
void
IdThreadWorker::getTrackId( const trackdata_ptr& track, bool autoCreate )
{
QueueItem* item = internalGet( artist_ptr(), album_ptr(), track, autoCreate, TrackType );
track->setIdFuture( item->promise.future() );
#if ID_THREAD_DEBUG
tDebug() << "QUEUEING TRACK:" << track->toString();
#endif
s_mutex.lock();
s_workQueue.enqueue( item );
s_mutex.unlock();
s_waitCond.wakeOne();
#if ID_THREAD_DEBUG
tDebug() << "DONE WOKE UP THREAD:" << track->toString();
#endif
}
void void
IdThreadWorker::run() IdThreadWorker::run()
{ {
@@ -141,11 +164,11 @@ IdThreadWorker::run()
{ {
s_mutex.lock(); s_mutex.lock();
#if ID_THREAD_DEBUG #if ID_THREAD_DEBUG
qDebug() << "IdWorkerThread waiting on condition..."; tDebug() << "IdWorkerThread waiting on condition...";
#endif #endif
s_waitCond.wait( &s_mutex ); s_waitCond.wait( &s_mutex );
#if ID_THREAD_DEBUG #if ID_THREAD_DEBUG
qDebug() << "IdWorkerThread WOKEN UP"; tDebug() << "IdWorkerThread WOKEN UP";
#endif #endif
while ( !s_workQueue.isEmpty() ) while ( !s_workQueue.isEmpty() )
@@ -154,12 +177,29 @@ IdThreadWorker::run()
s_mutex.unlock(); s_mutex.unlock();
#if ID_THREAD_DEBUG #if ID_THREAD_DEBUG
qDebug() << "WITH CONTENTS:" << (item->type == ArtistType ? item->artist->name() : item->album->artist()->name() + " _ " + item->album->name()); switch ( item->type )
{
case ArtistType:
{
tDebug() << "WITH CONTENTS:" << item->artist->name();
break;
}
case AlbumType:
{
tDebug() << "WITH CONTENTS:" << item->album->artist()->name() + " _ " + item->album->name();
break;
}
case TrackType:
{
tDebug() << "WITH CONTENTS:" << item->track->artist() + " _ " + item->track->track();
break;
}
}
#endif #endif
if ( item->type == ArtistType ) if ( item->type == ArtistType )
{ {
unsigned int id = m_impl->artistId( item->artist->name(), item->create ); unsigned int id = m_impl->artistId( item->artist->name(), item->create );
item->promise.reportFinished( &id ); item->promise.reportFinished( &id );
item->artist->id(); item->artist->id();
delete item; delete item;
@@ -168,11 +208,20 @@ IdThreadWorker::run()
{ {
unsigned int artistId = m_impl->artistId( item->album->artist()->name(), item->create ); unsigned int artistId = m_impl->artistId( item->album->artist()->name(), item->create );
unsigned int albumId = m_impl->albumId( artistId, item->album->name(), item->create ); unsigned int albumId = m_impl->albumId( artistId, item->album->name(), item->create );
item->promise.reportFinished( &albumId ); item->promise.reportFinished( &albumId );
item->album->id(); item->album->id();
delete item; delete item;
} }
else if ( item->type == TrackType )
{
unsigned int artistId = m_impl->artistId( item->track->artist(), item->create );
unsigned int trackId = m_impl->trackId( artistId, item->track->track(), item->create );
item->promise.reportFinished( &trackId );
item->track->trackId();
delete item;
}
s_mutex.lock(); s_mutex.lock();
} }

View File

@@ -42,6 +42,7 @@ public:
static void getArtistId( const Tomahawk::artist_ptr& artist, bool autoCreate = false ); static void getArtistId( const Tomahawk::artist_ptr& artist, bool autoCreate = false );
static void getAlbumId( const Tomahawk::album_ptr& album, bool autoCreate = false ); static void getAlbumId( const Tomahawk::album_ptr& album, bool autoCreate = false );
static void getTrackId( const Tomahawk::trackdata_ptr& trackData, bool autoCreate = false );
private: private:
Database* m_db; Database* m_db;