mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-10 02:28:13 -07:00
bitview: reorg part 10 + api changes
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use sonic_rs::{JsonValueTrait, Value};
|
||||
use serde_json::Value;
|
||||
|
||||
pub fn de_unquote_i64<'de, D>(deserializer: D) -> Result<Option<i64>, D::Error>
|
||||
where
|
||||
|
||||
@@ -16,8 +16,8 @@ use vecdb::{AnyCollectableVec, AnyStoredVec};
|
||||
|
||||
mod deser;
|
||||
mod format;
|
||||
mod ids;
|
||||
mod index;
|
||||
mod metrics;
|
||||
mod output;
|
||||
mod pagination;
|
||||
mod params;
|
||||
@@ -27,7 +27,7 @@ pub use format::Format;
|
||||
pub use index::Index;
|
||||
pub use output::{Output, Value};
|
||||
pub use pagination::{PaginatedIndexParam, PaginationParam};
|
||||
pub use params::{Params, ParamsOpt};
|
||||
pub use params::{Params, ParamsDeprec, ParamsOpt};
|
||||
use vecs::Vecs;
|
||||
|
||||
use crate::vecs::{IndexToVec, MetricToVec};
|
||||
@@ -65,7 +65,7 @@ impl<'a> Interface<'a> {
|
||||
}
|
||||
|
||||
pub fn search(&self, params: &Params) -> Result<Vec<(String, &&dyn AnyCollectableVec)>> {
|
||||
let ids = ¶ms.ids;
|
||||
let metrics = ¶ms.metrics;
|
||||
let index = params.index;
|
||||
|
||||
let ids_to_vec = self
|
||||
@@ -77,25 +77,25 @@ impl<'a> Interface<'a> {
|
||||
index
|
||||
)))?;
|
||||
|
||||
ids.iter()
|
||||
.map(|id| {
|
||||
let vec = ids_to_vec.get(id.as_str()).ok_or_else(|| {
|
||||
metrics.iter()
|
||||
.map(|metric| {
|
||||
let vec = ids_to_vec.get(metric.as_str()).ok_or_else(|| {
|
||||
let cached_errors = cached_errors();
|
||||
|
||||
if let Some(message) = cached_errors.get(id) {
|
||||
if let Some(message) = cached_errors.get(metric) {
|
||||
return Error::String(message)
|
||||
}
|
||||
|
||||
let mut message = format!(
|
||||
"No vec named \"{}\" indexed by \"{}\" found.\n",
|
||||
id,
|
||||
metric,
|
||||
index
|
||||
);
|
||||
|
||||
let mut matcher = Matcher::new(Config::DEFAULT);
|
||||
|
||||
let matches = Pattern::new(
|
||||
id.as_str(),
|
||||
metric.as_str(),
|
||||
CaseMatching::Ignore,
|
||||
Normalization::Smart,
|
||||
AtomKind::Fuzzy,
|
||||
@@ -111,33 +111,35 @@ impl<'a> Interface<'a> {
|
||||
&format!("\nMaybe you meant one of the following: {matches:#?} ?\n");
|
||||
}
|
||||
|
||||
if let Some(index_to_vec) = self.metric_to_index_to_vec().get(id.as_str()) {
|
||||
message += &format!("\nBut there is a vec named {id} which supports the following indexes: {:#?}\n", index_to_vec.keys());
|
||||
if let Some(index_to_vec) = self.metric_to_index_to_vec().get(metric.as_str()) {
|
||||
message += &format!("\nBut there is a vec named {metric} which supports the following indexes: {:#?}\n", index_to_vec.keys());
|
||||
}
|
||||
|
||||
cached_errors.insert(id.clone(), message.clone());
|
||||
cached_errors.insert(metric.clone(), message.clone());
|
||||
|
||||
Error::String(message)
|
||||
});
|
||||
vec.map(|vec| (id.clone(), vec))
|
||||
vec.map(|vec| (metric.clone(), vec))
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()
|
||||
}
|
||||
|
||||
pub fn format(
|
||||
&self,
|
||||
vecs: Vec<(String, &&dyn AnyCollectableVec)>,
|
||||
metrics: Vec<(String, &&dyn AnyCollectableVec)>,
|
||||
params: &ParamsOpt,
|
||||
) -> Result<Output> {
|
||||
let from = params.from().map(|from| {
|
||||
vecs.iter()
|
||||
metrics
|
||||
.iter()
|
||||
.map(|(_, v)| v.i64_to_usize(from))
|
||||
.min()
|
||||
.unwrap_or_default()
|
||||
});
|
||||
|
||||
let to = params.to().map(|to| {
|
||||
vecs.iter()
|
||||
metrics
|
||||
.iter()
|
||||
.map(|(_, v)| v.i64_to_usize(to))
|
||||
.min()
|
||||
.unwrap_or_default()
|
||||
@@ -147,8 +149,11 @@ impl<'a> Interface<'a> {
|
||||
|
||||
Ok(match format {
|
||||
Format::CSV => {
|
||||
let headers = vecs.iter().map(|(id, _)| id.as_str()).collect::<Vec<_>>();
|
||||
let mut values = vecs
|
||||
let headers = metrics
|
||||
.iter()
|
||||
.map(|(id, _)| id.as_str())
|
||||
.collect::<Vec<_>>();
|
||||
let mut values = metrics
|
||||
.iter()
|
||||
.map(|(_, vec)| Ok(vec.collect_range_string(from, to)?))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
@@ -190,7 +195,7 @@ impl<'a> Interface<'a> {
|
||||
Output::CSV(csv)
|
||||
}
|
||||
Format::JSON => {
|
||||
let mut values = vecs
|
||||
let mut values = metrics
|
||||
.iter()
|
||||
.map(|(_, vec)| -> Result<Vec<u8>> {
|
||||
Ok(vec.collect_range_json_bytes(from, to)?)
|
||||
|
||||
@@ -6,24 +6,29 @@ use serde::Deserialize;
|
||||
use sonic_rs::{JsonContainerTrait, JsonValueTrait, Value};
|
||||
|
||||
#[derive(Debug, Deref, JsonSchema)]
|
||||
pub struct MaybeIds(Vec<String>);
|
||||
pub struct MaybeMetrics(Vec<String>);
|
||||
|
||||
const MAX_VECS: usize = 32;
|
||||
const MAX_STRING_SIZE: usize = 64 * MAX_VECS;
|
||||
|
||||
impl From<String> for MaybeIds {
|
||||
impl From<String> for MaybeMetrics {
|
||||
fn from(value: String) -> Self {
|
||||
Self(vec![value])
|
||||
Self(vec![value.replace("-", "_").to_lowercase()])
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<Vec<&'a str>> for MaybeIds {
|
||||
impl<'a> From<Vec<&'a str>> for MaybeMetrics {
|
||||
fn from(value: Vec<&'a str>) -> Self {
|
||||
Self(value.iter().map(|s| s.to_string()).collect::<Vec<_>>())
|
||||
Self(
|
||||
value
|
||||
.iter()
|
||||
.map(|s| s.replace("-", "_").to_lowercase())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for MaybeIds {
|
||||
impl<'de> Deserialize<'de> for MaybeMetrics {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
@@ -32,7 +37,7 @@ impl<'de> Deserialize<'de> for MaybeIds {
|
||||
|
||||
if let Some(str) = value.as_str() {
|
||||
if str.len() <= MAX_STRING_SIZE {
|
||||
Ok(MaybeIds(sanitize_ids(
|
||||
Ok(MaybeMetrics(sanitize_metrics(
|
||||
str.split(",").map(|s| s.to_string()),
|
||||
)))
|
||||
} else {
|
||||
@@ -40,7 +45,7 @@ impl<'de> Deserialize<'de> for MaybeIds {
|
||||
}
|
||||
} else if let Some(vec) = value.as_array() {
|
||||
if vec.len() <= MAX_VECS {
|
||||
Ok(MaybeIds(sanitize_ids(
|
||||
Ok(MaybeMetrics(sanitize_metrics(
|
||||
vec.into_iter().map(|s| s.as_str().unwrap().to_string()),
|
||||
)))
|
||||
} else {
|
||||
@@ -52,14 +57,14 @@ impl<'de> Deserialize<'de> for MaybeIds {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for MaybeIds {
|
||||
impl fmt::Display for MaybeMetrics {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let s = self.0.join(",");
|
||||
write!(f, "{s}")
|
||||
}
|
||||
}
|
||||
|
||||
fn sanitize_ids(raw_ids: impl Iterator<Item = String>) -> Vec<String> {
|
||||
fn sanitize_metrics(raw_ids: impl Iterator<Item = String>) -> Vec<String> {
|
||||
let mut results = Vec::new();
|
||||
raw_ids.for_each(|s| {
|
||||
let mut current = String::new();
|
||||
@@ -6,23 +6,22 @@ use serde::Deserialize;
|
||||
use crate::{
|
||||
Format, Index,
|
||||
deser::{de_unquote_i64, de_unquote_usize},
|
||||
ids::MaybeIds,
|
||||
metrics::MaybeMetrics,
|
||||
};
|
||||
|
||||
#[derive(Debug, Deserialize, JsonSchema)]
|
||||
pub struct Params {
|
||||
#[serde(alias = "i")]
|
||||
#[schemars(description = "Index of requested vecs")]
|
||||
pub index: Index,
|
||||
#[serde(alias = "m")]
|
||||
#[schemars(description = "Requested metrics")]
|
||||
pub metrics: MaybeMetrics,
|
||||
|
||||
#[serde(alias = "v")]
|
||||
#[schemars(description = "Ids of requested vecs")]
|
||||
pub ids: MaybeIds,
|
||||
#[serde(alias = "i")]
|
||||
#[schemars(description = "Requested index")]
|
||||
pub index: Index,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub rest: ParamsOpt,
|
||||
}
|
||||
serde_with::flattened_maybe!(deserialize_rest, "rest");
|
||||
|
||||
impl Deref for Params {
|
||||
type Target = ParamsOpt;
|
||||
@@ -32,10 +31,10 @@ impl Deref for Params {
|
||||
}
|
||||
|
||||
impl From<((Index, String), ParamsOpt)> for Params {
|
||||
fn from(((index, id), rest): ((Index, String), ParamsOpt)) -> Self {
|
||||
fn from(((index, metric), rest): ((Index, String), ParamsOpt)) -> Self {
|
||||
Self {
|
||||
index,
|
||||
ids: MaybeIds::from(id),
|
||||
metrics: MaybeMetrics::from(metric),
|
||||
rest,
|
||||
}
|
||||
}
|
||||
@@ -106,3 +105,23 @@ impl ParamsOpt {
|
||||
self.format
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ParamsDeprec {
|
||||
#[serde(alias = "i")]
|
||||
pub index: Index,
|
||||
#[serde(alias = "v")]
|
||||
pub ids: MaybeMetrics,
|
||||
#[serde(flatten)]
|
||||
pub rest: ParamsOpt,
|
||||
}
|
||||
|
||||
impl From<ParamsDeprec> for Params {
|
||||
fn from(value: ParamsDeprec) -> Self {
|
||||
Params {
|
||||
index: value.index,
|
||||
metrics: value.ids,
|
||||
rest: value.rest,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user