mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-15 21:18:11 -07:00
query: add count param
This commit is contained in:
@@ -23,8 +23,11 @@ pub struct Params {
|
|||||||
pub from: Option<i64>,
|
pub from: Option<i64>,
|
||||||
#[clap(short, long, allow_hyphen_values = true)]
|
#[clap(short, long, allow_hyphen_values = true)]
|
||||||
#[serde(default, alias = "t")]
|
#[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<i64>,
|
pub to: Option<i64>,
|
||||||
|
#[serde(default, alias = "c")]
|
||||||
|
/// Number of values
|
||||||
|
pub count: Option<usize>,
|
||||||
#[clap(short = 'F', long)]
|
#[clap(short = 'F', long)]
|
||||||
/// Format of the output
|
/// Format of the output
|
||||||
pub format: Option<Format>,
|
pub format: Option<Format>,
|
||||||
|
|||||||
@@ -40,13 +40,27 @@ fn req_to_response_res(
|
|||||||
format,
|
format,
|
||||||
from,
|
from,
|
||||||
index,
|
index,
|
||||||
to,
|
mut to,
|
||||||
|
count,
|
||||||
values,
|
values,
|
||||||
}): AxumQuery<Params>,
|
}): AxumQuery<Params>,
|
||||||
AppState { query, .. }: AppState,
|
AppState { query, .. }: AppState,
|
||||||
) -> color_eyre::Result<Response> {
|
) -> color_eyre::Result<Response> {
|
||||||
let index = Index::try_from(index.as_str())?;
|
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(
|
let vecs = query.search(
|
||||||
index,
|
index,
|
||||||
&values.iter().map(|v| v.as_str()).collect::<Vec<_>>(),
|
&values.iter().map(|v| v.as_str()).collect::<Vec<_>>(),
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ where
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn i64_to_usize(i: i64, len: usize) -> usize {
|
fn i64_to_usize(i: i64, len: usize) -> usize {
|
||||||
if i >= 0 {
|
if i >= 0 {
|
||||||
i as usize
|
(i as usize).min(len)
|
||||||
} else {
|
} else {
|
||||||
let v = len as i64 + i;
|
let v = len as i64 + i;
|
||||||
if v < 0 { 0 } else { v as usize }
|
if v < 0 { 0 } else { v as usize }
|
||||||
|
|||||||
Reference in New Issue
Block a user