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);
}
public void PerformMaintenance()
public async Task PerformMaintenance()
{
// Be lazy and don't bother to perform maintenance if it's not needed
if(LoadedChunks < SoftLoadedChunkLimit ||
UnloadableChunks < MinUnloadeableChunks)
return;
int unloadedChunks = 0;
foreach(KeyValuePair<ChunkReference, Chunk> chunkEntry in loadedChunkspace)
{
if(!chunkEntry.Value.CouldUnload)
continue;
// 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()),
FileMode.Create,
FileAccess.Write,
FileShare.None
FileMode.Create
);
IFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(chunkSerializationSink, chunkEntry.Value);
await chunkEntry.Value.SaveTo(chunkSerializationSink);
// Remove the chunk from the loaded chunkspace
loadedChunkspace.Remove(chunkEntry.Key);
unloadedChunks++;
}
if (unloadedChunks > 0)
Log.WriteLine($"[RippleSpace/{Name}] Unloaded {unloadedChunks} inactive chunks.");
}
public async Task<long> Save()

View file

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