From cbe7b08642f2cfed26e4fd0c4c0798c8000db767 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 27 Jul 2017 15:34:13 +0900 Subject: [PATCH] Make BeatmapStore's BeatmapDatabase private --- .../Tests/TestCasePlaySongSelect.cs | 2 +- .../Tests/TestCaseResults.cs | 11 ++- .../Beatmaps/IO/ImportBeatmapTest.cs | 9 +- osu.Game/Beatmaps/BeatmapStore.cs | 88 +++++++++++++++---- osu.Game/Overlays/Music/PlaylistOverlay.cs | 3 +- osu.Game/Rulesets/Scoring/ScoreDatabase.cs | 3 +- osu.Game/Screens/Menu/Intro.cs | 10 +-- osu.Game/Screens/Select/SongSelect.cs | 2 +- 8 files changed, 85 insertions(+), 43 deletions(-) diff --git a/osu.Desktop.VisualTests/Tests/TestCasePlaySongSelect.cs b/osu.Desktop.VisualTests/Tests/TestCasePlaySongSelect.cs index d27a7507ab..2fb720c03e 100644 --- a/osu.Desktop.VisualTests/Tests/TestCasePlaySongSelect.cs +++ b/osu.Desktop.VisualTests/Tests/TestCasePlaySongSelect.cs @@ -34,7 +34,7 @@ namespace osu.Desktop.VisualTests.Tests store = new BeatmapStore(storage, null, backingDatabase, rulesets); for (int i = 0; i < 100; i += 10) - store.Database.Add(createTestBeatmapSet(i)); + store.Import(createTestBeatmapSet(i)); } Add(songSelect = new PlaySongSelect()); diff --git a/osu.Desktop.VisualTests/Tests/TestCaseResults.cs b/osu.Desktop.VisualTests/Tests/TestCaseResults.cs index 5b9ee2198d..f0a8a8394c 100644 --- a/osu.Desktop.VisualTests/Tests/TestCaseResults.cs +++ b/osu.Desktop.VisualTests/Tests/TestCaseResults.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.Linq; using osu.Framework.Allocation; using osu.Framework.Testing; using osu.Game.Beatmaps; @@ -15,14 +14,14 @@ namespace osu.Desktop.VisualTests.Tests { internal class TestCaseResults : TestCase { - private BeatmapStore db; + private BeatmapStore beatmaps; public override string Description => @"Results after playing."; [BackgroundDependencyLoader] - private void load(BeatmapStore db) + private void load(BeatmapStore beatmaps) { - this.db = db; + this.beatmaps = beatmaps; } private WorkingBeatmap beatmap; @@ -33,9 +32,9 @@ namespace osu.Desktop.VisualTests.Tests if (beatmap == null) { - var beatmapInfo = db.Database.Query().FirstOrDefault(b => b.RulesetID == 0); + var beatmapInfo = beatmaps.QueryBeatmap(b => b.RulesetID == 0); if (beatmapInfo != null) - beatmap = db.GetWorkingBeatmap(beatmapInfo); + beatmap = beatmaps.GetWorkingBeatmap(beatmapInfo); } Add(new Results(new Score diff --git a/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs b/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs index a9bf7f3dad..4582f5b2fb 100644 --- a/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs +++ b/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs @@ -119,8 +119,7 @@ namespace osu.Game.Tests.Beatmaps.IO Action waitAction = () => { - while (!(resultSets = store.Database. - Query().Where(s => s.OnlineBeatmapSetID == 241526)).Any()) + while (!(resultSets = store.QueryBeatmapSets(s => s.OnlineBeatmapSetID == 241526)).Any()) Thread.Sleep(50); }; @@ -136,16 +135,14 @@ namespace osu.Game.Tests.Beatmaps.IO //if we don't re-check here, the set will be inserted but the beatmaps won't be present yet. waitAction = () => { - while ((resultBeatmaps = store.Database. - QueryAndPopulate(s => s.OnlineBeatmapSetID == 241526 && s.BaseDifficultyID > 0)).Count() != 12) + while ((resultBeatmaps = store.QueryBeatmaps(s => s.OnlineBeatmapSetID == 241526 && s.BaseDifficultyID > 0)).Count() != 12) Thread.Sleep(50); }; Assert.IsTrue(waitAction.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(timeout), @"Beatmaps did not import to the database in allocated time"); - var set = resultSets.First(); - store.Database.Populate(set); + var set = store.QueryBeatmapSets(s => s.OnlineBeatmapSetID == 241526).First(); Assert.IsTrue(set.Beatmaps.Count == resultBeatmaps.Count(), $@"Incorrect database beatmap count post-import ({resultBeatmaps.Count()} but should be {set.Beatmaps.Count})."); diff --git a/osu.Game/Beatmaps/BeatmapStore.cs b/osu.Game/Beatmaps/BeatmapStore.cs index a5f74aad1a..092846170e 100644 --- a/osu.Game/Beatmaps/BeatmapStore.cs +++ b/osu.Game/Beatmaps/BeatmapStore.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Linq.Expressions; using Ionic.Zip; using osu.Framework.Extensions; using osu.Framework.Logging; @@ -24,9 +25,6 @@ namespace osu.Game.Beatmaps /// public class BeatmapStore { - // todo: make this private - public readonly BeatmapDatabase Database; - /// /// Fired when a new becomes available in the database. /// @@ -48,14 +46,16 @@ namespace osu.Game.Beatmaps private readonly RulesetDatabase rulesets; + private readonly BeatmapDatabase beatmaps; + // ReSharper disable once NotAccessedField.Local (we should keep a reference to this so it is not finalised) private BeatmapIPCChannel ipc; public BeatmapStore(Storage storage, FileDatabase files, SQLiteConnection connection, RulesetDatabase rulesets, IIpcHost importHost = null) { - Database = new BeatmapDatabase(connection); - Database.BeatmapSetAdded += s => BeatmapSetAdded?.Invoke(s); - Database.BeatmapSetRemoved += s => BeatmapSetRemoved?.Invoke(s); + beatmaps = new BeatmapDatabase(connection); + beatmaps.BeatmapSetAdded += s => BeatmapSetAdded?.Invoke(s); + beatmaps.BeatmapSetRemoved += s => BeatmapSetRemoved?.Invoke(s); this.storage = storage; this.files = files; @@ -106,14 +106,22 @@ namespace osu.Game.Beatmaps public BeatmapSetInfo Import(ArchiveReader archiveReader) { BeatmapSetInfo set = importToStorage(archiveReader); - - //If we have an ID then we already exist in the database. - if (set.ID == 0) - Database.Add(set); - + Import(set); return set; } + /// + /// Import a beatmap from a . + /// + /// The beatmap to be imported. + public void Import(BeatmapSetInfo beatmapSetInfo) + { + // If we have an ID then we already exist in the database. + if (beatmapSetInfo.ID != 0) return; + + beatmaps.Add(beatmapSetInfo); + } + /// /// Delete a beatmap from the store. /// Is a no-op for already deleted beatmaps. @@ -121,7 +129,7 @@ namespace osu.Game.Beatmaps /// The beatmap to delete. public void Delete(BeatmapSetInfo beatmapSet) { - if (!Database.Delete(beatmapSet)) return; + if (!beatmaps.Delete(beatmapSet)) return; if (!beatmapSet.Protected) files.Dereference(beatmapSet.Files); @@ -134,7 +142,7 @@ namespace osu.Game.Beatmaps /// The beatmap to restore. public void Undelete(BeatmapSetInfo beatmapSet) { - if (!Database.Undelete(beatmapSet)) return; + if (!beatmaps.Undelete(beatmapSet)) return; files.Reference(beatmapSet.Files); } @@ -150,7 +158,7 @@ namespace osu.Game.Beatmaps if (beatmapInfo == null || beatmapInfo == DefaultBeatmap?.BeatmapInfo) return DefaultBeatmap; - Database.Populate(beatmapInfo); + beatmaps.Populate(beatmapInfo); if (beatmapInfo.BeatmapSet == null) throw new InvalidOperationException($@"Beatmap set {beatmapInfo.BeatmapSetInfoID} is not in the local database."); @@ -170,7 +178,7 @@ namespace osu.Game.Beatmaps /// public void Reset() { - Database.Reset(); + beatmaps.Reset(); } /// @@ -180,14 +188,43 @@ namespace osu.Game.Beatmaps /// The first result for the provided query, or null if no results were found. public BeatmapSetInfo QueryBeatmapSet(Func query) { - BeatmapSetInfo set = Database.Query().FirstOrDefault(query); + BeatmapSetInfo set = beatmaps.Query().FirstOrDefault(query); if (set != null) - Database.Populate(set); + beatmaps.Populate(set); return set; } + /// + /// Perform a lookup query on available s. + /// + /// The query. + /// Results from the provided query. + public List QueryBeatmapSets(Expression> query) => beatmaps.QueryAndPopulate(query); + + /// + /// Perform a lookup query on available s. + /// + /// The query. + /// The first result for the provided query, or null if no results were found. + public BeatmapInfo QueryBeatmap(Func query) + { + BeatmapInfo set = beatmaps.Query().FirstOrDefault(query); + + if (set != null) + beatmaps.Populate(set); + + return set; + } + + /// + /// Perform a lookup query on available s. + /// + /// The query. + /// Results from the provided query. + public List QueryBeatmaps(Expression> query) => beatmaps.QueryAndPopulate(query); + /// /// Creates an from a valid storage path. /// @@ -229,11 +266,11 @@ namespace osu.Game.Beatmaps var overallHash = hashable.GetMd5Hash(); - var existing = Database.Query().FirstOrDefault(b => b.Hash == overallHash); + var existing = beatmaps.Query().FirstOrDefault(b => b.Hash == overallHash); if (existing != null) { - Database.Populate(existing); + beatmaps.Populate(existing); Undelete(existing); return existing; } @@ -278,5 +315,18 @@ namespace osu.Game.Beatmaps return beatmapSet; } + + /// + /// Returns a list of all usable s. + /// + /// Whether returned objects should be pre-populated with all data. + /// A list of available . + public List GetAllUsableBeatmapSets(bool populate = true) + { + if (populate) + return beatmaps.QueryAndPopulate(b => !b.DeletePending).ToList(); + else + return beatmaps.Query(b => !b.DeletePending).ToList(); + } } } diff --git a/osu.Game/Overlays/Music/PlaylistOverlay.cs b/osu.Game/Overlays/Music/PlaylistOverlay.cs index 780e713a3f..395456ad1b 100644 --- a/osu.Game/Overlays/Music/PlaylistOverlay.cs +++ b/osu.Game/Overlays/Music/PlaylistOverlay.cs @@ -77,8 +77,9 @@ namespace osu.Game.Overlays.Music }, }; - list.BeatmapSets = BeatmapSets = beatmaps.Database.QueryAndPopulate(b => !b.DeletePending).ToList(); + list.BeatmapSets = BeatmapSets = beatmaps.GetAllUsableBeatmapSets(); + // todo: these should probably be above the query. beatmaps.BeatmapSetAdded += s => list.AddBeatmapSet(s); beatmaps.BeatmapSetRemoved += s => list.RemoveBeatmapSet(s); diff --git a/osu.Game/Rulesets/Scoring/ScoreDatabase.cs b/osu.Game/Rulesets/Scoring/ScoreDatabase.cs index 84a532ec14..19dbf30b28 100644 --- a/osu.Game/Rulesets/Scoring/ScoreDatabase.cs +++ b/osu.Game/Rulesets/Scoring/ScoreDatabase.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using osu.Framework.Platform; using osu.Game.Beatmaps; using osu.Game.Database; @@ -54,7 +53,7 @@ namespace osu.Game.Rulesets.Scoring var version = sr.ReadInt32(); /* score.FileChecksum = */ var beatmapHash = sr.ReadString(); - score.Beatmap = beatmaps.Database.Query().FirstOrDefault(b => b.Hash == beatmapHash); + score.Beatmap = beatmaps.QueryBeatmap(b => b.Hash == beatmapHash); /* score.PlayerName = */ sr.ReadString(); /* var localScoreChecksum = */ diff --git a/osu.Game/Screens/Menu/Intro.cs b/osu.Game/Screens/Menu/Intro.cs index b0f067d6ee..d05ef8af65 100644 --- a/osu.Game/Screens/Menu/Intro.cs +++ b/osu.Game/Screens/Menu/Intro.cs @@ -76,13 +76,9 @@ namespace osu.Game.Screens.Menu if (!menuMusic) { - var query = beatmaps.Database.Query(b => !b.DeletePending); - int count = query.Count(); - if (count > 0) - { - setInfo = query.ElementAt(RNG.Next(0, count - 1)); - beatmaps.Database.Populate(setInfo); - } + var sets = beatmaps.GetAllUsableBeatmapSets(false); + if (sets.Count > 0) + setInfo = beatmaps.QueryBeatmapSet(s => s.ID == sets[RNG.Next(0, sets.Count - 1)].ID); } if (setInfo == null) diff --git a/osu.Game/Screens/Select/SongSelect.cs b/osu.Game/Screens/Select/SongSelect.cs index 1d87080958..fb7653ed7d 100644 --- a/osu.Game/Screens/Select/SongSelect.cs +++ b/osu.Game/Screens/Select/SongSelect.cs @@ -180,7 +180,7 @@ namespace osu.Game.Screens.Select initialAddSetsTask = new CancellationTokenSource(); - carousel.Beatmaps = store.Database.QueryAndPopulate(b => !b.DeletePending); + carousel.Beatmaps = store.GetAllUsableBeatmapSets(); Beatmap.ValueChanged += beatmap_ValueChanged;