hash

package
v0.0.0-...-512269d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 默认虚拟节点200
	DefaultReplicas = 200
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Hash

type Hash func(data []byte) uint32

可自定义hash函数

type HashRing

type HashRing struct {
	HashFunc Hash
	// 原始数据,所有的节点
	Nodes Nodes
	Mutex sync.RWMutex
	// 一致性hash 虚拟node,以及关联的节点
	NodeMap map[uint32]string
	// 原始数据,所有的节点权重
	Weights  map[string]int
	Replicas int
}

func NewHashRing

func NewHashRing(replicas int, hashFunc Hash) *HashRing

func (*HashRing) AddNode

func (h *HashRing) AddNode(nodeKey string, weight int)

添加节点并设置节点权重

func (*HashRing) AddNodes

func (h *HashRing) AddNodes(nodeWeight map[string]int)

添加多个节点与权重

func (*HashRing) GetNode

func (h *HashRing) GetNode(str string) string

获取指定字符串对应的节点名称

func (*HashRing) RemoveNode

func (h *HashRing) RemoveNode(nodeKey string)

删除节点

func (*HashRing) RemoveNodes

func (h *HashRing) RemoveNodes(nodeKeys []string)

删除多个节点

type Node

type Node struct {
	Key   string
	Value uint32
}

type Nodes

type Nodes []Node

func (Nodes) Len

func (n Nodes) Len() int

排序支持

func (Nodes) Less

func (n Nodes) Less(i, j int) bool

func (Nodes) Sort

func (n Nodes) Sort()

func (Nodes) Swap

func (n Nodes) Swap(i, j int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL