130 lines
2.9 KiB
Go
130 lines
2.9 KiB
Go
package stringx
|
|
|
|
import (
|
|
"gitee.com/timedb/wheatCache/pkg/errorx"
|
|
"gitee.com/timedb/wheatCache/pkg/proto"
|
|
"gitee.com/timedb/wheatCache/pkg/structure"
|
|
"strconv"
|
|
)
|
|
|
|
type StringSingle struct {
|
|
val *structure.Value
|
|
}
|
|
|
|
func NewStringSingle() structure.StringXInterface {
|
|
return &StringSingle{
|
|
val: structure.NewValue(),
|
|
}
|
|
}
|
|
|
|
func (s *StringSingle) SizeByte() int64 {
|
|
return int64(s.val.GetLength())
|
|
}
|
|
|
|
// RollBack TODO 事务相关, V2 实现
|
|
func (s *StringSingle) RollBack() error {
|
|
panic("not implemented") // TODO: Implement
|
|
}
|
|
|
|
// Begin 事务相关, V2 实现
|
|
func (s *StringSingle) Begin() error {
|
|
panic("not implemented") // TODO: Implement
|
|
}
|
|
|
|
// Comment 事务相关, V2 实现
|
|
func (s *StringSingle) Comment() error {
|
|
panic("not implemented") // TODO: Implement
|
|
}
|
|
|
|
func (s *StringSingle) Encode() ([]byte, error) {
|
|
panic("not implemented") // TODO: Implement
|
|
}
|
|
|
|
func (s *StringSingle) Set(req *proto.SetRequest) (*proto.SetResponse, error) {
|
|
length := s.val.ChangeValueLength(func() {
|
|
s.val.InferValue(req.Val)
|
|
})
|
|
|
|
return &proto.SetResponse{
|
|
Result: req.Val,
|
|
UpdateSize: length,
|
|
}, nil
|
|
}
|
|
|
|
func (s *StringSingle) Get(req *proto.GetRequest) (*proto.GetResponse, error) {
|
|
return &proto.GetResponse{
|
|
Result: s.val.ToString(),
|
|
UpdateSize: 0,
|
|
}, nil
|
|
}
|
|
|
|
func updateValueNotString(s *StringSingle, val int32) (string, int64, error) {
|
|
switch s.val.GetDynamicType() {
|
|
case structure.DynamicNull:
|
|
length := s.val.ChangeValueLength(func() {
|
|
s.val.SetInt(int64(val))
|
|
})
|
|
return strconv.Itoa(int(val)), length, nil
|
|
case structure.DynamicFloat:
|
|
f, err := s.val.ToFloat64()
|
|
if err != nil {
|
|
return "", 0, err
|
|
}
|
|
s.val.SetFloat64(f + float64(val))
|
|
return strconv.FormatFloat(f+1, 'f', 2, 64), 0, nil
|
|
case structure.DynamicInt:
|
|
i, err := s.val.ToInt()
|
|
if err != nil {
|
|
return "", 0, err
|
|
}
|
|
s.val.SetInt(int64(val) + i)
|
|
return strconv.Itoa(int(i + int64(val))), 0, nil
|
|
default:
|
|
return "", 0, errorx.New("string cannot perform add operations")
|
|
}
|
|
}
|
|
|
|
func (s *StringSingle) Add(req *proto.AddRequest) (*proto.AddResponse, error) {
|
|
result, length, err := updateValueNotString(s, req.Renewal)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &proto.AddResponse{
|
|
UpdateSize: length,
|
|
Result: result,
|
|
}, nil
|
|
}
|
|
|
|
func (s *StringSingle) Reduce(req *proto.ReduceRequest) (*proto.ReduceResponse, error) {
|
|
result, length, err := updateValueNotString(s, req.Renewal*-1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &proto.ReduceResponse{
|
|
UpdateSize: length,
|
|
Result: result,
|
|
}, nil
|
|
}
|
|
|
|
func (s *StringSingle) Setbit(req *proto.SetbitRequest) (*proto.SetbitResponse, error) {
|
|
length := s.val.ChangeValueLength(func() {
|
|
s.val.SetByte(int(req.Offer), req.Val)
|
|
})
|
|
return &proto.SetbitResponse{
|
|
UpdateSize: length,
|
|
}, nil
|
|
}
|
|
|
|
func (s *StringSingle) Getbit(req *proto.GetbitRequest) (*proto.GetbitResponse, error) {
|
|
b, err := s.val.GetByte(int(req.Offer))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &proto.GetbitResponse{
|
|
Val: b,
|
|
}, nil
|
|
}
|