< Summary

Information
Class: Anichron.Worker.ProxySweeps.EfProxyOrphanStore
Assembly: Anichron.Worker
File(s): /home/runner/work/anichron/anichron/src/Anichron.Worker/ProxySweeps/ProxyOrphanStore.cs
Tag: 19_45211875487
Line coverage
65%
Covered lines: 30
Uncovered lines: 16
Coverable lines: 46
Total lines: 112
Line coverage: 65.2%
Branch coverage
80%
Covered branches: 4
Total branches: 5
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ReferencedUnderAsync()100%11100%
IsReferencedAsync(...)100%11100%
RecordAsync()0%210%
SummariseAsync()100%11100%
GetPendingAsync()100%44100%
ForgetLoaded()100%11100%
ReferencedUnderQuery(...)100%11100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Worker/ProxySweeps/ProxyOrphanStore.cs

#LineLine coverage
 1using Anichron.Core;
 2using Anichron.Core.Data;
 3using Anichron.Core.Data.Repository;
 4using Anichron.Core.Domain;
 5using Anichron.Core.Work;
 6using Anichron.Worker.Ingestion.Proxy;
 7using Microsoft.EntityFrameworkCore;
 8using NodaTime;
 9
 10namespace Anichron.Worker.ProxySweeps;
 11
 12internal sealed record FoundOrphan(string ProxyPath, Guid? StorageConfigId, long SizeBytes, Instant LastWriteTime);
 13
 14internal interface IProxyOrphanStore
 15{
 16    /// <summary>Every <see cref="ProxyFile.ProxyPath"/> under a top-level shard folder, soft-deleted assets' included.<
 17    /// <param name="shard">The shard folder's name.</param>
 18    /// <param name="ct">Cancels the read.</param>
 19    Task<IReadOnlySet<string>> ReferencedUnderAsync(string shard, CancellationToken ct);
 20
 21    /// <summary>Whether any <see cref="ProxyFile"/> row, a soft-deleted asset's included, references the path.</summary
 22    /// <param name="proxyPath">A path relative to the proxy root.</param>
 23    /// <param name="ct">Cancels the read.</param>
 24    Task<bool> IsReferencedAsync(string proxyPath, CancellationToken ct);
 25
 26    /// <summary>Adds the orphans to the sweep's report, skipping paths it already holds.</summary>
 27    /// <param name="sweepId">The sweep's work item.</param>
 28    /// <param name="orphans">The orphans found.</param>
 29    /// <param name="ct">Cancels the insert.</param>
 30    Task RecordAsync(Guid sweepId, IReadOnlyList<FoundOrphan> orphans, CancellationToken ct);
 31
 32    /// <summary>What the sweep's report holds, rows an earlier attempt of it recorded included.</summary>
 33    /// <param name="sweepId">The sweep's work item.</param>
 34    /// <param name="ct">Cancels the read.</param>
 35    Task<ProxySweepResult> SummariseAsync(Guid sweepId, CancellationToken ct);
 36
 37    /// <summary>
 38    /// The first unprocessed orphans in the deletion's scope, by id: of its config, or, unscoped, of no
 39    /// existing config.
 40    /// </summary>
 41    /// <param name="deletion">The sweep, the scope, and the optional selection.</param>
 42    /// <param name="take">How many to read at most.</param>
 43    /// <param name="ct">Cancels the read.</param>
 44    Task<IReadOnlyList<ProxyOrphan>> GetPendingAsync(ProxyDeletionPayload deletion, int take, CancellationToken ct);
 45
 46    /// <summary>Stops tracking the orphans read so far.</summary>
 47    void ForgetLoaded();
 48}
 49
 1550internal sealed class EfProxyOrphanStore(AnichronDbContext db, IGuidFactory guids) : IProxyOrphanStore
 51{
 52    public async Task<IReadOnlySet<string>> ReferencedUnderAsync(string shard, CancellationToken ct)
 253        => (await ReferencedUnderQuery(db, shard).ToListAsync(ct)).ToHashSet(StringComparer.Ordinal);
 54
 55    public Task<bool> IsReferencedAsync(string proxyPath, CancellationToken ct)
 356        => db.ProxyFiles.IgnoreQueryFilters().AnyAsync(p => p.ProxyPath == proxyPath, ct);
 57
 58    // Stryker disable all
 59    public async Task RecordAsync(Guid sweepId, IReadOnlyList<FoundOrphan> orphans, CancellationToken ct)
 060    {
 061        if (orphans.Count == 0)
 062            return;
 63
 64        var ids = orphans.Select(_ => guids.NewGuid()).ToArray();
 065        var paths = orphans.Select(o => o.ProxyPath).ToArray();
 066        var configs = orphans.Select(o => o.StorageConfigId).ToArray();
 067        var sizes = orphans.Select(o => o.SizeBytes).ToArray();
 068        var times = orphans.Select(o => o.LastWriteTime).ToArray();
 069        await db.Database.ExecuteSqlInterpolatedAsync(
 070            $"""
 071            INSERT INTO "ProxyOrphans" ("Id", "SweepId", "ProxyPath", "StorageConfigId", "SizeBytes", "LastWriteTime")
 072            SELECT o.id, {sweepId}, o.path, o.config, o.size, o.time
 073            FROM unnest({ids}, {paths}, {configs}, {sizes}, {times}) AS o(id, path, config, size, time)
 074            ON CONFLICT ("SweepId", "ProxyPath") DO NOTHING
 075            """,
 076            ct);
 077    }
 78
 79    public async Task<ProxySweepResult> SummariseAsync(Guid sweepId, CancellationToken ct)
 280    {
 281        var report = db.ProxyOrphans.Where(o => o.SweepId == sweepId);
 282        var live = db.StorageConfigs.LiveIds();
 283        return new ProxySweepResult(
 284            await report.CountAsync(ct),
 285            await report.SumAsync(o => o.SizeBytes, ct),
 286            await report.CountAsync(o => o.ProxyPath.EndsWith(ProxyStagingWriter.TemporaryFileSuffix), ct),
 287            await report
 288                .Where(o => o.StorageConfigId != null && !live.Contains(o.StorageConfigId))
 289                .Select(o => o.StorageConfigId)
 290                .Distinct()
 291                .CountAsync(ct));
 292    }
 93
 94    public async Task<IReadOnlyList<ProxyOrphan>> GetPendingAsync(ProxyDeletionPayload deletion, int take, CancellationT
 795    {
 796        var pending = db.ProxyOrphans.Where(o => o.SweepId == deletion.SweepId && o.DeletionOutcome == null);
 797        var live = db.StorageConfigs.LiveIds();
 798        pending = deletion.StorageConfigId is { } configId
 799            ? pending.Where(o => o.StorageConfigId == configId)
 7100            : pending.Where(o => o.StorageConfigId == null || !live.Contains(o.StorageConfigId));
 7101        if (deletion.OrphanIds is { } selected)
 2102            pending = pending.Where(o => selected.Contains(o.Id));
 7103        return await pending.OrderBy(o => o.Id).Take(take).ToListAsync(ct);
 7104    }
 105
 1106    public void ForgetLoaded() => db.ChangeTracker.Clear();
 107
 108    internal static IQueryable<string> ReferencedUnderQuery(AnichronDbContext db, string shard)
 3109        => db.ProxyFiles.IgnoreQueryFilters()
 3110            .Where(p => p.ProxyPath.StartsWith(shard + "/"))
 3111            .Select(p => p.ProxyPath);
 112}