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;
#[derive(Default, Clone)]
pub struct TileSources(Arc<DashMap<String, BoxedSource>>);
impl TileSources {
#[must_use]
pub fn new(sources: Vec<Vec<BoxedSource>>) -> Self {
Self(Arc::new(
sources
.into_iter()
.flatten()
.map(|src| (src.get_id().to_string(), src))
.collect(),
))
}
#[must_use]
pub(crate) fn from_dashmap(map: Arc<DashMap<String, BoxedSource>>) -> Self {
Self(map)
}
#[must_use]
pub fn get_catalog(&self) -> TileCatalog {
self.0
.iter()
.map(|v| (v.key().clone(), v.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> {
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<(Vec<BoxedSource>, bool, TileInfo)> {
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 = 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);
}
}
Ok((
sources,
use_url_query,
info.expect("source_ids should be non-empty and contain at least one valid source"),
))
}
#[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.benefits_from_concurrent_scraping())
}
}