Skip to content

Commit

Permalink
Add support for SINTERCARD (#1859)
Browse files Browse the repository at this point in the history
* add sintercard

* fix pr comment
  • Loading branch information
dvora-h committed Feb 2, 2022
1 parent 7ea1bf7 commit b541da6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,19 @@ def sinter(self, keys, *args):
args = list_or_args(keys, args)
return self.execute_command("SINTER", *args)

def sintercard(self, numkeys: int, keys: List[str], limit: int = 0) -> int:
"""
Return the cardinality of the intersect of multiple sets specified by ``keys`.
When LIMIT provided (defaults to 0 and means unlimited), if the intersection
cardinality reaches limit partway through the computation, the algorithm will
exit and yield limit as the cardinality
For more information check https://redis.io/commands/sintercard
"""
args = [numkeys, *keys, "LIMIT", limit]
return self.execute_command("SINTERCARD", *args)

def sinterstore(self, dest, keys, *args):
"""
Store the intersection of sets specified by ``keys`` into a new
Expand Down
9 changes: 9 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,15 @@ def test_sinter(self, r):
r.sadd("b", "2", "3")
assert r.sinter("a", "b") == {b"2", b"3"}

@pytest.mark.onlynoncluster
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
def test_sintercard(self, unstable_r):
unstable_r.sadd("a", 1, 2, 3)
unstable_r.sadd("b", 1, 2, 3)
unstable_r.sadd("c", 1, 3, 4)
assert unstable_r.sintercard(3, ["a", "b", "c"]) == 2
assert unstable_r.sintercard(3, ["a", "b", "c"], limit=1) == 1

@pytest.mark.onlynoncluster
def test_sinterstore(self, r):
r.sadd("a", "1", "2", "3")
Expand Down

0 comments on commit b541da6

Please sign in to comment.