Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

MrLederer/ConsistentHash

Open more actions menu

Repository files navigation

_Consistent_Hash🪐

GitHub Workflow Status Azure DevOps coverage Nuget

A high performance, immutable, light-weight, dependency-free, ring consistent hash library.

💻 Usage

Construction

var nodeToWeight = new Dictionary<string, int>()
{
  { "NodeA", 100 },
  { "NodeB", 150 },
};
var hasher = ConsistentHash.Create(nodeToWeight);

Hashing

var node = hasher.Hash(Guid.NewGuid());
// node = "NodeB"

AddOrSet / AddOrSetRange

// {NodeA: 100, NodeB: 150}
hasher = hasher.AddOrSet(node: "NodeA", weight: 200); 
// {NodeA: 200, NodeB: 150}
hasher = hasher.AddOrSetRange(new Dictionary<string, int>() { { "NodeC", 500 }, {"NodeD", 35 } });
// {NodeA: 200, NodeB: 150, NodeC: 500, NodeD: 35}
hasher = hasher.AddOrSet(node: "NodeC", weight: 0);
// {NodeA: 200, NodeB: 150, NodeD: 35}
hasher = hasher.AddOrSet(node: "NodeD", weight: -100);
// {NodeA: 200, NodeB: 150}

Remove / RemoveRange

// {NodeA: 200, NodeB: 150, NodeC: 500, NodeD: 35}
hasher = hasher.Remove("NodeA");
// {NodeB: 150, NodeC: 500, NodeD: 35}
hasher = hasher.Remove("NonExistingNode");
// {NodeB: 150, NodeC: 500, NodeD: 35}
hasher = hasher.RemoveRange(new[] { "NodeC", "NodeD" });
// {NodeB: 150}

🌟 Features

  • High performance - Internally uses GetHashCode and XxHash for mapping nodes to values, and RadixSort for sorting
  • Deterministic - Given particular node/weight pairs, and hash key, the hash result will be identical.
  • Immutable
  • Handles collisions in a deterministic manner (WIP)
  • Thoroughly tested
  • Zero dependencies
  • Super light weight

⚡️ Performance

📉 API runtime and memory

Operation Runtime Memory usage Details
Hash O(log(N)) n/a N = Number of nodes
Construction O(∑ni=0Ni) O(∑ni=0Ni) Ni = Weight defined for node i
AddOrSetRange O(∑ni=0Ni) O(∑ni=0Ni) Ni = Weight defined for node i
RemoveRange O(∑ni=0Ni) O(∑ni=0Ni) Ni = Weight defined for node i
Update O(∑ni=0Ni) O(∑ni=0Ni) Ni = Weight defined for node i
Contains O(1) n/a
TryGetWeight O(1) n/a
Equals O(N) n/a N = Number of nodes

NOTE: Any altering call creates a new instance, so costing sum of all weights.

📊 Distribution Benchmarks (WIP)

📃 License

ConsistentHash🪐 is free and open-source software licensed under the GNU General Public License

About

High performance Ring Consistent Hash implementation

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.