diff --git a/pkg/structure/value.go b/pkg/structure/value.go index 3ab4fd4..3dfebb0 100644 --- a/pkg/structure/value.go +++ b/pkg/structure/value.go @@ -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) } diff --git a/storage/dao/stringx.go b/storage/dao/stringx.go index 21cadf9..87de94a 100644 --- a/storage/dao/stringx.go +++ b/storage/dao/stringx.go @@ -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 } diff --git a/storage/dao/stringx_test.go b/storage/dao/stringx_test.go index dab89b8..5d24a96 100644 --- a/storage/dao/stringx_test.go +++ b/storage/dao/stringx_test.go @@ -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) {