compress parsed data to .json.zst

This commit is contained in:
2026-09-23 18:25:15 +02:00
parent c6b27cd3c9
commit 55f7d453b1
9 changed files with 67 additions and 35 deletions
@@ -7,12 +7,14 @@ public class BackgroundJobManager
{
private readonly ILogger _parentLogger;
private readonly Config.SaveUploadConfig _uploadConfig;
private readonly int _zstdCompressionLevel;
private long _lastJobId;
public BackgroundJobManager(ILogger logger, Config.SaveUploadConfig uploadConfig)
public BackgroundJobManager(ILogger logger, Config.SaveUploadConfig uploadConfig, int zstdCompressionLevel)
{
_parentLogger = logger;
_uploadConfig = uploadConfig;
_zstdCompressionLevel = zstdCompressionLevel;
}
public SaveParsingOperation StartNewParsingOperation(
@@ -21,7 +23,7 @@ public class BackgroundJobManager
long nextId = Interlocked.Increment(ref _lastJobId);
var contextLogger = new ContextLogger($"BackgroundJob-{nextId}", _parentLogger);
var op = new SaveParsingOperation(nextId, meta, filter,
_uploadConfig.MaxSizePlainTextBytes, contextLogger, ct);
_uploadConfig.MaxSizePlainTextBytes, _zstdCompressionLevel, contextLogger, ct);
op.StartAsync();
return op;
}
@@ -3,6 +3,7 @@ using System.Text.Encodings.Web;
using System.Text.Json.Serialization;
using ParadoxSaveParser.WebAPI.Database;
using ParadoxSaveParser.WebAPI.SaveDataFilters;
using ZstdSharp;
namespace ParadoxSaveParser.WebAPI.BackgroundTasks;
@@ -13,6 +14,7 @@ public class SaveParsingOperation
private readonly SaveFileMetadata _meta;
private readonly ISaveDataFilter _filter;
private readonly long _maxGameDataBytes;
private readonly int _zstdCompressionLevel;
private readonly ContextLogger _logger;
private readonly CancellationToken _ct;
@@ -25,12 +27,13 @@ public class SaveParsingOperation
};
public SaveParsingOperation(long operationId, SaveFileMetadata meta, ISaveDataFilter filter,
long maxGameDataBytes, ContextLogger logger, CancellationToken ct)
long maxGameDataBytes, int zstdCompressionLevel, ContextLogger logger, CancellationToken ct)
{
OperationId = operationId;
_meta = meta;
_filter = filter;
_maxGameDataBytes = maxGameDataBytes;
_zstdCompressionLevel = zstdCompressionLevel;
_logger = logger;
_ct = ct;
}
@@ -90,7 +93,11 @@ public class SaveParsingOperation
await Program.DB.UpdateMetadataStatus(_meta, SaveFileProcessingStatus.SavingResults);
var resultFilePath = _meta.GetParsedDataPath();
await using (var resultFile = File.OpenWrite(resultFilePath))
await JsonSerializer.SerializeAsync(resultFile, parsedData, _jsonOptions, _ct);
{
// the zstd frame is finished only when compression stream is disposed, so it must happen before Done
await using var zstd = new CompressionStream(resultFile, _zstdCompressionLevel);
await JsonSerializer.SerializeAsync(zstd, parsedData, _jsonOptions, _ct);
}
await Program.DB.UpdateMetadataStatus(_meta, SaveFileProcessingStatus.Done);
}
}