1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 16:06:06 +08:00

Check filenames and timestamps before reusing an already imported model

This commit is contained in:
Dean Herbert 2020-06-03 18:03:10 +09:00
parent 73467410ab
commit c155ab8339
2 changed files with 21 additions and 5 deletions

View File

@ -258,9 +258,9 @@ namespace osu.Game.Beatmaps
/// <returns>The first result for the provided query, or null if no results were found.</returns> /// <returns>The first result for the provided query, or null if no results were found.</returns>
public BeatmapSetInfo QueryBeatmapSet(Expression<Func<BeatmapSetInfo, bool>> query) => beatmaps.ConsumableItems.AsNoTracking().FirstOrDefault(query); public BeatmapSetInfo QueryBeatmapSet(Expression<Func<BeatmapSetInfo, bool>> query) => beatmaps.ConsumableItems.AsNoTracking().FirstOrDefault(query);
protected override bool CanUndelete(BeatmapSetInfo existing, BeatmapSetInfo import) protected override bool CanReuseExisting(BeatmapSetInfo existing, BeatmapSetInfo import)
{ {
if (!base.CanUndelete(existing, import)) if (!base.CanReuseExisting(existing, import))
return false; return false;
var existingIds = existing.Beatmaps.Select(b => b.OnlineBeatmapID).OrderBy(i => i); var existingIds = existing.Beatmaps.Select(b => b.OnlineBeatmapID).OrderBy(i => i);

View File

@ -332,7 +332,7 @@ namespace osu.Game.Database
if (existing != null) if (existing != null)
{ {
if (CanUndelete(existing, item)) if (CanReuseExisting(existing, item))
{ {
Undelete(existing); Undelete(existing);
LogForModel(item, $"Found existing {HumanisedModelName} for {item} (ID {existing.ID}) skipping import."); LogForModel(item, $"Found existing {HumanisedModelName} for {item} (ID {existing.ID}) skipping import.");
@ -660,13 +660,29 @@ namespace osu.Game.Database
protected TModel CheckForExisting(TModel model) => model.Hash == null ? null : ModelStore.ConsumableItems.FirstOrDefault(b => b.Hash == model.Hash); protected TModel CheckForExisting(TModel model) => model.Hash == null ? null : ModelStore.ConsumableItems.FirstOrDefault(b => b.Hash == model.Hash);
/// <summary> /// <summary>
/// After an existing <typeparamref name="TModel"/> is found during an import process, the default behaviour is to restore the existing /// After an existing <typeparamref name="TModel"/> is found during an import process, the default behaviour is to use/restore the existing
/// item and skip the import. This method allows changing that behaviour. /// item and skip the import. This method allows changing that behaviour.
/// </summary> /// </summary>
/// <param name="existing">The existing model.</param> /// <param name="existing">The existing model.</param>
/// <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 CanUndelete(TModel existing, TModel import) => true; 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));
private IEnumerable<string> getFilenames(List<TFileModel> files)
{
foreach (var f in files.OrderBy(f => 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>();