From 7a18d373ec9a4b96151280c5d475ce7d1a64e474 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 17 Oct 2017 17:08:42 +0900 Subject: [PATCH] Improve performance of beatmap imports (still needs revision) --- osu.Game/Beatmaps/BeatmapManager.cs | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/osu.Game/Beatmaps/BeatmapManager.cs b/osu.Game/Beatmaps/BeatmapManager.cs index 1423e9138f..bf71033f36 100644 --- a/osu.Game/Beatmaps/BeatmapManager.cs +++ b/osu.Game/Beatmaps/BeatmapManager.cs @@ -98,7 +98,7 @@ namespace osu.Game.Beatmaps beatmaps = createBeatmapStore(context); files = new FileStore(context, storage); - this.storage = storage; + this.storage = files.Storage; this.rulesets = rulesets; this.api = api; @@ -174,16 +174,23 @@ namespace osu.Game.Beatmaps { var context = createContext(); + context.Database.AutoTransactionsEnabled = false; + using (var transaction = context.Database.BeginTransaction()) { // create local stores so we can isolate and thread safely, and share a context/transaction. - var filesForImport = new FileStore(() => context, storage); - var beatmapsForImport = createBeatmapStore(() => context); + var iFiles = new FileStore(() => context, storage); + var iBeatmaps = createBeatmapStore(() => context); + + BeatmapSetInfo set = importToStorage(iFiles, iBeatmaps, archiveReader); + + if (set.ID == 0) + { + iBeatmaps.Add(set); + context.SaveChanges(); + transaction.Commit(); + } - BeatmapSetInfo set = importToStorage(filesForImport, archiveReader); - beatmapsForImport.Add(set); - context.SaveChanges(); - transaction.Commit(); return set; } } @@ -308,7 +315,7 @@ namespace osu.Game.Beatmaps /// Is a no-op for already usable beatmaps. /// /// The beatmap to restore. - public void Undelete(BeatmapSetInfo beatmapSet) + private void undelete(BeatmapStore beatmaps, FileStore files, BeatmapSetInfo beatmapSet) { lock (beatmaps) if (!beatmaps.Undelete(beatmapSet)) return; @@ -410,7 +417,7 @@ namespace osu.Game.Beatmaps /// /// The beatmap archive to be read. /// The imported beatmap, or an existing instance if it is already present. - private BeatmapSetInfo importToStorage(FileStore files, ArchiveReader reader) + private BeatmapSetInfo importToStorage(FileStore files, BeatmapStore beatmaps, ArchiveReader reader) { // let's make sure there are actually .osu files to import. string mapName = reader.Filenames.FirstOrDefault(f => f.EndsWith(".osu")); @@ -432,7 +439,7 @@ namespace osu.Game.Beatmaps if (beatmapSet != null) { - Undelete(beatmapSet); + undelete(beatmaps, files, beatmapSet); // ensure all files are present and accessible foreach (var f in beatmapSet.Files) @@ -442,6 +449,8 @@ namespace osu.Game.Beatmaps files.Add(s, false); } + // todo: delete any files which shouldn't exist any more. + return beatmapSet; }