!79 feat-storage-template

Merge pull request !79 from bandl/feat-make-service
This commit is contained in:
bandl 2021-10-27 16:28:58 +00:00 committed by Gitee
commit 41e282de5d
18 changed files with 1176 additions and 1011 deletions

35
doc/storage/storage.md Normal file
View File

@ -0,0 +1,35 @@
### 快速进行 storage 开发
#### 分层,简介
```sh
.
├── cmd # storage 启动函数
│ └── root.go
├── dao # 实际处理层,接口实现全部再 dao 层里实现
│ ├── dao.go
│ ├── dao_test.go
│ ├── interface.gen.go
│ ├── listx.go # listx 相关功能
│ └── stringx.go
├── main.go
├── service # 接口层,由 gen-service 自动生成
│ ├── define.go
│ ├── single.go
│ └── single_service.gen.go
└── temp # 开发模板层
├── const.gen.go
├── const.template
├── dao.template
├── service.template
└── tem.yaml
```
#### 快速开发接口
1. 修改 temp/tem.yaml 文件,添加新接口
2. 在项目根目录执行 `make dcgen` 生成 proto 原始结构
3. 修改对应新添加接口的 proto 文件,再次执行 `make dcgen` 完成 proto 迁移
4. 执行 `make gen-service` 生成 dao 接口
5. 完成 新 dao 层接口, 根据需要添加单元测试。
6. 使用 make install 编译并且安装项目

View File

@ -22,6 +22,7 @@ install:
@make gen-middleware
@make build-storage
@make build-gateway
@sudo python3 ./shell/init_conf.py
.PHONY: storage
storage:
@ -46,3 +47,8 @@ gen-middleware:
.PHONY: init-conf
init-conf:
@python3 ./shell/init_conf.py
.PHONY: gen-service
gen-service:
@python3 ./shell/make_service.py

149
shell/make_service.py Normal file
View File

@ -0,0 +1,149 @@
import os
from typing import Dict, List
import re
from jinja2.environment import Template
sysPath = os.getcwd()
protoAddress = f"{sysPath}/protobuf"
tempPath = f"{sysPath}/storage/temp"
daoPath = f"{sysPath}/storage/dao"
servicePath = f"{sysPath}/storage/service"
class ProtoOption(object):
def __init__(self, method: str) -> None:
super().__init__()
self.method = method
self.option = []
self.ret = []
def add_option(self, opt: List[str]):
self.option.extend(opt)
def add_ret(self, opt: List[str]):
self.ret.extend(opt)
def __str__(self) -> str:
req = ", ".join([f"{i[0]} {i[1]}" for i in self.option])
resp = ", ".join([f"{i[0]} {i[1]}" for i in self.ret])
return f"{self.method}({req})({resp})"
def dist_to_ProOpt(req, resp) -> List[ProtoOption]:
def parse_type(l: str) -> List[str]:
l = l.strip()
if l == "":
return []
opt = l.split(";")
result = []
for l_opt in opt:
l_opt = l_opt.strip()
l_list = l_opt.split()
if len(l_list) == 0:
continue
val = l_list[0]
if val == "BaseKey":
val = "*proto.BaseKey"
result.append([l_list[1].capitalize(), val])
elif val == "repeated":
val = f"[]{l_list[1]}"
result.append([l_list[2].capitalize(), val])
else:
result.append([l_list[1].capitalize(), val])
return result
lists = []
for key, value in req.items():
p = ProtoOption(method=key)
p.add_option(parse_type(value))
p.add_ret(parse_type(resp.get(key, "")))
lists.append(p)
return lists
def parse_protobuf_to_Opt(name: str) -> List[ProtoOption]:
gather_req = re.compile(r'^(.*?)Request\s+{(.*?)}$')
gather_resp = re.compile(r'^(.*?)Response\s+{(.*?)}$')
with open(f"{protoAddress}/{name}", "r", encoding="utf-8") as fp:
text = fp.read()
text = re.sub(r'//.+', "", text) # 去掉注释
text = text.replace("\n", "")
result = text.split("message")[1:]
reqDist = {}
respDist = {}
for r in result:
req = gather_req.findall(r)
resp = gather_resp.findall(r)
if len(req) != 0:
req = req[0]
reqDist[req[0].strip()] = req[1].strip()
if len(resp) != 0:
resp = resp[0]
respDist[resp[0].strip()] = resp[1].strip()
return dist_to_ProOpt(reqDist, respDist)
def load_protobuf() -> List[ProtoOption]:
for _, _, file_name_list in os.walk(protoAddress):
break
li = []
for name in file_name_list:
if name.endswith("x.proto"):
li.extend(parse_protobuf_to_Opt(name))
return li
def go_fmt(path: str):
os.system(f"go fmt {path}")
def load_template(name: str) -> str:
with open(f"{tempPath}/{name}", "r", encoding="utf-8") as fp:
return fp.read()
def gen_dao_interface(proto):
tem_text = load_template("dao.template")
template = Template(tem_text)
text = template.render(keys=proto)
temp_path = f"{daoPath}/interface.gen.go"
with open(temp_path, 'w', encoding='utf-8') as f:
f.write(text)
def gen_single_service(proto):
tem_text = load_template("service.template")
template = Template(tem_text)
text = template.render(keys=proto)
temp_path = f"{servicePath}/single_service.gen.go"
with open(temp_path, 'w', encoding='utf-8') as f:
f.write(text)
def format_code_go():
go_fmt(f"{daoPath}/interface.gen.go")
go_fmt(f"{servicePath}/single_service.gen.go")
def main():
# 加载 protobuf
protobuf = load_protobuf()
# 生成 dao 接口
gen_dao_interface(protobuf)
gen_single_service(protobuf)
format_code_go()
if __name__ == "__main__":
main()

