Skip to content

Commit 295b547

Browse files
Support MINID and LIMIT on XADD (#1548)
* MINID and LIMIT
1 parent 7c77883 commit 295b547

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

redis/commands.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,24 +1640,40 @@ def xack(self, name, groupname, *ids):
16401640
return self.execute_command('XACK', name, groupname, *ids)
16411641

16421642
def xadd(self, name, fields, id='*', maxlen=None, approximate=True,
1643-
nomkstream=False):
1643+
nomkstream=False, minid=None, limit=None):
16441644
"""
16451645
Add to a stream.
16461646
name: name of the stream
16471647
fields: dict of field/value pairs to insert into the stream
16481648
id: Location to insert this record. By default it is appended.
1649-
maxlen: truncate old stream members beyond this size
1649+
maxlen: truncate old stream members beyond this size.
1650+
Can't be specify with minid.
1651+
minid: the minimum id in the stream to query.
1652+
Can't be specify with maxlen.
16501653
approximate: actual stream length may be slightly more than maxlen
16511654
nomkstream: When set to true, do not make a stream
1655+
limit: specifies the maximum number of entries to retrieve
16521656
"""
16531657
pieces = []
1658+
if maxlen is not None and minid is not None:
1659+
raise DataError("Only one of ```maxlen``` or ```minid```",
1660+
"may be specified")
1661+
16541662
if maxlen is not None:
16551663
if not isinstance(maxlen, int) or maxlen < 1:
16561664
raise DataError('XADD maxlen must be a positive integer')
16571665
pieces.append(b'MAXLEN')
16581666
if approximate:
16591667
pieces.append(b'~')
16601668
pieces.append(str(maxlen))
1669+
if minid is not None:
1670+
pieces.append(b'MINID')
1671+
if approximate:
1672+
pieces.append(b'~')
1673+
pieces.append(minid)
1674+
if limit is not None:
1675+
pieces.append(b"LIMIT")
1676+
pieces.append(limit)
16611677
if nomkstream:
16621678
pieces.append(b'NOMKSTREAM')
16631679
pieces.append(id)
@@ -2002,7 +2018,7 @@ def xtrim(self, name, maxlen=None, approximate=True, minid=None,
20022018
name: name of the stream.
20032019
maxlen: truncate old stream messages beyond this size
20042020
approximate: actual stream length may be slightly more than maxlen
2005-
minin: the minimum id in the stream to query
2021+
minid: the minimum id in the stream to query
20062022
limit: specifies the maximum number of entries to retrieve
20072023
"""
20082024
pieces = []

tests/test_commands.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,56 @@ def test_xadd_nomkstream(self, r):
24222422
r.xadd(stream, {'some': 'other'}, nomkstream=True)
24232423
assert r.xlen(stream) == 3
24242424

2425+
@skip_if_server_version_lt('6.2.0')
2426+
def test_xadd_minlen_and_limit(self, r):
2427+
stream = 'stream'
2428+
2429+
r.xadd(stream, {'foo': 'bar'})
2430+
r.xadd(stream, {'foo': 'bar'})
2431+
r.xadd(stream, {'foo': 'bar'})
2432+
r.xadd(stream, {'foo': 'bar'})
2433+
2434+
# Future self: No limits without approximate, according to the api
2435+
with pytest.raises(redis.ResponseError):
2436+
assert r.xadd(stream, {'foo': 'bar'}, maxlen=3,
2437+
approximate=False, limit=2)
2438+
2439+
# limit can not be provided without maxlen or minid
2440+
with pytest.raises(redis.ResponseError):
2441+
assert r.xadd(stream, {'foo': 'bar'}, limit=2)
2442+
2443+
# maxlen with a limit
2444+
assert r.xadd(stream, {'foo': 'bar'}, maxlen=3,
2445+
approximate=True, limit=2)
2446+
r.delete(stream)
2447+
2448+
# maxlen and minid can not be provided together
2449+
with pytest.raises(redis.DataError):
2450+
assert r.xadd(stream, {'foo': 'bar'}, maxlen=3,
2451+
minid="sometestvalue")
2452+
2453+
# minid with a limit
2454+
m1 = r.xadd(stream, {'foo': 'bar'})
2455+
r.xadd(stream, {'foo': 'bar'})
2456+
r.xadd(stream, {'foo': 'bar'})
2457+
r.xadd(stream, {'foo': 'bar'})
2458+
assert r.xadd(stream, {'foo': 'bar'}, approximate=True,
2459+
minid=m1, limit=3)
2460+
2461+
# pure minid
2462+
r.xadd(stream, {'foo': 'bar'})
2463+
r.xadd(stream, {'foo': 'bar'})
2464+
r.xadd(stream, {'foo': 'bar'})
2465+
m4 = r.xadd(stream, {'foo': 'bar'})
2466+
assert r.xadd(stream, {'foo': 'bar'}, approximate=False, minid=m4)
2467+
2468+
# minid approximate
2469+
r.xadd(stream, {'foo': 'bar'})
2470+
r.xadd(stream, {'foo': 'bar'})
2471+
m3 = r.xadd(stream, {'foo': 'bar'})
2472+
r.xadd(stream, {'foo': 'bar'})
2473+
assert r.xadd(stream, {'foo': 'bar'}, approximate=True, minid=m3)
2474+
24252475
@skip_if_server_version_lt('6.2.0')
24262476
def test_xautoclaim(self, r):
24272477
stream = 'stream'

0 commit comments

Comments
 (0)