1
0
Fork 0
mirror of https://github.com/sbrl/Nibriboard.git synced 2018-01-10 21:33:49 +00:00

[server] Update inactive chunk unloading to tie in with the main saving API

This commit is contained in:
Starbeamrainbowlabs 2017-12-26 22:37:15 +00:00
parent f2aafece24
commit 3e21ef5745
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 11 additions and 8 deletions

View file

@ -268,31 +268,34 @@ namespace Nibriboard.RippleSpace
return chunk.Remove(targetLineUniqueId); return chunk.Remove(targetLineUniqueId);
} }
public void PerformMaintenance() public async Task PerformMaintenance()
{ {
// Be lazy and don't bother to perform maintenance if it's not needed // Be lazy and don't bother to perform maintenance if it's not needed
if(LoadedChunks < SoftLoadedChunkLimit || if(LoadedChunks < SoftLoadedChunkLimit ||
UnloadableChunks < MinUnloadeableChunks) UnloadableChunks < MinUnloadeableChunks)
return; return;
int unloadedChunks = 0;
foreach(KeyValuePair<ChunkReference, Chunk> chunkEntry in loadedChunkspace) foreach(KeyValuePair<ChunkReference, Chunk> chunkEntry in loadedChunkspace)
{ {
if(!chunkEntry.Value.CouldUnload) if(!chunkEntry.Value.CouldUnload)
continue; continue;
// This chunk has been inactive for a while - let's serialise it and save it to disk // This chunk has been inactive for a while - let's serialise it and save it to disk
Stream chunkSerializationSink = new FileStream( Stream chunkSerializationSink = File.Open(
Path.Combine(StorageDirectory, chunkEntry.Key.AsFilepath()), Path.Combine(StorageDirectory, chunkEntry.Key.AsFilepath()),
FileMode.Create, FileMode.Create
FileAccess.Write,
FileShare.None
); );
IFormatter binaryFormatter = new BinaryFormatter(); await chunkEntry.Value.SaveTo(chunkSerializationSink);
binaryFormatter.Serialize(chunkSerializationSink, chunkEntry.Value);
// Remove the chunk from the loaded chunkspace // Remove the chunk from the loaded chunkspace
loadedChunkspace.Remove(chunkEntry.Key); loadedChunkspace.Remove(chunkEntry.Key);
unloadedChunks++;
} }
if (unloadedChunks > 0)
Log.WriteLine($"[RippleSpace/{Name}] Unloaded {unloadedChunks} inactive chunks.");
} }
public async Task<long> Save() public async Task<long> Save()

View file

@ -131,7 +131,7 @@ namespace Nibriboard.RippleSpace
Stopwatch maintenanceStopwatch = Stopwatch.StartNew(); Stopwatch maintenanceStopwatch = Stopwatch.StartNew();
foreach (Plane plane in Planes) foreach (Plane plane in Planes)
plane.PerformMaintenance(); await plane.PerformMaintenance();
LastMaintenanceDuration = maintenanceStopwatch.ElapsedMilliseconds; LastMaintenanceDuration = maintenanceStopwatch.ElapsedMilliseconds;