| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using AsiBackbone.Core.Audit; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.Storage.InMemory.Audit; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// In-memory audit residue lifecycle store for tests, samples, and development hosts. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// This store is not durable across process restarts. Production hosts should use a durable provider such as EF Core or |
| | | 11 | | /// </remarks> |
| | | 12 | | public sealed class InMemoryAuditResidueLifecycleStore : IAsiBackboneAuditResidueLifecycleStore |
| | | 13 | | { |
| | 1 | 14 | | private readonly ConcurrentDictionary<string, AuditResidueLifecycleEvent> events = new(StringComparer.Ordinal); |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public ValueTask<AuditResidueLifecycleEvent> AppendAsync( |
| | | 18 | | AuditResidueLifecycleEvent lifecycleEvent, |
| | | 19 | | CancellationToken cancellationToken = default) |
| | | 20 | | { |
| | 1 | 21 | | ArgumentNullException.ThrowIfNull(lifecycleEvent); |
| | 1 | 22 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 23 | | |
| | 1 | 24 | | return !events.TryAdd(lifecycleEvent.EventId, lifecycleEvent) |
| | 1 | 25 | | ? throw new InvalidOperationException($"Lifecycle event '{lifecycleEvent.EventId}' already exists.") |
| | 1 | 26 | | : ValueTask.FromResult(lifecycleEvent); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public ValueTask<AuditResidueLifecycleEvent?> FindByEventIdAsync( |
| | | 31 | | string eventId, |
| | | 32 | | CancellationToken cancellationToken = default) |
| | | 33 | | { |
| | 1 | 34 | | ArgumentException.ThrowIfNullOrWhiteSpace(eventId); |
| | 1 | 35 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 36 | | |
| | 1 | 37 | | _ = events.TryGetValue(eventId.Trim(), out AuditResidueLifecycleEvent? lifecycleEvent); |
| | | 38 | | |
| | 1 | 39 | | return ValueTask.FromResult(lifecycleEvent); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public ValueTask<IReadOnlyList<AuditResidueLifecycleEvent>> FindByCorrelationIdAsync( |
| | | 44 | | string correlationId, |
| | | 45 | | CancellationToken cancellationToken = default) |
| | | 46 | | { |
| | 0 | 47 | | ArgumentException.ThrowIfNullOrWhiteSpace(correlationId); |
| | 0 | 48 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 49 | | |
| | 0 | 50 | | string normalizedCorrelationId = correlationId.Trim(); |
| | | 51 | | |
| | 0 | 52 | | IReadOnlyList<AuditResidueLifecycleEvent> matches = [.. events.Values |
| | 0 | 53 | | .Where(lifecycleEvent => string.Equals(lifecycleEvent.CorrelationId, normalizedCorrelationId, StringComparis |
| | 0 | 54 | | .OrderBy(lifecycleEvent => lifecycleEvent.OccurredUtc) |
| | 0 | 55 | | .ThenBy(lifecycleEvent => lifecycleEvent.EventId)]; |
| | | 56 | | |
| | 0 | 57 | | return ValueTask.FromResult(matches); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public ValueTask<IReadOnlyList<AuditResidueLifecycleEvent>> FindByAuditResidueIdAsync( |
| | | 62 | | string auditResidueId, |
| | | 63 | | CancellationToken cancellationToken = default) |
| | | 64 | | { |
| | 0 | 65 | | ArgumentException.ThrowIfNullOrWhiteSpace(auditResidueId); |
| | 0 | 66 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 67 | | |
| | 0 | 68 | | string normalizedAuditResidueId = auditResidueId.Trim(); |
| | | 69 | | |
| | 0 | 70 | | IReadOnlyList<AuditResidueLifecycleEvent> matches = [.. events.Values |
| | 0 | 71 | | .Where(lifecycleEvent => string.Equals(lifecycleEvent.AuditResidueId, normalizedAuditResidueId, StringCompar |
| | 0 | 72 | | .OrderBy(lifecycleEvent => lifecycleEvent.OccurredUtc) |
| | 0 | 73 | | .ThenBy(lifecycleEvent => lifecycleEvent.EventId)]; |
| | | 74 | | |
| | 0 | 75 | | return ValueTask.FromResult(matches); |
| | | 76 | | } |
| | | 77 | | } |