remove decimal requirement

This commit is contained in:
finley
2023-06-20 22:22:18 +08:00
parent 91eb6495eb
commit 1a3b32d2d2
7 changed files with 25 additions and 41 deletions

View File

@@ -6,7 +6,6 @@ import (
"github.com/hdt3213/godis/interface/redis" "github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/protocol" "github.com/hdt3213/godis/redis/protocol"
"github.com/shopspring/decimal"
"strconv" "strconv"
"strings" "strings"
) )
@@ -394,7 +393,7 @@ func execHIncrByFloat(db *DB, args [][]byte) redis.Reply {
key := string(args[0]) key := string(args[0])
field := string(args[1]) field := string(args[1])
rawDelta := string(args[2]) rawDelta := string(args[2])
delta, err := decimal.NewFromString(rawDelta) delta, err := strconv.ParseFloat(rawDelta, 64)
if err != nil { if err != nil {
return protocol.MakeErrReply("ERR value is not a valid float") return protocol.MakeErrReply("ERR value is not a valid float")
} }
@@ -410,12 +409,12 @@ func execHIncrByFloat(db *DB, args [][]byte) redis.Reply {
dict.Put(field, args[2]) dict.Put(field, args[2])
return protocol.MakeBulkReply(args[2]) return protocol.MakeBulkReply(args[2])
} }
val, err := decimal.NewFromString(string(value.([]byte))) val, err := strconv.ParseFloat(string(value.([]byte)), 64)
if err != nil { if err != nil {
return protocol.MakeErrReply("ERR hash value is not a float") return protocol.MakeErrReply("ERR hash value is not a float")
} }
result := val.Add(delta) result := val + delta
resultBytes := []byte(result.String()) resultBytes := []byte(strconv.FormatFloat(result, 'f', -1, 64))
dict.Put(field, resultBytes) dict.Put(field, resultBytes)
db.addAof(utils.ToCmdLine3("hincrbyfloat", args...)) db.addAof(utils.ToCmdLine3("hincrbyfloat", args...))
return protocol.MakeBulkReply(resultBytes) return protocol.MakeBulkReply(resultBytes)

View File

@@ -5,6 +5,7 @@ import (
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/protocol" "github.com/hdt3213/godis/redis/protocol"
"github.com/hdt3213/godis/redis/protocol/asserts" "github.com/hdt3213/godis/redis/protocol/asserts"
"math"
"strconv" "strconv"
"testing" "testing"
) )
@@ -246,12 +247,18 @@ func TestHIncrBy(t *testing.T) {
} }
result = testDB.Exec(nil, utils.ToCmdLine("hincrbyfloat", key, "b", "1.2")) result = testDB.Exec(nil, utils.ToCmdLine("hincrbyfloat", key, "b", "1.2"))
if bulkResult, _ := result.(*protocol.BulkReply); string(bulkResult.Arg) != "1.2" { if bulkResult, ok := result.(*protocol.BulkReply); ok {
val, _ := strconv.ParseFloat(string(bulkResult.Arg), 10)
if math.Abs(val-1.2) > 1e-4 {
t.Error(fmt.Sprintf("expected %s, actually %s", "1.2", string(bulkResult.Arg))) t.Error(fmt.Sprintf("expected %s, actually %s", "1.2", string(bulkResult.Arg)))
} }
}
result = testDB.Exec(nil, utils.ToCmdLine("hincrbyfloat", key, "b", "1.2")) result = testDB.Exec(nil, utils.ToCmdLine("hincrbyfloat", key, "b", "1.2"))
if bulkResult, _ := result.(*protocol.BulkReply); string(bulkResult.Arg) != "2.4" { if bulkResult, ok := result.(*protocol.BulkReply); ok {
t.Error(fmt.Sprintf("expected %s, actually %s", "2.4", string(bulkResult.Arg))) val, _ := strconv.ParseFloat(string(bulkResult.Arg), 10)
if math.Abs(val-2.4) > 1e-4 {
t.Error(fmt.Sprintf("expected %s, actually %s", "1.2", string(bulkResult.Arg)))
}
} }
} }

View File

