Skip to content

Commit

Permalink
split AddCachedTexture into Add and SetCachedTextureValid, so that Ad…
Browse files Browse the repository at this point in the history
…dCachedTexture can just delete and re-add to simplify queries
  • Loading branch information
Jonathan Marshall committed May 2, 2012
1 parent baa48f7 commit 6b1dc99
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
11 changes: 10 additions & 1 deletion xbmc/TextureCache.cpp
Expand Up @@ -244,6 +244,12 @@ bool CTextureCache::IncrementUseCount(const CTextureDetails &details)
return m_database.IncrementUseCount(details);
}

bool CTextureCache::SetCachedTextureValid(const CStdString &url, bool updateable)
{
CSingleLock lock(m_databaseSection);
return m_database.SetCachedTextureValid(url, updateable);
}

bool CTextureCache::ClearCachedTexture(const CStdString &url, CStdString &cachedURL)
{
CSingleLock lock(m_databaseSection);
Expand Down Expand Up @@ -271,7 +277,10 @@ void CTextureCache::OnJobComplete(unsigned int jobID, bool success, CJob *job)
if (strcmp(job->GetType(), "cacheimage") == 0 && success)
{
CTextureCacheJob *cacheJob = (CTextureCacheJob *)job;
AddCachedTexture(cacheJob->m_url, cacheJob->m_details);
if (cacheJob->m_oldHash == cacheJob->m_details.hash)
SetCachedTextureValid(cacheJob->m_url, cacheJob->m_details.updateable);
else
AddCachedTexture(cacheJob->m_url, cacheJob->m_details);
// TODO: call back to the UI indicating that it can update it's image...
if (g_advancedSettings.m_useDDSFanart && !cacheJob->m_details.file.empty())
AddJob(new CTextureDDSJob(GetCachedPath(cacheJob->m_details.file)));
Expand Down
8 changes: 8 additions & 0 deletions xbmc/TextureCache.h
Expand Up @@ -199,6 +199,14 @@ class CTextureCache : public CJobQueue
*/
bool IncrementUseCount(const CTextureDetails &details);

/*! \brief Set a previously cached texture as valid in the database
Thread-safe wrapper of CTextureDatabase::SetCachedTextureValid
\param image url of the original image
\param updateable whether this image should be checked for updates
\return true if successful, false otherwise.
*/
bool SetCachedTextureValid(const CStdString &url, bool updateable);

virtual void OnJobComplete(unsigned int jobID, bool success, CJob *job);

CCriticalSection m_databaseSection;
Expand Down
34 changes: 12 additions & 22 deletions xbmc/TextureDatabase.cpp
Expand Up @@ -169,36 +169,26 @@ bool CTextureDatabase::GetCachedTexture(const CStdString &url, CTextureDetails &
return false;
}

bool CTextureDatabase::SetCachedTextureValid(const CStdString &url, bool updateable)
{
CStdString date = updateable ? CDateTime::GetCurrentDateTime().GetAsDBDateTime() : "";
CStdString sql = PrepareSQL("UPDATE texture SET lasthashcheck='%s' WHERE url='%s'", date.c_str(), url.c_str());
return ExecuteQuery(sql);
}

bool CTextureDatabase::AddCachedTexture(const CStdString &url, const CTextureDetails &details)
{
try
{
if (NULL == m_pDB.get()) return false;
if (NULL == m_pDS.get()) return false;

CStdString cacheURL(details.file);
CStdString date = details.updateable ? CDateTime::GetCurrentDateTime().GetAsDBDateTime() : "";
CStdString sql = PrepareSQL("DELETE FROM texture WHERE url='%s'", url.c_str());
m_pDS->exec(sql.c_str());

CStdString sql = PrepareSQL("select id,cachedurl from texture where url='%s'", url.c_str());
m_pDS->query(sql.c_str());
if (!m_pDS->eof())
{ // update
int textureID = m_pDS->fv(0).get_asInt();
if (cacheURL.IsEmpty())
cacheURL = m_pDS->fv(1).get_asString();
m_pDS->close();
if (!details.hash.empty())
sql = PrepareSQL("update texture set cachedurl='%s', usecount=1, lastusetime=CURRENT_TIMESTAMP, imagehash='%s', lasthashcheck='%s' where id=%u", cacheURL.c_str(), details.hash.c_str(), date.c_str(), textureID);
else
sql = PrepareSQL("update texture set cachedurl='%s', usecount=1, lastusetime=CURRENT_TIMESTAMP where id=%u", cacheURL.c_str(), textureID);
m_pDS->exec(sql.c_str());
}
else if (!cacheURL.IsEmpty())
{ // add the texture
m_pDS->close();
sql = PrepareSQL("insert into texture (id, url, cachedurl, usecount, lastusetime, imagehash, lasthashcheck) values(NULL, '%s', '%s', 1, CURRENT_TIMESTAMP, '%s', '%s')", url.c_str(), cacheURL.c_str(), details.hash.c_str(), date.c_str());
m_pDS->exec(sql.c_str());
}
CStdString date = details.updateable ? CDateTime::GetCurrentDateTime().GetAsDBDateTime() : "";
sql = PrepareSQL("insert into texture (id, url, cachedurl, usecount, lastusetime, imagehash, lasthashcheck) values(NULL, '%s', '%s', 1, CURRENT_TIMESTAMP, '%s', '%s')", url.c_str(), details.file.c_str(), details.hash.c_str(), date.c_str());
m_pDS->exec(sql.c_str());
}
catch (...)
{
Expand Down
1 change: 1 addition & 0 deletions xbmc/TextureDatabase.h
Expand Up @@ -33,6 +33,7 @@ class CTextureDatabase : public CDatabase

bool GetCachedTexture(const CStdString &originalURL, CTextureDetails &details);
bool AddCachedTexture(const CStdString &originalURL, const CTextureDetails &details);
bool SetCachedTextureValid(const CStdString &originalURL, bool updateable);
bool ClearCachedTexture(const CStdString &originalURL, CStdString &cacheFile);
bool IncrementUseCount(const CTextureDetails &details);

Expand Down

0 comments on commit 6b1dc99

Please sign in to comment.