diff --git a/storage/dao/dao.go b/storage/dao/dao.go index 9849ac2..2c61c06 100644 --- a/storage/dao/dao.go +++ b/storage/dao/dao.go @@ -5,6 +5,7 @@ import ( "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" ) @@ -194,3 +195,206 @@ func (d *Dao) Setnx(key *proto.BaseKey, val string) error { } 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 +}