Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add meta-get functionality #701

Merged
merged 8 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
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.

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)
8 changes: 8 additions & 0 deletions docs/src/tools.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Tools

## Mbtiles tools:
upsicleclown marked this conversation as resolved.
Show resolved Hide resolved
A small binary utility that allows users to interact with mbtiles files from the CLI as follows: `mbtiles <command> <file.mbtiles>`
upsicleclown marked this conversation as resolved.
Show resolved Hide resolved
- ### `meta-get`:
upsicleclown marked this conversation as resolved.
Show resolved Hide resolved
Retrieve a metadata value by key: `mbtiles meta-get <file.mbtiles> <options> <key>`.
upsicleclown marked this conversation as resolved.
Show resolved Hide resolved
#### Options:
- `-r, --raw` : return the raw metadata value
upsicleclown marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -21,6 +21,8 @@ serde_json.workspace = true
sqlx.workspace = true
thiserror.workspace = true
tilejson.workspace = true
tokio = { version = "1.25.0", features = ["full"] }
anyhow = "1.0"
nyurik marked this conversation as resolved.
Show resolved Hide resolved

# Bin dependencies
clap.workspace = true
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);
}
}