Skip to content

Commit

Permalink
change all catalog-types to use dashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Nov 16, 2024
1 parent 321af56 commit 8b6c7d0
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 166 deletions.
18 changes: 17 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ tokio = { version = "1", features = ["macros"] }
tokio-postgres-rustls = "0.13"
url = "2.5"
xxhash-rust = { version = "0.8", features = ["xxh3"] }
dashmap = { version = "6.1.0", features = ["inline", "serde"] }

[profile.dev.package]
# See https://github.com/launchbadge/sqlx#compile-time-verification
Expand Down
1 change: 1 addition & 0 deletions martin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ tilejson.workspace = true
tokio = { workspace = true, features = ["io-std"] }
tokio-postgres-rustls = { workspace = true, optional = true }
url.workspace = true
dashmap.workspace = true

[build-dependencies]
static-files = { workspace = true, optional = true }
Expand Down
4 changes: 2 additions & 2 deletions martin/src/bin/martin-cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ async fn run_tile_copy(args: CopyArgs, state: ServerState) -> MartinCpResult<()>
} else {
CopyDuplicateMode::Override
};
let mbt_type = init_schema(&mbt, &mut conn, src.sources.as_slice(), src.info, &args).await?;
let mbt_type = init_schema(&mbt, &mut conn, &src.sources, src.info, &args).await?;

