martin 1.16.0

Blazing fast and lightweight tile server with PostGIS, MBTiles, and PMTiles support
Documentation
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std::num::NonZeroUsize;

use serde::{Deserialize, Serialize};

use crate::config::args::BoundsCalcType;
use crate::config::file::tiles::duckdb::sources::{
    DuckDbDatabaseEntry, DuckDbSourceDefaults, GeoParquetEntry,
};
use crate::config::file::{
    CachePolicy, CollectUnrecognizedKeys, ConfigFileResult, ConfigurationLivecycleHooks,
    UnrecognizedValues,
};

const DEFAULT_POOL_SIZE: usize = 4;

fn default_pool_size() -> NonZeroUsize {
    NonZeroUsize::new(DEFAULT_POOL_SIZE).expect("default pool size must be non-zero")
}

#[expect(
    clippy::trivially_copy_pass_by_ref,
    reason = "serde skip_serializing_if requires `&T`"
)]
fn is_default_pool_size(v: &NonZeroUsize) -> bool {
    v.get() == DEFAULT_POOL_SIZE
}

#[expect(
    clippy::trivially_copy_pass_by_ref,
    reason = "serde skip_serializing_if requires `&T`"
)]
fn is_default_auto_bounds(v: &BoundsCalcType) -> bool {
    *v == BoundsCalcType::default()
}

#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, CollectUnrecognizedKeys)]
#[cfg_attr(feature = "unstable-schemas", derive(schemars::JsonSchema))]
pub struct DuckDbConfig {
    /// Connection pool size used by `DuckDB` sources unless overridden per-source.
    #[serde(
        default = "default_pool_size",
        skip_serializing_if = "is_default_pool_size"
    )]
    pub pool_size: NonZeroUsize,
    /// Optional `DuckDB` execution thread count for each connection.
    pub threads: Option<NonZeroUsize>,
    /// Optional `DuckDB` memory limit in megabytes for each connection.
    pub memory_limit_mb: Option<NonZeroUsize>,
    /// Bounds behavior for auto-generated `TileJSON` bounds.
    #[serde(default, skip_serializing_if = "is_default_auto_bounds")]
    pub auto_bounds: BoundsCalcType,
    /// Ordered source definitions.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub sources: Vec<DuckDbSourceEntry>,
    /// Zoom-level bounds for caching the tiles of every `DuckDB` source without its own `cache`.
    /// Overrides the top-level `cache` bounds.
    #[serde(default, skip_serializing_if = "CachePolicy::is_empty")]
    #[cfg_attr(
        feature = "unstable-schemas",
        schemars(with = "crate::config::file::CachePolicyShape")
    )]
    pub cache: CachePolicy,
    #[serde(flatten, skip_serializing)]
    #[cfg_attr(feature = "unstable-schemas", schemars(skip))]
    pub unrecognized: UnrecognizedValues,
}

impl Default for DuckDbConfig {
    fn default() -> Self {
        Self {
            pool_size: default_pool_size(),
            threads: None,
            memory_limit_mb: None,
            auto_bounds: BoundsCalcType::default(),
            sources: Vec::new(),
            cache: CachePolicy::default(),
            unrecognized: UnrecognizedValues::default(),
        }
    }
}

impl DuckDbConfig {
    /// Returns `true` when no sources are configured.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.sources.is_empty()
    }
}

impl ConfigurationLivecycleHooks for DuckDbConfig {
    async fn finalize(&mut self) -> ConfigFileResult<()> {
        let defaults = DuckDbSourceDefaults {
            pool_size: self.pool_size,
            threads: self.threads,
            memory_limit_mb: self.memory_limit_mb,
            auto_bounds: self.auto_bounds,
        };

        for source in &mut self.sources {
            source.finalize()?;
            source.apply_defaults(defaults);
        }

        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, CollectUnrecognizedKeys)]
#[serde(untagged)]
pub enum DuckDbSourceEntry {
    Database(DuckDbDatabaseEntry),
    GeoParquet(GeoParquetEntry),
}