View File

@ -6,7 +6,7 @@ import (
"log"
"net"
"gitee.com/timedb/wheatCache/storage/server/single"
"gitee.com/timedb/wheatCache/storage/service"
_ "gitee.com/timedb/wheatCache/conf"
"gitee.com/timedb/wheatCache/pkg/logx"
@ -24,7 +24,7 @@ var rootCmd = &cobra.Command{
Short: "storage",
Long: `start storage server`,
Run: func(cmd *cobra.Command, args []string) {
storageServer := single.NewServer()
storageServer := service.NewSingleServer()
conf := viper.GetStringMap("storage")
host := conf["host"].(string)
port := conf["port"].(int)

View File

@ -1,400 +1,15 @@
package dao
import (
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/lru"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
"gitee.com/timedb/wheatCache/pkg/structure/listx"
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
)
type Dao struct {
lru lru.CacheInterface
}
func NewDao(lru lru.CacheInterface) *Dao {
func NewDao(lru lru.CacheInterface) Interface {
return &Dao{
lru: lru,
}
}
// stringx 相关的方法
func (d *Dao) Set(key *proto.BaseKey, strVal string) (string, error) {
value, ok := d.lru.Get(key)
if ok {
if val, ok := value.(structure.StringXInterface); ok {
res, length := val.Set(strVal)
d.lru.UpdateLruSize(length)
return res, nil
} else {
return "", errorx.New("the key:%s is not stringx type", key)
}
}
// 不存在新建
strValue := stringx.NewStringSingle()
result, _ := strValue.Set(strVal)
err := d.lru.Add(key, strValue)
if err != nil {
return "", err
}
return result, nil
}
func (d *Dao) Get(key *proto.BaseKey) (string, error) {
val, ok := d.lru.Get(key)
if !ok {
return "", errorx.NotKeyErr(key.Key)
}
strVal, ok := val.(structure.StringXInterface)
if !ok {
return "", errorx.DaoTypeErr("stringx")
}
return strVal.Get(), nil
}
func (d *Dao) Add(key *proto.BaseKey, renewal int32) (string, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
val := stringx.NewStringSingle()
res, err := val.Add(renewal)
if err != nil {
return "", nil
}
d.lru.Add(key, val)
return res, nil
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return "", errorx.DaoTypeErr("stringx")
}
res, err := strVal.Add(renewal)
if err != nil {
return "", err
}
return res, nil
}
func (d *Dao) Reduce(key *proto.BaseKey, renewal int32) (string, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
val := stringx.NewStringSingle()
res, err := val.Reduce(renewal)
if err != nil {
return "", nil
}
d.lru.Add(key, val)
return res, nil
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return "", errorx.DaoTypeErr("stringx")
}
res, err := strVal.Reduce(renewal)
if err != nil {
return "", err
}
return res, nil
}
func (d *Dao) Setbit(key *proto.BaseKey, val bool, offer int32) error {
value, lruOk := d.lru.Get(key)
if !lruOk {
valStr := stringx.NewStringSingle()
length := valStr.Setbit(offer, val)
d.lru.UpdateLruSize(length)
d.lru.Add(key, valStr)
return nil
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return errorx.DaoTypeErr("stringx")
}
length := strVal.Setbit(offer, val)
d.lru.UpdateLruSize(length)
return nil
}
func (d *Dao) GetBit(key *proto.BaseKey, offer int32) (bool, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
return false, errorx.NotKeyErr(key.Key)
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return false, errorx.DaoTypeErr("stringx")
}
return strVal.Getbit(offer)
}
func (d *Dao) Getrange(key *proto.BaseKey, start, end int32) (string, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
return "", errorx.NotKeyErr(key.Key)
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return "", errorx.DaoTypeErr("stringx")
}
return strVal.Getrange(start, end)
}
func (d *Dao) Getset(key *proto.BaseKey, value string) (string, error) {
val, ok := d.lru.Get(key)
if !ok {
return "", errorx.NotKeyErr(key.Key)
}
strVal, ok := val.(structure.StringXInterface)
if !ok {
return "", errorx.DaoTypeErr("stringx")
}
oldValue := strVal.Get()
_, updateLength := strVal.Set(value)
d.lru.UpdateLruSize(updateLength)
return oldValue, nil
}
func (d *Dao) Strlen(key *proto.BaseKey) (int32, error) {
val, ok := d.lru.Get(key)
if !ok {
return 0, errorx.NotKeyErr(key.Key)
}
strVal, ok := val.(structure.StringXInterface)
if !ok {
return 0, errorx.DaoTypeErr("stringx")
}
return int32(strVal.GetLength()), nil
}
func (d *Dao) Setnx(key *proto.BaseKey, val string) error {
_, ok := d.lru.Get(key)
if ok {
return errorx.New("the key already exists")
}
strValue := stringx.NewStringSingle()
strValue.Set(val)
err := d.lru.Add(key, strValue)
if err != nil {
return err
}
return nil
}
// listx
func (d *Dao) LINdex(key *proto.BaseKey, index int32) (string, error) {
val, ok := d.lru.Get(key)
if !ok {
return "", errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return "", errorx.DaoTypeErr("listx")
}
return listVal.Index(int(index))
}
func (d *Dao) LLen(key *proto.BaseKey) (int32, error) {
val, ok := d.lru.Get(key)
if !ok {
return 0, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return 0, errorx.DaoTypeErr("listx")
}
return int32(listVal.Length()), nil
}
func (d *Dao) LPop(key *proto.BaseKey, count int32) ([]string, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
result, upLen := listVal.LPop(int(count))
d.lru.UpdateLruSize(upLen)
return result, nil
}
func (d *Dao) LPush(key *proto.BaseKey, values ...string) error {
val, ok := d.lru.Get(key)
if !ok {
list := listx.NewListXSingle()
list.LPush(values...)
return d.lru.Add(key, list)
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return errorx.DaoTypeErr("listx")
}
upLen := listVal.LPush(values...)
d.lru.UpdateLruSize(upLen)
return nil
}
func (d *Dao) LPushX(key *proto.BaseKey, values ...string) error {
val, ok := d.lru.Get(key)
if !ok {
return errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return errorx.DaoTypeErr("listx")
}
upLen := listVal.LPush(values...)
d.lru.UpdateLruSize(upLen)
return nil
}
func (d *Dao) LRange(key *proto.BaseKey, start, end int32) ([]string, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
return listVal.Range(int(start), int(end))
}
func (d *Dao) LRemove(key *proto.BaseKey, count int32, value string) (int, error) {
val, ok := d.lru.Get(key)
if !ok {
return 0, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return 0, errorx.DaoTypeErr("listx")
}
remCount, upLen := listVal.Remove(value, int(count))
d.lru.UpdateLruSize(upLen)
return remCount, nil
}
func (d *Dao) LSet(key *proto.BaseKey, index int32, value string) error {
val, ok := d.lru.Get(key)
if !ok {
return errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return errorx.DaoTypeErr("listx")
}
upLen, err := listVal.Insert(int(index), false, value)
d.lru.UpdateLruSize(upLen)
return err
}
func (d *Dao) RPop(key *proto.BaseKey, count int32) ([]string, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
result, upLen := listVal.RPop(int(count))
d.lru.UpdateLruSize(upLen)
return result, nil
}
func (d *Dao) LTrim(key *proto.BaseKey, start, end int32) error {
val, ok := d.lru.Get(key)
if !ok {
return errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return errorx.DaoTypeErr("listx")
}
upLen, err := listVal.Slice(int(start), int(end))
if err != nil {
return err
}
d.lru.UpdateLruSize(upLen)
return nil
}
func (d *Dao) RPush(key *proto.BaseKey, values ...string) error {
val, ok := d.lru.Get(key)
if !ok {
list := listx.NewListXSingle()
list.RPush(values...)
return d.lru.Add(key, list)
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return errorx.DaoTypeErr("listx")
}
upLen := listVal.RPush(values...)
d.lru.UpdateLruSize(upLen)
return nil
}
func (d *Dao) RPushX(key *proto.BaseKey, values ...string) error {
val, ok := d.lru.Get(key)
if !ok {
return errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return errorx.DaoTypeErr("listx")
}
upLen := listVal.RPush(values...)
d.lru.UpdateLruSize(upLen)
return nil
}

