| | 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 | | { |
| 299 | 12 | | private readonly Dictionary<(string profile, uint tile), Task> _tilesInProgress = new(); |
| 299 | 13 | | private readonly ReaderWriterLockSlim _tilesInProgressLock = new(); |
| | 14 | | private readonly Dictionary<string, Islands> _islands; |
| 299 | 15 | | private readonly ReaderWriterLockSlim _islandsLock = new(); |
| | 16 | |
|
| 177 | 17 | | internal RoutingNetworkIslandManager(int maxIslandSize) |
| 177 | 18 | | { |
| 177 | 19 | | this.MaxIslandSize = maxIslandSize; |
| 177 | 20 | | _islands = new(); |
| 177 | 21 | | } |
| | 22 | |
|
| 122 | 23 | | private RoutingNetworkIslandManager(int maxIslandSize, Dictionary<string, Islands> islands) |
| 122 | 24 | | { |
| 122 | 25 | | this.MaxIslandSize = maxIslandSize; |
| 122 | 26 | | _islands = islands; |
| 122 | 27 | | } |
| | 28 | |
|
| 257 | 29 | | internal int MaxIslandSize { get; } |
| | 30 | |
|
| | 31 | | internal bool TryGetIslandsFor(string profileName, out Islands islands) |
| 0 | 32 | | { |
| | 33 | | try |
| 0 | 34 | | { |
| 0 | 35 | | _islandsLock.EnterReadLock(); |
| | 36 | |
|
| 0 | 37 | | return _islands.TryGetValue(profileName, out islands); |
| | 38 | | } |
| | 39 | | finally |
| 0 | 40 | | { |
| 0 | 41 | | _islandsLock.ExitReadLock(); |
| 0 | 42 | | } |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | internal Islands GetIslandsFor(Profile profile) |
| 0 | 46 | | { |
| | 47 | | try |
| 0 | 48 | | { |
| 0 | 49 | | _islandsLock.EnterUpgradeableReadLock(); |
| | 50 | |
|
| 0 | 51 | | if (_islands.TryGetValue(profile.Name, out var islands)) return islands; |
| | 52 | |
|
| | 53 | | try |
| 0 | 54 | | { |
| 0 | 55 | | _islandsLock.EnterWriteLock(); |
| | 56 | |
|
| 0 | 57 | | islands = new Islands(); |
| 0 | 58 | | _islands[profile.Name] = islands; |
| 0 | 59 | | return islands; |
| | 60 | | } |
| | 61 | | finally |
| 0 | 62 | | { |
| 0 | 63 | | _islandsLock.ExitWriteLock(); |
| 0 | 64 | | } |
| | 65 | | } |
| | 66 | | finally |
| 0 | 67 | | { |
| 0 | 68 | | _islandsLock.ExitUpgradeableReadLock(); |
| 0 | 69 | | } |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | internal async Task BuildForTileAsync(RoutingNetwork network, Profile profile, uint tileId, |
| | 73 | | CancellationToken cancellationToken) |
| 0 | 74 | | { |
| | 75 | | // queue task, if not done yet. |
| | 76 | | Task task; |
| | 77 | | try |
| 0 | 78 | | { |
| 0 | 79 | | _tilesInProgressLock.EnterUpgradeableReadLock(); |
| | 80 | |
|
| 0 | 81 | | if (!_tilesInProgress.TryGetValue((profile.Name, tileId), out task)) |
| 0 | 82 | | { |
| | 83 | | try |
| 0 | 84 | | { |
| 0 | 85 | | _tilesInProgressLock.EnterWriteLock(); |
| | 86 | |
|
| 0 | 87 | | task = IslandBuilder.BuildForTileAsync(network, profile, tileId, cancellationToken); |
| 0 | 88 | | _tilesInProgress[(profile.Name, tileId)] = task; |
| 0 | 89 | | } |
| | 90 | | finally |
| 0 | 91 | | { |
| 0 | 92 | | _tilesInProgressLock.ExitWriteLock(); |
| 0 | 93 | | } |
| 0 | 94 | | } |
| 0 | 95 | | } |
| | 96 | | finally |
| 0 | 97 | | { |
| 0 | 98 | | _tilesInProgressLock.ExitUpgradeableReadLock(); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | // await the task. |
| 0 | 102 | | await task; |
| | 103 | |
|
| | 104 | | // remove from the queue. |
| | 105 | | try |
| 0 | 106 | | { |
| 0 | 107 | | _tilesInProgressLock.EnterUpgradeableReadLock(); |
| | 108 | |
|
| 0 | 109 | | if (_tilesInProgress.ContainsKey((profile.Name, tileId))) |
| 0 | 110 | | { |
| | 111 | | try |
| 0 | 112 | | { |
| 0 | 113 | | _tilesInProgressLock.EnterWriteLock(); |
| | 114 | |
|
| 0 | 115 | | _tilesInProgress.Remove((profile.Name, tileId)); |
| 0 | 116 | | } |
| | 117 | | finally |
| 0 | 118 | | { |
| 0 | 119 | | _tilesInProgressLock.ExitWriteLock(); |
| 0 | 120 | | } |
| 0 | 121 | | } |
| 0 | 122 | | } |
| | 123 | | finally |
| 0 | 124 | | { |
| 0 | 125 | | _tilesInProgressLock.ExitUpgradeableReadLock(); |
| 0 | 126 | | } |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | internal RoutingNetworkIslandManager Clone() |
| 122 | 130 | | { |
| | 131 | | try |
| 122 | 132 | | { |
| 122 | 133 | | _islandsLock.EnterReadLock(); |
| | 134 | |
|
| 122 | 135 | | var islands = new Dictionary<string, Islands>(); |
| 366 | 136 | | foreach (var (profileName, profileIslands) in _islands) |
| 0 | 137 | | { |
| 0 | 138 | | islands[profileName] = profileIslands.Clone(); |
| 0 | 139 | | } |
| | 140 | |
|
| 122 | 141 | | return new RoutingNetworkIslandManager(this.MaxIslandSize, islands); |
| | 142 | | } |
| | 143 | | finally |
| 122 | 144 | | { |
| 122 | 145 | | _islandsLock.ExitReadLock(); |
| 122 | 146 | | } |
| 122 | 147 | | } |
| | 148 | | } |