Skip to content

Commit e498182

Browse files
authored
MINID and LIMIT support for xtrim (#1508)
1 parent 37e7c09 commit e498182

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

redis/client.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -2979,17 +2979,35 @@ def xrevrange(self, name, max='+', min='-', count=None):
29792979

29802980
return self.execute_command('XREVRANGE', name, *pieces)
29812981

2982-
def xtrim(self, name, maxlen, approximate=True):
2982+
def xtrim(self, name, maxlen=None, approximate=True, minid=None,
2983+
limit=None):
29832984
"""
29842985
Trims old messages from a stream.
29852986
name: name of the stream.
29862987
maxlen: truncate old stream messages beyond this size
29872988
approximate: actual stream length may be slightly more than maxlen
2989+
minin: the minimum id in the stream to query
2990+
limit: specifies the maximum number of entries to retrieve
29882991
"""
2989-
pieces = [b'MAXLEN']
2992+
pieces = []
2993+
if maxlen is not None and minid is not None:
2994+
raise DataError("Only one of ```maxlen``` or ```minid```",
2995+
"may be specified")
2996+
2997+
if maxlen is not None:
2998+
pieces.append(b'MAXLEN')
2999+
if minid is not None:
3000+
pieces.append(b'MINID')
29903001
if approximate:
29913002
pieces.append(b'~')
2992-
pieces.append(maxlen)
3003+
if maxlen is not None:
3004+
pieces.append(maxlen)
3005+
if minid is not None:
3006+
pieces.append(minid)
3007+
if limit is not None:
3008+
pieces.append(b"LIMIT")
3009+
pieces.append(limit)
3010+
29933011
return self.execute_command('XTRIM', name, *pieces)
29943012

29953013
# SORTED SET COMMANDS

tests/test_commands.py

+41
Original file line numberDiff line numberDiff line change
@@ -2927,6 +2927,47 @@ def test_xtrim(self, r):
29272927
# 1 message is trimmed
29282928
assert r.xtrim(stream, 3, approximate=False) == 1
29292929

2930+
@skip_if_server_version_lt('6.2.4')
2931+
def test_xtrim_minlen_and_length_args(self, r):
2932+
stream = 'stream'
2933+
2934+
r.xadd(stream, {'foo': 'bar'})
2935+
r.xadd(stream, {'foo': 'bar'})
2936+
r.xadd(stream, {'foo': 'bar'})
2937+
r.xadd(stream, {'foo': 'bar'})
2938+
2939+
# Future self: No limits without approximate, according to the api
2940+
with pytest.raises(redis.ResponseError):
2941+
assert r.xtrim(stream, 3, approximate=False, limit=2)
2942+
2943+
# maxlen with a limit
2944+
assert r.xtrim(stream, 3, approximate=True, limit=2) == 0
2945+
r.delete(stream)
2946+
2947+
with pytest.raises(redis.DataError):
2948+
assert r.xtrim(stream, maxlen=3, minid="sometestvalue")
2949+
2950+
# minid with a limit
2951+
m1 = r.xadd(stream, {'foo': 'bar'})
2952+
r.xadd(stream, {'foo': 'bar'})
2953+
r.xadd(stream, {'foo': 'bar'})
2954+
r.xadd(stream, {'foo': 'bar'})
2955+
assert r.xtrim(stream, None, approximate=True, minid=m1, limit=3) == 0
2956+
2957+
# pure minid
2958+
r.xadd(stream, {'foo': 'bar'})
2959+
r.xadd(stream, {'foo': 'bar'})
2960+
r.xadd(stream, {'foo': 'bar'})
2961+
m4 = r.xadd(stream, {'foo': 'bar'})
2962+
assert r.xtrim(stream, None, approximate=False, minid=m4) == 7
2963+
2964+
# minid approximate
2965+
r.xadd(stream, {'foo': 'bar'})
2966+
r.xadd(stream, {'foo': 'bar'})
2967+
m3 = r.xadd(stream, {'foo': 'bar'})
2968+
r.xadd(stream, {'foo': 'bar'})
2969+
assert r.xtrim(stream, None, approximate=True, minid=m3) == 0
2970+
29302971
def test_bitfield_operations(self, r):
29312972
# comments show affected bits
29322973
bf = r.bitfield('a')

0 commit comments

Comments
 (0)