Class ConsistentHashBuilder<T>

java.lang.Object
cloud.opencode.base.hash.consistent.ConsistentHashBuilder<T>
Type Parameters:
T - node data type | 节点数据类型

public final class ConsistentHashBuilder<T> extends Object
Builder for consistent hash ring 一致性哈希环构建器

Provides a fluent API for configuring and building a consistent hash ring.

提供流畅的API来配置和构建一致性哈希环。

Usage Examples | 使用示例:

ConsistentHash<String> ring = ConsistentHash.<String>builder()
    .hashFunction(OpenHash.murmur3_128())
    .virtualNodeCount(150)
    .addNode("server1", "192.168.1.1")
    .addNode("server2", "192.168.1.2", 2)
    .concurrent(true)
    .build();

Features | 主要功能:

  • Fluent builder API for ConsistentHash ring construction - 流畅的一致性哈希环构建器API
  • Configurable virtual node count - 可配置虚拟节点数量
  • Custom hash function support - 自定义哈希函数支持

Performance | 性能特性:

  • Time complexity: O(n×v) for build() where n=number of nodes and v=virtual nodes per node (ring insertion with TreeMap O(log(n×v)) per entry) - 时间复杂度: build() 为 O(n×v),n 为节点数,v 为每节点虚拟节点数(TreeMap 每次插入 O(log(n×v)))
  • Space complexity: O(n×v) for the virtual node ring - 空间复杂度: 虚拟节点环为 O(n×v)
Since:
JDK 25, opencode-base-hash V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • hashFunction

      public ConsistentHashBuilder<T> hashFunction(HashFunction hashFunction)
      Sets the hash function 设置哈希函数
      Parameters:
      hashFunction - hash function | 哈希函数
      Returns:
      this builder | 此构建器
    • virtualNodeCount

      public ConsistentHashBuilder<T> virtualNodeCount(int count)
      Sets the virtual node count per physical node 设置每个物理节点的虚拟节点数
      Parameters:
      count - virtual node count | 虚拟节点数
      Returns:
      this builder | 此构建器
    • addNode

      public ConsistentHashBuilder<T> addNode(String nodeId, T nodeData)
      Adds a node with default weight 添加默认权重的节点
      Parameters:
      nodeId - node id | 节点ID
      nodeData - node data | 节点数据
      Returns:
      this builder | 此构建器
    • addNode

      public ConsistentHashBuilder<T> addNode(String nodeId, T nodeData, int weight)
      Adds a weighted node 添加带权重的节点
      Parameters:
      nodeId - node id | 节点ID
      nodeData - node data | 节点数据
      weight - node weight | 节点权重
      Returns:
      this builder | 此构建器
    • addNodes

      public ConsistentHashBuilder<T> addNodes(Collection<HashNode<T>> nodes)
      Adds multiple nodes 添加多个节点
      Parameters:
      nodes - collection of nodes | 节点集合
      Returns:
      this builder | 此构建器
    • concurrent

      public ConsistentHashBuilder<T> concurrent(boolean concurrent)
      Sets whether to use concurrent mode 设置是否使用并发模式
      Parameters:
      concurrent - whether to be thread-safe | 是否线程安全
      Returns:
      this builder | 此构建器
    • build

      public ConsistentHash<T> build()
      Builds the consistent hash ring 构建一致性哈希环
      Returns:
      consistent hash ring | 一致性哈希环