Skip to content

Commit

Permalink
Add mbtiles meta-get utility (#701)
Browse files Browse the repository at this point in the history
* Add functionality to retrieve a metadata value in an mbtiles file by
key; simple implementation for one of the items in #667
* Also, disable TTY in docker-up `just` target
  • Loading branch information
upsicleclown authored Jun 3, 2023
1 parent 290a7c1 commit 78e67c3
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 5 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ actix-cors = "0.6"
actix-http = "3"
actix-rt = "2"
actix-web = "4"
anyhow = "1.0"
async-trait = "0.1"
brotli = "3"
cargo-husky = { version = "1", features = ["user-hooks"], default-features = false }
Expand Down Expand Up @@ -119,3 +120,4 @@ sqlx = { version = "0.6", features = ["offline", "sqlite", "runtime-actix-native
subst = { version = "0.2", features = ["yaml"] }
thiserror = "1"
tilejson = "0.3"
tokio = { version = "1.28.2", features = ["macros"] }
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
- [Using with deck.gl](using-with-deck-gl.md)
- [Using with Mapbox](using-with-mapbox.md)
- [Recipes](recipes.md)
- [Tools](tools.md)
- [Development](development.md)
7 changes: 7 additions & 0 deletions docs/src/tools.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Tools

## MBTiles tools
A small utility that allows users to interact with mbtiles files from the CLI as follows: `mbtiles <command> ...`

#### `meta-get`
Retrieve a metadata value by its name: `mbtiles meta-get <file.mbtiles> <key>`. See `mbtiles meta-get --help` to see available options.
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ start-legacy: (docker-up "db-legacy")
[private]
docker-up name:
docker-compose up -d {{ name }}
docker-compose run --rm db-is-ready
docker-compose run -T --rm db-is-ready

alias _down := stop
alias _stop-db := stop
Expand Down
2 changes: 2 additions & 0 deletions martin-mbtiles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ thiserror.workspace = true
tilejson.workspace = true

# Bin dependencies
anyhow.workspace = true
clap.workspace = true
tokio.workspace = true

[dev-dependencies]
# For testing, might as well use the same async framework as the Martin itself
Expand Down
18 changes: 18 additions & 0 deletions martin-mbtiles/sqlx-data.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
{
"db": "SQLite",
"386a375cf65c3e5aef51deffc99d23bd852ba445c1058aed380fe83bed618c29": {
"describe": {
"columns": [
{
"name": "value",
"ordinal": 0,
"type_info": "Text"
}
],
"nullable": [
true
],
"parameters": {
"Right": 1
}
},
"query": "SELECT value from metadata where name = ?"
},
"5b298df51dccbf0d8a22433a99febc59c27dbf204d09a9c1fb0b3bf9aaad284b": {
"describe": {
"columns": [
Expand Down
36 changes: 32 additions & 4 deletions martin-mbtiles/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::Result;
use clap::{Parser, Subcommand};
use martin_mbtiles::Mbtiles;
use std::path::PathBuf;

#[derive(Parser, Debug)]
Expand All @@ -25,6 +27,11 @@ enum Commands {
MetaGetValue {
/// MBTiles file to read a value from
file: PathBuf,
/// Value to read
key: String,
/// Output the raw value
#[arg(short, long)]
raw: bool,
},
/// Sets a single value in the metadata table, or deletes it if no value.
#[command(name = "meta-set")]
Expand All @@ -41,10 +48,31 @@ enum Commands {
},
}

fn main() {
#[tokio::main]
async fn main() -> Result<()> {
let args = Args::parse();

println!("Parsed args:\n");
println!("{args:#?}");
println!();
match args.command {
Commands::MetaGetValue { file, key, raw } => {
let mbt = Mbtiles::new(&file).await?;

let value = mbt.get_metadata_value(&key).await?;

if raw {
if let Some(s) = value {
println!("{s}")
}
} else {
match value {
Some(s) => println!(r#"The value for metadata key "{key}" is:\n "{s}""#),
None => println!(r#"No value for metadata key "{key}""#),
}
}
}
_ => {
unimplemented!("Oops! This command is not yet available, stay tuned for future updates")
}
}

Ok(())
}
38 changes: 38 additions & 0 deletions martin-mbtiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ impl Mbtiles {
}
}

pub async fn get_metadata_value(&self, key: &str) -> MbtResult<Option<String>> {
let mut conn = self.pool.acquire().await?;

let query = query! {"SELECT value from metadata where name = ?", key};
let row = query.fetch_optional(&mut conn).await?;
if let Some(row) = row {
if let Some(value) = row.value {
return Ok(Some(value));
}
}
Ok(None)
}

pub async fn get_metadata(&self) -> MbtResult<Metadata> {
let mut conn = self.pool.acquire().await?;

Expand Down Expand Up @@ -314,4 +327,29 @@ mod tests {
);
assert_eq!(metadata.layer_type, Some("overlay".to_string()));
}

#[actix_rt::test]
async fn metadata_get_key() {
let mbt = Mbtiles::new(Path::new("../tests/fixtures/files/world_cities.mbtiles"))
.await
.unwrap();

assert_eq!(
mbt.get_metadata_value("bounds").await.unwrap().unwrap(),
"-123.123590,-37.818085,174.763027,59.352706"
);
assert_eq!(
mbt.get_metadata_value("name").await.unwrap().unwrap(),
"Major cities from Natural Earth data"
);
assert_eq!(
mbt.get_metadata_value("maxzoom").await.unwrap().unwrap(),
"6"
);
assert_eq!(
mbt.get_metadata_value("nonexistent_key").await.unwrap(),
None
);
assert_eq!(mbt.get_metadata_value("").await.unwrap(), None);
}
}

0 comments on commit 78e67c3

Please sign in to comment.