-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from rabbitmq/add_lz4
Implement lz4 codec example
- Loading branch information
Showing
4 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Sub Entry compress codecs | ||
--- | ||
|
||
By default the client implements only `None` and `Gzip` see [here](https://github.com/rabbitmq/rabbitmq-stream-dotnet-client#sub-entries-batching) for more details. | ||
|
||
|
||
You need to implement `ICompressionCodec` interface to create a new codec. | ||
Here you can find all the missing implementations: | ||
- [lz4](./lz4) | ||
- Snappy not implemented yet | ||
- Zstd not implemented yet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Lz4 Compress codec | ||
--- | ||
|
||
You need to add https://github.com/MiloszKrajewski/K4os.Compression.LZ4 as dependency | ||
|
||
|
||
How to use: | ||
|
||
- Register the codec | ||
```csharp | ||
StreamCompressionCodecs.RegisterCodec<StreamLz4Codec>(CompressionType.Lz4); | ||
``` | ||
|
||
- Send messages: | ||
```csharp | ||
var messages = new List<Message>(); | ||
... | ||
await producerLog.Send(1, messages, CompressionType.Lz4); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters