| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Itinero.Profiles; |
| | | 8 | | |
| | | 9 | | namespace Itinero.Network.Search.Islands; |
| | | 10 | | |
| | | 11 | | internal class RoutingNetworkIslandManager |
| | | 12 | | { |
| | 608 | 13 | | private readonly Dictionary<(string profile, uint tile), Task> _tilesInProgress = new(); |
| | 608 | 14 | | private readonly ReaderWriterLockSlim _tilesInProgressLock = new(); |
| | | 15 | | private readonly Dictionary<string, Islands> _islands; |
| | 608 | 16 | | private readonly Dictionary<(string profile, IslandKind kind), IslandDirectedGraph> _directedGraphs = new(); |
| | 608 | 17 | | private readonly ReaderWriterLockSlim _islandsLock = new(); |
| | | 18 | | |
| | | 19 | | // Per-profile semaphore that serialises IslandClassifier.BuildForTileAsync |
| | | 20 | | // calls against each other for the same profile. The shared Full+NonLocal |
| | | 21 | | // dgs are reset to their initial state at the end of each call (per spec); |
| | | 22 | | // running two classifications for the same profile concurrently would let |
| | | 23 | | // one wipe the other's working state mid-flight. Different profiles still |
| | | 24 | | // run in parallel — each has its own dg pair and its own semaphore. |
| | 608 | 25 | | private readonly ConcurrentDictionary<string, SemaphoreSlim> _buildSerialisers = new(); |
| | | 26 | | |
| | | 27 | | internal SemaphoreSlim GetBuildSerialiser(string profileName) => |
| | 69 | 28 | | _buildSerialisers.GetOrAdd(profileName, _ => new SemaphoreSlim(1, 1)); |
| | | 29 | | |
| | 351 | 30 | | internal RoutingNetworkIslandManager(int maxIslandSize) |
| | 351 | 31 | | { |
| | 351 | 32 | | this.MaxIslandSize = maxIslandSize; |
| | 351 | 33 | | _islands = new(); |
| | 351 | 34 | | } |
| | | 35 | | |
| | 257 | 36 | | private RoutingNetworkIslandManager(int maxIslandSize, Dictionary<string, Islands> islands) |
| | 257 | 37 | | { |
| | 257 | 38 | | this.MaxIslandSize = maxIslandSize; |
| | 257 | 39 | | _islands = islands; |
| | 257 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Checks if an edge is on an island using the directed graph. |
| | | 44 | | /// Returns true if island, false if not island, null if inconclusive. |
| | | 45 | | /// </summary> |
| | | 46 | | internal bool? IsEdgeOnIsland(Profile profile, EdgeId edgeId) => |
| | 7 | 47 | | this.IsEdgeOnIsland(profile.Name, edgeId); |
| | | 48 | | |
| | | 49 | | internal bool? IsEdgeOnIsland(string profileName, EdgeId edgeId) |
| | 7 | 50 | | { |
| | | 51 | | try |
| | 7 | 52 | | { |
| | 7 | 53 | | _islandsLock.EnterReadLock(); |
| | | 54 | | |
| | | 55 | | // Snapping is a Full-classification concern, so the existing |
| | | 56 | | // single-DG semantics route through the Full DG. |
| | 7 | 57 | | if (!_directedGraphs.TryGetValue((profileName, IslandKind.Full), out var dg)) |
| | 5 | 58 | | return null; |
| | | 59 | | |
| | 2 | 60 | | if (!_islands.TryGetValue(profileName, out var profileIslands)) |
| | 0 | 61 | | return null; |
| | 2 | 62 | | if (profileIslands.IsEdgeOnIsland(edgeId)) |
| | 0 | 63 | | return true; |
| | | 64 | | |
| | 2 | 65 | | if (dg.IsNotIsland(edgeId)) |
| | 0 | 66 | | return false; |
| | | 67 | | |
| | 2 | 68 | | return null; |
| | | 69 | | } |
| | | 70 | | finally |
| | 7 | 71 | | { |
| | 7 | 72 | | _islandsLock.ExitReadLock(); |
| | 7 | 73 | | } |
| | 7 | 74 | | } |
| | | 75 | | |
| | | 76 | | internal IslandDirectedGraph GetOrCreateDirectedGraph(Profile profile, IslandKind kind = IslandKind.Full) |
| | 3659 | 77 | | { |
| | 3659 | 78 | | var key = (profile.Name, kind); |
| | | 79 | | try |
| | 3659 | 80 | | { |
| | 3659 | 81 | | _islandsLock.EnterUpgradeableReadLock(); |
| | | 82 | | |
| | 7239 | 83 | | if (_directedGraphs.TryGetValue(key, out var dg)) return dg; |
| | | 84 | | |
| | | 85 | | try |
| | 79 | 86 | | { |
| | 79 | 87 | | _islandsLock.EnterWriteLock(); |
| | | 88 | | |
| | 79 | 89 | | dg = new IslandDirectedGraph(); |
| | 79 | 90 | | _directedGraphs[key] = dg; |
| | 79 | 91 | | return dg; |
| | | 92 | | } |
| | | 93 | | finally |
| | 79 | 94 | | { |
| | 79 | 95 | | _islandsLock.ExitWriteLock(); |
| | 79 | 96 | | } |
| | | 97 | | } |
| | | 98 | | finally |
| | 3659 | 99 | | { |
| | 3659 | 100 | | _islandsLock.ExitUpgradeableReadLock(); |
| | 3659 | 101 | | } |
| | 3659 | 102 | | } |
| | | 103 | | |
| | 2774 | 104 | | internal int MaxIslandSize { get; } |
| | | 105 | | |
| | | 106 | | internal bool TryGetIslandsFor(string profileName, out Islands islands) |
| | 0 | 107 | | { |
| | | 108 | | try |
| | 0 | 109 | | { |
| | 0 | 110 | | _islandsLock.EnterReadLock(); |
| | | 111 | | |
| | 0 | 112 | | return _islands.TryGetValue(profileName, out islands); |
| | | 113 | | } |
| | | 114 | | finally |
| | 0 | 115 | | { |
| | 0 | 116 | | _islandsLock.ExitReadLock(); |
| | 0 | 117 | | } |
| | 0 | 118 | | } |
| | | 119 | | |
| | | 120 | | internal Islands GetIslandsFor(Profile profile) |
| | 2426 | 121 | | { |
| | | 122 | | try |
| | 2426 | 123 | | { |
| | 2426 | 124 | | _islandsLock.EnterUpgradeableReadLock(); |
| | | 125 | | |
| | 4808 | 126 | | if (_islands.TryGetValue(profile.Name, out var islands)) return islands; |
| | | 127 | | |
| | | 128 | | try |
| | 44 | 129 | | { |
| | 44 | 130 | | _islandsLock.EnterWriteLock(); |
| | | 131 | | |
| | 44 | 132 | | islands = new Islands(); |
| | 44 | 133 | | _islands[profile.Name] = islands; |
| | 44 | 134 | | return islands; |
| | | 135 | | } |
| | | 136 | | finally |
| | 44 | 137 | | { |
| | 44 | 138 | | _islandsLock.ExitWriteLock(); |
| | 44 | 139 | | } |
| | | 140 | | } |
| | | 141 | | finally |
| | 2426 | 142 | | { |
| | 2426 | 143 | | _islandsLock.ExitUpgradeableReadLock(); |
| | 2426 | 144 | | } |
| | 2426 | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Returns whether the edge is in the profile's main-N component — the |
| | | 149 | | /// dominant SCC of the N-only subgraph, i.e. the "mainland" without |
| | | 150 | | /// L-edges. |
| | | 151 | | /// |
| | | 152 | | /// <list type="bullet"> |
| | | 153 | | /// <item><c>true</c>: edge is in main-N. Default for any edge in a done tile |
| | | 154 | | /// that is neither L-tagged, on an island, nor a non-main-N pocket member.</item> |
| | | 155 | | /// <item><c>false</c>: edge is not in main-N. Either L-tagged (passed in via |
| | | 156 | | /// <paramref name="isLocalAccess"/>), on an island (unreachable in Full), or |
| | | 157 | | /// recorded as a local edge (non-main-N pocket).</item> |
| | | 158 | | /// <item><c>null</c>: classification has not yet produced a verdict for this |
| | | 159 | | /// tile.</item> |
| | | 160 | | /// </list> |
| | | 161 | | /// |
| | | 162 | | /// The L-tag check is tag-driven and resolved by the caller (typically via |
| | | 163 | | /// the cost function's <c>localAccess</c> field on the result of <c>Get</c>), |
| | | 164 | | /// then passed in. The manager itself does not consult any tag storage. |
| | | 165 | | /// </summary> |
| | | 166 | | internal bool? IsMainN(Profile profile, EdgeId edgeId, bool isLocalAccess) |
| | 788377 | 167 | | { |
| | | 168 | | // L-tagged edge — never main-N, no storage lookup needed. |
| | 788379 | 169 | | if (isLocalAccess) return false; |
| | | 170 | | |
| | | 171 | | try |
| | 788375 | 172 | | { |
| | 788375 | 173 | | _islandsLock.EnterReadLock(); |
| | | 174 | | |
| | 1568830 | 175 | | if (!_islands.TryGetValue(profile.Name, out var islands)) return null; |
| | | 176 | | |
| | | 177 | | // Unreachable in the Full classification → not in main-N. |
| | 7921 | 178 | | if (islands.IsEdgeOnIsland(edgeId)) return false; |
| | | 179 | | |
| | | 180 | | // Non-main-N pocket → not in main-N. |
| | 7920 | 181 | | if (islands.IsEdgeLocal(edgeId)) return false; |
| | | 182 | | |
| | | 183 | | // Tile finished classifying and the edge is in neither set → main-N. |
| | | 184 | | // Otherwise we don't yet know. |
| | 7918 | 185 | | return islands.GetTileDone(edgeId.TileId) ? true : null; |
| | | 186 | | } |
| | | 187 | | finally |
| | 788375 | 188 | | { |
| | 788375 | 189 | | _islandsLock.ExitReadLock(); |
| | 788375 | 190 | | } |
| | 788377 | 191 | | } |
| | | 192 | | |
| | | 193 | | internal async Task BuildForTileAsync(RoutingNetwork network, Profile profile, uint tileId, |
| | | 194 | | CancellationToken cancellationToken) |
| | 7 | 195 | | { |
| | | 196 | | // queue task, if not done yet. |
| | | 197 | | Task task; |
| | | 198 | | try |
| | 7 | 199 | | { |
| | 7 | 200 | | _tilesInProgressLock.EnterUpgradeableReadLock(); |
| | | 201 | | |
| | 7 | 202 | | if (!_tilesInProgress.TryGetValue((profile.Name, tileId), out task)) |
| | 7 | 203 | | { |
| | | 204 | | try |
| | 7 | 205 | | { |
| | 7 | 206 | | _tilesInProgressLock.EnterWriteLock(); |
| | | 207 | | |
| | 7 | 208 | | task = IslandClassifier.BuildForTileAsync(network, profile, tileId, cancellationToken); |
| | 7 | 209 | | _tilesInProgress[(profile.Name, tileId)] = task; |
| | 7 | 210 | | } |
| | | 211 | | finally |
| | 7 | 212 | | { |
| | 7 | 213 | | _tilesInProgressLock.ExitWriteLock(); |
| | 7 | 214 | | } |
| | 7 | 215 | | } |
| | 7 | 216 | | } |
| | | 217 | | finally |
| | 7 | 218 | | { |
| | 7 | 219 | | _tilesInProgressLock.ExitUpgradeableReadLock(); |
| | 7 | 220 | | } |
| | | 221 | | |
| | | 222 | | // await the task. |
| | 7 | 223 | | await task; |
| | | 224 | | |
| | | 225 | | // remove from the queue. |
| | | 226 | | try |
| | 7 | 227 | | { |
| | 7 | 228 | | _tilesInProgressLock.EnterUpgradeableReadLock(); |
| | | 229 | | |
| | 7 | 230 | | if (_tilesInProgress.ContainsKey((profile.Name, tileId))) |
| | 7 | 231 | | { |
| | | 232 | | try |
| | 7 | 233 | | { |
| | 7 | 234 | | _tilesInProgressLock.EnterWriteLock(); |
| | | 235 | | |
| | 7 | 236 | | _tilesInProgress.Remove((profile.Name, tileId)); |
| | 7 | 237 | | } |
| | | 238 | | finally |
| | 7 | 239 | | { |
| | 7 | 240 | | _tilesInProgressLock.ExitWriteLock(); |
| | 7 | 241 | | } |
| | 7 | 242 | | } |
| | 7 | 243 | | } |
| | | 244 | | finally |
| | 7 | 245 | | { |
| | 7 | 246 | | _tilesInProgressLock.ExitUpgradeableReadLock(); |
| | 7 | 247 | | } |
| | 7 | 248 | | } |
| | | 249 | | |
| | | 250 | | internal RoutingNetworkIslandManager Clone() |
| | 257 | 251 | | { |
| | | 252 | | try |
| | 257 | 253 | | { |
| | 257 | 254 | | _islandsLock.EnterReadLock(); |
| | | 255 | | |
| | 257 | 256 | | var islands = new Dictionary<string, Islands>(); |
| | 771 | 257 | | foreach (var (profileName, profileIslands) in _islands) |
| | 0 | 258 | | { |
| | 0 | 259 | | islands[profileName] = profileIslands.Clone(); |
| | 0 | 260 | | } |
| | | 261 | | |
| | 257 | 262 | | return new RoutingNetworkIslandManager(this.MaxIslandSize, islands); |
| | | 263 | | } |
| | | 264 | | finally |
| | 257 | 265 | | { |
| | 257 | 266 | | _islandsLock.ExitReadLock(); |
| | 257 | 267 | | } |
| | 257 | 268 | | } |
| | | 269 | | } |