|
| 1 | +using ModularityKit.Mutator.Governance.Abstractions.Requests.Model; |
| 2 | +using ModularityKit.Mutator.Governance.Redis.Storage.Documents.Keys; |
| 3 | +using ModularityKit.Mutator.Governance.Redis.Storage.Documents.Materialization; |
| 4 | +using ModularityKit.Mutator.Governance.Redis.Storage.Documents.Payloads; |
| 5 | + |
| 6 | +namespace ModularityKit.Mutator.Governance.Redis.Storage.Documents.Reading; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Coordinates Redis document key creation, payload reads, and request materialization. |
| 10 | +/// </summary> |
| 11 | +internal sealed class RedisMutationRequestDocumentReader( |
| 12 | + RedisMutationRequestDocumentKeyFactory keyFactory, |
| 13 | + RedisMutationRequestPayloadReader payloadReader) |
| 14 | +{ |
| 15 | + private readonly RedisMutationRequestDocumentKeyFactory _keyFactory = |
| 16 | + keyFactory ?? throw new ArgumentNullException(nameof(keyFactory)); |
| 17 | + private readonly RedisMutationRequestPayloadReader _payloadReader = |
| 18 | + payloadReader ?? throw new ArgumentNullException(nameof(payloadReader)); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Loads request documents ordered by request creation time. |
| 22 | + /// </summary> |
| 23 | + /// <param name="requestIds">The request identifiers to load.</param> |
| 24 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 25 | + /// <returns>The materialized and ordered mutation requests.</returns> |
| 26 | + public Task<IReadOnlyList<MutationRequest>> LoadOrderedByCreatedAsync(IEnumerable<string> requestIds, CancellationToken cancellationToken) => |
| 27 | + LoadAsync(requestIds, cancellationToken, orderByCreated: true); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Loads request documents for the supplied identifiers. |
| 31 | + /// </summary> |
| 32 | + /// <param name="requestIds">The request identifiers to load.</param> |
| 33 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 34 | + /// <param name="orderByCreated">Whether to order results by request creation time.</param> |
| 35 | + /// <returns>The materialized mutation requests.</returns> |
| 36 | + public async Task<IReadOnlyList<MutationRequest>> LoadAsync(IEnumerable<string> requestIds, CancellationToken cancellationToken, bool orderByCreated) |
| 37 | + { |
| 38 | + cancellationToken.ThrowIfCancellationRequested(); |
| 39 | + |
| 40 | + var keys = _keyFactory.CreateKeys(requestIds); |
| 41 | + if (keys.Count == 0) |
| 42 | + return []; |
| 43 | + |
| 44 | + var values = await _payloadReader.LoadAsync(keys, cancellationToken).ConfigureAwait(false); |
| 45 | + return RedisMutationRequestDocumentMaterializer.Materialize(values, cancellationToken, orderByCreated); |
| 46 | + } |
| 47 | +} |
0 commit comments