feat(batch15): complete group 6 msgblock/consumerfilestore

This commit is contained in:
Joseph Doherty
2026-02-28 18:34:28 -05:00
parent 854f410aad
commit cfddfc9084
10 changed files with 720 additions and 45 deletions

View File

@@ -1,4 +1,5 @@
using System.Text.Json;
using IronSnappy;
namespace ZB.MOM.NatsNet.Server;
@@ -51,4 +52,38 @@ public static class StoreEnumExtensions
ArgumentNullException.ThrowIfNull(b);
UnmarshalJSON(ref alg, b.AsSpan());
}
public static (byte[]? Buffer, Exception? Error) Compress(this StoreCompression alg, byte[] buf)
{
ArgumentNullException.ThrowIfNull(buf);
const int checksumSize = FileStoreDefaults.RecordHashSize;
if (buf.Length < checksumSize)
return (null, new InvalidDataException("uncompressed buffer is too short"));
return alg switch
{
StoreCompression.NoCompression => (buf, null),
StoreCompression.S2Compression => CompressS2(buf, checksumSize),
_ => (null, new InvalidOperationException("compression algorithm not known")),
};
}
private static (byte[]? Buffer, Exception? Error) CompressS2(byte[] buf, int checksumSize)
{
try
{
var bodyLength = buf.Length - checksumSize;
var compressedBody = Snappy.Encode(buf.AsSpan(0, bodyLength));
var output = new byte[compressedBody.Length + checksumSize];
Buffer.BlockCopy(compressedBody, 0, output, 0, compressedBody.Length);
Buffer.BlockCopy(buf, bodyLength, output, compressedBody.Length, checksumSize);
return (output, null);
}
catch (Exception ex)
{
return (null, new IOException("error writing to compression writer", ex));
}
}
}