Skip to content

Commit

Permalink
Merge pull request #1309 from willbryant/xadd_minid
Browse files Browse the repository at this point in the history
Support the MINID option for XADD
  • Loading branch information
byroot authored Feb 18, 2025
2 parents 55725a2 + 4eb23d7 commit 2a4544c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/redis/commands/streams.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,25 @@ def xinfo(subcommand, key, group = nil)
# @param opts [Hash] several options for `XADD` command
#
# @option opts [String] :id the entry id, default value is `*`, it means auto generation
# @option opts [Integer] :maxlen max length of entries
# @option opts [Boolean] :approximate whether to add `~` modifier of maxlen or not
# @option opts [Integer] :maxlen max length of entries to keep
# @option opts [Integer] :minid min id of entries to keep
# @option opts [Boolean] :approximate whether to add `~` modifier of maxlen/minid or not
# @option opts [Boolean] :nomkstream whether to add NOMKSTREAM, default is not to add
#
# @return [String] the entry id
def xadd(key, entry, approximate: nil, maxlen: nil, nomkstream: nil, id: '*')
def xadd(key, entry, approximate: nil, maxlen: nil, minid: nil, nomkstream: nil, id: '*')
args = [:xadd, key]
args << 'NOMKSTREAM' if nomkstream
if maxlen
raise ArgumentError, "can't supply both maxlen and minid" if minid

args << "MAXLEN"
args << "~" if approximate
args << maxlen
elsif minid
args << "MINID"
args << "~" if approximate
args << minid
end
args << id
args.concat(entry.flatten)
Expand Down
10 changes: 10 additions & 0 deletions test/lint/streams.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ def test_xadd_with_maxlen_and_approximate_option
assert_match ENTRY_ID_FORMAT, actual
end

def test_xadd_with_minid_and_approximate_option
omit_version('6.2.0')
actual = redis.xadd('s1', { f1: 'v1', f2: 'v2' }, minid: '0-1', approximate: true)
assert_match ENTRY_ID_FORMAT, actual
end

def test_xadd_with_both_maxlen_and_minid
assert_raises(ArgumentError) { redis.xadd('s1', { f1: 'v1', f2: 'v2' }, maxlen: 2, minid: '0-1', approximate: true) }
end

def test_xadd_with_nomkstream_option
omit_version('6.2.0')

Expand Down

0 comments on commit 2a4544c

Please sign in to comment.