-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservers.go
More file actions
55 lines (48 loc) · 1.24 KB
/
servers.go
File metadata and controls
55 lines (48 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package clusterkit
import (
"errors"
"fmt"
)
func (c *Cluster) AddNode(node Node) error {
if node.ID == "" || node.IP == "" || node.Name == "" {
return errors.New("invalid node data")
}
c.Nodes = append(c.Nodes, node)
c.rebuildNodeMap()
return nil
}
// GetNodeByID returns a node by ID - O(1) using NodeMap
func (c *Cluster) GetNodeByID(nodeID string) (*Node, error) {
if node, exists := c.NodeMap[nodeID]; exists {
return node, nil
}
return nil, fmt.Errorf("node %s not found", nodeID)
}
// rebuildNodeMap rebuilds the NodeMap from the Nodes slice for O(1) lookups
func (c *Cluster) rebuildNodeMap() {
if c.NodeMap == nil {
c.NodeMap = make(map[string]*Node, len(c.Nodes))
} else {
// Clear existing map
for k := range c.NodeMap {
delete(c.NodeMap, k)
}
}
for i := range c.Nodes {
c.NodeMap[c.Nodes[i].ID] = &c.Nodes[i]
}
fmt.Printf("[DEBUG] rebuildNodeMap: %d nodes -> %d NodeMap entries\n", len(c.Nodes), len(c.NodeMap))
}
func (c *Cluster) ListNodes() []Node {
return c.Nodes
}
func (c *Cluster) RemoveNode(nodeID string) error {
for i, node := range c.Nodes {
if node.ID == nodeID {
c.Nodes = append(c.Nodes[:i], c.Nodes[i+1:]...)
c.rebuildNodeMap()
return nil
}
}
return errors.New("node not found")
}