Adds the media library scan snapshot, staging and directory fingerprint data model

This commit is contained in:
BinaryAlley committed 2026-08-09 13:17:34 -07:00
1 parent 4fec67a1f8
commit 1dc3da3da1
37 files changed
+7315 -89

No files matched your search

@@ -0,0 +1,34 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Common;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
/// <summary>
/// Repository entity for a directory scan fingerprint, holding the last write time of a directory of a media library, used to skip the scanning of directories that have not changed since the last scan.
/// </summary>
[DebuggerDisplay("Id: {Id}; Path: {Path}")]
public class DirectoryScanFingerprintEntity : IStorageEntity
{
/// <summary>
/// Gets the Id of the directory scan fingerprint.
/// </summary>
public required Guid Id { get; init; }
/// <summary>
/// Gets the Id of the media library to which this directory scan fingerprint belongs.
/// </summary>
public required Guid LibraryId { get; init; }
/// <summary>
/// Gets the path of the directory to which this directory scan fingerprint belongs.
/// </summary>
public required string Path { get; init; }
/// <summary>
/// Gets the last write time of the directory, in UTC, at the moment when the directory was scanned.
/// </summary>
public required DateTime LastWriteTimeUtc { get; init; }
}
@@ -23,6 +23,11 @@ public class LibraryScanResultEntity : IStorageEntity
/// </summary>
public required Guid LibraryScanId { get; init; }
/// <summary>
/// Gets the status of the media library scan file.
/// </summary>
public required LibraryScanFileStatus Status { get; init; }
/// <summary>
/// Gets the path of the media library scan file.
/// </summary>
@@ -39,9 +44,9 @@ public class LibraryScanResultEntity : IStorageEntity
public required long FileSize { get; init; }
/// <summary>
/// Gets the modification date of the media library scan file.
/// Gets the time and date when the file system file was last modified, stored in ticks.
/// </summary>
public required DateTime LastModified { get; init; }
public required long Ticks { get; init; }
/// <summary>
/// Gets the media library scan that this scan result belongs to.
@@ -0,0 +1,69 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Common;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
/// <summary>
/// Repository entity for a media library item, representing the current state of a previously scanned media item, as a snapshot.
/// </summary>
[DebuggerDisplay("Id: {Id}; Path: {Path}")]
public class LibraryScanSnapshotEntity : IStorageEntity, IAuditableEntity
{
/// <summary>
/// Gets the Id of the media library scan snapshot item.
/// </summary>
public required Guid Id { get; init; }
/// <summary>
/// Gets the Id of the media library to which this media library scan snapshot item belongs.
/// </summary>
public required Guid LibraryId { get; init; }
/// <summary>
/// Gets the path of the media library scan snapshot item.
/// </summary>
public required string Path { get; init; }
/// <summary>
/// Gets the hash calculated for the media library scan snapshot item.
/// </summary>
public required ulong ContentHash { get; init; }
/// <summary>
/// Gets the size of the media library scan snapshot item, in bytes.
/// </summary>
public required long FileSize { get; init; }
/// <summary>
/// Gets the time and date when the media library scan snapshot item was last modified, stored in ticks.
/// </summary>
public required long Ticks { get; init; }
/// <summary>
/// Gets the media library to which this media library scan snapshot item belongs.
/// </summary>
public required LibraryEntity Library { get; init; } = null!;
/// <summary>
/// Gets or sets the time and date when the entity was added.
/// </summary>
public required DateTime CreatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the Id of the user that created the entity.
/// </summary>
public required Guid CreatedBy { get; set; }
/// <summary>
/// Gets or sets the optional time and date when the entity was updated.
/// </summary>
public DateTime? UpdatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the optional Id of the user that updated the entity.
/// </summary>
public required Guid? UpdatedBy { get; set; }
}
@@ -0,0 +1,59 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Common;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
/// <summary>
/// Repository entity for a media library scan staging result, holding the file system items discovered during a scan, before they are compared against the media library scan snapshot of the previous scan.
/// </summary>
[DebuggerDisplay("Id: {Id}; Path: {Path}")]
public class LibraryScanStagingResultsEntity : IStorageEntity
{
/// <summary>
/// Gets the Id of the media library scan staging result.
/// </summary>
public required Guid Id { get; init; }
/// <summary>
/// Gets the Id of the media library scan that this staging result belongs to.
/// </summary>
public required Guid LibraryScanId { get; init; }
/// <summary>
/// Gets the path of the media library scan staging result.
/// </summary>
public required string Path { get; init; }
/// <summary>
/// Gets the size of the media library scan staging result, in bytes.
/// </summary>
public required long Size { get; init; }
/// <summary>
/// Gets the time and date when the file system item was last modified, stored in ticks.
/// </summary>
public required long Ticks { get; init; }
/// <summary>
/// Gets the hash calculated for the media library scan staging result.
/// </summary>
public required ulong ContentHash { get; init; }
/// <summary>
/// Gets the hash of the same file system item, as stored in the media library scan snapshot of a previous scan, or 0 if the file system item is new.
/// </summary>
public required ulong PreviousContentHash { get; init; }
/// <summary>
/// Gets whether this media library scan staging result represents a file system item that needs its content to be hashed, because it is either new, or it has changed since the previous scan.
/// </summary>
public required bool NeedsRehash { get; init; }
/// <summary>
/// Gets whether this media library scan staging result represents a file system item that was not present in the media library scan snapshot of a previous scan.
/// </summary>
public required bool IsNew { get; init; }
}
@@ -0,0 +1,24 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Common;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
/// <summary>
/// Interface defining the "insert range" action for interacting with a generic persistance medium.
/// </summary>
/// <typeparam name="TModel">The type used for the insert range action. It should implement <see cref="IStorageEntity"/>.</typeparam>
public interface IInsertRangeRepositoryAction<TModel> where TModel : IStorageEntity
{
/// <summary>
/// Saves a collection of elements of type <typeparamref name="TModel"/> in the storage medium.
/// </summary>
/// <param name="entities">The elements to be saved.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.</returns>
Task<ErrorOr<Created>> InsertRangeAsync(IReadOnlyCollection<TModel> entities, CancellationToken cancellationToken);
}
@@ -0,0 +1,33 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
/// <summary>
/// Interface for the repository for directory scan fingerprints.
/// </summary>
public interface IDirectoryScanFingerprintRepository : IRepository<DirectoryScanFingerprintEntity>
{
/// <summary>
/// Gets the directory scan fingerprints of a media library, mapped by the directory path.
/// </summary>
/// <param name="libraryId">The unique identifier of the library for which to get the directory scan fingerprints.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a path mapped dictionary of directory scan fingerprints, or an error.</returns>
Task<ErrorOr<Dictionary<string, DirectoryScanFingerprintEntity>>> GetMappedByLibraryIdAsync(Guid libraryId, CancellationToken cancellationToken);
/// <summary>
/// Inserts or updates the provided directory scan fingerprints in the storage medium.
/// </summary>
/// <param name="entities">The directory scan fingerprints to insert or update.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.</returns>
Task<ErrorOr<Updated>> UpsertRangeAsync(IReadOnlyCollection<DirectoryScanFingerprintEntity> entities, CancellationToken cancellationToken);
}
@@ -1,12 +1,7 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
@@ -17,11 +12,4 @@ namespace Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
public interface ILibraryScanResultRepository : IRepository<LibraryScanResultEntity>,
IInsertRepositoryAction<LibraryScanResultEntity>
{
/// <summary>
/// Gets all media library scan results that belong to the last scan of a library identified by <paramref name="libraryId"/>, from the storage medium.
/// </summary>
/// <param name="libraryId">The unique identifier of the library for which to get the results of the last scans.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a path mapped dictionary of <see cref="LibraryScanResultEntity"/>, or an error.</returns>
Task<ErrorOr<Dictionary<string, LibraryScanResultEntity>>> GetPathMappedByLibraryIdAsync(Guid libraryId, CancellationToken cancellationToken);
}
@@ -0,0 +1,38 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
/// <summary>
/// Interface for the repository for media library scan snapshots.
/// </summary>
public interface ILibraryScanSnapshotRepository : IRepository<LibraryScanSnapshotEntity>
{
/// <summary>
/// Gets the paths of the media library scan snapshot items that are no longer present in the current scan, meaning they were deleted from the storage medium.
/// </summary>
/// <param name="libraryId">The unique identifier of the library for which to get the deleted media library scan snapshot item paths.</param>
/// <param name="scanId">The unique identifier of the media library scan for which to determine the deleted media library scan snapshot item paths.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a collection of deleted media library scan snapshot item paths, or an error.</returns>
Task<ErrorOr<IReadOnlyList<string>>> GetDeletedPathsAsync(Guid libraryId, Guid scanId, CancellationToken cancellationToken);
/// <summary>
/// Atomically applies the results of the current scan to the storage medium, by replacing the media library scan snapshot of the previous scan with the snapshot of the current scan.
/// The operation updates the changed media library scan snapshot items, deletes the ones that are no longer present on disk, adds audit entries for all changed, new and deleted items,
/// and clears the staging results of the current scan.
/// </summary>
/// <param name="libraryId">The unique identifier of the library for which to apply the scan results.</param>
/// <param name="scanId">The unique identifier of the media library scan whose results are applied.</param>
/// <param name="userId">The unique identifier of the user that initiated the media library scan, used for audit purposes.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.</returns>
Task<ErrorOr<Updated>> ApplySnapshotSwapAsync(Guid libraryId, Guid scanId, Guid userId, CancellationToken cancellationToken);
}
@@ -0,0 +1,65 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using Lumina.Application.Common.Infrastructure.Models.MediaLibraryScanJobPayloads;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
/// <summary>
/// Interface for the repository for media library scan staging results.
/// </summary>
public interface ILibraryScanStagingResultsRepository : IRepository<LibraryScanStagingResultsEntity>,
IInsertRangeRepositoryAction<LibraryScanStagingResultsEntity>
{
/// <summary>
/// Marks the media library scan staging results of the current scan by comparing them against the media library scan snapshot of a previous scan, determining which file system items
/// are new, which ones changed and need their content hashed, and which ones are unchanged.
/// </summary>
/// <param name="scanId">The unique identifier of the media library scan whose staging results are marked.</param>
/// <param name="libraryId">The unique identifier of the library whose media library scan snapshot is compared against.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.</returns>
Task<ErrorOr<Updated>> MarkChangesAgainstSnapshotAsync(Guid scanId, Guid libraryId, CancellationToken cancellationToken);
/// <summary>
/// Gets the number of media library scan staging results of the current scan that need their content hashed.
/// </summary>
/// <param name="scanId">The unique identifier of the media library scan whose staging results are counted.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either the number of staging results that need hashing, or an error.</returns>
Task<ErrorOr<int>> GetFilesToHashCountAsync(Guid scanId, CancellationToken cancellationToken);
/// <summary>
/// Gets a page of the media library scan staging results that need their content hashed, ordered by path, using keyset pagination.
/// </summary>
/// <param name="scanId">The unique identifier of the media library scan whose staging results are retrieved.</param>
/// <param name="lastPath">The path of the last retrieved file system item, used for keyset pagination. Pass <see langword="null"/> to get the first page.</param>
/// <param name="pageSize">The maximum number of staging results to retrieve.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a page of staging results that need hashing, or an error.</returns>
Task<ErrorOr<IReadOnlyList<HashedFileSystemFile>>> GetFilesToHashPageAsync(Guid scanId, string? lastPath, int pageSize, CancellationToken cancellationToken);
/// <summary>
/// Updates the content hashes of the provided media library scan staging results.
/// </summary>
/// <param name="scanId">The unique identifier of the media library scan whose staging results are updated.</param>
/// <param name="hashedFiles">The file system items whose content hashes are updated.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.</returns>
Task<ErrorOr<Updated>> UpdateFileHashesAsync(Guid scanId, IReadOnlyCollection<HashedFileSystemFile> hashedFiles, CancellationToken cancellationToken);
/// <summary>
/// Clears all the media library scan staging results of the provided media library scan from the storage medium.
/// </summary>
/// <param name="scanId">The unique identifier of the media library scan whose staging results are cleared.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.</returns>
Task<ErrorOr<Success>> ClearForScanAsync(Guid scanId, CancellationToken cancellationToken);
}
@@ -1,14 +0,0 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics;
using System.IO;
#endregion
namespace Lumina.Application.Common.Infrastructure.Models.MediaLibraryScanJobPayloads;
/// <summary>
/// Represents an item for the payload of the file hashing media library scan job.
/// </summary>
/// <param name="File">The file to whom <paramref name="FileHash"/> belongs.</param>
/// <param name="FileHash">The hash of <paramref name="File"/>.</param>
[DebuggerDisplay("File: {File}; FileHash: {FileHash}")]
public record ChangedFileSystemFile(FileInfo File, ulong FileHash);
@@ -0,0 +1,70 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
#endregion
namespace Lumina.Application.Common.Infrastructure.Models.MediaLibraryScanJobPayloads;
/// <summary>
/// Represents an item for the payload of the file hashing media library scan job.
/// </summary>
[DebuggerDisplay("Path = {Path}, CurrentHash = {CurrentHash}, OldHash = {OldHash}, Ticks = {Ticks}, Size = {Size}")]
[StructLayout(LayoutKind.Sequential)]
public struct HashedFileSystemFile : IEquatable<HashedFileSystemFile>
{
/// <summary>
/// The path of the file system file.
/// </summary>
public string Path { get; set; }
/// <summary>
/// The size of the file system file.
/// </summary>
public long Size { get; set; }
/// <summary>
/// The current hash obtained by sampling the file system file contents.
/// </summary>
public ulong CurrentHash { get; set; }
/// <summary>
/// The old hash of the file system file, stored at the previous scan.
/// </summary>
public ulong OldHash { get; set; }
/// <summary>
/// The time and date when the file system file was last modified, stored in ticks.
/// </summary>
public long Ticks { get; set; }
/// <inheritdoc/>
public readonly bool Equals(HashedFileSystemFile other)
{
return Size == other.Size && CurrentHash == other.CurrentHash && OldHash == other.OldHash && Ticks == other.Ticks && Path == other.Path;
}
/// <inheritdoc/>
public override readonly bool Equals(object? obj)
{
return obj is HashedFileSystemFile item && Equals(item);
}
/// <inheritdoc/>
public static bool operator ==(HashedFileSystemFile left, HashedFileSystemFile right)
{
return left.Equals(right);
}
/// <inheritdoc/>
public static bool operator !=(HashedFileSystemFile left, HashedFileSystemFile right)
{
return !(left == right);
}
/// <inheritdoc/>
public override readonly int GetHashCode()
{
return HashCode.Combine(Size, CurrentHash, OldHash, Ticks, Path);
}
}