Skip to content

Commit

Permalink
better messaging on hash check
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Sep 5, 2023
1 parent c1d8c2e commit cff352f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion martin-mbtiles/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async fn validate_mbtiles(
let mbt = Mbtiles::new(file)?;
let opt = SqliteConnectOptions::new().filename(file).read_only(true);
let mut conn = SqliteConnection::connect_with(&opt).await?;
mbt.integrity_check(&mut conn, check_type).await?;
mbt.check_integrity(&mut conn, check_type).await?;
mbt.check_each_tile_hash(&mut conn).await?;
if update_agg_tiles_hash {
mbt.update_agg_tiles_hash(&mut conn).await?;
Expand Down
57 changes: 41 additions & 16 deletions martin-mbtiles/src/mbtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ impl Mbtiles {
Ok(None)
}

/// Get the aggregate tiles hash value from the metadata table
pub async fn get_agg_tiles_hash<T>(&self, conn: &mut T) -> MbtResult<Option<String>>
where
for<'e> &'e mut T: SqliteExecutor<'e>,
{
self.get_metadata_value(&mut *conn, "agg_tiles_hash").await
}

pub async fn set_metadata_value<T>(
&self,
conn: &mut T,
Expand Down Expand Up @@ -425,18 +433,8 @@ impl Mbtiles {
Ok(rusqlite_conn)
}

/// Compute new aggregate tiles hash and save it to the metadata table
pub async fn update_agg_tiles_hash<T>(&self, conn: &mut T) -> MbtResult<()>
where
for<'e> &'e mut T: SqliteExecutor<'e>,
{
let hash = self.calc_agg_tiles_hash().await?;
self.set_metadata_value(conn, "agg_tiles_hash", Some(hash))
.await
}

/// Perform SQLIte internal integrity check
pub async fn integrity_check<T>(
pub async fn check_integrity<T>(
&self,
conn: &mut T,
integrity_check: IntegrityCheckType,
Expand Down Expand Up @@ -475,10 +473,7 @@ impl Mbtiles {
where
for<'e> &'e mut T: SqliteExecutor<'e>,
{
let Some(stored) = self
.get_metadata_value(&mut *conn, "agg_tiles_hash")
.await?
else {
let Some(stored) = self.get_agg_tiles_hash(&mut *conn).await? else {
return Err(AggHashValueNotFound(self.filepath().to_string()));
};

Expand All @@ -491,6 +486,36 @@ impl Mbtiles {
Ok(())
}

/// Compute new aggregate tiles hash and save it to the metadata table (if needed)
pub async fn update_agg_tiles_hash<T>(&self, conn: &mut T) -> MbtResult<()>
where
for<'e> &'e mut T: SqliteExecutor<'e>,
{
let old_hash = self.get_agg_tiles_hash(&mut *conn).await?;
let hash = self.calc_agg_tiles_hash().await?;
if old_hash.as_ref() == Some(&hash) {
info!(
"agg_tiles_hash is already set to the correct value `{hash}` in {}",
self.filepath()
);
Ok(())
} else {
if let Some(old_hash) = old_hash {
info!(
"Updating agg_tiles_hash from {old_hash} to {hash} in {}",
self.filepath()
);
} else {
info!(
"Initializing agg_tiles_hash to {hash} in {}",
self.filepath()
);
}
self.set_metadata_value(&mut *conn, "agg_tiles_hash", Some(hash))
.await
}
}

pub async fn check_each_tile_hash<T>(&self, conn: &mut T) -> MbtResult<()>
where
for<'e> &'e mut T: SqliteExecutor<'e>,
Expand Down Expand Up @@ -673,7 +698,7 @@ mod tests {
async fn validate_valid_file() {
let (mut conn, mbt) = open("../tests/fixtures/files/zoomed_world_cities.mbtiles").await;

mbt.integrity_check(&mut conn, IntegrityCheckType::Quick)
mbt.check_integrity(&mut conn, IntegrityCheckType::Quick)
.await
.unwrap();
}
Expand Down

0 comments on commit cff352f

Please sign in to comment.