2019-06-20 00:33:51 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2019-01-24 16:43:03 +08:00
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
2019-05-28 17:59:21 +08:00
using System.Threading ;
2018-08-31 17:28:53 +08:00
using System.Threading.Tasks ;
2019-09-20 20:46:43 +08:00
using Humanizer ;
2018-07-18 11:58:28 +08:00
using JetBrains.Annotations ;
2018-04-13 17:19:50 +08:00
using Microsoft.EntityFrameworkCore ;
2018-11-28 18:16:05 +08:00
using osu.Framework.Extensions ;
2019-09-19 16:35:45 +08:00
using osu.Framework.Extensions.IEnumerableExtensions ;
2018-04-13 17:19:50 +08:00
using osu.Framework.Logging ;
using osu.Framework.Platform ;
2019-05-28 17:59:21 +08:00
using osu.Framework.Threading ;
2021-11-09 20:34:56 +08:00
using osu.Game.Extensions ;
2018-04-13 17:19:50 +08:00
using osu.Game.IO ;
using osu.Game.IO.Archives ;
using osu.Game.IPC ;
using osu.Game.Overlays.Notifications ;
namespace osu.Game.Database
{
/// <summary>
/// Encapsulates a model store class to give it import functionality.
/// Adds cross-functionality with <see cref="FileStore"/> to give access to the central file store for the provided model.
/// </summary>
/// <typeparam name="TModel">The model type.</typeparam>
/// <typeparam name="TFileModel">The associated file join type.</typeparam>
2021-11-05 15:27:13 +08:00
public abstract class ArchiveModelManager < TModel , TFileModel > : IModelImporter < TModel > , IModelManager < TModel > , IModelFileManager < TModel , TFileModel >
2018-04-13 17:19:50 +08:00
where TModel : class , IHasFiles < TFileModel > , IHasPrimaryKey , ISoftDelete
2021-10-20 16:33:36 +08:00
where TFileModel : class , INamedFileInfo , IHasPrimaryKey , new ( )
2018-04-13 17:19:50 +08:00
{
2019-08-08 17:26:03 +08:00
private const int import_queue_request_concurrency = 1 ;
2021-02-17 18:09:38 +08:00
/// <summary>
/// The size of a batch import operation before considering it a lower priority operation.
/// </summary>
private const int low_priority_import_batch_size = 1 ;
2019-08-08 17:26:03 +08:00
/// <summary>
/// A singleton scheduler shared by all <see cref="ArchiveModelManager{TModel,TFileModel}"/>.
/// </summary>
/// <remarks>
/// This scheduler generally performs IO and CPU intensive work so concurrency is limited harshly.
/// It is mainly being used as a queue mechanism for large imports.
/// </remarks>
private static readonly ThreadedTaskScheduler import_scheduler = new ThreadedTaskScheduler ( import_queue_request_concurrency , nameof ( ArchiveModelManager < TModel , TFileModel > ) ) ;
2021-02-17 18:09:38 +08:00
/// <summary>
/// A second scheduler for lower priority imports.
/// For simplicity, these will just run in parallel with normal priority imports, but a future refactor would see this implemented via a custom scheduler/queue.
/// See https://gist.github.com/peppy/f0e118a14751fc832ca30dd48ba3876b for an incomplete version of this.
/// </summary>
private static readonly ThreadedTaskScheduler import_scheduler_low_priority = new ThreadedTaskScheduler ( import_queue_request_concurrency , nameof ( ArchiveModelManager < TModel , TFileModel > ) ) ;
2018-04-13 17:19:50 +08:00
public Action < Notification > PostNotification { protected get ; set ; }
/// <summary>
2020-05-27 15:08:47 +08:00
/// Fired when a new or updated <typeparamref name="TModel"/> becomes available in the database.
2018-04-13 17:19:50 +08:00
/// This is not guaranteed to run on the update thread.
/// </summary>
2021-11-06 21:31:49 +08:00
public event Action < TModel > ItemUpdated ;
2018-04-13 17:19:50 +08:00
/// <summary>
2019-11-17 20:48:23 +08:00
/// Fired when a <typeparamref name="TModel"/> is removed from the database.
2018-04-13 17:19:50 +08:00
/// This is not guaranteed to run on the update thread.
/// </summary>
2021-11-06 21:31:49 +08:00
public event Action < TModel > ItemRemoved ;
2018-04-13 17:19:50 +08:00
2021-06-28 11:13:11 +08:00
public virtual IEnumerable < string > HandledExtensions = > new [ ] { @".zip" } ;
2018-04-13 17:19:50 +08:00
2020-01-14 18:23:34 +08:00
protected readonly FileStore Files ;
2018-04-13 17:19:50 +08:00
protected readonly IDatabaseContextFactory ContextFactory ;
protected readonly MutableDatabaseBackedStore < TModel > ModelStore ;
// ReSharper disable once NotAccessedField.Local (we should keep a reference to this so it is not finalised)
private ArchiveImportIPCChannel ipc ;
2019-03-01 09:25:21 +08:00
protected ArchiveModelManager ( Storage storage , IDatabaseContextFactory contextFactory , MutableDatabaseBackedStoreWithFileIncludes < TModel , TFileModel > modelStore , IIpcHost importHost = null )
2018-04-13 17:19:50 +08:00
{
ContextFactory = contextFactory ;
ModelStore = modelStore ;
2021-11-05 17:05:31 +08:00
ModelStore . ItemUpdated + = item = > handleEvent ( ( ) = > ItemUpdated ? . Invoke ( item ) ) ;
ModelStore . ItemRemoved + = item = > handleEvent ( ( ) = > ItemRemoved ? . Invoke ( item ) ) ;
2018-04-13 17:19:50 +08:00
Files = new FileStore ( contextFactory , storage ) ;
if ( importHost ! = null )
ipc = new ArchiveImportIPCChannel ( importHost , this ) ;
ModelStore . Cleanup ( ) ;
}
/// <summary>
2019-11-17 20:48:23 +08:00
/// Import one or more <typeparamref name="TModel"/> items from filesystem <paramref name="paths"/>.
2018-04-13 17:19:50 +08:00
/// </summary>
2021-02-17 18:09:38 +08:00
/// <remarks>
/// This will be treated as a low priority import if more than one path is specified; use <see cref="Import(ImportTask[])"/> to always import at standard priority.
/// This will post notifications tracking progress.
/// </remarks>
2018-04-13 17:19:50 +08:00
/// <param name="paths">One or more archive locations on disk.</param>
2019-06-12 16:08:50 +08:00
public Task Import ( params string [ ] paths )
2018-04-13 17:19:50 +08:00
{
2021-10-13 11:19:10 +08:00
var notification = new ImportProgressNotification ( ) ;
2018-04-13 17:19:50 +08:00
PostNotification ? . Invoke ( notification ) ;
2019-05-28 17:59:21 +08:00
2020-12-07 17:00:45 +08:00
return Import ( notification , paths . Select ( p = > new ImportTask ( p ) ) . ToArray ( ) ) ;
2019-02-25 17:24:06 +08:00
}
2020-12-16 21:28:16 +08:00
public Task Import ( params ImportTask [ ] tasks )
2020-12-07 17:00:45 +08:00
{
2021-10-13 11:19:10 +08:00
var notification = new ImportProgressNotification ( ) ;
2020-12-07 17:00:45 +08:00
PostNotification ? . Invoke ( notification ) ;
2020-12-16 21:28:16 +08:00
return Import ( notification , tasks ) ;
2020-12-07 17:00:45 +08:00
}
2021-09-30 18:33:12 +08:00
public async Task < IEnumerable < ILive < TModel > > > Import ( ProgressNotification notification , params ImportTask [ ] tasks )
2019-02-25 17:24:06 +08:00
{
2021-02-26 16:30:59 +08:00
if ( tasks . Length = = 0 )
{
notification . CompletionText = $"No {HumanisedModelName}s were found to import!" ;
notification . State = ProgressNotificationState . Completed ;
2021-09-30 18:33:12 +08:00
return Enumerable . Empty < ILive < TModel > > ( ) ;
2021-02-26 16:30:59 +08:00
}
2019-02-25 17:24:06 +08:00
notification . Progress = 0 ;
2019-09-22 02:00:24 +08:00
notification . Text = $"{HumanisedModelName.Humanize(LetterCasing.Title)} import is initialising..." ;
2018-04-13 17:19:50 +08:00
int current = 0 ;
2019-04-01 11:16:05 +08:00
2021-09-30 18:33:12 +08:00
var imported = new List < ILive < TModel > > ( ) ;
2019-06-10 12:19:58 +08:00
2021-02-17 18:09:38 +08:00
bool isLowPriorityImport = tasks . Length > low_priority_import_batch_size ;
2021-04-28 14:54:58 +08:00
try
2018-04-13 17:19:50 +08:00
{
2021-04-28 14:54:58 +08:00
await Task . WhenAll ( tasks . Select ( async task = >
2018-04-13 17:19:50 +08:00
{
2021-04-28 14:54:58 +08:00
notification . CancellationToken . ThrowIfCancellationRequested ( ) ;
2018-12-19 03:49:53 +08:00
2021-04-28 14:54:58 +08:00
try
2019-06-10 15:14:42 +08:00
{
2021-04-28 14:54:58 +08:00
var model = await Import ( task , isLowPriorityImport , notification . CancellationToken ) . ConfigureAwait ( false ) ;
lock ( imported )
{
if ( model ! = null )
imported . Add ( model ) ;
current + + ;
2018-04-13 17:19:50 +08:00
2021-04-28 14:54:58 +08:00
notification . Text = $"Imported {current} of {tasks.Length} {HumanisedModelName}s" ;
notification . Progress = ( float ) current / tasks . Length ;
}
2019-06-10 15:14:42 +08:00
}
2021-04-28 14:54:58 +08:00
catch ( TaskCanceledException )
{
throw ;
}
catch ( Exception e )
{
Logger . Error ( e , $@"Could not import ({task})" , LoggingTarget . Database ) ;
}
} ) ) . ConfigureAwait ( false ) ;
}
catch ( OperationCanceledException )
{
if ( imported . Count = = 0 )
2018-04-13 17:19:50 +08:00
{
2021-04-28 14:54:58 +08:00
notification . State = ProgressNotificationState . Cancelled ;
return imported ;
2018-04-13 17:19:50 +08:00
}
2021-04-28 14:54:58 +08:00
}
2018-04-13 17:19:50 +08:00
2018-09-07 17:14:23 +08:00
if ( imported . Count = = 0 )
{
2019-09-22 02:00:24 +08:00
notification . Text = $"{HumanisedModelName.Humanize(LetterCasing.Title)} import failed!" ;
2018-09-07 17:14:23 +08:00
notification . State = ProgressNotificationState . Cancelled ;
}
else
{
2019-02-25 17:24:06 +08:00
notification . CompletionText = imported . Count = = 1
2021-11-09 20:34:56 +08:00
? $"Imported {imported.First().Value.GetDisplayString()}!"
2019-07-05 13:47:55 +08:00
: $"Imported {imported.Count} {HumanisedModelName}s!" ;
2019-02-25 17:24:06 +08:00
2021-10-04 15:35:55 +08:00
if ( imported . Count > 0 & & PostImport ! = null )
2018-09-07 17:18:03 +08:00
{
2019-02-25 17:24:06 +08:00
notification . CompletionText + = " Click to view." ;
notification . CompletionClickAction = ( ) = >
{
2021-10-04 15:35:55 +08:00
PostImport ? . Invoke ( imported ) ;
2019-02-25 17:24:06 +08:00
return true ;
} ;
}
2018-09-07 17:14:23 +08:00
notification . State = ProgressNotificationState . Completed ;
}
2019-10-28 16:41:42 +08:00
return imported ;
2018-04-13 17:19:50 +08:00
}
2019-01-29 17:34:10 +08:00
/// <summary>
2019-11-17 20:48:23 +08:00
/// Import one <typeparamref name="TModel"/> from the filesystem and delete the file on success.
2020-12-07 17:00:45 +08:00
/// Note that this bypasses the UI flow and should only be used for special cases or testing.
2019-01-29 17:34:10 +08:00
/// </summary>
2020-12-08 11:48:59 +08:00
/// <param name="task">The <see cref="ImportTask"/> containing data about the <typeparamref name="TModel"/> to import.</param>
2021-02-17 18:09:38 +08:00
/// <param name="lowPriority">Whether this is a low priority import.</param>
2019-05-28 17:59:21 +08:00
/// <param name="cancellationToken">An optional cancellation token.</param>
2019-01-29 17:34:10 +08:00
/// <returns>The imported model, if successful.</returns>
2021-09-30 18:33:12 +08:00
public async Task < ILive < TModel > > Import ( ImportTask task , bool lowPriority = false , CancellationToken cancellationToken = default )
2019-01-29 17:34:10 +08:00
{
2019-06-10 12:37:20 +08:00
cancellationToken . ThrowIfCancellationRequested ( ) ;
2021-09-30 18:33:12 +08:00
ILive < TModel > import ;
2020-12-07 17:00:45 +08:00
using ( ArchiveReader reader = task . GetReader ( ) )
2021-03-08 11:57:16 +08:00
import = await Import ( reader , lowPriority , cancellationToken ) . ConfigureAwait ( false ) ;
2019-01-29 17:34:10 +08:00
// We may or may not want to delete the file depending on where it is stored.
// e.g. reconstructing/repairing database with items from default storage.
// Also, not always a single file, i.e. for LegacyFilesystemReader
// TODO: Add a check to prevent files from storage to be deleted.
try
{
2020-12-07 17:00:45 +08:00
if ( import ! = null & & File . Exists ( task . Path ) & & ShouldDeleteArchive ( task . Path ) )
File . Delete ( task . Path ) ;
2019-01-29 17:34:10 +08:00
}
catch ( Exception e )
{
2021-09-30 18:33:12 +08:00
LogForModel ( import ? . Value , $@"Could not delete original file after import ({task})" , e ) ;
2019-01-29 17:34:10 +08:00
}
return import ;
}
2021-10-04 15:35:55 +08:00
public Action < IEnumerable < ILive < TModel > > > PostImport { protected get ; set ; }
2018-09-07 15:30:11 +08:00
2018-04-13 17:19:50 +08:00
/// <summary>
2021-02-17 16:04:43 +08:00
/// Silently import an item from an <see cref="ArchiveReader"/>.
2018-04-13 17:19:50 +08:00
/// </summary>
/// <param name="archive">The archive to be imported.</param>
2021-02-17 18:09:38 +08:00
/// <param name="lowPriority">Whether this is a low priority import.</param>
2019-05-28 17:59:21 +08:00
/// <param name="cancellationToken">An optional cancellation token.</param>
2021-09-30 18:33:12 +08:00
public Task < ILive < TModel > > Import ( ArchiveReader archive , bool lowPriority = false , CancellationToken cancellationToken = default )
2018-04-13 17:19:50 +08:00
{
2019-06-10 12:37:20 +08:00
cancellationToken . ThrowIfCancellationRequested ( ) ;
2019-06-10 18:34:32 +08:00
TModel model = null ;
2019-06-10 16:12:37 +08:00
2018-07-18 11:58:28 +08:00
try
{
2019-06-10 16:12:37 +08:00
model = CreateModel ( archive ) ;
2018-11-28 18:16:05 +08:00
2020-01-02 12:20:38 +08:00
if ( model = = null )
2021-11-12 14:26:28 +08:00
return Task . FromResult < ILive < TModel > > ( null ) ;
2018-07-18 11:58:28 +08:00
}
2019-06-10 12:37:20 +08:00
catch ( TaskCanceledException )
{
throw ;
2018-07-18 11:58:28 +08:00
}
catch ( Exception e )
{
2021-06-28 11:13:11 +08:00
LogForModel ( model , @ $"Model creation of {archive.Name} failed." , e ) ;
2018-07-18 11:58:28 +08:00
return null ;
}
2019-06-10 16:12:37 +08:00
2021-02-17 18:09:38 +08:00
return Import ( model , archive , lowPriority , cancellationToken ) ;
2018-07-18 11:58:28 +08:00
}
2018-11-28 18:16:05 +08:00
/// <summary>
/// Any file extensions which should be included in hash creation.
/// Generally should include all file types which determine the file's uniqueness.
/// Large files should be avoided if possible.
/// </summary>
2020-09-11 14:06:10 +08:00
/// <remarks>
/// This is only used by the default hash implementation. If <see cref="ComputeHash"/> is overridden, it will not be used.
/// </remarks>
2018-11-28 18:16:05 +08:00
protected abstract string [ ] HashableFileTypes { get ; }
2020-05-02 13:35:12 +08:00
internal static void LogForModel ( TModel model , string message , Exception e = null )
2019-06-10 18:34:32 +08:00
{
string prefix = $"[{(model?.Hash ?? " ? ? ? ? ? ").Substring(0, 5)}]" ;
if ( e ! = null )
Logger . Error ( e , $"{prefix} {message}" , LoggingTarget . Database ) ;
else
Logger . Log ( $"{prefix} {message}" , LoggingTarget . Database ) ;
}
2021-06-27 15:35:13 +08:00
/// <summary>
/// Whether the implementation overrides <see cref="ComputeHash"/> with a custom implementation.
2021-06-28 13:07:21 +08:00
/// Custom hash implementations must bypass the early exit in the import flow (see <see cref="computeHashFast"/> usage).
2021-06-27 15:35:13 +08:00
/// </summary>
protected virtual bool HasCustomHashFunction = > false ;
2018-11-28 18:16:05 +08:00
/// <summary>
2018-11-30 14:09:15 +08:00
/// Create a SHA-2 hash from the provided archive based on file content of all files matching <see cref="HashableFileTypes"/>.
2018-11-28 18:16:05 +08:00
/// </summary>
2019-12-26 17:44:31 +08:00
/// <remarks>
/// In the case of no matching files, a hash will be generated from the passed archive's <see cref="ArchiveReader.Name"/>.
/// </remarks>
2021-10-24 22:48:46 +08:00
protected virtual string ComputeHash ( TModel item )
2018-11-28 18:16:05 +08:00
{
2021-10-20 14:51:14 +08:00
var hashableFiles = item . Files
. Where ( f = > HashableFileTypes . Any ( ext = > f . Filename . EndsWith ( ext , StringComparison . OrdinalIgnoreCase ) ) )
. OrderBy ( f = > f . Filename )
. ToArray ( ) ;
2021-10-21 12:35:26 +08:00
if ( hashableFiles . Length > 0 )
{
// for now, concatenate all hashable files in the set to create a unique hash.
MemoryStream hashable = new MemoryStream ( ) ;
2021-06-27 15:34:40 +08:00
2021-10-21 12:35:26 +08:00
foreach ( TFileModel file in hashableFiles )
{
2021-11-19 15:07:55 +08:00
using ( Stream s = Files . Store . GetStream ( file . FileInfo . GetStoragePath ( ) ) )
2021-10-21 12:35:26 +08:00
s . CopyTo ( hashable ) ;
}
2019-11-11 20:05:36 +08:00
2021-10-21 12:35:26 +08:00
if ( hashable . Length > 0 )
return hashable . ComputeSHA2Hash ( ) ;
2019-11-11 19:53:22 +08:00
}
2018-11-28 18:16:05 +08:00
2021-10-21 12:35:26 +08:00
return generateFallbackHash ( ) ;
2018-11-28 18:16:05 +08:00
}
2018-07-18 11:58:28 +08:00
/// <summary>
2021-02-17 16:04:43 +08:00
/// Silently import an item from a <typeparamref name="TModel"/>.
2018-07-18 11:58:28 +08:00
/// </summary>
/// <param name="item">The model to be imported.</param>
/// <param name="archive">An optional archive to use for model population.</param>
2021-02-17 18:09:38 +08:00
/// <param name="lowPriority">Whether this is a low priority import.</param>
2019-05-28 17:59:21 +08:00
/// <param name="cancellationToken">An optional cancellation token.</param>
2021-09-30 18:33:12 +08:00
public virtual async Task < ILive < TModel > > Import ( TModel item , ArchiveReader archive = null , bool lowPriority = false , CancellationToken cancellationToken = default ) = > await Task . Factory . StartNew ( async ( ) = >
2018-07-18 11:58:28 +08:00
{
2019-06-10 12:37:20 +08:00
cancellationToken . ThrowIfCancellationRequested ( ) ;
2021-06-27 15:48:42 +08:00
bool checkedExisting = false ;
TModel existing = null ;
2021-06-27 15:35:13 +08:00
if ( archive ! = null & & ! HasCustomHashFunction )
2021-06-27 15:34:40 +08:00
{
2021-06-27 15:48:42 +08:00
// this is a fast bail condition to improve large import performance.
2021-06-27 15:34:40 +08:00
item . Hash = computeHashFast ( archive ) ;
2021-06-27 15:48:42 +08:00
checkedExisting = true ;
existing = CheckForExisting ( item ) ;
2021-06-27 15:34:40 +08:00
2021-06-27 15:48:42 +08:00
if ( existing ! = null )
2021-06-27 15:34:40 +08:00
{
// bare minimum comparisons
2021-06-27 15:48:42 +08:00
//
// note that this should really be checking filesizes on disk (of existing files) for some degree of sanity.
// or alternatively doing a faster hash check. either of these require database changes and reprocessing of existing files.
2021-06-28 09:11:27 +08:00
if ( CanSkipImport ( existing , item ) & &
getFilenames ( existing . Files ) . SequenceEqual ( getShortenedFilenames ( archive ) . Select ( p = > p . shortened ) . OrderBy ( f = > f ) ) )
2021-06-27 19:22:48 +08:00
{
2021-06-28 11:13:11 +08:00
LogForModel ( item , @ $"Found existing (optimised) {HumanisedModelName} for {item} (ID {existing.ID}) – skipping import." ) ;
2021-06-27 19:22:48 +08:00
Undelete ( existing ) ;
2021-09-30 18:33:12 +08:00
return existing . ToEntityFrameworkLive ( ) ;
2021-06-27 19:22:48 +08:00
}
2021-06-28 09:42:42 +08:00
2021-06-28 11:13:11 +08:00
LogForModel ( item , @"Found existing (optimised) but failed pre-check." ) ;
2021-06-27 15:34:40 +08:00
}
}
2019-06-10 15:14:42 +08:00
void rollback ( )
{
if ( ! Delete ( item ) )
{
// We may have not yet added the model to the underlying table, but should still clean up files.
2021-06-28 11:13:11 +08:00
LogForModel ( item , @"Dereferencing files for incomplete import." ) ;
2019-06-10 15:14:42 +08:00
Files . Dereference ( item . Files . Select ( f = > f . FileInfo ) . ToArray ( ) ) ;
}
}
2021-07-03 23:58:12 +08:00
delayEvents ( ) ;
2018-05-28 18:56:27 +08:00
try
2018-04-13 17:19:50 +08:00
{
2021-06-28 11:13:11 +08:00
LogForModel ( item , @"Beginning import..." ) ;
2018-08-17 12:50:27 +08:00
2021-11-24 12:42:07 +08:00
if ( archive ! = null )
item . Files . AddRange ( createFileInfos ( archive , Files ) ) ;
2021-10-24 22:48:46 +08:00
item . Hash = ComputeHash ( item ) ;
2019-05-28 17:59:21 +08:00
2021-03-08 11:57:16 +08:00
await Populate ( item , archive , cancellationToken ) . ConfigureAwait ( false ) ;
2018-08-17 12:50:27 +08:00
2018-05-28 18:56:27 +08:00
using ( var write = ContextFactory . GetForWrite ( ) ) // used to share a context for full import. keep in mind this will block all writes.
{
2018-05-29 12:48:14 +08:00
try
{
2021-06-28 11:13:11 +08:00
if ( ! write . IsTransactionLeader ) throw new InvalidOperationException ( @ $"Ensure there is no parent transaction so errors can correctly be handled by {this}" ) ;
2018-04-13 17:19:50 +08:00
2021-06-27 15:48:42 +08:00
if ( ! checkedExisting )
existing = CheckForExisting ( item ) ;
2018-04-13 17:19:50 +08:00
2018-05-29 15:14:09 +08:00
if ( existing ! = null )
{
2021-06-28 08:54:18 +08:00
if ( CanReuseExisting ( existing , item ) )
2019-03-11 16:03:01 +08:00
{
Undelete ( existing ) ;
2021-06-28 11:13:11 +08:00
LogForModel ( item , @ $"Found existing {HumanisedModelName} for {item} (ID {existing.ID}) – skipping import." ) ;
2019-06-10 16:12:37 +08:00
// existing item will be used; rollback new import and exit early.
2019-06-10 15:14:42 +08:00
rollback ( ) ;
2019-06-10 16:12:37 +08:00
flushEvents ( true ) ;
2021-09-30 18:33:12 +08:00
return existing . ToEntityFrameworkLive ( ) ;
2019-03-11 16:03:01 +08:00
}
2019-06-10 18:34:32 +08:00
2021-06-28 11:13:11 +08:00
LogForModel ( item , @"Found existing but failed re-use check." ) ;
2019-06-10 18:34:32 +08:00
Delete ( existing ) ;
ModelStore . PurgeDeletable ( s = > s . ID = = existing . ID ) ;
2018-05-29 15:14:09 +08:00
}
2018-04-13 17:19:50 +08:00
2019-03-11 16:03:01 +08:00
PreImport ( item ) ;
2018-04-13 17:19:50 +08:00
2018-05-29 12:48:14 +08:00
// import to store
2019-02-25 17:24:06 +08:00
ModelStore . Add ( item ) ;
2018-05-29 12:48:14 +08:00
}
catch ( Exception e )
{
write . Errors . Add ( e ) ;
throw ;
}
2018-05-28 18:56:27 +08:00
}
2018-05-29 15:14:09 +08:00
2021-06-28 11:13:11 +08:00
LogForModel ( item , @"Import successfully completed!" ) ;
2018-04-13 17:19:50 +08:00
}
2018-05-29 17:37:45 +08:00
catch ( Exception e )
2018-05-28 18:56:27 +08:00
{
2019-06-10 15:14:42 +08:00
if ( ! ( e is TaskCanceledException ) )
2021-06-28 11:13:11 +08:00
LogForModel ( item , @"Database import or population failed and has been rolled back." , e ) ;
2019-06-10 15:14:42 +08:00
rollback ( ) ;
2019-06-10 16:12:37 +08:00
flushEvents ( false ) ;
throw ;
2018-05-29 18:43:52 +08:00
}
2018-05-28 18:56:27 +08:00
2019-06-10 16:12:37 +08:00
flushEvents ( true ) ;
2021-09-30 18:33:12 +08:00
return item . ToEntityFrameworkLive ( ) ;
2021-03-08 11:57:16 +08:00
} , cancellationToken , TaskCreationOptions . HideScheduler , lowPriority ? import_scheduler_low_priority : import_scheduler ) . Unwrap ( ) . ConfigureAwait ( false ) ;
2018-04-13 17:19:50 +08:00
2020-09-01 14:50:08 +08:00
/// <summary>
2020-09-25 12:10:04 +08:00
/// Replace an existing file with a new version.
2020-09-01 14:50:08 +08:00
/// </summary>
/// <param name="model">The item to operate on.</param>
2020-09-25 12:10:04 +08:00
/// <param name="file">The existing file to be replaced.</param>
2020-09-01 14:50:08 +08:00
/// <param name="contents">The new file contents.</param>
2021-11-29 17:08:02 +08:00
public void ReplaceFile ( TModel model , TFileModel file , Stream contents )
2020-09-25 12:10:04 +08:00
{
using ( ContextFactory . GetForWrite ( ) )
{
DeleteFile ( model , file ) ;
2021-11-29 17:08:02 +08:00
AddFile ( model , contents , file . Filename ) ;
2020-09-25 12:10:04 +08:00
}
}
/// <summary>
2021-05-10 21:32:56 +08:00
/// Delete an existing file.
2020-09-25 12:10:04 +08:00
/// </summary>
/// <param name="model">The item to operate on.</param>
/// <param name="file">The existing file to be deleted.</param>
public void DeleteFile ( TModel model , TFileModel file )
2020-01-08 11:36:07 +08:00
{
2021-10-22 13:48:20 +08:00
using ( var usage = ContextFactory . GetForWrite ( ) )
2020-01-08 11:36:07 +08:00
{
2021-10-22 13:48:20 +08:00
// Dereference the existing file info, since the file model will be removed.
if ( file . FileInfo ! = null )
2020-09-25 17:40:20 +08:00
{
2021-10-22 13:48:20 +08:00
Files . Dereference ( file . FileInfo ) ;
2020-01-14 17:43:06 +08:00
2021-10-22 13:48:20 +08:00
if ( file . ID > 0 )
{
// This shouldn't be required, but here for safety in case the provided TModel is not being change tracked
// Definitely can be removed once we rework the database backend.
usage . Context . Set < TFileModel > ( ) . Remove ( file ) ;
2021-10-20 14:22:47 +08:00
}
}
2021-10-22 13:48:20 +08:00
2020-01-09 18:42:47 +08:00
model . Files . Remove ( file ) ;
2020-09-25 12:10:04 +08:00
}
}
/// <summary>
/// Add a new file.
/// </summary>
/// <param name="model">The item to operate on.</param>
/// <param name="contents">The new file contents.</param>
/// <param name="filename">The filename for the new file.</param>
public void AddFile ( TModel model , Stream contents , string filename )
{
2021-10-22 13:48:20 +08:00
using ( ContextFactory . GetForWrite ( ) )
2021-10-20 14:22:47 +08:00
{
2020-01-09 18:42:47 +08:00
model . Files . Add ( new TFileModel
{
2020-09-25 12:10:04 +08:00
Filename = filename ,
2020-01-09 18:42:47 +08:00
FileInfo = Files . Add ( contents )
} ) ;
2020-01-08 11:36:07 +08:00
}
2021-10-22 13:48:20 +08:00
if ( model . ID > 0 )
Update ( model ) ;
2020-01-08 11:36:07 +08:00
}
2018-04-13 17:19:50 +08:00
/// <summary>
/// Perform an update of the specified item.
2020-01-08 11:36:07 +08:00
/// TODO: Support file additions/removals.
2018-04-13 17:19:50 +08:00
/// </summary>
/// <param name="item">The item to update.</param>
2020-01-08 11:36:07 +08:00
public void Update ( TModel item )
{
using ( ContextFactory . GetForWrite ( ) )
{
2020-09-11 14:06:10 +08:00
item . Hash = ComputeHash ( item ) ;
2020-01-08 11:36:07 +08:00
ModelStore . Update ( item ) ;
}
}
2020-06-02 16:22:09 +08:00
2018-04-13 17:19:50 +08:00
/// <summary>
/// Delete an item from the manager.
/// Is a no-op for already deleted items.
/// </summary>
/// <param name="item">The item to delete.</param>
2018-09-21 11:21:27 +08:00
/// <returns>false if no operation was performed</returns>
public bool Delete ( TModel item )
2018-04-13 17:19:50 +08:00
{
2018-05-30 12:43:43 +08:00
using ( ContextFactory . GetForWrite ( ) )
2018-04-13 17:19:50 +08:00
{
// re-fetch the model on the import context.
2018-09-21 08:01:04 +08:00
var foundModel = queryModel ( ) . Include ( s = > s . Files ) . ThenInclude ( f = > f . FileInfo ) . FirstOrDefault ( s = > s . ID = = item . ID ) ;
2018-04-13 17:19:50 +08:00
2018-09-21 11:21:27 +08:00
if ( foundModel = = null | | foundModel . DeletePending ) return false ;
2018-04-13 17:19:50 +08:00
if ( ModelStore . Delete ( foundModel ) )
Files . Dereference ( foundModel . Files . Select ( f = > f . FileInfo ) . ToArray ( ) ) ;
2018-09-21 11:21:27 +08:00
return true ;
2018-04-13 17:19:50 +08:00
}
}
/// <summary>
/// Delete multiple items.
/// This will post notifications tracking progress.
/// </summary>
2019-05-09 14:15:02 +08:00
public void Delete ( List < TModel > items , bool silent = false )
2018-04-13 17:19:50 +08:00
{
if ( items . Count = = 0 ) return ;
var notification = new ProgressNotification
{
Progress = 0 ,
2019-06-12 19:41:02 +08:00
Text = $"Preparing to delete all {HumanisedModelName}s..." ,
CompletionText = $"Deleted all {HumanisedModelName}s!" ,
2018-04-13 17:19:50 +08:00
State = ProgressNotificationState . Active ,
} ;
2019-05-09 14:15:02 +08:00
if ( ! silent )
PostNotification ? . Invoke ( notification ) ;
2018-04-13 17:19:50 +08:00
int i = 0 ;
2019-10-02 17:48:50 +08:00
foreach ( var b in items )
2018-04-13 17:19:50 +08:00
{
2019-10-02 17:48:50 +08:00
if ( notification . State = = ProgressNotificationState . Cancelled )
// user requested abort
return ;
2018-04-13 17:19:50 +08:00
2019-10-02 17:48:50 +08:00
notification . Text = $"Deleting {HumanisedModelName}s ({++i} of {items.Count})" ;
2018-04-13 17:19:50 +08:00
2019-10-02 17:48:50 +08:00
Delete ( b ) ;
2018-04-13 17:19:50 +08:00
2019-10-02 17:48:50 +08:00
notification . Progress = ( float ) i / items . Count ;
2018-04-13 17:19:50 +08:00
}
notification . State = ProgressNotificationState . Completed ;
}
/// <summary>
/// Restore multiple items that were previously deleted.
/// This will post notifications tracking progress.
/// </summary>
2019-05-09 14:15:02 +08:00
public void Undelete ( List < TModel > items , bool silent = false )
2018-04-13 17:19:50 +08:00
{
if ( ! items . Any ( ) ) return ;
var notification = new ProgressNotification
{
CompletionText = "Restored all deleted items!" ,
Progress = 0 ,
State = ProgressNotificationState . Active ,
} ;
2019-05-09 14:15:02 +08:00
if ( ! silent )
PostNotification ? . Invoke ( notification ) ;
2018-04-13 17:19:50 +08:00
int i = 0 ;
2019-10-03 10:23:21 +08:00
foreach ( var item in items )
2018-04-13 17:19:50 +08:00
{
2019-10-03 10:23:21 +08:00
if ( notification . State = = ProgressNotificationState . Cancelled )
// user requested abort
return ;
2018-04-13 17:19:50 +08:00
2019-10-03 10:23:21 +08:00
notification . Text = $"Restoring ({++i} of {items.Count})" ;
2018-04-13 17:19:50 +08:00
2019-10-03 10:23:21 +08:00
Undelete ( item ) ;
2018-04-13 17:19:50 +08:00
2019-10-03 10:23:21 +08:00
notification . Progress = ( float ) i / items . Count ;
2018-04-13 17:19:50 +08:00
}
notification . State = ProgressNotificationState . Completed ;
}
/// <summary>
/// Restore an item that was previously deleted. Is a no-op if the item is not in a deleted state, or has its protected flag set.
/// </summary>
/// <param name="item">The item to restore</param>
public void Undelete ( TModel item )
{
using ( var usage = ContextFactory . GetForWrite ( ) )
{
usage . Context . ChangeTracker . AutoDetectChangesEnabled = false ;
if ( ! ModelStore . Undelete ( item ) ) return ;
Files . Reference ( item . Files . Select ( f = > f . FileInfo ) . ToArray ( ) ) ;
usage . Context . ChangeTracker . AutoDetectChangesEnabled = true ;
}
}
2021-06-27 19:36:01 +08:00
private string computeHashFast ( ArchiveReader reader )
{
MemoryStream hashable = new MemoryStream ( ) ;
2021-10-27 12:04:41 +08:00
foreach ( string file in reader . Filenames . Where ( f = > HashableFileTypes . Any ( ext = > f . EndsWith ( ext , StringComparison . OrdinalIgnoreCase ) ) ) . OrderBy ( f = > f ) )
2021-06-27 19:36:01 +08:00
{
using ( Stream s = reader . GetStream ( file ) )
s . CopyTo ( hashable ) ;
}
2021-10-21 12:35:26 +08:00
if ( hashable . Length > 0 )
return hashable . ComputeSHA2Hash ( ) ;
2021-06-27 19:36:01 +08:00
2021-10-21 12:35:26 +08:00
return generateFallbackHash ( ) ;
2021-06-27 19:36:01 +08:00
}
2018-04-13 17:19:50 +08:00
/// <summary>
2021-03-08 11:57:16 +08:00
/// Create all required <see cref="IO.FileInfo"/>s for the provided archive, adding them to the global file store.
2018-04-13 17:19:50 +08:00
/// </summary>
private List < TFileModel > createFileInfos ( ArchiveReader reader , FileStore files )
{
var fileInfos = new List < TFileModel > ( ) ;
// import files to manager
2021-06-27 15:34:40 +08:00
foreach ( var filenames in getShortenedFilenames ( reader ) )
2019-11-11 19:53:22 +08:00
{
2021-06-27 15:34:40 +08:00
using ( Stream s = reader . GetStream ( filenames . original ) )
2019-11-11 19:53:22 +08:00
{
2018-04-13 17:19:50 +08:00
fileInfos . Add ( new TFileModel
{
2021-06-27 15:34:40 +08:00
Filename = filenames . shortened ,
2021-03-21 18:01:06 +08:00
FileInfo = files . Add ( s )
2018-04-13 17:19:50 +08:00
} ) ;
2019-11-11 19:53:22 +08:00
}
}
2018-04-13 17:19:50 +08:00
return fileInfos ;
}
2021-06-27 15:34:40 +08:00
private IEnumerable < ( string original , string shortened ) > getShortenedFilenames ( ArchiveReader reader )
{
string prefix = reader . Filenames . GetCommonPrefix ( ) ;
if ( ! ( prefix . EndsWith ( '/' ) | | prefix . EndsWith ( '\\' ) ) )
prefix = string . Empty ;
// import files to manager
foreach ( string file in reader . Filenames )
yield return ( file , file . Substring ( prefix . Length ) . ToStandardisedPath ( ) ) ;
}
2018-08-31 17:28:53 +08:00
#region osu - stable import
2019-06-27 20:41:11 +08:00
/// <summary>
2019-07-05 12:49:54 +08:00
/// Whether this specified path should be removed after successful import.
2019-06-27 20:41:11 +08:00
/// </summary>
2019-07-05 12:49:54 +08:00
/// <param name="path">The path for consideration. May be a file or a directory.</param>
/// <returns>Whether to perform deletion.</returns>
protected virtual bool ShouldDeleteArchive ( string path ) = > false ;
2019-06-27 20:41:11 +08:00
2018-08-31 17:28:53 +08:00
#endregion
2018-04-13 17:19:50 +08:00
/// <summary>
/// Create a barebones model from the provided archive.
/// Actual expensive population should be done in <see cref="Populate"/>; this should just prepare for duplicate checking.
/// </summary>
/// <param name="archive">The archive to create the model for.</param>
2018-08-25 13:51:42 +08:00
/// <returns>A model populated with minimal information. Returning a null will abort importing silently.</returns>
2018-04-13 17:19:50 +08:00
protected abstract TModel CreateModel ( ArchiveReader archive ) ;
/// <summary>
/// Populate the provided model completely from the given archive.
/// After this method, the model should be in a state ready to commit to a store.
/// </summary>
/// <param name="model">The model to populate.</param>
2018-07-18 11:58:28 +08:00
/// <param name="archive">The archive to use as a reference for population. May be null.</param>
2019-05-28 17:59:21 +08:00
/// <param name="cancellationToken">An optional cancellation token.</param>
2021-06-27 12:06:20 +08:00
protected abstract Task Populate ( TModel model , [ CanBeNull ] ArchiveReader archive , CancellationToken cancellationToken = default ) ;
2018-04-13 17:19:50 +08:00
2019-03-11 16:03:01 +08:00
/// <summary>
/// Perform any final actions before the import to database executes.
/// </summary>
/// <param name="model">The model prepared for import.</param>
protected virtual void PreImport ( TModel model )
{
}
2018-11-28 18:01:22 +08:00
/// <summary>
/// Check whether an existing model already exists for a new import item.
/// </summary>
2019-04-25 16:36:17 +08:00
/// <param name="model">The new model proposed for import.</param>
2018-11-28 18:01:22 +08:00
/// <returns>An existing model which matches the criteria to skip importing, else null.</returns>
2019-03-11 16:03:01 +08:00
protected TModel CheckForExisting ( TModel model ) = > model . Hash = = null ? null : ModelStore . ConsumableItems . FirstOrDefault ( b = > b . Hash = = model . Hash ) ;
2021-09-30 17:21:16 +08:00
public bool IsAvailableLocally ( TModel model ) = > CheckLocalAvailability ( model , ModelStore . ConsumableItems . Where ( m = > ! m . DeletePending ) ) ;
/// <summary>
/// Performs implementation specific comparisons to determine whether a given model is present in the local store.
/// </summary>
/// <param name="model">The <typeparamref name="TModel"/> whose existence needs to be checked.</param>
/// <param name="items">The usable items present in the store.</param>
/// <returns>Whether the <typeparamref name="TModel"/> exists.</returns>
protected virtual bool CheckLocalAvailability ( TModel model , IQueryable < TModel > items )
= > model . ID > 0 & & items . Any ( i = > i . ID = = model . ID & & i . Files . Any ( ) ) ;
2021-06-28 09:11:27 +08:00
/// <summary>
2021-08-23 19:23:46 +08:00
/// Whether import can be skipped after finding an existing import early in the process.
2021-06-28 09:11:27 +08:00
/// Only valid when <see cref="ComputeHash"/> is not overridden.
/// </summary>
/// <param name="existing">The existing model.</param>
/// <param name="import">The newly imported model.</param>
/// <returns>Whether to skip this import completely.</returns>
protected virtual bool CanSkipImport ( TModel existing , TModel import ) = > true ;
2019-03-11 16:03:01 +08:00
/// <summary>
2020-06-03 17:03:10 +08:00
/// After an existing <typeparamref name="TModel"/> is found during an import process, the default behaviour is to use/restore the existing
2019-03-11 16:03:01 +08:00
/// item and skip the import. This method allows changing that behaviour.
/// </summary>
/// <param name="existing">The existing model.</param>
/// <param name="import">The newly imported model.</param>
2019-09-20 18:39:21 +08:00
/// <returns>Whether the existing model should be restored and used. Returning false will delete the existing and force a re-import.</returns>
2021-06-28 08:54:18 +08:00
protected virtual bool CanReuseExisting ( TModel existing , TModel import ) = >
2020-06-03 21:35:01 +08:00
// 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 ) ) ;
2020-06-03 17:03:10 +08:00
2020-06-03 21:35:01 +08:00
private IEnumerable < long > getIDs ( List < TFileModel > files )
2020-06-03 17:03:10 +08:00
{
foreach ( var f in files . OrderBy ( f = > f . Filename ) )
2020-06-03 21:35:01 +08:00
yield return f . FileInfo . ID ;
2020-06-03 17:03:10 +08:00
}
2020-06-03 21:35:01 +08:00
private IEnumerable < string > getFilenames ( List < TFileModel > files )
2020-06-03 17:03:10 +08:00
{
foreach ( var f in files . OrderBy ( f = > f . Filename ) )
2020-06-03 21:35:01 +08:00
yield return f . Filename ;
2020-06-03 17:03:10 +08:00
}
2018-04-13 17:19:50 +08:00
private DbSet < TModel > queryModel ( ) = > ContextFactory . Get ( ) . Set < TModel > ( ) ;
2021-09-30 17:21:16 +08:00
public virtual string HumanisedModelName = > $"{typeof(TModel).Name.Replace(@" Info ", " ").ToLower()}" ;
2019-06-10 17:41:56 +08:00
2019-06-19 00:32:37 +08:00
#region Event handling / delaying
private readonly List < Action > queuedEvents = new List < Action > ( ) ;
/// <summary>
/// Allows delaying of outwards events until an operation is confirmed (at a database level).
/// </summary>
private bool delayingEvents ;
/// <summary>
/// Begin delaying outwards events.
/// </summary>
private void delayEvents ( ) = > delayingEvents = true ;
/// <summary>
/// Flush delayed events and disable delaying.
/// </summary>
/// <param name="perform">Whether the flushed events should be performed.</param>
private void flushEvents ( bool perform )
{
Action [ ] events ;
lock ( queuedEvents )
{
events = queuedEvents . ToArray ( ) ;
queuedEvents . Clear ( ) ;
}
if ( perform )
{
foreach ( var a in events )
a . Invoke ( ) ;
}
delayingEvents = false ;
}
private void handleEvent ( Action a )
{
if ( delayingEvents )
2019-11-11 19:53:22 +08:00
{
2019-06-19 00:32:37 +08:00
lock ( queuedEvents )
queuedEvents . Add ( a ) ;
2019-11-11 19:53:22 +08:00
}
2019-06-19 00:32:37 +08:00
else
a . Invoke ( ) ;
}
#endregion
2020-05-24 21:34:31 +08:00
2021-10-21 12:35:26 +08:00
private static string generateFallbackHash ( )
{
// if a hash could no be generated from file content, presume a unique / new import.
// therefore, let's use a guaranteed unique hash.
// this doesn't follow the SHA2 hashing schema intentionally, so such entries on the data store can be identified.
return Guid . NewGuid ( ) . ToString ( ) ;
}
2018-04-13 17:19:50 +08:00
}
}