From 35a5f956576b9316c61c9119f1e2119299e2597a Mon Sep 17 00:00:00 2001 From: CrystalP Date: Thu, 18 Apr 2024 09:42:48 -0400 Subject: [videodb] Remove KeepId from DeleteMovie DeleteMovie behaves very differently with KeepId=true and turns it into a "DeleteStreams" function. To simplify the logic, remove the parameter and make the only caller do the stream deletion directly. --- xbmc/video/VideoDatabase.cpp | 68 ++++++++++++--------------- xbmc/video/VideoDatabase.h | 1 - xbmc/video/jobs/VideoLibraryRefreshingJob.cpp | 2 +- 3 files changed, 31 insertions(+), 40 deletions(-) diff --git a/xbmc/video/VideoDatabase.cpp b/xbmc/video/VideoDatabase.cpp index 686dbbbe3d..432135b41c 100644 --- a/xbmc/video/VideoDatabase.cpp +++ b/xbmc/video/VideoDatabase.cpp @@ -2624,7 +2624,10 @@ int CVideoDatabase::SetDetailsForMovie(CVideoInfoTag& details, idMovie = GetMovieId(filePath); if (idMovie > -1) - DeleteMovie(idMovie, true); // true to keep the table entry + { + const int idFile{GetDbId(PrepareSQL("SELECT idFile FROM movie WHERE idMovie=%i", idMovie))}; + DeleteStreamDetails(idFile); + } else { // only add a new movie if we don't already have a valid idMovie @@ -3701,9 +3704,7 @@ void CVideoDatabase::DeleteBookMarkForEpisode(const CVideoInfoTag& tag) } //******************************************************************************************************************************** -void CVideoDatabase::DeleteMovie(int idMovie, - bool bKeepId /* = false */, - DeleteMovieCascadeAction ca /* = ALL_ASSETS */) +void CVideoDatabase::DeleteMovie(int idMovie, DeleteMovieCascadeAction ca /* = ALL_ASSETS */) { if (idMovie < 0) return; @@ -3717,55 +3718,46 @@ void CVideoDatabase::DeleteMovie(int idMovie, BeginTransaction(); - int idFile = GetDbId(PrepareSQL("SELECT idFile FROM movie WHERE idMovie=%i", idMovie)); + const int idFile{GetDbId(PrepareSQL("SELECT idFile FROM movie WHERE idMovie=%i", idMovie))}; DeleteStreamDetails(idFile); - // keep the movie table entry, linking to tv shows, and bookmarks - // so we can update the data in place - // the ancillary tables are still purged - if (!bKeepId) - { - const std::string path = GetSingleValue(PrepareSQL( - "SELECT strPath FROM path JOIN files ON files.idPath=path.idPath WHERE files.idFile=%i", - idFile)); - if (!path.empty()) - InvalidatePathHash(path); + const std::string path = GetSingleValue(PrepareSQL( + "SELECT strPath FROM path JOIN files ON files.idPath=path.idPath WHERE files.idFile=%i", + idFile)); + if (!path.empty()) + InvalidatePathHash(path); - const std::string strSQL = PrepareSQL("delete from movie where idMovie=%i", idMovie); - m_pDS->exec(strSQL); + const std::string strSQL{PrepareSQL("DELETE FROM movie WHERE idMovie=%i", idMovie)}; + m_pDS->exec(strSQL); - if (ca == DeleteMovieCascadeAction::ALL_ASSETS) - { - // The default version of the movie was removed by a delete trigger. - // Clean up the other assets attached to the movie, if any. + if (ca == DeleteMovieCascadeAction::ALL_ASSETS) + { + // The default version of the movie was removed by a delete trigger. + // Clean up the other assets attached to the movie, if any. - // need local dataset due to nested DeleteVideoAsset query - std::unique_ptr pDS{m_pDB->CreateDataset()}; + // need local dataset due to nested DeleteVideoAsset query + const std::unique_ptr pDS{m_pDB->CreateDataset()}; - pDS->query( - PrepareSQL("SELECT idFile FROM videoversion WHERE idMedia=%i AND media_type='%s'", - idMovie, MediaTypeMovie)); + pDS->query(PrepareSQL("SELECT idFile FROM videoversion WHERE idMedia=%i AND media_type='%s'", + idMovie, MediaTypeMovie)); - while (!pDS->eof()) + while (!pDS->eof()) + { + if (!DeleteVideoAsset(pDS->fv(0).get_asInt())) { - if (!DeleteVideoAsset(pDS->fv(0).get_asInt())) - { - RollbackTransaction(); - pDS->close(); - return; - } - pDS->next(); + RollbackTransaction(); + pDS->close(); + return; } - pDS->close(); + pDS->next(); } + pDS->close(); } //! @todo move this below CommitTransaction() once UPnP doesn't rely on this anymore - if (!bKeepId) - AnnounceRemove(MediaTypeMovie, idMovie); + AnnounceRemove(MediaTypeMovie, idMovie); CommitTransaction(); - } catch (...) { diff --git a/xbmc/video/VideoDatabase.h b/xbmc/video/VideoDatabase.h index 9c220a47b7..59d7dc5f9e 100644 --- a/xbmc/video/VideoDatabase.h +++ b/xbmc/video/VideoDatabase.h @@ -604,7 +604,6 @@ public: int UpdateDetailsForMovie(int idMovie, CVideoInfoTag& details, const std::map &artwork, const std::set &updatedDetails); void DeleteMovie(int idMovie, - bool bKeepId = false, DeleteMovieCascadeAction action = DeleteMovieCascadeAction::ALL_ASSETS); void DeleteTvShow(int idTvShow, bool bKeepId = false); void DeleteTvShow(const std::string& strPath); diff --git a/xbmc/video/jobs/VideoLibraryRefreshingJob.cpp b/xbmc/video/jobs/VideoLibraryRefreshingJob.cpp index 958a6f970c..35b5c09445 100644 --- a/xbmc/video/jobs/VideoLibraryRefreshingJob.cpp +++ b/xbmc/video/jobs/VideoLibraryRefreshingJob.cpp @@ -333,7 +333,7 @@ bool CVideoLibraryRefreshingJob::Work(CVideoDatabase &db) if (origDbId > 0) { if (scraper->Content() == CONTENT_MOVIES) - db.DeleteMovie(origDbId, false, DeleteMovieCascadeAction::DEFAULT_VERSION); + db.DeleteMovie(origDbId, DeleteMovieCascadeAction::DEFAULT_VERSION); else if (scraper->Content() == CONTENT_MUSICVIDEOS) db.DeleteMusicVideo(origDbId); else if (scraper->Content() == CONTENT_TVSHOWS) -- cgit v1.2.3 From 91bec7604e04db52437cd7fc9853f6bf37dce4a5 Mon Sep 17 00:00:00 2001 From: CrystalP Date: Thu, 18 Apr 2024 09:59:15 -0400 Subject: [videodb] Remove redundant deletion of stream details when setting movie details SetStreamDetailsForFileId clears existing details unconditionnally, then adds the new ones. Added doxygen. --- xbmc/video/VideoDatabase.cpp | 9 +-------- xbmc/video/VideoDatabase.h | 5 +++++ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/xbmc/video/VideoDatabase.cpp b/xbmc/video/VideoDatabase.cpp index 432135b41c..4d09ed4553 100644 --- a/xbmc/video/VideoDatabase.cpp +++ b/xbmc/video/VideoDatabase.cpp @@ -2623,16 +2623,9 @@ int CVideoDatabase::SetDetailsForMovie(CVideoInfoTag& details, if (idMovie < 0) idMovie = GetMovieId(filePath); - if (idMovie > -1) - { - const int idFile{GetDbId(PrepareSQL("SELECT idFile FROM movie WHERE idMovie=%i", idMovie))}; - DeleteStreamDetails(idFile); - } - else + if (idMovie == -1) { // only add a new movie if we don't already have a valid idMovie - // (DeleteMovie is called with bKeepId == true so the movie won't - // be removed from the movie table) idMovie = AddNewMovie(details); if (idMovie < 0) { diff --git a/xbmc/video/VideoDatabase.h b/xbmc/video/VideoDatabase.h index 59d7dc5f9e..d5db8f459b 100644 --- a/xbmc/video/VideoDatabase.h +++ b/xbmc/video/VideoDatabase.h @@ -591,6 +591,11 @@ public: const std::map& artwork, int idMVideo = -1); int SetStreamDetailsForFile(const CStreamDetails& details, const std::string& strFileNameAndPath); + /*! + * \brief Clear any existing stream details and add the new provided details to a file. + * \param[in] details New stream details + * \param[in] idFile Identifier of the file + */ void SetStreamDetailsForFileId(const CStreamDetails& details, int idFile); bool SetSingleValue(VideoDbContentType type, int dbId, int dbField, const std::string& strValue); -- cgit v1.2.3 From dc3b6c1a0e3642cb79a7d062749d49ea51bd2f70 Mon Sep 17 00:00:00 2001 From: CrystalP Date: Fri, 19 Apr 2024 09:40:57 -0400 Subject: [videodb] Preserve path hash when turning a movie into a version This is a small optimization: the path hash is deleted when a movie is turned into a version, and during the next libray scan, the folder of the movie is scanned (unnecessary) and the same hash is recreated. The file of the version hasn't moved or changed, the hash is still valid, might as well not delete it in the first place and save the directory scan. --- xbmc/video/VideoDatabase.cpp | 20 +++++++++++++------- xbmc/video/VideoDatabase.h | 9 ++++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/xbmc/video/VideoDatabase.cpp b/xbmc/video/VideoDatabase.cpp index 4d09ed4553..cc7bda1fd7 100644 --- a/xbmc/video/VideoDatabase.cpp +++ b/xbmc/video/VideoDatabase.cpp @@ -3697,7 +3697,9 @@ void CVideoDatabase::DeleteBookMarkForEpisode(const CVideoInfoTag& tag) } //******************************************************************************************************************************** -void CVideoDatabase::DeleteMovie(int idMovie, DeleteMovieCascadeAction ca /* = ALL_ASSETS */) +void CVideoDatabase::DeleteMovie(int idMovie, + DeleteMovieCascadeAction ca /* = ALL_ASSETS */, + DeleteMovieHashAction hashAction /* = HASH_DELETE */) { if (idMovie < 0) return; @@ -3714,11 +3716,14 @@ void CVideoDatabase::DeleteMovie(int idMovie, DeleteMovieCascadeAction ca /* = A const int idFile{GetDbId(PrepareSQL("SELECT idFile FROM movie WHERE idMovie=%i", idMovie))}; DeleteStreamDetails(idFile); - const std::string path = GetSingleValue(PrepareSQL( - "SELECT strPath FROM path JOIN files ON files.idPath=path.idPath WHERE files.idFile=%i", - idFile)); - if (!path.empty()) - InvalidatePathHash(path); + if (hashAction == DeleteMovieHashAction::HASH_DELETE) + { + const std::string path = GetSingleValue(PrepareSQL( + "SELECT strPath FROM path JOIN files ON files.idPath=path.idPath WHERE files.idFile=%i", + idFile)); + if (!path.empty()) + InvalidatePathHash(path); + } const std::string strSQL{PrepareSQL("DELETE FROM movie WHERE idMovie=%i", idMovie)}; m_pDS->exec(strSQL); @@ -12404,7 +12409,8 @@ bool CVideoDatabase::ConvertVideoToVersion(VideoDbContentType itemType, SetVideoVersionDefaultArt(idFile, dbIdSource, itemType); if (itemType == VideoDbContentType::MOVIES) - DeleteMovie(dbIdSource); + DeleteMovie(dbIdSource, DeleteMovieCascadeAction::ALL_ASSETS, + DeleteMovieHashAction::HASH_PRESERVE); } // Rename the default version diff --git a/xbmc/video/VideoDatabase.h b/xbmc/video/VideoDatabase.h index d5db8f459b..50b7feffde 100644 --- a/xbmc/video/VideoDatabase.h +++ b/xbmc/video/VideoDatabase.h @@ -414,6 +414,12 @@ enum class DeleteMovieCascadeAction ALL_ASSETS }; +enum class DeleteMovieHashAction +{ + HASH_DELETE, + HASH_PRESERVE +}; + #define COMPARE_PERCENTAGE 0.90f // 90% #define COMPARE_PERCENTAGE_MIN 0.50f // 50% @@ -609,7 +615,8 @@ public: int UpdateDetailsForMovie(int idMovie, CVideoInfoTag& details, const std::map &artwork, const std::set &updatedDetails); void DeleteMovie(int idMovie, - DeleteMovieCascadeAction action = DeleteMovieCascadeAction::ALL_ASSETS); + DeleteMovieCascadeAction action = DeleteMovieCascadeAction::ALL_ASSETS, + DeleteMovieHashAction hashAction = DeleteMovieHashAction::HASH_DELETE); void DeleteTvShow(int idTvShow, bool bKeepId = false); void DeleteTvShow(const std::string& strPath); void DeleteSeason(int idSeason, bool bKeepId = false); -- cgit v1.2.3