View File

@ -1,89 +1,5 @@
package dao
import (
"testing"
_ "gitee.com/timedb/wheatCache/conf"
"gitee.com/timedb/wheatCache/pkg/lru"
"gitee.com/timedb/wheatCache/pkg/proto"
"github.com/stretchr/testify/require"
)
func mockData(t *testing.T, d *Dao) {
values := []string{"1", "1.3", "abcdefg"}
for _, val := range values {
key := &proto.BaseKey{
Key: val,
}
_, err := d.Set(key, val)
require.NoError(t, err)
}
}
func TestDao_Set(t *testing.T) {
lruCache := lru.NewLRUCache()
dao := NewDao(lruCache)
key := &proto.BaseKey{
Key: "abc",
}
res, err := dao.Set(key, "abc")
require.NoError(t, err)
require.Equal(t, res, "abc")
res, err = dao.Get(key)
require.NoError(t, err)
require.Equal(t, res, "abc")
}
func TestDao_Add(t *testing.T) {
lruCache := lru.NewLRUCache()
dao := NewDao(lruCache)
mockData(t, dao)
resp, err := dao.Add(&proto.BaseKey{Key: "1"}, 2)
require.NoError(t, err)
require.Equal(t, resp, "3")
resp, err = dao.Add(&proto.BaseKey{Key: "1.3"}, 2)
require.NoError(t, err)
require.Equal(t, resp, "3.30")
_, err = dao.Add(&proto.BaseKey{Key: "abcdefg"}, 2)
require.Error(t, err)
}
func TestDao_Reduce(t *testing.T) {
lruCache := lru.NewLRUCache()
dao := NewDao(lruCache)
mockData(t, dao)
resp, err := dao.Reduce(&proto.BaseKey{Key: "1"}, 2)
require.NoError(t, err)
require.Equal(t, resp, "-1")
resp, err = dao.Reduce(&proto.BaseKey{Key: "1.3"}, 2)
require.NoError(t, err)
require.Equal(t, resp, "-0.70")
_, err = dao.Reduce(&proto.BaseKey{Key: "abcdefg"}, 2)
require.Error(t, err)
}
func TestDao_Setbit(t *testing.T) {
lruCache := lru.NewLRUCache()
dao := NewDao(lruCache)
key := &proto.BaseKey{
Key: "abc",
}
err := dao.Setbit(key, true, 3089)
require.NoError(t, err)
re, err := dao.GetBit(key, 3089)
require.NoError(t, err)
require.Equal(t, re, true)
}

