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

Feature: new mbtiles diff command #1068

Merged
merged 9 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
16 changes: 16 additions & 0 deletions docs/src/mbtiles-diff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Diffing MBTiles

## `mbtiles diff`

Diff command compares two mbtiles files `file A` and `file B`, and generates a delta (diff) file. If the diff file
is [applied](mbtiles-copy.md#mbtiles-apply-patch) to `A`, it will produce `B`.
The delta file will contain all tiles that are different between the two files (modifications, insertions, and deletions
as `NULL` values), for both the tile and metadata tables.
The only exception is `agg_tiles_has` metadata value. It will be renamed to `agg_tiles_hash_in_diff` and a
new `agg_tiles_hash` will be generated for the diff file itself.
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved

```shell
# This command will comapre `file_a.mbtiles` and `file_b.mbtiles`, and generate a new diff file `diff_result.mbtiles`,This command will compares file_a.mbtiles and file_b.mbtiles, and generates a new diff file diff_result.mbtiles
# If the diff file is applied to file_a, it will produce file_b.
mbtiles diff file_a.mbtiles file_b.mbtiles diff_result.mbtiles
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved
```
56 changes: 55 additions & 1 deletion mbtiles/src/bin/mbtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ enum Commands {
/// Value to set, or nothing if the key should be deleted.
value: Option<String>,
},
/// Compare two files A and B, and generate a new diff file. If the diff file is applied to A, it will produce B.
#[command(name = "diff")]
Diff {
file_a: PathBuf,
file_b: PathBuf,
diff: PathBuf,
},
/// Copy tiles from one mbtiles file to another.
#[command(name = "copy", alias = "cp")]
Copy(CopyArgs),
Expand Down Expand Up @@ -212,6 +219,28 @@ async fn main_int() -> anyhow::Result<()> {
println!("MBTiles file summary for {mbt}");
println!("{}", mbt.summary(&mut conn).await?);
}
Commands::Diff {
file_a,
file_b,
diff,
} => {
let opts = MbtilesCopier {
src_file: file_a,
diff_with_file: Some(file_b),
dst_file: diff,
copy: CopyType::All,
skip_agg_tiles_hash: false,
on_duplicate: Some(CopyDuplicateMode::Override),
dst_type_cli: None,
dst_type: None,
min_zoom: None,
max_zoom: None,
zoom_levels: vec![],
bbox: vec![],
apply_patch: None,
};
opts.run().await?;
}
}

Ok(())
Expand Down Expand Up @@ -253,7 +282,7 @@ mod tests {
use mbtiles::CopyDuplicateMode;

use super::*;
use crate::Commands::{ApplyPatch, Copy, MetaGetValue, MetaSetValue, Validate};
use crate::Commands::{ApplyPatch, Copy, Diff, MetaGetValue, MetaSetValue, Validate};
use crate::{Args, IntegrityCheckType};

#[test]
Expand Down Expand Up @@ -524,4 +553,29 @@ mod tests {
}
);
}

#[test]
fn test_diff() {
let file_a = PathBuf::from("../tests/fixtures/mbtiles/geography-class-jpg.mbtiles");
let file_b = PathBuf::from("file:copy_with_diff_with_file_mem_db?mode=memory&cache=shared");

let diff = PathBuf::from("../tests/fixtures/mbtiles/geography-class-jpg-modified.mbtiles");
assert_eq!(
Args::parse_from([
"mbtiles",
"diff",
file_a.to_str().unwrap(),
file_b.to_str().unwrap(),
diff.to_str().unwrap()
]),
Args {
verbose: false,
command: Diff {
file_a,
file_b,
diff,
}
}
);
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading