(精华)2020年6月27日 C#类库 一致性HASH帮助类
【摘要】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Core....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Core.Util
{
/// <summary>
/// 一致性HASH,解决传统HASH的扩容难的问题
/// 注:常用与分布式缓存与分表
/// </summary>
/// <typeparam name="T">泛型</typeparam>
public class ConsistentHash<T>
{
SortedDictionary<int, T> circle { get; set; } = new SortedDictionary<int, T>();//虚拟的圆环,对2的32方取模
int _replicate = 100; //虚拟节点数 count
int[] ayKeys = null; //缓存节点hash
/// <summary>
/// 初始化可迭代的节点数
/// </summary>
/// <param name="nodes">节点</param>
public void Init(IEnumerable<T> nodes)
{
Init(nodes, _replicate);
}
/// <summary>
/// 初始化可迭代的节点数,默认不缓存
/// </summary>
/// <param name="nodes">节点</param>
/// <param name="replicate"></param>
public void Init(IEnumerable<T> nodes, int replicate)
{
_replicate = replicate;
foreach (T node in nodes)
{
this.Add(node, false);
}
ayKeys = circle.Keys.ToArray();
}
/// <summary>
/// 添加节点,缓存
/// </summary>
/// <param name="node"></param>
public void Add(T node)
{
Add(node, true);
}
/// <summary>
/// 添加虚拟的圆环的节点
/// </summary>
/// <param name="node">节点</param>
/// <param name="updateKeyArray">是否缓存node的hash</param>
private void Add(T node, bool updateKeyArray)
{
for (int i = 0; i < _replicate; i++)
{
int hash = BetterHash(node.GetHashCode().ToString() + i);
circle[hash] = node;
}
if (updateKeyArray)
{
ayKeys = circle.Keys.ToArray();
}
}
/// <summary>
/// 删除真实机器节点,更新缓存
/// </summary>
/// <param name="node"></param>
public void Remove(T node)
{
for (int i = 0; i < _replicate; i++)
{
int hash = BetterHash(node.GetHashCode().ToString() + i);
if (!circle.Remove(hash))
{
throw new Exception("can not remove a node that not added");
}
}
ayKeys = circle.Keys.ToArray();
}
/// <summary>
/// 判断是否存在key对应的hash,有则返回没有返回最近一个节点
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private T GetNode_slow(String key)
{
int hash = BetterHash(key);
if (circle.ContainsKey(hash))
{
return circle[hash];
}
// 沿环的顺时针找到一个节点
int first = circle.Keys.FirstOrDefault(h => h >= hash);
if (first == new int())
{
first = ayKeys[0];
}
T node = circle[first];
return node;
}
int First_ge(int[] ay, int val)
{
int begin = 0;
int end = ay.Length - 1;
if (ay[end] < val || ay[0] > val)
{
return 0;
}
int mid = begin;
while (end - begin > 1)
{
mid = (end + begin) / 2;
if (ay[mid] >= val)
{
end = mid;
}
else
{
begin = mid;
}
}
if (ay[begin] > val || ay[end] < val)
{
throw new Exception("should not happen");
}
return end;
}
public T GetNode(String key)
{
int hash = BetterHash(key);
int first = First_ge(ayKeys, hash);
return circle[ayKeys[first]];
}
/// <summary>
/// MurMurHash2算法,性能高,碰撞率低
/// </summary>
/// <param name="key">计算hash的字符串</param>
/// <returns>hash值</returns>
public static int BetterHash(String key)
{
uint hash = MurmurHash2.Hash(Encoding.ASCII.GetBytes(key));
return (int)hash;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
MurMurHash2算法
using System;
using System.Runtime.InteropServices;
namespace Core.Util
{
public class MurmurHash2
{
public static UInt32 Hash(Byte[] data)
{
return Hash(data, 0xc58f1a7b);
}
const UInt32 m = 0x5bd1e995;
const Int32 r = 24;
[StructLayout(LayoutKind.Explicit)]
struct BytetoUInt32Converter
{
[FieldOffset(0)]
public Byte[] Bytes;
[FieldOffset(0)]
public UInt32[] UInts;
}
public static UInt32 Hash(Byte[] data, UInt32 seed)
{
Int32 length = data.Length;
if (length == 0)
return 0;
UInt32 h = seed ^ (UInt32)length;
Int32 currentIndex = 0;
// array will be length of Bytes but contains Uints
// therefore the currentIndex will jump with +1 while length will jump with +4
UInt32[] hackArray = new BytetoUInt32Converter { Bytes = data }.UInts;
while (length >= 4)
{
UInt32 k = hackArray[currentIndex++];
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
length -= 4;
}
currentIndex *= 4; // fix the length
switch (length)
{
case 3:
h ^= (UInt16)(data[currentIndex++] | data[currentIndex++] << 8);
h ^= (UInt32)data[currentIndex] << 16;
h *= m;
break;
case 2:
h ^= (UInt16)(data[currentIndex++] | data[currentIndex] << 8);
h *= m;
break;
case 1:
h ^= data[currentIndex];
h *= m;
break;
default:
break;
}
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/106982042
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)