View File

@ -1 +0,0 @@
package dao

View File

@ -0,0 +1,31 @@
// Code generated by gen-struct. DO NOT EDIT.
// make gen-service generated
package dao
import "gitee.com/timedb/wheatCache/pkg/proto"
type Interface interface {
LIndex(*proto.BaseKey, int32) (*proto.LIndexResponse, error)
LLen(*proto.BaseKey) (*proto.LLenResponse, error)
LPop(*proto.BaseKey, int32) (*proto.LPopResponse, error)
LPush(*proto.BaseKey, []string) (*proto.LPushResponse, error)
LPushX(*proto.BaseKey, []string) (*proto.LPushXResponse, error)
LRange(*proto.BaseKey, int32, int32) (*proto.LRangeResponse, error)
LRem(*proto.BaseKey, int32, string) (*proto.LRemResponse, error)
LSet(*proto.BaseKey, int32, string) (*proto.LSetResponse, error)
RPop(*proto.BaseKey, int32) (*proto.RPopResponse, error)
LTrim(*proto.BaseKey, int32, int32) (*proto.LTrimResponse, error)
RPush(*proto.BaseKey, []string) (*proto.RPushResponse, error)
RPushX(*proto.BaseKey, []string) (*proto.RPushXResponse, error)
Set(*proto.BaseKey, string) (*proto.SetResponse, error)
Get(*proto.BaseKey) (*proto.GetResponse, error)
Add(*proto.BaseKey, int32) (*proto.AddResponse, error)
Reduce(*proto.BaseKey, int32) (*proto.ReduceResponse, error)
Setnx(*proto.BaseKey, string) (*proto.SetnxResponse, error)
SetBit(*proto.BaseKey, bool, int32) (*proto.SetBitResponse, error)
GetBit(*proto.BaseKey, int32) (*proto.GetBitResponse, error)
GetRange(*proto.BaseKey, int32, int32) (*proto.GetRangeResponse, error)
GetSet(*proto.BaseKey, string) (*proto.GetSetResponse, error)
StrLen(*proto.BaseKey) (*proto.StrLenResponse, error)
}

220
storage/dao/listx.go Normal file
View File