impl DuckDbSourceEntry {
    pub(crate) fn finalize(&mut self) -> ConfigFileResult<()> {
        match self {
            Self::Database(v) => {
                v.finalize();
                Ok(())
            }
            Self::GeoParquet(v) => v.finalize(),
        }
    }

    pub(crate) fn apply_defaults(&mut self, defaults: DuckDbSourceDefaults) {
        match self {
            Self::Database(v) => v.settings.apply_defaults(defaults),
            Self::GeoParquet(v) => v.settings.apply_defaults(defaults),
        }
    }
}

#[cfg(feature = "unstable-schemas")]
impl schemars::JsonSchema for DuckDbSourceEntry {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "DuckDbSourceEntry".into()
    }

    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
        let database = generator.subschema_for::<DuckDbDatabaseEntry>();
        let geoparquet = generator.subschema_for::<GeoParquetEntry>();
        schemars::json_schema!({
            "description": "DuckDB source entry: exactly one of `database` or `geoparquet` must be present.",
            "oneOf": [
                database,
                geoparquet,
            ]
        })
    }
}

#[cfg(test)]
mod tests {
    use std::assert_matches;

    use super::*;
    use crate::config::file::tiles::duckdb::GeoParquetLocation;

    const GEOPARQUET_FIXTURE: &str = "../tests/fixtures/duckdb/geoparquet_polygons.parquet";

