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

✨ Add SINTERCARD #1201

Closed
wants to merge 1 commit into from
Closed
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it return Integer type?
Seems to need changing it

Suggested change
# @return [Array<String>] cardinality of the intersection
# @return [Integer] 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you support both sintercard("key1", "key2") and sintercard(["key1", "key2"]) that should be tested.

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