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,24 +82,30 @@ 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}");
using StreamReader nodesMapFileStream = new(Path.Join(ImportFolderPath, NodesMapRegionFileName), Encoding.ASCII); if (nodeRegionDict is null)
while (!nodesMapFileStream.EndOfStream)
{ {
string? line = nodesMapFileStream.ReadLine(); nodeRegionDict = new();
if(line is null) using StreamReader nodesMapFileStream = new(Path.Join(ImportFolderPath, NodesMapRegionFileName), Encoding.ASCII);
continue; while (!nodesMapFileStream.EndOfStream)
try
{ {
ulong id = ulong.Parse(line.Split('-')[0]); string? line = nodesMapFileStream.ReadLine();
if (id == nodeId) if(line is null)
return LoadRegionFromRegionId(long.Parse(line.Split('-')[1])); continue;
} try
catch (Exception e) {
{ ulong id = ulong.Parse(line.Split('-')[0]);
logger?.LogError(e, "Error parsing Node-line."); long regionId = long.Parse(line.Split('-')[1]);
nodeRegionDict.TryAdd(id, regionId);
}
catch (Exception e)
{
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;
} }