Skip to content

Commit

Permalink
zset score use int64
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Apr 30, 2024
1 parent 2d0d8ca commit c5ba685
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 82 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ qps: 892467.60
========== BatchSet ==========
size: 100*10000 enties
desc: key 10 bytes, value 10 bytes, 100 key-values a batch
cost: 379.674869ms
qps: 2633737.76
50th: 20317 ns
90th: 33609 ns
99th: 104627 ns
cost: 377.933308ms
qps: 2645850.27
50th: 20691 ns
90th: 32950 ns
99th: 95645 ns

========== Get ==========
size: 100*10000 enties
Expand Down Expand Up @@ -155,11 +155,11 @@ qps: 1091477.02

========== ZSet ==========
size: 100*10000 enties
desc: field 10 bytes, incr float64
cost: 1.321257444s
qps: 756843.75
50th: 1121 ns
90th: 1248 ns
99th: 2025 ns
desc: field 10 bytes, incr int64
cost: 1.209290877s
qps: 826918.57
50th: 1038 ns
90th: 1192 ns
99th: 2257 ns
```

22 changes: 11 additions & 11 deletions README_ZN.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ qps: 892467.60
========== BatchSet ==========
size: 100*10000 enties
desc: key 10 bytes, value 10 bytes, 100 key-values a batch
cost: 379.674869ms
qps: 2633737.76
50th: 20317 ns
90th: 33609 ns
99th: 104627 ns
cost: 377.933308ms
qps: 2645850.27
50th: 20691 ns
90th: 32950 ns
99th: 95645 ns

========== Get ==========
size: 100*10000 enties
Expand Down Expand Up @@ -157,11 +157,11 @@ qps: 1091477.02

========== ZSet ==========
size: 100*10000 enties
desc: field 10 bytes, incr float64
cost: 1.321257444s
qps: 756843.75
50th: 1121 ns
90th: 1248 ns
99th: 2025 ns
desc: field 10 bytes, incr int64
cost: 1.209290877s
qps: 826918.57
50th: 1038 ns
90th: 1192 ns
99th: 2257 ns
```

