feat(dao): update dao interface

This commit is contained in:
bandl 2021-10-27 21:38:01 +08:00
parent bbc4c27027
commit 5f188eaddf
6 changed files with 431 additions and 388 deletions

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 +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) (string, error)
LLen(*proto.BaseKey) (int32, error)
LPop(*proto.BaseKey, int32) ([]string, error)
LPush(*proto.BaseKey, []string) error
LPushX(*proto.BaseKey, []string) error
LRange(*proto.BaseKey, int32, int32) ([]string, error)
LRem(*proto.BaseKey, int32, string) (int32, error)
LSet(*proto.BaseKey, int32, string) error
RPop(*proto.BaseKey, int32) ([]string, error)
LTrim(*proto.BaseKey, int32, int32) error
RPush(*proto.BaseKey, []string) error
RPushX(*proto.BaseKey, []string) error
Set(*proto.BaseKey, string) (string, error)
Get(*proto.BaseKey) (string, error)
Add(*proto.BaseKey, int32) (string, error)
Reduce(*proto.BaseKey, int32) (string, error)
Setnx(*proto.BaseKey, string) error
SetBit(*proto.BaseKey, bool, int32) error
GetBit(*proto.BaseKey, int32) (bool, error)
GetRange(*proto.BaseKey, int32, int32) (string, error)
GetSet(*proto.BaseKey, string) (string, error)
StrLen(*proto.BaseKey) (int32, error)
}

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

@ -0,0 +1,213 @@
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) (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
}
func (d *Dao) LRem(key *proto.BaseKey, i int32, s string) (int32, error) {
panic("implement me")
}

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

@ -0,0 +1,185 @@
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) (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
}

View File

@ -15,7 +15,7 @@ type serverSingle struct {
lruProduce event.ProduceInterface
timeOut time.Duration
lruCache *lru.SingleCache
dao *dao.Dao
dao dao.Interface
}
func NewServer() proto.CommServerServer {