diff --git a/crates/brk_query/src/params.rs b/crates/brk_query/src/params.rs index 7221b8532..64f648f24 100644 --- a/crates/brk_query/src/params.rs +++ b/crates/brk_query/src/params.rs @@ -23,8 +23,11 @@ pub struct Params { pub from: Option, #[clap(short, long, allow_hyphen_values = true)] #[serde(default, alias = "t")] - /// Inclusive ending index, if negative will be from the end + /// Exclusive ending index, if negative will be from the end, overrides 'count' pub to: Option, + #[serde(default, alias = "c")] + /// Number of values + pub count: Option, #[clap(short = 'F', long)] /// Format of the output pub format: Option, diff --git a/crates/brk_server/src/api/query/mod.rs b/crates/brk_server/src/api/query/mod.rs index 6fe308af3..9b301ea99 100644 --- a/crates/brk_server/src/api/query/mod.rs +++ b/crates/brk_server/src/api/query/mod.rs @@ -40,13 +40,27 @@ fn req_to_response_res( format, from, index, - to, + mut to, + count, values, }): AxumQuery, AppState { query, .. }: AppState, ) -> color_eyre::Result { let index = Index::try_from(index.as_str())?; + if to.is_none() { + if let Some(c) = count { + let c = c as i64; + if let Some(f) = from { + if f.is_positive() || f.abs() > c { + to.replace(f + c); + } + } else { + to.replace(c); + } + } + } + let vecs = query.search( index, &values.iter().map(|v| v.as_str()).collect::>(), diff --git a/crates/brk_vec/src/traits/collectable.rs b/crates/brk_vec/src/traits/collectable.rs index 22d7ddfa9..c643a9d73 100644 --- a/crates/brk_vec/src/traits/collectable.rs +++ b/crates/brk_vec/src/traits/collectable.rs @@ -27,7 +27,7 @@ where #[inline] fn i64_to_usize(i: i64, len: usize) -> usize { if i >= 0 { - i as usize + (i as usize).min(len) } else { let v = len as i64 + i; if v < 0 { 0 } else { v as usize }