Skip to content

Commit 0909c54

Browse files
committed
Make Cache trait compat with Moka
1 parent 9cd05c8 commit 0909c54

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

src/async_reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
157157
// and it allows directory to be cached later without cloning it first.
158158
let offset = (self.header.leaf_offset + entry.offset) as _;
159159

160-
let entry = match self.cache.get_dir_entry(offset, tile_id) {
160+
let entry = match self.cache.get_dir_entry(offset, tile_id).await {
161161
DirCacheResult::NotCached => {
162162
// Cache miss - read from backend
163163
let length = entry.length as _;
164164
let dir = self.read_directory(offset, length).await.ok()?;
165165
let entry = dir.find_tile_id(tile_id).cloned();
166-
self.cache.insert_dir(offset, dir);
166+
self.cache.insert_dir(offset, dir).await;
167167
entry
168168
}
169169
DirCacheResult::NotFound => None,

src/cache.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::collections::HashMap;
22
use std::sync::{Arc, Mutex};
33

4+
use async_trait::async_trait;
5+
46
use crate::directory::{DirEntry, Directory};
57

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

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

2629
/// Insert a directory into the cache, using the offset as a key.
2730
/// Note that cache must be internally mutable.
28-
fn insert_dir(&self, offset: usize, directory: Directory);
31+
async fn insert_dir(&self, offset: usize, directory: Directory);
2932
}
3033

3134
pub struct NoCache;
3235

36+
#[async_trait]
3337
impl DirectoryCache for NoCache {
3438
#[inline]
35-
fn get_dir_entry(&self, _offset: usize, _tile_id: u64) -> DirCacheResult {
39+
async fn get_dir_entry(&self, _offset: usize, _tile_id: u64) -> DirCacheResult {
3640
DirCacheResult::NotCached
3741
}
3842

3943
#[inline]
40-
fn insert_dir(&self, _offset: usize, _directory: Directory) {}
44+
async fn insert_dir(&self, _offset: usize, _directory: Directory) {}
4145
}
4246

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

53+
#[async_trait]
4954
impl DirectoryCache for HashMapCache {
50-
fn get_dir_entry(&self, offset: usize, tile_id: u64) -> DirCacheResult {
55+
async fn get_dir_entry(&self, offset: usize, tile_id: u64) -> DirCacheResult {
5156
if let Some(dir) = self.cache.lock().unwrap().get(&offset) {
5257
return dir.find_tile_id(tile_id).into();
5358
}
5459
DirCacheResult::NotCached
5560
}
5661

57-
fn insert_dir(&self, offset: usize, directory: Directory) {
62+
async fn insert_dir(&self, offset: usize, directory: Directory) {
5863
self.cache.lock().unwrap().insert(offset, directory);
5964
}
6065
}

src/directory.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use varint_rs::VarintReader;
55

66
use crate::error::Error;
77

8+
#[derive(Clone)]
89
pub struct Directory {
910
entries: Vec<DirEntry>,
1011
}

0 commit comments

Comments
 (0)