Commit 4450beec authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Ensure we can fetch an entity before locking the cache.

This also fixes an assertion when refusing to fetch an entity because of
a failed precondition, since we wouldn't cache the entity.
But moreover, it reduces contention
parent cbc2c13d
......@@ -63,8 +63,10 @@ unsigned int AlbumTrack::id() const
ArtistPtr AlbumTrack::artist() const
{
if ( m_artistId == 0 )
return nullptr;
auto lock = m_artist.lock();
if ( m_artist.isCached() == false && m_artistId != 0 )
if ( m_artist.isCached() == false )
{
m_artist = Artist::fetch( m_dbConnection, m_artistId );
}
......
......@@ -92,9 +92,11 @@ std::shared_ptr<Media> Media::create( DBConnection dbConnection, Type type, cons
AlbumTrackPtr Media::albumTrack() const
{
if ( m_subType != SubType::AlbumTrack )
return nullptr;
auto lock = m_albumTrack.lock();
if ( m_albumTrack.isCached() == false && m_subType == SubType::AlbumTrack )
if ( m_albumTrack.isCached() == false )
m_albumTrack = AlbumTrack::fromMedia( m_dbConnection, m_id );
return m_albumTrack.get();
}
......@@ -122,9 +124,11 @@ void Media::setDuration( int64_t duration )
ShowEpisodePtr Media::showEpisode() const
{
auto lock = m_showEpisode.lock();
if ( m_subType != SubType::ShowEpisode )
return nullptr;
if ( m_showEpisode.isCached() == false && m_subType == SubType::ShowEpisode )
auto lock = m_showEpisode.lock();
if ( m_showEpisode.isCached() == false )
m_showEpisode = ShowEpisode::fromMedia( m_dbConnection, m_id );
return m_showEpisode.get();
}
......@@ -196,9 +200,12 @@ const std::vector<FilePtr>& Media::files() const
MoviePtr Media::movie() const
{
if ( m_subType != SubType::Movie )
return nullptr;
auto lock = m_movie.lock();
if ( m_movie.isCached() == false && m_subType == SubType::Movie )
if ( m_movie.isCached() == false )
m_movie = Movie::fromMedia( m_dbConnection, m_id );
return m_movie.get();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment