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

Python: add ZREVRANK command #1614

Merged
merged 3 commits into from
Jun 20, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* Python: Added BITPOS command ([#1604](https:/aws/glide-for-redis/pull/1604))
* Python: Added GETEX command ([#1612](https:/aws/glide-for-redis/pull/1612))
* Python: Added BITFIELD and BITFIELD_RO commands ([#1615](https:/aws/glide-for-redis/pull/1615))
* Python: Added ZREVRANK command ([#1614](https:/aws/glide-for-redis/pull/1614))

### Breaking Changes
* Node: Update XREAD to return a Map of Map ([#1494](https:/aws/glide-for-redis/pull/1494))
Expand Down
59 changes: 59 additions & 0 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3583,6 +3583,65 @@ async def zrank_withscore(
await self._execute_command(RequestType.ZRank, [key, member, "WITHSCORE"]),
)

async def zrevrank(self, key: str, member: str) -> Optional[int]:
"""
Returns the rank of `member` in the sorted set stored at `key`, where scores are ordered from the highest to
lowest, starting from `0`.

To get the rank of `member` with its score, see `zrevrank_withscore`.

See https://valkey.io/commands/zrevrank for more details.

Args:
key (str): The key of the sorted set.
member (str): The member whose rank is to be retrieved.

Returns:
Optional[int]: The rank of `member` in the sorted set, where ranks are ordered from high to low based on scores.
If `key` doesn't exist, or if `member` is not present in the set, `None` will be returned.

Examples:
>>> await client.zadd("my_sorted_set", {"member1": 10.5, "member2": 8.2, "member3": 9.6})
>>> await client.zrevrank("my_sorted_set", "member2")
2 # "member2" has the third-highest score in the sorted set "my_sorted_set"
"""
return cast(
Optional[int],
await self._execute_command(RequestType.ZRevRank, [key, member]),
)

async def zrevrank_withscore(
self, key: str, member: str
) -> Optional[List[Union[int, float]]]:
"""
Returns the rank of `member` in the sorted set stored at `key` with its score, where scores are ordered from the
highest to lowest, starting from `0`.

See https://valkey.io/commands/zrevrank for more details.

Args:
key (str): The key of the sorted set.
member (str): The member whose rank is to be retrieved.

Returns:
Optional[List[Union[int, float]]]: A list containing the rank (as `int`) and score (as `float`) of `member`
in the sorted set, where ranks are ordered from high to low based on scores.
If `key` doesn't exist, or if `member` is not present in the set, `None` will be returned.

Examples:
>>> await client.zadd("my_sorted_set", {"member1": 10.5, "member2": 8.2, "member3": 9.6})
>>> await client.zrevrank("my_sorted_set", "member2")
[2, 8.2] # "member2" with score 8.2 has the third-highest score in the sorted set "my_sorted_set"

Since: Redis version 7.2.0.
"""
return cast(
Optional[List[Union[int, float]]],
await self._execute_command(
RequestType.ZRevRank, [key, member, "WITHSCORE"]
),
)

async def zrem(
self,
key: str,
Expand Down
39 changes: 39 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2448,6 +2448,45 @@ def zrank_withscore(
"""
return self.append_command(RequestType.ZRank, [key, member, "WITHSCORE"])

def zrevrank(self: TTransaction, key: str, member: str) -> TTransaction:
"""
Returns the rank of `member` in the sorted set stored at `key`, where scores are ordered from the highest to
lowest, starting from `0`.

To get the rank of `member` with its score, see `zrevrank_withscore`.

See https://valkey.io/commands/zrevrank for more details.

Args:
key (str): The key of the sorted set.
member (str): The member whose rank is to be retrieved.

Command response:
Optional[int]: The rank of `member` in the sorted set, where ranks are ordered from high to low based on scores.
If `key` doesn't exist, or if `member` is not present in the set, `None` will be returned.
"""
return self.append_command(RequestType.ZRevRank, [key, member])

def zrevrank_withscore(self: TTransaction, key: str, member: str) -> TTransaction:
"""
Returns the rank of `member` in the sorted set stored at `key` with its score, where scores are ordered from the
highest to lowest, starting from `0`.

See https://valkey.io/commands/zrevrank for more details.

Args:
key (str): The key of the sorted set.
member (str): The member whose rank is to be retrieved.

Command response:
Optional[List[Union[int, float]]]: A list containing the rank (as `int`) and score (as `float`) of `member`
in the sorted set, where ranks are ordered from high to low based on scores.
If `key` doesn't exist, or if `member` is not present in the set, `None` will be returned.

Since: Redis version 7.2.0.
"""
return self.append_command(RequestType.ZRevRank, [key, member, "WITHSCORE"])

def zrem(
self: TTransaction,
key: str,
Expand Down
35 changes: 35 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4089,6 +4089,41 @@ async def test_zrank(self, redis_client: TRedisClient):
with pytest.raises(RequestError):
await redis_client.zrank(key, "one")

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_zrevrank(self, redis_client: TRedisClient):
key = get_random_string(10)
non_existing_key = get_random_string(10)
string_key = get_random_string(10)
member_scores = {"one": 1.0, "two": 2.0, "three": 3.0}

assert await redis_client.zadd(key, member_scores) == 3
assert await redis_client.zrevrank(key, "three") == 0
assert await redis_client.zrevrank(key, "non_existing_member") is None
assert (
await redis_client.zrevrank(non_existing_key, "non_existing_member") is None
)

if not check_if_server_version_lt(redis_client, "7.2.0"):
assert await redis_client.zrevrank_withscore(key, "one") == [2, 1.0]
assert (
await redis_client.zrevrank_withscore(key, "non_existing_member")
is None
)
assert (
await redis_client.zrevrank_withscore(
non_existing_key, "non_existing_member"
)
is None
)

# key exists, but it is not a sorted set
assert await redis_client.set(string_key, "foo") == OK
with pytest.raises(RequestError):
await redis_client.zrevrank(string_key, "member")
with pytest.raises(RequestError):
await redis_client.zrevrank_withscore(string_key, "member")

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_zdiff(self, redis_client: TRedisClient):
Expand Down
4 changes: 4 additions & 0 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,13 @@ async def transaction_test(
args.append(4)
transaction.zrank(key8, "one")
args.append(0)
transaction.zrevrank(key8, "one")
args.append(3)
if not await check_if_server_version_lt(redis_client, "7.2.0"):
transaction.zrank_withscore(key8, "one")
args.append([0, 1])
transaction.zrevrank_withscore(key8, "one")
args.append([3, 1])
transaction.zadd_incr(key8, "one", 3)
args.append(4)
transaction.zincrby(key8, 3, "one")
Expand Down
Loading