forked from coreos/torus
/
single.go
53 lines (43 loc) · 1.12 KB
/
single.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
package ring
import (
"fmt"
"github.com/coreos/torus"
"github.com/coreos/torus/models"
)
type single struct {
version int
peer *models.PeerInfo
permutation torus.PeerPermutation
}
func init() {
registerRing(Single, "single", makeSingle)
}
func makeSingle(r *models.Ring) (torus.Ring, error) {
if len(r.Peers) != 1 {
return nil, torus.ErrInvalid
}
return &single{
version: int(r.Version),
peer: r.Peers[0],
permutation: torus.PeerPermutation{
Peers: []string{r.Peers[0].UUID},
Replication: 1,
},
}, nil
}
func (s *single) GetPeers(key torus.BlockRef) (torus.PeerPermutation, error) {
return s.permutation, nil
}
func (s *single) Members() torus.PeerList { return []string{s.peer.UUID} }
func (s *single) Describe() string {
return fmt.Sprintf("Ring: Single\nUUID: %s", s.peer.UUID)
}
func (s *single) Type() torus.RingType { return Single }
func (s *single) Version() int { return s.version }
func (s *single) Marshal() ([]byte, error) {
var out models.Ring
out.Version = uint32(s.version)
out.Type = uint32(s.Type())
out.Peers = []*models.PeerInfo{s.peer}
return out.Marshal()
}