forked from p93542168/wheat-cache
!100 修复 stringx 的一些 bug
Merge pull request !100 from bandl/fix-dao-stringx
This commit is contained in:
commit
a37097b3dd
|
@ -3,6 +3,7 @@ package structure
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
|
@ -175,12 +176,34 @@ func (v *Value) GetByte(offset int) (bool, error) {
|
|||
}
|
||||
|
||||
func (v *Value) SliceByString(start, end int) ([]byte, error) {
|
||||
if v.onType != DynamicString {
|
||||
return nil, errorx.New("not is string")
|
||||
}
|
||||
if start > end {
|
||||
return nil, errorx.New("the end cannot be greater than the beginning")
|
||||
}
|
||||
|
||||
if v.onType == DynamicInt {
|
||||
ret, err := v.ToInt()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value := strconv.Itoa(int(ret))
|
||||
if end > len(value) {
|
||||
return nil, errorx.New("the maximum index is exceeded, max index: %d", len(value))
|
||||
}
|
||||
return []byte(value[start:end]), nil
|
||||
}
|
||||
|
||||
if v.onType == DynamicFloat {
|
||||
ret, err := v.ToFloat64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value := fmt.Sprintf("%.2f", ret)
|
||||
if end > len(value) {
|
||||
return nil, errorx.New("the maximum index is exceeded, max index: %d", len(value))
|
||||
}
|
||||
return []byte(value[start:end]), nil
|
||||
}
|
||||
|
||||
if end > v.length {
|
||||
return nil, errorx.New("the maximum index is exceeded, max index: %d", v.length)
|
||||
}
|
||||
|
|
|
@ -147,19 +147,28 @@ func (d *Dao) GetRange(key *proto.BaseKey, start, end int32) (*proto.GetRangeRes
|
|||
|
||||
func (d *Dao) GetSet(key *proto.BaseKey, value string) (*proto.GetSetResponse, error) {
|
||||
val, ok := d.lru.Get(key)
|
||||
|
||||
var oldValue string
|
||||
|
||||
if !ok {
|
||||
return nil, errorx.NotKeyErr(key.Key)
|
||||
oldValue = ""
|
||||
strValue := stringx.NewStringSingle()
|
||||
strValue.Set(value)
|
||||
err := d.lru.Add(key, strValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
} else {
|
||||
strVal, ok := val.(structure.StringXInterface)
|
||||
if !ok {
|
||||
return nil, errorx.DaoTypeErr("stringx")
|
||||
}
|
||||
oldValue = strVal.Get()
|
||||
_, updateLength := strVal.Set(value)
|
||||
d.lru.UpdateLruSize(updateLength)
|
||||
}
|
||||
|
||||
strVal, ok := val.(structure.StringXInterface)
|
||||
if !ok {
|
||||
return nil, errorx.DaoTypeErr("stringx")
|
||||
}
|
||||
|
||||
oldValue := strVal.Get()
|
||||
|
||||
_, updateLength := strVal.Set(value)
|
||||
d.lru.UpdateLruSize(updateLength)
|
||||
return &proto.GetSetResponse{Result: oldValue}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -115,6 +115,11 @@ func TestDao_GetRange(t *testing.T) {
|
|||
_, err = dao.GetRange(baseKey, 0, 7)
|
||||
require.Error(t, err)
|
||||
|
||||
_, err = dao.Set(baseKey, "123456")
|
||||
require.NoError(t, err)
|
||||
resp, err = dao.GetRange(baseKey, 0, 3)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "123")
|
||||
}
|
||||
|
||||
func TestDao_GetSet(t *testing.T) {
|
||||
|
@ -124,14 +129,18 @@ func TestDao_GetSet(t *testing.T) {
|
|||
|
||||
_, err := dao.Set(baseKey, "a")
|
||||
require.NoError(t, err)
|
||||
resp, err := dao.GetSet(baseKey, "a")
|
||||
resp, err := dao.GetSet(baseKey, "ab")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "a")
|
||||
|
||||
// TODO 这个测试有问题,等待修复,问题详情: https://gitee.com/wheat-os/wheat-cache/issues/I4IV41
|
||||
resp, err = dao.GetSet(baseKey, "s")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, nil)
|
||||
require.Equal(t, resp.Result, "a")
|
||||
|
||||
l := proto.NewBaseKey("l")
|
||||
resp, err = dao.GetSet(l, "s")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "")
|
||||
}
|
||||
|
||||
func TestDao_StrLen(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue