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

Item: Channel the file changes back to the Task

Incidentally, this fixes a missing file_id update when associating a
file with a playlist
parent c6ba8ad5
......@@ -131,8 +131,9 @@ parser::Task::Status MetadataParser::run( parser::Task& task )
return parser::Task::Status::Fatal;
}
task.item().setMedia( std::move( m ) );
// Will invoke ITaskCb::updateFileId to upadte m_fileId & its
// representation in DB
task.item().setFile( std::move( file ) );
task.updateFileId();
t->commit();
}
// Voluntarily trigger an exception for a valid, but less common case, to avoid database overhead
......@@ -275,6 +276,8 @@ bool MetadataParser::addPlaylistMedias( parser::Task& task ) const
LOG_ERROR( "Failed to add playlist file ", mrl );
return false;
}
// Will invoke ITaskCb::updateFileId to upadte m_fileId & its
// representation in DB
task.item().setFile( std::move( file ) );
t->commit();
}
......
......@@ -62,7 +62,7 @@ Task::Task( MediaLibraryPtr ml, sqlite::Row& row )
>> m_parentFolderId
>> m_parentPlaylistId
>> parentPlaylistIndex;
m_item = Item{ std::move( mrl ), parentPlaylistIndex };
m_item = Item{ this, std::move( mrl ), parentPlaylistIndex };
}
Task::Task( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
......@@ -74,7 +74,7 @@ Task::Task( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
, m_ml( ml )
, m_step( ParserStep::None )
, m_fileId( 0 )
, m_item( std::move( fileFs ), std::move( parentFolder ),
, m_item( this, std::move( fileFs ), std::move( parentFolder ),
std::move( parentFolderFs ), std::move( parentPlaylist ), parentPlaylistIndex )
{
}
......@@ -114,15 +114,21 @@ void Task::startParserStep()
sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_id );
}
bool Task::updateFileId()
bool Task::updateFileId( int64_t fileId )
{
// When restoring a task, we will invoke ITaskCb::updateFileId while the
// task already knows the fileId (since we're using it to restore the file instance)
// In this case, bail out. Otherwise, it is not expected for the task to change
// its associated file during the processing.
if ( m_fileId == fileId && fileId != 0 )
return true ;
assert( m_fileId == 0 );
assert( m_item.file() != nullptr && m_item.file()->id() != 0 );
assert( fileId != 0 );
static const std::string req = "UPDATE " + policy::TaskTable::Name + " SET "
"file_id = ? WHERE id_task = ?";
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_item.file()->id(), m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, fileId, m_id ) == false )
return false;
m_fileId = m_item.file()->id();
m_fileId = fileId;
return true;
}
......@@ -136,18 +142,20 @@ Task::Item& Task::item()
return m_item;
}
Task::Item::Item( std::string mrl, unsigned int subitemPosition )
: m_mrl( std::move( mrl ) )
Task::Item::Item( ITaskCb* taskCb, std::string mrl, unsigned int subitemPosition )
: m_taskCb( taskCb )
, m_mrl( std::move( mrl ) )
, m_duration( 0 )
, m_parentPlaylistIndex( subitemPosition )
{
}
Task::Item::Item( std::shared_ptr<fs::IFile> fileFs,
Task::Item::Item( ITaskCb* taskCb, std::shared_ptr<fs::IFile> fileFs,
std::shared_ptr<Folder> parentFolder,
std::shared_ptr<fs::IDirectory> parentFolderFs,
std::shared_ptr<Playlist> parentPlaylist, unsigned int parentPlaylistIndex )
: m_mrl( fileFs->mrl() )
: m_taskCb( taskCb )
, m_mrl( fileFs->mrl() )
, m_duration( 0 )
, m_fileFs( std::move( fileFs ) )
, m_parentFolder( std::move( parentFolder ) )
......@@ -187,7 +195,7 @@ const std::vector<Task::Item>& Task::Item::subItems() const
Task::Item& Task::Item::createSubItem( std::string mrl, unsigned int playlistIndex )
{
m_subItems.emplace_back( std::move( mrl ), playlistIndex );
m_subItems.emplace_back( nullptr, std::move( mrl ), playlistIndex );
return m_subItems.back();
}
......@@ -226,9 +234,11 @@ std::shared_ptr<File> Task::Item::file()
return m_file;
}
void Task::Item::setFile(std::shared_ptr<File> file)
bool Task::Item::setFile(std::shared_ptr<File> file)
{
m_file = std::move( file );
assert( m_taskCb != nullptr );
return m_taskCb->updateFileId( m_file->id() );
}
std::shared_ptr<Folder> Task::Item::parentFolder()
......@@ -371,7 +381,7 @@ bool Task::restoreLinkedEntities()
}
}
m_item = Item{ std::move( fileFs ), std::move( parentFolder ),
m_item = Item{ this, std::move( fileFs ), std::move( parentFolder ),
std::move( parentFolderFs ), std::move( parentPlaylist ),
m_item.parentPlaylistIndex() };
if ( file != nullptr )
......
......@@ -63,7 +63,14 @@ struct TaskTable
namespace parser
{
class Task : public DatabaseHelpers<Task, policy::TaskTable, cachepolicy::Uncached<Task>>
class ITaskCb
{
public:
virtual ~ITaskCb() = default;
virtual bool updateFileId( int64_t fileId ) = 0;
};
class Task : public DatabaseHelpers<Task, policy::TaskTable, cachepolicy::Uncached<Task>>, private ITaskCb
{
public:
enum class Status
......@@ -100,8 +107,8 @@ public:
*
* The position is used to keep subitems ordering for playlists
*/
Item( std::string mrl, unsigned int subitemIndex );
Item( std::shared_ptr<fs::IFile> fileFs,
Item( ITaskCb* taskCb, std::string mrl, unsigned int subitemIndex );
Item( ITaskCb* taskCb, std::shared_ptr<fs::IFile> fileFs,
std::shared_ptr<Folder> folder, std::shared_ptr<fs::IDirectory> folderFs,
std::shared_ptr<Playlist> parentPlaylist, unsigned int parentPlaylistIndex );
enum class Metadata : uint8_t
......@@ -173,7 +180,7 @@ public:
void setMedia( std::shared_ptr<Media> media );
std::shared_ptr<File> file();
void setFile( std::shared_ptr<File> file );
bool setFile( std::shared_ptr<File> file );
std::shared_ptr<Folder> parentFolder();
......@@ -186,6 +193,8 @@ public:
unsigned int parentPlaylistIndex() const;
private:
ITaskCb* m_taskCb;
std::string m_mrl;
std::unordered_map<Metadata, std::string> m_metadata;
std::vector<Item> m_subItems;
......@@ -231,7 +240,7 @@ public:
*/
void startParserStep();
bool updateFileId();
virtual bool updateFileId( int64_t fileId ) override;
int64_t id() const;
Item& item();
......
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