mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-12 11:38:13 -07:00
bitview: reorg part 6
This commit is contained in:
@@ -30,7 +30,7 @@ pub use pagination::{PaginatedIndexParam, PaginationParam};
|
||||
pub use params::{IdParam, Params, ParamsOpt};
|
||||
use vecs::Vecs;
|
||||
|
||||
use crate::vecs::{IdToVec, IndexToVec};
|
||||
use crate::vecs::{IndexToVec, MetricToVec};
|
||||
|
||||
pub fn cached_errors() -> &'static Cache<String, String> {
|
||||
static CACHE: OnceLock<Cache<String, String>> = OnceLock::new();
|
||||
@@ -70,7 +70,7 @@ impl<'a> Interface<'a> {
|
||||
|
||||
let ids_to_vec = self
|
||||
.vecs
|
||||
.index_to_id_to_vec
|
||||
.index_to_metric_to_vec
|
||||
.get(&index)
|
||||
.ok_or(Error::String(format!(
|
||||
"Index \"{}\" isn't a valid index",
|
||||
@@ -111,7 +111,7 @@ impl<'a> Interface<'a> {
|
||||
&format!("\nMaybe you meant one of the following: {matches:#?} ?\n");
|
||||
}
|
||||
|
||||
if let Some(index_to_vec) = self.id_to_index_to_vec().get(id.as_str()) {
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -214,16 +214,16 @@ impl<'a> Interface<'a> {
|
||||
self.format(self.search(¶ms)?, ¶ms.rest)
|
||||
}
|
||||
|
||||
pub fn id_to_index_to_vec(&self) -> &BTreeMap<&str, IndexToVec<'_>> {
|
||||
&self.vecs.id_to_index_to_vec
|
||||
pub fn metric_to_index_to_vec(&self) -> &BTreeMap<&str, IndexToVec<'_>> {
|
||||
&self.vecs.metric_to_index_to_vec
|
||||
}
|
||||
|
||||
pub fn index_to_id_to_vec(&self) -> &BTreeMap<Index, IdToVec<'_>> {
|
||||
&self.vecs.index_to_id_to_vec
|
||||
pub fn index_to_metric_to_vec(&self) -> &BTreeMap<Index, MetricToVec<'_>> {
|
||||
&self.vecs.index_to_metric_to_vec
|
||||
}
|
||||
|
||||
pub fn get_vecid_count(&self) -> usize {
|
||||
self.vecs.id_count
|
||||
pub fn get_metric_count(&self) -> usize {
|
||||
self.vecs.metric_count
|
||||
}
|
||||
|
||||
pub fn get_index_count(&self) -> usize {
|
||||
@@ -242,8 +242,8 @@ impl<'a> Interface<'a> {
|
||||
&self.vecs.accepted_indexes
|
||||
}
|
||||
|
||||
pub fn get_vecids(&self, pagination: PaginationParam) -> &[&str] {
|
||||
self.vecs.ids(pagination)
|
||||
pub fn get_metrics(&self, pagination: PaginationParam) -> &[&str] {
|
||||
self.vecs.metrics(pagination)
|
||||
}
|
||||
|
||||
pub fn get_index_to_vecids(&self, paginated_index: PaginatedIndexParam) -> Vec<&str> {
|
||||
|
||||
@@ -11,16 +11,16 @@ use super::index::Index;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Vecs<'a> {
|
||||
pub id_to_index_to_vec: BTreeMap<&'a str, IndexToVec<'a>>,
|
||||
pub index_to_id_to_vec: BTreeMap<Index, IdToVec<'a>>,
|
||||
pub ids: Vec<&'a str>,
|
||||
pub metric_to_index_to_vec: BTreeMap<&'a str, IndexToVec<'a>>,
|
||||
pub index_to_metric_to_vec: BTreeMap<Index, MetricToVec<'a>>,
|
||||
pub metrics: Vec<&'a str>,
|
||||
pub indexes: Vec<&'static str>,
|
||||
pub accepted_indexes: BTreeMap<&'static str, &'static [&'static str]>,
|
||||
pub index_count: usize,
|
||||
pub id_count: usize,
|
||||
pub metric_count: usize,
|
||||
pub vec_count: usize,
|
||||
id_to_indexes: BTreeMap<&'a str, Vec<&'static str>>,
|
||||
indexes_to_ids: BTreeMap<Index, Vec<&'a str>>,
|
||||
metric_to_indexes: BTreeMap<&'a str, Vec<&'static str>>,
|
||||
index_to_metrics: BTreeMap<Index, Vec<&'a str>>,
|
||||
}
|
||||
|
||||
impl<'a> Vecs<'a> {
|
||||
@@ -33,7 +33,11 @@ impl<'a> Vecs<'a> {
|
||||
.iter_any_collectable()
|
||||
.for_each(|vec| this.insert(vec));
|
||||
|
||||
let mut ids = this.id_to_index_to_vec.keys().cloned().collect::<Vec<_>>();
|
||||
let mut ids = this
|
||||
.metric_to_index_to_vec
|
||||
.keys()
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let sort_ids = |ids: &mut Vec<&str>| {
|
||||
ids.sort_unstable_by(|a, b| {
|
||||
@@ -48,26 +52,26 @@ impl<'a> Vecs<'a> {
|
||||
|
||||
sort_ids(&mut ids);
|
||||
|
||||
this.ids = ids;
|
||||
this.id_count = this.id_to_index_to_vec.keys().count();
|
||||
this.index_count = this.index_to_id_to_vec.keys().count();
|
||||
this.metrics = ids;
|
||||
this.metric_count = this.metric_to_index_to_vec.keys().count();
|
||||
this.index_count = this.index_to_metric_to_vec.keys().count();
|
||||
this.vec_count = this
|
||||
.index_to_id_to_vec
|
||||
.index_to_metric_to_vec
|
||||
.values()
|
||||
.map(|tree| tree.len())
|
||||
.sum::<usize>();
|
||||
this.indexes = this
|
||||
.index_to_id_to_vec
|
||||
.index_to_metric_to_vec
|
||||
.keys()
|
||||
.map(|i| i.serialize_long())
|
||||
.collect::<Vec<_>>();
|
||||
this.accepted_indexes = this
|
||||
.index_to_id_to_vec
|
||||
.index_to_metric_to_vec
|
||||
.keys()
|
||||
.map(|i| (i.serialize_long(), i.possible_values()))
|
||||
.collect::<BTreeMap<_, _>>();
|
||||
this.id_to_indexes = this
|
||||
.id_to_index_to_vec
|
||||
this.metric_to_indexes = this
|
||||
.metric_to_index_to_vec
|
||||
.iter()
|
||||
.map(|(id, index_to_vec)| {
|
||||
(
|
||||
@@ -79,12 +83,12 @@ impl<'a> Vecs<'a> {
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
this.indexes_to_ids = this
|
||||
.index_to_id_to_vec
|
||||
this.index_to_metrics = this
|
||||
.index_to_metric_to_vec
|
||||
.iter()
|
||||
.map(|(index, id_to_vec)| (*index, id_to_vec.keys().cloned().collect::<Vec<_>>()))
|
||||
.collect();
|
||||
this.indexes_to_ids
|
||||
this.index_to_metrics
|
||||
.values_mut()
|
||||
.for_each(|ids| sort_ids(ids));
|
||||
|
||||
@@ -101,7 +105,7 @@ impl<'a> Vecs<'a> {
|
||||
})
|
||||
.unwrap();
|
||||
let prev = self
|
||||
.id_to_index_to_vec
|
||||
.metric_to_index_to_vec
|
||||
.entry(name)
|
||||
.or_default()
|
||||
.insert(index, vec);
|
||||
@@ -110,7 +114,7 @@ impl<'a> Vecs<'a> {
|
||||
panic!()
|
||||
}
|
||||
let prev = self
|
||||
.index_to_id_to_vec
|
||||
.index_to_metric_to_vec
|
||||
.entry(index)
|
||||
.or_default()
|
||||
.insert(name, vec);
|
||||
@@ -120,22 +124,22 @@ impl<'a> Vecs<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ids(&self, pagination: PaginationParam) -> &[&'_ str] {
|
||||
let len = self.ids.len();
|
||||
pub fn metrics(&self, pagination: PaginationParam) -> &[&'_ str] {
|
||||
let len = self.metrics.len();
|
||||
let start = pagination.start(len);
|
||||
let end = pagination.end(len);
|
||||
&self.ids[start..end]
|
||||
&self.metrics[start..end]
|
||||
}
|
||||
|
||||
pub fn id_to_indexes(&self, id: String) -> Option<&Vec<&'static str>> {
|
||||
self.id_to_indexes.get(id.as_str())
|
||||
self.metric_to_indexes.get(id.as_str())
|
||||
}
|
||||
|
||||
pub fn index_to_ids(
|
||||
&self,
|
||||
PaginatedIndexParam { index, pagination }: PaginatedIndexParam,
|
||||
) -> Vec<&'a str> {
|
||||
let vec = self.indexes_to_ids.get(&index).unwrap();
|
||||
let vec = self.index_to_metrics.get(&index).unwrap();
|
||||
|
||||
let len = vec.len();
|
||||
let start = pagination.start(len);
|
||||
@@ -149,4 +153,4 @@ impl<'a> Vecs<'a> {
|
||||
pub struct IndexToVec<'a>(BTreeMap<Index, &'a dyn AnyCollectableVec>);
|
||||
|
||||
#[derive(Default, Deref, DerefMut)]
|
||||
pub struct IdToVec<'a>(BTreeMap<&'a str, &'a dyn AnyCollectableVec>);
|
||||
pub struct MetricToVec<'a>(BTreeMap<&'a str, &'a dyn AnyCollectableVec>);
|
||||
|
||||
Reference in New Issue
Block a user