| | | 1 | | using Anichron.Core.Domain; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace Anichron.Core.Data.Repository; |
| | | 5 | | |
| | 13 | 6 | | public sealed record ProxyOrphanView( |
| | 13 | 7 | | Guid Id, |
| | 13 | 8 | | string ProxyPath, |
| | 13 | 9 | | Guid? StorageConfigId, |
| | 13 | 10 | | long SizeBytes, |
| | 13 | 11 | | Instant LastWriteTime, |
| | 13 | 12 | | ProxyDeletionOutcome? DeletionOutcome, |
| | 13 | 13 | | Instant? ProcessedAt); |
| | | 14 | | |
| | | 15 | | /// <summary>Which orphans of a sweep to page through.</summary> |
| | | 16 | | /// <param name="SweepId">The sweep.</param> |
| | | 17 | | /// <param name="Outcome">Only orphans a deletion gave this outcome, or null for any.</param> |
| | | 18 | | /// <param name="Pending">Only orphans no deletion processed yet.</param> |
| | | 19 | | /// <param name="StorageConfigId">Only orphans of this storage config, or null for any.</param> |
| | | 20 | | /// <param name="Skip">Orphans before the page.</param> |
| | | 21 | | /// <param name="Take">The page size.</param> |
| | | 22 | | public sealed record ProxyOrphanQuery(Guid SweepId, ProxyDeletionOutcome? Outcome, bool Pending, Guid? StorageConfigId, |
| | | 23 | | |
| | | 24 | | public sealed record ProxyOrphanPage(IReadOnlyList<ProxyOrphanView> Items, int Total); |
| | | 25 | | |
| | | 26 | | /// <summary>An orphan's id and the live storage config its directory names.</summary> |
| | | 27 | | /// <param name="Id">The orphan.</param> |
| | | 28 | | /// <param name="LiveStorageConfigId">The config, or null when its path names none or the config no longer exists.</para |
| | | 29 | | public sealed record ProxyOrphanScope(Guid Id, Guid? LiveStorageConfigId); |
| | | 30 | | |
| | | 31 | | public interface IProxyOrphanReader |
| | | 32 | | { |
| | | 33 | | /// <summary>A page of a sweep's orphans, by path.</summary> |
| | | 34 | | /// <param name="query">The sweep, the optional outcome and config filters, and the page.</param> |
| | | 35 | | /// <param name="ct">Cancels the read.</param> |
| | | 36 | | /// <returns>The page, and the number of orphans matching the filters.</returns> |
| | | 37 | | Task<ProxyOrphanPage> ListAsync(ProxyOrphanQuery query, CancellationToken ct); |
| | | 38 | | |
| | | 39 | | /// <summary>The orphans of a sweep among the ids; ids that are not orphans of it are left out.</summary> |
| | | 40 | | /// <param name="sweepId">The sweep.</param> |
| | | 41 | | /// <param name="ids">The orphans asked for.</param> |
| | | 42 | | /// <param name="ct">Cancels the read.</param> |
| | | 43 | | Task<IReadOnlyList<ProxyOrphanScope>> FindAsync(Guid sweepId, IReadOnlyCollection<Guid> ids, CancellationToken ct); |
| | | 44 | | |
| | | 45 | | /// <summary>Each live storage config, null for none, that a sweep's unprocessed orphans name.</summary> |
| | | 46 | | /// <param name="sweepId">The sweep.</param> |
| | | 47 | | /// <param name="ct">Cancels the read.</param> |
| | | 48 | | Task<IReadOnlyList<Guid?>> PendingConfigsAsync(Guid sweepId, CancellationToken ct); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public sealed class EfProxyOrphanReader(AnichronDbContext db) : IProxyOrphanReader |
| | | 52 | | { |
| | | 53 | | public async Task<ProxyOrphanPage> ListAsync(ProxyOrphanQuery query, CancellationToken ct) |
| | | 54 | | { |
| | | 55 | | var matching = db.ProxyOrphans.Where(o => o.SweepId == query.SweepId); |
| | | 56 | | if (query.Outcome is { } outcome) |
| | | 57 | | matching = matching.Where(o => o.DeletionOutcome == outcome); |
| | | 58 | | if (query.Pending) |
| | | 59 | | matching = matching.Where(o => o.DeletionOutcome == null); |
| | | 60 | | if (query.StorageConfigId is { } configId) |
| | | 61 | | matching = matching.Where(o => o.StorageConfigId == configId); |
| | | 62 | | |
| | | 63 | | var items = await matching |
| | | 64 | | .OrderBy(o => o.ProxyPath) |
| | | 65 | | .Skip(query.Skip) |
| | | 66 | | .Take(query.Take) |
| | | 67 | | .Select(o => new ProxyOrphanView( |
| | | 68 | | o.Id, o.ProxyPath, o.StorageConfigId, o.SizeBytes, o.LastWriteTime, o.DeletionOutcome, o.ProcessedAt)) |
| | | 69 | | .ToListAsync(ct); |
| | | 70 | | return new ProxyOrphanPage(items, await matching.CountAsync(ct)); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | public async Task<IReadOnlyList<ProxyOrphanScope>> FindAsync(Guid sweepId, IReadOnlyCollection<Guid> ids, Cancellati |
| | | 74 | | { |
| | | 75 | | var live = db.StorageConfigs.LiveIds(); |
| | | 76 | | return await db.ProxyOrphans |
| | | 77 | | .Where(o => o.SweepId == sweepId && ids.Contains(o.Id)) |
| | | 78 | | .Select(o => new ProxyOrphanScope(o.Id, live.Contains(o.StorageConfigId) ? o.StorageConfigId : null)) |
| | | 79 | | .ToListAsync(ct); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public async Task<IReadOnlyList<Guid?>> PendingConfigsAsync(Guid sweepId, CancellationToken ct) |
| | | 83 | | { |
| | | 84 | | var live = db.StorageConfigs.LiveIds(); |
| | | 85 | | return await db.ProxyOrphans |
| | | 86 | | .Where(o => o.SweepId == sweepId && o.DeletionOutcome == null) |
| | | 87 | | .Select(o => live.Contains(o.StorageConfigId) ? o.StorageConfigId : null) |
| | | 88 | | .Distinct() |
| | | 89 | | .ToListAsync(ct); |
| | | 90 | | } |
| | | 91 | | } |