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 DynamicType int8
type UpdateLength int64
const ( const (
DynamicNull = DynamicType(iota) DynamicNull = DynamicType(iota)
DynamicInt DynamicInt

View File

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

View File

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

View File

@ -128,10 +128,10 @@ func (v *Value) InferValue(str string) {
} }
// ChangeValueLength 根据类型推断 change 的大小, 只用于 Set 操作不发生错误 // ChangeValueLength 根据类型推断 change 的大小, 只用于 Set 操作不发生错误
func (v *Value) ChangeValueLength(f func()) int64 { func (v *Value) ChangeValueLength(f func()) UpdateLength {
startLen := v.GetLength() startLen := v.GetLength()
f() f()
return int64(v.GetLength() - startLen) return UpdateLength(v.GetLength() - startLen)
} }
func (v *Value) SetByte(offset int, val bool) { 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 package server
import ( import (
@ -9,28 +6,8 @@ import (
"gitee.com/timedb/wheatCache/pkg/event" "gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/proto" "gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure" "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( func (s *serverSingle) Set(
cxt context.Context, cxt context.Context,
req *proto.SetRequest, req *proto.SetRequest,