| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Threading; |
| | | 4 | | using Itinero.Indexes; |
| | | 5 | | using Itinero.Profiles; |
| | | 6 | | |
| | | 7 | | namespace Itinero; |
| | | 8 | | |
| | | 9 | | public sealed partial class RouterDb |
| | | 10 | | { |
| | | 11 | | private readonly AttributeSetIndex _edgeTypeIndex; |
| | | 12 | | private readonly AttributeSetIndex _turnCostTypeIndex; |
| | | 13 | | private readonly AttributeSetMap _turnCostTypeMap; |
| | | 14 | | |
| | 14335 | 15 | | 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) |
| | 0 | 23 | | { |
| | 0 | 24 | | return _edgeTypeIndex.GetById(id); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The edge type map. |
| | | 29 | | /// </summary> |
| | 4281782 | 30 | | internal AttributeSetMap EdgeTypeMap { get; set; } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the edge type count. |
| | | 34 | | /// </summary> |
| | 0 | 35 | | 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() |
| | 228606 | 56 | | { |
| | 228606 | 57 | | var map = this.EdgeTypeMap; |
| | 228606 | 58 | | 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. |
| | 456723 | 62 | | if (cached != null && ReferenceEquals(cached.Map, map)) return (cached.Id, cached.Func); |
| | | 63 | | |
| | 489 | 64 | | var built = new MapCache |
| | 489 | 65 | | { |
| | 489 | 66 | | Id = map.Id, |
| | 489 | 67 | | Map = map, |
| | 54172 | 68 | | Func = a => _edgeTypeIndex.Get(map.Map(a)), |
| | 489 | 69 | | }; |
| | 489 | 70 | | Volatile.Write(ref _edgeTypeMapCache, built); |
| | | 71 | | |
| | 489 | 72 | | return (built.Id, built.Func); |
| | 228606 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets the turn cost type count. |
| | | 77 | | /// </summary> |
| | 0 | 78 | | 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) |
| | 0 | 86 | | { |
| | 0 | 87 | | return _turnCostTypeIndex.GetById(id); |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | private MapCache? _turnCostTypeMapCache; |
| | | 91 | | |
| | | 92 | | internal (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) GetTurnCostTypeMap() |
| | 7646 | 93 | | { |
| | 7646 | 94 | | var map = _turnCostTypeMap; |
| | 7646 | 95 | | var cached = Volatile.Read(ref _turnCostTypeMapCache); |
| | 15112 | 96 | | if (cached != null && ReferenceEquals(cached.Map, map)) return (cached.Id, cached.Func); |
| | | 97 | | |
| | 180 | 98 | | var built = new MapCache |
| | 180 | 99 | | { |
| | 180 | 100 | | Id = map.Id, |
| | 180 | 101 | | Map = map, |
| | 10274 | 102 | | Func = a => _turnCostTypeIndex.Get(map.Map(a)), |
| | 180 | 103 | | }; |
| | 180 | 104 | | Volatile.Write(ref _turnCostTypeMapCache, built); |
| | | 105 | | |
| | 180 | 106 | | return (built.Id, built.Func); |
| | 7646 | 107 | | } |
| | | 108 | | } |