MBTiles archive operations
MBTiles archive operations move a tile set through build, check, copy, update, cache, and release workflows. The mbtiles tool keeps tile content, metadata, schema choice, and aggregate checks aligned so an archive can be transferred or changed with a clear record of its expected state. For the storage model behind these operations, see MBTiles storage.
Archive operation model
An archive can be built from a tile directory, inspected, copied into another layout, changed by a patch, or maintained as a cache. Each path ends with checks that help confirm that the resulting tile set is the intended one.
flowchart LR
A[Tile directory] -->|pack| B[MBTiles archive]
B --> C[Inspect and validate]
B -->|copy or transcode| D[New layout or encoding]
B -->|diff| E[Patch archive]
E -->|apply| F[Updated archive]
B -->|purge| G[Managed tile cache]
D --> C
F --> C
References: crates/…/mbtiles.rs• L55-173 crates/…/mbtiles.rs• L322-420 crates/…/copier.rs• L140-232 crates/…/patcher.rs• L43-169
Archive inspection and validation
Validation
Validation checks the SQLite file and the tile set that it represents. It checks database integrity, tile coordinates and data types, schema links or stored tile hashes when available, and the file-wide agg_tiles_hash value when requested.
The aggregate hash is an ordered record of the tile set. A mismatch means that tile content or its metadata is stale. Use an update only after you know that the tile content is correct. A missing hash algorithm means MD5 for compatibility. An explicit unknown algorithm stops comparison instead of silently using the wrong one.
| Check | What it protects |
|---|---|
| SQLite check | Database structure and page integrity |
| Tile value check | Valid zoom, column, row, and tile data values |
| Per-tile check | Stored tile hashes or normalized-table references |
| Aggregate hash check | The complete tile set and its coordinate order |
Flat archives do not store per-tile hashes. Cache archives also skip that check. Both can still use the aggregate hash. A writable operation also needs a unique tile-coordinate constraint so duplicate handling cannot create ambiguous rows.
References: crates/…/validation.rs• L23-60 crates/…/validation.rs• L303-342 crates/…/validation.rs• L565-605 crates/…/validation.rs• L712-834 crates/…/validation.rs• L904-1006
Summaries and metadata maintenance
Summary output gives a compact operational view of an archive. It reports the file type and size together with tile counts, size ranges, zoom coverage, and geographic bounds.
Metadata maintenance keeps values derived from tile content aligned with the archive. A metadata update can recalculate zoom limits and detected tile encoding, then apply explicit set or delete actions as one transaction. A rejected edit leaves the file unchanged.
References: crates/…/mbtiles.rs• L351-364 crates/…/mbtiles.rs• L386-395 crates/…/summary.rs• L112-200 crates/…/update.rs• L135-161
Archive movement and conversion
Packing and unpacking
Packing turns a numeric {z}/{x}/{y}.{ext} tile directory into an MBTiles archive. It accepts XYZ or TMS coordinates, detects the tile format, rejects mixed formats, and records useful format, compression, zoom, and bounds metadata.
Unpacking turns an archive back into a directory tree. It can write either coordinate scheme. The result represents the same tile content even when the archive used an internal compression form.
References: crates/…/mbtiles.rs• L138-173 crates/…/mbtiles.rs• L396-416 crates/…/pack.rs• L33-60 crates/…/pack.rs• L119-276
Copying and filtering
Copying is the main archive movement operation. It can copy tiles, metadata, or both; select a zoom range or geographic area; convert among supported layouts; and merge several sources into one destination.
When sources overlap, the duplicate policy controls the result:
overridekeeps the later tile.ignorekeeps the existing tile.abortstops on a conflicting tile.
New destinations use the requested hash algorithm or the source algorithm. Existing destinations keep their own algorithm, and a conflicting request is rejected. Tile changes normally refresh the aggregate hash unless the operation explicitly skips it.
References: crates/…/mbtiles.rs• L176-260 crates/…/mbtiles.rs• L468-496 crates/…/copier.rs• L154-232 crates/…/copier.rs• L680-777
Bulk updates
Bulk updates apply many tile or metadata changes as one archive operation. Copy can merge sources and filter the selected tile set. Metadata updates group computed values and explicit edits in one transaction.
This supports a safe refresh pattern:
- Build or copy the desired state into a destination.
- Choose the duplicate rule when the destination already has tiles.
- Recompute or verify the aggregate hash.
- Validate the resulting archive before release.
When a normal archive is copied into the cache layout, the copied cache entry has no fetch, expiry, or entity-tag facts because the source does not provide them. Cache-to-cache copying preserves those facts.
References: crates/…/mbtiles.rs• L351-364 crates/…/copier.rs• L483-570 crates/…/copier.rs• L649-677 mbtiles/…/copy.rs• L470-551
Transcoding
Transcoding creates a new archive while applying a tile-data transformation. It supports changes to tile encoding or payload content without changing the source archive.
The workflow can reuse work for identical tile payloads. It does not write directly to the cache layout. A normalized source cannot be sent directly to the deduplication-ID layout during transcoding; copy it through a suitable flat layout first.
References: crates/…/errors.rs• L234-239 crates/…/transcoder.rs• L57-94 crates/…/transcoder.rs• L106-238
Cache lifecycle
Cache management
The cache layout stores tile data with fetch time, expiry time, and entity-tag information. A standard tiles view keeps the content readable as a normal tileset, while cache metadata is preserved only in cache-to-cache copies.
cache-purge first removes expired entries. With a size limit, it then removes entries in soonest-expiring order until the live cache fits the limit. It refuses a non-cache archive so a normal tileset is not changed by mistake.
References: crates/…/mbtiles.rs• L152-161 crates/…/mbtiles.rs• L432-447 crates/…/copier.rs• L649-671 crates/…/validation.rs• L633-640
Differential updates
Diff and patch workflows
A diff compares a source archive with a target archive and stores changed tile coordinates plus changed metadata. A target tile that is absent becomes a deletion marker. Applying the patch can therefore add, replace, and delete tiles without sending the full target archive.
The patch records the expected aggregate hash before and after application. The normal apply path checks the base hash before it writes, applies changes in one transaction, checks the result hash, and rolls back on a failed result check. Use in-place apply-patch when the base file may be changed. Use copy --apply-patch when the original must remain untouched.
sequenceDiagram
participant A as Source archive
participant B as Target archive
participant P as Patch archive
participant C as Recipient archive
A->>P: Compare with B
B-->>P: Changed tiles and metadata
P->>C: Check before-apply hash
P->>C: Apply additions, edits, and deletions
P-->>C: Check after-apply hash
C->>C: Store the resulting aggregate hash
Diff generation can compute missing source and target hashes without modifying the input archives. A patch file without its own aggregate hash or before/after hash is rejected unless force mode is used.
References: crates/…/mbtiles.rs• L88-107 crates/…/mbtiles.rs• L334-350 crates/…/copier.rs• L235-336 crates/…/copier.rs• L339-448 crates/…/patcher.rs• L43-169 mbtiles/…/diff_validation.rs• L49-76 mbtiles/…/diff_validation.rs• L99-184
Binary-diff workflows
Binary diffs reduce patch size when a changed tile shares much of its content with the old tile. The patch mode selects the tile byte form used for comparison and storage.
| Patch mode | Input tiles | Main result |
|---|---|---|
whole | Any supported encoding | Stores the full changed tile |
bin-diff-raw | Uncompressed | Stores a compressed binary difference |
bin-diff-gz | Gzip-compressed | Diffs decompressed tile content |
Binary patches are applied through copy --apply-patch, which creates a new destination and preserves the base archive. The direct apply-patch command accepts whole-tile patches only. Binary diff needs a flat or flat-with-hash layout. Gzip recompression can change final bytes even when the tile content is correct, so the gzip path skips the final aggregate-byte comparison and verifies each reconstructed tile instead.
References: crates/…/mbtiles.rs• L186-211 crates/…/copier.rs• L267-276 crates/…/copier.rs• L359-367 crates/…/copier.rs• L405-445 crates/…/patcher.rs• L48-53 crates/…/errors.rs• L193-211 mbtiles/…/copy.rs• L815-870
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to schema support must stay aligned across copy, validation, diff, patch, summary, and cache rules.
- Changes to hash metadata must stay aligned across source checks, destination writes, and patch before/after checks.
- Changes to tile coordinates or compression must be checked against pack/unpack conversion and binary patch behavior.
- Changes to cache columns or deletion behavior must preserve the fact that cache views cannot represent patch deletion markers.
- Changes to patch transaction checks must preserve rollback on a failed base or result hash check.
- Changes to destination hash handling must preserve rehashing when source and destination algorithms differ.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Tile coordinates or data types are invalid | Validation rejects the archive | Run validate before release or patch creation |
| Aggregate hash is missing or stale | Validation or safe patch use stops | Recompute it only after checking tile content |
| A patch is applied to the wrong base | The update is rejected before writing | Keep the before-apply hash check enabled |
| A patch result hash does not match | In-place application rolls back | Review the patch or use force only with explicit approval |
| Binary gzip output is compared as exact bytes | Valid reconstructed content can appear different | Use per-tile checks; use raw binary diff for byte-level output checks |
| A diff or patch targets a cache destination | The operation is rejected because deletion markers or cache facts do not fit | Copy through a standard layout first |
| A writable archive lacks a unique tile index | Duplicate handling is unsafe or patching cannot proceed | Add the required uniqueness constraint before writing |
| Pack input mixes tile formats | Packing stops before making an inconsistent archive | Use one tile format per input tree |
Key files
| File | Why to read it |
|---|---|
| crates/…/mbtiles.rs | Defines the command surface for archive inspection, movement, patching, and cache work |
| crates/…/validation.rs | Defines archive checks, hash meaning, schema detection, and patch metadata checks |
| crates/…/copier.rs | Connects copy, filtering, schema conversion, diff creation, and patch-on-copy workflows |
| crates/…/patcher.rs | Defines in-place patch checks, transaction behavior, metadata changes, and rollback |
| crates/…/pack.rs | Defines directory-to-archive and archive-to-directory movement |
| crates/…/transcoder.rs | Defines transformed archive creation and payload reuse |
| crates/…/summary.rs | Defines archive summary information |
| mbtiles/…/copy.rs | Shows cross-layout copy, filtering, cache metadata, diff, patch, and binary-diff guarantees |
| mbtiles/…/diff_validation.rs | Shows aggregate-hash checks and safe failure behavior for diff and patch workflows |