Skip to content

zdiff and zdiffstore #1518

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

Merged
merged 6 commits into from
Jul 29, 2021
Merged
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
22 changes: 20 additions & 2 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,8 @@ class Redis:
lambda r: r and set(r) or set()
),
**string_keys_to_dict(
'ZPOPMAX ZPOPMIN ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE',
zset_score_pairs
'ZPOPMAX ZPOPMIN ZDIFF ZRANGE ZRANGEBYSCORE ZREVRANGE '
'ZREVRANGEBYSCORE', zset_score_pairs
),
**string_keys_to_dict('BZPOPMIN BZPOPMAX', \
lambda r:
Expand Down Expand Up @@ -2902,6 +2902,24 @@ def zcount(self, name, min, max):
"""
return self.execute_command('ZCOUNT', name, min, max)

def zdiff(self, keys, withscores=False):
"""
Returns the difference between the first and all successive input
sorted sets provided in ``keys``.
"""
pieces = [len(keys), *keys]
if withscores:
pieces.append("WITHSCORES")
return self.execute_command("ZDIFF", *pieces)

def zdiffstore(self, dest, keys):
"""
Computes the difference between the first and all successive input
sorted sets provided in ``keys`` and stores the result in ``dest``.
"""
pieces = [len(keys), *keys]
return self.execute_command("ZDIFFSTORE", dest, *pieces)

def zincrby(self, name, amount, value):
"Increment the score of ``value`` in sorted set ``name`` by ``amount``"
return self.execute_command('ZINCRBY', name, amount, value)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,21 @@ def test_zcount(self, r):
assert r.zcount('a', 1, '(' + str(2)) == 1
assert r.zcount('a', 10, 20) == 0

@skip_if_server_version_lt('6.2.0')
def test_zdiff(self, r):
r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3})
r.zadd('b', {'a1': 1, 'a2': 2})
assert r.zdiff(['a', 'b']) == [b'a3']
assert r.zdiff(['a', 'b'], withscores=True) == [b'a3', b'3']

@skip_if_server_version_lt('6.2.0')
def test_zdiffstore(self, r):
r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3})
r.zadd('b', {'a1': 1, 'a2': 2})
assert r.zdiffstore("out", ['a', 'b'])
assert r.zrange("out", 0, -1) == [b'a3']
assert r.zrange("out", 0, -1, withscores=True) == [(b'a3', 3.0)]

def test_zincrby(self, r):
r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3})
assert r.zincrby('a', 1, 'a2') == 3.0
Expand Down