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

MediaGroup: Expose the total number of media when calling nbMedia()

No functional change for now, but what nbVideo/nbAudio/nbUnknown means
will change in the next commit(s)
refs #251
parent f462cc04
......@@ -46,7 +46,15 @@ public:
*/
virtual const std::string& name() const = 0;
/**
* @brief nbMedia Returns the number of media in this group
* @brief nbTotalMedia Returns the number of media in this group, not accounting
* for their presence.
*
* Even if all this group's media are missing, this will still return a non
* 0 count.
*/
virtual uint32_t nbTotalMedia() const = 0;
/**
* @brief nbMedia Returns the number of present media in this group
*/
virtual uint32_t nbMedia() const = 0;
/**
......
......@@ -106,6 +106,11 @@ uint32_t MediaGroup::nbMedia() const
return m_nbVideo + m_nbAudio + m_nbUnknown;
}
uint32_t MediaGroup::nbTotalMedia() const
{
return m_nbMedia;
}
uint32_t MediaGroup::nbVideo() const
{
return m_nbVideo;
......@@ -192,6 +197,7 @@ bool MediaGroup::add( int64_t mediaId, bool initForceSingleton )
}
if ( t != nullptr )
t->commit();
++m_nbMedia;
m_lastModificationDate = time( nullptr );
return Media::setMediaGroup( m_ml, mediaId, m_id );
}
......@@ -227,6 +233,7 @@ bool MediaGroup::remove( IMedia& media )
--m_nbUnknown;
break;
}
--m_nbMedia;
if ( media.duration() > 0 )
m_duration -= media.duration();
return true;
......@@ -426,6 +433,8 @@ void MediaGroup::createTriggers( sqlite::Connection* connection )
trigger( Triggers::UpdateDurationOnMediaChange, Settings::DbModelVersion ) );
sqlite::Tools::executeRequest( connection,
trigger( Triggers::UpdateDurationOnMediaDeletion, Settings::DbModelVersion ) );
sqlite::Tools::executeRequest( connection,
trigger( Triggers::UpdateTotalNbMedia, Settings::DbModelVersion ) );
}
void MediaGroup::createIndexes( sqlite::Connection* connection )
......@@ -642,6 +651,7 @@ std::string MediaGroup::trigger( MediaGroup::Triggers t, uint32_t dbModel )
std::to_string( static_cast<std::underlying_type_t<IMedia::Type>>(
IMedia::Type::Unknown ) ) +
" THEN 1 ELSE 0 END),"
" nb_media = nb_media - 1,"
" last_modification_date = strftime('%s')"
" WHERE id_group = old.group_id;"
" END";
......@@ -685,6 +695,19 @@ std::string MediaGroup::trigger( MediaGroup::Triggers t, uint32_t dbModel )
" SET duration = duration - old.duration"
" WHERE id_group = old.group_id;"
" END";
case Triggers::UpdateTotalNbMedia:
{
assert( dbModel >= 26 );
return "CREATE TRIGGER " + triggerName( t, dbModel ) +
" AFTER UPDATE OF group_id ON " + Media::Table::Name +
" WHEN IFNULL(old.group_id, 0) != IFNULL(new.group_id, 0)"
" BEGIN"
" UPDATE " + Table::Name + " SET nb_media = nb_media - 1"
" WHERE old.group_id IS NOT NULL AND id_group = old.group_id;"
" UPDATE " + Table::Name + " SET nb_media = nb_media + 1"
" WHERE new.group_id IS NOT NULL AND id_group = new.group_id;"
" END";
}
default:
assert( !"Invalid trigger" );
}
......@@ -723,6 +746,9 @@ std::string MediaGroup::triggerName(MediaGroup::Triggers t, uint32_t dbModel)
case Triggers::UpdateNbMediaPerType:
assert( dbModel >= 26 );
return "media_group_update_nb_media_types";
case Triggers::UpdateTotalNbMedia:
assert( dbModel >= 26 );
return "media_group_update_total_nb_media";
default:
assert( !"Invalid trigger" );
}
......
......@@ -56,6 +56,7 @@ public:
UpdateDurationOnMediaChange,
UpdateDurationOnMediaDeletion,
UpdateNbMediaPerType,
UpdateTotalNbMedia,
};
enum class Indexes : uint8_t
{
......@@ -73,6 +74,7 @@ public:
virtual int64_t id() const override;
virtual const std::string& name() const override;
virtual uint32_t nbMedia() const override;
virtual uint32_t nbTotalMedia() const override;
virtual uint32_t nbVideo() const override;
virtual uint32_t nbAudio() const override;
virtual uint32_t nbUnknown() const override;
......
......@@ -65,6 +65,12 @@ MediaGroup::schema( MediaGroup::Table::Name, 26 ),
"DROP TRIGGER " + MediaGroup::triggerName( MediaGroup::Triggers::DecrementNbMediaOnGroupChange, 25 ),
MediaGroup::trigger( MediaGroup::Triggers::UpdateNbMediaPerType, 26 ),
"DROP TRIGGER " + MediaGroup::triggerName( MediaGroup::Triggers::DecrementNbMediaOnDeletion, 25 ),
MediaGroup::trigger( MediaGroup::Triggers::DecrementNbMediaOnDeletion, 26 ),
/* Create new MediaGroup trigger */
MediaGroup::trigger( MediaGroup::Triggers::UpdateTotalNbMedia, 26 ),
/* Recreate MediaGroup indexes & triggers that were deleted during the migration */
MediaGroup::trigger( MediaGroup::Triggers::InsertFts, 26 ),
MediaGroup::trigger( MediaGroup::Triggers::DeleteFts, 26 ),
......
......@@ -208,9 +208,6 @@ TEST_F( MediaGroups, UpdateNbMediaTypeChange )
ASSERT_EQ( 0u, group2->nbVideo() );
ASSERT_EQ( 0u, group2->nbUnknown() );
// Insert an unknown media in a group
// Also insert a media for each group, to avoid their count to reach 0 which
// would cause the group to be deleted
auto m = ml->addMedia( "media.mkv", IMedia::Type::Unknown );
auto m2 = ml->addMedia( "media2.avi", IMedia::Type::Video );
auto m3 = ml->addMedia( "media3.mp3", IMedia::Type::Audio );
......@@ -289,16 +286,15 @@ TEST_F( MediaGroups, UpdateNbMediaNoDelete )
ASSERT_EQ( 0u, group1->nbAudio() );
ASSERT_EQ( 0u, group1->nbVideo() );
ASSERT_EQ( 0u, group1->nbUnknown() );
ASSERT_EQ( 0u, group1->nbMedia() );
// Insert an unknown media in a group
// Also insert a media for each group, to avoid their count to reach 0 which
// would cause the group to be deleted
auto m = ml->addMedia( "media.mkv", IMedia::Type::Unknown );
group1->add( *m );
ASSERT_EQ( 0u, group1->nbAudio() );
ASSERT_EQ( 0u, group1->nbVideo() );
ASSERT_EQ( 1u, group1->nbUnknown() );
ASSERT_EQ( 1u, group1->nbMedia() );
// Now change the media type
m->setType( IMedia::Type::Audio );
......@@ -307,6 +303,7 @@ TEST_F( MediaGroups, UpdateNbMediaNoDelete )
ASSERT_EQ( 1u, group1->nbAudio() );
ASSERT_EQ( 0u, group1->nbVideo() );
ASSERT_EQ( 0u, group1->nbUnknown() );
ASSERT_EQ( 1u, group1->nbMedia() );
}
TEST_F( MediaGroups, SortByNbMedia )
......
......@@ -75,6 +75,7 @@ namespace
"media_group_update_duration_on_media_change",
"media_group_update_duration_on_media_deletion",
"media_group_update_nb_media_types",
"media_group_update_total_nb_media",
"media_update_device_presence",
"show_decrement_nb_episode",
"show_increment_nb_episode",
......
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