I have a set of data which has a hierarchy of 3 levels. Each level has a name.
I am looking at combining all of these names into a single string then creating a numeric hash that can be used as a hash key for a service fabric stateful service.
I have seen lots online about finding data with keys but I am not sure how to actually create them in an efficient way.
Ideally I would like a hash that is quick and easy to generate in SQL Server 2017 and C#.
Can anyone point me in the right direction, please?
Paul
The SF team advice is to use the FNV-1 hashing algorithm for this.
Select a hash algorithm An important part of hashing is selecting your
hash algorithm. A consideration is whether the goal is to group
similar keys near each other (locality sensitive hashing)--or if
activity should be distributed broadly across all partitions
(distribution hashing), which is more common.
The characteristics of a good distribution hashing algorithm are that
it is easy to compute, it has few collisions, and it distributes the
keys evenly. A good example of an efficient hash algorithm is the
FNV-1 hash algorithm.
A good resource for general hash code algorithm choices is the
Wikipedia page on hash functions.
A C# implementation in this example here:
public long HashString(string input)
{
input = input.ToUpperInvariant();
var value = Encoding.UTF8.GetBytes(input);
ulong hash = 14695981039346656037;
unchecked
{
for (int i = 0; i < value.Length; ++i)
{
hash ^= value[i];
hash *= 1099511628211;
}
return (long)hash;
}
}
Remove the ToUpperInvariant
to make it case sensitive.