    #[test]
    fn source_list_may_mix_database_and_geoparquet() {
        let yaml = r"
pool_size: 4
auto_bounds: quick
sources:
  - database: /data/tiles.duckdb
    auto_publish:
      tables:
        from_schemas: autodetect
  - geoparquet: /data/buildings.parquet
    layer_id: buildings
    geometry_column: geom
    srid: 4326
    minzoom: 0
    maxzoom: 14
    extent: 4096
    buffer: 64
";
        let cfg: DuckDbConfig = serde_saphyr::from_str(yaml).expect("duckdb config");

        insta::assert_debug_snapshot!(cfg, @r#"
        DuckDbConfig {
            pool_size: 4,
            threads: None,
            memory_limit_mb: None,
            auto_bounds: Quick,
            sources: [
                Database(
                    DuckDbDatabaseEntry {
                        database: "/data/tiles.duckdb",
                        settings: DuckDbSourceSettings {
                            pool_size: None,
                            threads: None,
                            memory_limit_mb: None,
                            auto_bounds: None,
                        },
                        auto_publish: Some(
                            Object {
                                "tables": Object {
                                    "from_schemas": String("autodetect"),
                                },
                            },
                        ),
                        tables: None,
                        macros: None,
                        unrecognized: UnrecognizedValues(
                            {},
                        ),
                    },
                ),
                GeoParquet(
                    GeoParquetEntry {
                        geoparquet: "/data/buildings.parquet",
                        location: None,
                        layer_id: Some(
                            "buildings",
                        ),
                        id_column: None,
                        geometry_column: Some(
                            "geom",
                        ),
                        srid: Some(
                            4326,
                        ),
                        minzoom: Some(
                            0,
                        ),
                        maxzoom: Some(
                            14,
                        ),
                        extent: Some(
                            4096,
                        ),
                        buffer: Some(
                            64,
                        ),
                        clip_geom: None,
                        settings: DuckDbSourceSettings {
                            pool_size: None,
                            threads: None,
                            memory_limit_mb: None,
                            auto_bounds: None,
                        },
                        unrecognized: UnrecognizedValues(
                            {},
                        ),
                    },
                ),
            ],
            cache: CachePolicy {
                zoom: CacheZoomRange {
                    minzoom: None,
                    maxzoom: None,
                },
            },
            unrecognized: UnrecognizedValues(
                {},
            ),
        }
        "#);
    }

    #[tokio::test]
    async fn source_overrides_from_yaml_take_precedence_over_top_level() {
        let yaml = indoc::formatdoc! {"
            pool_size: 8
            threads: 2
            memory_limit_mb: 1024
            auto_bounds: quick
            sources:
              - geoparquet: {GEOPARQUET_FIXTURE}
                pool_size: 3
                memory_limit_mb: 256
                auto_bounds: skip
        "};
        let mut cfg: DuckDbConfig = serde_saphyr::from_str(&yaml).expect("duckdb config");
        cfg.finalize().await.expect("finalize duckdb config");

        assert_eq!(cfg.pool_size.get(), 8);
        assert_eq!(cfg.threads.map(NonZeroUsize::get), Some(2));
        assert_eq!(cfg.memory_limit_mb.map(NonZeroUsize::get), Some(1024));

        let DuckDbSourceEntry::GeoParquet(entry) = &cfg.sources[0] else {
            panic!("expected geoparquet entry");
        };
        assert_eq!(entry.geoparquet, GEOPARQUET_FIXTURE);
        assert_matches!(entry.location, Some(GeoParquetLocation::Local(_)));
        insta::assert_debug_snapshot!(entry.settings, @"
        DuckDbSourceSettings {
            pool_size: Some(
                3,
            ),
            threads: Some(
                2,
            ),
            memory_limit_mb: Some(
                256,
            ),
            auto_bounds: Some(
                Skip,
            ),
        }
        ");
    }

    #[test]
    fn source_entry_with_both_keys_deserializes_as_database() {
        let yaml = r"
sources:
  - database: /data/tiles.duckdb
    geoparquet: /data/buildings.parquet
";
        let cfg: DuckDbConfig = serde_saphyr::from_str(yaml).expect("duckdb config");

        insta::assert_debug_snapshot!(cfg, @r#"
        DuckDbConfig {
            pool_size: 4,
            threads: None,
            memory_limit_mb: None,
            auto_bounds: Quick,
            sources: [
                Database(
                    DuckDbDatabaseEntry {
                        database: "/data/tiles.duckdb",
                        settings: DuckDbSourceSettings {
                            pool_size: None,
                            threads: None,
                            memory_limit_mb: None,
                            auto_bounds: None,
                        },
                        auto_publish: None,
                        tables: None,
                        macros: None,
                        unrecognized: UnrecognizedValues(
                            {
                                "geoparquet": String("/data/buildings.parquet"),
                            },
                        ),
                    },
                ),
            ],
            cache: CachePolicy {
                zoom: CacheZoomRange {
                    minzoom: None,
                    maxzoom: None,
                },
            },
            unrecognized: UnrecognizedValues(
                {},
            ),
        }
        "#);
    }

    #[test]
    fn source_entry_rejects_missing_database_and_geoparquet() {
        let yaml = r"
sources:
  - layer_id: buildings
    srid: 4326
";
        let err = serde_saphyr::from_str::<DuckDbConfig>(yaml).expect_err("missing entry keys");
        assert!(
            err.to_string()
                .contains("data did not match any variant of untagged enum DuckDbSourceEntry")
        );
    }

    #[tokio::test]
    async fn top_level_config_finalizes_defaults_and_serializes() {
        use std::collections::HashMap;
        use std::path::Path;

        use crate::config::file::{Config, parse_config};

        let yaml = indoc::formatdoc! {"
            duckdb:
              pool_size: 8
              threads: 2
              memory_limit_mb: 1024
              sources:
                - geoparquet: {GEOPARQUET_FIXTURE}
                  layer_id: buildings
                - geoparquet: {GEOPARQUET_FIXTURE}
                  pool_size: 3
                  memory_limit_mb: 256
                  auto_bounds: skip
        "};
        let mut config: Config =
            parse_config(&yaml, &HashMap::new(), Path::new("<test>")).expect("parse config");
        config.finalize().await.expect("finalize");

        insta::assert_snapshot!(
            serde_saphyr::to_string(&config).expect("serialize config"),
            @"
        duckdb:
          pool_size: 8
          threads: 2
          memory_limit_mb: 1024
          sources:
          - geoparquet: ../tests/fixtures/duckdb/geoparquet_polygons.parquet
            layer_id: buildings
            pool_size: 8
            threads: 2
            memory_limit_mb: 1024
            auto_bounds: quick
          - geoparquet: ../tests/fixtures/duckdb/geoparquet_polygons.parquet
            pool_size: 3
            threads: 2
            memory_limit_mb: 256
            auto_bounds: skip
        "
        );
    }
}