From 680b204d11d7ad3e229f3eb2960f77f399661dfb Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Sun, 3 Apr 2011 05:44:32 -0400 Subject: [PATCH] Make caching work. Doesn't save/load to disk yet but it's ready to be used as a memcache at least. --- include/tomahawk/infosystem.h | 20 ++++++++++++++++++- src/infosystem/infoplugins/echonestplugin.cpp | 5 ----- src/infosystem/infoplugins/lastfmplugin.cpp | 4 ---- .../infoplugins/musixmatchplugin.cpp | 7 ------- src/infosystem/infosystem.cpp | 15 +------------- src/infosystem/infosystemcache.cpp | 13 +++++++++++- src/infosystem/infosystemcache.h | 3 ++- 7 files changed, 34 insertions(+), 33 deletions(-) diff --git a/include/tomahawk/infosystem.h b/include/tomahawk/infosystem.h index 2ab5d2da0..e0efec86b 100644 --- a/include/tomahawk/infosystem.h +++ b/include/tomahawk/infosystem.h @@ -19,6 +19,7 @@ #ifndef TOMAHAWK_INFOSYSTEM_H #define TOMAHAWK_INFOSYSTEM_H +#include #include #include #include @@ -150,7 +151,6 @@ signals: public slots: void infoSlot( QString target, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomData customData ); - void finishedSlot( QString target, Tomahawk::InfoSystem::InfoType type); private: QLinkedList< InfoPluginPtr > determineOrderedMatches( const InfoType type ) const; @@ -170,6 +170,24 @@ private: } +inline uint qHash( Tomahawk::InfoSystem::InfoCacheCriteria hash ) +{ + QCryptographicHash md5( QCryptographicHash::Md5 ); + foreach( QString key, hash.keys() ) + md5.addData( key.toUtf8() ); + foreach( QString value, hash.values() ) + md5.addData( value.toUtf8() ); + + QString hexData = md5.result(); + + uint returnval = 0; + + foreach( uint val, hexData.toUcs4() ) + returnval += val; + + return returnval; +} + Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoGenericMap ); Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoCustomData ); Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoCacheCriteria ); diff --git a/src/infosystem/infoplugins/echonestplugin.cpp b/src/infosystem/infoplugins/echonestplugin.cpp index ec852f787..38f64ed4c 100644 --- a/src/infosystem/infoplugins/echonestplugin.cpp +++ b/src/infosystem/infoplugins/echonestplugin.cpp @@ -163,7 +163,6 @@ void EchoNestPlugin::getArtistBiographySlot() } emit info( m_callerMap[reply], Tomahawk::InfoSystem::InfoArtistBiography, reply->property( "data" ), QVariant::fromValue(biographyMap), m_replyMap[reply] ); - emit finished( m_callerMap[reply], Tomahawk::InfoSystem::InfoArtistBiography); m_replyMap.remove(reply); m_callerMap.remove(reply); reply->deleteLater(); @@ -175,7 +174,6 @@ void EchoNestPlugin::getArtistFamiliaritySlot() Echonest::Artist artist = artistFromReply( reply ); qreal familiarity = artist.familiarity(); emit info( m_callerMap[reply], Tomahawk::InfoSystem::InfoArtistFamiliarity, reply->property( "data" ), familiarity, m_replyMap[reply] ); - emit finished( m_callerMap[reply], Tomahawk::InfoSystem::InfoArtistFamiliarity); m_replyMap.remove(reply); m_callerMap.remove(reply); reply->deleteLater(); @@ -187,7 +185,6 @@ void EchoNestPlugin::getArtistHotttnesssSlot() Echonest::Artist artist = artistFromReply( reply ); qreal hotttnesss = artist.hotttnesss(); emit info( m_callerMap[reply], Tomahawk::InfoSystem::InfoArtistHotttness, reply->property( "data" ), hotttnesss, m_replyMap[reply] ); - emit finished( m_callerMap[reply], Tomahawk::InfoSystem::InfoArtistHotttness); m_replyMap.remove(reply); m_callerMap.remove(reply); reply->deleteLater(); @@ -206,7 +203,6 @@ void EchoNestPlugin::getArtistTermsSlot() termsMap[ term.name() ] = termMap; } emit info( m_callerMap[reply], Tomahawk::InfoSystem::InfoArtistTerms, reply->property( "data" ), QVariant::fromValue(termsMap), m_replyMap[reply] ); - emit finished( m_callerMap[reply], Tomahawk::InfoSystem::InfoArtistTerms); m_replyMap.remove(reply); m_callerMap.remove(reply); reply->deleteLater(); @@ -224,7 +220,6 @@ void EchoNestPlugin::getMiscTopSlot() termsMap[ term.name().toLower() ] = termMap; } emit info( m_callerMap[reply], Tomahawk::InfoSystem::InfoMiscTopTerms, QVariant(), QVariant::fromValue(termsMap), m_replyMap[reply] ); - emit finished( m_callerMap[reply], Tomahawk::InfoSystem::InfoMiscTopTerms); m_replyMap.remove(reply); m_callerMap.remove(reply); reply->deleteLater(); diff --git a/src/infosystem/infoplugins/lastfmplugin.cpp b/src/infosystem/infoplugins/lastfmplugin.cpp index 5848f6f68..652d87414 100644 --- a/src/infosystem/infoplugins/lastfmplugin.cpp +++ b/src/infosystem/infoplugins/lastfmplugin.cpp @@ -95,7 +95,6 @@ void LastFmPlugin::dataError( const QString &caller, const InfoType type, const QVariant& data, Tomahawk::InfoSystem::InfoCustomData &customData ) { emit info( caller, type, data, QVariant(), customData ); - emit finished( caller, type ); return; } @@ -139,7 +138,6 @@ LastFmPlugin::nowPlaying( const QString &caller, const InfoType type, const QVar m_scrobbler->nowPlaying( m_track ); emit info( caller, type, data, QVariant(), customData ); - emit finished( caller, type ); } void @@ -158,7 +156,6 @@ LastFmPlugin::scrobble( const QString &caller, const InfoType type, const QVaria m_scrobbler->submit(); emit info( caller, type, data, QVariant(), customData ); - emit finished( caller, type ); } void @@ -230,7 +227,6 @@ LastFmPlugin::coverArtReturned() returnedData, customData ); - emit finished( reply->property( "caller" ).toString(), (Tomahawk::InfoSystem::InfoType)(reply->property( "type" ).toUInt()) ); InfoCustomData origData = reply->property( "origData" ).value< Tomahawk::InfoSystem::InfoCustomData >(); Tomahawk::InfoSystem::InfoCacheCriteria criteria; diff --git a/src/infosystem/infoplugins/musixmatchplugin.cpp b/src/infosystem/infoplugins/musixmatchplugin.cpp index cc2cd576f..128085ca6 100644 --- a/src/infosystem/infoplugins/musixmatchplugin.cpp +++ b/src/infosystem/infoplugins/musixmatchplugin.cpp @@ -53,7 +53,6 @@ void MusixMatchPlugin::getInfo(const QString &caller, const InfoType type, const if( artist.isEmpty() || track.isEmpty() ) { emit info(caller, Tomahawk::InfoSystem::InfoTrackLyrics, data, QVariant(), customData); - emit finished(caller, Tomahawk::InfoSystem::InfoTrackLyrics); return; } qDebug() << "artist is " << artist << ", track is " << track; @@ -76,7 +75,6 @@ bool MusixMatchPlugin::isValidTrackData(const QString &caller, const QVariant& d if (data.isNull() || !data.isValid() || !data.canConvert()) { emit info(caller, Tomahawk::InfoSystem::InfoTrackLyrics, data, QVariant(), customData); - emit finished(caller, Tomahawk::InfoSystem::InfoTrackLyrics); qDebug() << "MusixMatchPlugin::isValidTrackData: Data null, invalid, or can't convert"; return false; } @@ -84,14 +82,12 @@ bool MusixMatchPlugin::isValidTrackData(const QString &caller, const QVariant& d if (hash["trackName"].toString().isEmpty() ) { emit info(caller, Tomahawk::InfoSystem::InfoTrackLyrics, data, QVariant(), customData); - emit finished(caller, Tomahawk::InfoSystem::InfoTrackLyrics); qDebug() << "MusixMatchPlugin::isValidTrackData: Track name is empty"; return false; } if (hash["artistName"].toString().isEmpty() ) { emit info(caller, Tomahawk::InfoSystem::InfoTrackLyrics, data, QVariant(), customData); - emit finished(caller, Tomahawk::InfoSystem::InfoTrackLyrics); qDebug() << "MusixMatchPlugin::isValidTrackData: No artist name found"; return false; } @@ -114,7 +110,6 @@ void MusixMatchPlugin::trackSearchSlot() if (domNodeList.isEmpty()) { emit info(oldReply->property("caller").toString(), Tomahawk::InfoSystem::InfoTrackLyrics, oldReply->property("origData"), QVariant(), oldReply->property("customData").value()); - emit finished(oldReply->property("caller").toString(), Tomahawk::InfoSystem::InfoTrackLyrics); return; } QString track_id = domNodeList.at(0).toElement().text(); @@ -144,11 +139,9 @@ void MusixMatchPlugin::trackLyricsSlot() if (domNodeList.isEmpty()) { emit info(reply->property("caller").toString(), Tomahawk::InfoSystem::InfoTrackLyrics, reply->property("origData"), QVariant(), reply->property("customData").value()); - emit finished(reply->property("caller").toString(), Tomahawk::InfoSystem::InfoTrackLyrics); return; } QString lyrics = domNodeList.at(0).toElement().text(); qDebug() << "Emitting lyrics: " << lyrics; emit info(reply->property("caller").toString(), Tomahawk::InfoSystem::InfoTrackLyrics, reply->property("origData"), QVariant(lyrics), reply->property("customData").value()); - emit finished(reply->property("caller").toString(), Tomahawk::InfoSystem::InfoTrackLyrics); } diff --git a/src/infosystem/infosystem.cpp b/src/infosystem/infosystem.cpp index d03b53f10..7fd6158c6 100644 --- a/src/infosystem/infosystem.cpp +++ b/src/infosystem/infosystem.cpp @@ -90,18 +90,9 @@ InfoSystem::InfoSystem(QObject *parent) SLOT( infoSlot( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomData ) ), Qt::UniqueConnection ); - - connect( - plugin.data(), - SIGNAL( finished( QString, Tomahawk::InfoSystem::InfoType ) ), - this, - SLOT( finishedSlot( QString, Tomahawk::InfoSystem::InfoType ) ), Qt::UniqueConnection - ); } connect( m_cache, SIGNAL( info( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomData ) ), this, SLOT( infoSlot( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomData ) ), Qt::UniqueConnection ); - connect( m_cache, SIGNAL( finished( QString, Tomahawk::InfoSystem::InfoType ) ), - this, SLOT( finishedSlot( QString, Tomahawk::InfoSystem::InfoType ) ), Qt::UniqueConnection ); } InfoSystem::~InfoSystem() @@ -191,11 +182,7 @@ void InfoSystem::infoSlot(QString target, InfoType type, QVariant input, QVarian return; } emit info(target, type, input, output, customData); -} - -void InfoSystem::finishedSlot(QString target, InfoType type) -{ - qDebug() << Q_FUNC_INFO; + m_dataTracker[target][type] = m_dataTracker[target][type] - 1; qDebug() << "current count in dataTracker is " << m_dataTracker[target][type]; Q_FOREACH(InfoType testtype, m_dataTracker[target].keys()) diff --git a/src/infosystem/infosystemcache.cpp b/src/infosystem/infosystemcache.cpp index c25fb10bb..8e4801540 100644 --- a/src/infosystem/infosystemcache.cpp +++ b/src/infosystem/infosystemcache.cpp @@ -24,11 +24,22 @@ void Tomahawk::InfoSystem::InfoSystemCache::getCachedInfoSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ) { qDebug() << Q_FUNC_INFO; - emit notInCache( criteria, caller, type, input, customData ); + if( !m_memCache.contains( type ) || !m_memCache[type].contains( criteria ) ) + { + emit notInCache( criteria, caller, type, input, customData ); + return; + } + + emit info( caller, type, input, m_memCache[type][criteria], customData ); } void Tomahawk::InfoSystem::InfoSystemCache::updateCacheSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, Tomahawk::InfoSystem::InfoType type, QVariant output ) { qDebug() << Q_FUNC_INFO; + QHash< InfoCacheCriteria, QVariant > typecache; + if( m_memCache.contains( type ) ) + typecache = m_memCache[type]; + typecache[criteria] = output; + m_memCache[type] = typecache; } diff --git a/src/infosystem/infosystemcache.h b/src/infosystem/infosystemcache.h index 44476314a..ef31710c2 100644 --- a/src/infosystem/infosystemcache.h +++ b/src/infosystem/infosystemcache.h @@ -49,12 +49,13 @@ public: signals: void notInCache( Tomahawk::InfoSystem::InfoCacheCriteria criteria, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ); void info( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomData customData ); - void finished( QString, Tomahawk::InfoSystem::InfoType ); public slots: void getCachedInfoSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ); void updateCacheSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, Tomahawk::InfoSystem::InfoType type, QVariant output ); +private: + QHash< InfoType, QHash< InfoCacheCriteria, QVariant > > m_memCache; }; } //namespace InfoSystem