Commit 08eea9cc authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Parser: Restart the parsing process where we left off

parent dc8e4f44
......@@ -72,8 +72,7 @@ const std::vector<std::string> MediaLibrary::supportedAudioExtensions {
};
MediaLibrary::MediaLibrary()
: m_parser( new Parser )
, m_discoverer( new DiscovererWorker )
: m_discoverer( new DiscovererWorker )
{
}
......@@ -115,6 +114,8 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& sna
m_snapshotPath = snapshotPath;
m_callback = mlCallback;
m_dbConnection.reset( new SqliteConnection( dbPath ) );
m_parser.reset( new Parser( m_dbConnection.get() ) );
if ( mlCallback != nullptr )
{
......
......@@ -26,9 +26,10 @@
#include "IMedia.h"
Parser::Parser()
Parser::Parser( DBConnection dbConnection )
: m_thread( &Parser::run, this )
, m_stopParser( false )
, m_dbConnection( dbConnection )
{
}
......@@ -58,19 +59,21 @@ void Parser::addService(std::unique_ptr<IMetadataService> service)
});
}
void Parser::parse(std::shared_ptr<Media> file, IMediaLibraryCb* cb)
void Parser::parse( std::shared_ptr<Media> file )
{
std::lock_guard<std::mutex> lock( m_lock );
if ( m_services.size() == 0 )
return;
m_tasks.push( new Task( file, m_services, cb ) );
m_tasks.push( new Task( file, m_services, m_callback ) );
m_cond.notify_all();
}
void Parser::run()
{
LOG_INFO("Starting Parser thread");
restore();
while ( m_stopParser == false )
{
Task* task = nullptr;
......@@ -92,6 +95,21 @@ void Parser::run()
LOG_INFO("Exiting Parser thread");
}
void Parser::restore()
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name
+ " WHERE parsed = 0";
auto media = Media::fetchAll( m_dbConnection, req );
std::lock_guard<std::mutex> lock( m_lock );
for ( auto& it : media )
{
auto m = std::static_pointer_cast<Media>( it );
//FIXME: No callback here
m_tasks.push( new Task( m, m_services, nullptr ) );
}
}
Parser::Task::Task(std::shared_ptr<Media> file, Parser::ServiceList& serviceList, IMediaLibraryCb* metadataCb )
: file(file)
......
......@@ -36,7 +36,7 @@
class Parser : public IMetadataServiceCb
{
public:
Parser();
Parser(DBConnection dbConnection);
~Parser();
void addService(std::unique_ptr<IMetadataService> service );
void parse( std::shared_ptr<Media> file, IMediaLibraryCb* cb );
......@@ -44,6 +44,8 @@ class Parser : public IMetadataServiceCb
private:
virtual void done( std::shared_ptr<Media> file, IMetadataService::Status status, void* data ) override;
void run();
// Queues all unparsed files for parsing.
void restore();
private:
typedef std::unique_ptr<IMetadataService> ServicePtr;
......@@ -64,6 +66,7 @@ class Parser : public IMetadataServiceCb
std::mutex m_lock;
std::condition_variable m_cond;
std::atomic_bool m_stopParser;
DBConnection m_dbConnection;
};
#endif // PARSER_H
Supports Markdown
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