Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Sep 28, 2023
1 parent ad8bba5 commit d52787a
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 172 deletions.

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

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

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

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

6 changes: 2 additions & 4 deletions martin-mbtiles/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use std::path::{Path, PathBuf};

use anyhow::Result;
use clap::{Parser, Subcommand};
use martin_mbtiles::{
apply_mbtiles_diff, copy_mbtiles_file, IntegrityCheckType, Mbtiles, TileCopierOptions,
};
use martin_mbtiles::{apply_mbtiles_diff, IntegrityCheckType, Mbtiles, TileCopierOptions};
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::{Connection, SqliteConnection};

Expand Down Expand Up @@ -88,7 +86,7 @@ async fn main() -> Result<()> {
meta_set_value(file.as_path(), &key, value).await?;
}
Commands::Copy(opts) => {
copy_mbtiles_file(opts).await?;
opts.run().await?;
}
Commands::ApplyDiff {
src_file,
Expand Down
4 changes: 1 addition & 3 deletions martin-mbtiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ mod tile_copier;
pub use errors::MbtError;
pub use mbtiles::{IntegrityCheckType, Mbtiles, Metadata};
pub use mbtiles_pool::MbtilesPool;
pub use tile_copier::{
apply_mbtiles_diff, copy_mbtiles_file, CopyDuplicateMode, TileCopierOptions,
};
pub use tile_copier::{apply_mbtiles_diff, CopyDuplicateMode, TileCopierOptions};
34 changes: 17 additions & 17 deletions martin-mbtiles/src/mbtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,10 @@ impl Mbtiles {
.filename(self.filepath())
.read_only(readonly);
let mut conn = SqliteConnection::connect_with(&opt).await?;
self.attach_hash_fn(&mut conn).await?;
attach_hash_fn(&mut conn).await?;
Ok(conn)
}

async fn attach_hash_fn(&self, conn: &mut SqliteConnection) -> MbtResult<()> {
let handle = conn.lock_handle().await?.as_raw_handle().as_ptr();
// Safety: we know that the handle is a SQLite connection is locked and is not used anywhere else.
// The registered functions will be dropped when SQLX drops DB connection.
let rc = unsafe { sqlite_hashes::rusqlite::Connection::from_handle(handle) }?;
register_md5_function(&rc)?;
Ok(())
}

#[must_use]
pub fn filepath(&self) -> &str {
&self.filepath
Expand Down Expand Up @@ -490,7 +481,7 @@ impl Mbtiles {
}

/// Compute new aggregate tiles hash and save it to the metadata table (if needed)
pub(crate) async fn update_agg_tiles_hash<T>(&self, conn: &mut T) -> MbtResult<()>
pub async fn update_agg_tiles_hash<T>(&self, conn: &mut T) -> MbtResult<()>
where
for<'e> &'e mut T: SqliteExecutor<'e>,
{
Expand Down Expand Up @@ -573,7 +564,8 @@ where
let query = query(
// The md5_concat func will return NULL if there are no rows in the tiles table.
// For our use case, we will treat it as an empty string, and hash that.
"SELECT hex(
"SELECT
hex(
coalesce(
md5_concat(
cast(zoom_level AS text),
Expand All @@ -587,7 +579,16 @@ where
FROM tiles
ORDER BY zoom_level, tile_column, tile_row;",
);
return Ok(query.fetch_one(conn).await?.get::<String, _>(0));
Ok(query.fetch_one(conn).await?.get::<String, _>(0))
}

pub async fn attach_hash_fn(conn: &mut SqliteConnection) -> MbtResult<()> {
let handle = conn.lock_handle().await?.as_raw_handle().as_ptr();
// Safety: we know that the handle is a SQLite connection is locked and is not used anywhere else.
// The registered functions will be dropped when SQLX drops DB connection.
let rc = unsafe { sqlite_hashes::rusqlite::Connection::from_handle(handle) }?;
register_md5_function(&rc)?;
Ok(())
}

#[cfg(test)]
Expand All @@ -601,10 +602,9 @@ mod tests {

async fn open(filepath: &str) -> (SqliteConnection, Mbtiles) {
let mbt = Mbtiles::new(filepath).unwrap();
(
SqliteConnection::connect(mbt.filepath()).await.unwrap(),
mbt,
)
let mut conn = SqliteConnection::connect(mbt.filepath()).await.unwrap();
attach_hash_fn(&mut conn).await.unwrap();
(conn, mbt)
}

#[actix_rt::test]
Expand Down
Loading

0 comments on commit d52787a

Please sign in to comment.