github.com/searKing/golang/go@v1.2.117/container/hashring/hashring_string.go (about) 1 package hashring 2 3 // StringNodeLocator derived from NodeLocator, but explicit specialization with string 4 type StringNodeLocator struct { 5 nl *NodeLocator 6 } 7 8 func NewStringNodeLocator(opts ...NodeLocatorOption) *StringNodeLocator { 9 return &StringNodeLocator{nl: New(opts...)} 10 } 11 12 // GetAllNodes returns all available nodes 13 func (c *StringNodeLocator) GetAllNodes() []string { 14 return nodesToStrings(c.nl.GetAllNodes()...) 15 } 16 17 // GetPrimaryNode returns the first available node for a name, such as “127.0.0.1:11311-0” for "Alice" 18 func (c *StringNodeLocator) GetPrimaryNode(name string) (string, bool) { 19 node, has := c.nl.GetPrimaryNode(name) 20 return nodeToString(node), has 21 } 22 23 // GetMaxHashKey returns the last available node's HashKey 24 // that is, Maximum HashKey in the Hash Cycle 25 func (c *StringNodeLocator) GetMaxHashKey() (uint32, error) { 26 return c.nl.GetMaxHashKey() 27 } 28 29 // SetNodes setups the NodeLocator with the list of nodes it should use. 30 // If there are existing nodes not present in nodes, they will be removed. 31 // @param nodes a List of Nodes for this NodeLocator to use in 32 // its continuum 33 func (c *StringNodeLocator) SetNodes(nodes ...string) { 34 c.nl.SetNodes(stringsToNodes(nodes...)...) 35 } 36 37 // RemoveAllNodes removes all nodes in the continuum.... 38 func (c *StringNodeLocator) RemoveAllNodes() { 39 c.nl.RemoveAllNodes() 40 } 41 42 // AddNodes inserts nodes into the consistent hash cycle. 43 func (c *StringNodeLocator) AddNodes(nodes ...string) { 44 c.nl.AddNodes(stringsToNodes(nodes...)...) 45 } 46 47 // Remove removes nodes from the consistent hash cycle... 48 func (c *StringNodeLocator) RemoveNodes(nodes ...string) { 49 c.nl.RemoveNodes(stringsToNodes(nodes...)...) 50 } 51 52 // Get returns an element close to where name hashes to in the nodes. 53 func (c *StringNodeLocator) Get(name string) (string, bool) { 54 node, has := c.nl.GetPrimaryNode(name) 55 if node == nil { 56 return "", has 57 } 58 return nodeToString(node), has 59 } 60 61 // GetTwo returns the two closest distinct elements to the name input in the nodes. 62 func (c *StringNodeLocator) GetTwo(name string) (string, string, bool) { 63 firstNode, secondNode, has := c.nl.GetTwo(name) 64 return nodeToString(firstNode), nodeToString(secondNode), has 65 } 66 67 // GetN returns the N closest distinct elements to the name input in the nodes. 68 func (c *StringNodeLocator) GetN(name string, n int) ([]string, bool) { 69 nodes, has := c.nl.GetN(name, n) 70 return nodesToStrings(nodes...), has 71 } 72 73 func stringsToNodes(nodes ...string) []Node { 74 var _nodes []Node 75 for _, node := range nodes { 76 _nodes = append(_nodes, StringNode(node)) 77 } 78 return _nodes 79 } 80 81 func nodesToStrings(nodes ...Node) []string { 82 var _nodes []string 83 for _, node := range nodes { 84 _nodes = append(_nodes, nodeToString(node)) 85 } 86 return _nodes 87 } 88 89 func nodeToString(node Node) string { 90 if node == nil { 91 return "" 92 } 93 return string(node.(StringNode)) 94 }