| | | 1 | | using Anichron.Core.Domain; |
| | | 2 | | using Anichron.Worker.Ingestion.Classification; |
| | | 3 | | using Anichron.Worker.Ingestion.Extraction; |
| | | 4 | | |
| | | 5 | | namespace Anichron.Worker.Ingestion.Pipeline; |
| | | 6 | | |
| | | 7 | | internal sealed class IngestionContext |
| | | 8 | | { |
| | 533 | 9 | | private readonly List<ProxyFile> proxyFiles = []; |
| | 533 | 10 | | private readonly HashSet<ProxyFile> replacedFiles = []; |
| | 533 | 11 | | private readonly Lock proxyFilesLock = new(); |
| | 533 | 12 | | private readonly HashSet<Guid> stagedMovs = []; |
| | | 13 | | |
| | | 14 | | public required IngestionItem Item { get; init; } |
| | | 15 | | public required UserStorageConfig Config { get; init; } |
| | | 16 | | |
| | | 17 | | public required Guid AttemptId { get; init; } |
| | | 18 | | |
| | | 19 | | public Guid AssetId { get; set; } |
| | | 20 | | |
| | | 21 | | public string? ContentHash { get; set; } |
| | | 22 | | public string? SecondaryHash { get; set; } |
| | | 23 | | public IngestionClassification? Classification { get; set; } |
| | | 24 | | public ExtractedMedia? Extracted { get; set; } |
| | | 25 | | public string? Blurhash { get; set; } |
| | | 26 | | public MediaAsset? Asset { get; set; } |
| | | 27 | | |
| | | 28 | | /// <summary>Rows this item soft-deleted, counted once the save that soft-deleted them succeeded.</summary> |
| | | 29 | | public int SoftDeleted { get; private set; } |
| | | 30 | | |
| | | 31 | | /// <summary>Soft-deleted rows this item made active again, counted once the save that restored them succeeded.</sum |
| | | 32 | | public int AutoRestored { get; private set; } |
| | | 33 | | |
| | | 34 | | /// <summary>Rows soft-deleted on tracked entities, not saved yet.</summary> |
| | | 35 | | public int StagedSoftDeleted { get; private set; } |
| | | 36 | | |
| | | 37 | | /// <summary>Rows made active again on tracked entities, not saved yet.</summary> |
| | | 38 | | public int StagedAutoRestored { get; private set; } |
| | | 39 | | |
| | 53 | 40 | | public void StageSoftDeleted() => StagedSoftDeleted++; |
| | | 41 | | |
| | 26 | 42 | | public void StageAutoRestored() => StagedAutoRestored++; |
| | | 43 | | |
| | | 44 | | /// <summary>Counts the staged changes, once a save has persisted them.</summary> |
| | | 45 | | public void CommitCounts() |
| | 117 | 46 | | { |
| | 117 | 47 | | (SoftDeleted, AutoRestored) = (SoftDeleted + StagedSoftDeleted, AutoRestored + StagedAutoRestored); |
| | 117 | 48 | | (StagedSoftDeleted, StagedAutoRestored) = (0, 0); |
| | 117 | 49 | | } |
| | | 50 | | |
| | | 51 | | // Image proxies register concurrently. |
| | | 52 | | public IReadOnlyList<ProxyFile> ProxyFiles |
| | | 53 | | { |
| | | 54 | | get |
| | 50 | 55 | | { |
| | | 56 | | lock (proxyFilesLock) |
| | 50 | 57 | | return [.. proxyFiles]; |
| | 50 | 58 | | } |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public IReadOnlyList<ProxyFile> ProxyFilesOf(Guid ownerAssetId) |
| | 138 | 62 | | { |
| | | 63 | | lock (proxyFilesLock) |
| | 138 | 64 | | return [.. proxyFiles.Where(proxy => proxy.AssetId == ownerAssetId)]; |
| | 138 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary>The registered proxies whose final file did not exist before this attempt wrote it.</summary> |
| | | 68 | | public IReadOnlyList<ProxyFile> CreatedProxyFiles |
| | | 69 | | { |
| | | 70 | | get |
| | 36 | 71 | | { |
| | | 72 | | lock (proxyFilesLock) |
| | 36 | 73 | | return [.. proxyFiles.Where(proxy => !replacedFiles.Contains(proxy))]; |
| | 36 | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | public void AddProxyFile(ProxyFile proxyFile, bool replacedExistingFile = false) |
| | 253 | 78 | | { |
| | | 79 | | lock (proxyFilesLock) |
| | 253 | 80 | | { |
| | 253 | 81 | | proxyFiles.Add(proxyFile); |
| | 253 | 82 | | if (replacedExistingFile) |
| | 2 | 83 | | replacedFiles.Add(proxyFile); |
| | 253 | 84 | | } |
| | 253 | 85 | | } |
| | | 86 | | |
| | | 87 | | public void ReleaseProxyFiles(Guid ownerAssetId) |
| | 34 | 88 | | { |
| | | 89 | | lock (proxyFilesLock) |
| | 34 | 90 | | proxyFiles.RemoveAll(proxy => proxy.AssetId == ownerAssetId); |
| | 34 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary>Notes a video whose registered proxies the item's save hands to the database.</summary> |
| | | 94 | | /// <param name="movId">The video that owns the proxies.</param> |
| | 48 | 95 | | public void StageMovProxies(Guid movId) => stagedMovs.Add(movId); |
| | | 96 | | |
| | | 97 | | /// <summary>Releases the proxies of every staged video, so compensation leaves them in place.</summary> |
| | | 98 | | public void ReleaseStagedMovProxies() |
| | 117 | 99 | | { |
| | 417 | 100 | | foreach (var movId in stagedMovs) |
| | 33 | 101 | | ReleaseProxyFiles(movId); |
| | 117 | 102 | | } |
| | | 103 | | } |