hashing

package
v0.0.0-...-67c09b2 Latest Latest
Warning

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

Go to latest
Published: May 4, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

README

使用

package main
import (
"github.com/jvole/hashing"
"fmt"
)

func main (){

  nodeWeight := make(map[string]int)
    for i := 0; i < 10; i++ {
		si := fmt.Sprintf("%d", i)
    nodeWeight["211.104.10."+si] = 2
	}
    vitualSpots := 0
    hash := hashring.NewHashRing(vitualSpots)


    //add nodes
    hash.AddNodes(nodeWeight)

    // for _, v := range hash.GetNodes(){
    // 		fmt.Println(" nodeInfo:", v)
    // 	}
    //remove node
    // hash.RemoveNode("node3")


    //add node
    // hash.AddNode("node3", 3)


    //get key's node

    ipMap := make(map[string]int, 0)
    node0 :=make(map[string]string,0)//第二个节点所有数据
    	for i := 0; i < 1000; i++ {
    		si := fmt.Sprintf("key.%d", i)
    		k := hash.GetNode(si)
        if k=="211.104.10.2" {
    		    sii := fmt.Sprintf("%d", i)
            node0["211.104.10.2-"+sii] = si
        }
    		if _, ok := ipMap[k]; ok {
    			ipMap[k] += 1
    		} else {
    			ipMap[k] = 1
    		}
    	}

       for k, v := range ipMap {
       		fmt.Println("Node IP:", k, " count:", v)
       	}
      //for k,v := range node0{
        //fmt.Println(k+"===="+v)
      //}

}

Documentation

Index

Constants

View Source
const (
	DefaultVirualSpots = 40
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HashRing

type HashRing struct {
	// contains filtered or unexported fields
}

func NewHashRing

func NewHashRing(spots int) *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(s string) string

func (*HashRing) GetNodes

func (h *HashRing) GetNodes() []node

func (*HashRing) RemoveNode

func (h *HashRing) RemoveNode(nodeKey string)

func (*HashRing) UpdateNode

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

Jump to

Keyboard shortcuts

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