< Summary

Class:Itinero.RouterDb
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/RouterDb.cs
/home/runner/work/routing2/routing2/src/Itinero/RouterDb.Indexes.cs
/home/runner/work/routing2/routing2/src/Itinero/RouterDb.Meta.cs
/home/runner/work/routing2/routing2/src/Itinero/RouterDb.Mutation.cs
/home/runner/work/routing2/routing2/src/Itinero/RouterDb.Serialization.cs
Covered lines:91
Uncovered lines:14
Coverable lines:105
Total lines:306
Line coverage:86.6% (91 of 105)
Covered branches:20
Total branches:24
Branch coverage:83.3% (20 of 24)
Tag:284_38118444584

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%6100%
.ctor(...)50%287.5%
get_Latest()100%1100%
get_ProfileConfiguration()100%1100%
GetEdgeType(...)100%10%
get_EdgeTypeMap()100%1100%
get_EdgeTypeCount()100%10%
GetEdgeTypeMap()100%4100%
get_TurnCostTypeCount()100%10%
GetTurnCostType(...)100%10%
GetTurnCostTypeMap()100%4100%
get_Meta()100%1100%
ReadAttributesFrom(...)50%271.42%
WriteAttributesTo(...)100%1100%
get_HasMutableNetwork()100%1100%
GetMutableNetwork()50%280%
SetAsMutable(...)50%283.33%
Itinero.Network.Mutation.IRouterDbMutable.Finish(...)100%1100%
ReadFrom(...)100%2100%
WriteTo(...)100%1100%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/RouterDb.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.IO;
 3using System.Linq;
 4using System.Runtime.CompilerServices;
 5using Itinero.Data;
 6using Itinero.Data.Usage;
 7using Itinero.Indexes;
 8using Itinero.IO;
 9using Itinero.Network;
 10using Itinero.Network.Mutation;
 11using Itinero.Network.Serialization;
 12using Itinero.Profiles;
 13
 14[assembly: InternalsVisibleTo("Itinero.Tests")]
 15[assembly: InternalsVisibleTo("Itinero.Tests.Benchmarks")]
 16[assembly: InternalsVisibleTo("Itinero.Tests.Functional")]
 17
 18namespace Itinero;
 19
 20/// <summary>
 21/// Represents a router db.
 22/// </summary>
 23/// <remarks>
 24/// This is mostly a wrapper around a RoutingNetwork.
 25/// If an update happens to the underlying routing network, this update will be presented here atomically.
 26/// In other words, to do route planning, use routerDb.getLatest() to get a consistent routing network
 27/// </remarks>
 28public sealed partial class RouterDb : IRouterDbMutable
 29{
 30    /// <summary>
 31    /// Creates a new router db.
 32    /// </summary>
 33    /// <param name="configuration">The configuration.</param>
 50934    public RouterDb(RouterDbConfiguration? configuration = null)
 50935    {
 50936        configuration ??= RouterDbConfiguration.Default();
 37
 50938        this.Latest = new RoutingNetwork(this, configuration.Zoom, configuration.MaxIslandSize);
 50939        _edgeTypeIndex = configuration.EdgeTypeIndex;
 50940        this.EdgeTypeMap = configuration.EdgeTypeMap ?? AttributeSetMap.Default();
 50941        _turnCostTypeIndex = configuration.TurnCostTypeIndex;
 50942        _turnCostTypeMap = configuration.TurnCostTypeMap ?? AttributeSetMap.Default();
 43
 50944        this.ProfileConfiguration = new RouterDbProfileConfiguration();
 50945    }
 46
 147    private RouterDb(Stream stream, RouterDbReadSettings settings)
 148    {
 49        // check version #.
 150        var version = stream.ReadVarInt32();
 151        if (version != 1)
 052        {
 053            throw new InvalidDataException("Unknown version #.");
 54        }
 55
 56        // read network.
 157        this.Latest = stream.ReadFrom(this);
 58
 59        // read edge type map data.
 160        _edgeTypeIndex = settings.EdgeTypeAttributeSetIndex;
 161        _edgeTypeIndex.ReadFrom(stream);
 162        _turnCostTypeIndex = settings.TurnCostAttributeSetIndex;
 163        _turnCostTypeIndex.ReadFrom(stream);
 64
 65        // read attributes.
 166        this.Meta = new List<(string key, string value)>(this.ReadAttributesFrom(stream));
 67
 168        this.EdgeTypeMap = AttributeSetMap.Default();
 169        _turnCostTypeMap = AttributeSetMap.Default();
 170        this.ProfileConfiguration = new RouterDbProfileConfiguration();
 171    }
 72
 73    /// <summary>
 74    /// Gets the latest.
 75    /// </summary>
 557976    public RoutingNetwork Latest { get; private set; }
 77}

