Skip to content

Commit

Permalink
Python: add XDEL command (valkey-io#1619)
Browse files Browse the repository at this point in the history
* Python: add XDEL command

* Fix formatting
  • Loading branch information
aaron-congo authored and cyip10 committed Jun 24, 2024
1 parent 8d9d2ab commit 2b1cc79
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
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://github.com/aws/glide-for-redis/pull/1612))
* Python: Added BITFIELD and BITFIELD_RO commands ([#1615](https://github.com/aws/glide-for-redis/pull/1615))
* Python: Added ZREVRANK command ([#1614](https://github.com/aws/glide-for-redis/pull/1614))
* Python: Added XDEL command ([#1619](https://github.com/aws/glide-for-redis/pull/1619))

### Breaking Changes
* Node: Update XREAD to return a Map of Map ([#1494](https://github.com/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

0 comments on commit 2b1cc79

Please sign in to comment.