4 changes: 2 additions & 2 deletions benchmark/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func benchBitSet() {
func benchZSet() {
fmt.Println("========== ZSet ==========")
fmt.Println("size: 100*10000 enties")
fmt.Println("desc: field 10 bytes, incr float64")
fmt.Println("desc: field 10 bytes, incr int64")

quant := NewQuantile(N)
db := createDB()
Expand All @@ -237,7 +237,7 @@ func benchZSet() {
for i := 0; i < N; i++ {
t1 := time.Now()
k := fmt.Sprintf("%010d", i)
db.ZIncr("myzset", k, float64(i))
db.ZIncr("myzset", k, int64(i))
quant.Add(float64(time.Since(t1)))
}

Expand Down
6 changes: 0 additions & 6 deletions codeman/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package codeman

import (
"encoding/binary"
"math"

cache "github.com/xgzlucario/GigaCache"
"golang.org/x/exp/constraints"
Expand Down Expand Up @@ -67,11 +66,6 @@ func (s *Codec) Int(v int64) *Codec {
return s
}

func (s *Codec) Float(f float64) *Codec {
s.b = formatVarint(s.b, math.Float64bits(f))
return s
}

func (s *Codec) StrSlice(v []string) *Codec {
s.b = append(s.b, formatStrSlice(v)...)
return s
Expand Down
6 changes: 0 additions & 6 deletions codeman/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package codeman

import (
"encoding/binary"
"math"
)

type Reader struct {
Expand Down Expand Up @@ -86,8 +85,3 @@ func (s *Reader) Byte() byte {
r := s.readVarint()
return byte(r)
}

func (s *Reader) Float64() float64 {
r := s.readVarint()
return math.Float64frombits(r)
}
18 changes: 9 additions & 9 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ var cmdTable = []Cmd{
}},
{OpZAdd, func(db *DB, reader *codeman.Reader) error {
// key, field, score
return db.ZAdd(reader.Str(), reader.Str(), reader.Float64())
return db.ZAdd(reader.Str(), reader.Str(), reader.Int64())
}},
{OpZIncr, func(db *DB, reader *codeman.Reader) error {
// key, field, score
_, err := db.ZIncr(reader.Str(), reader.Str(), reader.Float64())
_, err := db.ZIncr(reader.Str(), reader.Str(), reader.Int64())
return err
}},
{OpZRemove, func(db *DB, reader *codeman.Reader) error {
Expand Down Expand Up @@ -812,7 +812,7 @@ func (db *DB) BitCount(key string) (uint64, error) {
}

// ZGet
func (db *DB) ZGet(zset, key string) (float64, error) {
func (db *DB) ZGet(zset, key string) (int64, error) {
zs, err := db.fetchZSet(zset)
if err != nil {
return 0, err
Expand All @@ -834,36 +834,36 @@ func (db *DB) ZCard(zset string) (int, error) {
}

// ZIter
func (db *DB) ZIter(zset string, f func(string, float64) bool) error {
func (db *DB) ZIter(zset string, f func(string, int64) bool) error {
zs, err := db.fetchZSet(zset)
if err != nil {
return err
}
zs.Iter(func(k string, s float64) bool {
zs.Iter(func(k string, s int64) bool {
return f(k, s)
})
return nil
}

// ZAdd
func (db *DB) ZAdd(zset, key string, score float64) error {
func (db *DB) ZAdd(zset, key string, score int64) error {
zs, err := db.fetchZSet(zset, true)
if err != nil {
return err
}
db.encode(newCodec(OpZAdd).Str(zset).Str(key).Float(score))
db.encode(newCodec(OpZAdd).Str(zset).Str(key).Int(score))
zs.Set(key, score)

return nil
}

// ZIncr
func (db *DB) ZIncr(zset, key string, incr float64) (float64, error) {
func (db *DB) ZIncr(zset, key string, incr int64) (int64, error) {
zs, err := db.fetchZSet(zset, true)
if err != nil {
return 0, err
}
db.encode(newCodec(OpZIncr).Str(zset).Str(key).Float(incr))
db.encode(newCodec(OpZIncr).Str(zset).Str(key).Int(incr))

return zs.Incr(key, incr), nil
}
Expand Down
16 changes: 8 additions & 8 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ func TestZSet(t *testing.T) {

// ZAdd
for i := 0; i < 1000; i++ {
err := db.ZAdd("zset", genKey(i), float64(i))
err := db.ZAdd("zset", genKey(i), int64(i))
assert.Nil(err)

// card
Expand All @@ -659,19 +659,19 @@ func TestZSet(t *testing.T) {
// zget
score, err := db.ZGet("zset", genKey(i))
assert.Nil(err)
assert.Equal(score, float64(i))
assert.Equal(score, int64(i))
}

// not exist
for i := 1000; i < 2000; i++ {
score, err := db.ZGet("zset", genKey(i))
assert.Equal(err, ErrKeyNotFound)
assert.Equal(score, float64(0))
assert.Equal(score, int64(0))
}

// iter
count := 0
err = db.ZIter("zset", func(key string, score float64) bool {
err = db.ZIter("zset", func(key string, score int64) bool {
count++
return count >= 1000
})
Expand All @@ -692,12 +692,12 @@ func TestZSet(t *testing.T) {
for i := 0; i < 1000; i++ {
num, err := db.ZIncr("zset", genKey(i), 3)
assert.Nil(err)
assert.Equal(num, float64(i+3))
assert.Equal(num, int64(i+3))
}
for i := 3000; i < 4000; i++ {
num, err := db.ZIncr("zset", genKey(i), 3)
assert.Nil(err)
assert.Equal(num, float64(3))
assert.Equal(num, int64(3))
}

// ZRemove
Expand Down Expand Up @@ -726,10 +726,10 @@ func TestZSet(t *testing.T) {
db.SAdd("set", "1")

n, err := db.ZGet("set", "1")
assert.Equal(n, float64(0))
assert.Equal(n, int64(0))
assert.ErrorContains(err, ErrWrongType.Error())

err = db.ZIter("set", func(key string, score float64) bool {
err = db.ZIter("set", func(key string, score int64) bool {
return false
})
assert.ErrorContains(err, ErrWrongType.Error())
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
require (
github.com/RoaringBitmap/roaring v1.9.3
github.com/bytedance/sonic v1.11.6
github.com/cockroachdb/swiss v0.0.0-20240303172742-c161743eb608
github.com/deckarep/golang-set/v2 v2.6.0
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/rosedblabs/wal v1.3.6
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/RoaringBitmap/roaring v1.9.3 h1:t4EbC5qQwnisr5PrP9nt0IRhRTb9gMUgQF4t4S2OByM=
github.com/RoaringBitmap/roaring v1.9.3/go.mod h1:6AXUsoIEzDTFFQCe1RbGA6uFONMhvejWj5rqITANK90=
github.com/aclements/go-perfevent v0.0.0-20240301234650-f7843625020f h1:JjxwchlOepwsUWcQwD2mLUAGE9aCp0/ehy6yCHFBOvo=
github.com/aclements/go-perfevent v0.0.0-20240301234650-f7843625020f/go.mod h1:tMDTce/yLLN/SK8gMOxQfnyeMeCg8KGzp0D1cbECEeo=
github.com/bits-and-blooms/bitset v1.12.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE=
github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
Expand All @@ -11,6 +13,8 @@ github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/cockroachdb/swiss v0.0.0-20240303172742-c161743eb608 h1:GXGrLNSC5+LGrwVgtczB6JCITxB9WGaLv7XilPkBDvc=
github.com/cockroachdb/swiss v0.0.0-20240303172742-c161743eb608/go.mod h1:yBRu/cnL4ks9bgy4vAASdjIW+/xMlFwuHKqtmh3GZQg=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
Loading

0 comments on commit c5ba685

Please sign in to comment.