From 30c34cf30f3ad63745352b55e49b9b2c64c2f7c2 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Mon, 4 Oct 2021 21:18:56 +0800 Subject: [PATCH] feat(structure): update structure stringx --- pkg/structure/define.go | 2 + pkg/structure/interface.gen.go | 17 ++--- pkg/structure/stringx/string.go | 78 +++++++------------- pkg/structure/value.go | 4 +- storage/server/single.go | 27 +++++++ storage/server/{single.gen.go => stringx.go} | 23 ------ 6 files changed, 65 insertions(+), 86 deletions(-) create mode 100644 storage/server/single.go rename storage/server/{single.gen.go => stringx.go} (92%) diff --git a/pkg/structure/define.go b/pkg/structure/define.go index cce5904..2563d9c 100644 --- a/pkg/structure/define.go +++ b/pkg/structure/define.go @@ -6,6 +6,8 @@ const ( type DynamicType int8 +type UpdateLength int64 + const ( DynamicNull = DynamicType(iota) DynamicInt diff --git a/pkg/structure/interface.gen.go b/pkg/structure/interface.gen.go index 6aa2437..6ed2c1e 100644 --- a/pkg/structure/interface.gen.go +++ b/pkg/structure/interface.gen.go @@ -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) } diff --git a/pkg/structure/stringx/string.go b/pkg/structure/stringx/string.go index 08f6850..6efe942 100644 --- a/pkg/structure/stringx/string.go +++ b/pkg/structure/stringx/string.go @@ -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 } diff --git a/pkg/structure/value.go b/pkg/structure/value.go index c56d998..6d65fac 100644 --- a/pkg/structure/value.go +++ b/pkg/structure/value.go @@ -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) { diff --git a/storage/server/single.go b/storage/server/single.go new file mode 100644 index 0000000..dd08c08 --- /dev/null +++ b/storage/server/single.go @@ -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() +} diff --git a/storage/server/single.gen.go b/storage/server/stringx.go similarity index 92% rename from storage/server/single.gen.go rename to storage/server/stringx.go index 851ab87..793a566 100644 --- a/storage/server/single.gen.go +++ b/storage/server/stringx.go @@ -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,