-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add hashmap-based directory cache traits
- Loading branch information
Showing
4 changed files
with
128 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::collections::HashMap; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use crate::directory::{Directory, Entry}; | ||
|
||
pub enum SearchResult { | ||
NotCached, | ||
NotFound, | ||
Found(Entry), | ||
} | ||
|
||
impl From<Option<&Entry>> for SearchResult { | ||
fn from(entry: Option<&Entry>) -> Self { | ||
match entry { | ||
Some(entry) => SearchResult::Found(entry.clone()), | ||
None => SearchResult::NotFound, | ||
} | ||
} | ||
} | ||
|
||
/// A cache for PMTiles directories. | ||
pub trait Cache { | ||
/// Get a directory from the cache, using the offset as a key. | ||
fn get_dir_entry(&self, offset: usize, tile_id: u64) -> SearchResult; | ||
|
||
/// 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); | ||
} | ||
|
||
pub struct NoCache; | ||
|
||
impl Cache for NoCache { | ||
#[inline] | ||
fn get_dir_entry(&self, _offset: usize, _tile_id: u64) -> SearchResult { | ||
SearchResult::NotCached | ||
} | ||
|
||
#[inline] | ||
fn insert_dir(&self, _offset: usize, _directory: Directory) {} | ||
} | ||
|
||
/// A simple HashMap-based implementation of a PMTiles directory cache. | ||
#[derive(Default)] | ||
pub struct HashMapCache { | ||
pub cache: Arc<Mutex<HashMap<usize, Directory>>>, | ||
} | ||
|
||
impl Cache for HashMapCache { | ||
fn get_dir_entry(&self, offset: usize, tile_id: u64) -> SearchResult { | ||
if let Some(dir) = self.cache.lock().unwrap().get(&offset) { | ||
return dir.find_tile_id(tile_id).into(); | ||
} | ||
SearchResult::NotCached | ||
} | ||
|
||
fn insert_dir(&self, offset: usize, directory: Directory) { | ||
self.cache.lock().unwrap().insert(offset, directory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters