-
Notifications
You must be signed in to change notification settings - Fork 649
/
set.go
130 lines (107 loc) · 2.51 KB
/
set.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package beacon
import (
"errors"
"strings"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils"
)
var (
_ Set = &set{}
errDuplicateID = errors.New("duplicated ID")
errDuplicateIP = errors.New("duplicated IP")
errUnknownID = errors.New("unknown ID")
errUnknownIP = errors.New("unknown IP")
)
type Set interface {
Add(Beacon) error
RemoveByID(ids.NodeID) error
RemoveByIP(utils.IPDesc) error
Len() int
IDsArg() string
IPsArg() string
}
type set struct {
ids map[ids.NodeID]int
ips map[string]int
beacons []Beacon
}
func NewSet() Set {
return &set{
ids: make(map[ids.NodeID]int),
ips: make(map[string]int),
}
}
func (s *set) Add(b Beacon) error {
id := b.ID()
_, duplicateID := s.ids[id]
if duplicateID {
return errDuplicateID
}
ipStr := b.IP().String()
_, duplicateIP := s.ips[ipStr]
if duplicateIP {
return errDuplicateIP
}
s.ids[id] = len(s.beacons)
s.ips[ipStr] = len(s.beacons)
s.beacons = append(s.beacons, b)
return nil
}
func (s *set) RemoveByID(idToRemove ids.NodeID) error {
indexToRemove, exists := s.ids[idToRemove]
if !exists {
return errUnknownID
}
toRemove := s.beacons[indexToRemove]
ipToRemove := toRemove.IP().String()
indexToMove := len(s.beacons) - 1
toMove := s.beacons[indexToMove]
idToMove := toMove.ID()
ipToMove := toMove.IP().String()
s.ids[idToMove] = indexToRemove
s.ips[ipToMove] = indexToRemove
s.beacons[indexToRemove] = toMove
delete(s.ids, idToRemove)
delete(s.ips, ipToRemove)
s.beacons[indexToMove] = nil
s.beacons = s.beacons[:indexToMove]
return nil
}
func (s *set) RemoveByIP(ip utils.IPDesc) error {
indexToRemove, exists := s.ips[ip.String()]
if !exists {
return errUnknownIP
}
toRemove := s.beacons[indexToRemove]
idToRemove := toRemove.ID()
return s.RemoveByID(idToRemove)
}
func (s *set) Len() int { return len(s.beacons) }
func (s *set) IDsArg() string {
sb := strings.Builder{}
if len(s.beacons) == 0 {
return ""
}
b := s.beacons[0]
_, _ = sb.WriteString(b.ID().String())
for _, b := range s.beacons[1:] {
_, _ = sb.WriteString(",")
_, _ = sb.WriteString(b.ID().String())
}
return sb.String()
}
func (s *set) IPsArg() string {
sb := strings.Builder{}
if len(s.beacons) == 0 {
return ""
}
b := s.beacons[0]
_, _ = sb.WriteString(b.IP().String())
for _, b := range s.beacons[1:] {
_, _ = sb.WriteString(",")
_, _ = sb.WriteString(b.IP().String())
}
return sb.String()
}