Skip to content

Commit

Permalink
TairZset: fix zadd score and member order (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangbodong22011 authored Aug 9, 2022
1 parent 3bb0871 commit 6ff382c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
1 change: 0 additions & 1 deletion tair/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func redisOptions() *redis.Options {
DialTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Password: "xxx",
MaxRetries: -1,

PoolSize: 10,
Expand Down
17 changes: 9 additions & 8 deletions tair/tairzset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tair
import (
"context"
"github.com/go-redis/redis/v8"
"strconv"
"strings"
)

Expand Down Expand Up @@ -62,12 +61,14 @@ type ExZAddMember struct {
Member string
}

func joinScoresToString(scores ...float64) string {
func joinScoresToString(scores ...string) string {
var builder strings.Builder
for _, score := range scores {
builder.WriteString(strconv.FormatFloat(score, 'E', -1, 64))
builder.WriteString(score)
builder.WriteString("#")
}
return builder.String()
strs := builder.String()
return strs[:len(strs)-1]
}

type ExZRangeArgs struct {
Expand Down Expand Up @@ -120,12 +121,12 @@ func (tc tairCmdable) exZAdd(ctx context.Context, key string, p *ExZAddArgs, mem
return cmd

}
func (tc tairCmdable) ExZAddManyScore(ctx context.Context, key string, member string, scores ...float64) *redis.IntCmd {
func (tc tairCmdable) ExZAddManyScore(ctx context.Context, key string, member string, scores ...string) *redis.IntCmd {
args := make([]interface{}, 4)
args[0] = "exzadd"
args[1] = key
args[2] = member
args[3] = joinScoresToString(scores...)
args[2] = joinScoresToString(scores...)
args[3] = member
cmd := redis.NewIntCmd(ctx, args...)
_ = tc(ctx, cmd)
return cmd
Expand Down Expand Up @@ -162,7 +163,7 @@ func (tc tairCmdable) ExZIncrBy(ctx context.Context, key string, score string, m
return cmd
}

func (tc tairCmdable) ExZIncrByManyScore(ctx context.Context, key string, member string, score ...float64) *redis.StringSliceCmd {
func (tc tairCmdable) ExZIncrByManyScore(ctx context.Context, key string, member string, score ...string) *redis.StringSliceCmd {
a := make([]interface{}, 4)
a[0] = "exzincryby"
a[1] = key
Expand Down
18 changes: 18 additions & 0 deletions tair/tairzset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ func (suite *TairZsetTestSuite) TestExZAdd() {
assert.Equal(suite.T(), res, int64(0))
}

func (suite *TairZsetTestSuite) TestExZAddManyScore() {
res, err := suite.tairClient.ExZAddManyScore(ctx, "k1", "a", "32", "21", "16").Result()
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), res, int64(1))

res, err = suite.tairClient.ExZAddManyScore(ctx, "k1", "d", "14", "4", "16").Result()
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), res, int64(1))

res, err = suite.tairClient.ExZAddManyScore(ctx, "k1", "c", "20", "7", "12").Result()
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), res, int64(1))

res2, err := suite.tairClient.ExZRange(ctx, "k1", 0, -1).Result()
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), res2, []string{"d", "c", "a"})
}

func (suite *TairZsetTestSuite) TestExZAddParams() {
res, err := suite.tairClient.ExZAddArgs(ctx, "foo", "1", "a", tair.ExZAddArgs{}.New().Xx()).Result()
assert.NoError(suite.T(), err)
Expand Down

0 comments on commit 6ff382c

Please sign in to comment.