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

[Redis 6.2] Add ZDIFF command #1044

Merged
merged 1 commit into from
Oct 22, 2021
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
24 changes: 24 additions & 0 deletions lib/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,30 @@ def zunionstore(destination, keys, weights: nil, aggregate: nil)
end
end

# Return the difference between the first and all successive input sorted sets
#
# @example
# redis.zadd("zsetA", [[1.0, "v1"], [2.0, "v2"]])
# redis.zadd("zsetB", [[3.0, "v2"], [2.0, "v3"]])
# redis.zdiff("zsetA", "zsetB")
# => ["v1"]
# @example With scores
# redis.zadd("zsetA", [[1.0, "v1"], [2.0, "v2"]])
# redis.zadd("zsetB", [[3.0, "v2"], [2.0, "v3"]])
# redis.zdiff("zsetA", "zsetB", :with_scores => true)
# => [["v1", 1.0]]
#
# @param [String, Array<String>] keys one or more keys to compute the difference
# @param [Hash] options
# - `:with_scores => true`: include scores in output
#
# @return [Array<String>, Array<[String, Float]>]
# - when `:with_scores` is not specified, an array of members
# - when `:with_scores` is specified, an array with `[member, score]` pairs
def zdiff(*keys, with_scores: false)
_zsets_operation(:zdiff, *keys, with_scores: with_scores)
end

# Get the number of fields in a hash.
#
# @param [String] key
Expand Down
7 changes: 7 additions & 0 deletions lib/redis/distributed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,13 @@ def zunionstore(destination, keys, **options)
end
end

# Return the difference between the first and all successive input sorted sets.
def zdiff(*keys, **options)
ensure_same_node(:zdiff, keys) do |node|
node.zdiff(*keys, **options)
end
end

# Get the number of fields in a hash.
def hlen(key)
node_for(key).hlen(key)
Expand Down
4 changes: 4 additions & 0 deletions test/cluster_commands_on_sorted_sets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ def test_zunionstore_with_aggregate
def test_zunionstore_with_weights
assert_raises(Redis::CommandError) { super }
end

def test_zdiff
assert_raises(Redis::CommandError) { super }
end
end
4 changes: 4 additions & 0 deletions test/distributed_commands_on_sorted_sets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,8 @@ def test_zunionstore_with_aggregate
def test_zunionstore_with_weights
assert_raises(Redis::Distributed::CannotDistribute) { super }
end

def test_zdiff
assert_raises(Redis::Distributed::CannotDistribute) { super }
end
end
15 changes: 15 additions & 0 deletions test/lint/sorted_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,21 @@ def test_zunionstore_expand
assert_equal 5, r.zunionstore('{1}baz', %w[{1}foo {1}bar])
end

def test_zdiff
target_version("6.2") do
r.zadd 'foo', 1, 's1'
r.zadd 'foo', 2, 's2'
r.zadd 'bar', 3, 's1'
r.zadd 'bar', 5, 's3'

assert_equal [], r.zdiff('foo', 'foo')
assert_equal ['s1', 's2'], r.zdiff('foo')

assert_equal ['s2'], r.zdiff('foo', 'bar')
assert_equal [['s2', 2.0]], r.zdiff('foo', 'bar', with_scores: true)
end
end

def test_zinter
target_version("6.2") do
r.zadd 'foo', 1, 's1'
Expand Down