mlt-core 0.13.0

MapLibre Tile library code
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
use std::borrow::Cow;
use std::collections::HashSet;

use derive_debug::Dbg;

#[cfg(feature = "unstable-v2")]
use crate::decoder::RleLayout;
use crate::decoder::{DictionaryType, FastPForKind, GeometryValues, PhysicalEncoding, StreamType};
use crate::encoder::geometry::VertexBufferType;
use crate::encoder::{IntEncoder, StagedId, StagedProperty};
use crate::tile::Extent;
use crate::{MltError, MltResult};

/// Owned variant of `Unknown`.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct EncodedUnknown {
    pub(crate) tag: u8,
    pub(crate) value: Vec<u8>,
}

impl EncodedUnknown {
    pub fn new(tag: u8, value: Vec<u8>) -> MltResult<Self> {
        if tag == 1 {
            return Err(MltError::ParsingColumnType(tag));
        }
        Ok(Self { tag, value })
    }

    #[must_use]
    pub fn tag(&self) -> u32 {
        u32::from(self.tag)
    }

    #[must_use]
    pub fn data(&self) -> &[u8] {
        &self.value
    }
}

/// Parameters derived from the vertex set of a feature collection, used to
/// normalize coordinates before space-filling-curve key computation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CurveParams {
    pub shift: u32,
    pub bits: u32,
}

impl Default for CurveParams {
    fn default() -> Self {
        Self { shift: 0, bits: 1 }
    }
}

impl CurveParams {
    /// Compute params from a flat `[x0, y0, x1, y1, …]` vertex slice.
    #[must_use]
    pub fn from_vertices(vertices: &[i32]) -> Self {
        if vertices.is_empty() {
            return Self::default();
        }
        let (min, max) = vertices
            .iter()
            .fold((i32::MAX, i32::MIN), |(mn, mx), &v| (mn.min(v), mx.max(v)));
        crate::codecs::hilbert::hilbert_curve_params_from_bounds(min, max)
    }
}

/// Columnar layer data being prepared for encoding (stage 2 of the encoding pipeline).
///
/// Holds fully-owned columnar data. Constructed directly (synthetics, benches) or
/// converted from [`TileLayer`](crate::TileLayer).
/// Consumed by encoding via [`StagedLayer::encode_into`] or `StagedLayer::encode_explicit`
/// (with explicit encoding mode enabled).
#[derive(Debug, PartialEq, Clone)]
pub struct StagedLayer {
    pub(crate) name: String,
    pub(crate) extent: Extent,
    pub(crate) id: StagedId,
    pub(crate) geometry: GeometryValues,
    pub(crate) properties: Vec<StagedProperty>,
}

#[cfg_attr(not(feature = "__private"), allow(dead_code))]
impl StagedLayer {
    pub fn new(
        name: impl Into<String>,
        extent: u32,
        id: StagedId,
        geometry: GeometryValues,
        properties: Vec<StagedProperty>,
    ) -> MltResult<Self> {
        let name = name.into();
        if name.is_empty() {
            return Err(MltError::MissingLayerName);
        }
        let extent = Extent::new(extent)?;
        let feature_count = geometry.feature_count();
        if let Some(actual) = id.feature_count()
            && actual != feature_count
        {
            return Err(MltError::StagedFeatureCountMismatch {
                column: "id".into(),
                expected: feature_count,
                actual,
            });
        }
        // Column names must be unique within a layer. A shared dictionary's `name()` is
        // only its prefix (which may repeat); its real columns are `{prefix}{suffix}`.
        // Scoped so `seen` releases its borrow of `properties` before the move below.
        {
            let mut seen: HashSet<Cow<str>> = HashSet::new();
            for property in &properties {
                let actual = property.feature_count();
                if actual != feature_count {
                    return Err(MltError::StagedFeatureCountMismatch {
                        column: property.name().to_string(),
                        expected: feature_count,
                        actual,
                    });
                }
                if let StagedProperty::SharedDict(sd) = property {
                    for item in &sd.items {
                        if !seen.insert(Cow::Owned(format!("{}{}", sd.prefix, item.suffix))) {
                            return Err(MltError::DuplicatePropertyName(format!(
                                "{}{}",
                                sd.prefix, item.suffix
                            )));
                        }
                    }
                } else if !seen.insert(Cow::Borrowed(property.name())) {
                    return Err(MltError::DuplicatePropertyName(property.name().to_string()));
                }
            }
        }
        Ok(Self {
            name,
            extent,
            id,
            geometry,
            properties,
        })
    }

    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    #[must_use]
    pub fn extent(&self) -> Extent {
        self.extent
    }

