Skip to content

Commit

Permalink
#3 updated godoc of node
Browse files Browse the repository at this point in the history
  • Loading branch information
Krishnakant C authored and Krishnakant C committed Sep 6, 2024
1 parent ca86c5e commit febbe6b
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion internal/client/common/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"
)

// Node represents information about a Kafka node.
// Node represents information about a Atomstate node.
type Node struct {
// ID is the unique identifier of the node.
ID int
Expand Down Expand Up @@ -92,6 +92,17 @@ func (n Node) Rack() string {
}

// String provides a string representation of the node.
//
// Returns:
//
// string: a string representation of the node in the format "Host:Port (id: IDString() rack: Rack())".
//
// Example:
//
// rackName := "rack1"
// node := Node{ID: 1, Host: "localhost", Port: 9092, RackName: &rackName}
// nodeStr := node.String()
// fmt.Println(nodeStr) // Output: localhost:9092 (id: 1 rack: rack1)
func (n Node) String() string {
rackStr := "none"
if n.HasRack() {
Expand All @@ -101,6 +112,24 @@ func (n Node) String() string {
}

// Equal checks if two nodes are equal.
//
// Parameters:
//
// other (Node): the other node to compare with.
//
// Returns:
//
// bool: true if the nodes are equal (same ID, Port, Host, and RackName), false otherwise.
//
// Example:
//
// node1 := Node{ID: 1, Host: "localhost", Port: 9092}
// node2 := Node{ID: 1, Host: "localhost", Port: 9092}
// if node1.Equal(node2) {
// fmt.Println("Nodes are equal")
// } else {
// fmt.Println("Nodes are not equal")
// }
func (n Node) Equal(other Node) bool {
if n.ID != other.ID || n.Port != other.Port || n.Host != other.Host {
return false
Expand All @@ -115,6 +144,19 @@ func (n Node) Equal(other Node) bool {
}

// HashCode calculates the hash code for the node.
// This implementation uses a simple hash function that combines the individual components
// of the Node struct using a prime number (31) as the multiplier.
//
// Returns:
//
// int: the hash code for the node.
//
// Example:
//
// rackName := "rack1"
// node := Node{ID: 1, Host: "localhost", Port: 9092, RackName: &rackName}
// hash := node.HashCode()
// fmt.Println("Node hash code:", hash)
func (n Node) HashCode() int {
hash := 1
hash = hash*31 + n.ID // Add ID
Expand Down

0 comments on commit febbe6b

Please sign in to comment.