From 3ca6ac6980543293464f56ad1effbf95f65ca8b7 Mon Sep 17 00:00:00 2001 From: Pirmin Kalberer Date: Tue, 12 Nov 2024 17:57:18 +0100 Subject: [PATCH] Fix runtime MBTile cache --- bbox-tile-server/src/store/mbtiles.rs | 49 +++++++++++++++++++++++---- bbox-tile-server/src/store/mod.rs | 1 + bbox.toml | 2 +- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/bbox-tile-server/src/store/mbtiles.rs b/bbox-tile-server/src/store/mbtiles.rs index 078544c2..288fe0f2 100644 --- a/bbox-tile-server/src/store/mbtiles.rs +++ b/bbox-tile-server/src/store/mbtiles.rs @@ -4,7 +4,7 @@ use crate::store::{StoreFromConfig, TileReader, TileStore, TileStoreError, TileW use async_trait::async_trait; use bbox_core::{Compression, Format, TileResponse}; use log::info; -use martin_mbtiles::{invert_y_value, CopyDuplicateMode, Metadata}; +use martin_mbtiles::{invert_y_value, Metadata}; use martin_tile_utils::Format as TileFormat; use sqlx::{Acquire, Executor, Statement}; use std::ffi::OsStr; @@ -81,14 +81,49 @@ impl TileWriter for MbtilesDatasource { } async fn put_tile(&self, xyz: &Xyz, data: Vec) -> Result<(), TileStoreError> { let mut conn = self.pool.acquire().await?; - self.mbtiles - .insert_tiles( - &mut conn, - self.layout, - CopyDuplicateMode::Override, - &[(xyz.z, xyz.x as u32, xyz.y as u32, data)], + // self.mbtiles + // .insert_tiles( + // &mut conn, + // self.layout, + // CopyDuplicateMode::Override, + // &[(xyz.z, xyz.x as u32, xyz.y as u32, data)], + // ) + // .await?; + debug_assert_eq!( + self.layout, + martin_mbtiles::MbtType::Normalized { hash_view: true } + ); + // TODO: common code with put_tiles + let mut tx = conn.begin().await?; + let sql2 = tx + .prepare( + "INSERT OR IGNORE INTO images (tile_id, tile_data) + VALUES (?1, ?2);", ) .await?; + let sql1 = tx + .prepare( + "INSERT OR REPLACE INTO map (zoom_level, tile_column, tile_row, tile_id) + VALUES (?1, ?2, ?3, ?4);", + ) + .await?; + let (z, x, y, tile_data) = (&xyz.z, &(xyz.x as u32), &(xyz.y as u32), &data); + let hash = blake3::hash(tile_data).to_hex(); + sql2.query() + .bind(hash.as_str()) + .bind(tile_data) + .execute(&mut *tx) + .await?; + + let y = invert_y_value(*z, *y); + sql1.query() + .bind(z) + .bind(x) + .bind(y) + .bind(hash.as_str()) + .execute(&mut *tx) + .await?; + tx.commit().await?; Ok(()) } async fn put_tiles(&mut self, tiles: &[(u8, u32, u32, Vec)]) -> Result<(), TileStoreError> { diff --git a/bbox-tile-server/src/store/mod.rs b/bbox-tile-server/src/store/mod.rs index 782c7458..f2fd1c7d 100644 --- a/bbox-tile-server/src/store/mod.rs +++ b/bbox-tile-server/src/store/mod.rs @@ -66,6 +66,7 @@ pub trait TileWriter: DynClone + Send + Sync { /// Write tile into store async fn put_tile(&self, xyz: &Xyz, data: Vec) -> Result<(), TileStoreError>; /// Write tile into store requiring &mut self + // mut is required for PMTiles writer async fn put_tile_mut(&mut self, xyz: &Xyz, data: Vec) -> Result<(), TileStoreError> { // Most implementations support writing without &mut self self.put_tile(xyz, data).await diff --git a/bbox.toml b/bbox.toml index 31582bc4..29dc8976 100644 --- a/bbox.toml +++ b/bbox.toml @@ -175,7 +175,7 @@ cache = "filecache" [[tileset]] name = "ne_umn" map_service = { project = "ne", suffix = "map", layers = "country", tile_size = 512 } -#cache = "mbtilecache" +cache = "mbtilecache" [[tileset]] name = "gebco"