From d54a75a5c53ec8b6fa6a58e27b062e9916644260 Mon Sep 17 00:00:00 2001 From: Mikal Stodal Date: Tue, 28 Feb 2017 14:35:42 +0100 Subject: [PATCH] Delete file after importing BeatmapSet w/error handling, also changed batch-commit of multiple sets to database. --- osu.Game/Database/BeatmapDatabase.cs | 48 ++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/osu.Game/Database/BeatmapDatabase.cs b/osu.Game/Database/BeatmapDatabase.cs index fc2c7558b0..9292fbab49 100644 --- a/osu.Game/Database/BeatmapDatabase.cs +++ b/osu.Game/Database/BeatmapDatabase.cs @@ -118,11 +118,44 @@ namespace osu.Game.Database public void Import(IEnumerable paths) { + Stack sets = new Stack(); + foreach (string p in paths) - Import(p); + try + { + BeatmapSetInfo set = importBeatmapSet(p); + + sets.Push(set); + } + catch (Exception e) + { + Logger.Error(e, $@"Could not import beatmap set"); + } + finally + { + // We may or may not want to delete the file depending on where it is stored. + // e.g. reconstructing/repairing database with beatmaps from default storage. + // TODO: Add a check to prevent files from storage to be deleted. + try + { + File.Delete(p); + } + catch (Exception e) + { + Logger.Error(e, $@"Could not delete file at {p}"); + } + } + + // Batch commit with multiple sets to database + Import(sets); } public void Import(string path) + { + Import(new [] { path }); + } + + private BeatmapSetInfo importBeatmapSet(string path) { string hash = null; @@ -156,7 +189,7 @@ namespace osu.Game.Database BeatmapSetAdded?.Invoke(existing); } - return; + return null; } var beatmapSet = new BeatmapSetInfo @@ -188,7 +221,7 @@ namespace osu.Game.Database } } - Import(new[] { beatmapSet }); + return beatmapSet; } public void Import(IEnumerable beatmapSets) @@ -198,10 +231,11 @@ namespace osu.Game.Database connection.BeginTransaction(); foreach (var s in beatmapSets) - { - connection.InsertWithChildren(s, true); - BeatmapSetAdded?.Invoke(s); - } + if (s != null) + { + connection.InsertWithChildren(s, true); + BeatmapSetAdded?.Invoke(s); + } connection.Commit(); }