/home/runner/work/routing2/routing2/src/Itinero/RouterDb.Indexes.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Threading;
 4using Itinero.Indexes;
 5using Itinero.Profiles;
 6
 7namespace Itinero;
 8
 9public sealed partial class RouterDb
 10{
 11    private readonly AttributeSetIndex _edgeTypeIndex;
 12    private readonly AttributeSetIndex _turnCostTypeIndex;
 13    private readonly AttributeSetMap _turnCostTypeMap;
 14
 1433515    internal RouterDbProfileConfiguration ProfileConfiguration { get; }
 16
 17    /// <summary>
 18    /// Gets the edge type attributes for the given id.
 19    /// </summary>
 20    /// <param name="id">The id.</param>
 21    /// <returns>The attributes.</returns>
 22    public IEnumerable<(string key, string value)> GetEdgeType(uint id)
 023    {
 024        return _edgeTypeIndex.GetById(id);
 025    }
 26
 27    /// <summary>
 28    /// The edge type map.
 29    /// </summary>
 428178230    internal AttributeSetMap EdgeTypeMap { get; set; }
 31
 32    /// <summary>
 33    /// Gets the edge type count.
 34    /// </summary>
 035    public long EdgeTypeCount => _edgeTypeIndex.Count;
 36
 37    /// <summary>
 38    /// Gets the turn attributes for the given type.
 39    /// </summary>
 40    /// <returns>The attributes.</returns>
 41    // Built fresh on every call before, and GetTileForRead calls this per tile access.
 42    private sealed class MapCache
 43    {
 44        public required Guid Id;
 45        public required Func<IEnumerable<(string key, string value)>, uint> Func;
 46        public required AttributeSetMap Map;
 47    }
 48
 49    private MapCache? _edgeTypeMapCache;
 50
 51    /// <summary>
 52    /// Gets the turn attributes for the given type.
 53    /// </summary>
 54    /// <returns>The attributes.</returns>
 55    internal (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) GetEdgeTypeMap()
 22860656    {
 22860657        var map = this.EdgeTypeMap;
 22860658        var cached = Volatile.Read(ref _edgeTypeMapCache);
 59
 60        // On the instance, not the id: EdgeTypeMap is settable and a new instance with the same
 61        // id would otherwise keep serving the old closure.
 45672362        if (cached != null && ReferenceEquals(cached.Map, map)) return (cached.Id, cached.Func);
 63
 48964        var built = new MapCache
 48965        {
 48966            Id = map.Id,
 48967            Map = map,
 5417268            Func = a => _edgeTypeIndex.Get(map.Map(a)),
 48969        };
 48970        Volatile.Write(ref _edgeTypeMapCache, built);
 71
 48972        return (built.Id, built.Func);
 22860673    }
 74
 75    /// <summary>
 76    /// Gets the turn cost type count.
 77    /// </summary>
 078    public long TurnCostTypeCount => _turnCostTypeIndex.Count;
 79
 80    /// <summary>
 81    /// Gets the turn attributes for the given type.
 82    /// </summary>
 83    /// <param name="id">The id or index.</param>
 84    /// <returns>The attributes.</returns>
 85    public IEnumerable<(string key, string value)> GetTurnCostType(uint id)
 086    {
 087        return _turnCostTypeIndex.GetById(id);
 088    }
 89
 90    private MapCache? _turnCostTypeMapCache;
 91
 92    internal (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) GetTurnCostTypeMap()
 764693    {
 764694        var map = _turnCostTypeMap;
 764695        var cached = Volatile.Read(ref _turnCostTypeMapCache);
 1511296        if (cached != null && ReferenceEquals(cached.Map, map)) return (cached.Id, cached.Func);
 97
 18098        var built = new MapCache
 18099        {
 180100            Id = map.Id,
 180101            Map = map,
 10274102            Func = a => _turnCostTypeIndex.Get(map.Map(a)),
 180103        };
 180104        Volatile.Write(ref _turnCostTypeMapCache, built);
 105
 180106        return (built.Id, built.Func);
 7646107    }
 108}

