Skip to content

Commit

Permalink
✨ Add ZINTERCARD
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrodCarpenter committed Jun 10, 2023
1 parent fde7873 commit 68c2a4e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/redis/commands/sorted_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,18 @@ def zinterstore(*args)
end
ruby2_keywords(:zinterstore) if respond_to?(:ruby2_keywords, true)

# This command is similar to ZINTER, but instead of returning the result set,
# it returns just the cardinality of the result.
#
# @param [Array<String>] keys list of sorted sets
# @param [Integer] limit will short circuit operation if limit is met
def zintercard(*keys, limit: nil)
args = [:zintercard, keys.size, keys]
args << "LIMIT" << limit if limit

send_command(args)
end

# Return the union of multiple sorted sets
#
# @example Retrieve the union of `2*zsetA` and `1*zsetB`
Expand Down
8 changes: 8 additions & 0 deletions lib/redis/distributed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,14 @@ def zinterstore(destination, *keys, **options)
end
end

# This command is similar to ZINTER, but instead of returning the result set,
# it returns just the cardinality of the result.
def zintercard(*keys, limit: nil)
ensure_same_node(:zintercard, keys) do |node|
node.zintercard(keys, limit)
end
end

# Return the union of multiple sorted sets.
def zunion(*keys, **options)
keys.flatten!(1)
Expand Down
11 changes: 11 additions & 0 deletions test/lint/sorted_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,17 @@ def test_zinterstore_expand
assert_equal 1, r.zinterstore('{1}baz', %w[{1}foo {1}bar], weights: [2.0, 3.0])
end

def test_zintercard
target_version('7.0') do
r.zadd('{1}foo', %w[0 a 1 b 2 c 3 d])
assert_equal 0, r.zintercard('{1}foo', '{2}foo2')

r.zadd('{2}foo', %w[0 a 1 b 2 c])
assert_equal 3, r.zintercard('{1}foo', '{2}foo2')
assert_equal 2, r.zintercard('{1}foo', '{2}foo2', limit: 2)
end
end

def test_zscan
r.zadd('foo', %w[0 a 1 b 2 c])
expected = ['0', [['a', 0.0], ['b', 1.0], ['c', 2.0]]]
Expand Down

0 comments on commit 68c2a4e

Please sign in to comment.