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 XDEL command #1619

Merged
merged 2 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 @@ -47,6 +47,7 @@
* 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))
* Python: Added XDEL command ([#1619](https:/aws/glide-for-redis/pull/1619))

### Breaking Changes
* Node: Update XREAD to return a Map of Map ([#1494](https:/aws/glide-for-redis/pull/1494))
Expand Down
23 changes: 23 additions & 0 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2705,6 +2705,29 @@ async def xadd(

return cast(Optional[str], await self._execute_command(RequestType.XAdd, args))

async def xdel(self, key: str, ids: List[str]) -> int:
"""
Removes the specified entries by id from a stream, and returns the number of entries deleted.

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

Args:
key (str): The key of the stream.
ids (List[str]): An array of entry ids.

Returns:
int: The number of entries removed from the stream. This number may be less than the number of entries in
`ids`, if the specified `ids` don't exist in the stream.

Examples:
>>> await client.xdel("key", ["1538561698944-0", "1538561698944-1"])
2 # Stream marked 2 entries as deleted.
"""
return cast(
int,
await self._execute_command(RequestType.XDel, [key] + ids),
)

async def xtrim(
self,
key: str,
Expand Down
16 changes: 16 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,22 @@ def xadd(

return self.append_command(RequestType.XAdd, args)

def xdel(self: TTransaction, key: str, ids: List[str]) -> TTransaction:
"""
Removes the specified entries by id from a stream, and returns the number of entries deleted.

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

Args:
key (str): The key of the stream.
ids (List[str]): An array of entry ids.

Command response:
int: The number of entries removed from the stream. This number may be less than the number of entries in
`ids`, if the specified `ids` don't exist in the stream.
"""
return self.append_command(RequestType.XDel, [key] + ids)

def xtrim(
self: TTransaction,
key: str,
Expand Down
37 changes: 37 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4766,6 +4766,43 @@ async def test_xadd_xtrim_xlen(self, redis_client: TRedisClient):
with pytest.raises(RequestError):
await redis_client.xlen(string_key)

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_xdel(self, redis_client: TRedisClient):
key1 = get_random_string(10)
string_key = get_random_string(10)
non_existing_key = get_random_string(10)
stream_id1 = "0-1"
stream_id2 = "0-2"
stream_id3 = "0-3"

assert (
await redis_client.xadd(
key1, [("f1", "foo1"), ("f2", "foo2")], StreamAddOptions(stream_id1)
)
== stream_id1
)
assert (
await redis_client.xadd(
key1, [("f1", "foo1"), ("f2", "foo2")], StreamAddOptions(stream_id2)
)
== stream_id2
)
assert await redis_client.xlen(key1) == 2

# deletes one stream id, and ignores anything invalid
assert await redis_client.xdel(key1, [stream_id1, stream_id3]) == 1
assert await redis_client.xdel(non_existing_key, [stream_id3]) == 0

# invalid argument - id list should not be empty
with pytest.raises(RequestError):
await redis_client.xdel(key1, [])

# key exists, but it is not a stream
assert await redis_client.set(string_key, "foo") == OK
with pytest.raises(RequestError):
await redis_client.xdel(string_key, [stream_id3])

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_pfadd(self, redis_client: TRedisClient):
Expand Down
2 changes: 2 additions & 0 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ async def transaction_test(
args.append(2)
transaction.xtrim(key11, TrimByMinId(threshold="0-2", exact=True))
args.append(1)
transaction.xdel(key11, ["0-2", "0-3"])
args.append(1)

transaction.lpush(key17, ["2", "1", "4", "3", "a"])
args.append(5)
Expand Down
Loading