using System.Collections.Concurrent; namespace StellaOps.Policy.Engine.Snapshots; internal interface ISnapshotStore { Task SaveAsync(SnapshotDetail snapshot, CancellationToken cancellationToken = default); Task GetAsync(string snapshotId, CancellationToken cancellationToken = default); Task> ListAsync(string? tenantId = null, CancellationToken cancellationToken = default); } internal sealed class InMemorySnapshotStore : ISnapshotStore { private readonly ConcurrentDictionary _snapshots = new(StringComparer.Ordinal); public Task SaveAsync(SnapshotDetail snapshot, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(snapshot); _snapshots[snapshot.SnapshotId] = snapshot; return Task.CompletedTask; } public Task GetAsync(string snapshotId, CancellationToken cancellationToken = default) { _snapshots.TryGetValue(snapshotId, out var value); return Task.FromResult(value); } public Task> ListAsync(string? tenantId = null, CancellationToken cancellationToken = default) { IEnumerable 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>(ordered); } }