@ -0,0 +1,220 @@
package dao
import (
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
"gitee.com/timedb/wheatCache/pkg/structure/listx"
)
func (d *Dao) LIndex(key *proto.BaseKey, index int32) (*proto.LIndexResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
result, err := listVal.Index(int(index))
if err != nil {
return nil, err
}
return &proto.LIndexResponse{Result: result}, nil
}
func (d *Dao) LLen(key *proto.BaseKey) (*proto.LLenResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
return &proto.LLenResponse{Length: int32(listVal.Length())}, nil
}
func (d *Dao) LPop(key *proto.BaseKey, count int32) (*proto.LPopResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
result, upLen := listVal.LPop(int(count))
d.lru.UpdateLruSize(upLen)
return &proto.LPopResponse{Results: result}, nil
}
func (d *Dao) LPush(key *proto.BaseKey, values []string) (*proto.LPushResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
list := listx.NewListXSingle()
list.LPush(values...)
err := d.lru.Add(key, list)
return &proto.LPushResponse{}, err
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
upLen := listVal.LPush(values...)
d.lru.UpdateLruSize(upLen)
return &proto.LPushResponse{}, nil
}
func (d *Dao) LPushX(key *proto.BaseKey, values []string) (*proto.LPushXResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
upLen := listVal.LPush(values...)
d.lru.UpdateLruSize(upLen)
return &proto.LPushXResponse{}, nil
}
func (d *Dao) LRange(key *proto.BaseKey, start, end int32) (*proto.LRangeResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
res, err := listVal.Range(int(start), int(end))
if err != nil {
return nil, err
}
return &proto.LRangeResponse{Values: res}, nil
}
func (d *Dao) LRem(key *proto.BaseKey, count int32, value string) (*proto.LRemResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
remCount, upLen := listVal.Remove(value, int(count))
d.lru.UpdateLruSize(upLen)
return &proto.LRemResponse{Count: int32(remCount)}, nil
}
func (d *Dao) LSet(key *proto.BaseKey, index int32, value string) (*proto.LSetResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
upLen, err := listVal.Insert(int(index), false, value)
d.lru.UpdateLruSize(upLen)
return &proto.LSetResponse{}, err
}
func (d *Dao) RPop(key *proto.BaseKey, count int32) (*proto.RPopResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
result, upLen := listVal.RPop(int(count))
d.lru.UpdateLruSize(upLen)
return &proto.RPopResponse{Result: result}, nil
}
func (d *Dao) LTrim(key *proto.BaseKey, start, end int32) (*proto.LTrimResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
upLen, err := listVal.Slice(int(start), int(end))
if err != nil {
return nil, err
}
d.lru.UpdateLruSize(upLen)
return &proto.LTrimResponse{}, nil
}
func (d *Dao) RPush(key *proto.BaseKey, values []string) (*proto.RPushResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
list := listx.NewListXSingle()
list.RPush(values...)
return &proto.RPushResponse{}, d.lru.Add(key, list)
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
upLen := listVal.RPush(values...)
d.lru.UpdateLruSize(upLen)
return &proto.RPushResponse{}, nil
}
func (d *Dao) RPushX(key *proto.BaseKey, values []string) (*proto.RPushXResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return nil, errorx.DaoTypeErr("listx")
}
upLen := listVal.RPush(values...)
d.lru.UpdateLruSize(upLen)
return &proto.RPushXResponse{}, nil
}

193
storage/dao/stringx.go Normal file
View File

@ -0,0 +1,193 @@
package dao
import (
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
)
// stringx 相关的方法
func (d *Dao) Set(key *proto.BaseKey, strVal string) (*proto.SetResponse, error) {
value, ok := d.lru.Get(key)
if ok {
if val, ok := value.(structure.StringXInterface); ok {
res, length := val.Set(strVal)
d.lru.UpdateLruSize(length)
return &proto.SetResponse{Result: res}, nil
} else {
return nil, errorx.New("the key:%s is not stringx type", key)
}
}
// 不存在新建
strValue := stringx.NewStringSingle()
result, _ := strValue.Set(strVal)
err := d.lru.Add(key, strValue)
if err != nil {
return nil, err
}
return &proto.SetResponse{Result: result}, nil
}
func (d *Dao) Get(key *proto.BaseKey) (*proto.GetResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.NotKeyErr(key.Key)
}
strVal, ok := val.(structure.StringXInterface)
if !ok {
return nil, errorx.DaoTypeErr("stringx")
}
return &proto.GetResponse{Result: strVal.Get()}, nil
}
func (d *Dao) Add(key *proto.BaseKey, renewal int32) (*proto.AddResponse, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
val := stringx.NewStringSingle()
res, err := val.Add(renewal)
if err != nil {
return nil, nil
}
d.lru.Add(key, val)
return &proto.AddResponse{Result: res}, nil
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return nil, errorx.DaoTypeErr("stringx")
}
res, err := strVal.Add(renewal)
if err != nil {
return nil, err
}
return &proto.AddResponse{Result: res}, nil
}
func (d *Dao) Reduce(key *proto.BaseKey, renewal int32) (*proto.ReduceResponse, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
val := stringx.NewStringSingle()
res, err := val.Reduce(renewal)
if err != nil {
return nil, err
}
d.lru.Add(key, val)
return &proto.ReduceResponse{Result: res}, nil
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return nil, errorx.DaoTypeErr("stringx")
}
res, err := strVal.Reduce(renewal)
if err != nil {
return nil, err
}
return &proto.ReduceResponse{Result: res}, nil
}
func (d *Dao) SetBit(key *proto.BaseKey, val bool, offer int32) (*proto.SetBitResponse, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
valStr := stringx.NewStringSingle()
length := valStr.Setbit(offer, val)
d.lru.UpdateLruSize(length)
d.lru.Add(key, valStr)
return &proto.SetBitResponse{}, nil
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return nil, errorx.DaoTypeErr("stringx")
}
length := strVal.Setbit(offer, val)
d.lru.UpdateLruSize(length)
return &proto.SetBitResponse{}, nil
}
func (d *Dao) GetBit(key *proto.BaseKey, offer int32) (*proto.GetBitResponse, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
return nil, errorx.NotKeyErr(key.Key)
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return nil, errorx.DaoTypeErr("stringx")
}
res, err := strVal.Getbit(offer)
if err != nil {
return nil, err
}
return &proto.GetBitResponse{Val: res}, nil
}
func (d *Dao) GetRange(key *proto.BaseKey, start, end int32) (*proto.GetRangeResponse, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
return nil, errorx.NotKeyErr(key.Key)
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return nil, errorx.DaoTypeErr("stringx")
}
res, err := strVal.Getrange(start, end)
if err != nil {
return nil, err
}
return &proto.GetRangeResponse{Result: res}, nil
}
func (d *Dao) GetSet(key *proto.BaseKey, value string) (*proto.GetSetResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.NotKeyErr(key.Key)
}
strVal, ok := val.(structure.StringXInterface)
if !ok {
return nil, errorx.DaoTypeErr("stringx")
}
oldValue := strVal.Get()
_, updateLength := strVal.Set(value)
d.lru.UpdateLruSize(updateLength)
return &proto.GetSetResponse{Result: oldValue}, nil
}
func (d *Dao) StrLen(key *proto.BaseKey) (*proto.StrLenResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.NotKeyErr(key.Key)
}
strVal, ok := val.(structure.StringXInterface)
if !ok {
return nil, errorx.DaoTypeErr("stringx")
}
return &proto.StrLenResponse{Length: int32(strVal.GetLength())}, nil
}
func (d *Dao) Setnx(key *proto.BaseKey, val string) (*proto.SetnxResponse, error) {
_, ok := d.lru.Get(key)
if ok {
return nil, errorx.New("the key already exists")
}
strValue := stringx.NewStringSingle()
strValue.Set(val)
err := d.lru.Add(key, strValue)
if err != nil {
return nil, err
}
return &proto.SetnxResponse{}, nil
}

