From 75bcc43e7efbcc269e6678b25ceff930158bdac8 Mon Sep 17 00:00:00 2001 From: Jerrod Carpenter <4128301+JerrodCarpenter@users.noreply.github.com> Date: Fri, 9 Jun 2023 21:35:35 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20SINTERCARD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/redis/commands/sets.rb | 13 +++++++++++++ lib/redis/distributed.rb | 9 +++++++++ test/lint/sets.rb | 10 ++++++++++ 3 files changed, 32 insertions(+) diff --git a/lib/redis/commands/sets.rb b/lib/redis/commands/sets.rb index e05751397..fcd538e57 100644 --- a/lib/redis/commands/sets.rb +++ b/lib/redis/commands/sets.rb @@ -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] keys keys pointing to sets to intersect + # @param [Interger] limits number of matches searched for + # @return [Array] 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] keys keys pointing to sets to unify diff --git a/lib/redis/distributed.rb b/lib/redis/distributed.rb index e92d417fa..d469a60e5 100644 --- a/lib/redis/distributed.rb +++ b/lib/redis/distributed.rb @@ -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) diff --git a/test/lint/sets.rb b/test/lint/sets.rb index bfaa5cf84..0c0d7711c 100644 --- a/test/lint/sets.rb +++ b/test/lint/sets.rb @@ -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'