|
| 1 | +using ModularityKit.Mutator.Governance.Abstractions.Lifecycle.Model; |
| 2 | +using ModularityKit.Mutator.Governance.Abstractions.Queries.Model; |
| 3 | +using ModularityKit.Mutator.Governance.Redis.Keys; |
| 4 | +using ModularityKit.Mutator.Governance.Redis.Storage.Candidates.Models; |
| 5 | +using StackExchange.Redis; |
| 6 | + |
| 7 | +namespace ModularityKit.Mutator.Governance.Redis.Storage.Candidates.Planning; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Builds candidate-id lookup plans for Redis-backed request queries. |
| 11 | +/// </summary> |
| 12 | +internal sealed class RedisMutationRequestCandidatePlanBuilder(RedisMutationRequestKeyspace keyspace) |
| 13 | +{ |
| 14 | + private readonly RedisMutationRequestKeyspace _keyspace = keyspace ?? throw new ArgumentNullException(nameof(keyspace)); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Builds a plan that loads all known request identifiers. |
| 18 | + /// </summary> |
| 19 | + /// <returns>The candidate plan.</returns> |
| 20 | + public RedisMutationRequestCandidatePlan BuildAllRequestsPlan() |
| 21 | + => Single(_keyspace.RequestIds()); |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Builds a plan that loads request identifiers for a specific state. |
| 25 | + /// </summary> |
| 26 | + /// <param name="stateId">The state identifier.</param> |
| 27 | + /// <returns>The candidate plan.</returns> |
| 28 | + public RedisMutationRequestCandidatePlan BuildByStateIdPlan(string stateId) |
| 29 | + => Single(_keyspace.RequestsByStateId(stateId)); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Builds a plan that loads pending request identifiers, optionally narrowed by pending reason. |
| 33 | + /// </summary> |
| 34 | + /// <param name="reason">The optional pending reason.</param> |
| 35 | + /// <returns>The candidate plan.</returns> |
| 36 | + public RedisMutationRequestCandidatePlan BuildPendingPlan(PendingMutationReason? reason) |
| 37 | + => Single(GetPendingKey(reason)); |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Builds a plan that loads pending request identifiers for a specific state. |
| 41 | + /// </summary> |
| 42 | + /// <param name="stateId">The state identifier.</param> |
| 43 | + /// <param name="reason">The optional pending reason.</param> |
| 44 | + /// <returns>The candidate plan.</returns> |
| 45 | + public RedisMutationRequestCandidatePlan BuildPendingByStateIdPlan(string stateId, PendingMutationReason? reason) |
| 46 | + => Intersect(_keyspace.RequestsByStateId(stateId), GetPendingKey(reason)); |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Builds a best-effort Redis candidate plan for the supplied request query. |
| 50 | + /// </summary> |
| 51 | + /// <param name="query">The request query to analyze.</param> |
| 52 | + /// <returns>The candidate plan.</returns> |
| 53 | + public RedisMutationRequestCandidatePlan BuildQueryPlan(MutationRequestQuery query) |
| 54 | + { |
| 55 | + ArgumentNullException.ThrowIfNull(query); |
| 56 | + |
| 57 | + if (query.RequestIds.Count > 0) |
| 58 | + return Explicit(query.RequestIds); |
| 59 | + |
| 60 | + if (query.PendingReasons.Count > 0) |
| 61 | + return Union(query.PendingReasons.Select(_keyspace.PendingRequestIds)); |
| 62 | + |
| 63 | + if (query.Statuses.Count > 0) |
| 64 | + { |
| 65 | + var pendingOnly = query.Statuses.All(status => status == MutationRequestStatus.Pending); |
| 66 | + return pendingOnly |
| 67 | + ? BuildPendingPlan(reason: null) |
| 68 | + : Union(query.Statuses.Select(_keyspace.RequestsByStatus)); |
| 69 | + } |
| 70 | + |
| 71 | + if (query.StateIds.Count > 0) |
| 72 | + return Union(query.StateIds.Select(_keyspace.RequestsByStateId)); |
| 73 | + |
| 74 | + return BuildAllRequestsPlan(); |
| 75 | + } |
| 76 | + |
| 77 | + private RedisMutationRequestCandidatePlan Explicit(IEnumerable<string> requestIds) |
| 78 | + => new(RedisMutationRequestCandidateOperation.ExplicitIds, Keys: [], |
| 79 | + ExplicitRequestIds: requestIds |
| 80 | + .Where(requestId => !string.IsNullOrWhiteSpace(requestId)) |
| 81 | + .Distinct(StringComparer.Ordinal) |
| 82 | + .ToArray()); |
| 83 | + |
| 84 | + private RedisMutationRequestCandidatePlan Single(RedisKey key) |
| 85 | + => new(RedisMutationRequestCandidateOperation.SingleSet, Keys: [key]); |
| 86 | + |
| 87 | + private RedisMutationRequestCandidatePlan Union(IEnumerable<RedisKey> keys) |
| 88 | + { |
| 89 | + var materialized = keys.Distinct().ToArray(); |
| 90 | + if (materialized.Length == 1) |
| 91 | + return Single(materialized[0]); |
| 92 | + |
| 93 | + return new RedisMutationRequestCandidatePlan( |
| 94 | + RedisMutationRequestCandidateOperation.Union, |
| 95 | + materialized); |
| 96 | + } |
| 97 | + |
| 98 | + private RedisMutationRequestCandidatePlan Intersect(RedisKey left, RedisKey right) |
| 99 | + => new(RedisMutationRequestCandidateOperation.Intersection, [left, right]); |
| 100 | + |
| 101 | + private RedisKey GetPendingKey(PendingMutationReason? reason) |
| 102 | + => reason.HasValue ? _keyspace.PendingRequestIds(reason.Value) : _keyspace.PendingRequestIds(); |
| 103 | +} |
0 commit comments