Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion martin/src/bin/martin-cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,15 @@ async fn start(copy_args: CopierArgs) -> MartinCpResult<()> {
args.merge_into_config(&mut config, &env)?;
config.finalize()?;

let sources = config.resolve().await?;
if let Some(cache_size_mb) = config.cache_size_mb
&& cache_size_mb > 0
{
info!(
"Ignoring setting cache_size_mb={cache_size_mb} as caching does not make sense for tile copying"
);
}

let sources = config.resolve(None).await?;

if let Some(file_name) = save_config {
config.save_to_file(file_name)?;
Expand Down
7 changes: 5 additions & 2 deletions martin/src/bin/martin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Parser;
use log::{error, info, log_enabled};
use martin::args::Args;
use martin::srv::new_server;
use martin::{Config, MartinResult, read_config};
use martin::{Config, MartinResult, construct_cache, read_config};
use martin_core::config::env::OsEnv;

const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -22,7 +22,10 @@ async fn start(args: Args) -> MartinResult<()> {

args.merge_into_config(&mut config, &env)?;
config.finalize()?;
let sources = config.resolve().await?;

let sources = config
.resolve(construct_cache(config.cache_size_mb))
.await?;

if let Some(file_name) = save_config {
config.save_to_file(file_name)?;
Expand Down
25 changes: 2 additions & 23 deletions martin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::MartinError::{ConfigLoadError, ConfigParseError, ConfigWriteError, No
use crate::file_config::FileConfigEnum;
use crate::source::{TileInfoSources, TileSources};
use crate::srv::{RESERVED_KEYWORDS, SrvConfig};
use crate::utils::{CacheValue, MainCache, OptMainCache, init_aws_lc_tls, parse_base_path};
use crate::utils::{OptMainCache, init_aws_lc_tls, parse_base_path};
use crate::{IdResolver, MartinResult};

pub type UnrecognizedValues = HashMap<String, serde_yaml::Value>;
Expand Down Expand Up @@ -138,30 +138,9 @@ impl Config {
if is_empty { Err(NoSources) } else { Ok(res) }
}

pub async fn resolve(&mut self) -> MartinResult<ServerState> {
pub async fn resolve(&mut self, cache: OptMainCache) -> MartinResult<ServerState> {
init_aws_lc_tls()?;
let resolver = IdResolver::new(RESERVED_KEYWORDS);
let cache_size = self.cache_size_mb.unwrap_or(512) * 1024 * 1024;
let cache = if cache_size > 0 {
info!("Initializing main cache with maximum size {cache_size}B");
Some(
MainCache::builder()
.weigher(|_key, value: &CacheValue| -> u32 {
match value {
CacheValue::Tile(v) => v.len().try_into().unwrap_or(u32::MAX),
#[cfg(feature = "pmtiles")]
CacheValue::PmtDirectory(v) => {
v.get_approx_byte_size().try_into().unwrap_or(u32::MAX)
}
}
})
.max_capacity(cache_size)
.build(),
)
} else {
info!("Caching is disabled");
None
};

Ok(ServerState {
tiles: self.resolve_tile_sources(&resolver, cache.clone()).await?,
Expand Down
2 changes: 1 addition & 1 deletion martin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod source;
pub use source::{Source, TileData, TileInfoSource, TileSources, UrlQuery};

mod utils;
pub use utils::{IdResolver, MartinError, MartinResult, NO_MAIN_CACHE};
pub use utils::{IdResolver, MartinError, MartinResult, NO_MAIN_CACHE, construct_cache};

pub mod args;
#[cfg(feature = "cog")]
Expand Down
30 changes: 30 additions & 0 deletions martin/src/utils/cache.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use log::info;
use martin_tile_utils::TileCoord;
use moka::future::Cache;

Expand All @@ -7,6 +8,35 @@ pub type MainCache = Cache<CacheKey, CacheValue>;
pub type OptMainCache = Option<MainCache>;
pub const NO_MAIN_CACHE: OptMainCache = None;

/// Constructs a main cache with the specified size in megabytes.
///
/// If the size is zero, caching is disabled.
/// Logs initialization and capacity.
#[must_use]
pub fn construct_cache(cache_size_mb: Option<u64>) -> OptMainCache {
let cache_size = cache_size_mb.unwrap_or(512) * 1024 * 1024;
if cache_size > 0 {
info!("Initializing main cache with maximum size {cache_size}B");
Some(
MainCache::builder()
.weigher(|_key, value: &CacheValue| -> u32 {
match value {
CacheValue::Tile(v) => v.len().try_into().unwrap_or(u32::MAX),
#[cfg(feature = "pmtiles")]
CacheValue::PmtDirectory(v) => {
v.get_approx_byte_size().try_into().unwrap_or(u32::MAX)
}
}
})
.max_capacity(cache_size)
.build(),
)
} else {
info!("Caching is disabled");
None
}
}

#[derive(Debug, Hash, PartialEq, Eq)]
pub enum CacheKey {
/// (`pmtiles_id`, `offset`)
Expand Down
2 changes: 1 addition & 1 deletion martin/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub(crate) mod cache;
pub use cache::{CacheKey, CacheValue, MainCache, NO_MAIN_CACHE, OptMainCache};
pub use cache::{CacheKey, CacheValue, MainCache, NO_MAIN_CACHE, OptMainCache, construct_cache};

mod error;
pub use error::*;
Expand Down
4 changes: 2 additions & 2 deletions martin/tests/utils/pg_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use indoc::formatdoc;
use martin::{Config, ServerState, Source};
use martin::{Config, ServerState, Source, construct_cache};

use crate::mock_cfg;

Expand All @@ -21,7 +21,7 @@ pub fn mock_pgcfg(yaml: &str) -> Config {

#[allow(dead_code)]
pub async fn mock_sources(mut config: Config) -> MockSource {
let res = config.resolve().await;
let res = config.resolve(construct_cache(config.cache_size_mb)).await;
let res = res.unwrap_or_else(|e| panic!("Failed to resolve config {config:?}: {e}"));
(res, config)
}
Expand Down
Loading