Commit 6e65070c authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

IMediaLibraryCb: Remove the differenciation between discovery & reload

parent 69d19a6b
......@@ -102,10 +102,24 @@ public:
// Tracks are never modified after their creation, so there is no tracksModified event
virtual void onTracksDeleted( std::vector<int64_t> trackIds ) = 0;
/**
* @brief onDiscoveryStarted This callback will be invoked each time the discoverer enters a new
* entrypoint. Typically, everytime it enters a new folder
* @param entryPoint The entrypoint being discovered
* This callback can be invoked multiple times even though a single entry point was asked to be
* discovered. ie. In the case of a file system discovery, discovering a folder would make this
* callback being invoked for all subfolders
*/
virtual void onDiscoveryStarted( const std::string& entryPoint ) = 0;
/**
* @brief onDiscoveryCompleted Will be invoked when the discovery of a specified entrypoint has
* completed.
* ie. in the case of a filesystem discovery, once the folder, and all its files and subfolders
* have been discovered.
* This will also be invoked with an empty entryPoint when the initial reload of the medialibrary
* has completed.
*/
virtual void onDiscoveryCompleted( const std::string& entryPoint ) = 0;
virtual void onReloadStarted( const std::string& entryPoint ) = 0;
virtual void onReloadCompleted( const std::string& entryPoint ) = 0;
/**
* @brief onParsingStatsUpdated Called when the parser statistics are updated
*
......
......@@ -109,9 +109,9 @@ void DiscovererWorker::run()
task = m_tasks.front();
m_tasks.pop();
}
m_cb->onDiscoveryStarted( task.entryPoint );
if ( task.reload == false )
{
m_cb->onDiscoveryStarted( task.entryPoint );
for ( auto& d : m_discoverers )
{
// Assume only one discoverer can handle an entrypoint.
......@@ -128,11 +128,9 @@ void DiscovererWorker::run()
if ( m_run == false )
break;
}
m_cb->onDiscoveryCompleted( task.entryPoint );
}
else
{
m_cb->onReloadStarted( task.entryPoint );
for ( auto& d : m_discoverers )
{
try
......@@ -149,8 +147,8 @@ void DiscovererWorker::run()
if ( m_run == false )
break;
}
m_cb->onReloadCompleted( task.entryPoint );
}
m_cb->onDiscoveryCompleted( task.entryPoint );
}
LOG_INFO( "Exiting DiscovererWorker thread" );
}
......
......@@ -34,23 +34,24 @@ namespace mock
class WaitForDiscoveryComplete : public mock::NoopCallback
{
public:
virtual void onDiscoveryCompleted( const std::string& ) override
WaitForDiscoveryComplete()
: m_waitingReload( false )
{
if ( --m_nbDiscoveryToWait == 0 )
m_cond.notify_all();
}
virtual void onReloadCompleted( const std::string& ) override
virtual void onDiscoveryCompleted( const std::string& entryPoint ) override
{
if ( --m_nbReloadExpected == 0 )
m_reloadCond.notify_all();
if ( entryPoint.empty() == true && m_waitingReload == false )
return;
m_done = true;
m_cond.notify_all();
}
bool wait()
{
std::unique_lock<std::mutex> lock( m_mutex );
return m_cond.wait_for( lock, std::chrono::seconds( 5 ), [this]() {
return m_nbDiscoveryToWait == 0;
return m_done.load();
} );
}
......@@ -59,32 +60,23 @@ public:
// would probably be to have an extra IMediaLibraryCb member to signal that a discovery has been queue.
// however, in practice, this is a callback that says "yep, you've called IMediaLibrary::discover()"
// which is probably lame.
void prepareForWait(int nbExpected)
void prepareForWait()
{
m_nbDiscoveryToWait = nbExpected;
m_done = false;
m_waitingReload = false;
}
void prepareForReload()
{
m_nbReloadExpected = 1;
}
bool waitForReload()
{
std::unique_lock<std::mutex> lock( m_mutex );
return m_reloadCond.wait_for( lock, std::chrono::seconds( 5 ), [this](){
return m_nbReloadExpected == 0;
});
m_done = false;
m_waitingReload = true;
}
private:
std::atomic_int m_nbDiscoveryToWait;
std::atomic_bool m_done;
std::atomic_bool m_waitingReload;
std::condition_variable m_cond;
std::mutex m_mutex;
std::atomic_int m_nbReloadExpected;
std::condition_variable m_reloadCond;
std::mutex m_reloadMutex;
};
}
......@@ -35,9 +35,7 @@ class NoopCallback : public IMediaLibraryCb
virtual void onMediaUpdated( std::vector<MediaPtr> ) override {}
virtual void onMediaDeleted( std::vector<int64_t> ) override {}
virtual void onDiscoveryStarted(const std::string&) override {}
virtual void onDiscoveryCompleted(const std::string&) override {}
virtual void onReloadStarted( const std::string& ) override {}
virtual void onReloadCompleted( const std::string& ) override {}
virtual void onDiscoveryCompleted( const std::string& ) override {}
virtual void onArtistsAdded( std::vector<ArtistPtr> ) override {}
virtual void onArtistsModified( std::vector<ArtistPtr> ) override {}
virtual void onArtistsDeleted( std::vector<int64_t> ) override {}
......
......@@ -21,8 +21,10 @@ bool MockCallback::waitForParsingComplete()
});
}
void MockCallback::onDiscoveryCompleted(const std::string&)
void MockCallback::onDiscoveryCompleted(const std::string& entryPoint )
{
if ( entryPoint.empty() == true )
return;
std::lock_guard<std::mutex> lock( m_parsingMutex );
m_discoveryCompleted = true;
}
......
......@@ -45,7 +45,7 @@ public:
bool waitForParsingComplete();
private:
virtual void onDiscoveryCompleted(const std::string&) override;
virtual void onDiscoveryCompleted( const std::string& ) override;
virtual void onParsingStatsUpdated(uint32_t percent) override;
std::condition_variable m_parsingCompleteVar;
......
......@@ -108,7 +108,7 @@ TEST_F( DeviceEntity, SetPresent )
TEST_F( DeviceFs, RemoveDisk )
{
cbMock->prepareForWait( 1 );
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
ASSERT_TRUE( discovered );
......@@ -123,7 +123,7 @@ TEST_F( DeviceFs, RemoveDisk )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
......@@ -135,7 +135,7 @@ TEST_F( DeviceFs, RemoveDisk )
TEST_F( DeviceFs, UnmountDisk )
{
cbMock->prepareForWait( 1 );
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
ASSERT_TRUE( discovered );
......@@ -150,7 +150,7 @@ TEST_F( DeviceFs, UnmountDisk )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
......@@ -163,7 +163,7 @@ TEST_F( DeviceFs, UnmountDisk )
cbMock->prepareForReload();
Reload();
reloaded = cbMock->waitForReload();
reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
......@@ -175,7 +175,7 @@ TEST_F( DeviceFs, UnmountDisk )
TEST_F( DeviceFs, ReplugDisk )
{
cbMock->prepareForWait( 1 );
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
ASSERT_TRUE( discovered );
......@@ -190,7 +190,7 @@ TEST_F( DeviceFs, ReplugDisk )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
......@@ -202,7 +202,7 @@ TEST_F( DeviceFs, ReplugDisk )
fsMock->addDevice( device );
cbMock->prepareForReload();
Reload();
reloaded = cbMock->waitForReload();
reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
......@@ -214,7 +214,7 @@ TEST_F( DeviceFs, ReplugDisk )
TEST_F( DeviceFs, ReplugDiskWithExtraFiles )
{
cbMock->prepareForWait( 1 );
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
ASSERT_TRUE( discovered );
......@@ -226,7 +226,7 @@ TEST_F( DeviceFs, ReplugDiskWithExtraFiles )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
......@@ -237,7 +237,7 @@ TEST_F( DeviceFs, ReplugDiskWithExtraFiles )
cbMock->prepareForReload();
Reload();
reloaded = cbMock->waitForReload();
reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
......@@ -246,7 +246,7 @@ TEST_F( DeviceFs, ReplugDiskWithExtraFiles )
TEST_F( DeviceFs, RemoveAlbum )
{
cbMock->prepareForWait( 1 );
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
ASSERT_TRUE( discovered );
......@@ -279,7 +279,7 @@ TEST_F( DeviceFs, RemoveAlbum )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
albums = ml->albums( SortingCriteria::Default, false );
......@@ -290,7 +290,7 @@ TEST_F( DeviceFs, RemoveAlbum )
TEST_F( DeviceFs, PartialAlbumRemoval )
{
cbMock->prepareForWait( 1 );
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
ASSERT_TRUE( discovered );
......@@ -317,7 +317,7 @@ TEST_F( DeviceFs, PartialAlbumRemoval )
auto device = fsMock->removeDevice( RemovableDeviceUuid );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
albums = ml->albums( SortingCriteria::Default, false );
......@@ -330,7 +330,7 @@ TEST_F( DeviceFs, PartialAlbumRemoval )
TEST_F( DeviceFs, ChangeDevice )
{
cbMock->prepareForWait( 1 );
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
ASSERT_TRUE( discovered );
......@@ -352,7 +352,7 @@ TEST_F( DeviceFs, ChangeDevice )
cbMock->prepareForReload();
Reload();
auto reloaded = cbMock->waitForReload();
auto reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
// Check that new files with the same name have different IDs
......@@ -369,7 +369,7 @@ TEST_F( DeviceFs, ChangeDevice )
cbMock->prepareForReload();
Reload();
reloaded = cbMock->waitForReload();
reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
f = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
......
......@@ -65,7 +65,7 @@ class Folders : public FoldersNoDiscover
virtual void SetUp() override
{
FoldersNoDiscover::SetUp();
cbMock->prepareForWait( 1 );
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
ASSERT_TRUE( discovered );
......@@ -97,7 +97,7 @@ TEST_F( Folders, Delete )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
// Recheck folder deletion from DB:
......@@ -109,7 +109,7 @@ TEST_F( Folders, Load )
{
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
auto files = ml->files();
......@@ -118,7 +118,7 @@ TEST_F( Folders, Load )
TEST_F( FoldersNoDiscover, InvalidPath )
{
cbMock->prepareForWait( 1 );
cbMock->prepareForWait();
ml->discover( "/invalid/path" );
bool discovered = cbMock->wait();
ASSERT_TRUE( discovered );
......@@ -136,7 +136,7 @@ TEST_F( Folders, List )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
f = ml->folder( f->path() );
......@@ -183,7 +183,7 @@ TEST_F( Folders, NewFolderWithFile )
// This will trigger a reload
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
ASSERT_EQ( 4u, ml->files().size() );
......@@ -204,7 +204,7 @@ TEST_F( Folders, NewFileInSubFolder )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
ASSERT_EQ( 4u, ml->files().size() );
......@@ -223,7 +223,7 @@ TEST_F( Folders, RemoveFileFromDirectory )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
ASSERT_EQ( 2u, ml->files().size() );
......@@ -242,7 +242,7 @@ TEST_F( Folders, RemoveDirectory )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
ASSERT_EQ( 2u, ml->files().size() );
......@@ -264,7 +264,7 @@ TEST_F( Folders, UpdateFile )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
f = ml->media( filePath );
......@@ -276,7 +276,7 @@ TEST_F( Folders, UpdateFile )
TEST_F( FoldersNoDiscover, Blacklist )
{
cbMock->prepareForWait( 1 );
cbMock->prepareForWait();
ml->banFolder( mock::FileSystemFactory::SubFolder );
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
......@@ -300,7 +300,7 @@ TEST_F( Folders, BlacklistAfterDiscovery )
TEST_F( FoldersNoDiscover, RemoveFromBlacklist )
{
cbMock->prepareForWait( 1 );
cbMock->prepareForWait();
ml->banFolder( mock::FileSystemFactory::SubFolder );
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
......@@ -314,7 +314,7 @@ TEST_F( FoldersNoDiscover, RemoveFromBlacklist )
cbMock->prepareForReload();
auto res = ml->unbanFolder( mock::FileSystemFactory::SubFolder );
ASSERT_TRUE( res );
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
ASSERT_EQ( 3u, files.size() );
......@@ -330,7 +330,7 @@ TEST_F( FoldersNoDiscover, NoMediaBeforeDiscovery )
fsMock->addFile( newFolder + "newfile.avi" );
fsMock->addFile( newFolder + ".nomedia" );
cbMock->prepareForWait( 1 );
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
ASSERT_TRUE( discovered );
......@@ -348,7 +348,7 @@ TEST_F( Folders, InsertNoMedia )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
......@@ -361,7 +361,7 @@ TEST_F( Folders, InsertNoMediaInRoot )
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
auto files = ml->files();
......@@ -376,7 +376,7 @@ TEST_F( Folders, ReloadSubDir )
cbMock->prepareForReload();
ml->reload( mock::FileSystemFactory::SubFolder );
bool reloaded = cbMock->waitForReload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
......@@ -384,7 +384,7 @@ TEST_F( Folders, ReloadSubDir )
cbMock->prepareForReload();
ml->reload();
reloaded = cbMock->waitForReload();
reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
......
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