1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:57:39 +08:00

Simplify reuse check using FileInfo IDs

This commit is contained in:
Dean Herbert 2020-06-03 22:35:01 +09:00
parent 25160dc220
commit 5ed3cd205f
2 changed files with 15 additions and 14 deletions

View File

@ -154,9 +154,9 @@ namespace osu.Game.Tests.Beatmaps.IO
}
[Test]
public async Task TestImportThenImportWithNewerTimestamp()
public async Task TestImportThenImportWithChangedFile()
{
using (HeadlessGameHost host = new CleanRunHeadlessGameHost(nameof(TestImportThenImportWithNewerTimestamp)))
using (HeadlessGameHost host = new CleanRunHeadlessGameHost(nameof(TestImportThenImportWithChangedFile)))
{
try
{
@ -174,8 +174,9 @@ namespace osu.Game.Tests.Beatmaps.IO
using (var zip = ZipArchive.Open(temp))
zip.WriteToDirectory(extractedFolder);
// change timestamp
new FileInfo(Directory.GetFiles(extractedFolder).First()).LastWriteTime = DateTime.Now;
// arbitrary write to non-hashed file
using (var sw = new FileInfo(Directory.GetFiles(extractedFolder, "*.mp3").First()).AppendText())
sw.WriteLine("text");
using (var zip = ZipArchive.Create())
{

View File

@ -667,10 +667,16 @@ namespace osu.Game.Database
/// <param name="import">The newly imported model.</param>
/// <returns>Whether the existing model should be restored and used. Returning false will delete the existing and force a re-import.</returns>
protected virtual bool CanReuseExisting(TModel existing, TModel import) =>
getFilenames(existing.Files).SequenceEqual(getFilenames(import.Files)) &&
// poor-man's (cheap) equality comparison, avoiding hashing unnecessarily.
// can switch to full hash checks on a per-case basis (or for all) if we decide this is not a performance issue.
getTimestamps(existing.Files).SequenceEqual(getTimestamps(import.Files));
// for the best or worst, we copy and import files of a new import before checking whether
// it is a duplicate. so to check if anything has changed, we can just compare all FileInfo IDs.
getIDs(existing.Files).SequenceEqual(getIDs(import.Files)) &&
getFilenames(existing.Files).SequenceEqual(getFilenames(import.Files));
private IEnumerable<long> getIDs(List<TFileModel> files)
{
foreach (var f in files.OrderBy(f => f.Filename))
yield return f.FileInfo.ID;
}
private IEnumerable<string> getFilenames(List<TFileModel> files)
{
@ -678,12 +684,6 @@ namespace osu.Game.Database
yield return f.Filename;
}
private IEnumerable<long> getTimestamps(List<TFileModel> files)
{
foreach (var f in files.OrderBy(f => f.Filename))
yield return File.GetLastWriteTimeUtc(Files.Storage.GetFullPath(f.FileInfo.StoragePath)).ToFileTime();
}
private DbSet<TModel> queryModel() => ContextFactory.Get().Set<TModel>();
protected virtual string HumanisedModelName => $"{typeof(TModel).Name.Replace("Info", "").ToLower()}";