1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-16 19:14:06 +02:00

* Only store weak-pointers in the internal Album cache.

This commit is contained in:
Christian Muehlhaeuser
2013-03-30 13:04:01 +01:00
parent a26df54d9f
commit 8e37ecbd05
2 changed files with 28 additions and 17 deletions

View File

@@ -34,15 +34,15 @@
using namespace Tomahawk; using namespace Tomahawk;
QHash< QString, album_ptr > Album::s_albumsByName = QHash< QString, album_ptr >(); QHash< QString, album_wptr > Album::s_albumsByName = QHash< QString, album_wptr >();
QHash< unsigned int, album_ptr > Album::s_albumsById = QHash< unsigned int, album_ptr >(); QHash< unsigned int, album_wptr > Album::s_albumsById = QHash< unsigned int, album_wptr >();
static QMutex s_nameCacheMutex; static QMutex s_nameCacheMutex;
static QMutex s_idCacheMutex;
static QReadWriteLock s_idMutex; static QReadWriteLock s_idMutex;
Album::~Album() Album::~Album()
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Deleting album:" << m_name << m_artist->name();
m_ownRef.clear(); m_ownRef.clear();
#ifndef ENABLE_HEADLESS #ifndef ENABLE_HEADLESS
@@ -63,12 +63,13 @@ Album::get( const Tomahawk::artist_ptr& artist, const QString& name, bool autoCr
if ( !Database::instance() || !Database::instance()->impl() ) if ( !Database::instance() || !Database::instance()->impl() )
return album_ptr(); return album_ptr();
QMutexLocker l( &s_nameCacheMutex ); QMutexLocker lock( &s_nameCacheMutex );
const QString key = albumCacheKey( artist, name ); const QString key = albumCacheKey( artist, name );
if ( s_albumsByName.contains( key ) ) if ( s_albumsByName.contains( key ) )
{ {
return s_albumsByName.value( key ); album_wptr album = s_albumsByName.value( key );
if ( !album.isNull() )
return album.toStrongRef();
} }
album_ptr album = album_ptr( new Album( name, artist ) ); album_ptr album = album_ptr( new Album( name, artist ) );
@@ -83,19 +84,24 @@ Album::get( const Tomahawk::artist_ptr& artist, const QString& name, bool autoCr
album_ptr album_ptr
Album::get( unsigned int id, const QString& name, const Tomahawk::artist_ptr& artist ) Album::get( unsigned int id, const QString& name, const Tomahawk::artist_ptr& artist )
{ {
static QHash< unsigned int, album_ptr > s_albums; s_idMutex.lockForRead();
static QMutex s_mutex; if ( s_albumsById.contains( id ) )
{
album_wptr album = s_albumsById.value( id );
s_idMutex.unlock();
QMutexLocker lock( &s_idCacheMutex ); if ( !album.isNull() )
return album;
}
s_idMutex.unlock();
QMutexLocker lock( &s_nameCacheMutex );
const QString key = albumCacheKey( artist, name ); const QString key = albumCacheKey( artist, name );
if ( s_albumsByName.contains( key ) ) if ( s_albumsByName.contains( key ) )
{ {
return s_albumsByName.value( key ); album_wptr album = s_albumsByName.value( key );
} if ( !album.isNull() )
if ( s_albumsById.contains( id ) ) return album;
{
return s_albumsById.value( id );
} }
album_ptr a = album_ptr( new Album( id, name, artist ), &QObject::deleteLater ); album_ptr a = album_ptr( new Album( id, name, artist ), &QObject::deleteLater );
@@ -104,7 +110,9 @@ Album::get( unsigned int id, const QString& name, const Tomahawk::artist_ptr& ar
if ( id > 0 ) if ( id > 0 )
{ {
s_idMutex.lockForWrite();
s_albumsById.insert( id, a ); s_albumsById.insert( id, a );
s_idMutex.unlock();
} }
return a; return a;
@@ -123,6 +131,7 @@ Album::Album( unsigned int id, const QString& name, const Tomahawk::artist_ptr&
, m_cover( 0 ) , m_cover( 0 )
#endif #endif
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Creating album:" << id << name << artist->name();
m_sortname = DatabaseImpl::sortname( name ); m_sortname = DatabaseImpl::sortname( name );
} }
@@ -138,9 +147,11 @@ Album::Album( const QString& name, const Tomahawk::artist_ptr& artist )
, m_cover( 0 ) , m_cover( 0 )
#endif #endif
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Creating album:" << name << artist->name();
m_sortname = DatabaseImpl::sortname( name ); m_sortname = DatabaseImpl::sortname( name );
} }
void void
Album::onTracksLoaded( Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection ) Album::onTracksLoaded( Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection )
{ {
@@ -187,7 +198,7 @@ Album::id() const
m_waitingForId = false; m_waitingForId = false;
if ( m_id > 0 ) if ( m_id > 0 )
s_albumsById[ m_id ] = m_ownRef.toStrongRef(); s_albumsById.insert( m_id, m_ownRef.toStrongRef() );
s_idMutex.unlock(); s_idMutex.unlock();
} }

View File

@@ -106,8 +106,8 @@ private:
QWeakPointer< Tomahawk::Album > m_ownRef; QWeakPointer< Tomahawk::Album > m_ownRef;
static QHash< QString, album_ptr > s_albumsByName; static QHash< QString, album_wptr > s_albumsByName;
static QHash< unsigned int, album_ptr > s_albumsById; static QHash< unsigned int, album_wptr > s_albumsById;
friend class ::IdThreadWorker; friend class ::IdThreadWorker;
}; };