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

Parser: Simplify progress notification

parent 148ab323
......@@ -61,13 +61,11 @@ public:
/**
* @brief onParsingStatsUpdated Called when the parser statistics are updated
*
* There is no waranty about how often this will be called. However: when
* nbParsed == nbToParse, the callback will be invoked, and nbToParse will be reset to 0
* There is no waranty about how often this will be called.
* @param percent The progress percentage [0,100]
*
* @param nbParsed The number of media that have been parsed
* @param nbToParse The total number of media to parse.
*/
virtual void onParsingStatsUpdated( uint32_t nbParsed, uint32_t nbToParse ) = 0;
virtual void onParsingStatsUpdated( uint32_t percent) = 0;
};
class IMediaLibrary
......
......@@ -30,8 +30,9 @@ Parser::Parser(DBConnection dbConnection , IMediaLibraryCb* cb)
: m_stopParser( false )
, m_dbConnection( dbConnection )
, m_callback( cb )
, m_nbParsed( 0 )
, m_nbToParse( 0 )
, m_opToDo( 0 )
, m_opDone( 0 )
, m_percent( 0 )
, m_paused( false )
{
}
......@@ -72,7 +73,8 @@ void Parser::parse( std::shared_ptr<Media> file )
if ( m_services.size() == 0 )
return;
m_tasks.push( new Task( file, m_services, m_callback ) );
updateStats( false, true );
++m_opToDo;
updateStats();
if ( m_paused == false )
m_cond.notify_all();
}
......@@ -153,31 +155,16 @@ void Parser::restore()
}
}
void Parser::updateStats( bool newMediaParsed, bool newMediaQueued )
void Parser::updateStats()
{
if ( m_callback == nullptr )
return;
if ( newMediaParsed == true )
m_nbParsed++;
else if ( newMediaQueued == true )
m_nbToParse++;
else
assert(false);
if ( m_nbParsed == m_nbToParse )
{
m_callback->onParsingStatsUpdated( m_nbParsed, m_nbToParse );
m_nbParsed = 0;
m_nbToParse = 0;
return;
}
// Only send an update every X new elem
const uint32_t NbElems = 3;
if ( ( newMediaParsed == true && m_nbParsed % NbElems == 0 ) ||
( newMediaQueued == true && m_nbToParse % NbElems == 0 ) )
auto percent = m_opToDo > 0 ? ( m_opDone * 100 / m_opToDo ) : 0;
if ( percent != m_percent )
{
m_callback->onParsingStatsUpdated( m_nbParsed, m_nbToParse );
m_percent = percent;
m_callback->onParsingStatsUpdated( m_percent );
}
}
......@@ -197,7 +184,8 @@ void Parser::done(std::shared_ptr<Media> file, IMetadataService::Status status,
if ( status == IMetadataService::Status::TemporaryUnavailable ||
status == IMetadataService::Status::Fatal )
{
updateStats( true, false );
++m_opDone;
updateStats();
delete t;
return;
}
......@@ -210,7 +198,8 @@ void Parser::done(std::shared_ptr<Media> file, IMetadataService::Status status,
++t->it;
if (t->it == t->end)
{
updateStats( true, false );
++m_opDone;
updateStats();
file->markParsed();
delete t;
return;
......
......@@ -49,7 +49,7 @@ class Parser : public IMetadataServiceCb
void run();
// Queues all unparsed files for parsing.
void restore();
void updateStats(bool newMediaParsed, bool newMediaQueue);
void updateStats();
private:
typedef std::unique_ptr<IMetadataService> ServicePtr;
......@@ -72,8 +72,9 @@ class Parser : public IMetadataServiceCb
std::atomic_bool m_stopParser;
DBConnection m_dbConnection;
IMediaLibraryCb* m_callback;
std::atomic_uint m_nbParsed;
std::atomic_uint m_nbToParse;
std::atomic_uint m_opToDo;
std::atomic_uint m_opDone;
std::atomic_uint m_percent;
bool m_paused;
};
......
......@@ -61,7 +61,7 @@ public:
m_reloadCond.notify_all();
}
virtual void onParsingStatsUpdated( uint32_t, uint32_t ) {}
virtual void onParsingStatsUpdated( uint32_t ) override {}
bool wait()
{
......
......@@ -26,9 +26,9 @@ void MockCallback::onDiscoveryCompleted(const std::string&)
m_discoveryCompleted = true;
}
void MockCallback::onParsingStatsUpdated(uint32_t nbParsed, uint32_t nbToParse)
void MockCallback::onParsingStatsUpdated(uint32_t percent)
{
if ( nbParsed == nbToParse && nbToParse > 0 )
if ( percent == 100 )
{
std::lock_guard<std::mutex> lock( m_parsingMutex );
if ( m_discoveryCompleted == false )
......
......@@ -27,7 +27,7 @@ private:
void onDiscoveryCompleted(const std::string&);
void onReloadStarted() {}
void onReloadCompleted() {}
void onParsingStatsUpdated(uint32_t nbParsed, uint32_t nbToParse);
void onParsingStatsUpdated(uint32_t percent);
std::condition_variable m_parsingCompleteVar;
std::mutex m_parsingMutex;
......
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