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

View File

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