diff --git a/CHANGELOG.md b/CHANGELOG.md index 26f15bd..5619ab0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,8 @@ Because ReRu is pre-1.0, minor versions may include breaking changes. - `GETEX` validates option syntax first, then the key and type, and only then the expiry value, so a missing key replies nil and a wrong type replies WRONGTYPE without value errors. +- `XINFO STREAM` compares `FULL` and `COUNT` as C strings, treats `COUNT 0` as + unlimited, and falls back to the default count of 10 for a negative count. - `XINFO STREAM` argument-shape errors and per-group `NOGROUP` texts match Redis: `XGROUP`/`XINFO CONSUMERS` name the missing group, `XPENDING` keeps the combined key/group text, and malformed `XINFO STREAM` shapes report the diff --git a/docs/COMPATIBILITY.md b/docs/COMPATIBILITY.md index 9e8eb8b..9da2c45 100644 --- a/docs/COMPATIBILITY.md +++ b/docs/COMPATIBILITY.md @@ -389,8 +389,7 @@ Stream IDs are parsed as C strings: leading whitespace, a leading `+`, leading zeros, and a NUL terminator are accepted, while trailing bytes are not. Stream subcommand arity errors name the command or `parent|subcommand`, and unknown subcommands are reported. A few stream keyword comparisons remain exact rather -than C-string (`XADD`/`XTRIM`/`XGROUP`/`XAUTOCLAIM` and `XINFO` options with a -NUL suffix), +than C-string (`XADD`/`XTRIM`/`XGROUP`/`XAUTOCLAIM` options with a NUL suffix), `XAUTOCLAIM`'s start ID, `XGROUP SETID`, and `XREAD` IDs do not accept `+`/`-`, `XGROUP DESTROY` on a missing key replies 0, `ENTRIESREAD` uses the generic integer error and its reporting differs from Redis, and some `XTRIM`/`XREAD` @@ -401,7 +400,7 @@ nothing. These provide at-least-once processing primitives, not exactly-once application execution. `XINFO GROUPS/CONSUMERS/STREAM` exposes group cursors, pending counts, consumers, -entry counters and stream endpoints. STREAM FULL accepts COUNT (zero means all) +entry counters and stream endpoints. STREAM FULL accepts COUNT (zero means all; a negative count uses the default 10) and returns entries plus group/consumer pending lists with delivery timestamps. ReRu uses a vector for streams: both radix-tree allocation metrics report zero. Consumer diagnostics include the Redis 7.2+ inactive/active-time fields. Newer diff --git a/src/stream_groups.rs b/src/stream_groups.rs index f91cdb2..a123580 100644 --- a/src/stream_groups.rs +++ b/src/stream_groups.rs @@ -883,11 +883,19 @@ fn group_lag(stream: &Stream, group: &Group) -> Option { fn stream_info(c: &[Vec], store: &Store) -> Result> { let mut entries = store.entries.lock().unwrap(); let stream = stream_mut(&mut entries, &c[2])?; - let full = c.get(3).is_some_and(|v| v.eq_ignore_ascii_case(b"FULL")); + let full = c.get(3).is_some_and(|v| cstr_eq(v, b"FULL")); let count = match c.len() { 3 => 10, 4 if full => 10, - 6 if full && c[4].eq_ignore_ascii_case(b"COUNT") => number(&c[5])? as usize, + 6 if full && cstr_eq(&c[4], b"COUNT") => { + match protocol::parse_int(&c[5]) { + None => return Err(protocol::error("value is not an integer or out of range")), + // Zero means every entry; a negative count falls back to 10. + Some(0) => usize::MAX, + Some(value) if value < 0 => 10, + Some(value) => usize::try_from(value).unwrap_or(usize::MAX), + } + } _ => { return Err(protocol::error(&format!( "unknown subcommand or wrong number of arguments for '{}'. Try XINFO HELP.", @@ -895,7 +903,6 @@ fn stream_info(c: &[Vec], store: &Store) -> Result> { ))) } }; - let limit = if count == 0 { usize::MAX } else { count }; let mut fields = vec![ protocol::bulk(b"length"), integer(stream.entries.len() as u64), @@ -943,7 +950,7 @@ fn stream_info(c: &[Vec], store: &Store) -> Result> { stream .entries .iter() - .take(limit) + .take(count) .map(|entry| entry_reply(entry.id, Some(entry))) .collect(), ), @@ -957,7 +964,7 @@ fn stream_info(c: &[Vec], store: &Store) -> Result> { group .pending .iter() - .take(limit) + .take(count) .map(|(id, pending)| { array(vec![ protocol::bulk(id.to_string().as_bytes()), @@ -993,7 +1000,7 @@ fn stream_info(c: &[Vec], store: &Store) -> Result> { array( owned .into_iter() - .take(limit) + .take(count) .map(|(id, pending)| { array(vec![ protocol::bulk(id.to_string().as_bytes()), diff --git a/tests/stream_groups.rs b/tests/stream_groups.rs index 02917c9..78f60c4 100644 --- a/tests/stream_groups.rs +++ b/tests/stream_groups.rs @@ -506,10 +506,17 @@ fn stream_diagnostics_include_pending_owners_limits_and_deleted_metadata() { c.call(&[b"XINFO", b"STREAM", b"s", b"COUNT", b"1"]), Reply::Error(_) )); - assert!(matches!( - c.call(&[b"XINFO", b"STREAM", b"s", b"FULL", b"COUNT", b"-1"]), - Reply::Error(_) - )); + let full = c.call(&[b"XINFO", b"STREAM", b"s", b"FULL", b"COUNT", b"-1"]); + assert_eq!(rows(field(&full, b"entries")).len(), 2); + let full = c.call(&[ + b"XINFO", + b"STREAM", + b"s", + b"FULL\x00junk", + b"COUNT\x00junk", + b"1", + ]); + assert_eq!(rows(field(&full, b"entries")).len(), 1); c.call(&[b"MULTI"]); c.call(&[b"XLEN", b"s"]); assert_eq!(c.call(&[b"EXEC"]), array(vec![integer(2)])); @@ -1754,3 +1761,40 @@ fn stream_group_texts_name_the_group_and_xinfo_stream_shapes() { Reply::Error(b"ERR unknown subcommand 'stream'. Try XINFO HELP.".to_vec()) ); } + +#[test] +fn xinfo_stream_count_zero_is_unlimited_and_negative_uses_the_default() { + let server = Server::start(); + let mut c = Client::new(&server); + for index in 1..=11 { + let id = format!("{index}-0"); + assert!(matches!( + c.call(&[b"XADD", b"big", id.as_bytes(), b"f", b"v"]), + Reply::Bulk(Some(_)) + )); + } + + for count in [b"-1".as_slice(), b"-100", b"-9223372036854775808"] { + let full = c.call(&[b"XINFO", b"STREAM", b"big", b"FULL", b"COUNT", count]); + assert_eq!( + rows(field(&full, b"entries")).len(), + 10, + "{count:?} must fall back to the default count" + ); + } + for count in [b"0".as_slice(), b"9223372036854775807"] { + let full = c.call(&[b"XINFO", b"STREAM", b"big", b"FULL", b"COUNT", count]); + assert_eq!(rows(field(&full, b"entries")).len(), 11, "{count:?}"); + } + assert_eq!( + c.call(&[ + b"XINFO", + b"STREAM", + b"big", + b"FULL", + b"COUNT", + b"9223372036854775808", + ]), + Reply::Error(b"ERR value is not an integer or out of range".to_vec()) + ); +}