Skip to content

Commit

Permalink
Clean up duplicate records of video local places on remove missing fi…
Browse files Browse the repository at this point in the history
…les.
  • Loading branch information
da3dsoul committed Jan 7, 2017
1 parent 3126ffe commit 515c193
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
13 changes: 7 additions & 6 deletions JMMServer/Entities/VideoLocal_Place.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,16 @@ public void RemoveRecord()
}
}

public void RemoveRecordWithOpenTransaction(ISession session, List<AnimeEpisode> episodesToUpdate,
List<AnimeSeries> seriesToUpdate)

public void RemoveRecordWithOpenTransaction(ISession session, ICollection<AnimeEpisode> episodesToUpdate,
ICollection<AnimeSeries> 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 =
Expand All @@ -181,8 +182,8 @@ public void RemoveRecordWithOpenTransaction(ISession session, List<AnimeEpisode>
}
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);
}
}
Expand Down
34 changes: 23 additions & 11 deletions JMMServer/Importer.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -818,8 +819,8 @@ public static void RemoveRecordsWithoutPhysicalFiles()
{
using (var session = DatabaseFactory.SessionFactory.OpenSession())
{
List<AnimeEpisode> episodesToUpdate = new List<AnimeEpisode>();
List<AnimeSeries> seriesToUpdate = new List<AnimeSeries>();
HashSet<AnimeEpisode> episodesToUpdate = new HashSet<AnimeEpisode>();
HashSet<AnimeSeries> seriesToUpdate = new HashSet<AnimeSeries>();
// get a full list of files
Dictionary<ImportFolder, List<VideoLocal_Place>> filesAll = RepoFactory.VideoLocalPlace.GetAll()
.Where(a => a.ImportFolder != null)
Expand All @@ -842,28 +843,39 @@ public static void RemoveRecordsWithoutPhysicalFiles()
IReadOnlyList<VideoLocal> videoLocalsAll = RepoFactory.VideoLocal.GetAll();
foreach (VideoLocal v in videoLocalsAll)
{
List<VideoLocal_Place> 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)
Expand All @@ -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();
Expand Down

0 comments on commit 515c193

Please sign in to comment.