| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationAreaCatalogProcessor.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using SwiftCollections; |
| | | 9 | | |
| | | 10 | | namespace Trailblazer.Pathing; |
| | | 11 | | |
| | | 12 | | internal sealed class NavigationAreaCatalogProcessor |
| | | 13 | | { |
| | 1212 | 14 | | private readonly SwiftQueue<NavigationAreaPolicyCommitOperation> _pending = new(); |
| | | 15 | | private readonly int _maxPending; |
| | | 16 | | private readonly int _maxBatchItems; |
| | | 17 | | private readonly int _maxPolicies; |
| | | 18 | | private readonly int _maxRulesPerPolicy; |
| | | 19 | | private readonly int _maxRules; |
| | | 20 | | private readonly int _navigationAreaCount; |
| | | 21 | | private readonly long _maxPendingBytes; |
| | | 22 | | private readonly NavigationOperationRejection[] _outcomes; |
| | | 23 | | private int _pendingRuleCount; |
| | | 24 | | private long _pendingRetainedBytes; |
| | | 25 | | private long _sequenceHighWater; |
| | 1212 | 26 | | private int _effectiveFrameHighWater = -1; |
| | 1212 | 27 | | private int _lastFrame = -1; |
| | | 28 | | |
| | 1212 | 29 | | internal NavigationAreaCatalogProcessor(TrailblazerWorldContextSettings settings) |
| | | 30 | | { |
| | 1212 | 31 | | _maxPending = settings.OperationLimits.MaxPendingOperations; |
| | 1212 | 32 | | _maxBatchItems = settings.OperationLimits.MaxBatchItems; |
| | 1212 | 33 | | _maxPolicies = settings.MaxAreaPolicies; |
| | 1212 | 34 | | _maxRulesPerPolicy = settings.MaxAreaRulesPerPolicy; |
| | 1212 | 35 | | _maxRules = settings.MaxAreaRules; |
| | 1212 | 36 | | _navigationAreaCount = settings.NavigationAreaCount; |
| | 1212 | 37 | | long maximumRuleBytes = (long)settings.MaxAreaRules * 24L; |
| | 1212 | 38 | | _maxPendingBytes = settings.OperationLimits.MaxPendingDescriptorBytes |
| | 1212 | 39 | | > long.MaxValue - maximumRuleBytes |
| | 1212 | 40 | | ? long.MaxValue |
| | 1212 | 41 | | : settings.OperationLimits.MaxPendingDescriptorBytes + maximumRuleBytes; |
| | 1212 | 42 | | _outcomes = new NavigationOperationRejection[_maxPending]; |
| | 1212 | 43 | | } |
| | | 44 | | |
| | 4410 | 45 | | internal int PendingCount => _pending.Count; |
| | | 46 | | |
| | 2206 | 47 | | internal int PendingRuleCount => _pendingRuleCount; |
| | | 48 | | |
| | 4408 | 49 | | internal long PendingRetainedBytes => _pendingRetainedBytes; |
| | | 50 | | |
| | | 51 | | internal void Reset() |
| | | 52 | | { |
| | 50 | 53 | | for (int i = 0; i < _pending.Count; i++) |
| | 1 | 54 | | _pending[i].Receipt.CompleteSuperseded(); |
| | 24 | 55 | | _pending.Clear(); |
| | 24 | 56 | | _pendingRuleCount = 0; |
| | 24 | 57 | | _pendingRetainedBytes = 0; |
| | 24 | 58 | | _sequenceHighWater = 0; |
| | 24 | 59 | | _effectiveFrameHighWater = -1; |
| | 24 | 60 | | _lastFrame = -1; |
| | 24 | 61 | | } |
| | | 62 | | |
| | | 63 | | internal bool Admit(NavigationAreaPolicyCommitOperation operation) |
| | | 64 | | { |
| | 199 | 65 | | if (!operation.Receipt.TryClaimAdmission()) |
| | 1 | 66 | | return false; |
| | | 67 | | |
| | 198 | 68 | | NavigationOperationRejection rejection = NavigationOperationRejection.None; |
| | 198 | 69 | | if (operation.PublicationSequence == _sequenceHighWater) |
| | 1 | 70 | | rejection = NavigationOperationRejection.DuplicateSequence; |
| | 197 | 71 | | else if (operation.PublicationSequence < _sequenceHighWater) |
| | 1 | 72 | | rejection = NavigationOperationRejection.RegressingSequence; |
| | 196 | 73 | | else if (operation.EffectiveFrame < _effectiveFrameHighWater) |
| | 1 | 74 | | rejection = NavigationOperationRejection.RegressingEffectiveFrame; |
| | 195 | 75 | | else if (operation.EffectiveFrame <= _lastFrame) |
| | 1 | 76 | | rejection = NavigationOperationRejection.LateEffectiveFrame; |
| | 194 | 77 | | else if (operation.Policy.RuleCount != _navigationAreaCount) |
| | 2 | 78 | | rejection = NavigationOperationRejection.ValidationFailed; |
| | 192 | 79 | | else if (_pending.Count >= _maxPending) |
| | 1 | 80 | | rejection = NavigationOperationRejection.CapacityExceeded; |
| | 191 | 81 | | else if (operation.Policy.RuleCount > _maxRules - _pendingRuleCount |
| | 191 | 82 | | || operation.Policy.RetainedBytes > _maxPendingBytes - _pendingRetainedBytes) |
| | 2 | 83 | | rejection = NavigationOperationRejection.CapacityExceeded; |
| | | 84 | | |
| | 198 | 85 | | if (operation.PublicationSequence > _sequenceHighWater) |
| | | 86 | | { |
| | 196 | 87 | | _sequenceHighWater = operation.PublicationSequence; |
| | 196 | 88 | | if (operation.EffectiveFrame > _effectiveFrameHighWater) |
| | 189 | 89 | | _effectiveFrameHighWater = operation.EffectiveFrame; |
| | | 90 | | } |
| | | 91 | | |
| | 198 | 92 | | if (rejection != NavigationOperationRejection.None) |
| | | 93 | | { |
| | 9 | 94 | | operation.Receipt.CompleteRejected(rejection); |
| | 9 | 95 | | return false; |
| | | 96 | | } |
| | 189 | 97 | | _pending.Enqueue(operation); |
| | 189 | 98 | | _pendingRuleCount += operation.Policy.RuleCount; |
| | 189 | 99 | | _pendingRetainedBytes += operation.Policy.RetainedBytes; |
| | 189 | 100 | | return true; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | internal PreparedFrame Prepare( |
| | | 104 | | int frame, |
| | | 105 | | NavigationAreaCatalog current, |
| | | 106 | | MaintenanceWorkMeter meter, |
| | | 107 | | long maxCatalogBytes, |
| | | 108 | | int maxCatalogPages) |
| | | 109 | | { |
| | 3479 | 110 | | _lastFrame = frame; |
| | 3479 | 111 | | NavigationAreaCatalog candidate = current; |
| | 3479 | 112 | | int eligible = 0; |
| | 3664 | 113 | | while (eligible < _pending.Count |
| | 3664 | 114 | | && eligible < _maxBatchItems |
| | 3664 | 115 | | && _pending[eligible].EffectiveFrame <= frame) |
| | | 116 | | { |
| | 188 | 117 | | NavigationAreaPolicy policy = _pending[eligible].Policy; |
| | 188 | 118 | | if (!meter.TryConsumeDependencyEntries(candidate.GetPublishWork( |
| | 188 | 119 | | policy, |
| | 188 | 120 | | _maxPolicies, |
| | 188 | 121 | | _maxRules))) |
| | | 122 | | break; |
| | 185 | 123 | | _outcomes[eligible] = candidate.TryPublish( |
| | 185 | 124 | | policy, |
| | 185 | 125 | | _maxPolicies, |
| | 185 | 126 | | _navigationAreaCount, |
| | 185 | 127 | | _maxRulesPerPolicy, |
| | 185 | 128 | | _maxRules, |
| | 185 | 129 | | out NavigationAreaCatalog next); |
| | 185 | 130 | | if (_outcomes[eligible] == NavigationOperationRejection.None |
| | 185 | 131 | | && (next.RetainedBytes > maxCatalogBytes |
| | 185 | 132 | | || next.PersistentPageCount > maxCatalogPages)) |
| | | 133 | | { |
| | 2 | 134 | | _outcomes[eligible] = NavigationOperationRejection.CapacityExceeded; |
| | | 135 | | } |
| | 183 | 136 | | else if (_outcomes[eligible] == NavigationOperationRejection.None) |
| | 178 | 137 | | candidate = next; |
| | 185 | 138 | | eligible++; |
| | | 139 | | } |
| | 3479 | 140 | | return new PreparedFrame(this, candidate, _outcomes, eligible); |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | internal readonly struct PreparedFrame |
| | | 144 | | { |
| | | 145 | | private readonly NavigationAreaCatalogProcessor _owner; |
| | | 146 | | private readonly NavigationOperationRejection[] _outcomes; |
| | | 147 | | |
| | | 148 | | internal PreparedFrame( |
| | | 149 | | NavigationAreaCatalogProcessor owner, |
| | | 150 | | NavigationAreaCatalog candidate, |
| | | 151 | | NavigationOperationRejection[] outcomes, |
| | | 152 | | int count) |
| | | 153 | | { |
| | 3479 | 154 | | _owner = owner; |
| | 3479 | 155 | | Candidate = candidate; |
| | 3479 | 156 | | _outcomes = outcomes; |
| | 3479 | 157 | | Count = count; |
| | 3479 | 158 | | } |
| | | 159 | | |
| | | 160 | | internal NavigationAreaCatalog Candidate { get; } |
| | | 161 | | |
| | | 162 | | internal int Count { get; } |
| | | 163 | | |
| | | 164 | | internal void Complete(int frame) |
| | | 165 | | { |
| | 6918 | 166 | | for (int i = 0; i < Count; i++) |
| | | 167 | | { |
| | 180 | 168 | | NavigationAreaPolicyCommitOperation operation = _owner._pending[i]; |
| | 180 | 169 | | if (_outcomes[i] == NavigationOperationRejection.None) |
| | 174 | 170 | | operation.Receipt.CompleteApplied(frame); |
| | | 171 | | else |
| | 6 | 172 | | operation.Receipt.CompleteRejected(_outcomes[i]); |
| | | 173 | | } |
| | 3279 | 174 | | _owner.RemovePrefix(Count); |
| | 3279 | 175 | | } |
| | | 176 | | |
| | | 177 | | internal void CompleteCapacityRejected() |
| | | 178 | | { |
| | 8 | 179 | | for (int i = 0; i < Count; i++) |
| | | 180 | | { |
| | 2 | 181 | | NavigationAreaPolicyCommitOperation operation = _owner._pending[i]; |
| | 2 | 182 | | NavigationOperationRejection rejection = _outcomes[i] == NavigationOperationRejection.None |
| | 2 | 183 | | ? NavigationOperationRejection.CapacityExceeded |
| | 2 | 184 | | : _outcomes[i]; |
| | 2 | 185 | | operation.Receipt.CompleteRejected(rejection); |
| | | 186 | | } |
| | 2 | 187 | | _owner.RemovePrefix(Count); |
| | 2 | 188 | | } |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | private void RemovePrefix(int count) |
| | | 192 | | { |
| | 6926 | 193 | | for (int i = 0; i < count; i++) |
| | | 194 | | { |
| | 182 | 195 | | NavigationAreaPolicyCommitOperation operation = _pending.Dequeue(); |
| | 182 | 196 | | _pendingRuleCount -= operation.Policy.RuleCount; |
| | 182 | 197 | | _pendingRetainedBytes -= operation.Policy.RetainedBytes; |
| | | 198 | | } |
| | 3281 | 199 | | } |
| | | 200 | | } |