/home/runner/work/routing2/routing2/src/Itinero/RouterDb.Meta.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.IO;
 3using System.Linq;
 4using Itinero.Network.Attributes;
 5
 6namespace Itinero;
 7
 8public sealed partial class RouterDb
 9{
 10    /// <summary>
 11    /// Gets or sets the meta-data attributes list.
 12    /// </summary>
 51813    public List<(string key, string value)> Meta { get; set; } = new();
 14
 15    private IEnumerable<(string key, string value)> ReadAttributesFrom(Stream stream)
 116    {
 117        var ver = stream.ReadByte();
 118        if (ver == 0)
 019        {
 020            return Enumerable.Empty<(string key, string value)>();
 21        }
 22
 123        return stream.ReadAttributesFrom();
 124    }
 25
 26    private void WriteAttributesTo(Stream stream)
 127    {
 128        stream.WriteByte(1);
 29
 130        this.Meta.WriteAttributesTo(stream);
 131    }
 32}

/home/runner/work/routing2/routing2/src/Itinero/RouterDb.Mutation.cs

#LineLine coverage
 1using System;
 2using Itinero.Network;
 3using Itinero.Network.Mutation;
 4
 5namespace Itinero;
 6
 7public sealed partial class RouterDb
 8{
 9    private RoutingNetworkMutator? _mutable;
 10
 11    /// <summary>
 12    /// Returns true if there is already a writer.
 13    /// </summary>
 4714    public bool HasMutableNetwork => _mutable != null;
 15
 16    /// <summary>
 17    /// Gets a mutable version of the latest network.
 18    /// </summary>
 19    /// <returns>The mutable version.</returns>
 20    public RoutingNetworkMutator GetMutableNetwork()
 33121    {
 33122        if (_mutable != null) throw new InvalidOperationException($"Only one mutable version is allowed at one time." +
 023                                                $"Check {nameof(this.HasMutableNetwork)} to check for a current mutable.
 24
 33125        return this.Latest.GetAsMutable();
 33126    }
 27
 28    internal RoutingNetworkMutator SetAsMutable(RoutingNetworkMutator mutable)
 33429    {
 33430        if (_mutable != null) throw new InvalidOperationException($"Only one mutable version is allowed at one time." +
 031                                                                  $"Check {nameof(this.HasMutableNetwork)} to check for 
 32
 33433        _mutable = mutable;
 33434        return _mutable;
 33435    }
 36
 37    void IRouterDbMutable.Finish(RoutingNetwork newNetwork)
 33138    {
 33139        this.Latest = newNetwork;
 40
 33141        _mutable = null;
 33142    }
 43}

/home/runner/work/routing2/routing2/src/Itinero/RouterDb.Serialization.cs

#LineLine coverage
 1using System.IO;
 2using Itinero.IO;
 3using Itinero.Network.Serialization;
 4
 5namespace Itinero;
 6
 7public sealed partial class RouterDb
 8{
 9    /// <summary>
 10    /// Reads a router db from the given stream.
 11    /// </summary>
 12    /// <param name="stream">The stream.</param>
 13    /// <param name="settings">The settings.</param>
 14    /// <returns>The router db as read from the stream.</returns>
 15    public static RouterDb ReadFrom(Stream stream, RouterDbReadSettings? settings = null)
 116    {
 117        settings ??= new RouterDbReadSettings();
 18
 119        return new(stream, settings);
 120    }
 21
 22    /// <summary>
 23    /// Writes the router db to the given stream.
 24    /// </summary>
 25    /// <param name="stream">The stream.</param>
 26    public void WriteTo(Stream stream)
 127    {
 28        // write version #.
 129        stream.WriteVarInt32(1);
 30
 31        // write network.
 132        using (var mutable = this.GetMutableNetwork())
 133        {
 134            mutable.WriteTo(stream);
 135        }
 36
 37        // write edge type map data.
 138        _edgeTypeIndex.WriteTo(stream);
 39
 40        // write turn cost type map data.
 141        _turnCostTypeIndex.WriteTo(stream);
 42
 43        // write attributes.
 144        this.WriteAttributesTo(stream);
 145    }
 46}