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

Make the primary key a part of the Table policy.

This allow the Cache helper to set it automatically, and avoid cache
misses when relying on the primary key as the cache column (since the
primary key was always 0)
parent 3ec6de52
......@@ -5,6 +5,7 @@
const std::string policy::AlbumTable::Name = "Album";
const std::string policy::AlbumTable::CacheColumn = "id_album";
unsigned int Album::* const policy::AlbumTable::PrimaryKey = &Album::m_id;
Album::Album(sqlite3* dbConnection, sqlite3_stmt* stmt)
: m_dbConnection( dbConnection )
......@@ -87,10 +88,8 @@ AlbumPtr Album::create( sqlite3* dbConnection, const std::string& id3Tag )
auto album = std::make_shared<Album>( id3Tag );
static const std::string& req = "INSERT INTO " + policy::AlbumTable::Name +
"(id_album, id3tag) VALUES(NULL, ?)";
auto pKey = _Cache::insert( dbConnection, album, req.c_str(), id3Tag );
if ( pKey == 0 )
if ( _Cache::insert( dbConnection, album, req.c_str(), id3Tag ) == false )
return nullptr;
album->m_id = pKey;
album->m_dbConnection = dbConnection;
return album;
}
......@@ -9,12 +9,15 @@
#include "Cache.h"
#include "IAlbum.h"
class Album;
namespace policy
{
struct AlbumTable
{
static const std::string Name;
static const std::string CacheColumn;
static unsigned int Album::*const PrimaryKey;
};
}
......@@ -50,6 +53,7 @@ class Album : public IAlbum, public Cache<Album, IAlbum, policy::AlbumTable>
std::vector<std::shared_ptr<IAlbumTrack>>* m_tracks;
friend class Cache<Album, IAlbum, policy::AlbumTable>;
friend class policy::AlbumTable;
};
#endif // ALBUM_H
......@@ -4,6 +4,7 @@
const std::string policy::AlbumTrackTable::Name = "AlbumTrack";
const std::string policy::AlbumTrackTable::CacheColumn = "id_track";
unsigned int AlbumTrack::* const policy::AlbumTrackTable::PrimaryKey = &AlbumTrack::m_id;
AlbumTrack::AlbumTrack( sqlite3* dbConnection, sqlite3_stmt* stmt )
: m_dbConnection( dbConnection )
......
......@@ -8,6 +8,7 @@
#include "Cache.h"
class Album;
class AlbumTrack;
namespace policy
{
......@@ -15,6 +16,7 @@ struct AlbumTrackTable
{
static const std::string Name;
static const std::string CacheColumn;
static unsigned int AlbumTrack::*const PrimaryKey;
};
}
......@@ -41,6 +43,7 @@ class AlbumTrack : public IAlbumTrack, public Cache<AlbumTrack, IAlbumTrack, pol
std::shared_ptr<Album> m_album;
friend class Cache<AlbumTrack, IAlbumTrack, policy::AlbumTrackTable>;
friend class policy::AlbumTrackTable;
};
#endif // ALBUMTRACK_H
......@@ -115,11 +115,12 @@ class Cache
* Create a new instance of the cache class.
*/
template <typename... Args>
static unsigned int insert( sqlite3* dbConnection, std::shared_ptr<IMPL> self, const char* req, const Args&... args )
static bool insert( sqlite3* dbConnection, std::shared_ptr<IMPL> self, const char* req, const Args&... args )
{
unsigned int pKey = SqliteTools::insert( dbConnection, req, args... );
if ( pKey == 0 )
return 0;
return false;
(self.get())->*TABLEPOLICY::PrimaryKey = pKey;
auto cacheKey = CACHEPOLICY::key( self );
std::lock_guard<std::mutex> lock( Mutex );
......@@ -127,7 +128,7 @@ class Cache
// a duplicated key should have been rejected by sqlite. This indicates an invalid state
assert( Store.find( cacheKey ) == Store.end() );
Store[cacheKey] = self;
return pKey;
return true;
}
private:
......
......@@ -11,6 +11,7 @@
const std::string policy::FileTable::Name = "File";
const std::string policy::FileTable::CacheColumn = "mrl";
unsigned int File::* const policy::FileTable::PrimaryKey = &File::m_id;
File::File( sqlite3* dbConnection, sqlite3_stmt* stmt )
: m_dbConnection( dbConnection )
......@@ -41,11 +42,10 @@ FilePtr File::create( sqlite3* dbConnection, const std::string& mrl )
auto self = std::make_shared<File>( mrl );
static const std::string req = "INSERT INTO " + policy::FileTable::Name +
" VALUES(NULL, ?, ?, ?, ?, ?, ?)";
auto pKey = _Cache::insert( dbConnection, self, req.c_str(), (int)self->m_type, self->m_duration,
bool pKey = _Cache::insert( dbConnection, self, req.c_str(), (int)self->m_type, self->m_duration,
self->m_albumTrackId, self->m_playCount, self->m_showEpisodeId, self->m_mrl );
if ( pKey == 0 )
if ( pKey == false )
return nullptr;
self->m_id = pKey;
self->m_dbConnection = dbConnection;
return self;
}
......
......@@ -19,6 +19,7 @@ struct FileTable
{
static const std::string Name;
static const std::string CacheColumn;
static unsigned int File::*const PrimaryKey;
};
struct FileCache
{
......@@ -80,6 +81,7 @@ class File : public IFile, public Cache<File, IFile, policy::FileTable, policy::
std::shared_ptr<ShowEpisode> m_showEpisode;
friend class Cache<File, IFile, policy::FileTable, policy::FileCache>;
friend class policy::FileTable;
};
#endif // FILE_H
......@@ -8,6 +8,7 @@
const std::string policy::LabelTable::Name = "Label";
const std::string policy::LabelTable::CacheColumn = "name";
unsigned int Label::* const policy::LabelTable::PrimaryKey = &Label::m_id;
Label::Label( sqlite3* dbConnection, sqlite3_stmt* stmt )
: m_dbConnection( dbConnection )
......@@ -53,11 +54,9 @@ LabelPtr Label::create( sqlite3* dbConnection, const std::string& name )
{
auto self = std::make_shared<Label>( name );
const char* req = "INSERT INTO Label VALUES(NULL, ?)";
auto pKey = _Cache::insert( dbConnection, self, req, self->m_name );
if ( pKey == 0 )
if ( _Cache::insert( dbConnection, self, req, self->m_name ) == false )
return nullptr;
self->m_dbConnection = dbConnection;
self->m_id = pKey;
return self;
}
......
......@@ -16,6 +16,7 @@ struct LabelTable
{
static const std::string Name;
static const std::string CacheColumn;
static unsigned int Label::*const PrimaryKey;
};
struct LabelCachePolicy
......@@ -49,6 +50,7 @@ class Label : public ILabel, public Cache<Label, ILabel, policy::LabelTable, pol
std::vector<std::shared_ptr<IFile>>* m_files;
friend class Cache<Label, ILabel, policy::LabelTable>;
friend class policy::LabelTable;
};
#endif // LABEL_H
......@@ -3,6 +3,7 @@
const std::string policy::ShowTable::Name = "Show";
const std::string policy::ShowTable::CacheColumn = "id_show";
unsigned int Show::* const policy::ShowTable::PrimaryKey = &Show::m_id;
Show::Show(sqlite3* dbConnection, sqlite3_stmt* stmt)
: m_dbConnection( dbConnection )
......
......@@ -6,12 +6,15 @@
#include "Cache.h"
#include "IShow.h"
class Show;
namespace policy
{
struct ShowTable
{
static const std::string Name;
static const std::string CacheColumn;
static unsigned int Show::*const PrimaryKey;
};
}
......@@ -41,6 +44,7 @@ class Show : public IShow, public Cache<Show, IShow, policy::ShowTable>
std::string m_tvdbId;
friend class Cache<Show, IShow, policy::ShowTable>;
friend class policy::ShowTable;
};
#endif // SHOW_H
......@@ -4,6 +4,7 @@
const std::string policy::ShowEpisodeTable::Name = "Show";
const std::string policy::ShowEpisodeTable::CacheColumn = "id_show";
unsigned int ShowEpisode::* const policy::ShowEpisodeTable::PrimaryKey = &ShowEpisode::m_id;
ShowEpisode::ShowEpisode( sqlite3* dbConnection, sqlite3_stmt* stmt )
: m_dbConnection( dbConnection )
......
......@@ -2,6 +2,7 @@
#define SHOWEPISODE_H
class Show;
class ShowEpisode;
#include <string>
#include <sqlite3.h>
......@@ -15,6 +16,7 @@ struct ShowEpisodeTable
{
static const std::string Name;
static const std::string CacheColumn;
static unsigned int ShowEpisode::*const PrimaryKey;
};
}
......@@ -48,6 +50,7 @@ class ShowEpisode : public IShowEpisode, public Cache<ShowEpisode, IShowEpisode,
std::shared_ptr<Show> m_show;
friend class Cache<ShowEpisode, IShowEpisode, policy::ShowEpisodeTable>;
friend class policy::ShowEpisodeTable;
};
#endif // SHOWEPISODE_H
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