use std::sync::Arc;
use actix_web::error::ErrorNotFound;
use dashmap::DashMap;
use martin_core::tiles::catalog::TileCatalog;
use martin_core::tiles::{BoxedSource, Source};
use martin_tile_utils::TileInfo;
use tracing::debug;
use crate::config::file::ProcessConfig;
pub struct ResolvedSources {
pub sources: Vec<(BoxedSource, ProcessConfig)>,
pub use_url_query: bool,
pub info: TileInfo,
}
#[derive(Default, Clone)]
pub struct TileSources(Arc<DashMap<String, (BoxedSource, ProcessConfig)>>);
impl TileSources {
#[must_use]
pub fn new(sources: Vec<Vec<BoxedSource>>) -> Self {
Self::new_with_process(
sources
.into_iter()
.map(|group| {
group
.into_iter()
.map(|src| (src, ProcessConfig::default()))
.collect()
})
.collect(),
)
}
#[must_use]
pub fn new_with_process(sources: Vec<Vec<(BoxedSource, ProcessConfig)>>) -> Self {
Self(Arc::new(
sources
.into_iter()
.flatten()
.map(|(src, pc)| (src.get_id().to_string(), (src, pc)))
.collect(),
))
}
#[must_use]
pub(crate) fn from_dashmap(map: Arc<DashMap<String, (BoxedSource, ProcessConfig)>>) -> Self {
Self(map)
}
#[must_use]
pub fn get_catalog(&self) -> TileCatalog {
self.0
.iter()
.map(|v| {
let (src, _pc) = v.value();
(v.key().clone(), src.get_catalog_entry())
})
.collect()
}
#[must_use]
pub fn source_names(&self) -> Vec<String> {
self.0.iter().map(|v| v.key().clone()).collect()
}
pub fn get_source(&self, id: &str) -> actix_web::Result<(BoxedSource, ProcessConfig)> {
Ok(self
.0
.get(id)
.ok_or_else(|| ErrorNotFound(format!("Source {id} does not exist")))?
.value()
.clone())
}
#[hotpath::measure]
pub fn get_sources(
&self,
source_ids: &str,
zoom: Option<u8>,
) -> actix_web::Result<ResolvedSources> {
let mut sources = Vec::new();
let mut info: Option<TileInfo> = None;
let mut use_url_query = false;
for id in source_ids.split(',') {
let (src, pc) = self.get_source(id)?;
let src_inf = src.get_tile_info();
use_url_query |= src.support_url_query();
match info {
Some(inf) if inf == src_inf => {}
Some(inf) => Err(ErrorNotFound(format!(
"Cannot merge sources with {inf} with {src_inf}"
)))?,
None => info = Some(src_inf),
}
if match zoom {
Some(zoom) if Self::check_zoom(&*src, id, zoom) => true,
None => true,
_ => false,
} {
sources.push((src, pc));
}
}
Ok(ResolvedSources {
sources,
use_url_query,
info: info.expect("at least one source must be present"),
})
}
#[must_use]
pub fn check_zoom(src: &dyn Source, id: &str, zoom: u8) -> bool {
let is_valid = src.is_valid_zoom(zoom);
if !is_valid {
debug!("Zoom {zoom} is not valid for source {id}");
}
is_valid
}
#[must_use]
pub fn benefits_from_concurrent_scraping(&self) -> bool {
self.0
.iter()
.any(|s| s.value().0.benefits_from_concurrent_scraping())
}
}