< Summary

Information
Class: Anichron.Worker.Ingestion.IngestionSaver
Assembly: Anichron.Worker
File(s): /home/runner/work/anichron/anichron/src/Anichron.Worker/Ingestion/IngestionSaver.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 74
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
TrySaveItemAsync()100%66100%
SaveAsync()100%22100%
SaveRetiringFirstAsync(...)100%11100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Worker/Ingestion/IngestionSaver.cs

#LineLine coverage
 1using Anichron.Core.Data;
 2using Anichron.Core.Data.Repository;
 3using Anichron.Core.Domain;
 4using Anichron.Worker.Ingestion.Classification;
 5using Anichron.Worker.Ingestion.Pipeline;
 6using Microsoft.EntityFrameworkCore;
 7
 8namespace Anichron.Worker.Ingestion;
 9
 14710internal sealed class IngestionSaver(IUnitOfWork unitOfWork, IMediaAssetRepository repository)
 11{
 12    /// <summary>
 13    /// Saves everything the item staged, in its one save: when the classification has
 14    /// <see cref="IngestionClassification.RetiredFirst"/> rows, they are soft-deleted first, in one transaction. After 
 15    /// commit, and on a lost race, the proxies of every staged video are released.
 16    /// </summary>
 17    /// <param name="context">The classified item whose staged changes and counts the save commits.</param>
 18    /// <param name="processed">The row with newly processed content; null when the item only reconciled stored rows.</p
 19    /// <param name="ct">Cancels the save.</param>
 20    /// <returns>
 21    /// False when another consumer took the content first, for a processed row whose content another row committed or
 22    /// for an item that retires rows first; nothing is saved then.
 23    /// </returns>
 24    public async Task<bool> TrySaveItemAsync(IngestionContext context, MediaAsset? processed, CancellationToken ct)
 12625    {
 12626        var retiredFirst = context.Classification!.RetiredFirst;
 27        try
 12628        {
 12629            await SaveAsync(context, retiredFirst, ct);
 9530            return true;
 31        }
 3132        catch (DbUpdateException ex) when (LostIngestionRace.Matches(ex))
 2233        {
 2234            if (processed is not null)
 1135            {
 1136                if (await repository.FindByHashAsync(processed.ContentHash, processed.StorageConfigId, ct) is null)
 437                    throw;
 738                return false;
 39            }
 40
 1141            if (retiredFirst.Count == 0)
 742                throw;
 443            return false;
 44        }
 10645    }
 46
 47    private async Task SaveAsync(IngestionContext context, IReadOnlyList<MediaAsset> retiredFirst, CancellationToken ct)
 12648    {
 49        try
 12650        {
 12651            if (retiredFirst.Count == 0)
 10952                await unitOfWork.SaveChangesAsync(ct);
 53            else
 1754                await SaveRetiringFirstAsync(retiredFirst, ct);
 9555        }
 3156        catch (DbUpdateException ex) when (LostIngestionRace.Matches(ex))
 2257        {
 2258            context.ReleaseStagedMovProxies();
 2259            throw;
 60        }
 61
 9562        context.CommitCounts();
 9563        context.ReleaseStagedMovProxies();
 9564    }
 65
 66    private Task SaveRetiringFirstAsync(IReadOnlyList<MediaAsset> retired, CancellationToken ct)
 1767        => unitOfWork.ExecuteInTransactionAsync(
 1768            async () =>
 1769            {
 1770                await repository.SoftDeleteAsync([.. retired.Select(row => row.Id)], ct);
 1771                await unitOfWork.SaveChangesAsync(ct);
 1772            },
 1773            ct);
 74}