2024-05-11 23:23:00 +02:00
|
|
|
use hyperlog_core::log::GraphItem;
|
|
|
|
|
2024-05-14 20:58:38 +02:00
|
|
|
use crate::{
|
2024-05-14 23:30:20 +02:00
|
|
|
services::{
|
|
|
|
get_available_roots::{self, GetAvailableRoots, GetAvailableRootsExt},
|
|
|
|
get_graph::{GetGraph, GetGraphExt},
|
|
|
|
},
|
2024-05-14 20:58:38 +02:00
|
|
|
state::SharedState,
|
|
|
|
};
|
2024-05-11 23:23:00 +02:00
|
|
|
|
2024-05-14 20:58:38 +02:00
|
|
|
pub struct Querier {
|
|
|
|
get_available_roots: GetAvailableRoots,
|
2024-05-14 23:30:20 +02:00
|
|
|
get_graph: GetGraph,
|
2024-05-14 20:58:38 +02:00
|
|
|
}
|
2024-05-11 23:23:00 +02:00
|
|
|
|
|
|
|
impl Querier {
|
2024-05-14 23:30:20 +02:00
|
|
|
pub fn new(get_available_roots: GetAvailableRoots, get_graph: GetGraph) -> Self {
|
2024-05-14 20:58:38 +02:00
|
|
|
Self {
|
|
|
|
get_available_roots,
|
2024-05-14 23:30:20 +02:00
|
|
|
get_graph,
|
2024-05-14 20:58:38 +02:00
|
|
|
}
|
2024-05-11 23:23:00 +02:00
|
|
|
}
|
|
|
|
|
2024-05-14 20:58:38 +02:00
|
|
|
pub async fn get_available_roots(&self) -> anyhow::Result<Option<Vec<String>>> {
|
|
|
|
let res = self
|
|
|
|
.get_available_roots
|
|
|
|
.execute(get_available_roots::Request {})
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
if res.roots.is_empty() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Some(res.roots))
|
2024-05-11 23:23:00 +02:00
|
|
|
}
|
|
|
|
|
2024-05-14 23:30:20 +02:00
|
|
|
pub async fn get(
|
2024-05-11 23:23:00 +02:00
|
|
|
&self,
|
|
|
|
root: &str,
|
|
|
|
path: impl IntoIterator<Item = impl Into<String>>,
|
2024-05-14 23:30:20 +02:00
|
|
|
) -> anyhow::Result<Option<GraphItem>> {
|
|
|
|
let graph = self
|
|
|
|
.get_graph
|
|
|
|
.execute(crate::services::get_graph::Request {
|
|
|
|
root: root.into(),
|
|
|
|
path: path.into_iter().map(|s| s.into()).collect(),
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(Some(graph.item))
|
2024-05-11 23:23:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait QuerierExt {
|
|
|
|
fn querier(&self) -> Querier;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl QuerierExt for SharedState {
|
|
|
|
fn querier(&self) -> Querier {
|
2024-05-14 23:30:20 +02:00
|
|
|
Querier::new(self.get_available_roots_service(), self.get_graph_service())
|
2024-05-11 23:23:00 +02:00
|
|
|
}
|
|
|
|
}
|