Fixes large library scans hitting the SQLite parameter limit

Bounds path lookups and enrichment resets to 20,000 parameters per query, restores failed scans from their staged metadata, and clears staged data only after the materialized items are saved.
This commit is contained in:
BinaryAlley committed 2026-10-29 20:23:24 +02:00
1 parent 800f4ab334
commit 6ef9592f00
22 files changed
+542 -94

No files matched your search

@@ -30,7 +30,7 @@ public interface ILibraryScanSnapshotRepository : IRepository<LibraryScanSnapsho
/// <param name="libraryId">The unique identifier of the library whose media library scan snapshot item paths are retrieved.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of media library scan snapshot item paths, or an error.</returns>
Task<Result<IReadOnlyList<string>>> GetPathsAsync(Guid libraryId, CancellationToken cancellationToken);
Task<Result<IReadOnlySet<string>>> GetPathsAsync(Guid libraryId, 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.
@@ -43,4 +43,14 @@ public interface ILibraryScanSnapshotRepository : IRepository<LibraryScanSnapsho
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
Task<Result<Updated>> ApplySnapshotSwapAsync(Guid libraryId, Guid scanId, Guid userId, CancellationToken cancellationToken);
/// <summary>
/// Removes the media library scan snapshot entries that the scan identified by <paramref name="scanId"/> added or modified, restoring the snapshot to
/// the state it had before the scan was applied.
/// </summary>
/// <param name="libraryId">The unique identifier of the library whose scan snapshot is rolled back.</param>
/// <param name="scanId">The unique identifier of the media library scan whose snapshot entries are removed.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
Task<Result<Deleted>> RollbackSnapshotForScanAsync(Guid libraryId, Guid scanId, CancellationToken cancellationToken);
}
@@ -25,6 +25,14 @@ public interface IMusicLibraryScanItemMetadataRepository : IRepository<MusicLibr
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="MusicLibraryScanItemMetadataEntity"/>, or an error.</returns>
Task<Result<IReadOnlyList<MusicLibraryScanItemMetadataEntity>>> GetByScanIdAsync(Guid scanId, CancellationToken cancellationToken);
/// <summary>
/// Gets all the staged music metadata items of the media library identified by <paramref name="libraryId"/>, regardless of the scan that staged them.
/// </summary>
/// <param name="libraryId">The Id of the media library whose staged music metadata is retrieved.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="MusicLibraryScanItemMetadataEntity"/>, or an error.</returns>
Task<Result<IReadOnlyList<MusicLibraryScanItemMetadataEntity>>> GetByLibraryIdAsync(Guid libraryId, CancellationToken cancellationToken);
/// <summary>
/// Deletes all the staged music metadata items of the media library scan identified by <paramref name="scanId"/>.
/// </summary>
@@ -77,7 +77,14 @@ public class LibraryScanCancelledDomainEventHandler : IDomainEventHandler<Librar
MediaLibraryScanCompositeId compositeId = MediaLibraryScanCompositeId.Create(domainEvent.ScanId, UserId.Create(getLibraryScansResult.Value.UserId));
_mediaLibrariesScanCancellationTracker.RemoveScan(compositeId);
_mediaLibrariesScanProgressTracker.RemoveScanProgress(compositeId);
Result<Success> clearStagingResult = await _unitOfWork.LibraryScanStagingResultsRepository.ClearForScanAsync(domainEvent.ScanId.Value, cancellationToken).ConfigureAwait(false);
// Cancelling the scan cancels its own token, so the cleanup must not observe it, otherwise the staged results of the cancelled scan would survive the cancellation.
// A scan that was cancelled after its snapshot was applied has not materialized its items yet, so its snapshot entries are rolled back, allowing the next
// scan to treat the files as new and extract their metadata again instead of skipping them as unchanged.
Result<Deleted> rollbackSnapshotResult = await _unitOfWork.LibraryScanSnapshotRepository.RollbackSnapshotForScanAsync(
domainEvent.LibraryId.Value, domainEvent.ScanId.Value, CancellationToken.None).ConfigureAwait(false);
if (rollbackSnapshotResult.IsFailure)
throw new EventualConsistencyException(rollbackSnapshotResult.FirstError, rollbackSnapshotResult.Errors);
Result<Success> clearStagingResult = await _unitOfWork.LibraryScanStagingResultsRepository.ClearForScanAsync(domainEvent.ScanId.Value, CancellationToken.None).ConfigureAwait(false);
if (clearStagingResult.IsFailure)
throw new EventualConsistencyException(clearStagingResult.FirstError, clearStagingResult.Errors);
}
@@ -86,11 +86,19 @@ public class LibraryScanFailedDomainEventHandler : IDomainEventHandler<LibrarySc
// Release the scan processing resources, regardless of whether the scan was already marked as failed by a concurrent job.
_mediaLibrariesScanCancellationTracker.RemoveScan(domainEvent.MediaLibraryScanCompositeId);
_mediaLibrariesScanProgressTracker.RemoveScanProgress(domainEvent.MediaLibraryScanCompositeId);
Result<Success> clearStagingResult = await _unitOfWork.LibraryScanStagingResultsRepository.ClearForScanAsync(domainEvent.MediaLibraryScanCompositeId.ScanId.Value, cancellationToken).ConfigureAwait(false);
// Releasing the resources cancels the scan's own token, so the cleanup that follows must not observe it, otherwise the staged results of a failed scan
// would survive the failure and leak into the storage medium.
// A scan that failed after its snapshot was applied has not materialized its items yet, so its snapshot entries are rolled back, allowing the next
// scan to treat the files as new and extract their metadata again instead of skipping them as unchanged.
Result<Deleted> rollbackSnapshotResult = await _unitOfWork.LibraryScanSnapshotRepository.RollbackSnapshotForScanAsync(
domainEvent.LibraryId.Value, domainEvent.MediaLibraryScanCompositeId.ScanId.Value, CancellationToken.None).ConfigureAwait(false);
if (rollbackSnapshotResult.IsFailure)
throw new EventualConsistencyException(rollbackSnapshotResult.FirstError, rollbackSnapshotResult.Errors);
Result<Success> clearStagingResult = await _unitOfWork.LibraryScanStagingResultsRepository.ClearForScanAsync(domainEvent.MediaLibraryScanCompositeId.ScanId.Value, CancellationToken.None).ConfigureAwait(false);
if (clearStagingResult.IsFailure)
throw new EventualConsistencyException(clearStagingResult.FirstError, clearStagingResult.Errors);
// Notify SignalR clients that the library scan failed.
await _debouncedLibraryScanProgressNotifier.SendLibraryScanFailedEventAsync(domainEvent.MediaLibraryScanCompositeId, cancellationToken).ConfigureAwait(false);
await _debouncedLibraryScanProgressNotifier.SendLibraryScanFailedEventAsync(domainEvent.MediaLibraryScanCompositeId, CancellationToken.None).ConfigureAwait(false);
}
}