Skip to content

Commit

Permalink
Rename MartinError, benchmarks, streamline get_tile
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Nov 20, 2023
1 parent c6a7ef6 commit 12c662f
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 69 deletions.
4 changes: 4 additions & 0 deletions martin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ path = "src/bin/martin.rs"
name = "martin-cp"
path = "src/bin/martin-cp.rs"

[[bench]]
name = "bench"
harness = false

[features]
default = []
bless-tests = []
Expand Down
79 changes: 79 additions & 0 deletions martin/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use actix_web::dev::ResourceDef;
use actix_web::test::TestRequest;
use actix_web::web::{Data, Path};
use actix_web::FromRequest;
use async_trait::async_trait;
use criterion::async_executor::FuturesExecutor;
use criterion::{criterion_group, criterion_main, Criterion};
use martin::srv::{get_tile_impl, TileRequest};
use martin::{CatalogSourceEntry, MartinError, Source, Tile, TileSources, UrlQuery, Xyz};
use martin_tile_utils::{Encoding, Format, TileInfo};
use tilejson::{tilejson, TileJSON};

#[derive(Clone, Debug)]
struct NullSource {
tilejson: TileJSON,
}

impl NullSource {
fn new() -> Self {
Self {
tilejson: tilejson! { "https://example.com/".to_string() },
}
}
}

#[async_trait]
impl Source for NullSource {
fn get_id(&self) -> &str {
"null"
}

fn get_tilejson(&self) -> &TileJSON {
&self.tilejson
}

fn get_tile_info(&self) -> TileInfo {
TileInfo::new(Format::Png, Encoding::Internal)
}

fn clone_source(&self) -> Box<dyn Source> {
Box::new(self.clone())
}

fn support_url_query(&self) -> bool {
false
}

async fn get_tile(&self, _xyz: &Xyz, _query: &Option<UrlQuery>) -> Result<Tile, MartinError> {
Ok(Vec::new())
}

fn get_catalog_entry(&self) -> CatalogSourceEntry {
CatalogSourceEntry::default()
}
}

async fn process_tile(resource: &ResourceDef, sources: Data<TileSources>) {
let mut request = TestRequest::get()
.uri("https://example.com/null/0/0/0")
.to_srv_request();
resource.capture_match_info(request.match_info_mut());
let (req, mut pl) = request.into_parts();
let path = Path::<TileRequest>::from_request(&req, &mut pl)
.await
.unwrap();
get_tile_impl(req, path, sources).await.unwrap();
}

fn bench_null_source(c: &mut Criterion) {
let sources = Data::new(TileSources::new(vec![vec![Box::new(NullSource::new())]]));
let resource = ResourceDef::new("/{source_ids}/{z}/{x}/{y}");
c.bench_function("get_table_source_tile", |b| {
b.to_async(FuturesExecutor)
.iter(|| process_tile(&resource, sources.clone()));
});
}

criterion_group!(benches, bench_null_source);
criterion_main!(benches);
6 changes: 3 additions & 3 deletions martin/src/args/connections.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Error;
use crate::MartinError;

