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

MediaLibrary: Vacuum old external & stream media

parent 37c9a188
......@@ -778,4 +778,15 @@ void Media::clearHistory( MediaLibraryPtr ml )
t->commit();
}
void Media::removeOldMedia( MediaLibraryPtr ml, std::chrono::seconds maxLifeTime )
{
const std::string req = "DELETE FROM " + policy::MediaTable::Name + " "
"WHERE real_last_played_date < ? AND ( type = ? OR type = ? ) "
"AND id_media NOT IN (SELECT media_id FROM PlaylistMediaRelation)";
auto deadline = std::chrono::duration_cast<std::chrono::seconds>(
(std::chrono::system_clock::now() - maxLifeTime).time_since_epoch() );
sqlite::Tools::executeDelete( ml->getConn(), req, deadline.count(),
IMedia::Type::External, IMedia::Type::Stream );
}
}
......@@ -147,6 +147,7 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
static Query<IMedia> fetchStreamHistory( MediaLibraryPtr ml );
static void clearHistory( MediaLibraryPtr ml );
static void removeOldMedia( MediaLibraryPtr ml, std::chrono::seconds maxLifeTime );
private:
static std::string sortRequest( const QueryParameters* params );
......
......@@ -370,6 +370,7 @@ bool MediaLibrary::start()
// Now that we know which devices are plugged, check for outdated devices
// Approximate 6 months for old device precision.
Device::removeOldDevices( this, std::chrono::seconds{ 3600 * 24 * 30 * 6 } );
Media::removeOldMedia( this, std::chrono::seconds{ 3600 * 24 * 30 * 6 } );
startDiscoverer();
if ( startParser() == false )
......
......@@ -220,3 +220,10 @@ void MediaLibraryTester::outdateAllDevices()
std::string req = "UPDATE " + policy::DeviceTable::Name + " SET last_seen = 1";
sqlite::Tools::executeUpdate( getConn(), req );
}
void MediaLibraryTester::outdateAllExternalMedia()
{
std::string req = "UPDATE " + policy::MediaTable::Name + " SET real_last_played_date = 1 "
"WHERE type = ? OR type = ?";
sqlite::Tools::executeUpdate( getConn(), req, IMedia::Type::External, IMedia::Type::Stream );
}
......@@ -78,6 +78,7 @@ public:
MediaPtr addMedia( const std::string& mrl );
void deleteMedia( int64_t mediaId );
void outdateAllDevices();
void outdateAllExternalMedia();
private:
std::shared_ptr<fs::IDirectory> dummyDirectory;
......
......@@ -35,6 +35,7 @@
#include "mocks/FileSystem.h"
#include "mocks/DiscovererCbMock.h"
#include "compat/Thread.h"
#include "Playlist.h"
class Medias : public Tests
{
......@@ -727,6 +728,28 @@ TEST_F( Medias, SearchExternal )
ASSERT_EQ( 2u, media.size() );
}
TEST_F( Medias, VacuumOldExternal )
{
auto m1 = ml->addExternalMedia( "foo.avi" );
auto m2 = ml->addExternalMedia( "bar.mp3" );
auto s1 = ml->addStream( "http://baz.mkv" );
auto playlist = ml->createPlaylist( "playlist" );
playlist->append( *m1 );
ml->outdateAllExternalMedia();
Reload();
m1 = ml->media( m1->id() );
m2 = ml->media( m2->id() );
s1 = ml->media( s1->id() );
ASSERT_NE( nullptr, m1 );
ASSERT_EQ( nullptr, m2 );
ASSERT_EQ( nullptr, s1 );
}
class FetchMedia : public Tests
{
protected:
......
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