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

Remove Cache helper

We now assume that entities are short lived and will be used either
readonly or from the same thread
parent d2bc5266
......@@ -155,7 +155,6 @@ noinst_HEADERS = \
src/ShowEpisode.h \
src/Show.h \
src/Thumbnail.h \
src/utils/Cache.h \
src/utils/Directory.h \
src/utils/Filename.h \
src/utils/ModificationsNotifier.h \
......
......@@ -141,30 +141,28 @@ const std::string& Album::artworkMrl() const
if ( m_thumbnailId == 0 )
return Thumbnail::EmptyMrl;
auto lock = m_thumbnail.lock();
if ( m_thumbnail.isCached() == false )
if ( m_thumbnail == nullptr )
{
auto thumbnail = Thumbnail::fetch( m_ml, m_thumbnailId );
if ( thumbnail == nullptr )
return Thumbnail::EmptyMrl;
m_thumbnail = std::move( thumbnail );
}
return m_thumbnail.get()->mrl();
return m_thumbnail->mrl();
}
std::shared_ptr<Thumbnail> Album::thumbnail()
{
if ( m_thumbnailId == 0 )
return nullptr;
auto lock = m_thumbnail.lock();
if ( m_thumbnail.isCached() == false )
if ( m_thumbnail == nullptr )
{
auto thumbnail = Thumbnail::fetch( m_ml, m_thumbnailId );
if ( thumbnail == nullptr )
return nullptr;
m_thumbnail = std::move( thumbnail );
}
return m_thumbnail.get();
return m_thumbnail;
}
bool Album::setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin,
......@@ -177,15 +175,14 @@ bool Album::setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin orig
std::unique_ptr<sqlite::Transaction> t;
if ( sqlite::Transaction::transactionInProgress() == false )
t = m_ml->getConn()->newTransaction();
auto lock = m_thumbnail.lock();
m_thumbnail = Thumbnail::create( m_ml, artworkMrl, Thumbnail::Origin::Album, isGenerated );
if ( m_thumbnail.get() == nullptr )
if ( m_thumbnail == nullptr )
return false;
static const std::string req = "UPDATE " + Album::Table::Name
+ " SET thumbnail_id = ? WHERE id_album = ?";
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_thumbnail.get()->id(), m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_thumbnail->id(), m_id ) == false )
return false;
m_thumbnailId = m_thumbnail.get()->id();
m_thumbnailId = m_thumbnail->id();
if ( t != nullptr )
t->commit();
return true;
......@@ -284,10 +281,9 @@ Query<IMedia> Album::tracks( GenrePtr genre, const QueryParameters* params ) con
std::vector<MediaPtr> Album::cachedTracks() const
{
auto lock = m_tracks.lock();
if ( m_tracks.isCached() == false )
if ( m_tracks.size() == 0 )
m_tracks = tracks( nullptr )->all();
return m_tracks.get();
return m_tracks;
}
Query<IMedia> Album::searchTracks( const std::string& pattern,
......@@ -310,16 +306,14 @@ std::shared_ptr<AlbumTrack> Album::addTrack( std::shared_ptr<Media> media, unsig
m_nbTracks++;
if ( media->duration() > 0 )
m_duration += media->duration();
auto lock = m_tracks.lock();
// Don't assume we have always have a valid value in m_tracks.
// While it's ok to assume that if we are currently parsing the album, we
// have a valid cache tracks, this isn't true when restarting an interrupted parsing.
// The nbTracks value will be correct however. If it's equal to one, it means we're inserting
// the first track in this album
if ( m_tracks.isCached() == false && m_nbTracks == 1 )
m_tracks.markCached();
if ( m_tracks.isCached() == true )
m_tracks.get().push_back( media );
if ( ( m_tracks.empty() == true && m_nbTracks == 1 ) ||
( m_tracks.empty() == false && m_nbTracks > 1 ) )
m_tracks.push_back( media );
return track;
}
......@@ -330,15 +324,11 @@ bool Album::removeTrack( Media& media, AlbumTrack& track )
auto genre = std::static_pointer_cast<Genre>( track.genre() );
if ( genre != nullptr )
genre->updateCachedNbTracks( -1 );
auto lock = m_tracks.lock();
if ( m_tracks.isCached() == true )
{
auto it = std::find_if( begin( m_tracks.get() ), end( m_tracks.get() ), [&media]( MediaPtr m ) {
return m->id() == media.id();
});
if ( it != end( m_tracks.get() ) )
m_tracks.get().erase( it );
}
auto it = std::find_if( begin( m_tracks ), end( m_tracks ), [&media]( MediaPtr m ) {
return m->id() == media.id();
});
if ( it != end( m_tracks ) )
m_tracks.erase( it );
return true;
}
......@@ -357,10 +347,9 @@ ArtistPtr Album::albumArtist() const
{
if ( m_artistId == 0 )
return nullptr;
auto lock = m_albumArtist.lock();
if ( m_albumArtist.isCached() == false )
if ( m_albumArtist == nullptr )
m_albumArtist = Artist::fetch( m_ml, m_artistId );
return m_albumArtist.get();
return m_albumArtist;
}
bool Album::setAlbumArtist( std::shared_ptr<Artist> artist )
......@@ -375,9 +364,9 @@ bool Album::setAlbumArtist( std::shared_ptr<Artist> artist )
return false;
if ( m_artistId != 0 )
{
if ( m_albumArtist.isCached() == false )
if ( m_albumArtist == nullptr )
albumArtist();
m_albumArtist.get()->updateNbAlbum( -1 );
m_albumArtist->updateNbAlbum( -1 );
}
m_artistId = artist->id();
m_albumArtist = artist;
......
......@@ -30,7 +30,6 @@
#include "database/DatabaseHelpers.h"
#include "medialibrary/IAlbum.h"
#include "utils/Cache.h"
#include "Thumbnail.h"
namespace medialibrary
......@@ -133,9 +132,9 @@ class Album : public IAlbum, public DatabaseHelpers<Album>
unsigned int m_duration;
bool m_isPresent;
mutable Cache<std::vector<MediaPtr>> m_tracks;
mutable Cache<std::shared_ptr<Artist>> m_albumArtist;
mutable Cache<std::shared_ptr<Thumbnail>> m_thumbnail;
mutable std::vector<MediaPtr> m_tracks;
mutable std::shared_ptr<Artist> m_albumArtist;
mutable std::shared_ptr<Thumbnail> m_thumbnail;
};
}
......
......@@ -75,12 +75,9 @@ ArtistPtr AlbumTrack::artist() const
{
if ( m_artistId == 0 )
return nullptr;
auto lock = m_artist.lock();
if ( m_artist.isCached() == false )
{
if ( m_artist == nullptr )
m_artist = Artist::fetch( m_ml, m_artistId );
}
return m_artist.get();
return m_artist;
}
int64_t AlbumTrack::artistId() const
......@@ -184,12 +181,9 @@ Query<IMedia> AlbumTrack::fromGenre( MediaLibraryPtr ml, int64_t genreId, const
GenrePtr AlbumTrack::genre()
{
auto l = m_genre.lock();
if ( m_genre.isCached() == false )
{
if ( m_genre == nullptr )
m_genre = Genre::fetch( m_ml, m_genreId );
}
return m_genre.get();
return m_genre;
}
int64_t AlbumTrack::genreId() const
......@@ -213,10 +207,13 @@ std::shared_ptr<IAlbum> AlbumTrack::album()
if ( m_albumId == 0 )
return nullptr;
auto lock = m_album.lock();
if ( m_album.isCached() == false )
m_album = Album::fetch( m_ml, m_albumId );
return m_album.get().lock();
auto album = m_album.lock();
if ( album == nullptr )
{
album = Album::fetch( m_ml, m_albumId );
m_album = album;
}
return album;
}
int64_t AlbumTrack::albumId() const
......
......@@ -30,7 +30,6 @@
#include "medialibrary/IAlbumTrack.h"
#include "medialibrary/IMediaLibrary.h"
#include "database/DatabaseHelpers.h"
#include "utils/Cache.h"
namespace medialibrary
{
......@@ -83,9 +82,9 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack>
const int64_t m_albumId;
const unsigned int m_discNumber;
mutable Cache<std::weak_ptr<Album>> m_album;
mutable Cache<std::shared_ptr<Artist>> m_artist;
mutable Cache<std::shared_ptr<Genre>> m_genre;
mutable std::weak_ptr<Album> m_album;
mutable std::shared_ptr<Artist> m_artist;
mutable std::shared_ptr<Genre> m_genre;
friend struct AlbumTrack::Table;
};
......
......@@ -178,30 +178,28 @@ const std::string& Artist::artworkMrl() const
if ( m_thumbnailId == 0 )
return Thumbnail::EmptyMrl;
auto lock = m_thumbnail.lock();
if ( m_thumbnail.isCached() == false )
if ( m_thumbnail == nullptr )
{
auto thumbnail = Thumbnail::fetch( m_ml, m_thumbnailId );
if ( thumbnail == nullptr )
return Thumbnail::EmptyMrl;
m_thumbnail = std::move( thumbnail );
}
return m_thumbnail.get()->mrl();
return m_thumbnail->mrl();
}
std::shared_ptr<Thumbnail> Artist::thumbnail()
{
if ( m_thumbnailId == 0 )
return nullptr;
auto lock = m_thumbnail.lock();
if ( m_thumbnail.isCached() == false )
if ( m_thumbnail == nullptr )
{
auto thumbnail = Thumbnail::fetch( m_ml, m_thumbnailId );
if ( thumbnail == nullptr )
return nullptr;
m_thumbnail = std::move( thumbnail );
}
return m_thumbnail.get();
return m_thumbnail;
}
bool Artist::setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin origin,
......@@ -214,15 +212,14 @@ bool Artist::setArtworkMrl( const std::string& artworkMrl, Thumbnail::Origin ori
std::unique_ptr<sqlite::Transaction> t;
if ( sqlite::Transaction::transactionInProgress() == false )
t = m_ml->getConn()->newTransaction();
auto lock = m_thumbnail.lock();
m_thumbnail = Thumbnail::create( m_ml, artworkMrl, Thumbnail::Origin::Artist, isGenerated );
if ( m_thumbnail.get() == nullptr )
if ( m_thumbnail == nullptr )
return false;
static const std::string req = "UPDATE " + Artist::Table::Name +
" SET thumbnail_id = ? WHERE id_artist = ?";
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_thumbnail.get()->id(), m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_thumbnail->id(), m_id ) == false )
return false;
m_thumbnailId = m_thumbnail.get()->id();
m_thumbnailId = m_thumbnail->id();
if ( t != nullptr )
t->commit();
return true;
......
......@@ -25,7 +25,6 @@
#include "database/DatabaseHelpers.h"
#include "medialibrary/IArtist.h"
#include "medialibrary/IMediaLibrary.h"
#include "utils/Cache.h"
#include "Thumbnail.h"
namespace medialibrary
......@@ -95,7 +94,7 @@ private:
std::string m_mbId;
bool m_isPresent;
mutable Cache<std::shared_ptr<Thumbnail>> m_thumbnail;
mutable std::shared_ptr<Thumbnail> m_thumbnail;
friend struct Artist::Table;
};
......
......@@ -97,8 +97,7 @@ const std::string& File::mrl() const
if ( m_isRemovable == false )
return m_mrl;
auto lock = m_fullPath.lock();
if ( m_fullPath.isCached() )
if ( m_fullPath.empty() == false )
return m_fullPath;
auto folder = Folder::fetch( m_ml, m_folderId );
if ( folder == nullptr )
......@@ -163,14 +162,14 @@ std::shared_ptr<Media> File::media() const
{
if ( m_mediaId == 0 )
return nullptr;
auto lock = m_media.lock();
if ( m_media.isCached() == false )
auto media = m_media.lock();
if ( media == nullptr )
{
auto media = Media::fetch( m_ml, m_mediaId );
media = Media::fetch( m_ml, m_mediaId );
assert( isDeleted() == true || media != nullptr );
m_media = media;
}
return m_media.get().lock();
return media;
}
bool File::destroy()
......
......@@ -26,7 +26,6 @@
#include "medialibrary/filesystem/IFile.h"
#include "database/DatabaseHelpers.h"
#include "database/SqliteConnection.h"
#include "utils/Cache.h"
#include "parser/Parser.h"
namespace medialibrary
......@@ -118,8 +117,8 @@ private:
const bool m_isExternal;
// Contains the full path as a MRL
mutable Cache<std::string> m_fullPath;
mutable Cache<std::weak_ptr<Media>> m_media;
mutable std::string m_fullPath;
mutable std::weak_ptr<Media> m_media;
friend File::Table;
};
......
......@@ -100,7 +100,7 @@ std::shared_ptr<Folder> Folder::create( MediaLibraryPtr ml, const std::string& m
if ( device.isRemovable() == true )
{
self->m_deviceMountpoint = deviceFs.mountpoint();
self->m_fullPath = self->m_deviceMountpoint.get() + path;
self->m_fullPath = self->m_deviceMountpoint + path;
}
return self;
}
......@@ -223,7 +223,7 @@ std::shared_ptr<Folder> Folder::fromMrl( MediaLibraryPtr ml, const std::string&
if ( folder == nullptr )
return nullptr;
folder->m_deviceMountpoint = deviceFs->mountpoint();
folder->m_fullPath = folder->m_deviceMountpoint.get() + path;
folder->m_fullPath = folder->m_deviceMountpoint + path;
return folder;
}
......@@ -237,8 +237,7 @@ const std::string& Folder::mrl() const
if ( m_isRemovable == false )
return m_path;
auto lock = m_deviceMountpoint.lock();
if ( m_deviceMountpoint.isCached() == true )
if ( m_fullPath.empty() == false )
return m_fullPath;
// We can't compute the full path of a folder if it's removable and the device isn't present.
......@@ -251,9 +250,9 @@ const std::string& Folder::mrl() const
return m_fullPath;
}
auto fsFactory = m_ml->fsFactoryForMrl( m_device.get()->scheme() );
auto fsFactory = m_ml->fsFactoryForMrl( m_device->scheme() );
assert( fsFactory != nullptr );
auto deviceFs = fsFactory->createDevice( m_device.get()->uuid() );
auto deviceFs = fsFactory->createDevice( m_device->uuid() );
// In case the device lister hasn't been updated accordingly, we might think
// a device still is present while it's not.
if( deviceFs == nullptr )
......@@ -263,7 +262,7 @@ const std::string& Folder::mrl() const
return m_fullPath;
}
m_deviceMountpoint = deviceFs->mountpoint();
m_fullPath = m_deviceMountpoint.get() + m_path;
m_fullPath = m_deviceMountpoint + m_path;
return m_fullPath;
}
......@@ -314,16 +313,15 @@ int64_t Folder::deviceId() const
bool Folder::isPresent() const
{
auto deviceLock = m_device.lock();
if ( m_device.isCached() == false )
if ( m_device == nullptr )
m_device = Device::fetch( m_ml, m_deviceId );
// There must be a device containing the folder, since we never create a folder
// without a device
assert( m_device.get() != nullptr );
assert( m_device != nullptr );
// However, handle potential sporadic errors gracefully
if( m_device.get() == nullptr )
if( m_device == nullptr )
return false;
return m_device.get()->isPresent();
return m_device->isPresent();
}
bool Folder::isBanned() const
......
......@@ -25,7 +25,6 @@
#include "medialibrary/IFolder.h"
#include "medialibrary/filesystem/IDevice.h"
#include "database/DatabaseHelpers.h"
#include "utils/Cache.h"
#include <sqlite3.h>
......@@ -99,8 +98,8 @@ private:
const int64_t m_deviceId;
const bool m_isRemovable;
mutable Cache<std::string> m_deviceMountpoint;
mutable Cache<std::shared_ptr<Device>> m_device;
mutable std::string m_deviceMountpoint;
mutable std::shared_ptr<Device> m_device;
// This contains the full path, including device mountpoint (and mrl scheme,
// as its part of the mountpoint
mutable std::string m_fullPath;
......
......@@ -125,16 +125,13 @@ AlbumTrackPtr Media::albumTrack() const
{
if ( m_subType != SubType::AlbumTrack )
return nullptr;
auto lock = m_albumTrack.lock();
if ( m_albumTrack.isCached() == false )
if ( m_albumTrack == nullptr )
m_albumTrack = AlbumTrack::fromMedia( m_ml, m_id );
return m_albumTrack.get();
return m_albumTrack;
}
void Media::setAlbumTrack( AlbumTrackPtr albumTrack )
{
auto lock = m_albumTrack.lock();
m_albumTrack = albumTrack;
m_subType = SubType::AlbumTrack;
m_changed = true;
......@@ -158,15 +155,13 @@ ShowEpisodePtr Media::showEpisode() const
if ( m_subType != SubType::ShowEpisode )
return nullptr;
auto lock = m_showEpisode.lock();
if ( m_showEpisode.isCached() == false )
if ( m_showEpisode == nullptr )
m_showEpisode = ShowEpisode::fromMedia( m_ml, m_id );
return m_showEpisode.get();
return m_showEpisode;
}
void Media::setShowEpisode( ShowEpisodePtr episode )
{
auto lock = m_showEpisode.lock();
m_showEpisode = episode;
m_subType = SubType::ShowEpisode;
m_changed = true;
......@@ -232,8 +227,7 @@ bool Media::setFavorite( bool favorite )
const std::vector<FilePtr>& Media::files() const
{
auto lock = m_files.lock();
if ( m_files.isCached() == false )
if ( m_files.empty() == true )
{
static const std::string req = "SELECT * FROM " + File::Table::Name
+ " WHERE media_id = ?";
......@@ -252,16 +246,13 @@ MoviePtr Media::movie() const
if ( m_subType != SubType::Movie )
return nullptr;
auto lock = m_movie.lock();
if ( m_movie.isCached() == false )
if ( m_movie == nullptr )
m_movie = Movie::fromMedia( m_ml, m_id );
return m_movie.get();
return m_movie;
}
void Media::setMovie(MoviePtr movie)
{
auto lock = m_movie.lock();
m_movie = movie;
m_subType = SubType::Movie;
m_changed = true;
......@@ -317,15 +308,14 @@ const std::string& Media::thumbnail() const
if ( m_thumbnailId == 0 || m_thumbnailGenerated == false )
return Thumbnail::EmptyMrl;
auto lock = m_thumbnail.lock();
if ( m_thumbnail.isCached() == false )
if ( m_thumbnail == nullptr )
{
auto thumbnail = Thumbnail::fetch( m_ml, m_thumbnailId );
if ( thumbnail == nullptr )
return Thumbnail::EmptyMrl;
m_thumbnail = std::move( thumbnail );
}
return m_thumbnail.get()->mrl();
return m_thumbnail->mrl();
}
bool Media::isThumbnailGenerated() const
......@@ -404,7 +394,6 @@ bool Media::setThumbnail( const std::string& thumbnailMrl, Thumbnail::Origin ori
std::unique_ptr<sqlite::Transaction> t;
if ( sqlite::Transaction::transactionInProgress() == false )
t = m_ml->getConn()->newTransaction();
auto lock = m_thumbnail.lock();
auto thumbnail = Thumbnail::create( m_ml, thumbnailMrl, origin, isGenerated );
if ( thumbnail == nullptr )
return false;
......@@ -445,45 +434,30 @@ bool Media::save()
std::shared_ptr<File> Media::addFile( const fs::IFile& fileFs, int64_t parentFolderId,
bool isFolderFsRemovable, IFile::Type type )
{
auto file = File::createFromMedia( m_ml, m_id, type, fileFs, parentFolderId, isFolderFsRemovable);
if ( file == nullptr )
return nullptr;
auto lock = m_files.lock();
if ( m_files.isCached() )
m_files.get().push_back( file );
return file;
return File::createFromMedia( m_ml, m_id, type, fileFs, parentFolderId, isFolderFsRemovable);
}
FilePtr Media::addExternalMrl( const std::string& mrl , IFile::Type type )
{
FilePtr file;
try
{
file = File::createFromMedia( m_ml, m_id, type, mrl );
return File::createFromMedia( m_ml, m_id, type, mrl );
}
catch ( const sqlite::errors::Generic& ex )
{
LOG_ERROR( "Failed to add media external MRL: ", ex.what() );
return nullptr;
}
if ( file == nullptr )
return nullptr;
auto lock = m_files.lock();
if ( m_files.isCached() )
m_files.get().push_back( file );
return file;
}
void Media::removeFile( File& file )
{
file.destroy();
auto lock = m_files.lock();
if ( m_files.isCached() == false )
return;
m_files.get().erase( std::remove_if( begin( m_files.get() ), end( m_files.get() ), [&file]( const FilePtr& f ) {
auto it = std::remove_if( begin( m_files ), end( m_files ), [&file]( const FilePtr& f ) {
return f->id() == file.id();
}));
});
if ( it != end( m_files ) )
m_files.erase( it );
}
std::string Media::addRequestJoin( const QueryParameters* params, bool forceFile,
......
......@@ -29,7 +29,6 @@
#include "File.h"
#include "Thumbnail.h"
#include "database/DatabaseHelpers.h"
#include "utils/Cache.h"
#include "medialibrary/IMetadata.h"
#include "Metadata.h"
......@@ -185,12 +184,12 @@ private:
mutable std::atomic_uint m_nbPlaylists;
// Auto fetched related properties
mutable Cache<AlbumTrackPtr> m_albumTrack;
mutable Cache<ShowEpisodePtr> m_showEpisode;
mutable Cache<MoviePtr> m_movie;
mutable Cache<std::vector<FilePtr>> m_files;
mutable AlbumTrackPtr m_albumTrack;
mutable ShowEpisodePtr m_showEpisode;
mutable MoviePtr m_movie;
mutable std::vector<FilePtr> m_files;
mutable Metadata m_metadata;
mutable Cache<std::shared_ptr<Thumbnail>> m_thumbnail;
mutable std::shared_ptr<Thumbnail> m_thumbnail;
bool m_changed;
friend Media::Table;
......
......@@ -26,7 +26,6 @@
#include "database/SqliteTools.h"
#include "database/DatabaseHelpers.h"
#include "utils/Cache.h"
namespace medialibrary
{
......
......@@ -25,7 +25,6 @@
#endif
#include "Thumbnail.h"
#include "utils/Cache.h"
#include "utils/Filename.h"
namespace medialibrary
......@@ -91,21 +90,20 @@ Thumbnail::Origin Thumbnail::origin() const
}
bool Thumbnail::setMrlFromPrimaryKey( MediaLibraryPtr ml,
Cache<std::shared_ptr<Thumbnail>>& thumbnail,
std::shared_ptr<Thumbnail>& thumbnail,
int64_t thumbnailId, std::string mrl,
Origin origin )
{
auto lock = thumbnail.lock();
if ( thumbnail.isCached() == false )
if ( thumbnail == nullptr )
{
thumbnail = Thumbnail::fetch( ml, thumbnailId );
if ( thumbnail.get() == nullptr )
if ( thumbnail == nullptr )
{
LOG_WARN( "Failed to fetch thumbnail entity #", thumbnailId );
return false;
}
}
return thumbnail.get()->update( std::move( mrl ), origin );
return thumbnail->update( std::move( mrl ), origin );
}
void Thumbnail::createTable( sqlite::Connection* dbConnection )
......
......@@ -29,9 +29,6 @@ namespace medialibrary
class Thumbnail;
template <typename T>
class Cache;
class Thumbnail : public DatabaseHelpers<Thumbnail>
{
public:
......@@ -67,7 +64,7 @@ public:
* @param thumbnailId The thumbnail primary key
*/
static bool setMrlFromPrimaryKey( MediaLibraryPtr ml,
Cache<std::shared_ptr<Thumbnail>>& thumbnail,
std::shared_ptr<Thumbnail>& thumbnail,
int64_t thumbnailId, std::string mrl,
Origin origin );
......
......@@ -67,17 +67,14 @@ std::shared_ptr<fs::IDirectory> FileSystemFactory::createDirectory( const std::s
std::shared_ptr<fs::IDevice> FileSystemFactory::createDevice( const std::string& uuid )
{
auto lock = m_deviceCache.lock();
auto it = m_deviceCache.get().find( uuid );
if ( it != end( m_deviceCache.get() ) )
auto it = m_deviceCache.find( uuid );
if ( it != end( m_deviceCache ) )
return it->second;
return nullptr;
}
std::shared_ptr<fs::IDevice> FileSystemFactory::createDeviceFromMrl( const std::string& mrl )
{
auto lock = m_deviceCache.lock();
std::string canonicalMountpoint;
try
{
......@@ -91,7 +88,7 @@ std::shared_ptr<fs::IDevice> FileSystemFactory::createDeviceFromMrl( const std::
return nullptr;
}
std::shared_ptr<fs::IDevice> res;
for ( const auto& p : m_deviceCache.get() )
for ( const auto& p : m_deviceCache )
{
if ( canonicalMountpoint.find( p.second->mountpoint() ) == 0 )
{
......@@ -107,11 +104,7 @@ void FileSystemFactory::refreshDevices()
LOG_INFO( "Refreshing devices from IDeviceLister" );
auto devices = m_deviceLister->devices();
auto lock = m_deviceCache.lock();
if ( m_deviceCache.isCached() == false )
m_deviceCache = DeviceCacheMap{};
for ( auto& devicePair : m_deviceCache.get() )
for ( auto& devicePair : m_deviceCache )
{
auto it = std::find_if( begin( devices ), end( devices ),
[&devicePair]( decltype(devices)::value_type& deviceTuple ) {
......@@ -135,7 +128,7 @@ void FileSystemFactory::refreshDevices()
const auto& mountpoint = std::get<1>( d );
const auto removable = std::get<2>( d );
LOG_INFO( "Caching device ", uuid, " mounted on ", mountpoint, ". Removable: ", removable ? "true" : "false" );
m_deviceCache.get().emplace( uuid, std::make_shared<fs::Device>( uuid, mountpoint, removable ) );
m_deviceCache.emplace( uuid, std::make_shared<fs::Device>( uuid, mountpoint, removable ) );
}
}
......
......@@ -25,7 +25,6 @@
#include "compat/Mutex.h"
#include "medialibrary/filesystem/IFileSystemFactory.h"
#include "medialibrary/Types.h"
#include "utils/Cache.h"
#include <string>
#include <unordered_map>
......@@ -54,7 +53,7 @@ namespace factory
private:
DeviceListerPtr m_deviceLister;
Cache<DeviceCacheMap> m_deviceCache;
DeviceCacheMap m_deviceCache;
};
}
......