#[derive(Debug, Clone)]
pub enum State<T: Clone> {
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Arguments {
}

/// Check that all params have been claimed
pub fn check(self) -> Result<(), Error> {
pub fn check(self) -> Result<(), MartinError> {
let mut unrecognized = Vec::new();
for (i, value) in self.values.into_iter().enumerate() {
if let State::Ignore = self.state[i] {
Expand All @@ -68,7 +68,7 @@ impl Arguments {
if unrecognized.is_empty() {
Ok(())
} else {
Err(Error::UnrecognizableConnections(unrecognized))
Err(MartinError::UnrecognizableConnections(unrecognized))
}
}
}
4 changes: 2 additions & 2 deletions martin/src/args/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ mod tests {

use super::*;
use crate::test_utils::{os, some, FauxEnv};
use crate::Error;
use crate::MartinError;

#[test]
fn test_extract_conn_strings() {
Expand All @@ -217,7 +217,7 @@ mod tests {
vec!["postgresql://localhost:5432", "postgres://localhost:5432"]
);
assert!(matches!(args.check(), Err(
Error::UnrecognizableConnections(v)) if v == vec!["mysql://localhost:3306"]));
MartinError::UnrecognizableConnections(v)) if v == vec!["mysql://localhost:3306"]));
}

#[test]
Expand Down
10 changes: 6 additions & 4 deletions martin/src/args/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use crate::args::srv::SrvArgs;
use crate::args::State::{Ignore, Share, Take};
use crate::config::Config;
use crate::file_config::FileConfigEnum;
use crate::{Error, OptOneMany, Result};
use crate::MartinError::ConfigAndConnectionsError;
use crate::{OptOneMany, Result};

#[derive(Parser, Debug, PartialEq, Default)]
#[command(about, version)]
Expand Down Expand Up @@ -65,7 +66,7 @@ impl Args {
warn!("The WATCH_MODE env variable is no longer supported, and will be ignored");
}
if self.meta.config.is_some() && !self.meta.connection.is_empty() {
return Err(Error::ConfigAndConnectionsError(self.meta.connection));
return Err(ConfigAndConnectionsError(self.meta.connection));
}

self.srv.merge_into_config(&mut config.srv);
Expand Down Expand Up @@ -122,6 +123,7 @@ mod tests {
use crate::pg::PgConfig;
use crate::test_utils::{some, FauxEnv};
use crate::utils::OptOneMany;
use crate::MartinError::UnrecognizableConnections;

fn parse(args: &[&str]) -> Result<(Config, MetaArgs)> {
let args = Args::parse_from(args);
Expand Down Expand Up @@ -188,7 +190,7 @@ mod tests {
let env = FauxEnv::default();
let mut config = Config::default();
let err = args.merge_into_config(&mut config, &env).unwrap_err();
assert!(matches!(err, crate::Error::ConfigAndConnectionsError(..)));
assert!(matches!(err, ConfigAndConnectionsError(..)));
}

#[test]
Expand All @@ -199,6 +201,6 @@ mod tests {
let mut config = Config::default();
let err = args.merge_into_config(&mut config, &env).unwrap_err();
let bad = vec!["foobar".to_string()];
assert!(matches!(err, crate::Error::UnrecognizableConnections(v) if v == bad));
assert!(matches!(err, UnrecognizableConnections(v) if v == bad));
}
}
13 changes: 6 additions & 7 deletions martin/src/bin/martin-cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use log::{debug, error, info, log_enabled};
use martin::args::{Args, ExtraArgs, MetaArgs, OsEnv, PgArgs, SrvArgs};
use martin::srv::{get_composite_tile, merge_tilejson, RESERVED_KEYWORDS};
use martin::{
append_rect, read_config, Config, Error, IdResolver, Result, ServerState, TileRect, Xyz,
append_rect, read_config, Config, IdResolver, MartinError, Result, ServerState, TileRect, Xyz,
};
use mbtiles::{
init_mbtiles_schema, is_empty_database, CopyDuplicateMode, MbtType, MbtTypeCli, Mbtiles,
Expand Down Expand Up @@ -223,7 +223,7 @@ fn iterate_tiles(tiles: Vec<TileRect>) -> impl Iterator<Item = Xyz> {
})
}

pub async fn run_tile_copy(args: CopyArgs, state: ServerState) -> Result<()> {
async fn run_tile_copy(args: CopyArgs, state: ServerState) -> Result<()> {
let output_file = &args.output_file;
let concurrency = args.concurrency.unwrap_or(1);
let (sources, _use_url_query, info) = state.tiles.get_sources(args.source.as_str(), None)?;
Expand All @@ -233,7 +233,6 @@ pub async fn run_tile_copy(args: CopyArgs, state: ServerState) -> Result<()> {
let tiles = compute_tile_ranges(&args);
let mbt = Mbtiles::new(output_file)?;
let mut conn = mbt.open_or_new().await?;
let on_dupl = args.on_duplicate;

let dst_type = if is_empty_database(&mut conn).await? {
let dst_type = match args.dst_type.unwrap_or(MbtTypeCli::Normalized) {
Expand Down Expand Up @@ -269,14 +268,14 @@ pub async fn run_tile_copy(args: CopyArgs, state: ServerState) -> Result<()> {
try_join!(
async move {
stream::iter(iterate_tiles(tiles))
.map(Ok::<Xyz, Error>)
.map(Ok::<Xyz, MartinError>)
.try_for_each_concurrent(concurrency, |xyz| {
let tx = tx.clone();
async move {
let data = get_composite_tile(sources, info, &xyz, None).await?;
tx.send(Tile { xyz, data })
.await
.map_err(|e| Error::InternalError(e.to_string()))?;
.map_err(|e| MartinError::InternalError(e.into()))?;
Ok(())
}
})
Expand All @@ -293,7 +292,7 @@ pub async fn run_tile_copy(args: CopyArgs, state: ServerState) -> Result<()> {
} else {
batch.push((tile.xyz.z, tile.xyz.x, tile.xyz.y, tile.data));
if batch.len() >= BATCH_SIZE || last_saved.elapsed() > SAVE_EVERY {
mbt.insert_tiles(&mut conn, dst_type, on_dupl, &batch)
mbt.insert_tiles(&mut conn, dst_type, args.on_duplicate, &batch)
.await?;
batch.clear();
last_saved = Instant::now();
Expand All @@ -308,7 +307,7 @@ pub async fn run_tile_copy(args: CopyArgs, state: ServerState) -> Result<()> {
}
}
if !batch.is_empty() {
mbt.insert_tiles(&mut conn, dst_type, on_dupl, &batch)
mbt.insert_tiles(&mut conn, dst_type, args.on_duplicate, &batch)
.await?;
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion martin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::pmtiles::PmtSource;
use crate::source::{TileInfoSources, TileSources};
use crate::sprites::SpriteSources;
use crate::srv::SrvConfig;
use crate::Error::{ConfigLoadError, ConfigParseError, ConfigWriteError, NoSources};
use crate::MartinError::{ConfigLoadError, ConfigParseError, ConfigWriteError, NoSources};
use crate::{IdResolver, OptOneMany, Result};

pub type UnrecognizedValues = HashMap<String, serde_yaml::Value>;
Expand Down
8 changes: 4 additions & 4 deletions martin/src/file_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
use crate::config::{copy_unrecognized_config, UnrecognizedValues};
use crate::file_config::FileError::{InvalidFilePath, InvalidSourceFilePath, IoError};
use crate::source::{Source, TileInfoSources};
use crate::utils::{Error, IdResolver, OptOneMany};
use crate::utils::{IdResolver, MartinError, OptOneMany};
use crate::OptOneMany::{Many, One};

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -102,7 +102,7 @@ impl FileConfigEnum {
}
}

pub fn finalize(&self, prefix: &str) -> Result<UnrecognizedValues, Error> {
pub fn finalize(&self, prefix: &str) -> Result<UnrecognizedValues, MartinError> {
let mut res = UnrecognizedValues::new();
if let Self::Config(cfg) = self {
copy_unrecognized_config(&mut res, prefix, &cfg.unrecognized);
Expand Down Expand Up @@ -171,12 +171,12 @@ pub async fn resolve_files<Fut>(
idr: IdResolver,
extension: &str,
new_source: &mut impl FnMut(String, PathBuf) -> Fut,
) -> Result<TileInfoSources, Error>
) -> Result<TileInfoSources, MartinError>
where
Fut: Future<Output = Result<Box<dyn Source>, FileError>>,
{
resolve_int(config, idr, extension, new_source)
.map_err(crate::Error::from)
.map_err(crate::MartinError::from)
.await
}

Expand Down
8 changes: 5 additions & 3 deletions martin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ mod config;
pub use config::{read_config, Config, ServerState};

mod source;
pub use source::Source;
pub use source::{
CatalogSourceEntry, Source, Tile, TileInfoSource, TileInfoSources, TileSources, UrlQuery,
};

mod utils;
pub use utils::{
append_rect, decode_brotli, decode_gzip, Error, IdResolver, OptBoolObj, OptOneMany, Result,
TileRect, Xyz,
append_rect, decode_brotli, decode_gzip, IdResolver, MartinError, OptBoolObj, OptOneMany,
Result, TileRect, Xyz,
};

pub mod args;
Expand Down
8 changes: 6 additions & 2 deletions martin/src/mbtiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tilejson::TileJSON;
use crate::file_config::FileError;
use crate::file_config::FileError::{AquireConnError, InvalidMetadata, IoError};
use crate::source::{Tile, UrlQuery};
use crate::{Error, Source, Xyz};
use crate::{MartinError, Source, Xyz};

#[derive(Clone)]
pub struct MbtSource {
Expand Down Expand Up @@ -81,7 +81,11 @@ impl Source for MbtSource {
Box::new(self.clone())
}

async fn get_tile(&self, xyz: &Xyz, _url_query: &Option<UrlQuery>) -> Result<Tile, Error> {
async fn get_tile(
&self,
xyz: &Xyz,
_url_query: &Option<UrlQuery>,
) -> Result<Tile, MartinError> {
if let Some(tile) = self
.mbtiles
.get_tile(xyz.z, xyz.x, xyz.y)
Expand Down
8 changes: 6 additions & 2 deletions martin/src/pmtiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tilejson::TileJSON;
use crate::file_config::FileError;
use crate::file_config::FileError::{InvalidMetadata, IoError};
use crate::source::{Source, Tile, UrlQuery};
use crate::{Error, Xyz};
use crate::{MartinError, Xyz};

#[derive(Clone)]
pub struct PmtSource {
Expand Down Expand Up @@ -129,7 +129,11 @@ impl Source for PmtSource {
Box::new(self.clone())
}

async fn get_tile(&self, xyz: &Xyz, _url_query: &Option<UrlQuery>) -> Result<Tile, Error> {
async fn get_tile(
&self,
xyz: &Xyz,
_url_query: &Option<UrlQuery>,
) -> Result<Tile, MartinError> {
// TODO: optimize to return Bytes
if let Some(t) = self
.pmtiles
Expand Down
3 changes: 2 additions & 1 deletion martin/src/srv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pub use config::{SrvConfig, KEEP_ALIVE_DEFAULT, LISTEN_ADDRESSES_DEFAULT};

mod server;
pub use server::{
get_composite_tile, merge_tilejson, new_server, router, Catalog, RESERVED_KEYWORDS,
get_composite_tile, get_tile_impl, merge_tilejson, new_server, router, Catalog, TileRequest,
RESERVED_KEYWORDS,
};
Loading

0 comments on commit 12c662f

Please sign in to comment.