From 95fe8bea23b9a1f4979623788b078b0f3f42aa7e Mon Sep 17 00:00:00 2001 From: nzdev <834725+nzdev@users.noreply.github.com> Date: Tue, 24 Nov 2020 10:09:27 +1300 Subject: [PATCH 1/5] Serialize more primitives --- .../NuCache/DataSource/SerializerBase.cs | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs index 8b02946fc2..e6ae789702 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs @@ -4,7 +4,7 @@ using CSharpTest.Net.Serialization; namespace Umbraco.Web.PublishedCache.NuCache.DataSource { - internal abstract class SerializerBase + public abstract class SerializerBase { private const char PrefixNull = 'N'; private const char PrefixString = 'S'; @@ -18,6 +18,12 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource private const char PrefixByte = 'O'; private const char PrefixByteArray = 'A'; private const char PrefixCompressedStringByteArray = 'C'; + private const char PrefixSignedByte = 'E'; + private const char PrefixBool = 'M'; + private const char PrefixGuid = 'G'; + private const char PrefixTimeSpan = 'T'; + private const char PrefixInt16 = 'Q'; + private const char PrefixChar = 'R'; protected string ReadString(Stream stream) => PrimitiveSerializer.String.ReadFrom(stream); protected int ReadInt(Stream stream) => PrimitiveSerializer.Int32.ReadFrom(stream); @@ -86,6 +92,18 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource return PrimitiveSerializer.DateTime.ReadFrom(stream); case PrefixByteArray: return PrimitiveSerializer.Bytes.ReadFrom(stream); + case PrefixSignedByte: + return PrimitiveSerializer.SByte.ReadFrom(stream); + case PrefixBool: + return PrimitiveSerializer.Boolean.ReadFrom(stream); + case PrefixGuid: + return PrimitiveSerializer.Guid.ReadFrom(stream); + case PrefixTimeSpan: + return PrimitiveSerializer.TimeSpan.ReadFrom(stream); + case PrefixInt16: + return PrimitiveSerializer.Int16.ReadFrom(stream); + case PrefixChar: + return PrimitiveSerializer.Char.ReadFrom(stream); case PrefixCompressedStringByteArray: return new LazyCompressedString(PrimitiveSerializer.Bytes.ReadFrom(stream)); default: @@ -157,6 +175,36 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource PrimitiveSerializer.Char.WriteTo(PrefixCompressedStringByteArray, stream); PrimitiveSerializer.Bytes.WriteTo(lazyCompressedString.GetBytes(), stream); } + else if (value is sbyte signedByteValue) + { + PrimitiveSerializer.Char.WriteTo(PrefixSignedByte, stream); + PrimitiveSerializer.SByte.WriteTo(signedByteValue, stream); + } + else if (value is bool boolValue) + { + PrimitiveSerializer.Char.WriteTo(PrefixBool, stream); + PrimitiveSerializer.Boolean.WriteTo(boolValue, stream); + } + else if (value is Guid guidValue) + { + PrimitiveSerializer.Char.WriteTo(PrefixGuid, stream); + PrimitiveSerializer.Guid.WriteTo(guidValue, stream); + } + else if (value is TimeSpan timespanValue) + { + PrimitiveSerializer.Char.WriteTo(PrefixTimeSpan, stream); + PrimitiveSerializer.TimeSpan.WriteTo(timespanValue, stream); + } + else if (value is short int16Value) + { + PrimitiveSerializer.Char.WriteTo(PrefixInt16, stream); + PrimitiveSerializer.Int16.WriteTo(int16Value, stream); + } + else if (value is char charValue) + { + PrimitiveSerializer.Char.WriteTo(PrefixChar, stream); + PrimitiveSerializer.Char.WriteTo(charValue, stream); + } else throw new NotSupportedException("Value type " + value.GetType().FullName + " cannot be serialized."); } From e8195bfff05af636d15e350ec39a9b49091d5b6f Mon Sep 17 00:00:00 2001 From: nzdev <834725+nzdev@users.noreply.github.com> Date: Tue, 24 Nov 2020 10:10:38 +1300 Subject: [PATCH 2/5] fix deserialization order --- .../MsgPackContentNestedDataSerializer.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/MsgPackContentNestedDataSerializer.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/MsgPackContentNestedDataSerializer.cs index aad337b236..42468ad930 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/MsgPackContentNestedDataSerializer.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/MsgPackContentNestedDataSerializer.cs @@ -48,7 +48,13 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource public ContentCacheDataModel Deserialize(int contentTypeId, string stringData, byte[] byteData) { - if (stringData != null) + if (byteData != null) + { + var content = MessagePackSerializer.Deserialize(byteData, _options); + Expand(contentTypeId, content); + return content; + } + else if (stringData != null) { // NOTE: We don't really support strings but it's possible if manually used (i.e. tests) var bin = Convert.FromBase64String(stringData); @@ -56,12 +62,6 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource Expand(contentTypeId, content); return content; } - else if (byteData != null) - { - var content = MessagePackSerializer.Deserialize(byteData, _options); - Expand(contentTypeId, content); - return content; - } else { return null; From f09fcfbdd4ed4ef9584ab7f4982a09c3fa15da74 Mon Sep 17 00:00:00 2001 From: nzdev <834725+nzdev@users.noreply.github.com> Date: Fri, 27 Nov 2020 14:45:44 +1300 Subject: [PATCH 3/5] Fix serialization of decompressed lazy strings --- .../NuCache/DataSource/LazyCompressedString.cs | 9 ++++++++- .../NuCache/DataSource/SerializerBase.cs | 12 ++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/LazyCompressedString.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/LazyCompressedString.cs index 3e0e796d36..99e6f44af7 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/LazyCompressedString.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/LazyCompressedString.cs @@ -13,6 +13,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource private byte[] _bytes; private string _str; private readonly object _locker; + private bool _isDecompressed; /// /// Constructor @@ -22,7 +23,8 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource { _locker = new object(); _bytes = bytes; - _str = null; + _str = null; + _isDecompressed = false; } public byte[] GetBytes() @@ -31,6 +33,10 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource throw new InvalidOperationException("The bytes have already been expanded"); return _bytes; } + public bool IsDecompressed() + { + return _isDecompressed; + } public override string ToString() { @@ -41,6 +47,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource if (_bytes == null) throw new PanicException("Bytes have already been cleared"); _str = Encoding.UTF8.GetString(LZ4Pickler.Unpickle(_bytes)); _bytes = null; + _isDecompressed = true; } return _str; } diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs index e6ae789702..f7e2f32f5d 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs @@ -172,8 +172,16 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource } else if (value is LazyCompressedString lazyCompressedString) { - PrimitiveSerializer.Char.WriteTo(PrefixCompressedStringByteArray, stream); - PrimitiveSerializer.Bytes.WriteTo(lazyCompressedString.GetBytes(), stream); + if (lazyCompressedString.IsDecompressed()) + { + PrimitiveSerializer.Char.WriteTo(PrefixString, stream); + PrimitiveSerializer.String.WriteTo(lazyCompressedString, stream); + } + else + { + PrimitiveSerializer.Char.WriteTo(PrefixCompressedStringByteArray, stream); + PrimitiveSerializer.Bytes.WriteTo(lazyCompressedString.GetBytes(), stream); + } } else if (value is sbyte signedByteValue) { From f8bb53ac0338d41241659a9771d7e076bd2891c3 Mon Sep 17 00:00:00 2001 From: nzdev <834725+nzdev@users.noreply.github.com> Date: Wed, 16 Dec 2020 19:20:24 +1300 Subject: [PATCH 4/5] changed isdecompressed to a property. Changed serializerbase to internal --- .../NuCache/DataSource/LazyCompressedString.cs | 9 +++++---- .../PublishedCache/NuCache/DataSource/SerializerBase.cs | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/LazyCompressedString.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/LazyCompressedString.cs index 99e6f44af7..ea904b0cf4 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/LazyCompressedString.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/LazyCompressedString.cs @@ -33,10 +33,11 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource throw new InvalidOperationException("The bytes have already been expanded"); return _bytes; } - public bool IsDecompressed() - { - return _isDecompressed; - } + /// + /// Whether the bytes have been decompressed to a string. If true calling GetBytes() will throw InvalidOperationException. + /// + public bool IsDecompressed => _isDecompressed; + public override string ToString() { diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs index f7e2f32f5d..9e6baed4fe 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs @@ -4,7 +4,7 @@ using CSharpTest.Net.Serialization; namespace Umbraco.Web.PublishedCache.NuCache.DataSource { - public abstract class SerializerBase + internal abstract class SerializerBase { private const char PrefixNull = 'N'; private const char PrefixString = 'S'; From 697206188f4933c64a4a66a11c85b5eecad54b43 Mon Sep 17 00:00:00 2001 From: nzdev <834725+nzdev@users.noreply.github.com> Date: Wed, 16 Dec 2020 19:23:14 +1300 Subject: [PATCH 5/5] use property --- .../PublishedCache/NuCache/DataSource/SerializerBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs index 9e6baed4fe..cafb40657d 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs @@ -172,7 +172,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource } else if (value is LazyCompressedString lazyCompressedString) { - if (lazyCompressedString.IsDecompressed()) + if (lazyCompressedString.IsDecompressed) { PrimitiveSerializer.Char.WriteTo(PrefixString, stream); PrimitiveSerializer.String.WriteTo(lazyCompressedString, stream);