Commit 6a76b675 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Cache: Don't use a shared_ptr to load the cache key

This is lighter, and allows us to use the interface type instead of
the implementation type as the key() method parameter.
parent 83ffd1a6
......@@ -31,7 +31,7 @@ class IFolder
public:
virtual ~IFolder() = default;
virtual unsigned int id() const = 0;
virtual const std::string& path() = 0;
virtual const std::string& path() const = 0;
// This will only returns the files in this immediate folder
virtual std::vector<MediaPtr> files() = 0;
virtual std::vector<FolderPtr> folders() = 0;
......
......@@ -34,7 +34,7 @@ class ILabel
virtual ~ILabel() {}
virtual unsigned int id() const = 0;
virtual const std::string& name() = 0;
virtual const std::string& name() const = 0;
virtual std::vector<MediaPtr> files() = 0;
};
......
......@@ -153,7 +153,7 @@ std::shared_ptr<Album> Artist::unknownAlbum()
return nullptr;
if ( updateNbAlbum( 1 ) == false )
{
Album::destroy( m_dbConnection, album );
Album::destroy( m_dbConnection, album.get() );
return nullptr;
}
}
......
......@@ -32,7 +32,7 @@ namespace policy
const std::string FolderTable::CacheColumn = "path";
unsigned int Folder::* const FolderTable::PrimaryKey = &Folder::m_id;
const FolderCache::KeyType&FolderCache::key(const std::shared_ptr<Folder>& self)
const FolderCache::KeyType&FolderCache::key( const IFolder* self )
{
return self->path();
}
......@@ -95,7 +95,7 @@ unsigned int Folder::id() const
return m_id;
}
const std::string& Folder::path()
const std::string& Folder::path() const
{
return m_path;
}
......
......@@ -46,7 +46,7 @@ struct FolderTable
struct FolderCache
{
using KeyType = std::string;
static const KeyType& key( const std::shared_ptr<Folder>& self );
static const KeyType& key( const IFolder* self );
static KeyType key( sqlite::Row& row );
};
......@@ -64,7 +64,7 @@ public:
static std::shared_ptr<Folder> create( DBConnection connection, const std::string& path, time_t lastModificationDate, bool isRemovable, unsigned int parentId );
virtual unsigned int id() const override;
virtual const std::string& path() override;
virtual const std::string& path() const override;
virtual std::vector<MediaPtr> files() override;
virtual std::vector<FolderPtr> folders() override;
virtual FolderPtr parent() override;
......
......@@ -50,7 +50,7 @@ unsigned int Label::id() const
return m_id;
}
const std::string& Label::name()
const std::string& Label::name() const
{
return m_name;
}
......@@ -90,7 +90,7 @@ bool Label::createTable(DBConnection dbConnection)
return sqlite::Tools::executeRequest( dbConnection, req );
}
const std::string&policy::LabelCachePolicy::key( const std::shared_ptr<ILabel> self )
const std::string&policy::LabelCachePolicy::key( const ILabel* self )
{
return self->name();
}
......
......@@ -44,7 +44,7 @@ struct LabelTable
struct LabelCachePolicy
{
typedef std::string KeyType;
static const std::string& key(const std::shared_ptr<ILabel> self );
static const std::string& key(const ILabel* self );
static std::string key( sqlite::Row& row );
};
......@@ -60,7 +60,7 @@ class Label : public ILabel, public Cache<Label, ILabel, policy::LabelTable, pol
public:
virtual unsigned int id() const override;
virtual const std::string& name() override;
virtual const std::string& name() const override;
virtual std::vector<MediaPtr> files() override;
static LabelPtr create( DBConnection dbConnection, const std::string& name );
......
......@@ -348,7 +348,7 @@ bool Media::removeLabel( LabelPtr label )
return sqlite::Tools::executeDelete( m_dbConnection, req, label->id(), m_id );
}
const std::string& policy::MediaCache::key(const std::shared_ptr<Media> self )
const std::string& policy::MediaCache::key( const IMedia* self )
{
return self->mrl();
}
......
......@@ -52,7 +52,7 @@ struct MediaTable
struct MediaCache
{
typedef std::string KeyType;
static const std::string& key(const std::shared_ptr<Media> self);
static const std::string& key(const IMedia* self);
static std::string key( sqlite::Row& row );
};
}
......
......@@ -250,12 +250,12 @@ bool MediaLibrary::deleteFile( const std::string& mrl )
bool MediaLibrary::deleteFile( MediaPtr file )
{
return Media::destroy( m_dbConnection.get(), std::static_pointer_cast<Media>( file ) );
return Media::destroy( m_dbConnection.get(), file.get() );
}
bool MediaLibrary::deleteFolder( FolderPtr folder )
{
if ( Folder::destroy( m_dbConnection.get(), std::static_pointer_cast<Folder>( folder ) ) == false )
if ( Folder::destroy( m_dbConnection.get(), folder.get() ) == false )
return false;
Media::clear();
return true;
......@@ -273,7 +273,7 @@ bool MediaLibrary::deleteLabel( const std::string& text )
bool MediaLibrary::deleteLabel( LabelPtr label )
{
return Label::destroy( m_dbConnection.get(), std::static_pointer_cast<Label>( label ) );
return Label::destroy( m_dbConnection.get(), label.get() );
}
AlbumPtr MediaLibrary::album( unsigned int id )
......
......@@ -35,10 +35,6 @@ class PrimaryKeyCacheKeyPolicy
{
public:
typedef unsigned int KeyType;
static unsigned int key( const std::shared_ptr<TYPE>& self )
{
return self->id();
}
static unsigned int key( const TYPE* self )
{
return self->id();
......@@ -71,7 +67,7 @@ class PrimaryKeyCacheKeyPolicy
* - Inherit this class and specify the template parameter & policies accordingly
* - Make this class a friend class of the class you inherit from
*/
template <typename IMPL, typename INTF, typename TABLEPOLICY, typename CACHEPOLICY = PrimaryKeyCacheKeyPolicy<IMPL> >
template <typename IMPL, typename INTF, typename TABLEPOLICY, typename CACHEPOLICY = PrimaryKeyCacheKeyPolicy<INTF> >
class Cache
{
public:
......@@ -140,13 +136,7 @@ class Cache
return sqlite::Tools::executeDelete( dbConnection, req, key );
}
static bool destroy( DBConnection dbConnection, const std::shared_ptr<IMPL>& self )
{
const auto& key = CACHEPOLICY::key( self );
return destroy( dbConnection, key );
}
static bool destroy( DBConnection dbConnection, const IMPL* self )
static bool destroy( DBConnection dbConnection, const INTF* self )
{
const auto& key = CACHEPOLICY::key( self );
return destroy( dbConnection, key );
......@@ -163,7 +153,7 @@ class Cache
* @param key The key used for cache
* @return
*/
static bool discard( const std::shared_ptr<IMPL>& record )
static bool discard( const IMPL* record )
{
auto key = CACHEPOLICY::key( record );
Lock lock( Mutex );
......@@ -187,7 +177,7 @@ class Cache
if ( pKey == 0 )
return false;
(self.get())->*TABLEPOLICY::PrimaryKey = pKey;
auto cacheKey = CACHEPOLICY::key( self );
auto cacheKey = CACHEPOLICY::key( self.get() );
// We expect the cache column to be PRIMARY KEY / UNIQUE, so an insertion with
// a duplicated key should have been rejected by sqlite. This indicates an invalid state
......
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