let progress = Progress::new(&tiles);
info!(
Expand Down Expand Up @@ -392,7 +392,7 @@ fn parse_encoding(encoding: &str) -> MartinCpResult<AcceptEncoding> {
async fn init_schema(
mbt: &Mbtiles,
conn: &mut SqliteConnection,
sources: &[&dyn Source],
sources: &[Box<dyn Source>],
tile_info: TileInfo,
args: &CopyArgs,
) -> Result<MbtType, MartinError> {
Expand Down
53 changes: 27 additions & 26 deletions martin/src/fonts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::collections::hash_map::Entry;
use std::collections::{BTreeMap, HashMap};
use std::ffi::OsStr;
use std::fmt::Debug;
use std::path::PathBuf;
use std::sync::OnceLock;

use bit_set::BitSet;
use dashmap::DashMap;
use itertools::Itertools as _;
use log::{debug, info, warn};
use pbf_font_tools::freetype::{Face, Library};
Expand Down Expand Up @@ -103,11 +102,11 @@ fn get_available_codepoints(face: &mut Face) -> Option<GetGlyphInfo> {

#[derive(Debug, Clone, Default)]
pub struct FontSources {
fonts: HashMap<String, FontSource>,
fonts: DashMap<String, FontSource>,
masks: Vec<BitSet>,
}

pub type FontCatalog = BTreeMap<String, CatalogFontEntry>;
pub type FontCatalog = DashMap<String, CatalogFontEntry>;

#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
Expand All @@ -125,7 +124,7 @@ impl FontSources {
return Ok(Self::default());
}

let mut fonts = HashMap::new();
let mut fonts = DashMap::new();
let lib = Library::init()?;

for path in config.iter() {
Expand All @@ -150,7 +149,7 @@ impl FontSources {
pub fn get_catalog(&self) -> FontCatalog {
self.fonts
.iter()
.map(|(k, v)| (k.clone(), v.catalog_entry.clone()))
.map(|v| (v.key().clone(), v.catalog_entry.clone()))
.sorted_by(|(a, _), (b, _)| a.cmp(b))
.collect()
}
Expand Down Expand Up @@ -242,7 +241,7 @@ pub struct FontSource {
fn recurse_dirs(
lib: &Library,
path: PathBuf,
fonts: &mut HashMap<String, FontSource>,
fonts: &mut DashMap<String, FontSource>,
is_top_level: bool,
) -> FontResult<()> {
let start_count = fonts.len();
Expand Down Expand Up @@ -275,7 +274,7 @@ fn recurse_dirs(

fn parse_font(
lib: &Library,
fonts: &mut HashMap<String, FontSource>,
fonts: &mut DashMap<String, FontSource>,
path: PathBuf,
) -> FontResult<()> {
static RE_SPACES: OnceLock<Regex> = OnceLock::new();
Expand All @@ -301,29 +300,28 @@ fn parse_font(
.replace_all(name.as_str(), " ")
.to_string();

match fonts.entry(name) {
Entry::Occupied(v) => {
match fonts.get(&name) {
Some(v) => {
warn!(
"Ignoring duplicate font {} from {} because it was already configured from {}",
v.key(),
path.display(),
v.get().path.display()
v.path.display()
);
}
Entry::Vacant(v) => {
let key = v.key();
None => {
let Some((codepoints, glyphs, ranges, start, end)) =
get_available_codepoints(&mut face)
else {
warn!(
"Ignoring font {key} from {} because it has no available glyphs",
"Ignoring font {name} from {} because it has no available glyphs",
path.display()
);
continue;
};

info!(
"Configured font {key} with {glyphs} glyphs ({start:04X}-{end:04X}) from {}",
"Configured font {name} with {glyphs} glyphs ({start:04X}-{end:04X}) from {}",
path.display()
);
debug!(
Expand All @@ -338,18 +336,21 @@ fn parse_font(
.join(", "),
);

v.insert(FontSource {
path: path.clone(),
face_index,
codepoints,
catalog_entry: CatalogFontEntry {
family,
style,
glyphs,
start,
end,
fonts.insert(
name,
FontSource {
path: path.clone(),
face_index,
codepoints,
catalog_entry: CatalogFontEntry {
family,
style,
glyphs,
start,
end,
},
},
});
);
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions martin/src/source.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::collections::{BTreeMap, HashMap};
use std::collections::HashMap;
use std::fmt::Debug;

use actix_web::error::ErrorNotFound;
use async_trait::async_trait;
use dashmap::DashMap;
use log::debug;
use martin_tile_utils::{TileCoord, TileInfo};
use serde::{Deserialize, Serialize};
Expand All @@ -18,8 +19,8 @@ pub type TileInfoSource = Box<dyn Source>;
pub type TileInfoSources = Vec<TileInfoSource>;

#[derive(Default, Clone)]
pub struct TileSources(HashMap<String, Box<dyn Source>>);
pub type TileCatalog = BTreeMap<String, CatalogSourceEntry>;
pub struct TileSources(DashMap<String, TileInfoSource>);
pub type TileCatalog = DashMap<String, CatalogSourceEntry>;

impl TileSources {
#[must_use]
Expand All @@ -37,16 +38,17 @@ impl TileSources {
pub fn get_catalog(&self) -> TileCatalog {
self.0
.iter()
.map(|(id, src)| (id.to_string(), src.get_catalog_entry()))
.map(|v| (v.key().to_string(), v.get_catalog_entry()))
.collect()
}

pub fn get_source(&self, id: &str) -> actix_web::Result<&dyn Source> {
pub fn get_source(&self, id: &str) -> actix_web::Result<TileInfoSource> {
Ok(self
.0
.get(id)
.ok_or_else(|| ErrorNotFound(format!("Source {id} does not exist")))?
.as_ref())
.value()
.clone())
}

/// Get a list of sources, and the tile info for the merged sources.
Expand All @@ -56,7 +58,7 @@ impl TileSources {
&self,
source_ids: &str,
zoom: Option<u8>,
) -> actix_web::Result<(Vec<&dyn Source>, bool, TileInfo)> {
) -> actix_web::Result<(Vec<TileInfoSource>, bool, TileInfo)> {
let mut sources = Vec::new();
let mut info: Option<TileInfo> = None;
let mut use_url_query = false;
Expand All @@ -78,7 +80,7 @@ impl TileSources {

// TODO: Use chained-if-let once available
if match zoom {
Some(zoom) if Self::check_zoom(src, id, zoom) => true,
Some(zoom) if Self::check_zoom(&*src, id, zoom) => true,
None => true,
_ => false,
} {
Expand Down Expand Up @@ -107,7 +109,7 @@ pub trait Source: Send + Debug {

fn get_tile_info(&self) -> TileInfo;

fn clone_source(&self) -> Box<dyn Source>;
fn clone_source(&self) -> TileInfoSource;

fn support_url_query(&self) -> bool {
false
Expand Down Expand Up @@ -139,7 +141,7 @@ pub trait Source: Send + Debug {
}
}

impl Clone for Box<dyn Source> {
impl Clone for TileInfoSource {
fn clone(&self) -> Self {
self.clone_source()
}
Expand Down
Loading

0 comments on commit 8b6c7d0

Please sign in to comment.