From 3e21ef57453090a86e2de0babd5dfd25ef9140fc Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 26 Dec 2017 22:37:15 +0000 Subject: [PATCH] [server] Update inactive chunk unloading to tie in with the main saving API --- Nibriboard/RippleSpace/Plane.cs | 17 ++++++++++------- Nibriboard/RippleSpace/RippleSpaceManager.cs | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Nibriboard/RippleSpace/Plane.cs b/Nibriboard/RippleSpace/Plane.cs index 8ff6c09..6ef79b7 100644 --- a/Nibriboard/RippleSpace/Plane.cs +++ b/Nibriboard/RippleSpace/Plane.cs @@ -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 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 Save() diff --git a/Nibriboard/RippleSpace/RippleSpaceManager.cs b/Nibriboard/RippleSpace/RippleSpaceManager.cs index a237446..87b20cf 100644 --- a/Nibriboard/RippleSpace/RippleSpaceManager.cs +++ b/Nibriboard/RippleSpace/RippleSpaceManager.cs @@ -131,7 +131,7 @@ namespace Nibriboard.RippleSpace Stopwatch maintenanceStopwatch = Stopwatch.StartNew(); foreach (Plane plane in Planes) - plane.PerformMaintenance(); + await plane.PerformMaintenance(); LastMaintenanceDuration = maintenanceStopwatch.ElapsedMilliseconds;