Skip to content

Commit

Permalink
Changed to bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
GilboaAWS committed Jul 3, 2024
1 parent 63f547b commit 7a394d9
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 44 deletions.
1 change: 0 additions & 1 deletion glide-core/src/protobuf/redis_request.proto
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ enum RequestType {
Move = 174;
SInterCard = 175;
XRevRange = 176;
SortReadOnly = 177;
Copy = 178;
MSetNX = 179;
LPos = 180;
Expand Down
1 change: 0 additions & 1 deletion glide-core/src/request_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ pub enum RequestType {
Move = 174,
SInterCard = 175,
XRevRange = 176,
SortReadOnly = 177,
Copy = 178,
MSetNX = 179,
LPos = 180,
Expand Down
16 changes: 8 additions & 8 deletions python/python/glide/async_commands/cluster_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,11 +660,11 @@ async def sort(

async def sort_ro(
self,
key: str,
key: TEncodable,
limit: Optional[Limit] = None,
order: Optional[OrderBy] = None,
alpha: Optional[bool] = None,
) -> List[str]:
) -> List[bytes]:
"""
Sorts the elements in the list, set, or sorted set at `key` and returns the result.
This command is routed depending on the client's `ReadFrom` strategy.
Expand All @@ -674,31 +674,31 @@ async def sort_ro(
See https://valkey.io/commands/sort_ro for more details.
Args:
key (str): The key of the list, set, or sorted set to be sorted.
key (TEncodable): The key of the list, set, or sorted set to be sorted.
limit (Optional[Limit]): Limiting the range of the query by setting offset and result count. See `Limit` class for more information.
order (Optional[OrderBy]): Specifies the order to sort the elements.
Can be `OrderBy.ASC` (ascending) or `OrderBy.DESC` (descending).
alpha (Optional[bool]): When `True`, sorts elements lexicographically. When `False` (default), sorts elements numerically.
Use this when the list, set, or sorted set contains string values that cannot be converted into double precision floating point numbers.
Returns:
List[str]: A list of sorted elements.
List[bytes]: A list of sorted elements.
Examples:
>>> await client.lpush("mylist", '3', '1', '2')
>>> await client.sort_ro("mylist")
['1', '2', '3']
[b'1', b'2', b'3']
>>> await client.sort_ro("mylist", order=OrderBy.DESC)
['3', '2', '1']
[b'3', b'2', b'1']
>>> await client.lpush("mylist", '2', '1', '2', '3', '3', '1')
>>> await client.sort_ro("mylist", limit=Limit(2, 3))
['1', '2', '2']
[b'1', b'2', b'2']
>>> await client.lpush("mylist", "a", "b", "c", "d")
>>> await client.sort_ro("mylist", limit=Limit(2, 2), order=OrderBy.DESC, alpha=True)
['b', 'a']
[b'b', b'a']
Since: Redis version 7.0.0.
"""
Expand Down
26 changes: 13 additions & 13 deletions python/python/glide/async_commands/standalone_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,22 +488,22 @@ async def sort(

async def sort_ro(
self,
key: str,
by_pattern: Optional[str] = None,
key: TEncodable,
by_pattern: Optional[TEncodable] = None,
limit: Optional[Limit] = None,
get_patterns: Optional[List[str]] = None,
get_patterns: Optional[List[TEncodable]] = None,
order: Optional[OrderBy] = None,
alpha: Optional[bool] = None,
) -> List[Optional[str]]:
) -> List[Optional[bytes]]:
"""
Sorts the elements in the list, set, or sorted set at `key` and returns the result.
The `sort_ro` command can be used to sort elements based on different criteria and apply transformations on sorted elements.
See https://valkey.io/commands/sort_ro for more details.
Args:
key (str): The key of the list, set, or sorted set to be sorted.
by_pattern (Optional[str]): A pattern to sort by external keys instead of by the elements stored at the key themselves.
key (TEncodable): The key of the list, set, or sorted set to be sorted.
by_pattern (Optional[TEncodable]): A pattern to sort by external keys instead of by the elements stored at the key themselves.
The pattern should contain an asterisk (*) as a placeholder for the element values, where the value
from the key replaces the asterisk to create the key name. For example, if `key` contains IDs of objects,
`by_pattern` can be used to sort these IDs based on an attribute of the objects, like their weights or
Expand All @@ -512,7 +512,7 @@ async def sort_ro(
keys `weight_<element>`.
If not provided, elements are sorted by their value.
limit (Optional[Limit]): Limiting the range of the query by setting offset and result count. See `Limit` class for more information.
get_pattern (Optional[str]): A pattern used to retrieve external keys' values, instead of the elements at `key`.
get_pattern (Optional[TEncodable]): A pattern used to retrieve external keys' values, instead of the elements at `key`.
The pattern should contain an asterisk (*) as a placeholder for the element values, where the value
from `key` replaces the asterisk to create the key name. This allows the sorted elements to be
transformed based on the related keys values. For example, if `key` contains IDs of users, `get_pattern`
Expand All @@ -527,28 +527,28 @@ async def sort_ro(
Use this when the list, set, or sorted set contains string values that cannot be converted into double precision floating point
Returns:
List[Optional[str]]: Returns a list of sorted elements.
List[Optional[bytes]]: Returns a list of sorted elements.
Examples:
>>> await client.lpush("mylist", 3, 1, 2)
>>> await client.sort_ro("mylist")
['1', '2', '3']
[b'1', b'2', b'3']
>>> await client.sort_ro("mylist", order=OrderBy.DESC)
['3', '2', '1']
[b'3', b'2', b'1']
>>> await client.lpush("mylist2", 2, 1, 2, 3, 3, 1)
>>> await client.sort_ro("mylist2", limit=Limit(2, 3))
['2', '2', '3']
[b'2', b'2', b'3']
>>> await client.hset("user:1", "name", "Alice", "age", 30)
>>> await client.hset("user:2", "name", "Bob", "age", 25)
>>> await client.lpush("user_ids", 2, 1)
>>> await client.sort_ro("user_ids", by_pattern="user:*->age", get_patterns=["user:*->name"])
['Bob', 'Alice']
[b'Bob', b'Alice']
Since: Redis version 7.0.0.
"""
args = _build_sort_args(key, by_pattern, limit, get_patterns, order, alpha)
result = await self._execute_command(RequestType.SortReadOnly, args)
return cast(List[Optional[str]], result)
return cast(List[Optional[bytes]], result)

async def sort_store(
self,
Expand Down
20 changes: 10 additions & 10 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4649,10 +4649,10 @@ def sort(

def sort_ro(
self: TTransaction,
key: str,
by_pattern: Optional[str] = None,
key: TEncodable,
by_pattern: Optional[TEncodable] = None,
limit: Optional[Limit] = None,
get_patterns: Optional[List[str]] = None,
get_patterns: Optional[List[TEncodable]] = None,
order: Optional[OrderBy] = None,
alpha: Optional[bool] = None,
) -> TTransaction:
Expand All @@ -4663,8 +4663,8 @@ def sort_ro(
See https://valkey.io/commands/sort_ro for more details.
Args:
key (str): The key of the list, set, or sorted set to be sorted.
by_pattern (Optional[str]): A pattern to sort by external keys instead of by the elements stored at the key themselves.
key (TEncodable): The key of the list, set, or sorted set to be sorted.
by_pattern (Optional[TEncodable]): A pattern to sort by external keys instead of by the elements stored at the key themselves.
The pattern should contain an asterisk (*) as a placeholder for the element values, where the value
from the key replaces the asterisk to create the key name. For example, if `key` contains IDs of objects,
`by_pattern` can be used to sort these IDs based on an attribute of the objects, like their weights or
Expand All @@ -4673,7 +4673,7 @@ def sort_ro(
keys `weight_<element>`.
If not provided, elements are sorted by their value.
limit (Optional[Limit]): Limiting the range of the query by setting offset and result count. See `Limit` class for more information.
get_pattern (Optional[str]): A pattern used to retrieve external keys' values, instead of the elements at `key`.
get_pattern (Optional[TEncodable]): A pattern used to retrieve external keys' values, instead of the elements at `key`.
The pattern should contain an asterisk (*) as a placeholder for the element values, where the value
from `key` replaces the asterisk to create the key name. This allows the sorted elements to be
transformed based on the related keys values. For example, if `key` contains IDs of users, `get_pattern`
Expand All @@ -4688,7 +4688,7 @@ def sort_ro(
Use this when the list, set, or sorted set contains string values that cannot be converted into double precision floating point numbers.
Command response:
List[Optional[str]]: Returns a list of sorted elements.
List[Optional[bytes]]: Returns a list of sorted elements.
Since: Redis version 7.0.0.
"""
Expand Down Expand Up @@ -4837,7 +4837,7 @@ def sort(

def sort_ro(
self: TTransaction,
key: str,
key: TEncodable,
limit: Optional[Limit] = None,
order: Optional[OrderBy] = None,
alpha: Optional[bool] = None,
Expand All @@ -4848,15 +4848,15 @@ def sort_ro(
See https://valkey.io/commands/sort_ro for more details.
Args:
key (str): The key of the list, set, or sorted set to be sorted.
key (TEncodable): The key of the list, set, or sorted set to be sorted.
limit (Optional[Limit]): Limiting the range of the query by setting offset and result count. See `Limit` class for more information.
order (Optional[OrderBy]): Specifies the order to sort the elements.
Can be `OrderBy.ASC` (ascending) or `OrderBy.DESC` (descending).
alpha (Optional[bool]): When `True`, sorts elements lexicographically. When `False` (default), sorts elements numerically.
Use this when the list, set, or sorted set contains string values that cannot be converted into double precision floating point numbers.
Command response:
List[str]: A list of sorted elements.
List[bytes]: A list of sorted elements.
Since: Redis version 7.0.0.
"""
Expand Down
18 changes: 9 additions & 9 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4739,7 +4739,7 @@ async def test_sort_and_sort_store_with_get_or_by_args(
order=OrderBy.ASC,
alpha=True,
)
assert result_ro == ["Alice", "Bob"]
assert result_ro == [b"Alice", b"Bob"]

# Test sort_store with all arguments
sort_store_result = await redis_client.sort_store(
Expand Down Expand Up @@ -4772,7 +4772,7 @@ async def test_sort_and_sort_store_with_get_or_by_args(
get_patterns=["user:*->name"],
alpha=True,
)
assert result_ro == ["Dave", "Bob", "Alice", "Charlie", "Eve"]
assert result_ro == [b"Dave", b"Bob", b"Alice", b"Charlie", b"Eve"]

# Test sort with `by` argument with missing keys to sort by
assert await redis_client.lpush("user_ids", ["a"]) == 6
Expand All @@ -4793,7 +4793,7 @@ async def test_sort_and_sort_store_with_get_or_by_args(
get_patterns=["user:*->name"],
alpha=True,
)
assert result_ro == [None, "Dave", "Bob", "Alice", "Charlie", "Eve"]
assert result_ro == [None, b"Dave", b"Bob", b"Alice", b"Charlie", b"Eve"]

# Test sort with `by` argument with missing keys to sort by
result = await redis_client.sort(
Expand All @@ -4813,7 +4813,7 @@ async def test_sort_and_sort_store_with_get_or_by_args(
get_patterns=["user:*->age"],
alpha=True,
)
assert result_ro == [None, "30", "25", "35", "20", "40"]
assert result_ro == [None, b"30", b"25", b"35", b"20", b"40"]

# Test Limit with count 0
result = await redis_client.sort(
Expand Down Expand Up @@ -4868,23 +4868,23 @@ async def test_sort_and_sort_store_without_get_or_by_args(

if not skip_sort_ro_test:
result_ro = await redis_client.sort_ro(key)
assert result_ro == ["1", "2", "3", "4", "5"]
assert result_ro == [b"1", b"2", b"3", b"4", b"5"]

# limit argument
result = await redis_client.sort(key, limit=Limit(1, 3))
assert result == [b"2", b"3", b"4"]

if not skip_sort_ro_test:
result_ro = await redis_client.sort_ro(key, limit=Limit(1, 3))
assert result_ro == ["2", "3", "4"]
assert result_ro == [b"2", b"3", b"4"]

# order argument
result = await redis_client.sort(key, order=OrderBy.DESC)
assert result == [b"5", b"4", b"3", b"2", b"1"]

if not skip_sort_ro_test:
result_ro = await redis_client.sort_ro(key, order=OrderBy.DESC)
assert result_ro == ["5", "4", "3", "2", "1"]
assert result_ro == [b"5", b"4", b"3", b"2", b"1"]

assert await redis_client.lpush(key, ["a"]) == 6

Expand All @@ -4903,7 +4903,7 @@ async def test_sort_and_sort_store_without_get_or_by_args(

if not skip_sort_ro_test:
result_ro = await redis_client.sort_ro(key, alpha=True)
assert result_ro == ["1", "2", "3", "4", "5", "a"]
assert result_ro == [b"1", b"2", b"3", b"4", b"5", b"a"]

# Combining multiple arguments
result = await redis_client.sort(
Expand All @@ -4915,7 +4915,7 @@ async def test_sort_and_sort_store_without_get_or_by_args(
result_ro = await redis_client.sort_ro(
key, limit=Limit(1, 3), order=OrderBy.DESC, alpha=True
)
assert result_ro == ["5", "4", "3"]
assert result_ro == [b"5", b"4", b"3"]

# Test sort_store with combined arguments
sort_store_result = await redis_client.sort_store(
Expand Down
4 changes: 2 additions & 2 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,8 @@ async def test_standalone_transaction(self, redis_client: GlideClient):
assert isinstance(result[0], str)
assert "# Memory" in result[0]
assert result[1:5] == [OK, False, OK, value.encode()]
assert result[5:14] == [2, 2, 2, [b"Bob", b"Alice"], [b"Bob", b"Alice"], 2, OK, None, 0]
assert result[14:] == expected
assert result[5:13] == [2, 2, 2, [b"Bob", b"Alice"], 2, OK, None, 0]
assert result[13:] == expected

def test_transaction_clear(self):
transaction = Transaction()
Expand Down

0 comments on commit 7a394d9

Please sign in to comment.