-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.go
More file actions
105 lines (85 loc) · 2.59 KB
/
helpers.go
File metadata and controls
105 lines (85 loc) · 2.59 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package clusterkit
// ============================================
// SIMPLE API - Clean and Intuitive
// ============================================
// GetNodes returns all nodes (primary + replicas) for a partition
// Time Complexity: O(R) where R = replication factor
func (ck *ClusterKit) GetNodes(partition *Partition) []Node {
if partition == nil {
return []Node{}
}
ck.mu.RLock()
defer ck.mu.RUnlock()
nodes := make([]Node, 0, 1+len(partition.ReplicaNodes))
// Add primary node - O(1) map lookup
if primaryNode, exists := ck.cluster.NodeMap[partition.PrimaryNode]; exists {
nodes = append(nodes, *primaryNode)
}
// Add replica nodes - O(R) where R = replication factor
for _, replicaID := range partition.ReplicaNodes {
if replicaNode, exists := ck.cluster.NodeMap[replicaID]; exists {
nodes = append(nodes, *replicaNode)
}
}
return nodes
}
// GetPrimary returns the primary node for a partition
// Time Complexity: O(1)
func (ck *ClusterKit) GetPrimary(partition *Partition) *Node {
if partition == nil {
return nil
}
ck.mu.RLock()
defer ck.mu.RUnlock()
// O(1) map lookup
if primaryNode, exists := ck.cluster.NodeMap[partition.PrimaryNode]; exists {
nodeCopy := *primaryNode
return &nodeCopy
}
return nil
}
// GetReplicas returns the replica nodes for a partition
// Time Complexity: O(R) where R = replication factor
func (ck *ClusterKit) GetReplicas(partition *Partition) []Node {
if partition == nil {
return []Node{}
}
ck.mu.RLock()
defer ck.mu.RUnlock()
replicas := make([]Node, 0, len(partition.ReplicaNodes))
// O(R) - loop through replicas with O(1) map lookup each
for _, replicaID := range partition.ReplicaNodes {
if replicaNode, exists := ck.cluster.NodeMap[replicaID]; exists {
replicas = append(replicas, *replicaNode)
}
}
return replicas
}
// GetMyNodeID returns the current node's ID
func (ck *ClusterKit) GetMyNodeID() string {
return ck.nodeID
}
// IsPrimary checks if the current node is the primary for a partition
func (ck *ClusterKit) IsPrimary(partition *Partition) bool {
if partition == nil {
return false
}
myNodeID := ck.GetMyNodeID()
return partition.PrimaryNode == myNodeID
}
// IsReplica checks if the current node is a replica for a partition
func (ck *ClusterKit) IsReplica(partition *Partition) bool {
if partition == nil {
return false
}
myNodeID := ck.GetMyNodeID()
for _, replicaID := range partition.ReplicaNodes {
if replicaID == myNodeID {
return true
}
}
return false
}
// ============================================
// Context-Aware Versions (Optional)
// ============================================