Dispose of Region-filestream

This commit is contained in:
glax 2023-04-09 16:22:22 +02:00
parent 58d1031524
commit fc5d388ecd
2 changed files with 20 additions and 24 deletions

View File

@ -59,16 +59,4 @@ public class Region
else return null;
}
public static Region? FromFile(string filePath)
{
if (File.Exists(filePath))
return JsonSerializer.Deserialize<Region>(new FileStream(filePath, FileMode.Open), serializerOptions)!;
else return null;
}
public static Region? FromId(string path, ulong regionId)
{
string filePath = Path.Join(path, $"{regionId}.region");
return FromFile(filePath);
}
}

View File

@ -1,3 +1,4 @@
using System.Text.Json;
using OSMDatastructure;
using OSMDatastructure.Graph;
@ -24,7 +25,7 @@ namespace OSMImporter
return value;
else
{
Region? loadedRegion = LoadRegion(id);
Region? loadedRegion = RegionFromId(id);
if(loadedRegion is not null)
_regions.Add(loadedRegion!.regionHash, value: loadedRegion);
return loadedRegion;
@ -36,17 +37,6 @@ namespace OSMImporter
return this._regions.Values.ToArray();
}
private Region? LoadRegion(Coordinates coordinates)
{
return LoadRegion(Coordinates.GetRegionHashCode(coordinates));
}
private Region? LoadRegion(ulong id)
{
Console.WriteLine($"Load Region {id}");
return Region.FromId(workingDirectory, id);
}
public OsmNode? GetNode(Coordinates coordinates)
{
Region? regionWithNode = GetRegion(coordinates);
@ -60,5 +50,23 @@ namespace OSMImporter
Region? r = GetRegion(regionId);
return r?.GetNode(nodeId);
}
private Region? RegionFromFile(string filePath)
{
Region? retRegion = null;
if (File.Exists(filePath))
{
FileStream regionFile = new FileStream(filePath, FileMode.Open);
retRegion = JsonSerializer.Deserialize<Region>(regionFile, Region.serializerOptions)!;
regionFile.Dispose();
}
return retRegion;
}
private Region? RegionFromId(ulong regionId)
{
string filePath = Path.Join(workingDirectory, $"{regionId}.region");
return RegionFromFile(filePath);
}
}
}