feat(storage-dao): add hashx

This commit is contained in:
bandl 2021-11-05 20:44:09 +08:00
parent 222bcc4eea
commit fcdb2310fe
1 changed files with 148 additions and 18 deletions

View File

@ -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)
}
func (d *Dao) HExists(_ *proto.BaseKey, _ string) (*proto.HExistsResponse, error) {
panic("not implemented") // TODO: Implement
hashVal, ok := value.(structure.HashXInterface)
if !ok {
return nil, errorx.DaoTypeErr("hashx")
}
func (d *Dao) HGet(_ *proto.BaseKey, _ []string) (*proto.HGetResponse, error) {
panic("not implemented") // TODO: Implement
for _, hK := range hKeys {
up, err := hashVal.Del(hK)
if err != nil {
return nil, err
}
d.lru.UpdateLruSize(up)
}
func (d *Dao) HGetAll(_ *proto.BaseKey) (*proto.HGetAllResponse, error) {
panic("not implemented") // TODO: Implement
return &proto.HDelResponse{}, nil
}
func (d *Dao) HIncrBy(_ *proto.BaseKey, _ []string) (*proto.HIncrByResponse, 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)
}
func (d *Dao) HKeys(_ *proto.BaseKey) (*proto.HKeysResponse, error) {
panic("not implemented") // TODO: Implement
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) HLen(_ *proto.BaseKey) (*proto.HLenResponse, 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)
}
func (d *Dao) HSet(_ *proto.BaseKey, _ map[string]string) (*proto.HSetResponse, error) {
panic("not implemented") // TODO: Implement
hashVal, ok := value.(structure.HashXInterface)
if !ok {
return nil, errorx.DaoTypeErr("hashx")
}
func (d *Dao) HSetX(_ *proto.BaseKey, _ map[string]string) (*proto.HSetXResponse, error) {
panic("not implemented") // TODO: Implement
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(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(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(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(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(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(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
}