server: use query for search

This commit is contained in:
nym21
2025-03-05 16:55:37 +01:00
parent b27297cdc6
commit d2ca6f1d46
10 changed files with 104 additions and 155 deletions

View File

@@ -6,7 +6,7 @@ use axum::{
http::{HeaderMap, StatusCode, Uri},
response::{IntoResponse, Response},
};
use brk_query::{Format, Index, Params};
use brk_query::{Format, Index, Output, Params};
use color_eyre::eyre::eyre;
use serde_json::Value;
@@ -34,7 +34,8 @@ pub async fn handler(
response
}
Err(error) => {
let mut response = (StatusCode::INTERNAL_SERVER_ERROR, error.to_string()).into_response();
let mut response =
(StatusCode::INTERNAL_SERVER_ERROR, error.to_string()).into_response();
log_result(response.status(), path, instant);
response.headers_mut().insert_cors();
response
@@ -53,89 +54,25 @@ fn req_to_response_res(
}): AxumQuery<Params>,
AppState { query, .. }: AppState,
) -> color_eyre::Result<Response> {
let indexes = index
.to_lowercase()
.split(",")
.flat_map(|s| Index::try_from(s).ok())
.collect::<Vec<_>>();
let index = Index::try_from(index.as_str())?;
if indexes.len() > 1 {
return Err(eyre!("Multiple indexes aren't supported"));
} else if indexes.is_empty() {
return Err(eyre!("Unknown index"));
}
let output = query.search(
index,
&values.iter().map(|v| v.as_str()).collect::<Vec<_>>(),
from,
to,
format,
)?;
let ids = values
.into_iter()
.map(|v| v.to_lowercase())
.flat_map(|v| v.split(",").map(|v| v.to_owned()).collect::<Vec<_>>())
.map(|s| {
let opt = query.vecid_to_index_to_vec.get(&s.replace("_", "-"));
(s, opt)
})
.filter(|(_, opt)| opt.is_some())
.map(|(id, vec)| (id, vec.unwrap()))
.collect::<Vec<_>>();
if ids.is_empty() {
return Ok(Json(()).into_response());
}
let values = ids
.iter()
.flat_map(|(_, i_to_v)| i_to_v.get(indexes.first().unwrap()))
.map(|vec| -> brk_vec::Result<Vec<Value>> { vec.collect_range_values(from, to) })
.collect::<brk_vec::Result<Vec<_>>>()?;
if ids.is_empty() {
return Ok(Json(()).into_response());
}
let ids_last_i = ids.len() - 1;
let mut response = match format {
Some(Format::CSV) | Some(Format::TSV) => {
let delimiter = if format == Some(Format::CSV) { ',' } else { '\t' };
let mut csv = ids
.into_iter()
.map(|(id, _)| id)
.collect::<Vec<_>>()
.join(&delimiter.to_string());
csv.push('\n');
let values_len = values.first().unwrap().len();
(0..values_len).for_each(|i| {
let mut line = "".to_string();
values.iter().enumerate().for_each(|(id_i, v)| {
line += &v.get(i).unwrap().to_string();
if id_i == ids_last_i {
line.push('\n');
} else {
line.push(delimiter);
}
});
csv += &line;
});
csv.into_response()
}
Some(Format::MD) => "".into_response(),
Some(Format::JSON) | None => {
if values.len() == 1 {
let values = values.first().unwrap();
if values.len() == 1 {
let value = values.first().unwrap();
Json(value).into_response()
} else {
Json(values).into_response()
}
} else {
Json(values).into_response()
}
}
let mut response = match output {
Output::CSV(s) => s.into_response(),
Output::TSV(s) => s.into_response(),
Output::Json(v) => match v {
brk_query::Value::Single(v) => Json(v).into_response(),
brk_query::Value::List(l) => Json(l).into_response(),
brk_query::Value::Matrix(m) => Json(m).into_response(),
},
Output::MD(s) => s.into_response(),
};
let headers = response.headers_mut();