From 5f188eaddf8663c9b7102147254eeac89c348095 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Wed, 27 Oct 2021 21:38:01 +0800 Subject: [PATCH] feat(dao): update dao interface --- storage/dao/dao.go | 387 +------------------------------- storage/dao/define.go | 1 - storage/dao/interface.gen.go | 31 +++ storage/dao/listx.go | 213 ++++++++++++++++++ storage/dao/stringx.go | 185 +++++++++++++++ storage/server/single/single.go | 2 +- 6 files changed, 431 insertions(+), 388 deletions(-) delete mode 100644 storage/dao/define.go create mode 100644 storage/dao/interface.gen.go create mode 100644 storage/dao/listx.go create mode 100644 storage/dao/stringx.go diff --git a/storage/dao/dao.go b/storage/dao/dao.go index 2c61c06..de70569 100644 --- a/storage/dao/dao.go +++ b/storage/dao/dao.go @@ -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 -} diff --git a/storage/dao/define.go b/storage/dao/define.go deleted file mode 100644 index 07a0cc0..0000000 --- a/storage/dao/define.go +++ /dev/null @@ -1 +0,0 @@ -package dao diff --git a/storage/dao/interface.gen.go b/storage/dao/interface.gen.go new file mode 100644 index 0000000..217fe0d --- /dev/null +++ b/storage/dao/interface.gen.go @@ -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) +} diff --git a/storage/dao/listx.go b/storage/dao/listx.go new file mode 100644 index 0000000..1df204a --- /dev/null +++ b/storage/dao/listx.go @@ -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") +} diff --git a/storage/dao/stringx.go b/storage/dao/stringx.go new file mode 100644 index 0000000..7fc3ebd --- /dev/null +++ b/storage/dao/stringx.go @@ -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 +} diff --git a/storage/server/single/single.go b/storage/server/single/single.go index 0ebc17d..6b66c98 100644 --- a/storage/server/single/single.go +++ b/storage/server/single/single.go @@ -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 {