Revert "removes the SQLDatabase enum value" and renames some stuff

This commit is contained in:
Shannon
2020-08-13 23:09:40 +10:00
parent 044585a4f3
commit 9ac2c27110
9 changed files with 97 additions and 32 deletions

View File

@@ -10,14 +10,13 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource
// 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()
public NuCachePropertyCompressionOptions GetNuCachePropertyOptions()
{
NuCachePropertyOptions options = new NuCachePropertyOptions
{
PropertyMap = GetPropertyMap(),
LZ4CompressionLevel = K4os.Compression.LZ4.LZ4Level.L10_OPT,
MinimumCompressibleStringLength = null
};
var options = new NuCachePropertyCompressionOptions(
GetPropertyMap(),
K4os.Compression.LZ4.LZ4Level.L10_OPT,
null);
return options;
}

View File

@@ -5,7 +5,7 @@
/// <summary>
/// Serializes/Deserializes <see cref="ContentNestedData"/> document to the SQL Database as bytes
/// </summary>
public interface IContentNestedDataByteSerializer
public interface IContentNestedDataByteSerializer : IContentNestedDataSerializer
{
ContentNestedData DeserializeBytes(byte[] data);
byte[] SerializeBytes(ContentNestedData nestedData);

View File

@@ -8,6 +8,6 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource
{
public interface INuCachePropertyOptionsFactory
{
NuCachePropertyOptions GetNuCachePropertyOptions();
NuCachePropertyCompressionOptions GetNuCachePropertyOptions();
}
}

View File

@@ -68,6 +68,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource
switch (map.CompressLevel)
{
case NucachePropertyCompressionLevel.SQLDatabase:
case NucachePropertyCompressionLevel.NuCacheDatabase:
if (!(pdata.Value is null) && pdata.Value is byte[] byteArrayValue)
{
@@ -149,7 +150,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource
}
}
private static readonly NuCacheCompressionOptions DefaultMap = new NuCacheCompressionOptions(NucachePropertyCompressionLevel.None, NucachePropertyDecompressionLevel.NotCompressed, null);
private readonly NuCachePropertyOptions _nucachePropertyOptions;
private readonly NuCachePropertyCompressionOptions _nucachePropertyOptions;
public NuCacheCompressionOptions GetSerializationMap(string propertyAlias)
{

View File

@@ -1,17 +1,23 @@
using MessagePack;
using K4os.Compression.LZ4;
using MessagePack;
using MessagePack.Formatters;
using MessagePack.Resolvers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Umbraco.Web.PublishedCache.NuCache.DataSource
{
/// <summary>
/// Serializes/Deserializes <see cref="ContentNestedData"/> document to the SQL Database as bytes using MessagePack
/// </summary>
internal class MsgPackContentNestedDataSerializer : IContentNestedDataByteSerializer, IContentNestedDataSerializer
internal class MsgPackContentNestedDataSerializer : IContentNestedDataByteSerializer
{
private readonly MessagePackSerializerOptions _options;
private MessagePackSerializerOptions _options;
private readonly NuCachePropertyCompressionOptions _propertyOptions;
public MsgPackContentNestedDataSerializer()
public MsgPackContentNestedDataSerializer(INuCachePropertyOptionsFactory propertyOptionsFactory = null)
{
var defaultOptions = ContractlessStandardResolver.Options;
@@ -31,6 +37,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource
_options = defaultOptions
.WithResolver(resolver)
.WithCompression(MessagePackCompression.Lz4BlockArray);
_propertyOptions = propertyOptionsFactory?.GetNuCachePropertyOptions() ?? NuCachePropertyCompressionOptions.Empty;
}
public string ToJson(string serialized)
@@ -49,10 +56,45 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource
public string Serialize(ContentNestedData nestedData)
{
Optimize(nestedData);
var bin = MessagePackSerializer.Serialize(nestedData, _options);
return Convert.ToBase64String(bin);
}
/// <summary>
/// Compress properties and map property names to shorter names
/// </summary>
/// <param name="nestedData"></param>
private void Optimize(ContentNestedData nestedData)
{
if (_propertyOptions.PropertyMap != null && _propertyOptions.PropertyMap.Count > 0)
{
foreach (var map in _propertyOptions.PropertyMap)
{
if (map.Value.CompressLevel.Equals(NucachePropertyCompressionLevel.SQLDatabase))
{
if (nestedData.PropertyData.TryGetValue(map.Key, out PropertyData[] properties))
{
foreach (var property in properties.Where(x => x.Value != null && x.Value is string))
{
property.Value = LZ4Pickler.Pickle(Encoding.UTF8.GetBytes(property.Value as string), _propertyOptions.LZ4CompressionLevel);
}
}
}
// if there is an alias map for this property then use that instead of the real property alias
// (used to save memory, the mapped alias is normally a single char or at least a smaller string)
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.Add(map.Value.MappedAlias, properties2);
}
}
}
}
public ContentNestedData DeserializeBytes(byte[] data) => MessagePackSerializer.Deserialize<ContentNestedData>(data, _options);
public byte[] SerializeBytes(ContentNestedData nestedData) => MessagePackSerializer.Serialize(nestedData, _options);

View File

@@ -0,0 +1,32 @@
using K4os.Compression.LZ4;
using System;
using System.Collections.Generic;
namespace Umbraco.Web.PublishedCache.NuCache.DataSource
{
public class NuCachePropertyCompressionOptions
{
/// <summary>
/// Returns empty options
/// </summary>
public static NuCachePropertyCompressionOptions Empty { get; } = new NuCachePropertyCompressionOptions();
private NuCachePropertyCompressionOptions()
{
}
public NuCachePropertyCompressionOptions(IReadOnlyDictionary<string, NuCacheCompressionOptions> propertyMap, LZ4Level lZ4CompressionLevel, long? minimumCompressibleStringLength)
{
PropertyMap = propertyMap ?? throw new ArgumentNullException(nameof(propertyMap));
LZ4CompressionLevel = lZ4CompressionLevel;
MinimumCompressibleStringLength = minimumCompressibleStringLength;
}
public IReadOnlyDictionary<string, NuCacheCompressionOptions> PropertyMap { get; } = new Dictionary<string, NuCacheCompressionOptions>();
public LZ4Level LZ4CompressionLevel { get; } = LZ4Level.L00_FAST;
public long? MinimumCompressibleStringLength { get; }
}
}

View File

@@ -7,6 +7,14 @@
{
None = 0,
/// <summary>
/// Compress property data at the nucache SQL DB table level
/// </summary>
/// <remarks>
/// Only necessary if the document in the nucache SQL DB table isn't stored as compressed bytes
/// </remarks>
SQLDatabase = 1,
/// <summary>
/// Compress property data at the nucache BTree level
/// </summary>

View File

@@ -1,17 +0,0 @@
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<string, NuCacheCompressionOptions> PropertyMap { get; set; } = new Dictionary<string, NuCacheCompressionOptions>();
public K4os.Compression.LZ4.LZ4Level LZ4CompressionLevel { get; set; } = K4os.Compression.LZ4.LZ4Level.L00_FAST;
public long? MinimumCompressibleStringLength { get; set; }
}
}