@@ -260,6 +260,9 @@ func TestKeys(t *testing.T) {
testDB.Exec(nil, utils.ToCmdLine("set", key, value)) testDB.Exec(nil, utils.ToCmdLine("set", key, value))
testDB.Exec(nil, utils.ToCmdLine("set", "a:"+key, value)) testDB.Exec(nil, utils.ToCmdLine("set", "a:"+key, value))
testDB.Exec(nil, utils.ToCmdLine("set", "b:"+key, value)) testDB.Exec(nil, utils.ToCmdLine("set", "b:"+key, value))
testDB.Exec(nil, utils.ToCmdLine("set", "b:"+key, value))
testDB.Exec(nil, utils.ToCmdLine("set", "c:"+key, value, "EX", "0"))
time.Sleep(time.Second)
result := testDB.Exec(nil, utils.ToCmdLine("keys", "*")) result := testDB.Exec(nil, utils.ToCmdLine("keys", "*"))
asserts.AssertMultiBulkReplySize(t, result, 3) asserts.AssertMultiBulkReplySize(t, result, 3)

View File

@@ -12,7 +12,6 @@ import (
"github.com/hdt3213/godis/interface/redis" "github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/protocol" "github.com/hdt3213/godis/redis/protocol"
"github.com/shopspring/decimal"
) )
func (db *DB) getAsString(key string) ([]byte, protocol.ErrorReply) { func (db *DB) getAsString(key string) ([]byte, protocol.ErrorReply) {
@@ -477,7 +476,7 @@ func execIncrBy(db *DB, args [][]byte) redis.Reply {
func execIncrByFloat(db *DB, args [][]byte) redis.Reply { func execIncrByFloat(db *DB, args [][]byte) redis.Reply {
key := string(args[0]) key := string(args[0])
rawDelta := string(args[1]) rawDelta := string(args[1])
delta, err := decimal.NewFromString(rawDelta) delta, err := strconv.ParseFloat(rawDelta, 64)
if err != nil { if err != nil {
return protocol.MakeErrReply("ERR value is not a valid float") return protocol.MakeErrReply("ERR value is not a valid float")
} }
@@ -487,11 +486,11 @@ func execIncrByFloat(db *DB, args [][]byte) redis.Reply {
return errReply return errReply
} }
if bytes != nil { if bytes != nil {
val, err := decimal.NewFromString(string(bytes)) val, err := strconv.ParseFloat(string(bytes), 64)
if err != nil { if err != nil {
return protocol.MakeErrReply("ERR value is not a valid float") return protocol.MakeErrReply("ERR value is not a valid float")
} }
resultBytes := []byte(val.Add(delta).String()) resultBytes := []byte(strconv.FormatFloat(val+delta, 'f', -1, 64))
db.PutEntity(key, &database.DataEntity{ db.PutEntity(key, &database.DataEntity{
Data: resultBytes, Data: resultBytes,
}) })

View File

@@ -5,6 +5,7 @@ import (
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/protocol" "github.com/hdt3213/godis/redis/protocol"
"github.com/hdt3213/godis/redis/protocol/asserts" "github.com/hdt3213/godis/redis/protocol/asserts"
"math"
"strconv" "strconv"
"testing" "testing"
) )
@@ -259,7 +260,7 @@ func TestIncr(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
if int(val) != expected { if math.Abs(val-float64(expected)) > 1e-4 {
t.Errorf("expect %d, actual: %d", expected, int(val)) t.Errorf("expect %d, actual: %d", expected, int(val))
return return
} }

11
go.mod
View File

@@ -4,15 +4,4 @@ go 1.17
require ( require (
github.com/hdt3213/rdb v1.0.10 github.com/hdt3213/rdb v1.0.10
github.com/shopspring/decimal v1.2.0
)
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/sys v0.8.0 // indirect
) )

18
go.sum
View File

@@ -1,24 +1,16 @@
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.8.7/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= github.com/bytedance/sonic v1.8.7/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/hdt3213/rdb v1.0.9 h1:x9uiLpgpLSgyKWo8WwYSc5hMg0vglo+u5i5dTnJW33Y=
github.com/hdt3213/rdb v1.0.9/go.mod h1:A1RWBSb4QGdX8fNs2bSoWxkzcWlWGbCC7OgOTFhPG+k=
github.com/hdt3213/rdb v1.0.10 h1:j0wJv6Cp1faMH3v5+u5SYa0MfBGOnOc5nn+JEYbIVxA= github.com/hdt3213/rdb v1.0.10 h1:j0wJv6Cp1faMH3v5+u5SYa0MfBGOnOc5nn+JEYbIVxA=
github.com/hdt3213/rdb v1.0.10/go.mod h1:A1RWBSb4QGdX8fNs2bSoWxkzcWlWGbCC7OgOTFhPG+k= github.com/hdt3213/rdb v1.0.10/go.mod h1:A1RWBSb4QGdX8fNs2bSoWxkzcWlWGbCC7OgOTFhPG+k=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@@ -26,14 +18,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=