    #[must_use]
    pub fn id(&self) -> &StagedId {
        &self.id
    }

    #[must_use]
    pub fn geometry(&self) -> &GeometryValues {
        &self.geometry
    }

    #[must_use]
    pub fn properties(&self) -> &[StagedProperty] {
        &self.properties
    }
}

/// Which wire format layers are encoded to.
#[cfg(feature = "unstable-v2")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum WireVersion {
    /// Tag `0x01` - the stable v1 format.
    #[default]
    V01,
    /// Tag `0x02` - the experimental v2 format (see `docs/migrating-to-v2.md`).
    ///
    /// Currently limited to ID, scalar, string, shared-dictionary and geometry columns.
    V02,
}

#[cfg(feature = "unstable-v2")]
impl WireVersion {
    /// The layer tag byte identifying this format on the wire.
    #[must_use]
    pub(crate) fn tag(self) -> u8 {
        match self {
            Self::V01 => 1,
            Self::V02 => 2,
        }
    }

    /// The `FastPFor` block size and word order used by this format.
    #[must_use]
    pub(crate) fn fastpfor_kind(self) -> FastPForKind {
        match self {
            Self::V01 => FastPForKind::Block256Be,
            Self::V02 => FastPForKind::Block128Le,
        }
    }

    /// The RLE stream data layout used by this format.
    #[must_use]
    pub(crate) fn rle_layout(self) -> RleLayout {
        match self {
            Self::V01 => RleLayout::Split,
            Self::V02 => RleLayout::Interleaved,
        }
    }
}

/// Global encoder settings controlling which optimization strategies are attempted.
#[derive(Debug, Clone, Copy, PartialEq, Hash)]
#[expect(
    clippy::struct_excessive_bools,
    reason = "enums would not model this better, not a state machine"
)]
pub struct EncoderConfig {
    /// The wire format to encode layers to.
    #[cfg(feature = "unstable-v2")]
    wire_version: WireVersion,
    /// Generate tessellation data for polygons and multi-polygons.
    tessellate: bool,
    /// Try sorting features by the Z-order (Morton) curve index of their first vertex.
    attempt_spatial_morton_sort: bool,
    /// Try sorting features by the Hilbert curve index of their first vertex.
    attempt_spatial_hilbert_sort: bool,
    /// Try sorting features by their feature ID in ascending order.
    attempt_id_sort: bool,
    /// Allow `FSST` string compression
    allow_fsst: bool,
    /// Allow `FastPFOR` integer compression
    allow_fastpfor: bool,
    /// Allow string grouping into shared dictionaries
    allow_shared_dict: bool,
    /// Allow the v2-only float dictionary encoding
    #[cfg(feature = "unstable-v2")]
    allow_float_dict: bool,
    /// Allow the v2-only ALP float encoding
    #[cfg(feature = "unstable-v2")]
    allow_float_alp: bool,
}
impl Default for EncoderConfig {
    fn default() -> Self {
        Self {
            #[cfg(feature = "unstable-v2")]
            wire_version: WireVersion::V01,
            tessellate: false,
            attempt_spatial_morton_sort: true,
            attempt_spatial_hilbert_sort: true,
            attempt_id_sort: true,
            allow_fsst: true,
            allow_fastpfor: true,
            allow_shared_dict: true,
            // Off by default while the encoding is still being measured.
            #[cfg(feature = "unstable-v2")]
            allow_float_dict: false,
            #[cfg(feature = "unstable-v2")]
            allow_float_alp: false,
        }
    }
}

