mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-05 21:57:41 +02:00
* Only store weak-pointers in the internal Artist cache.
This commit is contained in:
@@ -34,19 +34,17 @@
|
|||||||
|
|
||||||
#include <QReadWriteLock>
|
#include <QReadWriteLock>
|
||||||
|
|
||||||
#define ID_THREAD_DEBUG 0
|
|
||||||
|
|
||||||
using namespace Tomahawk;
|
using namespace Tomahawk;
|
||||||
|
|
||||||
QHash< QString, artist_ptr > Artist::s_artistsByName = QHash< QString, artist_ptr >();
|
QHash< QString, artist_wptr > Artist::s_artistsByName = QHash< QString, artist_wptr >();
|
||||||
QHash< unsigned int, artist_ptr > Artist::s_artistsById = QHash< unsigned int, artist_ptr >();
|
QHash< unsigned int, artist_wptr > Artist::s_artistsById = QHash< unsigned int, artist_wptr >();
|
||||||
|
|
||||||
static QMutex s_nameCacheMutex;
|
static QMutex s_nameCacheMutex;
|
||||||
static QMutex s_idCacheMutex;
|
|
||||||
static QReadWriteLock s_idMutex;
|
static QReadWriteLock s_idMutex;
|
||||||
|
|
||||||
Artist::~Artist()
|
Artist::~Artist()
|
||||||
{
|
{
|
||||||
|
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Deleting artist:" << m_name;
|
||||||
m_ownRef.clear();
|
m_ownRef.clear();
|
||||||
|
|
||||||
#ifndef ENABLE_HEADLESS
|
#ifndef ENABLE_HEADLESS
|
||||||
@@ -61,19 +59,18 @@ Artist::get( const QString& name, bool autoCreate )
|
|||||||
if ( name.isEmpty() )
|
if ( name.isEmpty() )
|
||||||
return artist_ptr();
|
return artist_ptr();
|
||||||
|
|
||||||
const QString sortname = name.toLower();
|
|
||||||
|
|
||||||
QMutexLocker lock( &s_nameCacheMutex );
|
QMutexLocker lock( &s_nameCacheMutex );
|
||||||
|
const QString sortname = name.toLower();
|
||||||
if ( s_artistsByName.contains( sortname ) )
|
if ( s_artistsByName.contains( sortname ) )
|
||||||
return s_artistsByName.value( sortname );
|
{
|
||||||
|
artist_wptr artist = s_artistsByName.value( sortname );
|
||||||
|
if ( !artist.isNull() )
|
||||||
|
return artist.toStrongRef();
|
||||||
|
}
|
||||||
|
|
||||||
if ( !Database::instance() || !Database::instance()->impl() )
|
if ( !Database::instance() || !Database::instance()->impl() )
|
||||||
return artist_ptr();
|
return artist_ptr();
|
||||||
|
|
||||||
#if ID_THREAD_DEBUG
|
|
||||||
tDebug() << "Creating artist:" << name << "( sortname:" << sortname << ")";
|
|
||||||
#endif
|
|
||||||
|
|
||||||
artist_ptr artist = artist_ptr( new Artist( name ), &QObject::deleteLater );
|
artist_ptr artist = artist_ptr( new Artist( name ), &QObject::deleteLater );
|
||||||
artist->setWeakRef( artist.toWeakRef() );
|
artist->setWeakRef( artist.toWeakRef() );
|
||||||
artist->loadId( autoCreate );
|
artist->loadId( autoCreate );
|
||||||
@@ -86,25 +83,35 @@ Artist::get( const QString& name, bool autoCreate )
|
|||||||
artist_ptr
|
artist_ptr
|
||||||
Artist::get( unsigned int id, const QString& name )
|
Artist::get( unsigned int id, const QString& name )
|
||||||
{
|
{
|
||||||
QMutexLocker lock( &s_idCacheMutex );
|
s_idMutex.lockForRead();
|
||||||
|
if ( s_artistsById.contains( id ) )
|
||||||
|
{
|
||||||
|
artist_wptr artist = s_artistsById.value( id );
|
||||||
|
s_idMutex.unlock();
|
||||||
|
|
||||||
|
if ( !artist.isNull() )
|
||||||
|
return artist;
|
||||||
|
}
|
||||||
|
s_idMutex.unlock();
|
||||||
|
|
||||||
|
QMutexLocker lock( &s_nameCacheMutex );
|
||||||
const QString sortname = name.toLower();
|
const QString sortname = name.toLower();
|
||||||
if ( s_artistsByName.contains( sortname ) )
|
if ( s_artistsByName.contains( sortname ) )
|
||||||
{
|
{
|
||||||
return s_artistsByName.value( sortname );
|
artist_wptr artist = s_artistsByName.value( sortname );
|
||||||
}
|
if ( !artist.isNull() )
|
||||||
if ( s_artistsById.contains( id ) )
|
return artist;
|
||||||
{
|
|
||||||
return s_artistsById.value( id );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
artist_ptr a = artist_ptr( new Artist( id, name ), &QObject::deleteLater );
|
artist_ptr a = artist_ptr( new Artist( id, name ), &QObject::deleteLater );
|
||||||
a->setWeakRef( a.toWeakRef() );
|
a->setWeakRef( a.toWeakRef() );
|
||||||
|
|
||||||
s_artistsByName.insert( sortname, a );
|
s_artistsByName.insert( sortname, a );
|
||||||
|
|
||||||
if ( id > 0 )
|
if ( id > 0 )
|
||||||
{
|
{
|
||||||
|
s_idMutex.lockForWrite();
|
||||||
s_artistsById.insert( id, a );
|
s_artistsById.insert( id, a );
|
||||||
|
s_idMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
return a;
|
return a;
|
||||||
@@ -125,6 +132,7 @@ Artist::Artist( unsigned int id, const QString& name )
|
|||||||
, m_cover( 0 )
|
, m_cover( 0 )
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
|
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Creating artist:" << id << name;
|
||||||
m_sortname = DatabaseImpl::sortname( name, true );
|
m_sortname = DatabaseImpl::sortname( name, true );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,6 +151,7 @@ Artist::Artist( const QString& name )
|
|||||||
, m_cover( 0 )
|
, m_cover( 0 )
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
|
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Creating artist:" << name;
|
||||||
m_sortname = DatabaseImpl::sortname( name, true );
|
m_sortname = DatabaseImpl::sortname( name, true );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,25 +295,19 @@ Artist::id() const
|
|||||||
|
|
||||||
if ( waiting )
|
if ( waiting )
|
||||||
{
|
{
|
||||||
#if ID_THREAD_DEBUG
|
// qDebug() << Q_FUNC_INFO << "Asked for artist ID and NOT loaded yet" << m_name << m_idFuture.isFinished();
|
||||||
qDebug() << Q_FUNC_INFO << "Asked for artist ID and NOT loaded yet" << m_name << m_idFuture.isFinished();
|
|
||||||
#endif
|
|
||||||
m_idFuture.waitForFinished();
|
m_idFuture.waitForFinished();
|
||||||
#if ID_THREAD_DEBUG
|
// qDebug() << "DONE WAITING:" << m_idFuture.resultCount() << m_idFuture.isResultReadyAt( 0 ) << m_idFuture.isCanceled() << m_idFuture.isFinished() << m_idFuture.isPaused() << m_idFuture.isRunning() << m_idFuture.isStarted();
|
||||||
qDebug() << "DONE WAITING:" << m_idFuture.resultCount() << m_idFuture.isResultReadyAt(0) << m_idFuture.isCanceled() << m_idFuture.isFinished() << m_idFuture.isPaused() << m_idFuture.isRunning() << m_idFuture.isStarted();
|
|
||||||
#endif
|
|
||||||
finalid = m_idFuture.result();
|
finalid = m_idFuture.result();
|
||||||
|
|
||||||
#if ID_THREAD_DEBUG
|
// qDebug() << Q_FUNC_INFO << "Got loaded artist:" << m_name << finalid;
|
||||||
qDebug() << Q_FUNC_INFO << "Got loaded artist:" << m_name << finalid;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
s_idMutex.lockForWrite();
|
s_idMutex.lockForWrite();
|
||||||
m_id = finalid;
|
m_id = finalid;
|
||||||
m_waitingForFuture = false;
|
m_waitingForFuture = false;
|
||||||
|
|
||||||
if ( m_id > 0 )
|
if ( m_id > 0 )
|
||||||
s_artistsById[ m_id ] = m_ownRef.toStrongRef();
|
s_artistsById.insert( m_id, m_ownRef.toStrongRef() );
|
||||||
|
|
||||||
s_idMutex.unlock();
|
s_idMutex.unlock();
|
||||||
}
|
}
|
||||||
|
@@ -135,8 +135,8 @@ private:
|
|||||||
|
|
||||||
QWeakPointer< Tomahawk::Artist > m_ownRef;
|
QWeakPointer< Tomahawk::Artist > m_ownRef;
|
||||||
|
|
||||||
static QHash< QString, artist_ptr > s_artistsByName;
|
static QHash< QString, artist_wptr > s_artistsByName;
|
||||||
static QHash< unsigned int, artist_ptr > s_artistsById;
|
static QHash< unsigned int, artist_wptr > s_artistsById;
|
||||||
|
|
||||||
friend class ::IdThreadWorker;
|
friend class ::IdThreadWorker;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user