Skip to content

Commit

Permalink
imporved error handling func hash helper
Browse files Browse the repository at this point in the history
  • Loading branch information
y-eight committed Nov 16, 2022
1 parent af72e24 commit 91ef486
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
21 changes: 18 additions & 3 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package data

import (
l "log"
"strconv"
"time"

Expand Down Expand Up @@ -183,8 +184,13 @@ func (n *Node) Convert() *meshv1.Node {
// Convert a given mesh node to a database node
// with a given state of the node
func Convert(n *meshv1.Node, state int) *Node {
id, err := h.Hash(n.Target)
if err != nil {
l.Printf("Could not get the hash value of the ID, please check the hash function")
}

return &Node{
Id: h.Hash(n.Target),
Id: id,
Name: n.Name,
Target: n.Target,
State: state,
Expand All @@ -195,11 +201,20 @@ func Convert(n *meshv1.Node, state int) *Node {
// Get the id of a database node.
// The id is a hash integer
func GetId(n *Node) uint32 {
return h.Hash(n.Target)
id, err := h.Hash(n.Target)
if err != nil {
l.Printf("Could not get the hash value of the ID, please check the hash function")
}

return id
}

// Get the id of a given sample.
// The id is a hash integer
func GetSampleId(p *Sample) uint32 {
return h.Hash(p.From + p.To + strconv.FormatInt(p.Key, 10))
id, err := h.Hash(p.From + p.To + strconv.FormatInt(p.Key, 10))
if err != nil {
l.Printf("Could not get the hash value of the sample, please check the hash function")
}
return id
}
16 changes: 10 additions & 6 deletions data/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func Test_NewMemDb(t *testing.T) {
}
}

// helper function
func value(n uint32, _ error) uint32 {
return n
}
func Test_Convert(t *testing.T) {
tests := []struct {
name string
Expand All @@ -88,7 +92,7 @@ func Test_Convert(t *testing.T) {
},
state: 1,
expectedNode: &Node{
Id: h.Hash("tegraT"),
Id: value(h.Hash("tegraT")),
Name: "test",
State: 1,
Target: "tegraT",
Expand All @@ -98,7 +102,7 @@ func Test_Convert(t *testing.T) {
{
name: "Node to MeshNode",
inputNode: &Node{
Id: h.Hash("tegraT"),
Id: value(h.Hash("tegraT")),
Name: "test",
State: 12,
Target: "tegraT",
Expand Down Expand Up @@ -142,12 +146,12 @@ func Test_GetId(t *testing.T) {
{
name: "Node with target",
node: &Node{Target: "tegraT"},
expectedId: h.Hash("tegraT"),
expectedId: value(h.Hash("tegraT")),
},
{
name: "Node without target",
node: &Node{},
expectedId: h.Hash(""),
expectedId: value(h.Hash("")),
},
}

Expand All @@ -174,12 +178,12 @@ func Test_GetSampleId(t *testing.T) {
To: "Gose",
Key: 1,
},
expectedId: h.Hash("EagleGose1"),
expectedId: value(h.Hash("EagleGose1")),
},
{
name: "Empty samples",
sample: &Sample{},
expectedId: h.Hash("0"),
expectedId: value(h.Hash("0")),
},
}

Expand Down
9 changes: 6 additions & 3 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,13 @@ func ValidateAddress(domain string) bool {
return RegExp.MatchString(domain)
}

func Hash(s string) uint32 {
func Hash(s string) (uint32, error) {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
_, err := h.Write([]byte(s))
if err != nil {
return 0, errors.New("Generating a hash value failed: " + err.Error())
}
return h.Sum32(), nil
}

// ------------------
Expand Down
12 changes: 10 additions & 2 deletions mesh/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,21 @@ func (m *Mesh) retryPushSample(node *meshv1.Node) {
// Get the ID of a node
// Hash integer value of the target field (name of node)
func GetId(n *meshv1.Node) uint32 {
return h.Hash(n.Target)
id, err := h.Hash(n.Target)
if err != nil {
log.Printf("Could not get the hash value of the sample, please check the hash function")
}
return id
}

// Get the ID of a sample
// Hash integer value of the concatenated From, To and Key field
func GetSampleId(p *meshv1.Sample) uint32 {
return h.Hash(p.From + p.To + strconv.FormatInt(p.Key, 10))
id, err := h.Hash(p.From + p.To + strconv.FormatInt(p.Key, 10))
if err != nil {
log.Printf("Could not get the hash value of the sample, please check the hash function")
}
return id
}

// Setup the Logger
Expand Down

0 comments on commit 91ef486

Please sign in to comment.