1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-15 11:52:56 +08:00

Move collections db in-place

This commit is contained in:
smoogipoo 2021-06-23 15:03:34 +09:00
parent a1da9c5ee3
commit c9a203cc11

View File

@ -257,27 +257,42 @@ namespace osu.Game.Collections
{ {
Interlocked.Increment(ref lastSave); Interlocked.Increment(ref lastSave);
// This is NOT thread-safe!!
try try
{ {
// This is NOT thread-safe!! var tempPath = Path.GetTempFileName();
using (var sw = new SerializationWriter(storage.GetStream(database_name, FileAccess.Write))) using (var ms = new MemoryStream())
{ {
sw.Write(database_version); using (var sw = new SerializationWriter(ms, true))
var collectionsCopy = Collections.ToArray();
sw.Write(collectionsCopy.Length);
foreach (var c in collectionsCopy)
{ {
sw.Write(c.Name.Value); sw.Write(database_version);
var beatmapsCopy = c.Beatmaps.ToArray(); var collectionsCopy = Collections.ToArray();
sw.Write(beatmapsCopy.Length); sw.Write(collectionsCopy.Length);
foreach (var b in beatmapsCopy) foreach (var c in collectionsCopy)
sw.Write(b.MD5Hash); {
sw.Write(c.Name.Value);
var beatmapsCopy = c.Beatmaps.ToArray();
sw.Write(beatmapsCopy.Length);
foreach (var b in beatmapsCopy)
sw.Write(b.MD5Hash);
}
} }
using (var fs = File.OpenWrite(tempPath))
ms.WriteTo(fs);
var storagePath = storage.GetFullPath(database_name);
var storageBackupPath = storage.GetFullPath($"{database_name}.bak");
if (File.Exists(storageBackupPath))
File.Delete(storageBackupPath);
if (File.Exists(storagePath))
File.Move(storagePath, storageBackupPath);
File.Move(tempPath, storagePath);
} }
if (saveFailures < 10) if (saveFailures < 10)