Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(redis): added and impl ZADDNX command #3944

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions core/stores/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2074,6 +2074,45 @@
return
}

// Zaddnx is the implementation of redis zadd nx command.
func (s *Redis) Zaddnx(key string, score int64, value string) (val bool, err error) {
return s.ZaddnxCtx(context.Background(), key, score, value)
}

// ZaddnxCtx is the implementation of redis zadd nx command.
func (s *Redis) ZaddnxCtx(ctx context.Context, key string, score int64, value string) (val bool, err error) {
return s.ZaddnxFloatCtx(ctx, key, float64(score), value)
}

// ZaddnxFloat is the implementation of redis zaddnx command.
func (s *Redis) ZaddnxFloat(key string, score float64, value string) (bool, error) {
return s.ZaddFloatCtx(context.Background(), key, score, value)
}

// ZaddnxFloatCtx is the implementation of redis zaddnx command.
func (s *Redis) ZaddnxFloatCtx(ctx context.Context, key string, score float64, value string) (
val bool, err error) {
err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s)
if err != nil {
return err
}

Check warning on line 2099 in core/stores/redis/redis.go

View check run for this annotation

Codecov / codecov/patch

core/stores/redis/redis.go#L2098-L2099

Added lines #L2098 - L2099 were not covered by tests

v, err := conn.ZAddNX(ctx, key, red.Z{
Score: score,
Member: value,
}).Result()
if err != nil {
return err
}

Check warning on line 2107 in core/stores/redis/redis.go

View check run for this annotation

Codecov / codecov/patch

core/stores/redis/redis.go#L2106-L2107

Added lines #L2106 - L2107 were not covered by tests

val = v == 1
return nil
}, acceptable)

return
}

// Zadds is the implementation of redis zadds command.
func (s *Redis) Zadds(key string, ps ...Pair) (int64, error) {
return s.ZaddsCtx(context.Background(), key, ps...)
Expand Down
18 changes: 18 additions & 0 deletions core/stores/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,24 @@ func TestRedis_SortedSetByFloat64(t *testing.T) {
})
}

func TestRedis_Zaddnx(t *testing.T) {
runOnRedis(t, func(client *Redis) {
ok, err := client.Zadd("key", 1, "value1")
assert.Nil(t, err)
assert.True(t, ok)
ok, err = client.Zaddnx("key", 2, "value1")
assert.Nil(t, err)
assert.False(t, ok)

ok, err = client.ZaddFloat("key", 1.1, "value2")
assert.Nil(t, err)
assert.True(t, ok)
ok, err = client.ZaddnxFloat("key", 1.1, "value3")
assert.Nil(t, err)
assert.True(t, ok)
})
}

func TestRedis_IncrbyFloat(t *testing.T) {
runOnRedis(t, func(client *Redis) {
incrVal, err := client.IncrbyFloat("key", 0.002)
Expand Down
Loading