impl EncoderConfig {
    #[cfg(feature = "unstable-v2")]
    #[must_use]
    pub fn wire_version(self) -> WireVersion {
        self.wire_version
    }

    #[must_use]
    pub fn tessellate(self) -> bool {
        self.tessellate
    }

    #[must_use]
    pub fn attempt_spatial_morton_sort(self) -> bool {
        self.attempt_spatial_morton_sort
    }

    #[must_use]
    pub fn attempt_spatial_hilbert_sort(self) -> bool {
        self.attempt_spatial_hilbert_sort
    }

    #[must_use]
    pub fn attempt_id_sort(self) -> bool {
        self.attempt_id_sort
    }

    #[must_use]
    pub fn allow_fsst(self) -> bool {
        self.allow_fsst
    }

    #[must_use]
    pub fn allow_fastpfor(self) -> bool {
        self.allow_fastpfor
    }

    /// The `FastPFor` encoding to race, or `None` when `FastPFor` is switched off.
    #[must_use]
    pub(crate) fn fastpfor(self) -> Option<PhysicalEncoding> {
        #[cfg(feature = "unstable-v2")]
        let kind = self.wire_version.fastpfor_kind();
        #[cfg(not(feature = "unstable-v2"))]
        let kind = FastPForKind::Block256Be;
        self.allow_fastpfor
            .then_some(PhysicalEncoding::FastPFor(kind))
    }

    #[must_use]
    pub fn allow_shared_dict(self) -> bool {
        self.allow_shared_dict
    }

    /// Whether float columns may use a dictionary, which only v2 can express.
    #[cfg(feature = "unstable-v2")]
    #[must_use]
    pub fn allow_float_dict(self) -> bool {
        self.allow_float_dict && self.wire_version != WireVersion::V01
    }

    /// Whether float columns may use ALP, which only v2 can express.
    #[cfg(feature = "unstable-v2")]
    #[must_use]
    pub fn allow_float_alp(self) -> bool {
        self.allow_float_alp && self.wire_version != WireVersion::V01
    }

    #[cfg(feature = "unstable-v2")]
    #[must_use]
    pub fn with_wire_version(mut self, version: WireVersion) -> Self {
        self.wire_version = version;
        self
    }

    #[must_use]
    pub fn with_tessellation(mut self, enabled: bool) -> Self {
        self.tessellate = enabled;
        self
    }

    #[must_use]
    pub fn with_spatial_morton_sort(mut self, enabled: bool) -> Self {
        self.attempt_spatial_morton_sort = enabled;
        self
    }

    #[must_use]
    pub fn with_spatial_hilbert_sort(mut self, enabled: bool) -> Self {
        self.attempt_spatial_hilbert_sort = enabled;
        self
    }

    #[must_use]
    pub fn with_id_sort(mut self, enabled: bool) -> Self {
        self.attempt_id_sort = enabled;
        self
    }

    #[must_use]
    pub fn with_fsst(mut self, enabled: bool) -> Self {
        self.allow_fsst = enabled;
        self
    }

    #[must_use]
    pub fn with_fastpfor(mut self, enabled: bool) -> Self {
        self.allow_fastpfor = enabled;
        self
    }

    #[must_use]
    pub fn with_shared_dict(mut self, enabled: bool) -> Self {
        self.allow_shared_dict = enabled;
        self
    }

