-
Notifications
You must be signed in to change notification settings - Fork 4
/
id.go
75 lines (66 loc) · 1.66 KB
/
id.go
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
package PaxiBFT
import (
"strconv"
"strings"
"github.com/salemmohammed/PaxiBFT/log"
)
// ID represents a generic identifier in format of Zone.Node
type ID string
// NewID returns a new ID type given two int number of zone and node
func NewID(zone, node int) ID {
if zone < 0 {
zone = -zone
}
if node < 0 {
node = -node
}
// return ID(fmt.Sprintf("%d.%d", zone, node))
return ID(strconv.Itoa(zone) + "." + strconv.Itoa(node))
}
// NewID returns a new ID type given two int number of zone and node
func NewIDRest(zone, node int) ID {
// return ID(fmt.Sprintf("%d.%d", zone, node))
return ID(strconv.Itoa(zone) + "." + strconv.Itoa(node))
}
// Zone returns Zond ID component
func (i ID) Zone() int {
if !strings.Contains(string(i), ".") {
log.Warningf("id %s does not contain \".\"\n", i)
return 0
}
s := strings.Split(string(i), ".")[0]
zone, err := strconv.ParseUint(s, 10, 64)
if err != nil {
log.Errorf("Failed to convert Zone %s to int\n", s)
return 0
}
return int(zone)
}
// Node returns Node ID component
func (i ID) Node() int {
var s string
if !strings.Contains(string(i), ".") {
log.Warningf("id %s does not contain \".\"\n", i)
s = string(i)
} else {
s = strings.Split(string(i), ".")[1]
}
node, err := strconv.ParseUint(s, 10, 64)
if err != nil {
log.Errorf("Failed to convert Node %s to int\n", s)
return 0
}
return int(node)
}
type IDs []ID
func (a IDs) Len() int { return len(a) }
func (a IDs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a IDs) Less(i, j int) bool {
if a[i].Zone() < a[j].Zone() {
return true
} else if a[i].Zone() > a[j].Zone() {
return false
} else {
return a[i].Node() < a[j].Node()
}
}