Browse Source

fix: Adding UInt64 conversion function

Matthias Ladkau 4 years ago
parent
commit
a63fe0f575

+ 1 - 0
.gitignore

@@ -10,3 +10,4 @@
 /eliasdb
 /examples/tutorial/run/
 /examples/chat/run/
+/examples/data-mining/docker-images/eliasdb/eliasdb

+ 4 - 4
cluster/distributedstoragemanager.go

@@ -80,7 +80,7 @@ func (dsm *DistributedStorageManager) Root(root int) uint64 {
 	dsm.rootError = err
 
 	if res != nil {
-		ret = res.(uint64)
+		ret = toUInt64(res)
 	}
 
 	return ret
@@ -192,7 +192,7 @@ func (dsm *DistributedStorageManager) insertOrUpdate(insert bool, loc uint64, o
 	cloc, err := dsm.ds.sendDataRequest(member, request)
 
 	if err == nil {
-		return cloc.(uint64), err
+		return toUInt64(cloc), err
 
 	}
 
@@ -210,7 +210,7 @@ func (dsm *DistributedStorageManager) insertOrUpdate(insert bool, loc uint64, o
 
 			cloc, nerr := dsm.ds.sendDataRequest(member, request)
 			if nerr == nil {
-				ret = cloc.(uint64)
+				ret = toUInt64(cloc)
 				err = nil
 				break
 			}
@@ -225,7 +225,7 @@ func (dsm *DistributedStorageManager) insertOrUpdate(insert bool, loc uint64, o
 		for _, member := range replicatingMembers {
 			cloc, nerr := dsm.ds.sendDataRequest(member, request)
 			if nerr == nil {
-				ret = cloc.(uint64)
+				ret = toUInt64(cloc)
 				err = nil
 				break
 			}

+ 1 - 1
cluster/memberaddresstable.go

@@ -337,7 +337,7 @@ func (mat *memberAddressTable) newlocCounter(dsname string) (uint64, bool, error
 		return 1, false, err
 	}
 
-	ret := v.(uint64)
+	ret := toUInt64(v)
 
 	// Store counter in the cache
 

+ 6 - 6
cluster/memberstorage.go

@@ -189,7 +189,7 @@ func (ms *memberStorage) handleSetRootRequest(distTable *DistributionTable, requ
 
 	sm := ms.dataStorage(dsname, true)
 
-	sm.SetRoot(root, request.Value.(uint64))
+	sm.SetRoot(root, toUInt64(request.Value))
 
 	if !request.Transfer {
 		ms.at.AddTransferRequest(distTable.OtherReplicationMembers(0, ms.ds.MemberManager.Name()),
@@ -227,7 +227,7 @@ func (ms *memberStorage) handleInsertRequest(distTable *DistributionTable, reque
 
 		// If this is a transfer request we know already the cluster location
 
-		cloc = request.Args[RPLoc].(uint64)
+		cloc = toUInt64(request.Args[RPLoc])
 	}
 
 	if err == nil {
@@ -285,7 +285,7 @@ func (ms *memberStorage) handleUpdateRequest(distTable *DistributionTable, reque
 	var newVersion uint64
 
 	dsname := request.Args[RPStoreName].(string)
-	cloc := request.Args[RPLoc].(uint64)
+	cloc := toUInt64(request.Args[RPLoc])
 	*response = 0
 
 	// Get the translation
@@ -305,7 +305,7 @@ func (ms *memberStorage) handleUpdateRequest(distTable *DistributionTable, reque
 				newVersion = transRec.ver + 1
 
 			} else {
-				newVersion = request.Args[RPVer].(uint64)
+				newVersion = toUInt64(request.Args[RPVer])
 
 				if newVersion >= transRec.ver {
 					err = sm.Update(transRec.loc, request.Value)
@@ -380,7 +380,7 @@ func (ms *memberStorage) handleFreeRequest(distTable *DistributionTable, request
 	var err error
 
 	dsname := request.Args[RPStoreName].(string)
-	cloc := request.Args[RPLoc].(uint64)
+	cloc := toUInt64(request.Args[RPLoc])
 
 	// Get the translation
 
@@ -439,7 +439,7 @@ func (ms *memberStorage) handleFetchRequest(distTable *DistributionTable,
 	var err error
 
 	dsname := request.Args[RPStoreName].(string)
-	cloc := request.Args[RPLoc].(uint64)
+	cloc := toUInt64(request.Args[RPLoc])
 
 	// Get the translation
 

+ 32 - 0
cluster/util.go

@@ -0,0 +1,32 @@
+/*
+ * EliasDB
+ *
+ * Copyright 2016 Matthias Ladkau. All rights reserved.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+package cluster
+
+import (
+	"fmt"
+	"strconv"
+
+	"devt.de/krotik/common/errorutil"
+)
+
+/*
+toUInt64 safely converts an interface{} to an uint64.
+*/
+func toUInt64(v interface{}) uint64 {
+	if vu, ok := v.(uint64); ok {
+		return vu
+	}
+
+	cloc, err := strconv.ParseInt(fmt.Sprint(v), 10, 64)
+	errorutil.AssertOk(err)
+
+	return uint64(cloc)
+}

+ 26 - 0
cluster/util_test.go

@@ -0,0 +1,26 @@
+/*
+ * EliasDB
+ *
+ * Copyright 2016 Matthias Ladkau. All rights reserved.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+package cluster
+
+import "testing"
+
+func TestToUInt64(t *testing.T) {
+
+	if res := toUInt64("1"); res != 1 {
+		t.Error("Unexpected result: ", res)
+		return
+	}
+
+	if res := toUInt64(uint64(1)); res != 1 {
+		t.Error("Unexpected result: ", res)
+		return
+	}
+}