    /// Allow float columns to store one code per value into a dictionary of the distinct ones.
    /// Off by default, and only v2 can express it.
    /// A column takes it only when it comes out strictly smaller in stored bytes.
    #[cfg(feature = "unstable-v2")]
    #[must_use]
    pub fn with_float_dict(mut self, enabled: bool) -> Self {
        self.allow_float_dict = enabled;
        self
    }

    /// Allow float columns to store each value as a decimal-scaled integer.
    #[cfg(feature = "unstable-v2")]
    #[must_use]
    pub fn with_float_alp(mut self, enabled: bool) -> Self {
        self.allow_float_alp = enabled;
        self
    }
}

/// How to encode a string column.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StrEncoding {
    Plain,
    Dict,
    Fsst,
    FsstDict,
    FrontDict,
    FsstFrontDict,
}

/// How to encode a float column, pinned rather than costed against the alternatives.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FloatEncoding {
    None,
    #[cfg(feature = "unstable-v2")]
    Dict,
    #[cfg(feature = "unstable-v2")]
    Alp,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ColumnKind {
    Id,
    Geometry,
    Property,
}

/// Context for per-stream encoding decisions in [`ExplicitEncoder`] callbacks.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct StreamCtx<'a> {
    pub kind: ColumnKind,
    pub stream_type: StreamType,
    pub name: &'a str,
    pub subname: &'a str,
}
impl<'a> StreamCtx<'a> {
    /// Stream with a logical sub-part (e.g. string column `"lengths"` / `"offsets"`, shared-dict child suffix).
    #[inline]
    #[must_use]
    pub const fn new(
        kind: ColumnKind,
        stream_type: StreamType,
        name: &'a str,
        subname: &'a str,
    ) -> Self {
        Self {
            kind,
            stream_type,
            name,
            subname,
        }
    }

    #[inline]
    #[must_use]
    pub const fn id(stream_type: StreamType) -> Self {
        Self::new(ColumnKind::Id, stream_type, "", "")
    }

    #[inline]
    #[must_use]
    pub const fn geom(stream_type: StreamType, name: &'a str) -> Self {
        Self::new(ColumnKind::Geometry, stream_type, name, "")
    }

    #[inline]
    #[must_use]
    pub const fn prop(stream_type: StreamType, name: &'a str) -> Self {
        Self::new(ColumnKind::Property, stream_type, name, "")
    }

    #[inline]
    #[must_use]
    pub const fn prop_data(name: &'a str) -> Self {
        let stream_type = StreamType::Data(DictionaryType::None);
        Self::new(ColumnKind::Property, stream_type, name, "")
    }

    #[inline]
    #[must_use]
    pub const fn prop2(stream_type: StreamType, prefix: &'a str, suffix: &'a str) -> Self {
        Self::new(ColumnKind::Property, stream_type, prefix, suffix)
    }
}

/// Explicit, deterministic encoding configuration for synthetics and tests.
///
/// All encoding choices are caller-specified via callbacks so one struct can cover any combination without per-stream boilerplate.
#[derive(Dbg)]
pub struct ExplicitEncoder {
    /// Vertex buffer layout for geometry streams.
    pub vertex_buffer_type: VertexBufferType,
    /// Per-stream override for the skip-empty-stream rule used by `write_geo_u32_stream`.
    #[dbg(skip)]
    pub force_stream: Box<dyn for<'a> Fn(&'a StreamCtx<'a>) -> bool>,
    /// Return the [`IntEncoder`] for a stream identified by [`StreamCtx`].
    #[dbg(skip)]
    pub get_int_encoder: Box<dyn for<'a> Fn(&'a StreamCtx<'a>) -> IntEncoder>,
    /// Return the string encoding strategy for a string property column.
    #[dbg(skip)]
    pub get_str_encoding: Box<dyn Fn(&str) -> StrEncoding>,
    /// Return the logical encoding for a float property column.
    #[dbg(skip)]
    pub get_float_encoding: Box<dyn Fn(&str) -> FloatEncoding>,
}