From 8e37ecbd05c33fd55841393c093acca8709f5247 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sat, 30 Mar 2013 13:04:01 +0100 Subject: [PATCH] * Only store weak-pointers in the internal Album cache. --- src/libtomahawk/Album.cpp | 41 +++++++++++++++++++++++++-------------- src/libtomahawk/Album.h | 4 ++-- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/libtomahawk/Album.cpp b/src/libtomahawk/Album.cpp index 961131bb8..4dc2ae72d 100644 --- a/src/libtomahawk/Album.cpp +++ b/src/libtomahawk/Album.cpp @@ -34,15 +34,15 @@ 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 >(); +QHash< QString, album_wptr > Album::s_albumsByName = QHash< QString, album_wptr >(); +QHash< unsigned int, album_wptr > Album::s_albumsById = QHash< unsigned int, album_wptr >(); static QMutex s_nameCacheMutex; -static QMutex s_idCacheMutex; static QReadWriteLock s_idMutex; Album::~Album() { + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Deleting album:" << m_name << m_artist->name(); m_ownRef.clear(); #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() ) return album_ptr(); - QMutexLocker l( &s_nameCacheMutex ); - + QMutexLocker lock( &s_nameCacheMutex ); const QString key = albumCacheKey( artist, name ); 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 ) ); @@ -83,19 +84,24 @@ Album::get( const Tomahawk::artist_ptr& artist, const QString& name, bool autoCr album_ptr Album::get( unsigned int id, const QString& name, const Tomahawk::artist_ptr& artist ) { - static QHash< unsigned int, album_ptr > s_albums; - static QMutex s_mutex; + s_idMutex.lockForRead(); + 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 ); if ( s_albumsByName.contains( key ) ) { - return s_albumsByName.value( key ); - } - if ( s_albumsById.contains( id ) ) - { - return s_albumsById.value( id ); + album_wptr album = s_albumsByName.value( key ); + if ( !album.isNull() ) + return album; } 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 ) { + s_idMutex.lockForWrite(); s_albumsById.insert( id, a ); + s_idMutex.unlock(); } return a; @@ -123,6 +131,7 @@ Album::Album( unsigned int id, const QString& name, const Tomahawk::artist_ptr& , m_cover( 0 ) #endif { + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Creating album:" << id << name << artist->name(); m_sortname = DatabaseImpl::sortname( name ); } @@ -138,9 +147,11 @@ Album::Album( const QString& name, const Tomahawk::artist_ptr& artist ) , m_cover( 0 ) #endif { + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Creating album:" << name << artist->name(); m_sortname = DatabaseImpl::sortname( name ); } + void Album::onTracksLoaded( Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection ) { @@ -187,7 +198,7 @@ Album::id() const m_waitingForId = false; if ( m_id > 0 ) - s_albumsById[ m_id ] = m_ownRef.toStrongRef(); + s_albumsById.insert( m_id, m_ownRef.toStrongRef() ); s_idMutex.unlock(); } diff --git a/src/libtomahawk/Album.h b/src/libtomahawk/Album.h index 6565b400a..88e4f88b3 100644 --- a/src/libtomahawk/Album.h +++ b/src/libtomahawk/Album.h @@ -106,8 +106,8 @@ private: QWeakPointer< Tomahawk::Album > m_ownRef; - static QHash< QString, album_ptr > s_albumsByName; - static QHash< unsigned int, album_ptr > s_albumsById; + static QHash< QString, album_wptr > s_albumsByName; + static QHash< unsigned int, album_wptr > s_albumsById; friend class ::IdThreadWorker; };