fusenapi/shared/shared_state.go
2023-08-09 16:54:52 +08:00

49 lines
787 B
Go

package shared
import (
"context"
"log"
"github.com/lni/dragonboat/v4"
)
type SharedState struct {
shardID uint64
replicaID uint64
nh *dragonboat.NodeHost
}
func (ss *SharedState) GetUserState(Userid int64) (us *UserState, err error) {
ius, err := ss.nh.SyncRead(context.TODO(), ss.shardID, Userid)
if err != nil {
log.Println(err)
return nil, err
}
if ius != nil {
return ius.(*UserState), nil
}
cs := ss.nh.GetNoOPSession(128)
cmd := &CmdUpdate{UserId: Userid}
cmdBuf, err := FsPasser.PackToBytes(cmd)
if err != nil {
return nil, err
}
result, err := ss.nh.SyncPropose(context.TODO(), cs, cmdBuf)
if err != nil {
return nil, err
}
us = &UserState{}
err = us.Decode(result.Data)
if err != nil {
return nil, err
}
return us, err
}