Skip to content

Commit

Permalink
Add Moka cache support
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Nov 30, 2023
1 parent 634a395 commit ba833e4
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 30 deletions.
167 changes: 167 additions & 0 deletions 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 @@ -50,6 +50,7 @@ json-patch = "1.2"
log = "0.4"
martin-tile-utils = { path = "./martin-tile-utils", version = "0.1.0" }
mbtiles = { path = "./mbtiles", version = "0.8.0" }
moka = { version = "0.12", features = ["future"] }
num_cpus = "1"
pbf_font_tools = { version = "2.5.0", features = ["freetype"] }
pmtiles = { version = "0.5", features = ["http-async", "mmap-async-tokio", "tilejson"] }
Expand Down
1 change: 1 addition & 0 deletions martin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ json-patch.workspace = true
log.workspace = true
martin-tile-utils.workspace = true
mbtiles.workspace = true
moka.workspace = true
num_cpus.workspace = true
pbf_font_tools.workspace = true
pmtiles.workspace = true
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 @@ -40,7 +40,7 @@ pub enum FileError {
AquireConnError(String),

#[error(r#"PMTiles error {0} processing {1}"#)]
PmtError(pmtiles::error::Error, String),
PmtError(pmtiles::PmtError, String),
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -178,7 +178,7 @@ pub struct FileConfigSource {
pub path: PathBuf,
}

async fn dummy_resolver(_id: String, _url: Url) -> Result<Box<dyn Source>, FileError> {
async fn dummy_resolver(_id: String, _url: Url) -> FileResult<Box<dyn Source>> {
unreachable!()
}

Expand All @@ -203,13 +203,13 @@ pub async fn resolve_files_urls<Fut1, Fut2>(
extension: &str,
new_source: &mut impl FnMut(String, PathBuf) -> Fut1,
new_url_source: &mut impl FnMut(String, Url) -> Fut2,
) -> Result<TileInfoSources, Error>
) -> MartinResult<TileInfoSources>
where
Fut1: Future<Output = Result<Box<dyn Source>, FileError>>,
Fut2: Future<Output = Result<Box<dyn Source>, FileError>>,
{
resolve_int(config, idr, extension, true, new_source, new_url_source)
.map_err(crate::Error::from)
.map_err(crate::MartinError::from)
.await
}

Expand Down
22 changes: 13 additions & 9 deletions martin/src/pmtiles/file_pmtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use pmtiles::mmap::MmapBackend;
use pmtiles::{Compression, TileType};
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::file_config::FileResult;
use crate::source::{Source, UrlQuery};
use crate::{MartinResult, TileCoord, TileData};

#[derive(Clone)]
pub struct PmtFileSource {
Expand All @@ -36,11 +36,11 @@ impl Debug for PmtFileSource {
}

impl PmtFileSource {
pub async fn new_box(id: String, path: PathBuf) -> Result<Box<dyn Source>, FileError> {
pub async fn new_box(id: String, path: PathBuf) -> FileResult<Box<dyn Source>> {
Ok(Box::new(PmtFileSource::new(id, path).await?))
}

async fn new(id: String, path: PathBuf) -> Result<Self, FileError> {
async fn new(id: String, path: PathBuf) -> FileResult<Self> {
let backend = MmapBackend::try_from(path.as_path())
.await
.map_err(|e| {
Expand Down Expand Up @@ -70,8 +70,8 @@ impl PmtFileSource {
id: String,
path: PathBuf,
reader: AsyncPmTilesReader<MmapBackend>,
) -> Result<Self, FileError> {
let hdr = &reader.header;
) -> FileResult<Self> {
let hdr = &reader.get_header();

if hdr.tile_type != TileType::Mvt && hdr.tile_compression != Compression::None {
return Err(InvalidMetadata(
Expand Down Expand Up @@ -145,14 +145,18 @@ impl Source for PmtFileSource {
Box::new(self.clone())
}

async fn get_tile(&self, xyz: &Xyz, _url_query: &Option<UrlQuery>) -> Result<Tile, Error> {
async fn get_tile(
&self,
xyz: &TileCoord,
_url_query: &Option<UrlQuery>,
) -> MartinResult<TileData> {
// TODO: optimize to return Bytes
if let Some(t) = self
.pmtiles
.get_tile(xyz.z, u64::from(xyz.x), u64::from(xyz.y))
.await
{
Ok(t.data.to_vec())
Ok(t.to_vec())
} else {
trace!(
"Couldn't find tile data in {}/{}/{} of {}",
Expand Down
Loading

0 comments on commit ba833e4

Please sign in to comment.