Skip to content
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

IGNITE-17830 .NET: Fix Guid serialization to match UUID #1187

Merged
merged 4 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,8 @@ public void packUuid(UUID val) {

packExtensionTypeHeader(ClientMsgPackType.UUID, 16);

buf.writeLong(val.getMostSignificantBits());
buf.writeLong(val.getLeastSignificantBits());
buf.writeLongLE(val.getMostSignificantBits());
buf.writeLongLE(val.getLeastSignificantBits());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ public UUID unpackUuid() {
throw new MessageSizeException("Expected 16 bytes for UUID extension, but got " + len, len);
}

return new UUID(buf.readLong(), buf.readLong());
return new UUID(buf.readLongLE(), buf.readLongLE());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,8 @@ public async Task TestAllSupportedArgTypes()
await Test(BigInteger.Pow(1234, 56));

await Test(Guid.Empty);

// TODO IGNITE-17830 String representation should be the same in Java and C#.
await Test(
new Guid(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }), "07080506-0102-0304-100f-0e0d0c0b0a09");
await Test(new Guid(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }));
await Test(Guid.NewGuid());

async Task Test(object val, string? expectedStr = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MessagePackExtensionsTest
/** Byte representation of the UUID above, serialized by Java ClientMessagePacker. */
private static readonly sbyte[] JavaUuidBytes =
{
-40, 3, 111, 36, 20, 106, 36, 74, 64, 24, -93, 108, 62, -100, -11, -76, 32, -126
-40, 3, 24, 64, 74, 36, 106, 20, 36, 111, -126, 32, -76, -11, -100, 62, 108, -93
};

private static readonly string?[] TestStrings =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ public static void Write(Guid guid, Span<byte> span)
var written = guid.TryWriteBytes(span);
Debug.Assert(written, "written");

// Reverse endianness of the first part.
var a = BinaryPrimitives.ReverseEndianness(MemoryMarshal.Read<int>(span));
MemoryMarshal.Write(span, ref a);
// Reverse first part order: abc -> cba. Parts are little-endian on any system.
var a = MemoryMarshal.Read<int>(span);
var b = MemoryMarshal.Read<short>(span[4..]);
var c = MemoryMarshal.Read<short>(span[6..]);

var b = BinaryPrimitives.ReverseEndianness(MemoryMarshal.Read<short>(span[4..]));
MemoryMarshal.Write(span[4..], ref b);
MemoryMarshal.Write(span[4..8], ref a);
MemoryMarshal.Write(span[2..4], ref b);
MemoryMarshal.Write(span[..2], ref c);

var c = BinaryPrimitives.ReverseEndianness(MemoryMarshal.Read<short>(span[6..]));
MemoryMarshal.Write(span[6..], ref c);
// Reverse second part order: defghijk -> kjihgfed.
span[8..16].Reverse();
}

/// <summary>
Expand All @@ -55,18 +57,18 @@ public static void Write(Guid guid, Span<byte> span)
/// <returns>Guid.</returns>
public static Guid Read(ReadOnlySpan<byte> span)
{
// Hoist bounds checks.
var k = span[15];
var a = BinaryPrimitives.ReverseEndianness(MemoryMarshal.Read<int>(span));
var b = BinaryPrimitives.ReverseEndianness(MemoryMarshal.Read<short>(span[4..]));
var c = BinaryPrimitives.ReverseEndianness(MemoryMarshal.Read<short>(span[6..]));
var d = span[8];
var e = span[9];
var f = span[10];
var g = span[11];
var h = span[12];
var i = span[13];
var j = span[14];
var d = span[15];
var e = span[14];
var f = span[13];
var g = span[12];
var h = span[11];
var i = span[10];
var j = span[9];
var k = span[8];

var a = BinaryPrimitives.ReadInt32LittleEndian(span[4..8]);
var b = BinaryPrimitives.ReadInt16LittleEndian(span[2..4]);
var c = BinaryPrimitives.ReadInt16LittleEndian(span[..2]);

return new Guid(a, b, c, d, e, f, g, h, i, j, k);
}
Expand Down