-
Notifications
You must be signed in to change notification settings - Fork 0
/
master_variables_store.go
52 lines (42 loc) · 1.24 KB
/
master_variables_store.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
package gopaxos
import "github.com/buptmiao/gopaxos/paxospb"
type masterVariableStore struct {
logStorage LogStorage
}
func newMasterVariableStore(ls LogStorage) *masterVariableStore {
return &masterVariableStore{
logStorage: ls,
}
}
func (m *masterVariableStore) write(wo WriteOptions, groupIdx int, mVar *paxospb.MasterVariables) error {
value, err := mVar.Marshal()
if err != nil {
lPLGErr(groupIdx, "Variables.Marshal fail")
return err
}
err = m.logStorage.SetMasterVariables(wo, groupIdx, value)
if err != nil {
lPLGErr(groupIdx, "DB.Put fail, groupidx %d bufferlen %d err: %v",
groupIdx, len(value), err)
return err
}
return nil
}
func (m *masterVariableStore) read(groupIdx int) (*paxospb.MasterVariables, error) {
value, err := m.logStorage.GetMasterVariables(groupIdx)
if err != nil && err != ErrNotFoundFromStorage {
lPLGErr(groupIdx, "DB.Get fail, groupidx %d err: %v", groupIdx, err)
return nil, err
}
if err == ErrNotFoundFromStorage {
lPLGImp(groupIdx, "DB.Get not found, groupidx %d", groupIdx)
return nil, err
}
mVar := &paxospb.MasterVariables{}
err = mVar.Unmarshal(value)
if err != nil {
lPLGErr(groupIdx, "Variables.Unmarshal fail, bufferlen %d", len(value))
return nil, err
}
return mVar, nil
}