feat(structure): update structure stringx

This commit is contained in:
bandl 2021-10-04 21:18:56 +08:00
parent c27328ced4
commit 30c34cf30f
6 changed files with 65 additions and 86 deletions

View File

@ -6,6 +6,8 @@ const (
type DynamicType int8
type UpdateLength int64
const (
DynamicNull = DynamicType(iota)
DynamicInt

View File

@ -1,10 +1,5 @@
// Code generated by gen-struct. DO NOT EDIT.
// make gen-struct generated
package structure
import "gitee.com/timedb/wheatCache/pkg/proto"
type KeyBaseInterface interface {
SizeByte() int64
@ -20,10 +15,10 @@ type KeyBaseInterface interface {
type StringXInterface interface {
KeyBaseInterface
Set(*proto.SetRequest) (*proto.SetResponse, error)
Get(*proto.GetRequest) (*proto.GetResponse, error)
Add(*proto.AddRequest) (*proto.AddResponse, error)
Reduce(*proto.ReduceRequest) (*proto.ReduceResponse, error)
Setbit(*proto.SetbitRequest) (*proto.SetbitResponse, error)
Getbit(*proto.GetbitRequest) (*proto.GetbitResponse, error)
Set(val string) (string, UpdateLength)
Get() string
Add(renewal int32) (string, error)
Reduce(renewal int32) (string, error)
Setbit(offer int32, val bool) UpdateLength
Getbit(offer int32) (bool, error)
}

View File

@ -2,7 +2,6 @@ package stringx
import (
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
"strconv"
)
@ -40,90 +39,69 @@ func (s *StringSingle) Encode() ([]byte, error) {
panic("not implemented") // TODO: Implement
}
func (s *StringSingle) Set(req *proto.SetRequest) (*proto.SetResponse, error) {
func (s *StringSingle) Set(val string) (string, structure.UpdateLength) {
length := s.val.ChangeValueLength(func() {
s.val.InferValue(req.Val)
s.val.InferValue(val)
})
return &proto.SetResponse{
Result: req.Val,
UpdateSize: length,
}, nil
return val, length
}
func (s *StringSingle) Get(req *proto.GetRequest) (*proto.GetResponse, error) {
return &proto.GetResponse{
Result: s.val.ToString(),
UpdateSize: 0,
}, nil
func (s *StringSingle) Get() string {
return s.val.ToString()
}
func updateValueNotString(s *StringSingle, val int32) (string, int64, error) {
func updateValueNotString(s *StringSingle, val int32) (string, 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
s.val.SetInt(int64(val))
return strconv.Itoa(int(val)), nil
case structure.DynamicFloat:
f, err := s.val.ToFloat64()
if err != nil {
return "", 0, err
return "", err
}
s.val.SetFloat64(f + float64(val))
return strconv.FormatFloat(f+1, 'f', 2, 64), 0, nil
return strconv.FormatFloat(f+1, 'f', 2, 64), nil
case structure.DynamicInt:
i, err := s.val.ToInt()
if err != nil {
return "", 0, err
return "", err
}
s.val.SetInt(int64(val) + i)
return strconv.Itoa(int(i + int64(val))), 0, nil
return strconv.Itoa(int(i + int64(val))), nil
default:
return "", 0, errorx.New("string cannot perform add operations")
return "", errorx.New("string cannot perform add operations")
}
}
func (s *StringSingle) Add(req *proto.AddRequest) (*proto.AddResponse, error) {
result, length, err := updateValueNotString(s, req.Renewal)
func (s *StringSingle) Add(renewal int32) (string, error) {
result, err := updateValueNotString(s, renewal)
if err != nil {
return nil, err
return "", err
}
return &proto.AddResponse{
UpdateSize: length,
Result: result,
}, nil
return result, nil
}
func (s *StringSingle) Reduce(req *proto.ReduceRequest) (*proto.ReduceResponse, error) {
result, length, err := updateValueNotString(s, req.Renewal*-1)
func (s *StringSingle) Reduce(renewal int32) (string, error) {
result, err := updateValueNotString(s, -1*renewal)
if err != nil {
return nil, err
return "", err
}
return &proto.ReduceResponse{
UpdateSize: length,
Result: result,
}, nil
return result, nil
}
func (s *StringSingle) Setbit(req *proto.SetbitRequest) (*proto.SetbitResponse, error) {
func (s *StringSingle) Setbit(offer int32, val bool) structure.UpdateLength {
length := s.val.ChangeValueLength(func() {
s.val.SetByte(int(req.Offer), req.Val)
s.val.SetByte(int(offer), val)
})
return &proto.SetbitResponse{
UpdateSize: length,
}, nil
return length
}
func (s *StringSingle) Getbit(req *proto.GetbitRequest) (*proto.GetbitResponse, error) {
b, err := s.val.GetByte(int(req.Offer))
func (s *StringSingle) Getbit(offer int32) (bool, error) {
b, err := s.val.GetByte(int(offer))
if err != nil {
return nil, err
return false, err
}
return &proto.GetbitResponse{
Val: b,
}, nil
return b, err
}

View File

@ -128,10 +128,10 @@ func (v *Value) InferValue(str string) {
}
// ChangeValueLength 根据类型推断 change 的大小, 只用于 Set 操作不发生错误
func (v *Value) ChangeValueLength(f func()) int64 {
func (v *Value) ChangeValueLength(f func()) UpdateLength {
startLen := v.GetLength()
f()
return int64(v.GetLength() - startLen)
return UpdateLength(v.GetLength() - startLen)
}
func (v *Value) SetByte(offset int, val bool) {

27
storage/server/single.go Normal file
View File

@ -0,0 +1,27 @@
package server
import (
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
"time"
)
type serverSingle struct {
middleProduce event.ProduceInterface
lruProduce event.ProduceInterface
ttl time.Duration
}
func NewServer() proto.CommServerServer {
ser := &serverSingle{}
return ser
}
// TODO 移除
func mockLruValue() structure.KeyBaseInterface {
return stringx.NewStringSingle()
}

View File

@ -1,6 +1,3 @@
// Code generated by gen-struct. DO NOT EDIT.
// make gen-struct generated
package server
import (
@ -9,28 +6,8 @@ import (
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
"time"
)
type serverSingle struct {
middleProduce event.ProduceInterface
lruProduce event.ProduceInterface
ttl time.Duration
}
func NewServer() proto.CommServerServer {
ser := &serverSingle{}
return ser
}
// TODO 移除
func mockLruValue() structure.KeyBaseInterface {
return stringx.NewStringSingle()
}
func (s *serverSingle) Set(
cxt context.Context,
req *proto.SetRequest,