< Summary

Class:Itinero.Network.Search.Islands.RoutingNetworkIslandManager
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Network/Search/Islands/RoutingNetworkIslandManager.cs
Covered lines:24
Uncovered lines:63
Coverable lines:87
Total lines:148
Line coverage:27.5% (24 of 87)
Covered branches:1
Total branches:8
Branch coverage:12.5% (1 of 8)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
.ctor(...)100%1100%
get_MaxIslandSize()100%1100%
TryGetIslandsFor(...)100%10%
GetIslandsFor(...)0%20%
BuildForTileAsync()0%40%
Clone()50%276.92%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Network/Search/Islands/RoutingNetworkIslandManager.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Diagnostics.CodeAnalysis;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using Itinero.Profiles;
 7
 8namespace Itinero.Network.Search.Islands;
 9
 10internal class RoutingNetworkIslandManager
 11{
 29912    private readonly Dictionary<(string profile, uint tile), Task> _tilesInProgress = new();
 29913    private readonly ReaderWriterLockSlim _tilesInProgressLock = new();
 14    private readonly Dictionary<string, Islands> _islands;
 29915    private readonly ReaderWriterLockSlim _islandsLock = new();
 16
 17717    internal RoutingNetworkIslandManager(int maxIslandSize)
 17718    {
 17719        this.MaxIslandSize = maxIslandSize;
 17720        _islands = new();
 17721    }
 22
 12223    private RoutingNetworkIslandManager(int maxIslandSize, Dictionary<string, Islands> islands)
 12224    {
 12225        this.MaxIslandSize = maxIslandSize;
 12226        _islands = islands;
 12227    }
 28
 25729    internal int MaxIslandSize { get; }
 30
 31    internal bool TryGetIslandsFor(string profileName, out Islands islands)
 032    {
 33        try
 034        {
 035            _islandsLock.EnterReadLock();
 36
 037            return _islands.TryGetValue(profileName, out islands);
 38        }
 39        finally
 040        {
 041            _islandsLock.ExitReadLock();
 042        }
 043    }
 44
 45    internal Islands GetIslandsFor(Profile profile)
 046    {
 47        try
 048        {
 049            _islandsLock.EnterUpgradeableReadLock();
 50
 051            if (_islands.TryGetValue(profile.Name, out var islands)) return islands;
 52
 53            try
 054            {
 055                _islandsLock.EnterWriteLock();
 56
 057                islands = new Islands();
 058                _islands[profile.Name] = islands;
 059                return islands;
 60            }
 61            finally
 062            {
 063                _islandsLock.ExitWriteLock();
 064            }
 65        }
 66        finally
 067        {
 068            _islandsLock.ExitUpgradeableReadLock();
 069        }
 070    }
 71
 72    internal async Task BuildForTileAsync(RoutingNetwork network, Profile profile, uint tileId,
 73        CancellationToken cancellationToken)
 074    {
 75        // queue task, if not done yet.
 76        Task task;
 77        try
 078        {
 079            _tilesInProgressLock.EnterUpgradeableReadLock();
 80
 081            if (!_tilesInProgress.TryGetValue((profile.Name, tileId), out task))
 082            {
 83                try
 084                {
 085                    _tilesInProgressLock.EnterWriteLock();
 86
 087                    task = IslandBuilder.BuildForTileAsync(network, profile, tileId, cancellationToken);
 088                    _tilesInProgress[(profile.Name, tileId)] = task;
 089                }
 90                finally
 091                {
 092                    _tilesInProgressLock.ExitWriteLock();
 093                }
 094            }
 095        }
 96        finally
 097        {
 098            _tilesInProgressLock.ExitUpgradeableReadLock();
 099        }
 100
 101        // await the task.
 0102        await task;
 103
 104        // remove from the queue.
 105        try
 0106        {
 0107            _tilesInProgressLock.EnterUpgradeableReadLock();
 108
 0109            if (_tilesInProgress.ContainsKey((profile.Name, tileId)))
 0110            {
 111                try
 0112                {
 0113                    _tilesInProgressLock.EnterWriteLock();
 114
 0115                    _tilesInProgress.Remove((profile.Name, tileId));
 0116                }
 117                finally
 0118                {
 0119                    _tilesInProgressLock.ExitWriteLock();
 0120                }
 0121            }
 0122        }
 123        finally
 0124        {
 0125            _tilesInProgressLock.ExitUpgradeableReadLock();
 0126        }
 0127    }
 128
 129    internal RoutingNetworkIslandManager Clone()
 122130    {
 131        try
 122132        {
 122133            _islandsLock.EnterReadLock();
 134
 122135            var islands = new Dictionary<string, Islands>();
 366136            foreach (var (profileName, profileIslands) in _islands)
 0137            {
 0138                islands[profileName] = profileIslands.Clone();
 0139            }
 140
 122141            return new RoutingNetworkIslandManager(this.MaxIslandSize, islands);
 142        }
 143        finally
 122144        {
 122145            _islandsLock.ExitReadLock();
 122146        }
 122147    }
 148}