From 0d159751b99eb5bc3946e69e58271ce1bb5d507a Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 13 Aug 2020 19:15:13 +1000 Subject: [PATCH] Cleans up code - removes tuples everywhere --- .../AppSettingsNucachePropertyMapFactory.cs | 9 ++-- .../Lz4DictionaryOfPropertyDataSerializer.cs | 42 ++++++++-------- .../MsgPackContentNestedDataSerializer.cs | 6 +-- .../DataSource/NuCacheCompressionOptions.cs | 50 +++++++++++++++++++ .../DataSource/NucachePropertyOptions.cs | 9 ++-- src/Umbraco.Web/Umbraco.Web.csproj | 1 + 6 files changed, 83 insertions(+), 34 deletions(-) create mode 100644 src/Umbraco.Web/PublishedCache/NuCache/DataSource/NuCacheCompressionOptions.cs diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/AppSettingsNucachePropertyMapFactory.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/AppSettingsNucachePropertyMapFactory.cs index 6ca4c3e666..271695d250 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/AppSettingsNucachePropertyMapFactory.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/AppSettingsNucachePropertyMapFactory.cs @@ -7,7 +7,8 @@ using System.Threading.Tasks; namespace Umbraco.Web.PublishedCache.NuCache.DataSource { - public class AppSettingsNuCachePropertyMapFactory : INuCachePropertyOptionsFactory + // TODO: We'll remove this when the responsibility for compressing property data is at the property editor level + internal class AppSettingsNuCachePropertyMapFactory : INuCachePropertyOptionsFactory { public NuCachePropertyOptions GetNuCachePropertyOptions() { @@ -20,9 +21,9 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource return options; } - public IReadOnlyDictionary GetPropertyMap() + public IReadOnlyDictionary GetPropertyMap() { - var propertyMap = new Dictionary(); + var propertyMap = new Dictionary(); // TODO: Use xml/json/c# to define map var propertyDictionarySerializerMap = ConfigurationManager.AppSettings["Umbraco.Web.PublishedCache.NuCache.PropertySerializationMap"]; if (!string.IsNullOrWhiteSpace(propertyDictionarySerializerMap)) @@ -39,7 +40,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource ); return v; }) - .ToList().ForEach(x => propertyMap.Add(x.alias, (x.compressionLevel, x.decompressionLevel, x.mappedAlias))); + .ToList().ForEach(x => propertyMap.Add(x.alias, new NuCacheCompressionOptions(x.compressionLevel, x.decompressionLevel, x.mappedAlias))); } return propertyMap; } diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/Lz4DictionaryOfPropertyDataSerializer.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/Lz4DictionaryOfPropertyDataSerializer.cs index 501ead8386..c62af1293f 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/Lz4DictionaryOfPropertyDataSerializer.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/Lz4DictionaryOfPropertyDataSerializer.cs @@ -16,7 +16,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource { None = 0, SQLDatabase = 1, - NucacheDatabase = 2 + NuCacheDatabase = 2 } /// /// If/where to decompress custom properties for nucache @@ -28,22 +28,22 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource Lazy = 2 } - + internal class Lz4DictionaryOfPropertyDataSerializer : SerializerBase, ISerializer>, IDictionaryOfPropertyDataSerializer { - private readonly IReadOnlyDictionary _compressProperties; - private readonly IReadOnlyDictionary _uncompressProperties; + private readonly IReadOnlyDictionary _compressProperties; + private readonly IReadOnlyDictionary _uncompressProperties; public Lz4DictionaryOfPropertyDataSerializer(INuCachePropertyOptionsFactory nucachePropertyOptionsFactory) { var nucachePropertyOptions = nucachePropertyOptionsFactory.GetNuCachePropertyOptions(); - _compressProperties = nucachePropertyOptions.PropertyMap.ToList().ToDictionary(x => string.Intern(x.Key), x => (x.Value.compress,x.Value.decompressionLevel, string.Intern(x.Value.mappedAlias))); - _uncompressProperties = _compressProperties.ToList().ToDictionary(x => x.Value.mappedAlias, x => (x.Value.compress, x.Value.decompressionLevel, x.Key)); + _compressProperties = nucachePropertyOptions.PropertyMap.ToList().ToDictionary(x => string.Intern(x.Key), x => new NuCacheCompressionOptions(x.Value.CompressLevel, x.Value.DecompressLevel, string.Intern(x.Value.MappedAlias))); + _uncompressProperties = _compressProperties.ToList().ToDictionary(x => x.Value.MappedAlias, x => new NuCacheCompressionOptions(x.Value.CompressLevel, x.Value.DecompressLevel, x.Key)); _nucachePropertyOptions = nucachePropertyOptions; } - + public IDictionary ReadFrom(Stream stream) { @@ -82,11 +82,11 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource pdata.Segment = ReadStringObject(stream, true) ?? string.Empty; pdata.Value = ReadObject(stream); - if ((map.Compress.Equals(NucachePropertyCompressionLevel.NucacheDatabase) || map.Compress.Equals(NucachePropertyCompressionLevel.SQLDatabase)) + if ((map.CompressLevel.Equals(NucachePropertyCompressionLevel.NuCacheDatabase) || map.CompressLevel.Equals(NucachePropertyCompressionLevel.SQLDatabase)) && pdata.Value != null && pdata.Value is byte[] byteArrayValue) { //Compressed string - switch (map.decompressionLevel) + switch (map.DecompressLevel) { case NucachePropertyDecompressionLevel.Lazy: pdata.Value = new LazyCompressedString(byteArrayValue); @@ -132,7 +132,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource WriteObject(pdata.Segment ?? string.Empty, stream); //Only compress strings - if (pdata.Value is string stringValue && pdata.Value != null && map.Compress.Equals(NucachePropertyCompressionLevel.NucacheDatabase) + if (pdata.Value is string stringValue && pdata.Value != null && map.CompressLevel.Equals(NucachePropertyCompressionLevel.NuCacheDatabase) && (_nucachePropertyOptions.MinimumCompressibleStringLength == null || !_nucachePropertyOptions.MinimumCompressibleStringLength.HasValue || stringValue.Length > _nucachePropertyOptions.MinimumCompressibleStringLength.Value)) @@ -145,33 +145,33 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource } } } - private readonly (NucachePropertyCompressionLevel Compress, NucachePropertyDecompressionLevel decompressionLevel, string MappedAlias) DEFAULT_MAP =(NucachePropertyCompressionLevel.None, NucachePropertyDecompressionLevel.NotCompressed, null); + private static readonly NuCacheCompressionOptions DefaultMap = new NuCacheCompressionOptions(NucachePropertyCompressionLevel.None, NucachePropertyDecompressionLevel.NotCompressed, null); private readonly NuCachePropertyOptions _nucachePropertyOptions; - public (NucachePropertyCompressionLevel Compress, NucachePropertyDecompressionLevel decompressionLevel, string MappedAlias) GetSerializationMap(string propertyAlias) + public NuCacheCompressionOptions GetSerializationMap(string propertyAlias) { if (_compressProperties == null) { - return DEFAULT_MAP; + return DefaultMap; } - if (_compressProperties.TryGetValue(propertyAlias, out (NucachePropertyCompressionLevel compress, NucachePropertyDecompressionLevel decompressionLevel, string mappedAlias) map)) + if (_compressProperties.TryGetValue(propertyAlias, out var map1)) { - return map; + return map1; } - return DEFAULT_MAP; + return DefaultMap; } - public (NucachePropertyCompressionLevel Compress, NucachePropertyDecompressionLevel decompressionLevel, string MappedAlias) GetDeSerializationMap(string propertyAlias) + public NuCacheCompressionOptions GetDeSerializationMap(string propertyAlias) { if (_uncompressProperties == null) { - return DEFAULT_MAP; + return DefaultMap; } - if (_uncompressProperties.TryGetValue(propertyAlias, out (NucachePropertyCompressionLevel compress, NucachePropertyDecompressionLevel decompressionLevel, string mappedAlias) map)) + if (_uncompressProperties.TryGetValue(propertyAlias, out var map2)) { - return map; + return map2; } - return DEFAULT_MAP; + return DefaultMap; } } } diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/MsgPackContentNestedDataSerializer.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/MsgPackContentNestedDataSerializer.cs index 31421e7177..133586382c 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/MsgPackContentNestedDataSerializer.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/MsgPackContentNestedDataSerializer.cs @@ -69,7 +69,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource { foreach (var map in _propertyOptions.PropertyMap) { - if (map.Value.compress.Equals(NucachePropertyCompressionLevel.SQLDatabase)) + if (map.Value.CompressLevel.Equals(NucachePropertyCompressionLevel.SQLDatabase)) { if (nestedData.PropertyData.TryGetValue(map.Key, out PropertyData[] properties)) { @@ -79,11 +79,11 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource } } } - if (map.Value.mappedAlias != null && !map.Key.Equals(map.Value.mappedAlias) + if (map.Value.MappedAlias != null && !map.Key.Equals(map.Value.MappedAlias) && nestedData.PropertyData.Remove(map.Key) && nestedData.PropertyData.TryGetValue(map.Key, out PropertyData[] properties2)) { nestedData.PropertyData.Remove(map.Key); - nestedData.PropertyData.Add(map.Value.mappedAlias, properties2); + nestedData.PropertyData.Add(map.Value.MappedAlias, properties2); } } } diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/NuCacheCompressionOptions.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/NuCacheCompressionOptions.cs new file mode 100644 index 0000000000..64f1b675bc --- /dev/null +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/NuCacheCompressionOptions.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; + +namespace Umbraco.Web.PublishedCache.NuCache.DataSource +{ + public struct NuCacheCompressionOptions : IEquatable + { + public NuCacheCompressionOptions(NucachePropertyCompressionLevel compressLevel, NucachePropertyDecompressionLevel decompressLevel, string mappedAlias) + { + CompressLevel = compressLevel; + DecompressLevel = decompressLevel; + MappedAlias = mappedAlias ?? throw new ArgumentNullException(nameof(mappedAlias)); + } + + public NucachePropertyCompressionLevel CompressLevel { get; private set; } + public NucachePropertyDecompressionLevel DecompressLevel { get; private set; } + public string MappedAlias { get; private set; } + + public override bool Equals(object obj) + { + return obj is NuCacheCompressionOptions options && Equals(options); + } + + public bool Equals(NuCacheCompressionOptions other) + { + return CompressLevel == other.CompressLevel && + DecompressLevel == other.DecompressLevel && + MappedAlias == other.MappedAlias; + } + + public override int GetHashCode() + { + var hashCode = 961370163; + hashCode = hashCode * -1521134295 + CompressLevel.GetHashCode(); + hashCode = hashCode * -1521134295 + DecompressLevel.GetHashCode(); + hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(MappedAlias); + return hashCode; + } + + public static bool operator ==(NuCacheCompressionOptions left, NuCacheCompressionOptions right) + { + return left.Equals(right); + } + + public static bool operator !=(NuCacheCompressionOptions left, NuCacheCompressionOptions right) + { + return !(left == right); + } + } +} diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/NucachePropertyOptions.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/NucachePropertyOptions.cs index be0118f563..f88f30ccd8 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/NucachePropertyOptions.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/NucachePropertyOptions.cs @@ -1,17 +1,14 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Umbraco.Web.PublishedCache.NuCache.DataSource { + public class NuCachePropertyOptions { - public IReadOnlyDictionary PropertyMap - { get; set; } = new Dictionary(); + public IReadOnlyDictionary PropertyMap { get; set; } = new Dictionary(); public K4os.Compression.LZ4.LZ4Level LZ4CompressionLevel { get; set; } = K4os.Compression.LZ4.LZ4Level.L00_FAST; diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index a4e5640d36..18c4a8ed93 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -259,6 +259,7 @@ +