!31 更新 模板, 添加基础类型

Merge pull request !31 from bandl/feat-gen-storage
This commit is contained in:
bandl 2021-09-25 07:43:12 +00:00 committed by Gitee
commit 3d7df7149a
13 changed files with 423 additions and 55 deletions

View File

@ -6,7 +6,7 @@ import (
)
// New TODO 添加链路追踪等 @bandl @lgq
func New(msg string, opt ...interface{}) error {
msg = fmt.Sprintf(msg, opt...)
func New(msg string, format ...interface{}) error {
msg = fmt.Sprintf(msg, format...)
return errors.New(msg)
}

View File

@ -1,3 +1,5 @@
package lru
// define
import "gitee.com/timedb/wheatCache/pkg/structure"
type SingleWorkFunc func() (*structure.ResultConn, error)

View File

@ -26,6 +26,7 @@ type SetRequest struct {
unknownFields protoimpl.UnknownFields
Key *BaseKey `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
Val string `protobuf:"bytes,2,opt,name=val,proto3" json:"val,omitempty"`
}
func (x *SetRequest) Reset() {
@ -67,6 +68,13 @@ func (x *SetRequest) GetKey() *BaseKey {
return nil
}
func (x *SetRequest) GetVal() string {
if x != nil {
return x.Val
}
return ""
}
type GetRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@ -120,6 +128,8 @@ type AddRequest struct {
unknownFields protoimpl.UnknownFields
Key *BaseKey `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
// 自动增加
Raise int32 `protobuf:"varint,2,opt,name=raise,proto3" json:"raise,omitempty"`
}
func (x *AddRequest) Reset() {
@ -161,12 +171,20 @@ func (x *AddRequest) GetKey() *BaseKey {
return nil
}
func (x *AddRequest) GetRaise() int32 {
if x != nil {
return x.Raise
}
return 0
}
type ReduceRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Key *BaseKey `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
Cut int32 `protobuf:"varint,2,opt,name=cut,proto3" json:"cut,omitempty"`
}
func (x *ReduceRequest) Reset() {
@ -208,12 +226,21 @@ func (x *ReduceRequest) GetKey() *BaseKey {
return nil
}
func (x *ReduceRequest) GetCut() int32 {
if x != nil {
return x.Cut
}
return 0
}
type SetbitRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Key *BaseKey `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
Key *BaseKey `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
Office int32 `protobuf:"varint,2,opt,name=office,proto3" json:"office,omitempty"`
Val bool `protobuf:"varint,3,opt,name=val,proto3" json:"val,omitempty"`
}
func (x *SetbitRequest) Reset() {
@ -255,12 +282,27 @@ func (x *SetbitRequest) GetKey() *BaseKey {
return nil
}
func (x *SetbitRequest) GetOffice() int32 {
if x != nil {
return x.Office
}
return 0
}
func (x *SetbitRequest) GetVal() bool {
if x != nil {
return x.Val
}
return false
}
type GetbitRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Key *BaseKey `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
Key *BaseKey `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
Office int32 `protobuf:"varint,2,opt,name=office,proto3" json:"office,omitempty"`
}
func (x *GetbitRequest) Reset() {
@ -302,29 +344,44 @@ func (x *GetbitRequest) GetKey() *BaseKey {
return nil
}
func (x *GetbitRequest) GetOffice() int32 {
if x != nil {
return x.Office
}
return 0
}
var File_stringx_proto protoreflect.FileDescriptor
var file_stringx_proto_rawDesc = []byte{
0x0a, 0x0d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x78, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a,
0x0a, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x28, 0x0a, 0x0a, 0x53,
0x0a, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x3a, 0x0a, 0x0a, 0x53,
0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79,
0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79,
0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x28, 0x0a, 0x0a, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75,
0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b,
0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22,
0x28, 0x0a, 0x0a, 0x41, 0x64, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a,
0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73,
0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x2b, 0x0a, 0x0d, 0x52, 0x65, 0x64,
0x75, 0x63, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65,
0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65,
0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x2b, 0x0a, 0x0d, 0x53, 0x65, 0x74, 0x62, 0x69, 0x74,
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01,
0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03,
0x6b, 0x65, 0x79, 0x22, 0x2b, 0x0a, 0x0d, 0x47, 0x65, 0x74, 0x62, 0x69, 0x74, 0x52, 0x65, 0x71,
0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28,
0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79,
0x42, 0x0b, 0x5a, 0x09, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x33,
0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x76, 0x61, 0x6c, 0x18, 0x02, 0x20, 0x01,
0x28, 0x09, 0x52, 0x03, 0x76, 0x61, 0x6c, 0x22, 0x28, 0x0a, 0x0a, 0x47, 0x65, 0x74, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01,
0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65,
0x79, 0x22, 0x3e, 0x0a, 0x0a, 0x41, 0x64, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12,
0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42,
0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x72,
0x61, 0x69, 0x73, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x72, 0x61, 0x69, 0x73,
0x65, 0x22, 0x3d, 0x0a, 0x0d, 0x52, 0x65, 0x64, 0x75, 0x63, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65,
0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32,
0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x10,
0x0a, 0x03, 0x63, 0x75, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x63, 0x75, 0x74,
0x22, 0x55, 0x0a, 0x0d, 0x53, 0x65, 0x74, 0x62, 0x69, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08,
0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x16, 0x0a,
0x06, 0x6f, 0x66, 0x66, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x6f,
0x66, 0x66, 0x69, 0x63, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x76, 0x61, 0x6c, 0x18, 0x03, 0x20, 0x01,
0x28, 0x08, 0x52, 0x03, 0x76, 0x61, 0x6c, 0x22, 0x43, 0x0a, 0x0d, 0x47, 0x65, 0x74, 0x62, 0x69,
0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18,
0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52,
0x03, 0x6b, 0x65, 0x79, 0x12, 0x16, 0x0a, 0x06, 0x6f, 0x66, 0x66, 0x69, 0x63, 0x65, 0x18, 0x02,
0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x6f, 0x66, 0x66, 0x69, 0x63, 0x65, 0x42, 0x0b, 0x5a, 0x09,
0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x33,
}
var (

View File

@ -1,3 +1,29 @@
package structure
const (
defaultLen = 32 // 默认创建的 value 大小
)
type DynamicType int8
const (
DynamicNull = DynamicType(iota)
DynamicInt
DynamicFloat
DynamicString
)
type ResultConn struct {
Result []string
UpdateSize int64 // 操作以后发生变更的大小, 负数表示减小
}
func NewResult(s ...string) *ResultConn {
res := make([]string, len(s))
res = append(res, s...)
return &ResultConn{
Result: res,
}
}

View File

@ -22,7 +22,7 @@ type KeyBaseInterface interface {
type {{key}}Interface interface{
KeyBaseInterface
{% for val in value -%}
{{val}}(*proto.{{val}}Request) ([]string, error)
{{val}}(*proto.{{val}}Request) (*ResultConn, error)
{% endfor %}
}
{% endfor -%}

View File

@ -0,0 +1,45 @@
// TODO 添加 storage server 模板
package server
import (
context "context"
"gitee.com/timedb/wheatCache/pkg/proto"
)
type server struct {
}
func NewServer() proto.CommServerServer {
ser := &server{}
return ser
}
func (s *server) Get(
cxt context.Context,
req *proto.GetRequest,
) (*proto.CommendResponse, error) {
panic("implement me")
}
func (s *server) Set(
ctx context.Context,
req *proto.SetRequest,
) (*proto.CommendResponse, error) {
panic("implement me")
}
func (s *server) Add(ctx context.Context, request *proto.AddRequest) (*proto.CommendResponse, error) {
panic("implement me")
}
func (s *server) Reduce(ctx context.Context, request *proto.ReduceRequest) (*proto.CommendResponse, error) {
panic("implement me")
}
func (s *server) Setbit(ctx context.Context, request *proto.SetbitRequest) (*proto.CommendResponse, error) {
panic("implement me")
}
func (s *server) Getbit(ctx context.Context, request *proto.GetbitRequest) (*proto.CommendResponse, error) {
panic("implement me")
}

View File

@ -20,10 +20,10 @@ type KeyBaseInterface interface {
type StringXInterface interface {
KeyBaseInterface
Set(*proto.SetRequest) ([]string, error)
Get(*proto.GetRequest) ([]string, error)
Add(*proto.AddRequest) ([]string, error)
Reduce(*proto.ReduceRequest) ([]string, error)
Setbit(*proto.SetbitRequest) ([]string, error)
Getbit(*proto.GetbitRequest) ([]string, error)
Set(*proto.SetRequest) (*ResultConn, error)
Get(*proto.GetRequest) (*ResultConn, error)
Add(*proto.AddRequest) (*ResultConn, error)
Reduce(*proto.ReduceRequest) (*ResultConn, error)
Setbit(*proto.SetbitRequest) (*ResultConn, error)
Getbit(*proto.GetbitRequest) (*ResultConn, error)
}

View File

@ -1 +0,0 @@
package stringx

View File

@ -1,51 +1,63 @@
package stringx
import "gitee.com/timedb/wheatCache/pkg/proto"
import (
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
)
type StringX struct {
Value string
type StringSingle struct {
val *structure.Value
}
func (s *StringX) SizeByte() int64 {
panic("implement me")
func NewStringSingle() structure.StringXInterface {
return &StringSingle{
val: structure.NewValue(),
}
}
func (s *StringX) RollBack() error {
panic("implement me")
func (s *StringSingle) SizeByte() int64 {
return int64(s.val.GetLength())
}
func (s *StringX) Begin() error {
panic("implement me")
// TODO RollBack 事务相关, V2 实现
func (s *StringSingle) RollBack() error {
panic("not implemented") // TODO: Implement
}
func (s *StringX) Comment() error {
panic("implement me")
// Begin 事务相关, V2 实现
func (s *StringSingle) Begin() error {
panic("not implemented") // TODO: Implement
}
func (s *StringX) Encode() ([]byte, error) {
panic("implement me")
// Comment 事务相关, V2 实现
func (s *StringSingle) Comment() error {
panic("not implemented") // TODO: Implement
}
func (s *StringX) Set(req *proto.SetRequest) ([]string, error) {
panic("implement me")
func (s *StringSingle) Encode() ([]byte, error) {
panic("not implemented") // TODO: Implement
}
func (s *StringX) Get(req *proto.GetRequest) ([]string, error) {
panic("implement me")
func (s *StringSingle) Set(req *proto.SetRequest) (*structure.ResultConn, error) {
return nil, nil
}
func (s *StringX) Add(req *proto.AddRequest) ([]string, error) {
panic("implement me")
func (s *StringSingle) Get(req *proto.GetRequest) (*structure.ResultConn, error) {
panic("not implemented") // TODO: Implement
}
func (s *StringX) Reduce(req *proto.ReduceRequest) ([]string, error) {
panic("implement me")
func (s *StringSingle) Add(req *proto.AddRequest) (*structure.ResultConn, error) {
panic("not implemented") // TODO: Implement
}
func (s *StringX) Setbit(req *proto.SetbitRequest) ([]string, error) {
panic("implement me")
func (s *StringSingle) Reduce(req *proto.ReduceRequest) (*structure.ResultConn, error) {
panic("not implemented") // TODO: Implement
}
func (s *StringX) Getbit(req *proto.GetbitRequest) ([]string, error) {
panic("implement me")
func (s *StringSingle) Setbit(req *proto.SetbitRequest) (*structure.ResultConn, error) {
panic("not implemented") // TODO: Implement
}
func (s *StringSingle) Getbit(req *proto.GetbitRequest) (*structure.ResultConn, error) {
panic("not implemented") // TODO: Implement
}

View File

@ -0,0 +1,19 @@
package stringx
import (
"fmt"
"gitee.com/timedb/wheatCache/pkg/proto"
"github.com/stretchr/testify/require"
"testing"
)
func TestStringSingle_Set(t *testing.T) {
s := new(StringSingle)
p := s.SizeByte()
require.Equal(t, p, int64(16))
s.Set(&proto.SetRequest{
Val: "",
})
p = s.SizeByte()
fmt.Println(p)
}

115
pkg/structure/value.go Normal file
View File

@ -0,0 +1,115 @@
package structure
import (
"bytes"
"encoding/binary"
"math"
"strconv"
"gitee.com/timedb/wheatCache/pkg/errorx"
)
// Value 提供一个基础的 动态类型
type Value struct {
val []byte
length int
onType DynamicType
}
func NewValue() *Value {
return &Value{
val: make([]byte, defaultLen),
length: 0,
onType: DynamicNull,
}
}
func (v *Value) GetLength() int {
return v.length
}
func (v *Value) GetDynamicType() DynamicType {
return v.onType
}
func (v *Value) SetString(str string) {
v.onType = DynamicString
if len(v.val) >= len(str) {
copy(v.val, str)
v.length = len(str)
return
}
// 超过 cap
if len(str) > cap(v.val) {
v.val = make([]byte, len(str))
copy(v.val, str)
v.length = len(str)
return
}
// 在 cap 以内
copy(v.val, str[:v.length])
v.val = append(v.val, str[v.length:]...)
}
func (v *Value) ToString() string {
return string(v.val[:v.length])
}
// SetInt 使用高位存储
func (v *Value) SetInt(i int64) {
byteBuf := bytes.NewBuffer([]byte{})
binary.Write(byteBuf, binary.BigEndian, i)
v.val = byteBuf.Bytes()
v.length = len(v.val)
v.onType = DynamicInt
}
func (v *Value) ToInt() (reI int64, err error) {
if v.onType != DynamicInt {
return 0, errorx.New("can only be resolved from dynamic int")
}
byteBuff := bytes.NewBuffer(v.val)
err = binary.Read(byteBuff, binary.BigEndian, &reI)
if err != nil {
return 0, err
}
return reI, nil
}
func (v *Value) SetFloat64(f float64) {
bits := math.Float64bits(f)
v.length = 8
binary.LittleEndian.PutUint64(v.val[:v.length], bits)
v.onType = DynamicFloat
}
func (v *Value) ToFloat64() (float64, error) {
if v.onType != DynamicFloat {
return 0, errorx.New("can only be resolved from dynamic float")
}
bits := binary.LittleEndian.Uint64(v.val[:v.length])
return math.Float64frombits(bits), nil
}
// InferValue 通过字符串来自动推导类型,性能较低
func (v *Value) InferValue(str string) {
rInt, err := strconv.Atoi(str)
if err == nil {
v.SetInt(int64(rInt))
return
}
rf, err := strconv.ParseFloat(str, 64)
if err == nil {
v.SetFloat64(rf)
return
}
v.SetString(str)
}

View File

@ -0,0 +1,86 @@
package structure
import (
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestValue_SetString(t *testing.T) {
value := Value{
val: make([]byte, 32, 64),
}
testStr := "awjkbk沙袋jdawd"
value.SetString(testStr)
require.Equal(t, testStr, value.ToString())
}
func TestValue_GetString(t *testing.T) {
value := NewValue()
str := ""
for i := 0; i < 100; i++ {
str += strconv.Itoa(i)
}
value.SetString(str)
require.Equal(t, value.ToString(), str)
value.SetString("aw1")
require.Equal(t, value.ToString(), "aw1")
}
func Test_IntToBytes(t *testing.T) {
value := NewValue()
value.SetInt(2<<62 - 1)
require.Equal(t, value.GetLength(), 8)
}
func TestValue_ToInt(t *testing.T) {
value := NewValue()
value.SetInt(90)
rel, err := value.ToInt()
require.NoError(t, err)
require.Equal(t, rel, int64(90))
value.SetString("abc")
require.Equal(t, value.ToString(), "abc")
_, err = value.ToInt()
require.Error(t, err)
}
func TestValue_SetFloat64(t *testing.T) {
value := NewValue()
value.SetFloat64(2.35)
f, err := value.ToFloat64()
require.NoError(t, err)
require.Equal(t, f, 2.35)
value.SetInt(-10)
rel, err := value.ToInt()
require.NoError(t, err)
require.Equal(t, rel, int64(-10))
value.SetFloat64(-2.35)
f, err = value.ToFloat64()
require.NoError(t, err)
require.Equal(t, f, -2.35)
}
func TestValue_InferValue(t *testing.T) {
value := NewValue()
value.InferValue("hello world")
require.Equal(t, value.GetDynamicType(), DynamicString)
require.Equal(t, value.ToString(), "hello world")
value.InferValue("1")
require.Equal(t, value.GetDynamicType(), DynamicInt)
i, err := value.ToInt()
require.NoError(t, err)
require.Equal(t, i, int64(1))
value.InferValue("-1.2")
require.Equal(t, value.GetDynamicType(), DynamicFloat)
f, err := value.ToFloat64()
require.NoError(t, err)
require.Equal(t, f, -1.2)
}

View File

@ -4,6 +4,7 @@ option go_package = "pkg/proto";
message SetRequest {
BaseKey key = 1;
string val = 2;
}
message GetRequest {
@ -12,16 +13,22 @@ message GetRequest {
message AddRequest {
BaseKey key = 1;
//
int32 raise = 2;
}
message ReduceRequest {
BaseKey key = 1;
int32 cut = 2;
}
message SetbitRequest {
BaseKey key = 1;
int32 office = 2;
bool val = 3;
}
message GetbitRequest {
BaseKey key = 1;
int32 office = 2;
}