Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ itertools = "0.14"
json-patch = "4"
lambda-web = { version = "0.2.1", features = ["actix4"] }
log = "0.4"
martin-tile-utils = { path = "./martin-tile-utils", version = "0.6.0" }
mbtiles = { path = "./mbtiles", version = "0.12.0" }
martin-tile-utils = { path = "./martin-tile-utils", version = "0.6.1" }
mbtiles = { path = "./mbtiles", version = "0.12.2" }
md5 = "0.7.0"
moka = { version = "0.12", features = ["future"] }
num_cpus = "1"
Expand Down
2 changes: 1 addition & 1 deletion martin/src/mbtiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Debug for MbtSource {

impl MbtSource {
async fn new(id: String, path: PathBuf) -> FileResult<Self> {
let mbt = MbtilesPool::new(&path)
let mbt = MbtilesPool::open_readonly(&path)
.await
.map_err(|e| io::Error::other(format!("{e:?}: Cannot open file {}", path.display())))
.map_err(|e| IoError(e, path.clone()))?;
Expand Down
2 changes: 1 addition & 1 deletion mbtiles/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mbtiles"
version = "0.12.1"
version = "0.12.2"
authors = ["Yuri Astrakhan <YuriAstrakhan@gmail.com>", "MapLibre contributors"]
description = "A simple low-level MbTiles access and processing library, with some tile format detection and other relevant heuristics."
keywords = ["mbtiles", "maps", "tiles", "mvt", "tilejson"]
Expand Down
48 changes: 47 additions & 1 deletion mbtiles/src/mbtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
use sqlite_compressions::{register_bsdiffraw_functions, register_gzip_functions};
use sqlite_hashes::register_md5_functions;
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::{Connection as _, Executor, SqliteConnection, SqliteExecutor, Statement, query};
use sqlx::{Connection as _, Executor, Row, SqliteConnection, SqliteExecutor, Statement, query};

use crate::bindiff::PatchType;
use crate::errors::{MbtError, MbtResult};
Expand Down Expand Up @@ -132,6 +132,8 @@ impl Mbtiles {
}

/// Get a tile from the database
///
/// See [`Mbtiles::get_tile_and_hash`] if you do need the hash
pub async fn get_tile<T>(
&self,
conn: &mut T,
Expand All @@ -153,6 +155,50 @@ impl Mbtiles {
Ok(None)
}

/// Get a tile and its hash if it exists from the database
///
/// For [`MbtType::Flat`] accessing the hash is not possible, we thus md5 hash the tile data.
///
/// See [`Mbtiles::get_tile`] if you don't need the hash
pub async fn get_tile_and_hash(
&self,
conn: &mut SqliteConnection,
mbt_type: MbtType,
z: u8,
x: u32,
y: u32,
) -> MbtResult<Option<(Vec<u8>, Option<String>)>> {
let sql = Self::get_tile_and_hash_sql(mbt_type);
let y = invert_y_value(z, y);
let Some(row) = query(sql)
.bind(z)
.bind(x)
.bind(y)
.fetch_optional(conn)
.await?
else {
return Ok(None);
};
Ok(Some((row.get(0), row.get(1))))
}

/// sql query for getting tile and hash
///
/// For [`MbtType::Flat`] accessing it is not possible, we thus md5 hash the tile data.
fn get_tile_and_hash_sql(mbt_type: MbtType) -> &'static str {
match mbt_type {
MbtType::Flat => {
"SELECT tile_data, NULL as tile_hash from tiles where zoom_level = ? AND tile_column = ? AND tile_row = ?"
}
MbtType::FlatWithHash | MbtType::Normalized { hash_view: true } => {
"SELECT tile_data, tile_hash from tiles_with_hash where zoom_level = ? AND tile_column = ? AND tile_row = ?"
}
MbtType::Normalized { hash_view: false } => {
"SELECT images.tile_data, images.tile_id AS tile_hash FROM map JOIN images ON map.tile_id = images.tile_id where map.zoom_level = ? AND map.tile_column = ? AND map.tile_row = ?"
}
}
}

pub async fn insert_tiles(
&self,
conn: &mut SqliteConnection,
Expand Down
Loading
Loading