View File

@ -1,12 +0,0 @@
package single
import (
"sync"
)
var (
oneSingleServer sync.Once
sysSingleServer *serverSingle
)
const timeOutDefault = 2

View File

@ -1,282 +0,0 @@
package single
import (
context "context"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/lru"
"gitee.com/timedb/wheatCache/pkg/proto"
)
func (s *serverSingle) LIndex(
ctx context.Context,
req *proto.LIndexRequest,
) (*proto.LIndexResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LINdex(req.Key, req.Index)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.LIndexResponse{
Result: resp.(string),
}, nil
}
func (s *serverSingle) LLen(
ctx context.Context,
req *proto.LLenRequest,
) (*proto.LLenResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LLen(req.Key)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.LLenResponse{
Length: resp.(int32),
}, nil
}
func (s *serverSingle) LPop(
ctx context.Context,
request *proto.LPopRequest,
) (*proto.LPopResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LPop(request.Key, request.Count)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.LPopResponse{
Results: resp.([]string),
}, nil
}
func (s *serverSingle) LPush(
ctx context.Context,
req *proto.LPushRequest,
) (*proto.LPushResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return nil, s.dao.LPush(req.Key, req.Values...)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
_, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.LPushResponse{}, nil
}
func (s *serverSingle) LPushX(
ctx context.Context,
req *proto.LPushXRequest,
) (*proto.LPushXResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return nil, s.dao.LPush(req.Key, req.Values...)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
_, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.LPushXResponse{}, nil
}
func (s *serverSingle) LRange(
ctx context.Context,
req *proto.LRangeRequest,
) (*proto.LRangeResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LRange(req.Key, req.Start, req.End)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.LRangeResponse{
Values: resp.([]string),
}, nil
}
func (s *serverSingle) LRem(
ctx context.Context,
req *proto.LRemRequest,
) (*proto.LRemResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LRemove(req.Key, req.Count, req.Value)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.LRemResponse{
Count: resp.(int32),
}, nil
}
func (s *serverSingle) LSet(
ctx context.Context,
req *proto.LSetRequest,
) (*proto.LSetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return nil, s.dao.LSet(req.Key, req.Index, req.Value)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
_, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.LSetResponse{}, nil
}
func (s *serverSingle) RPop(
ctx context.Context,
req *proto.RPopRequest,
) (*proto.RPopResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.RPop(req.Key, req.Count)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.RPopResponse{
Result: resp.([]string),
}, nil
}
func (s *serverSingle) LTrim(
ctx context.Context,
req *proto.LTrimRequest,
) (*proto.LTrimResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return nil, s.dao.LTrim(req.Key, req.Start, req.End)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
_, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.LTrimResponse{}, nil
}
func (s *serverSingle) RPush(
ctx context.Context,
req *proto.RPushRequest,
) (*proto.RPushResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return nil, s.dao.RPush(req.Key, req.Values...)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
_, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.RPushResponse{}, nil
}
func (s *serverSingle) RPushX(
ctx context.Context,
req *proto.RPushXRequest,
) (*proto.RPushXResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return nil, s.dao.RPushX(req.Key, req.Values...)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
_, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.RPushXResponse{}, nil
}

