forked from coreos/torus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring_main.go
69 lines (53 loc) · 1.35 KB
/
ring_main.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
package ring
import (
"github.com/coreos/agro"
"github.com/coreos/agro/models"
)
const (
Empty agro.RingType = iota
Single
Mod
Union
Ketama
)
func Unmarshal(b []byte) (agro.Ring, error) {
var a models.Ring
err := a.Unmarshal(b)
if err != nil {
return nil, err
}
return CreateRing(&a)
}
type createRingFunc func(r *models.Ring) (agro.Ring, error)
var ringRegistry map[agro.RingType]createRingFunc
var ringNames map[string]agro.RingType
func registerRing(t agro.RingType, name string, newFunc createRingFunc) {
if ringRegistry == nil {
ringRegistry = make(map[agro.RingType]createRingFunc)
}
if _, ok := ringRegistry[t]; ok {
panic("agro: attempted to register ring type " + string(t) + " twice")
}
ringRegistry[t] = newFunc
if ringNames == nil {
ringNames = make(map[string]agro.RingType)
}
if _, ok := ringNames[name]; ok {
panic("agro: attempted to register ring name " + name + " twice")
}
ringNames[name] = t
}
func CreateRing(r *models.Ring) (agro.Ring, error) {
return ringRegistry[agro.RingType(r.Type)](r)
}
func RingTypeFromString(s string) (agro.RingType, bool) {
v, ok := ringNames[s]
return v, ok
}
type changeReplication struct{ to int }
func (c changeReplication) ModifyRing(r agro.ModifyableRing) {
r.ChangeReplication(c.to)
}
func ReplicationLevel(x int) agro.RingModification {
return changeReplication{to: x}
}