From 5ff4028c296b414b4a32379bf9acb7348b86f6d1 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 24 Feb 2017 18:10:37 +0900 Subject: [PATCH 1/2] Make beatmap importing async. --- osu.Desktop/OsuGameDesktop.cs | 2 +- osu.Game/OsuGame.cs | 7 ++++--- osu.Game/OsuGameBase.cs | 4 +++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/osu.Desktop/OsuGameDesktop.cs b/osu.Desktop/OsuGameDesktop.cs index 33de58be3c..d9b9c31617 100644 --- a/osu.Desktop/OsuGameDesktop.cs +++ b/osu.Desktop/OsuGameDesktop.cs @@ -56,7 +56,7 @@ namespace osu.Desktop // this method will only be executed if e.Effect in dragEnter gets set to something other that None. var dropData = e.Data.GetData(DataFormats.FileDrop) as object[]; var filePaths = dropData.Select(f => f.ToString()).ToArray(); - ImportBeatmaps(filePaths); + ImportBeatmapsAsync(filePaths); } private void dragEnter(DragEventArgs e) diff --git a/osu.Game/OsuGame.cs b/osu.Game/OsuGame.cs index 55eb3f861e..49f1353ab7 100644 --- a/osu.Game/OsuGame.cs +++ b/osu.Game/OsuGame.cs @@ -25,6 +25,7 @@ using OpenTK; using System.Linq; using osu.Framework.Graphics.Primitives; using System.Collections.Generic; +using System.Threading.Tasks; using osu.Game.Overlays.Notifications; namespace osu.Game @@ -81,7 +82,7 @@ namespace osu.Game if (args?.Length > 0) { var paths = args.Where(a => !a.StartsWith(@"-")); - ImportBeatmaps(paths); + ImportBeatmapsAsync(paths); } Dependencies.Cache(this); @@ -89,9 +90,9 @@ namespace osu.Game PlayMode = LocalConfig.GetBindable(OsuConfig.PlayMode); } - public void ImportBeatmaps(IEnumerable paths) + protected async void ImportBeatmapsAsync(IEnumerable paths) { - Schedule(delegate { Dependencies.Get().Import(paths); }); + await Task.Run(() => BeatmapDatabase.Import(paths)); } protected override void LoadComplete() diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index 331c3adeca..1c34743567 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -23,6 +23,8 @@ namespace osu.Game { protected OsuConfigManager LocalConfig; + protected BeatmapDatabase BeatmapDatabase; + protected override string MainResourceFile => @"osu.Game.Resources.dll"; public APIAccess API; @@ -40,7 +42,7 @@ namespace osu.Game { Dependencies.Cache(this); Dependencies.Cache(LocalConfig); - Dependencies.Cache(new BeatmapDatabase(Host.Storage, Host)); + Dependencies.Cache(BeatmapDatabase = new BeatmapDatabase(Host.Storage, Host)); Dependencies.Cache(new OsuColour()); //this completely overrides the framework default. will need to change once we make a proper FontStore. From 7a60a5e4999e19115a2d8e144e8afe48ceb8b808 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sat, 25 Feb 2017 10:39:13 +0900 Subject: [PATCH 2/2] Lock the BeatmapDatabase's connection during imports. This should avoid any potential issues with intertwined transactions on the same connection while still allowing higher throughput when importing. --- osu.Game/Database/BeatmapDatabase.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/osu.Game/Database/BeatmapDatabase.cs b/osu.Game/Database/BeatmapDatabase.cs index 66c348279d..ff58031c57 100644 --- a/osu.Game/Database/BeatmapDatabase.cs +++ b/osu.Game/Database/BeatmapDatabase.cs @@ -181,15 +181,18 @@ namespace osu.Game.Database public void Import(IEnumerable beatmapSets) { - connection.BeginTransaction(); - - foreach (var s in beatmapSets) + lock (connection) { - connection.InsertWithChildren(s, true); - BeatmapSetAdded?.Invoke(s); - } + connection.BeginTransaction(); - connection.Commit(); + foreach (var s in beatmapSets) + { + connection.InsertWithChildren(s, true); + BeatmapSetAdded?.Invoke(s); + } + + connection.Commit(); + } } public void Delete(BeatmapSetInfo beatmapSet)