Improve performance of beatmap imports (still needs revision)

This commit is contained in:
Dean Herbert 2017-10-17 17:08:42 +09:00
parent 64dfce258f
commit 7a18d373ec
1 changed files with 19 additions and 10 deletions

View File

@ -98,7 +98,7 @@ public BeatmapManager(Storage storage, Func<OsuDbContext> context, RulesetStore
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 @@ public BeatmapSetInfo Import(ArchiveReader archiveReader)
{
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 @@ public void Restore(BeatmapInfo beatmap)
/// Is a no-op for already usable beatmaps.
/// </summary>
/// <param name="beatmapSet">The beatmap to restore.</param>
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 @@ private ArchiveReader getReaderFrom(string path)
/// </summary>
/// <param name="reader">The beatmap archive to be read.</param>
/// <returns>The imported beatmap, or an existing instance if it is already present.</returns>
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 @@ private BeatmapSetInfo importToStorage(FileStore files, ArchiveReader reader)
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 @@ private BeatmapSetInfo importToStorage(FileStore files, ArchiveReader reader)
files.Add(s, false);
}
// todo: delete any files which shouldn't exist any more.
return beatmapSet;
}