-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sub batch publish #57
Conversation
Codecov Report
@@ Coverage Diff @@
## main #57 +/- ##
==========================================
+ Coverage 87.87% 88.83% +0.96%
==========================================
Files 53 55 +2
Lines 2779 3153 +374
Branches 115 131 +16
==========================================
+ Hits 2442 2801 +359
- Misses 304 316 +12
- Partials 33 36 +3
Continue to review full report at Codecov.
|
A complete Lz4 Codec using https://github.com/MiloszKrajewski/K4os.Compression.LZ4: public class StreamLz4Codec : ICompressionCodec
{
private ReadOnlySequence<byte> compressedReadOnlySequence;
private static int WriteUInt32(Span<byte> span, uint value)
{
BinaryPrimitives.WriteUInt32BigEndian(span, value);
return 4;
}
private static int Write(Span<byte> span, ReadOnlySequence<byte> msg)
{
msg.CopyTo(span);
return (int) msg.Length;
}
public void Compress(List<Message> messages)
{
MessagesCount = messages.Count;
UnCompressedSize = messages.Sum(msg => 4 + msg.Size);
var messagesSource = new Span<byte>(new byte[UnCompressedSize]);
var offset = 0;
foreach (var msg in messages)
{
offset += WriteUInt32(messagesSource.Slice(offset), (uint) msg.Size);
offset += msg.Write(messagesSource.Slice(offset));
}
using var source = new MemoryStream(messagesSource.ToArray());
using var destination = new MemoryStream();
var settings = new LZ4EncoderSettings
{
ChainBlocks = false
};
using (var target = LZ4Stream.Encode(destination, settings, false))
{
source.CopyTo(target);
}
compressedReadOnlySequence = new ReadOnlySequence<byte>(destination.ToArray());
}
public int Write(Span<byte> span)
{
return Write(span, compressedReadOnlySequence);
}
public int CompressedSize => (int) compressedReadOnlySequence.Length;
public int UnCompressedSize { get; private set; }
public int MessagesCount { get; private set; }
public CompressionType CompressionType => CompressionType.Lz4;
public ReadOnlySequence<byte> UnCompress(ReadOnlySequence<byte> source, uint dataLen, uint unCompressedDataSize)
{
using var target = new MemoryStream();
using (var sourceDecode = LZ4Stream.Decode(new MemoryStream(source.ToArray())))
{
sourceDecode.CopyTo(target);
}
return new ReadOnlySequence<byte>(target.ToArray());
}
} Then: StreamCompressionCodecs.RegisterCodec<StreamLz4Codec>(CompressionType.Lz4); |
The current implementation supports:
GZIP
compressionNone
compressionPerformances look good (my
2,8 GHz Quad-Core Intel Core i7
mac)Consumer usage does not change.
Note:
The limit for sub-batching messages is
ushort
so it is not possible to store more than65535
messages at time.In this case the
publish/3
method will raise an exception:see the tests:
Add custom codec
You can implement the
ICompressionCodec
interface, with something like:then register the codec for the specific compress type
See: #57 (comment)