1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use std::collections::HashMap;
use std::fmt::Debug;
use async_trait::async_trait;
use martin_tile_utils::{TileCoord, TileData, TileInfo};
use tilejson::TileJSON;
use crate::CacheZoomRange;
use crate::tiles::catalog::CatalogSourceEntry;
#[cfg(feature = "postgres")]
use crate::tiles::postgres::ActiveQueryRegistry;
use crate::tiles::{MartinCoreResult, Tile};
/// URL query parameters for dynamic tile generation.
pub type UrlQuery = HashMap<String, String>;
/// Core trait for tile sources providing data to Martin
///
/// Implementors can serve tiles from databases, files, or other backends.
#[async_trait]
pub trait Source: Send + Sync + Debug {
/// Unique source identifier used in URLs.
fn get_id(&self) -> &str;
/// `TileJSON` specification served to clients.
fn get_tilejson(&self) -> &TileJSON;
/// Technical tile information (format, encoding, etc.).
fn get_tile_info(&self) -> TileInfo;
/// Creates a boxed clone for trait object storage.
fn clone_source(&self) -> BoxedSource;
/// A version string for this source, if available. Default: None.
/// If available, this string is appended to tile URLs as a query parameter,
/// invalidating caches.
fn get_version(&self) -> Option<String> {
None
}
/// Whether this source accepts URL query parameters. Default: false.
fn support_url_query(&self) -> bool {
false
}
/// Whether martin-cp should use concurrent scraping. Default: false.
fn benefits_from_concurrent_scraping(&self) -> bool {
false
}
/// Returnes the cancellation registry for queries
///
/// Only works for postgresql sources for now
#[cfg(feature = "postgres")]
fn cancel_registry(&self) -> Option<ActiveQueryRegistry> {
None
}
/// Zoom-level bounds for tile caching.
fn cache_zoom(&self) -> CacheZoomRange;
/// Retrieves tile data for the given coordinates.
///
/// # Arguments
/// * `xyz` - Tile coordinates (x, y, zoom)
/// * `url_query` - Optional query parameters for dynamic tiles
async fn get_tile(
&self,
xyz: TileCoord,
url_query: Option<&UrlQuery>,
) -> MartinCoreResult<TileData>;
/// Retrieves tile with etag for the given coordinates.
///
/// Default implementation calls [`get_tile()`](Self::get_tile) and computes etag using `xxh3_128`.
/// Sources can override this for more performance.
///
/// # Arguments
/// * `xyz` - Tile coordinates (x, y, zoom)
/// * `url_query` - Optional query parameters for dynamic tiles
async fn get_tile_with_etag(
&self,
xyz: TileCoord,
url_query: Option<&UrlQuery>,
) -> MartinCoreResult<Tile> {
let data = self.get_tile(xyz, url_query).await?;
Ok(Tile::new_hash_etag(data, self.get_tile_info()))
}
/// Validates zoom level against `TileJSON` min/max zoom constraints.
fn is_valid_zoom(&self, zoom: u8) -> bool {
let tj = self.get_tilejson();
tj.minzoom.is_none_or(|minzoom| zoom >= minzoom)
&& tj.maxzoom.is_none_or(|maxzoom| zoom <= maxzoom)
}
/// Attempts to create a fresh instance of this source.
///
/// Sources that return a `MartinCoreError::SourceNeedReload` from `get_tile()` must also
/// implement this method.
///
/// The default implementation asserts.
async fn try_reload(&self) -> MartinCoreResult<BoxedSource> {
unreachable!()
}
/// Generates catalog entry for this source.
fn get_catalog_entry(&self) -> CatalogSourceEntry {
let id = self.get_id();
let tilejson = self.get_tilejson();
let info = self.get_tile_info();
CatalogSourceEntry {
content_type: info.format.content_type().to_owned(),
content_encoding: info.encoding.compression().map(str::to_owned),
name: tilejson.name.as_ref().filter(|v| *v != id).cloned(),
description: tilejson.description.clone(),
attribution: tilejson.attribution.clone(),
// FIXME: surface `tilejson.vector_layers.len()` once we always have it.
layer_count: None,
// FIXME: surface the source's mtime (mbtiles/pmtiles modtime, etc.).
last_modified_at: None,
}
}
}
/// Boxed tile source trait object for storage in collections.
pub type BoxedSource = Box<dyn Source>;
impl Clone for BoxedSource {
fn clone(&self) -> Self {
self.clone_source()
}
}