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

feat: enable to use legacy FNV hashes #216

Merged
merged 3 commits into from
Feb 22, 2024
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
1 change: 1 addition & 0 deletions sample/SampleWebApp/appsettings.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"SuppressException": false,
"UseSslStream": false,
"UseIPv6": false,
"UseLegacyNodeLocator": false,
"Transcoder": "MessagePackTranscoder",
"KeyTransformer": "Enyim.Caching.Memcached.SHA1KeyTransformer",
"Authentication": {
Expand Down
1 change: 1 addition & 0 deletions sample/SampleWebApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"queueTimeout": "00:00:00.150"
},
"suppressException": false,
"UseLegacyNodeLocator": false,
"Transcoder": "MessagePackTranscoder"
},

Expand Down
21 changes: 14 additions & 7 deletions src/Enyim.Caching/Configuration/MemcachedClientConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System;
using System.Collections.Generic;
using System.Net;
using Enyim.Caching.Memcached;
using Enyim.Reflection;
using Enyim.Caching.Memcached.Protocol.Binary;
using Enyim.Caching.Memcached.Transcoders;
using Enyim.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using Enyim.Caching.Memcached.Transcoders;

namespace Enyim.Caching.Configuration
{
Expand Down Expand Up @@ -150,7 +150,14 @@ public MemcachedClientConfiguration(

if (NodeLocator == null)
{
NodeLocator = options.Servers.Count > 1 ? typeof(DefaultNodeLocator) : typeof(SingleNodeLocator);
if (options.Servers.Count > 1)
{
NodeLocator = options.UseLegacyNodeLocator ? typeof(LegacyNodeLocator) : typeof(DefaultNodeLocator);
}
else
{
NodeLocator = typeof(SingleNodeLocator);
}
}

if (!string.IsNullOrEmpty(options.Transcoder))
Expand Down
4 changes: 2 additions & 2 deletions src/Enyim.Caching/Configuration/MemcachedClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Security;
using System.Threading.Tasks;

namespace Enyim.Caching.Configuration
{
Expand All @@ -16,6 +14,8 @@ public class MemcachedClientOptions : IOptions<MemcachedClientOptions>

public List<Server> Servers { get; set; } = new List<Server>();

public bool UseLegacyNodeLocator { get; set; }

public Authentication Authentication { get; set; }

public string KeyTransformer { get; set; }
Expand Down
16 changes: 11 additions & 5 deletions src/Enyim.Caching/FnvHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Enyim
/// Calculation found at http://lists.danga.com/pipermail/memcached/2007-April/003846.html, but
/// it is pretty much available everywhere
/// </remarks>
public class FNV64 : System.Security.Cryptography.HashAlgorithm, IUIntHashAlgorithm
public class FNV64 : HashAlgorithm, IUIntHashAlgorithm
{
protected const ulong Init = 0xcbf29ce484222325L;
protected const ulong Prime = 0x100000001b3L;
Expand All @@ -20,10 +20,10 @@ public class FNV64 : System.Security.Cryptography.HashAlgorithm, IUIntHashAlgori
/// <summary>
/// Initializes a new instance of the <see cref="T:FNV64"/> class.
/// </summary>
public FNV64()
public FNV64(bool initialize)
{
//base.HashSize = 64;
Initialize();
if (initialize) Initialize();
}

/// <summary>
Expand Down Expand Up @@ -76,6 +76,8 @@ uint IUIntHashAlgorithm.ComputeHash(byte[] data)
/// </summary>
public sealed class FNV64a : FNV64
{
public FNV64a(bool initialize) : base(initialize) { }

/// <summary>Routes data written to the object into the <see cref="T:FNV64" /> hash algorithm for computing the hash.</summary>
/// <param name="array">The input data. </param>
/// <param name="ibStart">The offset into the byte array from which to begin using data. </param>
Expand Down Expand Up @@ -108,9 +110,9 @@ public class FNV1 : HashAlgorithm, IUIntHashAlgorithm
/// <summary>
/// Initializes a new instance of the <see cref="T:FNV1a"/> class.
/// </summary>
public FNV1()
public FNV1(bool initialize)
{
Initialize();
if (initialize) Initialize();
}

/// <summary>
Expand Down Expand Up @@ -163,6 +165,8 @@ uint IUIntHashAlgorithm.ComputeHash(byte[] data)
/// </summary>
public class FNV1a : FNV1
{
public FNV1a(bool initialize) : base(initialize) { }

/// <summary>Routes data written to the object into the <see cref="T:FNV1a" /> hash algorithm for computing the hash.</summary>
/// <param name="array">The input data. </param>
/// <param name="ibStart">The offset into the byte array from which to begin using data. </param>
Expand All @@ -185,6 +189,8 @@ protected override void HashCore(byte[] array, int ibStart, int cbSize)
/// <remarks>Algorithm found at http://bretm.home.comcast.net/hash/6.html</remarks>
public class ModifiedFNV : FNV1a
{
public ModifiedFNV(bool initialize) : base(initialize) { }

/// <summary>
/// Returns the computed <see cref="T:ModifiedFNV" /> hash value after all data has been written to the object.
/// </summary>
Expand Down
10 changes: 5 additions & 5 deletions src/Enyim.Caching/Memcached/Locators/DefaultNodeLocator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

Expand Down Expand Up @@ -42,7 +42,7 @@ private void BuildIndex(List<IMemcachedNode> nodes)

foreach (IMemcachedNode node in nodes)
{
var tmpKeys = DefaultNodeLocator.GenerateKeys(node, _serverAddressMutations);
var tmpKeys = GenerateKeys(node, _serverAddressMutations);

for (var i = 0; i < tmpKeys.Length; i++)
{
Expand Down Expand Up @@ -126,7 +126,7 @@ private IMemcachedNode FindNode(string key)
{
if (_keys.Length == 0) return null;

uint itemKeyHash = BitConverter.ToUInt32(new FNV1a().ComputeHash(Encoding.UTF8.GetBytes(key)), 0);
uint itemKeyHash = BitConverter.ToUInt32(new FNV1a(true).ComputeHash(Encoding.UTF8.GetBytes(key)), 0);
// get the index of the server assigned to this hash
int foundIndex = Array.BinarySearch<uint>(_keys, itemKeyHash);

Expand Down Expand Up @@ -168,11 +168,11 @@ private static uint[] GenerateKeys(IMemcachedNode node, int numberOfKeys)
// server will be stored with keys 0x0000aabb & 0x0000ccdd
// (or a bit differently based on the little/big indianness of the host)
string address = node.EndPoint.ToString();
var fnv = new FNV1a();
var fnv = new FNV1a(true);

for (int i = 0; i < numberOfKeys; i++)
{
byte[] data = fnv.ComputeHash(Encoding.UTF8.GetBytes(String.Concat(i, "-", address)));
byte[] data = fnv.ComputeHash(Encoding.UTF8.GetBytes(string.Concat(i, "-", address)));

for (int h = 0; h < PartCount; h++)
{
Expand Down
Loading
Loading