View File

@ -1,237 +0,0 @@
package single
import (
context "context"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/lru"
"gitee.com/timedb/wheatCache/pkg/proto"
)
func (s *serverSingle) Set(
ctx context.Context,
req *proto.SetRequest,
) (*proto.SetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Set(req.Key, req.Val)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.SetResponse{
Result: resp.(string),
}, nil
}
func (s *serverSingle) Get(
cxt context.Context,
req *proto.GetRequest,
) (*proto.GetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Get(req.Key)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.GetResponse{
Result: resp.(string),
}, nil
}
func (s serverSingle) Add(
cxt context.Context,
req *proto.AddRequest,
) (*proto.AddResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Add(req.Key, req.Renewal)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.AddResponse{
Result: resp.(string),
}, nil
}
func (s *serverSingle) Reduce(
cxt context.Context,
req *proto.ReduceRequest,
) (*proto.ReduceResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Add(req.Key, req.Renewal)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.ReduceResponse{
Result: resp.(string),
}, nil
}
func (s *serverSingle) SetBit(
cxt context.Context,
req *proto.SetBitRequest,
) (*proto.SetBitResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return nil, s.dao.Setbit(req.Key, req.Val, req.Offer)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(cxt, lruEvent)
_, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.SetBitResponse{}, nil
}
func (s *serverSingle) GetBit(
cxt context.Context,
req *proto.GetBitRequest,
) (*proto.GetBitResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.GetBit(req.Key, req.Offer)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(cxt, lruEvent)
flag, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.GetBitResponse{
Val: flag.(bool),
}, nil
}
func (s *serverSingle) GetRange(
ctx context.Context,
req *proto.GetRangeRequest,
) (*proto.GetRangeResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Getrange(req.Key, req.Start, req.End)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
flag, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.GetRangeResponse{
Result: flag.(string),
}, nil
}
func (s *serverSingle) GetSet(
ctx context.Context,
req *proto.GetSetRequest,
) (*proto.GetSetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Getset(req.Key, req.Val)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
result, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.GetSetResponse{
Result: result.(string),
}, nil
}
func (s *serverSingle) StrLen(
ctx context.Context,
req *proto.StrLenRequest,
) (*proto.StrLenResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Strlen(req.Key)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
flag, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.StrLenResponse{
Length: flag.(int32),
}, nil
}
func (s *serverSingle) Setnx(
ctx context.Context,
req *proto.SetnxRequest,
) (*proto.SetnxResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return nil, s.dao.Setnx(req.Key, req.Val)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
_, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return &proto.SetnxResponse{}, nil
}

12
storage/service/define.go Normal file
View File

@ -0,0 +1,12 @@
package service
import (
"sync"
)
var (
oneSingleServer sync.Once
sysSingleService *singleService
)
const timeOutDefault = 2

View File

