Skip to content

Commit

Permalink
Make Cache trait compat with Moka
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Nov 10, 2023
1 parent 9cd05c8 commit 0909c54
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/async_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
// and it allows directory to be cached later without cloning it first.
let offset = (self.header.leaf_offset + entry.offset) as _;

let entry = match self.cache.get_dir_entry(offset, tile_id) {
let entry = match self.cache.get_dir_entry(offset, tile_id).await {
DirCacheResult::NotCached => {
// Cache miss - read from backend
let length = entry.length as _;
let dir = self.read_directory(offset, length).await.ok()?;
let entry = dir.find_tile_id(tile_id).cloned();
self.cache.insert_dir(offset, dir);
self.cache.insert_dir(offset, dir).await;
entry
}
DirCacheResult::NotFound => None,
Expand Down
17 changes: 11 additions & 6 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use async_trait::async_trait;

use crate::directory::{DirEntry, Directory};

pub enum DirCacheResult {
Expand All @@ -19,25 +21,27 @@ impl From<Option<&DirEntry>> for DirCacheResult {
}

/// A cache for PMTiles directories.
#[async_trait]
pub trait DirectoryCache {
/// Get a directory from the cache, using the offset as a key.
fn get_dir_entry(&self, offset: usize, tile_id: u64) -> DirCacheResult;
async fn get_dir_entry(&self, offset: usize, tile_id: u64) -> DirCacheResult;

/// Insert a directory into the cache, using the offset as a key.
/// Note that cache must be internally mutable.
fn insert_dir(&self, offset: usize, directory: Directory);
async fn insert_dir(&self, offset: usize, directory: Directory);
}

pub struct NoCache;

#[async_trait]
impl DirectoryCache for NoCache {
#[inline]
fn get_dir_entry(&self, _offset: usize, _tile_id: u64) -> DirCacheResult {
async fn get_dir_entry(&self, _offset: usize, _tile_id: u64) -> DirCacheResult {
DirCacheResult::NotCached
}

#[inline]
fn insert_dir(&self, _offset: usize, _directory: Directory) {}
async fn insert_dir(&self, _offset: usize, _directory: Directory) {}
}

/// A simple HashMap-based implementation of a PMTiles directory cache.
Expand All @@ -46,15 +50,16 @@ pub struct HashMapCache {
pub cache: Arc<Mutex<HashMap<usize, Directory>>>,
}

#[async_trait]
impl DirectoryCache for HashMapCache {
fn get_dir_entry(&self, offset: usize, tile_id: u64) -> DirCacheResult {
async fn get_dir_entry(&self, offset: usize, tile_id: u64) -> DirCacheResult {
if let Some(dir) = self.cache.lock().unwrap().get(&offset) {
return dir.find_tile_id(tile_id).into();
}
DirCacheResult::NotCached
}

fn insert_dir(&self, offset: usize, directory: Directory) {
async fn insert_dir(&self, offset: usize, directory: Directory) {
self.cache.lock().unwrap().insert(offset, directory);
}
}
1 change: 1 addition & 0 deletions src/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use varint_rs::VarintReader;

use crate::error::Error;

#[derive(Clone)]
pub struct Directory {
entries: Vec<DirEntry>,
}
Expand Down

0 comments on commit 0909c54

Please sign in to comment.