-
Notifications
You must be signed in to change notification settings - Fork 13
/
profession-raft.go
100 lines (80 loc) · 1.95 KB
/
profession-raft.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
package process_master
import (
"os"
"github.com/eolinker/eosc/log"
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/utils"
"google.golang.org/protobuf/proto"
)
type ProfessionRaft struct {
eosc.IProfessions
}
func (p *ProfessionRaft) Append(cmd string, data []byte) error {
return nil
}
func (p *ProfessionRaft) Complete() error {
return nil
}
func NewProfessionRaft(IProfessionsData eosc.IProfessions) *ProfessionRaft {
return &ProfessionRaft{IProfessions: IProfessionsData}
}
func (p *ProfessionRaft) Set(name string, profession *eosc.ProfessionConfig) error {
// todo raft sender
return p.IProfessions.Set(name, profession)
}
func (p *ProfessionRaft) Delete(name string) error {
// todo raft sender
return p.IProfessions.Delete(name)
}
func (p *ProfessionRaft) encode() ([]byte, error) {
list := p.IProfessions.All()
pcd := &eosc.ProfessionConfigs{
Data: list,
}
data, err := proto.Marshal(pcd)
if err != nil {
return nil, err
}
return data, nil
}
func (p *ProfessionRaft) decode(data []byte) ([]*eosc.ProfessionConfig, error) {
pcd := new(eosc.ProfessionConfigs)
if len(data) > 0 {
err := proto.Unmarshal(data, pcd)
if err != nil {
log.Warn("decode professions data:", err)
}
} else {
log.Info("decode professions empty")
}
return pcd.Data, nil
}
func (p *ProfessionRaft) Encode(startIndex int) ([]byte, []*os.File, error) {
data, err := p.encode()
if err != nil {
return nil, nil, err
}
return utils.EncodeFrame(data), nil, nil
}
func (p *ProfessionRaft) ResetHandler(data []byte) error {
ps, err := p.decode(data)
if err != nil {
return err
}
p.IProfessions.Reset(ps)
return nil
}
func (p *ProfessionRaft) CommitHandler(cmd string, data []byte) error {
return nil
}
func (p *ProfessionRaft) Snapshot() []byte {
data, err := p.encode()
if err != nil {
return nil
}
return data
}
func (p *ProfessionRaft) ProcessHandler(cmd string, body []byte) ([]byte, interface{}, error) {
// todo
return nil, nil, nil
}