| | | 1 | | using AsiBackbone.Core.Audit; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Storage.InMemory.Audit; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// In-memory audit ledger intended for tests, samples, and local validation hosts. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// This type is not durable storage. It records audit residue in process memory and is suitable only for local developm |
| | | 10 | | /// tests, examples, and non-production validation flows. |
| | | 11 | | /// </remarks> |
| | | 12 | | public sealed class InMemoryAuditLedger : IAsiBackboneAuditSink |
| | | 13 | | { |
| | 3 | 14 | | private readonly Lock syncRoot = new(); |
| | 3 | 15 | | private readonly List<IAsiBackboneAuditResidue> records = []; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Gets a snapshot of all recorded audit residue values. |
| | | 19 | | /// </summary> |
| | | 20 | | public IReadOnlyList<IAsiBackboneAuditResidue> Records |
| | | 21 | | { |
| | | 22 | | get |
| | 3 | 23 | | { |
| | | 24 | | lock (syncRoot) |
| | | 25 | | { |
| | 3 | 26 | | return Array.AsReadOnly([.. records]); |
| | | 27 | | } |
| | 3 | 28 | | } |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public ValueTask WriteAsync( |
| | | 33 | | IAsiBackboneAuditResidue residue, |
| | | 34 | | CancellationToken cancellationToken = default) |
| | | 35 | | { |
| | 3 | 36 | | return WriteCore(residue, cancellationToken); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | ValueTask IAsiBackboneAuditSink.WriteAsync( |
| | | 40 | | IAsiBackboneAuditResidue residue, |
| | | 41 | | CancellationToken cancellationToken) |
| | | 42 | | { |
| | 0 | 43 | | return WriteCore(residue, cancellationToken); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets a snapshot of records matching the supplied correlation identifier. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="correlationId">The correlation identifier to match.</param> |
| | | 50 | | /// <returns>Audit residue values with the supplied correlation identifier.</returns> |
| | | 51 | | public IReadOnlyList<IAsiBackboneAuditResidue> GetByCorrelationId(string correlationId) |
| | | 52 | | { |
| | 1 | 53 | | ArgumentException.ThrowIfNullOrWhiteSpace(correlationId); |
| | | 54 | | |
| | 1 | 55 | | string normalizedCorrelationId = correlationId.Trim(); |
| | | 56 | | |
| | | 57 | | lock (syncRoot) |
| | | 58 | | { |
| | 1 | 59 | | return Array.AsReadOnly([ |
| | 1 | 60 | | .. records.Where(record => string.Equals( |
| | 1 | 61 | | record.CorrelationId, |
| | 1 | 62 | | normalizedCorrelationId, |
| | 1 | 63 | | StringComparison.Ordinal)) |
| | 1 | 64 | | ]); |
| | | 65 | | } |
| | 1 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Attempts to get a record by its audit event identifier. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="eventId">The audit event identifier.</param> |
| | | 72 | | /// <returns>The matching audit residue, or <see langword="null"/> when none is found.</returns> |
| | | 73 | | public IAsiBackboneAuditResidue? GetByEventId(string eventId) |
| | | 74 | | { |
| | 1 | 75 | | ArgumentException.ThrowIfNullOrWhiteSpace(eventId); |
| | | 76 | | |
| | 1 | 77 | | string normalizedEventId = eventId.Trim(); |
| | | 78 | | |
| | | 79 | | lock (syncRoot) |
| | | 80 | | { |
| | 2 | 81 | | return records.FirstOrDefault(record => string.Equals( |
| | 2 | 82 | | record.EventId, |
| | 2 | 83 | | normalizedEventId, |
| | 2 | 84 | | StringComparison.Ordinal)); |
| | | 85 | | } |
| | 1 | 86 | | } |
| | | 87 | | |
| | | 88 | | private ValueTask WriteCore( |
| | | 89 | | IAsiBackboneAuditResidue residue, |
| | | 90 | | CancellationToken cancellationToken) |
| | | 91 | | { |
| | 3 | 92 | | ArgumentNullException.ThrowIfNull(residue); |
| | 3 | 93 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 94 | | |
| | | 95 | | lock (syncRoot) |
| | | 96 | | { |
| | 3 | 97 | | records.Add(residue); |
| | 3 | 98 | | } |
| | | 99 | | |
| | 3 | 100 | | return ValueTask.CompletedTask; |
| | | 101 | | } |
| | | 102 | | } |