public override void RegenerateDb() { RegenerateDb( Cache.Values.Where(a => a.MediaVersion < SVR_VideoLocal.MEDIA_VERSION || a.MediaBlob == null).ToList(), a => { //Fix possible paths in filename if (!string.IsNullOrEmpty(a.FileName)) { int b = a.FileName.LastIndexOf("\\"); if (b > 0) { a.FileName = a.FileName.Substring(b + 1); } } Save(a, false); }); //Fix possible paths in filename try { Cache.Values.Where(a => a.FileName.Contains("\\")) .ToList() .ForEach(a => { int b = a.FileName.LastIndexOf("\\"); a.FileName = a.FileName.Substring(b + 1); Save(a, false); }); } catch (Exception e) { } using (var session = DatabaseFactory.SessionFactory.OpenSession()) { Dictionary <string, List <SVR_VideoLocal> > locals = Cache.Values.Where(a => !string.IsNullOrWhiteSpace(a.Hash)) .GroupBy(a => a.Hash) .ToDictionary(g => g.Key, g => g.ToList()); using (var transaction = session.BeginTransaction()) { foreach (SVR_VideoLocal remove in Cache.Values.Where(a => a.IsEmpty()).ToList()) { RepoFactory.VideoLocal.DeleteWithOpenTransaction(session, remove); } transaction.Commit(); } var toRemove = new List <SVR_VideoLocal>(); var comparer = new VideoLocalComparer(); foreach (string hash in locals.Keys) { List <SVR_VideoLocal> values = locals[hash]; values.Sort(comparer); SVR_VideoLocal to = values.First(); List <SVR_VideoLocal> froms = values.Except(to).ToList(); foreach (SVR_VideoLocal from in froms) { List <SVR_VideoLocal_Place> places = from.Places; if (places == null || places.Count == 0) { continue; } using (var transaction = session.BeginTransaction()) { foreach (SVR_VideoLocal_Place place in places) { place.VideoLocalID = to.VideoLocalID; RepoFactory.VideoLocalPlace.SaveWithOpenTransaction(session, place); } transaction.Commit(); } } toRemove.AddRange(froms); } using (var transaction = session.BeginTransaction()) { foreach (SVR_VideoLocal remove in toRemove) { DeleteWithOpenTransaction(session, remove); } transaction.Commit(); } } }
public override void RegenerateDb() { ServerState.Instance.ServerStartingStatus = string.Format( Resources.Database_Validating, nameof(VideoLocal), " Checking Media Info"); int count = 0; int max; try { var list = Cache.Values.Where(a => a.MediaVersion < SVR_VideoLocal.MEDIA_VERSION || a.MediaBlob == null) .ToList(); max = list.Count; list.ForEach( a => { CommandRequest_ReadMediaInfo cmd = new CommandRequest_ReadMediaInfo(a.VideoLocalID); cmd.Save(); count++; ServerState.Instance.ServerStartingStatus = string.Format( Resources.Database_Validating, nameof(VideoLocal), " Queuing Media Info Commands - " + count + "/" + max); }); } catch { // ignore } using (var session = DatabaseFactory.SessionFactory.OpenSession()) { Dictionary <string, List <SVR_VideoLocal> > locals = Cache.Values .Where(a => !string.IsNullOrWhiteSpace(a.Hash)) .GroupBy(a => a.Hash) .ToDictionary(g => g.Key, g => g.ToList()); ServerState.Instance.ServerStartingStatus = string.Format( Resources.Database_Validating, nameof(VideoLocal), " Cleaning Empty Records"); using (var transaction = session.BeginTransaction()) { var list = Cache.Values.Where(a => a.IsEmpty()).ToList(); count = 0; max = list.Count; foreach (SVR_VideoLocal remove in list) { RepoFactory.VideoLocal.DeleteWithOpenTransaction(session, remove); count++; ServerState.Instance.ServerStartingStatus = string.Format( Resources.Database_Validating, nameof(VideoLocal), " Cleaning Empty Records - " + count + "/" + max); } transaction.Commit(); } var toRemove = new List <SVR_VideoLocal>(); var comparer = new VideoLocalComparer(); ServerState.Instance.ServerStartingStatus = string.Format( Resources.Database_Validating, nameof(VideoLocal), " Checking for Duplicate Records"); foreach (string hash in locals.Keys) { List <SVR_VideoLocal> values = locals[hash]; values.Sort(comparer); SVR_VideoLocal to = values.First(); List <SVR_VideoLocal> froms = values.Except(to).ToList(); foreach (SVR_VideoLocal from in froms) { List <SVR_VideoLocal_Place> places = from.Places; if (places == null || places.Count == 0) { continue; } using (var transaction = session.BeginTransaction()) { foreach (SVR_VideoLocal_Place place in places) { place.VideoLocalID = to.VideoLocalID; RepoFactory.VideoLocalPlace.SaveWithOpenTransaction(session, place); } transaction.Commit(); } } toRemove.AddRange(froms); } count = 0; max = toRemove.Count; foreach (SVR_VideoLocal[] batch in toRemove.Batch(50)) { using (var transaction = session.BeginTransaction()) { foreach (SVR_VideoLocal remove in batch) { count++; ServerState.Instance.ServerStartingStatus = string.Format( Resources.Database_Validating, nameof(VideoLocal), " Cleaning Duplicate Records - " + count + "/" + max); DeleteWithOpenTransaction(session, remove); } transaction.Commit(); } } ServerState.Instance.ServerStartingStatus = string.Format( Resources.Database_Validating, nameof(VideoLocal), " Cleaning Fragmented Records"); using (var transaction = session.BeginTransaction()) { var list = Cache.Values.SelectMany(a => RepoFactory.CrossRef_File_Episode.GetByHash(a.Hash)) .Where(a => RepoFactory.AniDB_Anime.GetByAnimeID(a.AnimeID) == null || a.GetEpisode() == null).ToArray(); count = 0; max = list.Length; foreach (var xref in list) { // We don't need to update anything since they don't exist RepoFactory.CrossRef_File_Episode.DeleteWithOpenTransaction(session, xref); count++; ServerState.Instance.ServerStartingStatus = string.Format( Resources.Database_Validating, nameof(VideoLocal), " Cleaning Fragmented Records - " + count + "/" + max); } transaction.Commit(); } } }
public override void RegenerateDb() { int count = 0; int max = 0; ServerState.Instance.CurrentSetupStatus = string.Format( Commons.Properties.Resources.Database_Cache, typeof(VideoLocal).Name, " Generating Media Info"); try { count = 0; var list = Cache.Values.Where(a => a.MediaVersion < SVR_VideoLocal.MEDIA_VERSION || a.MediaBlob == null).ToList(); max = list.Count; list.ForEach( a => { //Fix possible paths in filename if (!string.IsNullOrEmpty(a.FileName)) { int b = a.FileName.LastIndexOf($"{Path.DirectorySeparatorChar}", StringComparison.Ordinal); if (b > 0) { a.FileName = a.FileName.Substring(b + 1); } } Save(a, false); count++; ServerState.Instance.CurrentSetupStatus = string.Format( Commons.Properties.Resources.Database_Cache, typeof(VideoLocal).Name, " Generating Media Info - " + count + "/" + max); }); } catch { } //Fix possible paths in filename try { ServerState.Instance.CurrentSetupStatus = string.Format( Commons.Properties.Resources.Database_Cache, typeof(VideoLocal).Name, " Cleaning File Paths"); var list = Cache.Values.Where(a => a.FileName.Contains("\\")).ToList(); count = 0; max = list.Count; list.ForEach(a => { int b = a.FileName.LastIndexOf($"{Path.DirectorySeparatorChar}", StringComparison.Ordinal); a.FileName = a.FileName.Substring(b + 1); Save(a, false); count++; ServerState.Instance.CurrentSetupStatus = string.Format( Commons.Properties.Resources.Database_Cache, typeof(VideoLocal).Name, " Cleaning File Paths - " + count + "/" + max); }); } catch { } using (var session = DatabaseFactory.SessionFactory.OpenSession()) { Dictionary <string, List <SVR_VideoLocal> > locals = Cache.Values.Where(a => !string.IsNullOrWhiteSpace(a.Hash)) .GroupBy(a => a.Hash) .ToDictionary(g => g.Key, g => g.ToList()); ServerState.Instance.CurrentSetupStatus = string.Format( Commons.Properties.Resources.Database_Cache, typeof(VideoLocal).Name, " Cleaning Empty Records"); using (var transaction = session.BeginTransaction()) { var list = Cache.Values.Where(a => a.IsEmpty()).ToList(); count = 0; max = list.Count; foreach (SVR_VideoLocal remove in list) { RepoFactory.VideoLocal.DeleteWithOpenTransaction(session, remove); count++; ServerState.Instance.CurrentSetupStatus = string.Format( Commons.Properties.Resources.Database_Cache, typeof(VideoLocal).Name, " Cleaning Empty Records - " + count + "/" + max); } transaction.Commit(); } var toRemove = new List <SVR_VideoLocal>(); var comparer = new VideoLocalComparer(); count = 0; max = locals.Keys.Count; ServerState.Instance.CurrentSetupStatus = string.Format( Commons.Properties.Resources.Database_Cache, typeof(VideoLocal).Name, " Cleaning Duplicate Records"); foreach (string hash in locals.Keys) { List <SVR_VideoLocal> values = locals[hash]; values.Sort(comparer); SVR_VideoLocal to = values.First(); List <SVR_VideoLocal> froms = values.Except(to).ToList(); foreach (SVR_VideoLocal from in froms) { List <SVR_VideoLocal_Place> places = from.Places; if (places == null || places.Count == 0) { continue; } using (var transaction = session.BeginTransaction()) { foreach (SVR_VideoLocal_Place place in places) { place.VideoLocalID = to.VideoLocalID; RepoFactory.VideoLocalPlace.SaveWithOpenTransaction(session, place); } transaction.Commit(); } } toRemove.AddRange(froms); count++; ServerState.Instance.CurrentSetupStatus = string.Format( Commons.Properties.Resources.Database_Cache, typeof(VideoLocal).Name, " Cleaning Duplicate Records - " + count + "/" + max); } using (var transaction = session.BeginTransaction()) { foreach (SVR_VideoLocal remove in toRemove) { DeleteWithOpenTransaction(session, remove); } transaction.Commit(); } ServerState.Instance.CurrentSetupStatus = string.Format( Commons.Properties.Resources.Database_Cache, typeof(VideoLocal).Name, " Cleaning Fragmented Records"); using (var transaction = session.BeginTransaction()) { var list = Cache.Values.SelectMany(a => RepoFactory.CrossRef_File_Episode.GetByHash(a.Hash)) .Where(a => RepoFactory.AniDB_Anime.GetByAnimeID(a.AnimeID) == null || a.GetEpisode() == null).ToArray(); count = 0; max = list.Length; foreach (var xref in list) { // We don't need to update anything since they don't exist RepoFactory.CrossRef_File_Episode.DeleteWithOpenTransaction(session, xref); count++; ServerState.Instance.CurrentSetupStatus = string.Format( Commons.Properties.Resources.Database_Cache, typeof(VideoLocal).Name, " Cleaning Fragmented Records - " + count + "/" + max); } transaction.Commit(); } } }
public override void PreInit(IProgress <InitProgress> progress, int batchSize) { List <SVR_VideoLocal> emptylist = Where(a => a.IsEmpty()).ToList(); InitProgress regen = new InitProgress(); if (emptylist.Count > 0) { regen.Title = string.Format(Commons.Properties.Resources.Database_Validating, typeof(VideoLocal).Name, " Cleaning Empty Records"); regen.Step = 0; regen.Total = emptylist.Count; progress.Report(regen); foreach (SVR_VideoLocal vl in emptylist) { Delete(vl); regen.Step++; progress.Report(regen); } } Dictionary <string, List <SVR_VideoLocal> > locals = Where(a => !string.IsNullOrWhiteSpace(a.Hash)).GroupBy(a => a.Hash).Where(a => a.Count() > 1).ToDictionary(g => g.Key, g => g.ToList()); if (locals.Count > 0) { var toRemove = new List <SVR_VideoLocal>(); var comparer = new VideoLocalComparer(); regen.Step = 0; regen.Total = locals.Count; regen.Title = string.Format(Commons.Properties.Resources.Database_Validating, typeof(VideoLocal).Name, " Cleaning Duplicate Records"); foreach (string hash in locals.Keys) { List <SVR_VideoLocal> values = locals[hash]; values.Sort(comparer); SVR_VideoLocal to = values.First(); List <SVR_VideoLocal> froms = values.ToList(); froms.Remove(to); foreach (SVR_VideoLocal from in froms) { using (var upd = Repo.Instance.VideoLocal_Place.BeginBatchUpdate(() => Repo.Instance.VideoLocal_Place.GetByVideoLocal(from.VideoLocalID))) { foreach (SVR_VideoLocal_Place place in upd) { place.VideoLocalID = to.VideoLocalID; upd.Update(place); } upd.Commit(); } } regen.Step++; progress.Report(regen); toRemove.AddRange(froms); } Delete(toRemove); } List <CrossRef_File_Episode> frags = WhereAll().SelectMany(a => Repo.Instance.CrossRef_File_Episode.GetByHash(a.Hash)).AsQueryable().Where(a => Repo.Instance.AniDB_Anime.GetByID(a.AnimeID) == null || a.GetEpisode() == null).ToList(); if (frags.Count > 0) { regen.Step = 0; regen.Total = frags.Count; regen.Title = string.Format(Commons.Properties.Resources.Database_Validating, typeof(VideoLocal).Name, " Cleaning Fragmented Records"); Repo.Instance.CrossRef_File_Episode.Delete(frags); } }