Cache nodeRegionMap

This commit is contained in:
glax 2024-07-25 00:52:25 +02:00
parent 7833eb5e83
commit edc0cd01f4

View File

@ -12,6 +12,7 @@ public class RegionLoader(float regionSize, string? importFolderPath = null, ILo
internal readonly string ImportFolderPath = Path.Join(importFolderPath ?? Environment.CurrentDirectory, regionSize.ToString(CultureInfo.InvariantCulture)); internal readonly string ImportFolderPath = Path.Join(importFolderPath ?? Environment.CurrentDirectory, regionSize.ToString(CultureInfo.InvariantCulture));
private const string NodesMapRegionFileName = "nodes.map"; private const string NodesMapRegionFileName = "nodes.map";
private const string WayMapRegionFileName = "ways.map"; private const string WayMapRegionFileName = "ways.map";
private Dictionary<ulong, long>? nodeRegionDict = null;
public Graph.Graph? LoadRegionFromRegionId(long regionId) public Graph.Graph? LoadRegionFromRegionId(long regionId)
{ {
@ -81,6 +82,9 @@ public class RegionLoader(float regionSize, string? importFolderPath = null, ILo
public Graph.Graph? LoadRegionFromNodeId(ulong nodeId) public Graph.Graph? LoadRegionFromNodeId(ulong nodeId)
{ {
logger?.LogDebug($"Loading Region for Node {nodeId}"); logger?.LogDebug($"Loading Region for Node {nodeId}");
if (nodeRegionDict is null)
{
nodeRegionDict = new();
using StreamReader nodesMapFileStream = new(Path.Join(ImportFolderPath, NodesMapRegionFileName), Encoding.ASCII); using StreamReader nodesMapFileStream = new(Path.Join(ImportFolderPath, NodesMapRegionFileName), Encoding.ASCII);
while (!nodesMapFileStream.EndOfStream) while (!nodesMapFileStream.EndOfStream)
{ {
@ -90,15 +94,18 @@ public class RegionLoader(float regionSize, string? importFolderPath = null, ILo
try try
{ {
ulong id = ulong.Parse(line.Split('-')[0]); ulong id = ulong.Parse(line.Split('-')[0]);
if (id == nodeId) long regionId = long.Parse(line.Split('-')[1]);
return LoadRegionFromRegionId(long.Parse(line.Split('-')[1])); nodeRegionDict.TryAdd(id, regionId);
} }
catch (Exception e) catch (Exception e)
{ {
logger?.LogError(e, "Error parsing Node-line."); logger?.LogError(e, "Error parsing Node-line.");
} }
} }
}
if (!nodeRegionDict.TryGetValue(nodeId, out long rId))
return LoadRegionFromRegionId(rId);
logger?.LogWarning($"Could not find Node {nodeId}"); logger?.LogWarning($"Could not find Node {nodeId}");
return null; return null;
} }