From b52ac826c6be226d54d7fd75d0bc1b34ea4892ad Mon Sep 17 00:00:00 2001 From: Jerrod Carpenter <4128301+JerrodCarpenter@users.noreply.github.com> Date: Fri, 9 Jun 2023 21:08:58 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20ZINTERCARD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/redis/commands/sorted_sets.rb | 12 ++++++++++++ lib/redis/distributed.rb | 8 ++++++++ test/lint/sorted_sets.rb | 11 +++++++++++ 3 files changed, 31 insertions(+) diff --git a/lib/redis/commands/sorted_sets.rb b/lib/redis/commands/sorted_sets.rb index f07f95275..158c3d9fe 100644 --- a/lib/redis/commands/sorted_sets.rb +++ b/lib/redis/commands/sorted_sets.rb @@ -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] 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` diff --git a/lib/redis/distributed.rb b/lib/redis/distributed.rb index e92d417fa..0da328236 100644 --- a/lib/redis/distributed.rb +++ b/lib/redis/distributed.rb @@ -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) diff --git a/test/lint/sorted_sets.rb b/test/lint/sorted_sets.rb index 10f8ce5e9..09bdf728a 100644 --- a/test/lint/sorted_sets.rb +++ b/test/lint/sorted_sets.rb @@ -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}foo2', %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]]]