Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / scripts / maps / tile_build / src / manifest.rs

manifest.rs165 lines · 6.0 KiBRaw
1//! `manifest` — the `planet4.manifest.json` schema for incremental updates.
2//!
3//! The manifest is the only versioned thing the client fetches on a check
4//! (bytes, not gigabytes): it pins the base archive, names the overlay
5//! segments and sidecar sections, and carries the hashes that make each
6//! download verifiable and resumable. See the plan file
7//! (`maps_incremental_updates.plan.md`, Phase 1) for the rollout order.
8//!
9//! Wire form is plain JSON with fixed field names (documented on each
10//! struct); the publisher (`upload_planet.py --manifest`) writes it, the
11//! client parses it. No serde dependency: the host crate stays cargo-offline
12//! buildable, and the schema is small enough to emit by hand.
13
14/// Manifest format version. Bump when a client must understand new fields to
15/// apply the overlay safely.
16pub const MANIFEST_VERSION: u32 = 1;
17/// Oldest manifest version this publisher writes. Clients below
18/// `MIN_CLIENT_COMPAT` must full-download instead of applying overlays.
19pub const MIN_CLIENT_COMPAT: u32 = 1;
20/// Largest overlay segment the publisher emits. 512 MiB keeps peak transient
21/// phone disk to one segment (the no-2x guarantee) while keeping the segment
22/// count for a monthly overlay in the single digits.
23pub const SEGMENT_CAP_BYTES: u64 = 512 * 1024 * 1024;
24/// Chunk size for large sidecar sections (graph nodes/edges/intermediate,
25/// transit). 32 MiB granularity lets the client skip unchanged chunks.
26pub const SECTION_CHUNK_BYTES: u64 = 32 * 1024 * 1024;
27
28/// The base archive this overlay applies to.
29pub struct BaseRef {
30 /// Download URL of the base `.mamaps` (today `planet4.mamaps`).
31 pub url: String,
32 /// `build_id` from the base header: the overlay refuses a mismatched base.
33 pub build_id: u64,
34 /// Base length in bytes (truncation check).
35 pub len: u64,
36}
37
38/// One overlay segment file: a small self-contained `.mamaps` holding only
39/// changed/added tile bodies plus tombstones, with overlay-relative offsets.
40pub struct SegmentRef {
41 /// Download URL of the segment file.
42 pub url: String,
43 /// Segment length in bytes.
44 pub len: u64,
45 /// Lowercase hex SHA-256 of the segment file.
46 pub sha256: String,
47 /// Sequence index (apply order; segments are independent but ordered).
48 pub seq: u32,
49}
50
51/// One sidecar section published as an independently chunked file (large
52/// kinds: graph nodes/edges/intermediate, transit) or ridden inside overlay
53/// segments (small kinds — absent here).
54pub struct SectionRef {
55 /// Archive kind id (`ARCHIVE_KIND_*`).
56 pub kind: u8,
57 /// Download URL of the section file.
58 pub url: String,
59 /// Section length in bytes.
60 pub len: u64,
61 /// Lowercase hex SHA-256 of the whole section file.
62 pub sha256: String,
63 /// Chunk size in bytes (always [`SECTION_CHUNK_BYTES`]).
64 pub chunk_size: u64,
65 /// Lowercase hex SHA-256 per chunk, in order.
66 pub chunk_hashes: Vec<String>,
67}
68
69/// The manifest: base pin + overlay sequence + sections + compat floor.
70pub struct Manifest {
71 /// [`MANIFEST_VERSION`].
72 pub version: u32,
73 /// Base archive reference.
74 pub base: BaseRef,
75 /// Overlay sequence number (monotonic; client applies when ahead).
76 pub overlay_seq: u64,
77 /// `overlay_build_id` (derived from base id + new OSM id + section hashes).
78 pub overlay_build_id: u64,
79 /// Overlay segments, ascending `seq`.
80 pub segments: Vec<SegmentRef>,
81 /// Independently chunked sidecar sections, ascending `kind`.
82 pub sections: Vec<SectionRef>,
83 /// Deleted tile ids (answered absent even though the base holds them).
84 pub tombstones: Vec<u64>,
85 /// [`MIN_CLIENT_COMPAT`].
86 pub min_client: u32,
87}
88
89/// Render a [`Manifest`] as canonical JSON (fixed field order, no whitespace
90/// beyond separators) so publishers and tests hash the same bytes.
91pub fn render(manifest: &Manifest) -> String {
92 let mut out = String::from("{");
93 out.push_str(&format!("\"version\":{},", manifest.version));
94 out.push_str("\"base\":{");
95 out.push_str(&format!(
96 "\"url\":{},\"build_id\":\"{:#018x}\",\"len\":{}",
97 json_string(&manifest.base.url),
98 manifest.base.build_id,
99 manifest.base.len,
100 ));
101 out.push_str("},");
102 out.push_str(&format!(
103 "\"overlay_seq\":{},\"overlay_build_id\":\"{:#018x}\",",
104 manifest.overlay_seq, manifest.overlay_build_id,
105 ));
106 out.push_str("\"segments\":[");
107 for (n, s) in manifest.segments.iter().enumerate() {
108 if n > 0 {
109 out.push(',');
110 }
111 out.push_str(&format!(
112 "{{\"url\":{},\"len\":{},\"sha256\":{},\"seq\":{}}}",
113 json_string(&s.url),
114 s.len,
115 json_string(&s.sha256),
116 s.seq,
117 ));
118 }
119 out.push_str("],\"sections\":[");
120 for (n, s) in manifest.sections.iter().enumerate() {
121 if n > 0 {
122 out.push(',');
123 }
124 out.push_str(&format!(
125 "{{\"kind\":{},\"url\":{},\"len\":{},\"sha256\":{},\"chunk_size\":{},\"chunk_hashes\":[",
126 s.kind,
127 json_string(&s.url),
128 s.len,
129 json_string(&s.sha256),
130 s.chunk_size,
131 ));
132 for (m, h) in s.chunk_hashes.iter().enumerate() {
133 if m > 0 {
134 out.push(',');
135 }
136 out.push_str(&json_string(h));
137 }
138 out.push_str("]}");
139 }
140 out.push_str("],\"tombstones\":[");
141 for (n, id) in manifest.tombstones.iter().enumerate() {
142 if n > 0 {
143 out.push(',');
144 }
145 out.push_str(&id.to_string());
146 }
147 out.push_str(&format!("],\"min_client\":{}}}", manifest.min_client));
148 out
149}
150
151/// A JSON string literal with the escapes this schema needs (URLs and hex
152/// digests carry no control characters, but backslash/quote must survive).
153fn json_string(s: &str) -> String {
154 let mut out = String::with_capacity(s.len() + 2);
155 out.push('"');
156 for c in s.chars() {
157 match c {
158 '"' => out.push_str("\\\""),
159 '\\' => out.push_str("\\\\"),
160 _ => out.push(c),
161 }
162 }
163 out.push('"');
164 out
165}