feat(listx): update dao listx

This commit is contained in:
bandl 2021-10-27 23:30:45 +08:00
parent 511a66bd98
commit 19a0259f58
1 changed files with 67 additions and 60 deletions

View File

@ -7,34 +7,40 @@ import (
"gitee.com/timedb/wheatCache/pkg/structure/listx"
)
func (d *Dao) LIndex(key *proto.BaseKey, index int32) (string, error) {
func (d *Dao) LIndex(key *proto.BaseKey, index int32) (*proto.LIndexResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return "", errorx.KeyBaseIsNilErr()
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return "", errorx.DaoTypeErr("listx")
return nil, errorx.DaoTypeErr("listx")
}
return listVal.Index(int(index))
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) (int32, error) {
func (d *Dao) LLen(key *proto.BaseKey) (*proto.LLenResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return 0, errorx.KeyBaseIsNilErr()
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return 0, errorx.DaoTypeErr("listx")
return nil, errorx.DaoTypeErr("listx")
}
return int32(listVal.Length()), nil
return &proto.LLenResponse{Length: int32(listVal.Length())}, nil
}
func (d *Dao) LPop(key *proto.BaseKey, count int32) ([]string, error) {
func (d *Dao) LPop(key *proto.BaseKey, count int32) (*proto.LPopResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
@ -49,46 +55,30 @@ func (d *Dao) LPop(key *proto.BaseKey, count int32) ([]string, error) {
d.lru.UpdateLruSize(upLen)
return result, nil
return &proto.LPopResponse{Results: result}, nil
}
func (d *Dao) LPush(key *proto.BaseKey, values []string) error {
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...)
return d.lru.Add(key, list)
err := d.lru.Add(key, list)
return &proto.LPushResponse{}, err
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return errorx.DaoTypeErr("listx")
return nil, errorx.DaoTypeErr("listx")
}
upLen := listVal.LPush(values...)
d.lru.UpdateLruSize(upLen)
return nil
return &proto.LPushResponse{}, 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) {
func (d *Dao) LPushX(key *proto.BaseKey, values []string) (*proto.LPushXResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
@ -99,43 +89,64 @@ func (d *Dao) LRange(key *proto.BaseKey, start, end int32) ([]string, error) {
return nil, errorx.DaoTypeErr("listx")
}
return listVal.Range(int(start), int(end))
upLen := listVal.LPush(values...)
d.lru.UpdateLruSize(upLen)
return &proto.LPushXResponse{}, nil
}
func (d *Dao) LRemove(key *proto.BaseKey, count int32, value string) (int, error) {
func (d *Dao) LRange(key *proto.BaseKey, start, end int32) (*proto.LRangeResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return 0, errorx.KeyBaseIsNilErr()
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return 0, errorx.DaoTypeErr("listx")
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 remCount, nil
return &proto.LRemResponse{Count: int32(remCount)}, nil
}
func (d *Dao) LSet(key *proto.BaseKey, index int32, value string) error {
func (d *Dao) LSet(key *proto.BaseKey, index int32, value string) (*proto.LSetResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return errorx.KeyBaseIsNilErr()
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return errorx.DaoTypeErr("listx")
return nil, errorx.DaoTypeErr("listx")
}
upLen, err := listVal.Insert(int(index), false, value)
d.lru.UpdateLruSize(upLen)
return err
return &proto.LSetResponse{}, err
}
func (d *Dao) RPop(key *proto.BaseKey, count int32) ([]string, error) {
func (d *Dao) RPop(key *proto.BaseKey, count int32) (*proto.RPopResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.KeyBaseIsNilErr()
@ -150,64 +161,60 @@ func (d *Dao) RPop(key *proto.BaseKey, count int32) ([]string, error) {
d.lru.UpdateLruSize(upLen)
return result, nil
return &proto.RPopResponse{Result: result}, nil
}
func (d *Dao) LTrim(key *proto.BaseKey, start, end int32) error {
func (d *Dao) LTrim(key *proto.BaseKey, start, end int32) (*proto.LTrimResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return errorx.KeyBaseIsNilErr()
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return errorx.DaoTypeErr("listx")
return nil, errorx.DaoTypeErr("listx")
}
upLen, err := listVal.Slice(int(start), int(end))
if err != nil {
return err
return nil, err
}
d.lru.UpdateLruSize(upLen)
return nil
return &proto.LTrimResponse{}, nil
}
func (d *Dao) RPush(key *proto.BaseKey, values []string) error {
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 d.lru.Add(key, list)
return &proto.RPushResponse{}, d.lru.Add(key, list)
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return errorx.DaoTypeErr("listx")
return nil, errorx.DaoTypeErr("listx")
}
upLen := listVal.RPush(values...)
d.lru.UpdateLruSize(upLen)
return nil
return &proto.RPushResponse{}, nil
}
func (d *Dao) RPushX(key *proto.BaseKey, values []string) error {
func (d *Dao) RPushX(key *proto.BaseKey, values []string) (*proto.RPushXResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return errorx.KeyBaseIsNilErr()
return nil, errorx.KeyBaseIsNilErr()
}
listVal, ok := val.(structure.ListXInterface)
if !ok {
return errorx.DaoTypeErr("listx")
return nil, 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")
return &proto.RPushXResponse{}, nil
}