This commit is contained in:
StellaOps Bot
2025-11-24 07:49:18 +02:00
parent bb709b643e
commit 5970f0d9bd
16 changed files with 690 additions and 14 deletions

View File

@@ -0,0 +1,49 @@
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Hosting;
namespace StellaOps.Policy.Engine.Overlay;
/// <summary>
/// Persists overlay projections as NDJSON files under overlay/{tenant}/{ruleId}/{version}.ndjson.
/// </summary>
internal sealed class FileOverlayStore : IOverlayStore
{
private readonly string _root;
public FileOverlayStore(IHostEnvironment env)
{
_root = Path.Combine(env.ContentRootPath, "overlay");
}
public async Task SaveAsync(OverlayProjection projection, CancellationToken cancellationToken = default)
{
if (projection is null)
{
throw new ArgumentNullException(nameof(projection));
}
var tenant = Sanitize(projection.Tenant);
var rule = Sanitize(projection.RuleId);
var dir = Path.Combine(_root, tenant, rule);
Directory.CreateDirectory(dir);
var path = Path.Combine(dir, $"{projection.Version}.ndjson");
var lines = new[]
{
JsonSerializer.Serialize(new OverlayProjectionHeader("overlay-projection-v1")),
JsonSerializer.Serialize(projection)
};
await File.WriteAllLinesAsync(path, lines, Encoding.UTF8, cancellationToken).ConfigureAwait(false);
}
private static string Sanitize(string value)
{
foreach (var ch in Path.GetInvalidFileNameChars())
{
value = value.Replace(ch, '_');
}
return string.IsNullOrWhiteSpace(value) ? "unknown" : value;
}
}