This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
/
ring.go
161 lines (136 loc) · 2.69 KB
/
ring.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package torus
import (
"math/big"
"github.com/coreos/torus/models"
)
type RingType int
type Ring interface {
GetPeers(key BlockRef) (PeerPermutation, error)
Members() PeerList
Describe() string
Type() RingType
Version() int
Marshal() ([]byte, error)
}
type ModifyableRing interface {
ChangeReplication(r int) (Ring, error)
}
type RingAdder interface {
ModifyableRing
AddPeers(PeerInfoList) (Ring, error)
}
type RingRemover interface {
ModifyableRing
RemovePeers(PeerList) (Ring, error)
}
type PeerPermutation struct {
Replication int
Peers PeerList
}
type PeerList []string
func (pl PeerList) IndexAt(uuid string) int {
for i, x := range pl {
if x == uuid {
return i
}
}
return -1
}
func (pl PeerList) Has(uuid string) bool {
return pl.IndexAt(uuid) != -1
}
func (pl PeerList) AndNot(b PeerList) PeerList {
var out PeerList
for _, x := range pl {
if !b.Has(x) {
out = append(out, x)
}
}
return out
}
func (pl PeerList) Union(b PeerList) PeerList {
var out PeerList
for _, x := range pl {
out = append(out, x)
}
for _, x := range b {
if !pl.Has(x) {
out = append(out, x)
}
}
return out
}
func (pl PeerList) Intersect(b PeerList) PeerList {
var out PeerList
for _, x := range pl {
if b.Has(x) {
out = append(out, x)
}
}
return out
}
// Applicative! Applicative! My kingdom for Applicative!
type PeerInfoList []*models.PeerInfo
func (pi PeerInfoList) UUIDAt(uuid string) int {
for i, x := range pi {
if x.UUID == uuid {
return i
}
}
return -1
}
func (pi PeerInfoList) HasUUID(uuid string) bool {
return pi.UUIDAt(uuid) != -1
}
func (pi PeerInfoList) AndNot(b PeerList) PeerInfoList {
var out PeerInfoList
for _, x := range pi {
if !b.Has(x.UUID) {
out = append(out, x)
}
}
return out
}
func (pi PeerInfoList) Union(b PeerInfoList) PeerInfoList {
var out PeerInfoList
for _, x := range pi {
out = append(out, x)
}
for _, x := range b {
if !pi.HasUUID(x.UUID) {
out = append(out, x)
}
}
return out
}
func (pi PeerInfoList) Intersect(b PeerInfoList) PeerInfoList {
var out PeerInfoList
for _, x := range pi {
if b.HasUUID(x.UUID) {
out = append(out, x)
}
}
return out
}
func (pi PeerInfoList) PeerList() PeerList {
out := make([]string, len(pi))
for i, x := range pi {
out[i] = x.UUID
}
return PeerList(out)
}
func (pi PeerInfoList) GetWeights() map[string]int {
out := make(map[string]int)
if len(pi) == 0 {
return out
}
gcd := big.NewInt(int64(pi[0].TotalBlocks))
for _, p := range pi[1:] {
gcd.GCD(nil, nil, gcd, big.NewInt(int64(p.TotalBlocks)))
}
for _, p := range pi {
out[p.UUID] = int(p.TotalBlocks / uint64(gcd.Int64()))
clog.Infof("%s: weight %d", p.UUID, out[p.UUID])
}
return out
}