-
Notifications
You must be signed in to change notification settings - Fork 35
/
replication.go
54 lines (44 loc) · 985 Bytes
/
replication.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
package beehive
import "math/rand"
type replicationStrategy interface {
// SelectHives selects n hives that are not blacklisted. If not possible, it
// returns an empty slice.
selectHives(blackList []uint64, n int) []uint64
}
type rndRepliction struct {
hive *hive
}
func (r *rndRepliction) selectHives(blacklist []uint64, n int) []uint64 {
if n <= 0 {
return nil
}
blmap := make(map[uint64]uint64)
for _, h := range blacklist {
blmap[h] = h
}
lives := r.hive.registry.hives()
whitelist := make([]uint64, 0, len(lives))
for _, h := range lives {
if h.ID == r.hive.ID() || blmap[h.ID] != 0 {
continue
}
whitelist = append(whitelist, h.ID)
}
if len(whitelist) < n {
n = len(whitelist)
}
if n == 0 {
return nil
}
rndHives := make([]uint64, 0, n)
for _, i := range rand.Perm(n) {
rndHives = append(rndHives, whitelist[i])
}
return rndHives
}
func newRndReplication(h *hive) *rndRepliction {
r := &rndRepliction{
hive: h,
}
return r
}