From fcdb2310fe230129bd2ebe9dcab16164c21f1e75 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Fri, 5 Nov 2021 20:44:09 +0800 Subject: [PATCH] feat(storage-dao): add hashx --- storage/dao/hashx.go | 166 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 148 insertions(+), 18 deletions(-) diff --git a/storage/dao/hashx.go b/storage/dao/hashx.go index 6edcab9..5b3c77c 100644 --- a/storage/dao/hashx.go +++ b/storage/dao/hashx.go @@ -1,39 +1,169 @@ package dao -import "gitee.com/wheat-os/wheatCache/pkg/proto" +import ( + "gitee.com/wheat-os/wheatCache/pkg/errorx" + "gitee.com/wheat-os/wheatCache/pkg/proto" + "gitee.com/wheat-os/wheatCache/pkg/structure" + "gitee.com/wheat-os/wheatCache/pkg/structure/hashx" +) func (d *Dao) HDel(key *proto.BaseKey, hKeys []string) (*proto.HDelResponse, error) { - panic("not implemented") // TODO: Implement + value, ok := d.lru.Get(key) + if !ok { + return nil, errorx.NotKeyErr(key.Key) + } + + hashVal, ok := value.(structure.HashXInterface) + if !ok { + return nil, errorx.DaoTypeErr("hashx") + } + + for _, hK := range hKeys { + up, err := hashVal.Del(hK) + if err != nil { + return nil, err + } + d.lru.UpdateLruSize(up) + } + + return &proto.HDelResponse{}, nil + } -func (d *Dao) HExists(_ *proto.BaseKey, _ string) (*proto.HExistsResponse, error) { - panic("not implemented") // TODO: Implement +func (d *Dao) HExists(key *proto.BaseKey, hKeys string) (*proto.HExistsResponse, error) { + value, ok := d.lru.Get(key) + if !ok { + return nil, errorx.NotKeyErr(key.Key) + } + + hashVal, ok := value.(structure.HashXInterface) + if !ok { + return nil, errorx.DaoTypeErr("hashx") + } + _, err := hashVal.Get(hKeys) + if err != nil { + return &proto.HExistsResponse{Exists: false}, nil + } + return &proto.HExistsResponse{Exists: true}, nil } -func (d *Dao) HGet(_ *proto.BaseKey, _ []string) (*proto.HGetResponse, error) { - panic("not implemented") // TODO: Implement +func (d *Dao) HGet(key *proto.BaseKey, hKeys []string) (*proto.HGetResponse, error) { + value, ok := d.lru.Get(key) + if !ok { + return nil, errorx.NotKeyErr(key.Key) + } + + hashVal, ok := value.(structure.HashXInterface) + if !ok { + return nil, errorx.DaoTypeErr("hashx") + } + + result := make(map[string]string) + for _, hK := range hKeys { + res, err := hashVal.Get(hK) + if err != nil { + return nil, err + } + result[hK] = res + } + return &proto.HGetResponse{Items: result}, nil } -func (d *Dao) HGetAll(_ *proto.BaseKey) (*proto.HGetAllResponse, error) { - panic("not implemented") // TODO: Implement +func (d *Dao) HGetAll(key *proto.BaseKey) (*proto.HGetAllResponse, error) { + value, ok := d.lru.Get(key) + if !ok { + return nil, errorx.NotKeyErr(key.Key) + } + + hashVal, ok := value.(structure.HashXInterface) + if !ok { + return nil, errorx.DaoTypeErr("hashx") + } + return &proto.HGetAllResponse{Items: hashVal.Item()}, nil } -func (d *Dao) HIncrBy(_ *proto.BaseKey, _ []string) (*proto.HIncrByResponse, error) { - panic("not implemented") // TODO: Implement +func (d *Dao) HIncrBy(key *proto.BaseKey, hKeys []string, renewal int32) (*proto.HIncrByResponse, error) { + value, ok := d.lru.Get(key) + if !ok { + return nil, errorx.NotKeyErr(key.Key) + } + + hashVal, ok := value.(structure.HashXInterface) + if !ok { + return nil, errorx.DaoTypeErr("hashx") + } + count, result, err := hashVal.Add(int(renewal), hKeys...) + return &proto.HIncrByResponse{ + Count: int32(count), + Values: result, + }, err } -func (d *Dao) HKeys(_ *proto.BaseKey) (*proto.HKeysResponse, error) { - panic("not implemented") // TODO: Implement +func (d *Dao) HKeys(key *proto.BaseKey) (*proto.HKeysResponse, error) { + value, ok := d.lru.Get(key) + if !ok { + return nil, errorx.NotKeyErr(key.Key) + } + + hashVal, ok := value.(structure.HashXInterface) + if !ok { + return nil, errorx.DaoTypeErr("hashx") + } + + return &proto.HKeysResponse{Keys: hashVal.Key()}, nil } -func (d *Dao) HLen(_ *proto.BaseKey) (*proto.HLenResponse, error) { - panic("not implemented") // TODO: Implement +func (d *Dao) HLen(key *proto.BaseKey) (*proto.HLenResponse, error) { + value, ok := d.lru.Get(key) + if !ok { + return nil, errorx.NotKeyErr(key.Key) + } + + hashVal, ok := value.(structure.HashXInterface) + if !ok { + return nil, errorx.DaoTypeErr("hashx") + } + + return &proto.HLenResponse{Length: int32(hashVal.Length())}, nil } -func (d *Dao) HSet(_ *proto.BaseKey, _ map[string]string) (*proto.HSetResponse, error) { - panic("not implemented") // TODO: Implement +func (d *Dao) HSet(key *proto.BaseKey, set map[string]string) (*proto.HSetResponse, error) { + value, ok := d.lru.Get(key) + if !ok { + hashVal := hashx.NewHashXSingle() + for k, v := range set { + hashVal.Set(k, v) + } + err := d.lru.Add(key, hashVal) + return &proto.HSetResponse{}, err + } + + hashVal, ok := value.(structure.HashXInterface) + if !ok { + return nil, errorx.DaoTypeErr("hashx") + } + for k, v := range set { + upLength := hashVal.Set(k, v) + d.lru.UpdateLruSize(upLength) + } + + return &proto.HSetResponse{}, nil } -func (d *Dao) HSetX(_ *proto.BaseKey, _ map[string]string) (*proto.HSetXResponse, error) { - panic("not implemented") // TODO: Implement +func (d *Dao) HSetX(key *proto.BaseKey, set map[string]string) (*proto.HSetXResponse, error) { + value, ok := d.lru.Get(key) + if !ok { + return &proto.HSetXResponse{}, errorx.NotKeyErr(key.Key) + } + + hashVal, ok := value.(structure.HashXInterface) + if !ok { + return nil, errorx.DaoTypeErr("hashx") + } + for k, v := range set { + upLength := hashVal.Set(k, v) + d.lru.UpdateLruSize(upLength) + } + + return &proto.HSetXResponse{}, nil }