// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using System.Linq; using Microsoft.EntityFrameworkCore; using osu.Game.Database; namespace osu.Game.Beatmaps { /// /// Handles the storage and retrieval of Beatmaps/BeatmapSets to the database backing /// public class BeatmapStore : DatabaseBackedStore { public event Action BeatmapSetAdded; public event Action BeatmapSetRemoved; public event Action BeatmapHidden; public event Action BeatmapRestored; public BeatmapStore(Func factory) : base(factory) { } /// /// Add a to the database. /// /// The beatmap to add. public void Add(BeatmapSetInfo beatmapSet) { var context = GetContext(); foreach (var beatmap in beatmapSet.Beatmaps.Where(b => b.Metadata != null)) { // If we detect a new metadata object it'll be attached to the current context so it can be reused // to prevent duplicate entries when persisting. To accomplish this we look in the cache (.Local) // of the corresponding table (.Set()) for matching entries to our criteria. var contextMetadata = context.Set().Local.SingleOrDefault(e => e.Equals(beatmap.Metadata)); if (contextMetadata != null) beatmap.Metadata = contextMetadata; else context.BeatmapMetadata.Attach(beatmap.Metadata); } context.BeatmapSetInfo.Attach(beatmapSet); context.SaveChanges(); BeatmapSetAdded?.Invoke(beatmapSet); } /// /// Delete a from the database. /// /// The beatmap to delete. /// Whether the beatmap's was changed. public bool Delete(BeatmapSetInfo beatmapSet) { var context = GetContext(); Refresh(ref beatmapSet, BeatmapSets); if (beatmapSet.DeletePending) return false; beatmapSet.DeletePending = true; context.SaveChanges(); BeatmapSetRemoved?.Invoke(beatmapSet); return true; } /// /// Restore a previously deleted . /// /// The beatmap to restore. /// Whether the beatmap's was changed. public bool Undelete(BeatmapSetInfo beatmapSet) { var context = GetContext(); Refresh(ref beatmapSet, BeatmapSets); if (!beatmapSet.DeletePending) return false; beatmapSet.DeletePending = false; context.SaveChanges(); BeatmapSetAdded?.Invoke(beatmapSet); return true; } /// /// Hide a in the database. /// /// The beatmap to hide. /// Whether the beatmap's was changed. public bool Hide(BeatmapInfo beatmap) { var context = GetContext(); Refresh(ref beatmap, Beatmaps); if (beatmap.Hidden) return false; beatmap.Hidden = true; context.SaveChanges(); BeatmapHidden?.Invoke(beatmap); return true; } /// /// Restore a previously hidden . /// /// The beatmap to restore. /// Whether the beatmap's was changed. public bool Restore(BeatmapInfo beatmap) { var context = GetContext(); Refresh(ref beatmap, Beatmaps); if (!beatmap.Hidden) return false; beatmap.Hidden = false; context.SaveChanges(); BeatmapRestored?.Invoke(beatmap); return true; } public override void Cleanup() { var context = GetContext(); var purgeable = context.BeatmapSetInfo.Where(s => s.DeletePending && !s.Protected) .Include(s => s.Beatmaps).ThenInclude(b => b.Metadata) .Include(s => s.Beatmaps).ThenInclude(b => b.BaseDifficulty) .Include(s => s.Metadata); // metadata is M-N so we can't rely on cascades context.BeatmapMetadata.RemoveRange(purgeable.Select(s => s.Metadata)); context.BeatmapMetadata.RemoveRange(purgeable.SelectMany(s => s.Beatmaps.Select(b => b.Metadata).Where(m => m != null))); // todo: we can probably make cascades work here with a FK in BeatmapDifficulty. just make to make it work correctly. context.BeatmapDifficulty.RemoveRange(purgeable.SelectMany(s => s.Beatmaps.Select(b => b.BaseDifficulty))); // cascades down to beatmaps. context.BeatmapSetInfo.RemoveRange(purgeable); context.SaveChanges(); } public IQueryable BeatmapSets => GetContext().BeatmapSetInfo .Include(s => s.Metadata) .Include(s => s.Beatmaps).ThenInclude(s => s.Ruleset) .Include(s => s.Beatmaps).ThenInclude(b => b.BaseDifficulty) .Include(s => s.Beatmaps).ThenInclude(b => b.Metadata) .Include(s => s.Files).ThenInclude(f => f.FileInfo); public IQueryable Beatmaps => GetContext().BeatmapInfo .Include(b => b.BeatmapSet).ThenInclude(s => s.Metadata) .Include(b => b.Metadata) .Include(b => b.Ruleset) .Include(b => b.BaseDifficulty); } }