Exemple of byte[] entry #227
Answered
by
guillaume-chervet
guillaume-chervet
asked this question in
Q&A
-
hi @sakno , Again dotnext Cluster is awesome and thank you for that. Do you have a sample how ? using System.Text;
using DotNext.IO;
using DotNext.Runtime.Serialization;
using DotNext.Text;
namespace RaftNode;
public struct AddKeyValueCommand : ISerializable<AddKeyValueCommand>
{
public const int Id = 2;
public string Key { get; set; }
public string Value { get; set; }
long? IDataTransferObject.Length => sizeof(int) + sizeof(int);
public async ValueTask WriteToAsync<TWriter>(TWriter writer, CancellationToken token)
where TWriter : notnull, IAsyncBinaryWriter
{
var command = this;
await writer.EncodeAsync(command.Key.AsMemory(), new EncodingContext(Encoding.UTF8, false),
LengthFormat.LittleEndian, token).ConfigureAwait(false);
await writer.EncodeAsync(command.Value.AsMemory(), new EncodingContext(Encoding.UTF8, false),
LengthFormat.LittleEndian, token).ConfigureAwait(false);
}
#pragma warning disable CA2252
public static async ValueTask<AddKeyValueCommand> ReadFromAsync<TReader>(TReader reader, CancellationToken token)
#pragma warning restore CA2252
where TReader : notnull, IAsyncBinaryReader
{
var key = await reader
.DecodeAsync(new DecodingContext(Encoding.UTF8, false), LengthFormat.LittleEndian, token: token)
.ConfigureAwait(false);
var value = await reader.DecodeAsync(new DecodingContext(Encoding.UTF8, false), LengthFormat.LittleEndian, token:token).ConfigureAwait(false);
return new AddKeyValueCommand
{
Key = key.ToString(),
Value = value.ToString()
};
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
guillaume-chervet
Mar 13, 2024
Replies: 1 comment
-
I found this sample in unittest : so cool :) private readonly struct RaftLogEntry : IRaftLogEntry
{
private readonly ReadOnlyMemory<byte> content;
private readonly bool knownLength;
internal RaftLogEntry(long term, byte[] content, bool knownLength)
{
Term = term;
Timestamp = DateTimeOffset.UtcNow;
this.content = content;
this.knownLength = knownLength;
}
public long Term { get; }
public DateTimeOffset Timestamp { get; }
bool IDataTransferObject.IsReusable => true;
long? IDataTransferObject.Length => knownLength ? content.Length : null;
ValueTask IDataTransferObject.WriteToAsync<TWriter>(TWriter writer, CancellationToken token)
=> writer.WriteAsync(content, null, token);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
guillaume-chervet
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found this sample in unittest :
so cool :)