@ -1,4 +1,4 @@
package single
package service
import (
"time"
@ -10,15 +10,15 @@ import (
"github.com/spf13/viper"
)
type serverSingle struct {
type singleService struct {
middleProduce event.ProduceInterface
lruProduce event.ProduceInterface
timeOut time.Duration
lruCache *lru.SingleCache
dao *dao.Dao
dao dao.Interface
}
func NewServer() proto.CommServerServer {
func NewSingleServer() proto.CommServerServer {
oneSingleServer.Do(func() {
timeOut := viper.GetInt("storage.timeOut")
if timeOut == 0 {
@ -27,14 +27,14 @@ func NewServer() proto.CommServerServer {
lruCache := lru.NewLRUCache()
ser := &serverSingle{
ser := &singleService{
lruCache: lruCache,
lruProduce: event.NewProduce(lruCache.GetDriver()),
timeOut: time.Duration(timeOut) * time.Second,
dao: dao.NewDao(lruCache),
}
sysSingleServer = ser
sysSingleService = ser
})
return sysSingleServer
return sysSingleService
}

View File

@ -0,0 +1,473 @@
// Code generated by gen-struct. DO NOT EDIT.
// make gen-service generated
package service
import (
context "context"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/lru"
"gitee.com/timedb/wheatCache/pkg/proto"
)
func (s *singleService) LIndex(
ctx context.Context,
req *proto.LIndexRequest,
) (*proto.LIndexResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LIndex(req.Key, req.Index)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.LIndexResponse), nil
}
func (s *singleService) LLen(
ctx context.Context,
req *proto.LLenRequest,
) (*proto.LLenResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LLen(req.Key)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.LLenResponse), nil
}
func (s *singleService) LPop(
ctx context.Context,
req *proto.LPopRequest,
) (*proto.LPopResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LPop(req.Key, req.Count)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.LPopResponse), nil
}
func (s *singleService) LPush(
ctx context.Context,
req *proto.LPushRequest,
) (*proto.LPushResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LPush(req.Key, req.Values)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.LPushResponse), nil
}
func (s *singleService) LPushX(
ctx context.Context,
req *proto.LPushXRequest,
) (*proto.LPushXResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LPushX(req.Key, req.Values)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.LPushXResponse), nil
}
func (s *singleService) LRange(
ctx context.Context,
req *proto.LRangeRequest,
) (*proto.LRangeResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LRange(req.Key, req.Start, req.End)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.LRangeResponse), nil
}
func (s *singleService) LRem(
ctx context.Context,
req *proto.LRemRequest,
) (*proto.LRemResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LRem(req.Key, req.Count, req.Value)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.LRemResponse), nil
}
func (s *singleService) LSet(
ctx context.Context,
req *proto.LSetRequest,
) (*proto.LSetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LSet(req.Key, req.Index, req.Value)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.LSetResponse), nil
}
func (s *singleService) RPop(
ctx context.Context,
req *proto.RPopRequest,
) (*proto.RPopResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.RPop(req.Key, req.Count)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.RPopResponse), nil
}
func (s *singleService) LTrim(
ctx context.Context,
req *proto.LTrimRequest,
) (*proto.LTrimResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.LTrim(req.Key, req.Start, req.End)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.LTrimResponse), nil
}
func (s *singleService) RPush(
ctx context.Context,
req *proto.RPushRequest,
) (*proto.RPushResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.RPush(req.Key, req.Values)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.RPushResponse), nil
}
func (s *singleService) RPushX(
ctx context.Context,
req *proto.RPushXRequest,
) (*proto.RPushXResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.RPushX(req.Key, req.Values)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.RPushXResponse), nil
}
func (s *singleService) Set(
ctx context.Context,
req *proto.SetRequest,
) (*proto.SetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Set(req.Key, req.Val)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.SetResponse), nil
}
func (s *singleService) Get(
ctx context.Context,
req *proto.GetRequest,
) (*proto.GetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Get(req.Key)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.GetResponse), nil
}
func (s *singleService) Add(
ctx context.Context,
req *proto.AddRequest,
) (*proto.AddResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Add(req.Key, req.Renewal)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.AddResponse), nil
}
func (s *singleService) Reduce(
ctx context.Context,
req *proto.ReduceRequest,
) (*proto.ReduceResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Reduce(req.Key, req.Renewal)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.ReduceResponse), nil
}
func (s *singleService) Setnx(
ctx context.Context,
req *proto.SetnxRequest,
) (*proto.SetnxResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Setnx(req.Key, req.Val)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.SetnxResponse), nil
}
func (s *singleService) SetBit(
ctx context.Context,
req *proto.SetBitRequest,
) (*proto.SetBitResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.SetBit(req.Key, req.Val, req.Offer)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.SetBitResponse), nil
}
func (s *singleService) GetBit(
ctx context.Context,
req *proto.GetBitRequest,
) (*proto.GetBitResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.GetBit(req.Key, req.Offer)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.GetBitResponse), nil
}
func (s *singleService) GetRange(
ctx context.Context,
req *proto.GetRangeRequest,
) (*proto.GetRangeResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.GetRange(req.Key, req.Start, req.End)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.GetRangeResponse), nil
}
func (s *singleService) GetSet(
ctx context.Context,
req *proto.GetSetRequest,
) (*proto.GetSetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.GetSet(req.Key, req.Val)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.GetSetResponse), nil
}
func (s *singleService) StrLen(
ctx context.Context,
req *proto.StrLenRequest,
) (*proto.StrLenResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.StrLen(req.Key)
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.StrLenResponse), nil
}

12
storage/temp/dao.template Normal file
View File

@ -0,0 +1,12 @@
// Code generated by gen-struct. DO NOT EDIT.
// make gen-service generated
package dao
import "gitee.com/timedb/wheatCache/pkg/proto"
type Interface interface {
{%for key in keys %}
{{key.method}}({% for req in key.option %} {{req[1]}}, {% endfor %}) (*proto.{{key.method}}Response, error)
{%- endfor %}
}

View File

@ -0,0 +1,35 @@
// Code generated by gen-struct. DO NOT EDIT.
// make gen-service generated
package service
import (
context "context"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/lru"
"gitee.com/timedb/wheatCache/pkg/proto"
)
{% for key in keys %}
func (s *singleService) {{key.method}}(
ctx context.Context,
req *proto.{{key.method}}Request,
) (*proto.{{key.method}}Response, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.{{key.method}}({% for opt in key.option %}req.{{opt[0]}}, {% endfor %})
})
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
s.lruProduce.Recovery(lruEvent)
if err != nil {
return nil, err
}
return resp.(*proto.{{key.method}}Response), nil
}
{% endfor %}