diff --git a/data/data.go b/data/data.go index 53ac1e0..238ef24 100644 --- a/data/data.go +++ b/data/data.go @@ -22,6 +22,7 @@ package data import ( + l "log" "strconv" "time" @@ -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, @@ -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 } diff --git a/data/data_test.go b/data/data_test.go index 943cef4..db084a0 100644 --- a/data/data_test.go +++ b/data/data_test.go @@ -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 @@ -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", @@ -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", @@ -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("")), }, } @@ -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")), }, } diff --git a/helper/helper.go b/helper/helper.go index cb52066..c87d9ac 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -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 } // ------------------ diff --git a/mesh/mesh.go b/mesh/mesh.go index 4f56cec..3135384 100644 --- a/mesh/mesh.go +++ b/mesh/mesh.go @@ -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