fix: Fixes serializing strings and uses memory mapped files for loading (#1133)
This commit is contained in:
parent
bc746dbf14
commit
2ae762904f
5 changed files with 363 additions and 324 deletions
|
|
@ -15,44 +15,91 @@
|
|||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.MemoryMappedFiles;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Server
|
||||
namespace Server;
|
||||
|
||||
public static class AdhocPersistence
|
||||
{
|
||||
public static class AdhocPersistence
|
||||
/**
|
||||
* Serializes to memory synchronously. Optional buffer can be provided.
|
||||
* Note: The buffer may not be the same after returning from the function if more data is written
|
||||
* than the initial buffer can handle.
|
||||
*/
|
||||
public static BufferWriter Serialize(Action<IGenericWriter> serializer)
|
||||
{
|
||||
public static void Serialize(string filePath, Action<IGenericWriter> serializer)
|
||||
{
|
||||
var fullPath = Path.Combine(Core.BaseDirectory, filePath);
|
||||
var file = new FileInfo(fullPath);
|
||||
file.Directory?.Create();
|
||||
var saveBuffer = new BufferWriter(true);
|
||||
serializer(saveBuffer);
|
||||
return saveBuffer;
|
||||
}
|
||||
|
||||
using var bin = new BinaryFileWriter(fullPath, true);
|
||||
serializer(bin);
|
||||
/**
|
||||
* Writes a buffer to disk. This function should be called asynchronously.
|
||||
*/
|
||||
public static void WriteSnapshot(string filePath, Span<byte> buffer)
|
||||
{
|
||||
var fullPath = PathUtility.GetFullPath(filePath, Core.BaseDirectory);
|
||||
var file = new FileInfo(fullPath);
|
||||
PathUtility.EnsureDirectory(file.DirectoryName);
|
||||
|
||||
using var fs = new FileStream(fullPath, FileMode.Create, FileAccess.Write);
|
||||
fs.Write(buffer);
|
||||
}
|
||||
|
||||
public static void SerializeAndSnapshot(string filePath, Action<IGenericWriter> serializer)
|
||||
{
|
||||
var saveBuffer = Serialize(serializer);
|
||||
Task.Run(() => { WriteSnapshot(filePath, saveBuffer.Buffer.AsSpan(0, (int)saveBuffer.Position)); });
|
||||
}
|
||||
|
||||
public static void Deserialize(string filePath, Action<IGenericReader> deserializer)
|
||||
{
|
||||
var fullPath = PathUtility.GetFullPath(filePath, Core.BaseDirectory);
|
||||
var file = new FileInfo(fullPath);
|
||||
|
||||
if (!file.Exists)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public static void Deserialize(string filePath, Action<IGenericReader> deserializer)
|
||||
var fileLength = file.Length;
|
||||
if (fileLength == 0)
|
||||
{
|
||||
var fullPath = Path.Combine(Core.BaseDirectory, filePath);
|
||||
var file = new FileInfo(fullPath);
|
||||
file.Directory?.Create();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!file.Exists)
|
||||
{
|
||||
return;
|
||||
}
|
||||
string error;
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
using var mmf = MemoryMappedFile.CreateFromFile(fullPath, FileMode.Open);
|
||||
using var stream = mmf.CreateViewStream();
|
||||
using var br = new BinaryFileReader(stream);
|
||||
deserializer(br);
|
||||
|
||||
error = br.Position != fileLength
|
||||
? $"Serialized {fileLength} bytes, but {br.Position} bytes deserialized"
|
||||
: null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
error = e.ToString();
|
||||
}
|
||||
|
||||
if (error != null)
|
||||
{
|
||||
Console.WriteLine($"***** Bad deserialize of {file.FullName} *****");
|
||||
Console.WriteLine(error);
|
||||
|
||||
Console.WriteLine("Skip this file and continue? (y/n)");
|
||||
|
||||
var pressedKey = Console.ReadKey(true).Key;
|
||||
|
||||
if (pressedKey != ConsoleKey.Y)
|
||||
{
|
||||
using FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
using var br = new BinaryFileReader(fs);
|
||||
deserializer(br);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Utility.PushColor(ConsoleColor.Red);
|
||||
Console.WriteLine($"***** Bad deserialize of {file.FullName} *****");
|
||||
Console.WriteLine(e.ToString());
|
||||
Utility.PopColor();
|
||||
throw new Exception("Deserialization failed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue