Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
upsicleclown committed Jun 15, 2023
1 parent 9e7c33e commit 89e414d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 48 deletions.
18 changes: 9 additions & 9 deletions martin-mbtiles/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use sqlx::{Connection, SqliteConnection};
about = "A utility to work with .mbtiles file content"
)]
pub struct Args {
/// Display detailed information
#[arg(short, long)]
verbose: bool,
#[command(subcommand)]
command: Commands,
}
Expand Down Expand Up @@ -46,9 +49,6 @@ enum Commands {
src_file: PathBuf,
/// MBTiles file to write to
dst_file: PathBuf,
/// Display detailed copying information
#[arg(short, long)]
verbose: bool,
/// Minimum zoom level to copy
#[arg(long)]
min_zoom: Option<u8>,
Expand All @@ -72,21 +72,21 @@ async fn main() -> Result<()> {
Commands::Copy {
src_file,
dst_file,
verbose,
min_zoom,
max_zoom,
zoom_levels,
} => {
let mut tile_copier_options =
TileCopierOptions::new().verbose(verbose).zooms(zoom_levels);
let mut copy_opts = TileCopierOptions::new()
.verbose(args.verbose)
.zooms(zoom_levels);
if let Some(v) = min_zoom {
tile_copier_options = tile_copier_options.min_zoom(v);
copy_opts = copy_opts.min_zoom(v);
};
if let Some(v) = max_zoom {
tile_copier_options = tile_copier_options.max_zoom(v);
copy_opts = copy_opts.max_zoom(v);
};

let tile_copier = TileCopier::new(src_file, dst_file, tile_copier_options)?;
let tile_copier = TileCopier::new(src_file, dst_file, copy_opts)?;

tile_copier.run().await?;
}
Expand Down
12 changes: 6 additions & 6 deletions martin-mbtiles/src/mbtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Metadata {
}

#[derive(Debug, PartialEq)]
pub enum Type {
pub enum MbtType {
TileTables,
DeDuplicated,
}
Expand Down Expand Up @@ -276,14 +276,14 @@ impl Mbtiles {
Ok(None)
}

pub async fn detect_type<T>(&self, conn: &mut T) -> MbtResult<Type>
pub async fn detect_type<T>(&self, conn: &mut T) -> MbtResult<MbtType>
where
for<'e> &'e mut T: SqliteExecutor<'e>,
{
if is_deduplicated_type(&mut *conn).await? {
Ok(Type::DeDuplicated)
Ok(MbtType::DeDuplicated)
} else if is_tile_tables_type(&mut *conn).await? {
Ok(Type::TileTables)
Ok(MbtType::TileTables)
} else {
Err(MbtError::InvalidDataFormat(self.filepath.clone()))
}
Expand Down Expand Up @@ -384,11 +384,11 @@ mod tests {
async fn detect_type() {
let (mut conn, mbt) = open("../tests/fixtures/files/world_cities.mbtiles").await;
let res = mbt.detect_type(&mut conn).await.unwrap();
assert_eq!(res, Type::TileTables);
assert_eq!(res, MbtType::TileTables);

let (mut conn, mbt) = open("../tests/fixtures/files/geography-class-jpg.mbtiles").await;
let res = mbt.detect_type(&mut conn).await.unwrap();
assert_eq!(res, Type::DeDuplicated);
assert_eq!(res, MbtType::DeDuplicated);

let (mut conn, mbt) = open(":memory:").await;
let res = mbt.detect_type(&mut conn).await;
Expand Down
54 changes: 22 additions & 32 deletions martin-mbtiles/src/tile_copier.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate core;

use crate::errors::MbtResult;
use crate::mbtiles::Type;
use crate::mbtiles::MbtType;
use crate::{MbtError, Mbtiles};
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::{query, Connection, Row, SqliteConnection};
Expand Down Expand Up @@ -56,6 +56,7 @@ impl TileCopierOptions {
self
}
}

impl TileCopier {
pub fn new(
src_filepath: PathBuf,
Expand Down Expand Up @@ -111,40 +112,35 @@ impl TileCopier {
.await?;

match storage_type {
Type::TileTables => self.copy_standard_compliant_tiles(&mut conn).await,
Type::DeDuplicated => self.copy_deduplicated_tiles(&mut conn).await,
MbtType::TileTables => self.copy_tile_tables(&mut conn).await,
MbtType::DeDuplicated => self.copy_deduplicated(&mut conn).await,
}
}

async fn copy_standard_compliant_tiles(&self, conn: &mut SqliteConnection) -> MbtResult<()> {
async fn copy_tile_tables(&self, conn: &mut SqliteConnection) -> MbtResult<()> {
// TODO: Handle options
// - bbox
// - verbose
// - zoom

self.run_query_with_options(
conn,
String::from("INSERT INTO tiles SELECT * FROM sourceDb.tiles"),
)
.await?;
self.run_query_with_options(conn, "INSERT INTO tiles SELECT * FROM sourceDb.tiles")
.await?;

Ok(())
}

async fn copy_deduplicated_tiles(&self, conn: &mut SqliteConnection) -> MbtResult<()> {
async fn copy_deduplicated(&self, conn: &mut SqliteConnection) -> MbtResult<()> {
query("INSERT INTO map SELECT * FROM sourceDb.map")
.execute(&mut *conn)
.await?;

self.run_query_with_options(
conn,
String::from(
"INSERT INTO images
"INSERT INTO images
SELECT images.tile_data, images.tile_id
FROM sourceDb.images
JOIN sourceDb.map
ON images.tile_id = map.tile_id",
),
)
.await?;

Expand All @@ -154,37 +150,31 @@ impl TileCopier {
async fn run_query_with_options(
&self,
conn: &mut SqliteConnection,
mut sql: String,
sql: &str,
) -> MbtResult<()> {
let mut params: Vec<String> = vec![];

if !&self.options.zooms.is_empty() {
sql.push_str(
format!(
" WHERE zoom_level IN ({})",
vec!["?"; self.options.zooms.len()].join(",")
)
.as_str(),
);
for zoom_level in &self.options.zooms {
params.push(zoom_level.to_string());
}
let sql = if !&self.options.zooms.is_empty() {
params.extend(self.options.zooms.iter().map(|z| z.to_string()));
format!(
"{sql} WHERE zoom_level IN ({})",
vec!["?"; self.options.zooms.len()].join(",")
)
} else if let Some(min_zoom) = &self.options.min_zoom {
if let Some(max_zoom) = &self.options.max_zoom {
sql.push_str(" WHERE zoom_level BETWEEN ? AND ?");

params.push(min_zoom.to_string());
params.push(max_zoom.to_string());
format!("{sql} WHERE zoom_level BETWEEN ? AND ?")
} else {
sql.push_str(" WHERE zoom_level >= ? ");

params.push(min_zoom.to_string());
format!("{sql} WHERE zoom_level >= ? ")
}
} else if let Some(max_zoom) = &self.options.max_zoom {
sql.push_str(" WHERE zoom_level <= ? ");

params.push(max_zoom.to_string());
}
format!("{sql} WHERE zoom_level <= ? ")
} else {
sql.to_string()
};

let mut query = query(sql.as_str());

Expand Down
4 changes: 3 additions & 1 deletion tests/expected/mbtiles/help.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
A utility to work with .mbtiles file content

Usage: mbtiles <COMMAND>
Usage: mbtiles [OPTIONS] <COMMAND>

Commands:
meta-get Gets a single value from the MBTiles metadata table
copy Copy tiles from one mbtiles file to another
help Print this message or the help of the given subcommand(s)

Options:
-v, --verbose Display detailed information
-h, --help Print help
-V, --version Print version

0 comments on commit 89e414d

Please sign in to comment.