Skip to content

Commit

Permalink
✨ Add SINTERCARD
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrodCarpenter committed Jun 10, 2023
1 parent fde7873 commit 75bcc43
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/redis/commands/sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ def sinterstore(destination, *keys)
send_command([:sinterstore, destination].concat(keys))
end

# This command is similar to SINTER, but instead of returning the result set,
# it returns just the cardinality of the result.
#
# @param [String, Array<String>] keys keys pointing to sets to intersect
# @param [Interger] limits number of matches searched for
# @return [Array<String>] cardinality of the intersection
def sintercard(*keys, limit: nil)
args = [:sintercard, keys.size, keys]
args << "LIMIT" << limit if limit

send_command(args)
end

# Add multiple sets.
#
# @param [String, Array<String>] keys keys pointing to sets to unify
Expand Down
9 changes: 9 additions & 0 deletions lib/redis/distributed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,15 @@ def sinterstore(destination, *keys)
end
end

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

# Add multiple sets.
def sunion(*keys)
keys.flatten!(1)
Expand Down
10 changes: 10 additions & 0 deletions test/lint/sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ def test_variadic_sinterstore_expand
assert_equal 1, r.sinterstore('{1}baz', '{1}foo', '{1}bar')
end

def test_sintercard
target_version("7.0") do
r.sadd '{1}foo', 's1'
r.sadd '{1}foo', 's2'
r.sadd '{1}bar', 's2'

assert_equal 1, r.sintercard('{1}foo', '{1}bar')
end
end

def test_sunion
r.sadd 'foo', 's1'
r.sadd 'foo', 's2'
Expand Down

0 comments on commit 75bcc43

Please sign in to comment.