Some checks failed
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Docs CI / lint-and-preview (push) Has been cancelled
Mirror Thin Bundle Sign & Verify / mirror-sign (push) Has been cancelled
api-governance / spectral-lint (push) Has been cancelled
45 lines
1.7 KiB
C#
45 lines
1.7 KiB
C#
using System.Collections.Concurrent;
|
|
|
|
namespace StellaOps.Policy.Engine.Snapshots;
|
|
|
|
internal interface ISnapshotStore
|
|
{
|
|
Task SaveAsync(SnapshotDetail snapshot, CancellationToken cancellationToken = default);
|
|
Task<SnapshotDetail?> GetAsync(string snapshotId, CancellationToken cancellationToken = default);
|
|
Task<IReadOnlyList<SnapshotDetail>> ListAsync(string? tenantId = null, CancellationToken cancellationToken = default);
|
|
}
|
|
|
|
internal sealed class InMemorySnapshotStore : ISnapshotStore
|
|
{
|
|
private readonly ConcurrentDictionary<string, SnapshotDetail> _snapshots = new(StringComparer.Ordinal);
|
|
|
|
public Task SaveAsync(SnapshotDetail snapshot, CancellationToken cancellationToken = default)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(snapshot);
|
|
_snapshots[snapshot.SnapshotId] = snapshot;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task<SnapshotDetail?> GetAsync(string snapshotId, CancellationToken cancellationToken = default)
|
|
{
|
|
_snapshots.TryGetValue(snapshotId, out var value);
|
|
return Task.FromResult(value);
|
|
}
|
|
|
|
public Task<IReadOnlyList<SnapshotDetail>> ListAsync(string? tenantId = null, CancellationToken cancellationToken = default)
|
|
{
|
|
IEnumerable<SnapshotDetail> items = _snapshots.Values;
|
|
if (!string.IsNullOrWhiteSpace(tenantId))
|
|
{
|
|
items = items.Where(s => string.Equals(s.TenantId, tenantId, StringComparison.Ordinal));
|
|
}
|
|
|
|
var ordered = items
|
|
.OrderBy(s => s.GeneratedAt, StringComparer.Ordinal)
|
|
.ThenBy(s => s.SnapshotId, StringComparer.Ordinal)
|
|
.ToList();
|
|
|
|
return Task.FromResult<IReadOnlyList<SnapshotDetail>>(ordered);
|
|
}
|
|
}
|