From 515c19307558fe925e16550dfabbb414f78bac9f Mon Sep 17 00:00:00 2001 From: da3dsoul Date: Sat, 7 Jan 2017 05:56:39 -0500 Subject: [PATCH] Clean up duplicate records of video local places on remove missing files. --- JMMServer/Entities/VideoLocal_Place.cs | 13 +++++----- JMMServer/Importer.cs | 34 +++++++++++++++++--------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/JMMServer/Entities/VideoLocal_Place.cs b/JMMServer/Entities/VideoLocal_Place.cs index 817a54717..e8572028d 100644 --- a/JMMServer/Entities/VideoLocal_Place.cs +++ b/JMMServer/Entities/VideoLocal_Place.cs @@ -164,15 +164,16 @@ public void RemoveRecord() } } - public void RemoveRecordWithOpenTransaction(ISession session, List episodesToUpdate, - List seriesToUpdate) + + public void RemoveRecordWithOpenTransaction(ISession session, ICollection episodesToUpdate, + ICollection seriesToUpdate) { logger.Info("RemoveRecordsWithoutPhysicalFiles : {0}", FullServerPath); VideoLocal v = VideoLocal; if (v.Places.Count <= 1) { - episodesToUpdate.AddRange(v.GetAnimeEpisodes()); - seriesToUpdate.AddRange(v.GetAnimeEpisodes().Select(a => a.GetAnimeSeries())); + v.GetAnimeEpisodes().ForEach(a => episodesToUpdate.Add(a)); + v.GetAnimeEpisodes().Select(a => a.GetAnimeSeries()).ToList().ForEach(a => seriesToUpdate.Add(a)); RepoFactory.VideoLocalPlace.DeleteWithOpenTransaction(session, this); RepoFactory.VideoLocal.DeleteWithOpenTransaction(session, v); CommandRequest_DeleteFileFromMyList cmdDel = @@ -181,8 +182,8 @@ public void RemoveRecordWithOpenTransaction(ISession session, List } else { - episodesToUpdate.AddRange(v.GetAnimeEpisodes()); - seriesToUpdate.AddRange(v.GetAnimeEpisodes().Select(a => a.GetAnimeSeries())); + v.GetAnimeEpisodes().ForEach(a => episodesToUpdate.Add(a)); + v.GetAnimeEpisodes().Select(a => a.GetAnimeSeries()).ToList().ForEach(a => seriesToUpdate.Add(a)); RepoFactory.VideoLocalPlace.DeleteWithOpenTransaction(session, this); } } diff --git a/JMMServer/Importer.cs b/JMMServer/Importer.cs index 061ddc51d..a2f917e20 100644 --- a/JMMServer/Importer.cs +++ b/JMMServer/Importer.cs @@ -1,5 +1,6 @@ using System; -using System.Collections.Generic; + using System.Collections; + using System.Collections.Generic; using System.IO; using System.Linq; using JMMContracts; @@ -818,8 +819,8 @@ public static void RemoveRecordsWithoutPhysicalFiles() { using (var session = DatabaseFactory.SessionFactory.OpenSession()) { - List episodesToUpdate = new List(); - List seriesToUpdate = new List(); + HashSet episodesToUpdate = new HashSet(); + HashSet seriesToUpdate = new HashSet(); // get a full list of files Dictionary> filesAll = RepoFactory.VideoLocalPlace.GetAll() .Where(a => a.ImportFolder != null) @@ -842,28 +843,39 @@ public static void RemoveRecordsWithoutPhysicalFiles() IReadOnlyList videoLocalsAll = RepoFactory.VideoLocal.GetAll(); foreach (VideoLocal v in videoLocalsAll) { + List places = v.Places; if (v.Places?.Count > 0) { - foreach (VideoLocal_Place place in v.Places) + foreach (VideoLocal_Place place in places) { if (!string.IsNullOrWhiteSpace(place?.FullServerPath)) continue; logger.Info("RemoveRecordsWithOrphanedImportFolder : {0}", v.FileName); - episodesToUpdate.AddRange(v.GetAnimeEpisodes()); - seriesToUpdate.AddRange(v.GetAnimeEpisodes().Select(a => a.GetAnimeSeries())); + episodesToUpdate.UnionWith(v.GetAnimeEpisodes()); + seriesToUpdate.UnionWith(v.GetAnimeEpisodes().Select(a => a.GetAnimeSeries())); RepoFactory.VideoLocalPlace.DeleteWithOpenTransaction(session, place); } } - if (v.Places?.Count > 0) continue; + places = v.Places; + if (places?.Count == 1) continue; + if (places?.Count > 0) + { + places = places.DistinctBy(a => a.FullServerPath).ToList(); + places = v.Places?.Except(places).ToList(); + foreach (VideoLocal_Place place in places) + { + RepoFactory.VideoLocalPlace.DeleteWithOpenTransaction(session, place); + } + continue; + } // delete video local record logger.Info("RemoveOrphanedVideoLocal : {0}", v.FileName); - episodesToUpdate.AddRange(v.GetAnimeEpisodes()); - seriesToUpdate.AddRange(v.GetAnimeEpisodes().Select(a => a.GetAnimeSeries())); + episodesToUpdate.UnionWith(v.GetAnimeEpisodes()); + seriesToUpdate.UnionWith(v.GetAnimeEpisodes().Select(a => a.GetAnimeSeries())); RepoFactory.VideoLocal.DeleteWithOpenTransaction(session, v); CommandRequest_DeleteFileFromMyList cmdDel = new CommandRequest_DeleteFileFromMyList(v.Hash, v.FileSize); cmdDel.Save(); } - episodesToUpdate = episodesToUpdate.DistinctBy(a => a.AnimeEpisodeID).ToList(); foreach (AnimeEpisode ep in episodesToUpdate) { if (ep.AnimeEpisodeID == 0) @@ -881,7 +893,7 @@ public static void RemoveRecordsWithoutPhysicalFiles() LogManager.GetCurrentClassLogger().Error(ex, ex.ToString()); } } - seriesToUpdate = seriesToUpdate.DistinctBy(a => a.AnimeSeriesID).ToList(); + foreach (AnimeSeries ser in seriesToUpdate) { ser.QueueUpdateStats();