| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics.CodeAnalysis; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Itinero.Profiles; |
| | | 7 | | |
| | | 8 | | namespace Itinero.Network.Search.Islands; |
| | | 9 | | |
| | | 10 | | internal class RoutingNetworkIslandManager |
| | | 11 | | { |
| | 408 | 12 | | private readonly Dictionary<(string profile, uint tile), Task> _tilesInProgress = new(); |
| | 408 | 13 | | private readonly ReaderWriterLockSlim _tilesInProgressLock = new(); |
| | | 14 | | private readonly Dictionary<string, Islands> _islands; |
| | 408 | 15 | | private readonly Dictionary<string, IslandDirectedGraph> _directedGraphs = new(); |
| | 408 | 16 | | private readonly ReaderWriterLockSlim _islandsLock = new(); |
| | | 17 | | |
| | 236 | 18 | | internal RoutingNetworkIslandManager(int maxIslandSize) |
| | 236 | 19 | | { |
| | 236 | 20 | | this.MaxIslandSize = maxIslandSize; |
| | 236 | 21 | | _islands = new(); |
| | 236 | 22 | | } |
| | | 23 | | |
| | 172 | 24 | | private RoutingNetworkIslandManager(int maxIslandSize, Dictionary<string, Islands> islands) |
| | 172 | 25 | | { |
| | 172 | 26 | | this.MaxIslandSize = maxIslandSize; |
| | 172 | 27 | | _islands = islands; |
| | 172 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Checks if an edge is on an island using the directed graph. |
| | | 32 | | /// Returns true if island, false if not island, null if inconclusive. |
| | | 33 | | /// </summary> |
| | | 34 | | internal bool? IsEdgeOnIsland(Profile profile, EdgeId edgeId) => |
| | 16 | 35 | | this.IsEdgeOnIsland(profile.Name, edgeId); |
| | | 36 | | |
| | | 37 | | internal bool? IsEdgeOnIsland(string profileName, EdgeId edgeId) |
| | 16 | 38 | | { |
| | | 39 | | try |
| | 16 | 40 | | { |
| | 16 | 41 | | _islandsLock.EnterReadLock(); |
| | | 42 | | |
| | 16 | 43 | | if (!_directedGraphs.TryGetValue(profileName, out var dg)) |
| | 2 | 44 | | return null; |
| | | 45 | | |
| | 14 | 46 | | if (!_islands.TryGetValue(profileName, out var profileIslands)) |
| | 0 | 47 | | return null; |
| | 14 | 48 | | if (profileIslands.IsEdgeOnIsland(edgeId)) |
| | 0 | 49 | | return true; |
| | | 50 | | |
| | 14 | 51 | | if (dg.IsNotIsland(edgeId)) |
| | 12 | 52 | | return false; |
| | | 53 | | |
| | 2 | 54 | | return null; |
| | | 55 | | } |
| | | 56 | | finally |
| | 16 | 57 | | { |
| | 16 | 58 | | _islandsLock.ExitReadLock(); |
| | 16 | 59 | | } |
| | 16 | 60 | | } |
| | | 61 | | |
| | | 62 | | internal IslandDirectedGraph GetOrCreateDirectedGraph(Profile profile) |
| | 30 | 63 | | { |
| | | 64 | | try |
| | 30 | 65 | | { |
| | 30 | 66 | | _islandsLock.EnterUpgradeableReadLock(); |
| | | 67 | | |
| | 51 | 68 | | if (_directedGraphs.TryGetValue(profile.Name, out var dg)) return dg; |
| | | 69 | | |
| | | 70 | | try |
| | 9 | 71 | | { |
| | 9 | 72 | | _islandsLock.EnterWriteLock(); |
| | | 73 | | |
| | 9 | 74 | | dg = new IslandDirectedGraph(); |
| | 9 | 75 | | _directedGraphs[profile.Name] = dg; |
| | 9 | 76 | | return dg; |
| | | 77 | | } |
| | | 78 | | finally |
| | 9 | 79 | | { |
| | 9 | 80 | | _islandsLock.ExitWriteLock(); |
| | 9 | 81 | | } |
| | | 82 | | } |
| | | 83 | | finally |
| | 30 | 84 | | { |
| | 30 | 85 | | _islandsLock.ExitUpgradeableReadLock(); |
| | 30 | 86 | | } |
| | 30 | 87 | | } |
| | | 88 | | |
| | 341 | 89 | | internal int MaxIslandSize { get; } |
| | | 90 | | |
| | | 91 | | internal bool TryGetIslandsFor(string profileName, out Islands islands) |
| | 0 | 92 | | { |
| | | 93 | | try |
| | 0 | 94 | | { |
| | 0 | 95 | | _islandsLock.EnterReadLock(); |
| | | 96 | | |
| | 0 | 97 | | return _islands.TryGetValue(profileName, out islands); |
| | | 98 | | } |
| | | 99 | | finally |
| | 0 | 100 | | { |
| | 0 | 101 | | _islandsLock.ExitReadLock(); |
| | 0 | 102 | | } |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | internal Islands GetIslandsFor(Profile profile) |
| | 35 | 106 | | { |
| | | 107 | | try |
| | 35 | 108 | | { |
| | 35 | 109 | | _islandsLock.EnterUpgradeableReadLock(); |
| | | 110 | | |
| | 61 | 111 | | if (_islands.TryGetValue(profile.Name, out var islands)) return islands; |
| | | 112 | | |
| | | 113 | | try |
| | 9 | 114 | | { |
| | 9 | 115 | | _islandsLock.EnterWriteLock(); |
| | | 116 | | |
| | 9 | 117 | | islands = new Islands(); |
| | 9 | 118 | | _islands[profile.Name] = islands; |
| | 9 | 119 | | return islands; |
| | | 120 | | } |
| | | 121 | | finally |
| | 9 | 122 | | { |
| | 9 | 123 | | _islandsLock.ExitWriteLock(); |
| | 9 | 124 | | } |
| | | 125 | | } |
| | | 126 | | finally |
| | 35 | 127 | | { |
| | 35 | 128 | | _islandsLock.ExitUpgradeableReadLock(); |
| | 35 | 129 | | } |
| | 35 | 130 | | } |
| | | 131 | | |
| | | 132 | | internal async Task BuildForTileAsync(RoutingNetwork network, Profile profile, uint tileId, |
| | | 133 | | CancellationToken cancellationToken) |
| | 0 | 134 | | { |
| | | 135 | | // queue task, if not done yet. |
| | | 136 | | Task task; |
| | | 137 | | try |
| | 0 | 138 | | { |
| | 0 | 139 | | _tilesInProgressLock.EnterUpgradeableReadLock(); |
| | | 140 | | |
| | 0 | 141 | | if (!_tilesInProgress.TryGetValue((profile.Name, tileId), out task)) |
| | 0 | 142 | | { |
| | | 143 | | try |
| | 0 | 144 | | { |
| | 0 | 145 | | _tilesInProgressLock.EnterWriteLock(); |
| | | 146 | | |
| | 0 | 147 | | task = IslandBuilder.BuildForTileAsync(network, profile, tileId, cancellationToken); |
| | 0 | 148 | | _tilesInProgress[(profile.Name, tileId)] = task; |
| | 0 | 149 | | } |
| | | 150 | | finally |
| | 0 | 151 | | { |
| | 0 | 152 | | _tilesInProgressLock.ExitWriteLock(); |
| | 0 | 153 | | } |
| | 0 | 154 | | } |
| | 0 | 155 | | } |
| | | 156 | | finally |
| | 0 | 157 | | { |
| | 0 | 158 | | _tilesInProgressLock.ExitUpgradeableReadLock(); |
| | 0 | 159 | | } |
| | | 160 | | |
| | | 161 | | // await the task. |
| | 0 | 162 | | await task; |
| | | 163 | | |
| | | 164 | | // remove from the queue. |
| | | 165 | | try |
| | 0 | 166 | | { |
| | 0 | 167 | | _tilesInProgressLock.EnterUpgradeableReadLock(); |
| | | 168 | | |
| | 0 | 169 | | if (_tilesInProgress.ContainsKey((profile.Name, tileId))) |
| | 0 | 170 | | { |
| | | 171 | | try |
| | 0 | 172 | | { |
| | 0 | 173 | | _tilesInProgressLock.EnterWriteLock(); |
| | | 174 | | |
| | 0 | 175 | | _tilesInProgress.Remove((profile.Name, tileId)); |
| | 0 | 176 | | } |
| | | 177 | | finally |
| | 0 | 178 | | { |
| | 0 | 179 | | _tilesInProgressLock.ExitWriteLock(); |
| | 0 | 180 | | } |
| | 0 | 181 | | } |
| | 0 | 182 | | } |
| | | 183 | | finally |
| | 0 | 184 | | { |
| | 0 | 185 | | _tilesInProgressLock.ExitUpgradeableReadLock(); |
| | 0 | 186 | | } |
| | 0 | 187 | | } |
| | | 188 | | |
| | | 189 | | internal RoutingNetworkIslandManager Clone() |
| | 172 | 190 | | { |
| | | 191 | | try |
| | 172 | 192 | | { |
| | 172 | 193 | | _islandsLock.EnterReadLock(); |
| | | 194 | | |
| | 172 | 195 | | var islands = new Dictionary<string, Islands>(); |
| | 516 | 196 | | foreach (var (profileName, profileIslands) in _islands) |
| | 0 | 197 | | { |
| | 0 | 198 | | islands[profileName] = profileIslands.Clone(); |
| | 0 | 199 | | } |
| | | 200 | | |
| | 172 | 201 | | return new RoutingNetworkIslandManager(this.MaxIslandSize, islands); |
| | | 202 | | } |
| | | 203 | | finally |
| | 172 | 204 | | { |
| | 172 | 205 | | _islandsLock.ExitReadLock(); |
| | 172 | 206 | | } |
| | 172 | 207 | | } |
| | | 208 | | } |