From 7d689a6e1133628b700ee9b9ee5432e75542c9a8 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 13 Aug 2020 23:32:05 +1000 Subject: [PATCH] adds notes, ensures that we optimize property data when using msgpack binary serialization too! --- .../DataSource/Lz4DictionaryOfPropertyDataSerializer.cs | 6 +++--- .../DataSource/MsgPackContentNestedDataSerializer.cs | 7 ++++++- .../PublishedCache/NuCache/DataSource/SerializerBase.cs | 4 ++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/Lz4DictionaryOfPropertyDataSerializer.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/Lz4DictionaryOfPropertyDataSerializer.cs index 752cedc18b..b3d7f7dcbc 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/Lz4DictionaryOfPropertyDataSerializer.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/Lz4DictionaryOfPropertyDataSerializer.cs @@ -67,7 +67,8 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource pdata.Value = ReadObject(stream); switch (map.CompressLevel) - { + { + // If the property is compressed at either the DB or Nucache level, it means it's compressed here and we need to decompress case NucachePropertyCompressionLevel.SQLDatabase: case NucachePropertyCompressionLevel.NuCacheDatabase: if (!(pdata.Value is null) && pdata.Value is byte[] byteArrayValue) @@ -80,8 +81,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource break; case NucachePropertyDecompressionLevel.NotCompressed: //Shouldn't be any not compressed - // TODO: Do we need to throw here? - break; + throw new InvalidOperationException($"{NucachePropertyDecompressionLevel.NotCompressed} cannot be a decompression option for property {alias} since it's compresion option is {map.CompressLevel}"); case NucachePropertyDecompressionLevel.Immediate: default: pdata.Value = Encoding.UTF8.GetString(LZ4Pickler.Unpickle(byteArrayValue)); diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/MsgPackContentNestedDataSerializer.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/MsgPackContentNestedDataSerializer.cs index 24e2bc7b27..09ca1278b4 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/MsgPackContentNestedDataSerializer.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/MsgPackContentNestedDataSerializer.cs @@ -97,7 +97,12 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource public ContentNestedData DeserializeBytes(byte[] data) => MessagePackSerializer.Deserialize(data, _options); - public byte[] SerializeBytes(ContentNestedData nestedData) => MessagePackSerializer.Serialize(nestedData, _options); + public byte[] SerializeBytes(ContentNestedData nestedData) + { + Optimize(nestedData); + + return MessagePackSerializer.Serialize(nestedData, _options); + } //private class ContentNestedDataResolver : IFormatterResolver //{ diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs index 885b5cf80e..4a11a8f0e5 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs @@ -16,6 +16,10 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource private const char PrefixDouble = 'B'; private const char PrefixDateTime = 'D'; private const char PrefixByte = 'O'; + + // TODO: It might make sense to have another prefix for an LZ4 compressed byte array. + // Would be an improvement for the SQLDatabase compression option because then you could mix compressed and decompressed properties with the same alias. + // For example, don't compress recent content, but compress older content. private const char PrefixByteArray = 'A'; protected string ReadString(Stream stream) => PrimitiveSerializer.String.ReadFrom(stream);